[fix] LTRIM cold-segment replica divergence#398
Open
warrick1016 wants to merge 4 commits intofeature/ror-update-8.2.0from
Open
[fix] LTRIM cold-segment replica divergence#398warrick1016 wants to merge 4 commits intofeature/ror-update-8.2.0from
warrick1016 wants to merge 4 commits intofeature/ror-update-8.2.0from
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.