From 7003168a8057d6c8b987ea5491417331e5e48678 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 29 Nov 2026 17:46:04 -0800 Subject: [PATCH] fix: preserve staged candidate counts at the capture barrier --- .../storage/chunk_copy_error_equal_test.go | 29 +++++++++++++++---- seaweed-block/core/storage/chunk_layer.go | 3 +- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/seaweed-block/core/storage/chunk_copy_error_equal_test.go b/seaweed-block/core/storage/chunk_copy_error_equal_test.go index 6e16b230e6..8c3668c888 100644 --- a/seaweed-block/core/storage/chunk_copy_error_equal_test.go +++ b/seaweed-block/core/storage/chunk_copy_error_equal_test.go @@ -91,13 +91,11 @@ func TestCopyErrorKeepsCapturedEqualMarks(t *testing.T) { if err != nil { t.Fatal(err) } - wantCompared, wantCandidates := int64(2), 0 + wantCompared := int64(2) if mode == "write_after_capture" { wantCompared++ } - if want == 5 { - wantCandidates = 1 - } + wantCandidates := int(wantCompared) if result.Stats.CandidateChunks != wantCandidates || compared.Load() != wantCompared || repeated.Load() != 0 { t.Fatalf("retry candidates=%d compared=%d repeated=%d; want %d,%d,0", result.Stats.CandidateChunks, compared.Load(), repeated.Load(), wantCandidates, wantCompared) } @@ -174,8 +172,8 @@ func TestInterruptedStagingKeepsEqualWork(t *testing.T) { if err != nil { t.Fatal(err) } - if result.Stats.CandidateChunks != 1 || retryReads.Load() != int64(8-want) || repeated.Load() != 0 { - t.Fatalf("retry candidates=%d reads=%d repeated=%d, want 1,%d,0", result.Stats.CandidateChunks, retryReads.Load(), repeated.Load(), 8-want) + if result.Stats.CandidateChunks != 8-want || retryReads.Load() != int64(8-want) || repeated.Load() != 0 { + t.Fatalf("retry candidates=%d reads=%d repeated=%d, want %d,%d,0", result.Stats.CandidateChunks, retryReads.Load(), repeated.Load(), 8-want, 8-want) } if !bytes.Equal(blockOf(f.imageOf(t, result.Head), uint32(written*ctChunkBlocks)), makeBlock(ctBlockSize, 0xEC)) { t.Fatal("retry lost the write after the staging read") @@ -183,3 +181,22 @@ func TestInterruptedStagingKeepsEqualWork(t *testing.T) { }) } } + +func TestStagingEqualChunksStillCountAsCandidates(t *testing.T) { + _, l, _ := unknownFilledVolume(t) + l.cfg.UploadConcurrency = 1 + l.cfg.COWSpillFree = func() (int64, error) { return 2 * (cowSpillFreeReserve + ctChunkBytes), nil } + var reads atomic.Int64 + l.testAfterStageReadHook = func(uint64) { reads.Add(1) } + result, err := l.Cut(ctTimeout(t)) + if err != nil { + t.Fatal(err) + } + const total = ctNumBlocks / ctChunkBlocks + if reads.Load() != total || result.Stats.StagedChunks != total { + t.Fatalf("fixture reads=%d staged=%d, want %d", reads.Load(), result.Stats.StagedChunks, total) + } + if result.Stats.CandidateChunks != total { + t.Fatalf("staging compared %d equal chunks, CandidateChunks=%d, want %d", total, result.Stats.CandidateChunks, total) + } +} diff --git a/seaweed-block/core/storage/chunk_layer.go b/seaweed-block/core/storage/chunk_layer.go index 6775adfb43..63a9cdacea 100644 --- a/seaweed-block/core/storage/chunk_layer.go +++ b/seaweed-block/core/storage/chunk_layer.go @@ -1070,7 +1070,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch return chunkCutResult{}, fmt.Errorf("%w: %w: cut LSN %d, HEAD remote_lsn %d", errChunkCaptureRefused, errChunkLSNBehind, h, baseHead.RemoteLSN) } - st.CandidateChunks = len(candidates) + st.CandidateChunks = max(st.CandidateChunks, len(candidates)) var copyErr error copyStart := time.Now() bytesBeforeCopy := st.UploadedBytes @@ -1790,6 +1790,7 @@ func (l *chunkLayerImpl) stage(ctx context.Context, st *chunkCutStats, spillCap head := copyHead(l.head) l.stMu.Unlock() st.StageRounds++ + st.CandidateChunks = max(st.CandidateChunks, len(batch)) roundChunks, err := l.stagePayloadRound(ctx, batch, vers, head, st) if err != nil { return nil, err