• zhaozhao.zz's avatar
    fix scripts access wrong slot if they disagree with pre-declared keys (#12906) · bb2b6e29
    zhaozhao.zz authored
    Regarding how to obtain the hash slot of a key, there is an optimization
    in `getKeySlot()`, it is used to avoid redundant hash calculations for
    keys: when the current client is in the process of executing a command,
    it can directly use the slot of the current client because the slot to
    access has already been calculated in advance in `processCommand()`.
    
    However, scripts are a special case where, in default mode or with
    `allow-cross-slot-keys` enabled, they are allowed to access keys beyond
    the pre-declared range. This means that the keys they operate on may not
    belong to the slot of the pre-declared keys. Currently, when the
    commands in a script are executed, the slot of the original client
    (i.e., the current client) is not correctly updated, leading to
    subsequent access to the wrong slot.
    
    This PR fixes the above issue. When checking the cluster constraints in
    a script, the slot to be accessed by the current command is set for the
    original client (i.e., the current client). This ensures that
    `getKeySlot()` gets the correct slot cache.
    
    Additionally, the following modifications are made:
    
    1. The 'sort' and 'sort_ro' commands use `getKeySlot()` instead of
    `c->slot` because the client could be an engine client in a script and
    can lead to potential bug.
    2. `getKeySlot()` is also used in pubsub to obtain the slot for the
    channel, standardizing the way slots are retrieved.
    bb2b6e29
script.h 4.73 KB