From 5513728cb18419e8823b3f0af5478da99ce2383e Mon Sep 17 00:00:00 2001 From: Ran Shidlansik Date: Mon, 9 Dec 2024 18:43:52 +0200 Subject: [PATCH 1/2] Cancel activedefrag in case we empty db Signed-off-by: Ran Shidlansik --- src/db.c | 4 ++++ src/defrag.c | 10 ++++++++++ src/server.h | 1 + 3 files changed, 15 insertions(+) diff --git a/src/db.c b/src/db.c index 3c3ccb4899..b092b7484c 100644 --- a/src/db.c +++ b/src/db.c @@ -559,6 +559,10 @@ long long emptyData(int dbnum, int flags, void(callback)(dict *)) { return -1; } + /* In case we are in the process of activedefrag, the db keys and expire might be in the stage of defrag. + * we thus cancel the active defrag so that it will be restarted from the beginning. */ + cancelActiveDefrag(); + /* Fire the flushdb modules event. */ moduleFireServerEvent(VALKEYMODULE_EVENT_FLUSHDB, VALKEYMODULE_SUBEVENT_FLUSHDB_START, &fi); diff --git a/src/defrag.c b/src/defrag.c index 9c195e8959..da5dea9de3 100644 --- a/src/defrag.c +++ b/src/defrag.c @@ -1367,6 +1367,12 @@ static void updateDefragCpuPercent(void) { } } +void cancelActiveDefrag(void) { + if (defragIsRunning()) { + // Defrag has been disabled while running + endDefragCycle(false); + } +} void monitorActiveDefrag(void) { if (!server.active_defrag_enabled) return; @@ -1382,6 +1388,10 @@ void monitorActiveDefrag(void) { #else /* HAVE_DEFRAG */ +void cancelActiveDefrag(void) { + /* Not implemented yet. */ +} + void monitorActiveDefrag(void) { /* Not implemented yet. */ } diff --git a/src/server.h b/src/server.h index 896ff735b3..07352e8fbf 100644 --- a/src/server.h +++ b/src/server.h @@ -3354,6 +3354,7 @@ void enterExecutionUnit(int update_cached_time, long long us); void exitExecutionUnit(void); void resetServerStats(void); void monitorActiveDefrag(void); +void cancelActiveDefrag(void); void defragWhileBlocked(void); unsigned int getLRUClock(void); unsigned int LRU_CLOCK(void); From cbb5dac74155a2e77ac4d15c603e1e871e411932 Mon Sep 17 00:00:00 2001 From: Ran Shidlansik Date: Mon, 9 Dec 2024 18:52:15 +0200 Subject: [PATCH 2/2] fix comment Signed-off-by: Ran Shidlansik --- src/defrag.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/defrag.c b/src/defrag.c index da5dea9de3..b7b045340f 100644 --- a/src/defrag.c +++ b/src/defrag.c @@ -1369,7 +1369,7 @@ static void updateDefragCpuPercent(void) { void cancelActiveDefrag(void) { if (defragIsRunning()) { - // Defrag has been disabled while running + // Defrag is requested to stop while running endDefragCycle(false); } }