@@ -377,43 +377,41 @@ static void defragLater(robj *obj) {
377377static 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
419417static void scanLaterZsetCallback (void * privdata , void * element_ref ) {
0 commit comments