From 6c510bc5cb84e8abca13e52d0e0b6c7a6d6c5d35 Mon Sep 17 00:00:00 2001 From: John Sully Date: Sat, 29 May 2021 00:24:46 +0000 Subject: [PATCH] Repair async rehash code removed by merge Former-commit-id: 14a3ab8133c474bf8f1ad636c8699d5d59c5394e --- src/ae.cpp | 4 +++- src/dict.cpp | 14 ++++++++++---- src/server.cpp | 11 +++++++++++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/ae.cpp b/src/ae.cpp index d96eee7bc..405f89537 100644 --- a/src/ae.cpp +++ b/src/ae.cpp @@ -814,7 +814,9 @@ int aeThreadOwnsLock() int aeLockContested(int threshold) { - return g_lock.m_ticket.m_active < static_cast(g_lock.m_ticket.m_avail - threshold); + ticket ticketT; + __atomic_load(&g_lock.m_ticket.u, &ticketT.u, __ATOMIC_RELAXED); + return ticketT.m_active < static_cast(ticketT.m_avail - threshold); } int aeLockContention() diff --git a/src/dict.cpp b/src/dict.cpp index 65fe1e951..57872b5c4 100644 --- a/src/dict.cpp +++ b/src/dict.cpp @@ -350,6 +350,7 @@ int dictTryExpand(dict *d, unsigned long size, bool fShrink) { int dictRehash(dict *d, int n) { int empty_visits = n*10; /* Max number of empty buckets to visit. */ if (!dictIsRehashing(d)) return 0; + if (d->asyncdata) return 0; while(n-- && d->ht[0].used != 0) { dictEntry *de, *nextde; @@ -694,7 +695,7 @@ static dictEntry *dictGenericDelete(dict *d, const void *key, int nofree) { if (!nofree) { if (table == 0 && d->asyncdata != nullptr && (ssize_t)idx < d->rehashidx) { he->next = d->asyncdata->deGCList; - d->asyncdata->deGCList = he->next; + d->asyncdata->deGCList = he; } else { dictFreeKey(d, he); dictFreeVal(d, he); @@ -750,9 +751,14 @@ dictEntry *dictUnlink(dict *ht, const void *key) { void dictFreeUnlinkedEntry(dict *d, dictEntry *he) { if (he == NULL) return; - dictFreeKey(d, he); - dictFreeVal(d, he); - zfree(he); + if (d->asyncdata) { + he->next = d->asyncdata->deGCList; + d->asyncdata->deGCList = he; + } else { + dictFreeKey(d, he); + dictFreeVal(d, he); + zfree(he); + } } /* Destroy an entire dictionary */ diff --git a/src/server.cpp b/src/server.cpp index 5237ec5f8..1613d8a42 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -2059,6 +2059,11 @@ bool expireOwnKeys() return false; } +int hash_spin_worker() { + auto ctl = serverTL->rehashCtl; + return dictRehashSomeAsync(ctl, 1); +} + /* This function handles 'background' operations we are required to do * incrementally in Redis databases, such as active key expiring, resizing, * rehashing. */ @@ -2154,6 +2159,12 @@ void databasesCron(bool fMainThread) { } } } + + if (serverTL->rehashCtl) { + setAeLockSetThreadSpinWorker(hash_spin_worker); + } else { + setAeLockSetThreadSpinWorker(nullptr); + } } /* We take a cached value of the unix time in the global state because with