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..b7b045340f 100644 --- a/src/defrag.c +++ b/src/defrag.c @@ -1367,6 +1367,12 @@ static void updateDefragCpuPercent(void) { } } +void cancelActiveDefrag(void) { + if (defragIsRunning()) { + // Defrag is requested to stop 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);