From 6d00eb63b414fe39ba1c2181d25e05f3cabfd110 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Mon, 23 Nov 2026 18:05:55 -0800 Subject: [PATCH] fix: a cut's payload time counts staging; the layer counts staged chunks A cut's PayloadNs now counts its staging rounds even when they uploaded nothing (every staged chunk was zero), so a first cut that spent its time reading and hashing shows it. The layer status gains StagedChunks: the chunks staged by every cut attempt, failed ones included, so a cut retried after its deadline shows the progress its earlier attempts made. DirtyChunks is documented as the next cut's candidates, not its progress. Co-Authored-By: Claude Opus 5.5 --- seaweed-block/core/storage/chunk_layer.go | 7 +- seaweed-block/core/storage/chunk_seam.go | 12 +++- .../storage/chunk_stage_accounting_test.go | 65 +++++++++++++++++++ seaweed-block/docs/status-schema.md | 3 +- 4 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 seaweed-block/core/storage/chunk_stage_accounting_test.go diff --git a/seaweed-block/core/storage/chunk_layer.go b/seaweed-block/core/storage/chunk_layer.go index 5e5c917556..dd74cd178c 100644 --- a/seaweed-block/core/storage/chunk_layer.go +++ b/seaweed-block/core/storage/chunk_layer.go @@ -778,9 +778,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch if !guestFrozen { staged, intents, err = l.stage(ctx, &st, spill.bytes) } - if st.UploadedBytes > 0 { - st.PayloadNs += time.Since(stageStart).Nanoseconds() - } + st.PayloadNs += time.Since(stageStart).Nanoseconds() if err != nil { dropCapturedIntents(ctx, intents) return chunkCutResult{}, err @@ -1631,6 +1629,9 @@ func (l *chunkLayerImpl) stage(ctx context.Context, st *chunkCutStats, spillCap staged[idx] = sc } st.StagedChunks += len(batch) + l.stMu.Lock() + l.stats.StagedChunks += uint64(len(batch)) + l.stMu.Unlock() if hook := l.testAfterStageHook; hook != nil { hook(round) } diff --git a/seaweed-block/core/storage/chunk_seam.go b/seaweed-block/core/storage/chunk_seam.go index bbd219d4fb..e72a00a958 100644 --- a/seaweed-block/core/storage/chunk_seam.go +++ b/seaweed-block/core/storage/chunk_seam.go @@ -489,8 +489,13 @@ type chunkCutStats struct { // results whose outcome stayed unresolved after the HEAD re-read. // - PendingUnresolved: an unresolved proposal is held now. // - DirtyChunks is the number of chunks dirty since the last HEAD now -// (the C11 bitmap popcount); the cut scheduler's threshold reads it. -// Chunks still awaiting reconciliation are not counted. +// (the C11 bitmap popcount): the next cut's candidates, not its +// progress (it does not fall while a cut runs or across failed +// attempts); the cut scheduler's threshold reads it. Chunks still +// awaiting reconciliation are not counted. +// - StagedChunks counts the chunks every cut attempt of this layer +// staged, failed attempts included (in-process): the progress a cut +// retried after its deadline has made. // - Reconciling: the dirty set is being reconciled against HEAD; no cut // publishes until it is done. ReconcileDone of ReconcileTotal chunks // are compared. @@ -508,7 +513,8 @@ type chunkLayerStats struct { CASConflicts uint64 // HEAD CAS precondition failures CASUnknown uint64 // HEAD CAS results still unresolved after the re-read PendingUnresolved bool // an unresolved HEAD proposal is held now - DirtyChunks int // chunks dirty since the last HEAD (not counting those awaiting reconciliation) + DirtyChunks int // chunks dirty since the last HEAD (not counting those awaiting reconciliation): candidates, not progress + StagedChunks uint64 // chunks staged by every cut attempt, failed ones included (in-process) Reconciling bool // the dirty set is being reconciled against HEAD; no cut publishes meanwhile ReconcileDone int // chunks compared so far ReconcileTotal int // chunks to compare diff --git a/seaweed-block/core/storage/chunk_stage_accounting_test.go b/seaweed-block/core/storage/chunk_stage_accounting_test.go new file mode 100644 index 0000000000..1416056a42 --- /dev/null +++ b/seaweed-block/core/storage/chunk_stage_accounting_test.go @@ -0,0 +1,65 @@ +package storage + +import ( + "context" + "errors" + "testing" + "time" +) + +// zeroStagedLayer is a layer past its zero baseline cut with n chunks +// rewritten with zeros: candidates that staging reads and hashes but never +// uploads. The spill cap of two chunks makes the cut stage them. +func zeroStagedLayer(t *testing.T, n int) (*fakeChunkRemote, *chunkLayerImpl) { + t.Helper() + s, f, l, w := precopyLayer(t, 1, 0) + l.cfg.COWSpillLimit = 2 * ctChunkBytes + for c := 0; c < n; c++ { + w.write(t, s, uint32(c*ctChunkBlocks+2), 0) + } + return f, l +} + +// The payload time of a cut counts its staging rounds even when they +// upload nothing (every staged chunk was zero), so a first cut that spent +// its time reading and hashing shows it. +func TestChunkStage_PayloadTimeCountsStagingWithoutUploads(t *testing.T) { + _, l := zeroStagedLayer(t, 6) + // Staging takes measurable time on any clock resolution. + l.testAfterStageHook = func(int) { time.Sleep(20 * time.Millisecond) } + r, err := l.Cut(ctTimeout(t)) + if err != nil { + t.Fatal(err) + } + st := r.Stats + if st.StageRounds == 0 || st.StagedChunks == 0 { + t.Fatalf("cut stats %+v; want staging rounds", st) + } + if st.UploadedBytes != 0 { + t.Fatalf("uploaded %d bytes; want none (every staged chunk is zero)", st.UploadedBytes) + } + if st.PayloadNs < int64(20*time.Millisecond) { + t.Fatalf("payload time %d ns with %d chunks staged; want the staging time", st.PayloadNs, st.StagedChunks) + } +} + +// The layer counts every chunk staged by any cut attempt, a failed one +// included, so a first cut retried after its deadline shows the progress +// its earlier attempts made; the last successful cut's stats do not move. +func TestChunkStage_StagedChunksCountsFailedAttempts(t *testing.T) { + _, l := zeroStagedLayer(t, 6) + before := l.Stats() + ctx, cancel := context.WithCancel(context.Background()) + l.testBeforeBarrierLockHook = cancel + if _, err := l.Cut(ctx); !errors.Is(err, context.Canceled) { + t.Fatalf("cut: %v; want it cancelled before the barrier", err) + } + l.testBeforeBarrierLockHook = nil + after := l.Stats() + if after.StagedChunks <= before.StagedChunks { + t.Fatalf("staged chunks %d before, %d after a failed attempt that staged; want more", before.StagedChunks, after.StagedChunks) + } + if after.CutSeq != before.CutSeq || after.LastCut.StagedChunks != before.LastCut.StagedChunks { + t.Fatal("a failed attempt changed the last successful cut") + } +} diff --git a/seaweed-block/docs/status-schema.md b/seaweed-block/docs/status-schema.md index 93db9c50d6..8e38ecfade 100644 --- a/seaweed-block/docs/status-schema.md +++ b/seaweed-block/docs/status-schema.md @@ -227,7 +227,8 @@ Durable store state of the served volume. Go type `volume.durableStatusBody`. | `Volumes[].Chunk.Stats.CASConflicts` | uint64 | HEAD CAS precondition failures | | `Volumes[].Chunk.Stats.CASUnknown` | uint64 | HEAD CAS results still unresolved after the re-read | | `Volumes[].Chunk.Stats.PendingUnresolved` | bool | an unresolved HEAD proposal is held now | -| `Volumes[].Chunk.Stats.DirtyChunks` | int | chunks dirty since the last HEAD (not counting those awaiting reconciliation) | +| `Volumes[].Chunk.Stats.DirtyChunks` | int | chunks dirty since the last HEAD (not counting those awaiting reconciliation): candidates, not progress | +| `Volumes[].Chunk.Stats.StagedChunks` | uint64 | chunks staged by every cut attempt, failed ones included (in-process) | | `Volumes[].Chunk.Stats.Reconciling` | bool | the dirty set is being reconciled against HEAD; no cut publishes meanwhile | | `Volumes[].Chunk.Stats.ReconcileDone` | int | chunks compared so far | | `Volumes[].Chunk.Stats.ReconcileTotal` | int | chunks to compare |