Skip to content

Commit 84fd626

Browse files
authored
Refactor scanLaterList to fix latency issue (valkey-io#2514)
1 parent c6c91d1 commit 84fd626

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

src/defrag.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -377,43 +377,41 @@ static void defragLater(robj *obj) {
377377
static long scanLaterList(robj *ob, unsigned long *cursor, monotime endtime) {
378378
quicklist *ql = ob->ptr;
379379
quicklistNode *node;
380-
long iterations = 0;
381-
int bookmark_failed = 0;
382380
serverAssert(ob->type == OBJ_LIST && ob->encoding == OBJ_ENCODING_QUICKLIST);
383381

382+
/* Find starting node */
384383
if (*cursor == 0) {
385-
/* if cursor is 0, we start new iteration */
386384
node = ql->head;
387385
} else {
388386
node = quicklistBookmarkFind(ql, "_AD");
389387
if (!node) {
390-
/* if the bookmark was deleted, it means we reached the end. */
391388
*cursor = 0;
392-
return 0;
389+
return 0; /* either bookmark failed to create or deleted, skip this one and continue with other quicklists*/
393390
}
394391
node = node->next;
395392
}
396393

397-
(*cursor)++;
394+
/* Process nodes until time expires or list ends */
398395
while (node) {
399396
activeDefragQuickListNode(ql, &node);
400397
server.stat_active_defrag_scanned++;
401-
if (++iterations > 128 && !bookmark_failed) {
402-
if (getMonotonicUs() > endtime) {
403-
if (!quicklistBookmarkCreate(&ql, "_AD", node)) {
404-
bookmark_failed = 1;
405-
} else {
406-
ob->ptr = ql; /* bookmark creation may have re-allocated the quicklist */
407-
return 1;
408-
}
398+
399+
/* Check time limit after processing each node */
400+
if (getMonotonicUs() > endtime) {
401+
if (quicklistBookmarkCreate(&ql, "_AD", node)) {
402+
ob->ptr = ql; /* bookmark creation may have re-allocated the quicklist */
403+
(*cursor)++;
404+
return 1;
409405
}
410-
iterations = 0;
406+
break; /* bookmark creation failed - skip this one and continue with other quicklists */
411407
}
412408
node = node->next;
413409
}
410+
411+
/* Completed processing all nodes */
414412
quicklistBookmarkDelete(ql, "_AD");
415413
*cursor = 0;
416-
return bookmark_failed ? 1 : 0;
414+
return 0;
417415
}
418416

419417
static void scanLaterZsetCallback(void *privdata, void *element_ref) {

tests/unit/memefficiency.tcl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ run_solo {defrag} {
386386
# number of total fields. lists are progressively increasing sizes.
387387
set n 200000
388388

389-
perform_defrag_test $title populate {
389+
perform_defrag_test $title latency 5 populate {
390390
set rd [valkey_deferring_client]
391391
$rd client reply off
392392
set val [string repeat A 350]

0 commit comments

Comments
 (0)