From 13b1f154e304c94e165ca2c062517a8c234f525d Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sat, 21 Nov 2026 17:36:42 -0800 Subject: [PATCH 1/4] storage: test that a cut revoked mid-upload keeps its uploads A cut whose host budget is revoked after two of five chunk PUTs (the limiter's MaxHold abort) leaves HEAD unchanged; the next cut uploads only the three chunks not yet uploaded, retransmits nothing, and publishes the full image. Co-Authored-By: Claude Opus 5.5 --- .../storage/chunk_cut_revoke_reuse_test.go | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 seaweed-block/core/storage/chunk_cut_revoke_reuse_test.go diff --git a/seaweed-block/core/storage/chunk_cut_revoke_reuse_test.go b/seaweed-block/core/storage/chunk_cut_revoke_reuse_test.go new file mode 100644 index 0000000000..6d6f37e3cd --- /dev/null +++ b/seaweed-block/core/storage/chunk_cut_revoke_reuse_test.go @@ -0,0 +1,87 @@ +package storage + +import ( + "context" + "errors" + "testing" +) + +// revokingBudget admits every cut and keeps its abort, so the test can +// revoke the cut's host budget the way the limiter does at MaxHold. +type revokingBudget struct{ abort func(error) } + +func (b *revokingBudget) Admit(_ context.Context, _ int64, abort func(error)) (uint64, error) { + b.abort = abort + return 1, nil +} +func (b *revokingBudget) ReserveCOW(uint64, int64) error { return nil } +func (b *revokingBudget) Release(uint64) {} + +// revokeAfterRemote revokes the budget at the PUT after `after` completed +// ones, and counts the bytes each attempt uploads. +type revokeAfterRemote struct { + *fakeChunkRemote + budget *revokingBudget + after int // revoke once this many PUTs completed; < 0: never + completed int + bytes int + keys map[string]bool +} + +func (r *revokeAfterRemote) Put(ctx context.Context, key string, b []byte) error { + if r.after >= 0 && r.completed == r.after { + r.budget.abort(errors.New("test: cut held its host slot past MaxHold")) + <-ctx.Done() + return context.Cause(ctx) + } + if err := r.fakeChunkRemote.Put(ctx, key, b); err != nil { + return err + } + if r.keys[key] { + return errors.New("test: retransmitted a completed upload") + } + r.keys[key] = true + r.completed++ + r.bytes += len(b) + return nil +} + +// A cut whose host budget is revoked mid-upload (MaxHold) keeps the +// chunks it already uploaded: the next cut uploads only the rest, fewer +// bytes than the whole dirty set, and retransmits nothing. +func TestCutRevokedMidUploadKeepsItsUploads(t *testing.T) { + const dirty = 5 + s, f, l, w := precopyLayer(t, 0, dirty) + l.cfg.UploadConcurrency = 1 + budget := &revokingBudget{} + r := &revokeAfterRemote{fakeChunkRemote: f, budget: budget, after: 2, keys: map[string]bool{}} + l.remote = r + l.cfg.Budget = budget + base := f.stats().head.Gen + + if _, err := l.Cut(ctTimeout(t)); !errors.Is(err, errCutBudgetRevoked) { + t.Fatalf("first cut: %v; want the budget revocation", err) + } + if f.stats().head.Gen != base { + t.Fatal("a revoked cut moved HEAD") + } + first := r.bytes + if first != 2*ctChunkBytes { + t.Fatalf("first attempt uploaded %d bytes before the revocation; want %d", first, 2*ctChunkBytes) + } + + r.after, r.bytes = -1, 0 + result, err := l.Cut(ctTimeout(t)) + if err != nil || result.Head.Gen != base+1 { + t.Fatalf("retry: %+v %v", result, err) + } + if result.Stats.StageRounds != 0 { + t.Fatalf("the retry staged (%d rounds); this control is for uploads made after the barrier", result.Stats.StageRounds) + } + if r.bytes >= dirty*ctChunkBytes || r.bytes != (dirty-2)*ctChunkBytes { + t.Fatalf("retry uploaded %d bytes; want only the %d chunks not yet uploaded (%d bytes), less than the dirty set (%d)", + r.bytes, dirty-2, (dirty-2)*ctChunkBytes, dirty*ctChunkBytes) + } + assertImage(t, "published image", f.imageOf(t, result.Head), w.imageAt(s.NextLSN()-1)) + assertNoIntentsLeft(t, f) +} From 7d56ce0324e424bad636c2e65ce5afc039a9476c Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sat, 21 Nov 2026 17:40:03 -0800 Subject: [PATCH 2/4] docs(invariants): INV-CUT-STAGED-REUSE-001 lists the MaxHold revocation test Co-Authored-By: Claude Opus 5.5 --- seaweed-block/docs/design/invariants.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index 2af43683a9..cab8be717d 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -578,7 +578,7 @@ Notes: | `INV-CUT-QUIESCED-001` | HEAD is exactly the image at a quiesced cut H, and H is a synced LSN. | M5 Quiesce → Copy → CAS | `core/storage/chunk_layer.go` `captureLocked` | `TestChunkLayer_C1_HeadIsQuiescedCutImage`, `TestChunkLayer_C3_EveryHeadIsExactUnderConcurrentWrites` (`core/storage/chunk_layer_contract_test.go`) | `CutPipeline` `HeadImage`; `CutCapture` `HeadImage` | ACTIVE | | `INV-CUT-BIRTH-001` | A captured chunk birth is bounded by the same-barrier cut frontier: native capture uses min(conservative owned-map maximum, H), retaining lower inherited births. A dropped unacked suffix and reused LSN cannot emit BirthLSN > HEAD.RemoteLSN; the manifest validator still refuses future births. | M5 Quiesce -> Copy -> Manifest | `core/storage/chunk_birth.go` `captureBirths`; `core/frontend/chunkstore/manifest.go` `PrepareManifest` | `TestChunkBirthDroppedSuffixPublishesWithRealManifest` (`core/storage/chunk_birth_manifest_test.go`), `TestChunkBirthCOWDoesNotObservePostBarrierWrite`, `TestChunkBirthBASEUsesInheritedFrontierIncludingZero` | Cut image at H; real recovery/manifest regression, not a new model claim | ACTIVE | | `INV-CUT-RECONCILING-001` | ARCHIVED: the legacy bitmap/reconciler is retired. Native owned-map validation and fail-closed attach admission replace its dirty-coverage protection; a provided legacy bitmap is ignored. | M5 native map admission | `core/storage/chunk_layer.go` `AttachChunkLayer` | `TestChunkAttachRefusesAnUnmappedNativeStore`; `TestChunkLayerV3_IgnoresProvidedLegacyBitmap` (`core/storage/chunk_layer_map_only_v3_test.go`) | legacy reconciliation transition retired | ARCHIVED | -| `INV-CUT-STAGED-REUSE-001` | Completed uploads survive a refused point in-process, under their intents. A chunk pre-copied outside the barrier is used only if HEAD generation is unchanged and no write and no same-open `Recover` touched it after staging; otherwise it is copied again in the native COW window. | M5 Stage → Copy | `core/storage/chunk_layer.go` `captureLocked`, `stage` | `TestChunkStageV3_SpillBudgetTriggersPrecopy` (`core/storage/chunk_cut_progress_test.go`); `TestChunkStageV3_AbortedPointReusesUploads` (`core/storage/chunk_cut_progress_test.go`); `TestChunkPrecopy_RedirtiedStagedChunkIsRecopied` (`core/storage/chunk_layer_precopy_test.go`); `TestNativeReplacementRecoverInvalidatesStagedImage` (`core/storage/legacy_replacement_v3_test.go`) | `CutPipeline` `HeadImage` | ACTIVE | +| `INV-CUT-STAGED-REUSE-001` | Completed uploads survive a refused point in-process, under their intents. A chunk pre-copied outside the barrier is used only if HEAD generation is unchanged and no write and no same-open `Recover` touched it after staging; otherwise it is copied again in the native COW window. | M5 Stage → Copy | `core/storage/chunk_layer.go` `captureLocked`, `stage` | `TestChunkStageV3_SpillBudgetTriggersPrecopy` (`core/storage/chunk_cut_progress_test.go`); `TestChunkStageV3_AbortedPointReusesUploads` (`core/storage/chunk_cut_progress_test.go`); `TestCutRevokedMidUploadKeepsItsUploads` (`core/storage/chunk_cut_revoke_reuse_test.go`); `TestChunkPrecopy_RedirtiedStagedChunkIsRecopied` (`core/storage/chunk_layer_precopy_test.go`); `TestNativeReplacementRecoverInvalidatesStagedImage` (`core/storage/legacy_replacement_v3_test.go`) | `CutPipeline` `HeadImage` | ACTIVE | | `INV-CUT-BITMAP-001` | The owned map never under-reports: its checkpoint coverage is durable before WAL recycling; UNKNOWN is cleared only after definite HEAD publication and an unchanged capture token. | M5 CAS → Settled; M1 InExtent → Checkpointed | `core/storage/walstore_map_checkpoint_v3.go` `publishCheckpointV3`; `core/storage/chunk_layer_map_v3.go` `persistCapturedMapV3` | `TestWALStoreV3_AutomaticMapCheckpoint` (`core/storage/walstore_map_checkpoint_v3_test.go`); `TestChunkLayerV3_ChangedCaptureTokenKeepsUNKNOWN` (`core/storage/chunk_layer_map_v3_test.go`) | `CutCapture` `HeadImage` | ACTIVE | | `INV-CUT-RECOVER-BARRIER-001` | Native `Recover` aborts and drains OPEN COW owners before lower locks, with admission held through recovery and a hook snapshot recheck. No recovered record above H can enter a HEAD labelled H. | M5 Quiesce; M1 Recovering | `core/storage/walstore.go` `WALStore.Recover`; `chunk_cut_cow.go` `holdChunkDestructive` | `TestCutCOWV3_RecoveryAndCloseDrainSavingOwner` (`core/storage/chunk_cut_cow_lifetime_test.go`); `TestCutCOWV3_DestructiveSnapshotRetriesFirstAttachment` (`core/storage/chunk_cut_cow_lifetime_test.go`); `TestCutCOWV3_RecoveryAndCloseDrainSavingOwner` (`core/storage/chunk_cut_cow_lifetime_test.go`) | — | ACTIVE | | `INV-CUT-UPLOAD-FROM-BUFFER` | CopyFinish owns an immutable at-H slot. Every PUT retry sends that slot, without rereading local bytes; transient failures keep the point until the caller deadline, window abort or 30-second object deadline. Guest writes to a copied chunk need no save. | M5 Copy -> Upload | `chunk_cut_cow.go` `copyCOWEntry`; `chunk_upload.go` `putCaptured` | `TestCutUploadRetriesPastThreeAttemptsAtSamePoint`, `TestChunkCutUploadDeadlineBoundRetainsDirt`, `TestCutCOWV3_PutRetryUsesFrozenSlot` (`core/storage/chunk_cut_alignment_test.go`) | CutCOW2 BufFrozen; separate upload action extension pending | ACTIVE | From 78d5b5e5f24d3adc6558c4f07b382c94186b443f Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sat, 21 Nov 2026 17:43:14 -0800 Subject: [PATCH 3/4] storage: the revoke-reuse test counts every PUT attempt before the fake A re-send of an already completed key is now counted before the fake remote sees it (the real put path reconciles such a re-send), and the test asserts no completed key is re-sent. Co-Authored-By: Claude Opus 5.5 --- .../storage/chunk_cut_revoke_reuse_test.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/seaweed-block/core/storage/chunk_cut_revoke_reuse_test.go b/seaweed-block/core/storage/chunk_cut_revoke_reuse_test.go index 6d6f37e3cd..5f1d1a5dc6 100644 --- a/seaweed-block/core/storage/chunk_cut_revoke_reuse_test.go +++ b/seaweed-block/core/storage/chunk_cut_revoke_reuse_test.go @@ -18,13 +18,16 @@ func (b *revokingBudget) ReserveCOW(uint64, int64) error { return nil } func (b *revokingBudget) Release(uint64) {} // revokeAfterRemote revokes the budget at the PUT after `after` completed -// ones, and counts the bytes each attempt uploads. +// ones. Every PUT attempt is counted before it reaches the fake, so a +// re-send of a completed key is recorded even if the caller then +// reconciles it. type revokeAfterRemote struct { *fakeChunkRemote budget *revokingBudget after int // revoke once this many PUTs completed; < 0: never completed int - bytes int + bytes int // bytes of every PUT attempt + dups int // attempts for a key that already completed keys map[string]bool } @@ -34,15 +37,15 @@ func (r *revokeAfterRemote) Put(ctx context.Context, key string, b []byte) error <-ctx.Done() return context.Cause(ctx) } + r.bytes += len(b) + if r.keys[key] { + r.dups++ + } if err := r.fakeChunkRemote.Put(ctx, key, b); err != nil { return err } - if r.keys[key] { - return errors.New("test: retransmitted a completed upload") - } r.keys[key] = true r.completed++ - r.bytes += len(b) return nil } @@ -78,6 +81,9 @@ func TestCutRevokedMidUploadKeepsItsUploads(t *testing.T) { if result.Stats.StageRounds != 0 { t.Fatalf("the retry staged (%d rounds); this control is for uploads made after the barrier", result.Stats.StageRounds) } + if r.dups != 0 { + t.Fatalf("%d PUTs re-sent an already completed key", r.dups) + } if r.bytes >= dirty*ctChunkBytes || r.bytes != (dirty-2)*ctChunkBytes { t.Fatalf("retry uploaded %d bytes; want only the %d chunks not yet uploaded (%d bytes), less than the dirty set (%d)", r.bytes, dirty-2, (dirty-2)*ctChunkBytes, dirty*ctChunkBytes) From f83bb48a7a66e41c2e3ba8821fb38e99e59a845e Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sat, 21 Nov 2026 17:55:39 -0800 Subject: [PATCH 4/4] docs(invariants): leave INV-CUT-STAGED-REUSE-001 unchanged in this test-only change A TEST change may not touch the ledger; the row's Tests cell is updated in a separate docs change. Co-Authored-By: Claude Opus 5.5 --- seaweed-block/docs/design/invariants.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index cab8be717d..2af43683a9 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -578,7 +578,7 @@ Notes: | `INV-CUT-QUIESCED-001` | HEAD is exactly the image at a quiesced cut H, and H is a synced LSN. | M5 Quiesce → Copy → CAS | `core/storage/chunk_layer.go` `captureLocked` | `TestChunkLayer_C1_HeadIsQuiescedCutImage`, `TestChunkLayer_C3_EveryHeadIsExactUnderConcurrentWrites` (`core/storage/chunk_layer_contract_test.go`) | `CutPipeline` `HeadImage`; `CutCapture` `HeadImage` | ACTIVE | | `INV-CUT-BIRTH-001` | A captured chunk birth is bounded by the same-barrier cut frontier: native capture uses min(conservative owned-map maximum, H), retaining lower inherited births. A dropped unacked suffix and reused LSN cannot emit BirthLSN > HEAD.RemoteLSN; the manifest validator still refuses future births. | M5 Quiesce -> Copy -> Manifest | `core/storage/chunk_birth.go` `captureBirths`; `core/frontend/chunkstore/manifest.go` `PrepareManifest` | `TestChunkBirthDroppedSuffixPublishesWithRealManifest` (`core/storage/chunk_birth_manifest_test.go`), `TestChunkBirthCOWDoesNotObservePostBarrierWrite`, `TestChunkBirthBASEUsesInheritedFrontierIncludingZero` | Cut image at H; real recovery/manifest regression, not a new model claim | ACTIVE | | `INV-CUT-RECONCILING-001` | ARCHIVED: the legacy bitmap/reconciler is retired. Native owned-map validation and fail-closed attach admission replace its dirty-coverage protection; a provided legacy bitmap is ignored. | M5 native map admission | `core/storage/chunk_layer.go` `AttachChunkLayer` | `TestChunkAttachRefusesAnUnmappedNativeStore`; `TestChunkLayerV3_IgnoresProvidedLegacyBitmap` (`core/storage/chunk_layer_map_only_v3_test.go`) | legacy reconciliation transition retired | ARCHIVED | -| `INV-CUT-STAGED-REUSE-001` | Completed uploads survive a refused point in-process, under their intents. A chunk pre-copied outside the barrier is used only if HEAD generation is unchanged and no write and no same-open `Recover` touched it after staging; otherwise it is copied again in the native COW window. | M5 Stage → Copy | `core/storage/chunk_layer.go` `captureLocked`, `stage` | `TestChunkStageV3_SpillBudgetTriggersPrecopy` (`core/storage/chunk_cut_progress_test.go`); `TestChunkStageV3_AbortedPointReusesUploads` (`core/storage/chunk_cut_progress_test.go`); `TestCutRevokedMidUploadKeepsItsUploads` (`core/storage/chunk_cut_revoke_reuse_test.go`); `TestChunkPrecopy_RedirtiedStagedChunkIsRecopied` (`core/storage/chunk_layer_precopy_test.go`); `TestNativeReplacementRecoverInvalidatesStagedImage` (`core/storage/legacy_replacement_v3_test.go`) | `CutPipeline` `HeadImage` | ACTIVE | +| `INV-CUT-STAGED-REUSE-001` | Completed uploads survive a refused point in-process, under their intents. A chunk pre-copied outside the barrier is used only if HEAD generation is unchanged and no write and no same-open `Recover` touched it after staging; otherwise it is copied again in the native COW window. | M5 Stage → Copy | `core/storage/chunk_layer.go` `captureLocked`, `stage` | `TestChunkStageV3_SpillBudgetTriggersPrecopy` (`core/storage/chunk_cut_progress_test.go`); `TestChunkStageV3_AbortedPointReusesUploads` (`core/storage/chunk_cut_progress_test.go`); `TestChunkPrecopy_RedirtiedStagedChunkIsRecopied` (`core/storage/chunk_layer_precopy_test.go`); `TestNativeReplacementRecoverInvalidatesStagedImage` (`core/storage/legacy_replacement_v3_test.go`) | `CutPipeline` `HeadImage` | ACTIVE | | `INV-CUT-BITMAP-001` | The owned map never under-reports: its checkpoint coverage is durable before WAL recycling; UNKNOWN is cleared only after definite HEAD publication and an unchanged capture token. | M5 CAS → Settled; M1 InExtent → Checkpointed | `core/storage/walstore_map_checkpoint_v3.go` `publishCheckpointV3`; `core/storage/chunk_layer_map_v3.go` `persistCapturedMapV3` | `TestWALStoreV3_AutomaticMapCheckpoint` (`core/storage/walstore_map_checkpoint_v3_test.go`); `TestChunkLayerV3_ChangedCaptureTokenKeepsUNKNOWN` (`core/storage/chunk_layer_map_v3_test.go`) | `CutCapture` `HeadImage` | ACTIVE | | `INV-CUT-RECOVER-BARRIER-001` | Native `Recover` aborts and drains OPEN COW owners before lower locks, with admission held through recovery and a hook snapshot recheck. No recovered record above H can enter a HEAD labelled H. | M5 Quiesce; M1 Recovering | `core/storage/walstore.go` `WALStore.Recover`; `chunk_cut_cow.go` `holdChunkDestructive` | `TestCutCOWV3_RecoveryAndCloseDrainSavingOwner` (`core/storage/chunk_cut_cow_lifetime_test.go`); `TestCutCOWV3_DestructiveSnapshotRetriesFirstAttachment` (`core/storage/chunk_cut_cow_lifetime_test.go`); `TestCutCOWV3_RecoveryAndCloseDrainSavingOwner` (`core/storage/chunk_cut_cow_lifetime_test.go`) | — | ACTIVE | | `INV-CUT-UPLOAD-FROM-BUFFER` | CopyFinish owns an immutable at-H slot. Every PUT retry sends that slot, without rereading local bytes; transient failures keep the point until the caller deadline, window abort or 30-second object deadline. Guest writes to a copied chunk need no save. | M5 Copy -> Upload | `chunk_cut_cow.go` `copyCOWEntry`; `chunk_upload.go` `putCaptured` | `TestCutUploadRetriesPastThreeAttemptsAtSamePoint`, `TestChunkCutUploadDeadlineBoundRetainsDirt`, `TestCutCOWV3_PutRetryUsesFrozenSlot` (`core/storage/chunk_cut_alignment_test.go`) | CutCOW2 BufFrozen; separate upload action extension pending | ACTIVE |