Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for when active rehashing kvstore dictionaries are replaced by defrag #1512

Merged
merged 1 commit into from
Jan 6, 2025

Conversation

ranshid
Copy link
Member

@ranshid ranshid commented Jan 6, 2025

The kvstore operates the active rehashing by traversing a list of dictionaries which were registered to it when they started rehashing.

uint64_t kvstoreIncrementallyRehash(kvstore *kvs, uint64_t threshold_us) {
    if (listLength(kvs->rehashing) == 0) return 0;
 
    /* Our goal is to rehash as many dictionaries as we can before reaching threshold_us,
     * after each dictionary completes rehashing, it removes itself from the list. */
    listNode *node;
    monotime timer;
    uint64_t elapsed_us = 0;
    elapsedStart(&timer);
    while ((node = listFirst(kvs->rehashing))) {
        dictRehashMicroseconds(listNodeValue(node), threshold_us - elapsed_us);
 
        elapsed_us = elapsedUs(timer);
        if (elapsed_us >= threshold_us) {
            break; /* Reached the time limit. */
        }
    }
    return elapsed_us;
}

the problem is that active defrag may realloc some of the dictionary structures while they are registered on the list:

void activeDefragCycle(void) {
...
db = &server.db[current_db];
kvstoreDictLUTDefrag(db->keys, dictDefragTables);
kvstoreDictLUTDefrag(db->expires, dictDefragTables);

where the dictionary might be relocated in dictDefragTables:

dict *dictDefragTables(dict *d) {
    dict *ret = NULL;
    dictEntry **newtable;
    /* handle the dict struct */
    if ((ret = activeDefragAlloc(d))) d = ret; <---- HERE
    /* handle the first hash table */
    if (!d->ht_table[0]) return ret; /* created but unused */
    newtable = activeDefragAlloc(d->ht_table[0]);
    if (newtable) d->ht_table[0] = newtable;
    /* handle the second hash table */
    if (d->ht_table[1]) {
        newtable = activeDefragAlloc(d->ht_table[1]);
        if (newtable) d->ht_table[1] = newtable;
    }
    return ret;
}

The Solution is to make sure we update the rehashing list node withy the new(or not) dictionary pointer after applying the defrag function.

@ranshid ranshid requested a review from madolson January 6, 2025 18:54
@madolson madolson added the release-notes This issue should get a line item in the release notes label Jan 6, 2025
@madolson madolson merged commit f1a02b4 into valkey-io:8.0 Jan 6, 2025
54 of 60 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-notes This issue should get a line item in the release notes
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants