Skip to content

Commit

Permalink
Fix resize hash tables stuck on the last non-empty slot
Browse files Browse the repository at this point in the history
  • Loading branch information
soloestoy committed Nov 23, 2023
1 parent 4634769 commit 58e2bf2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ long long emptyDbStructure(redisDb *dbarray, int dbnum, int async,
dbarray[j].expires_cursor = 0;
for (dbKeyType subdict = DB_MAIN; subdict <= DB_EXPIRES; subdict++) {
dbarray[j].sub_dict[subdict].key_count = 0;
dbarray[j].sub_dict[subdict].resize_cursor = 0;
dbarray[j].sub_dict[subdict].resize_cursor = -1;
if (server.cluster_enabled) {
if (dbarray[j].sub_dict[subdict].rehashing)
listEmpty(dbarray[j].sub_dict[subdict].rehashing);
Expand Down
25 changes: 12 additions & 13 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -641,20 +641,19 @@ int htNeedsResize(dict *dict) {
* In non cluster-enabled setup, it resize main/expires dictionary based on the same condition described above. */
void tryResizeHashTables(int dbid) {
redisDb *db = &server.db[dbid];
int slot = 0;
for (dbKeyType subdict = DB_MAIN; subdict <= DB_EXPIRES; subdict++) {
dbIterator *dbit = dbIteratorInitFromSlot(db, subdict, db->sub_dict[subdict].resize_cursor);
for (int i = 0; i < CRON_DBS_PER_CALL; i++) {
dict *d = dbGetDictFromIterator(dbit);
slot = dbIteratorGetCurrentSlot(dbit);
dbIteratorNextDict(dbit);
if (!d) break;
if (htNeedsResize(d))
dictResize(d);
if (dbSize(db, subdict)) {
if (db->sub_dict[subdict].resize_cursor == -1)
db->sub_dict[subdict].resize_cursor = findSlotByKeyIndex(db, 1, subdict);

for (int i = 0; i < CRON_DBS_PER_CALL && db->sub_dict[subdict].resize_cursor != -1; i++) {
int slot = db->sub_dict[subdict].resize_cursor;
dict *d = subdict == DB_MAIN ? db->dict[slot] : db->expires[slot];
if (htNeedsResize(d))
dictResize(d);
db->sub_dict[subdict].resize_cursor = dbGetNextNonEmptySlot(db, slot, subdict);
}
}
/* Save current iterator position in the resize_cursor. */
db->sub_dict[subdict].resize_cursor = slot;
dbReleaseIterator(dbit);
}
}

Expand Down Expand Up @@ -2637,7 +2636,7 @@ void initDbState(redisDb *db){
for (dbKeyType subdict = DB_MAIN; subdict <= DB_EXPIRES; subdict++) {
db->sub_dict[subdict].rehashing = listCreate();
db->sub_dict[subdict].key_count = 0;
db->sub_dict[subdict].resize_cursor = 0;
db->sub_dict[subdict].resize_cursor = -1;
db->sub_dict[subdict].slot_size_index = server.cluster_enabled ? zcalloc(sizeof(unsigned long long) * (CLUSTER_SLOTS + 1)) : NULL;
db->sub_dict[subdict].bucket_count = 0;
}
Expand Down

0 comments on commit 58e2bf2

Please sign in to comment.