diff --git a/src/db.c b/src/db.c index 60c5d208ae..8647d629d6 100644 --- a/src/db.c +++ b/src/db.c @@ -58,6 +58,7 @@ int keyIsExpiredWithSlot(serverDb *db, robj *key, int slot); int keyIsExpired(serverDb *db, robj *key); static void dbSetValue(serverDb *db, robj *key, robj *val, int overwrite, dictEntry *de); static int getKVStoreIndexForKey(sds key); +dictEntry *dbFindExpiresWithSlot(serverDb *db, void *key, int slot); /* Update LFU when an object is accessed. * Firstly, decrement the counter if the decrement time is reached. @@ -439,8 +440,8 @@ int dbGenericDeleteWithSlot(serverDb *db, robj *key, int async, int flags, int s /* Helper for sync and async delete. */ int dbGenericDelete(serverDb *db, robj *key, int async, int flags) { - int slot = server.cluster_enabled ? getKeySlot(key->ptr) : 0; - return dbGenericDeleteWithSlot(db, key, async, flags, slot); + int dict_index = getKVStoreIndexForKey(key->ptr); + return dbGenericDeleteWithSlot(db, key, async, flags, dict_index); } /* Delete a key, value, and associated expiration entry if any, from the DB */ @@ -1711,8 +1712,8 @@ long long getExpireWithSlot(serverDb *db, robj *key, int slot) { /* Return the expire time of the specified key, or -1 if no expire * is associated with this key (i.e. the key is non volatile) */ long long getExpire(serverDb *db, robj *key) { - int slot = server.cluster_enabled ? getKeySlot(key->ptr) : 0; - return getExpireWithSlot(db, key, slot); + int dict_index = getKVStoreIndexForKey(key->ptr); + return getExpireWithSlot(db, key, dict_index); } void deleteExpiredKeyAndPropagateWithSlot(serverDb *db, robj *keyobj, int slot) { @@ -1802,8 +1803,8 @@ int keyIsExpiredWithSlot(serverDb *db, robj *key, int slot) { /* Check if the key is expired. */ int keyIsExpired(serverDb *db, robj *key) { - int slot = server.cluster_enabled ? getKeySlot(key->ptr) : 0; - return keyIsExpiredWithSlot(db, key, slot); + int dict_index = getKVStoreIndexForKey(key->ptr); + return keyIsExpiredWithSlot(db, key, dict_index); } keyStatus expireIfNeededWithSlot(serverDb *db, robj *key, int flags, int slot) { @@ -1881,8 +1882,8 @@ keyStatus expireIfNeededWithSlot(serverDb *db, robj *key, int flags, int slot) { * The function returns KEY_EXPIRED if the key is expired BUT not deleted, * or returns KEY_DELETED if the key is expired and deleted. */ keyStatus expireIfNeeded(serverDb *db, robj *key, int flags) { - int slot = server.cluster_enabled ? getKeySlot(key->ptr) : 0; - return expireIfNeededWithSlot(db, key, flags, slot); + int dict_index = getKVStoreIndexForKey(key->ptr); + return expireIfNeededWithSlot(db, key, flags, dict_index); } /* CB passed to kvstoreExpand. diff --git a/src/server.h b/src/server.h index b892364770..29d675bb18 100644 --- a/src/server.h +++ b/src/server.h @@ -3338,7 +3338,6 @@ int calculateKeySlot(sds key); int dbExpand(serverDb *db, uint64_t db_size, int try_expand); int dbExpandExpires(serverDb *db, uint64_t db_size, int try_expand); dictEntry *dbFind(serverDb *db, void *key); -dictEntry *dbFindExpiresWithSlot(serverDb *db, void *key, int slot); dictEntry *dbFindExpires(serverDb *db, void *key); unsigned long long dbSize(serverDb *db); unsigned long long dbScan(serverDb *db, unsigned long long cursor, dictScanFunction *scan_cb, void *privdata); diff --git a/tests/unit/cluster/multi-slot-operations.tcl b/tests/unit/cluster/multi-slot-operations.tcl index a425a3fcdd..fe3246a3fa 100644 --- a/tests/unit/cluster/multi-slot-operations.tcl +++ b/tests/unit/cluster/multi-slot-operations.tcl @@ -107,15 +107,3 @@ test "DELSLOTSRANGE command with several boundary conditions test suite" { assert_match "*9829 11000*12001 12100*12201 13104*" [$master4 CLUSTER SLOTS] } } cluster_allocate_with_continuous_slots_local - -start_cluster 1 0 {tags {external:skip cluster}} { - test "Regression test for multi-exec with RANDOMKEY accessing the wrong per-slot dictionary" { - R 0 SETEX FOO 10000 BAR - R 0 SETEX FIZZ 10000 BUZZ - - R 0 MULTI - R 0 GET FOO - R 0 RANDOMKEY - R 0 EXEC - } -} diff --git a/tests/unit/multi.tcl b/tests/unit/multi.tcl index dafbc66c10..ffdaa3edd4 100644 --- a/tests/unit/multi.tcl +++ b/tests/unit/multi.tcl @@ -919,3 +919,15 @@ start_server {overrides {appendonly {yes} appendfilename {appendonly.aof} append r get foo } {} } + +start_cluster 1 0 {tags {"external:skip cluster"}} { + test "Regression test for multi-exec with RANDOMKEY accessing the wrong per-slot dictionary" { + R 0 SETEX FOO 10000 BAR + R 0 SETEX FIZZ 10000 BUZZ + + R 0 MULTI + R 0 DEL FOO + R 0 RANDOMKEY + assert_equal [R 0 EXEC] {1 FIZZ} + } +}