Commit 2783aa1d authored by Vitaly Arbuzov's avatar Vitaly Arbuzov
Browse files

Fix randomkey NPE when DB is empty in cluster mode

parent c45570f4
...@@ -394,7 +394,8 @@ dict *getRandomDict(redisDb *db) { ...@@ -394,7 +394,8 @@ dict *getRandomDict(redisDb *db) {
target |= rand(); target |= rand();
} }
unsigned long long int key_count = dbSize(db); unsigned long long int key_count = dbSize(db);
target = key_count ? target % key_count : 0; /* Random key index in range [0..KEY_COUNT), or 0 if db is empty. */ if (!key_count) return db->dict[0];
target %= key_count; /* Random key index in range [0..KEY_COUNT). */
/* In linearly enumerated key space, find a slot that contains target key. */ /* In linearly enumerated key space, find a slot that contains target key. */
dbIterator dbit; dbIterator dbit;
dbInitIterator(&dbit, db); dbInitIterator(&dbit, db);
...@@ -402,11 +403,11 @@ dict *getRandomDict(redisDb *db) { ...@@ -402,11 +403,11 @@ dict *getRandomDict(redisDb *db) {
while ((d = dbNextDict(&dbit))) { while ((d = dbNextDict(&dbit))) {
unsigned long long int ds = dictSize(d); unsigned long long int ds = dictSize(d);
if (target < ds) { /* Found dict that contains target key. */ if (target < ds) { /* Found dict that contains target key. */
break; return d;
} }
target -= ds; target -= ds;
} }
return d; serverPanic("Bug in random dict selection, iteration completed without finding a slot for the target element.");
} }
/* Helper for sync and async delete. */ /* Helper for sync and async delete. */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment