From 8876d264ac9ad132dbabf70e6d6ba8c88c583e87 Mon Sep 17 00:00:00 2001 From: "zhaozhao.zz" Date: Mon, 19 Feb 2024 20:29:54 +0800 Subject: [PATCH] Calculate the incremental rehash time more precisely (#13063) In the `databasesCron()`, the time consumed by `kvstoreIncrementallyRehash()` is used to calculate the exit condition. However, within `kvstoreIncrementallyRehash()`, the loop first checks for timeout before performing rehashing. Therefore, the time for the last rehash isn't accounted for, making the consumed time inaccurate. We need to precisely calculate all the time spent on rehashing. Additionally, the time allocated to `kvstoreIncrementallyRehash()` should be the remaining time, which is `INCREMENTAL_REHASHING_THRESHOLD_US` minus the already consumed `elapsed_us`. --- src/kvstore.c | 6 +++--- src/server.c | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/kvstore.c b/src/kvstore.c index d4afb1825..9e3061ba8 100644 --- a/src/kvstore.c +++ b/src/kvstore.c @@ -615,16 +615,16 @@ uint64_t kvstoreIncrementallyRehash(kvstore *kvs, uint64_t threshold_us) { * after each dictionary completes rehashing, it removes itself from the list. */ listNode *node; monotime timer; - uint64_t elapsed_us = UINT64_MAX; + 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. */ } - dictRehashMicroseconds(listNodeValue(node), threshold_us - elapsed_us); } - assert(elapsed_us != UINT64_MAX); return elapsed_us; } diff --git a/src/server.c b/src/server.c index 7aa8e7aab..b24d54b7e 100644 --- a/src/server.c +++ b/src/server.c @@ -1093,10 +1093,10 @@ void databasesCron(void) { uint64_t elapsed_us = 0; for (j = 0; j < dbs_per_call; j++) { redisDb *db = &server.db[rehash_db % server.dbnum]; - elapsed_us += kvstoreIncrementallyRehash(db->keys, INCREMENTAL_REHASHING_THRESHOLD_US); + elapsed_us += kvstoreIncrementallyRehash(db->keys, INCREMENTAL_REHASHING_THRESHOLD_US - elapsed_us); if (elapsed_us >= INCREMENTAL_REHASHING_THRESHOLD_US) break; - elapsed_us += kvstoreIncrementallyRehash(db->expires, INCREMENTAL_REHASHING_THRESHOLD_US); + elapsed_us += kvstoreIncrementallyRehash(db->expires, INCREMENTAL_REHASHING_THRESHOLD_US - elapsed_us); if (elapsed_us >= INCREMENTAL_REHASHING_THRESHOLD_US) break; rehash_db++;