Skip to content

[fix] LTRIM cold-segment replica divergence#398

Open
warrick1016 wants to merge 4 commits intofeature/ror-update-8.2.0from
feat/8.2_wwc
Open

[fix] LTRIM cold-segment replica divergence#398
warrick1016 wants to merge 4 commits intofeature/ror-update-8.2.0from
feat/8.2_wwc

Conversation

@warrick1016
Copy link
Collaborator

No description provided.

warrick1016 and others added 4 commits March 18, 2026 16:46
…tMerge

Race condition: swapListMetaTrimCold() shrinks in-memory meta (reduces last
ridx) after a SWAP_IN is dispatched but before it completes. The decoded delta
then contains HOT elements at ridx positions beyond the trimmed main range,
violating the invariant checked by listMetaAlign() at line 575:
  serverAssert(last->index+last->len >= delta_last->index+delta_last->len)

Root cause: swapListMetaTrimCold trims the meta's ridx range but does not
cancel or clip any in-flight SWAP_IN that was dispatched under the old (larger)
range. When the SWAP_IN completes, metaListMerge receives a delta that extends
past main's end, causing the assertion failure in the swap thread.

Fix: at the start of metaListMerge, before the hot/cold swap logic, compute
main's last ridx and delta's last ridx. If delta extends beyond main, count the
excess HOT elements (iterating from LIST_HEAD = high-ridx end) and discard them:
  - listTypeDelRange removes excess elements from the quicklist tail
  - listMetaTrimToLen removes the corresponding meta segments

The orphaned RocksDB entries (trimmed from meta but not yet physically deleted)
remain until the key is fully removed, but they no longer cause correctness
issues because the meta no longer references them.

Also added a forward declaration of listMetaTrimToLen (static, defined after
swapListMetaDelRange) so it can be called from metaListMerge earlier in the
same translation unit.

Validated:
  - swap/unit/list_ltrim_cold_repl_diverge: 4/4 PASS (was triggering the
    listMetaAlign assertion before this fix)
  - swap/unit/list: full regression suite PASS

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Guard test (swap/unit/list_listmetaalign_guard.tcl):
  Master+replica stress test with aggressive eviction.  Concurrent LRANGE
  (large cold SWAP_IN on replica) + aggressive LTRIM (triggers
  swapListMetaTrimCold) + LPUSH/RPOP/LMOVE.  After 10 s, verifies master/
  replica digests match and no crash occurred.

  Note: the race is a cross-thread timing issue between the swap thread
  calling listCreateOrMergeObject (reads live meta) and the main thread
  calling swapListMetaTrimCold (writes the same meta).  The race window is
  narrow so this stress test documents the scenario rather than reproducing
  the crash deterministically.

Unit test (C-level, in swapListMetaTest):
  'meta-list: merge clips delta exceeding main ridx range'
  Directly calls metaListMerge() with a trimmed main (ridx 0..39) and a
  decoded delta that covers the old larger range (ridx 0..59).

  WITHOUT the fix: metaListMerge calls listMetaAlign(main, orig_delta_meta)
  where main_last=40 < delta_last=60 -> serverAssert fires -> process crashes
  with the exact assertion from list_coredump.txt:
    ctrip_swap_list.c:575 'last->index+last->len >= delta_last->index+delta_last->len'

  WITH the fix: delta is clipped to ridx 0..39 before the merge, no
  assertion, final main covers ridx 0..39 with 40 HOT elements.

Validated:
  - Unit test (redis-server test swap): CRASH without fix, PASS with fix
  - Guard test: 3/3 PASS with fix

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant