From dff8c712b3f5f5488c4aaa39c875e4214b245016 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Fri, 20 Nov 2026 20:49:42 -0800 Subject: [PATCH] restore: bound the synchronous walker tests, join the dropIntent worker, ledger rows - Walker tests that call HydrateAll synchronously run under a ctLocalBound timeout, so a walker that never finishes fails instead of hanging. - The dropIntent test releases and joins its worker at cleanup (an unbounded delete, the failing case, no longer leaks a goroutine). - invariants.md: INV-RESTORE-PARENT-HOLD-001 is Go only (the model does not cover retention of an aborted target); new INV-CAT-INTENT-DROP-BOUNDED-001 row and M6 transition for the bounded catalog intent delete. Co-Authored-By: Claude Opus 5.5 --- .../core/frontend/chunkstore/restore_test.go | 24 +++++++++++++++---- .../core/storage/chunk_hydrate_all_test.go | 17 +++++++++---- seaweed-block/docs/design/invariants.md | 4 +++- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/seaweed-block/core/frontend/chunkstore/restore_test.go b/seaweed-block/core/frontend/chunkstore/restore_test.go index f94095ef68..347efc463a 100644 --- a/seaweed-block/core/frontend/chunkstore/restore_test.go +++ b/seaweed-block/core/frontend/chunkstore/restore_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "crypto/sha256" + "errors" "fmt" "path" "strings" @@ -292,18 +293,23 @@ func TestRestore_SnapshotsRefusedUntilIndependent(t *testing.T) { } } -// hangingDeleteFiler blocks DeleteEntry until its ctx ends and records whether -// that ctx carried a deadline. +// hangingDeleteFiler blocks DeleteEntry until its ctx ends (or the test +// releases it) and records whether that ctx carried a deadline. type hangingDeleteFiler struct { Filer hadDeadline chan bool + release chan struct{} } func (h hangingDeleteFiler) DeleteEntry(ctx context.Context, dir, name string, expected map[string][]byte) error { _, ok := ctx.Deadline() h.hadDeadline <- ok - <-ctx.Done() - return ctx.Err() + select { + case <-ctx.Done(): + return ctx.Err() + case <-h.release: + return errors.New("released by the test") + } } // dropIntent outlives a cancelled caller, but one unresponsive delete must still be bounded. @@ -311,11 +317,19 @@ func TestCatalog_DropIntentIsBoundedAfterTheCallerIsCancelled(t *testing.T) { old := dropIntentTimeout dropIntentTimeout = 50 * time.Millisecond t.Cleanup(func() { dropIntentTimeout = old }) - h := hangingDeleteFiler{Filer: newFakeFiler(), hadDeadline: make(chan bool, 1)} + h := hangingDeleteFiler{Filer: newFakeFiler(), hadDeadline: make(chan bool, 1), release: make(chan struct{})} c := &Catalog{f: h} ctx, cancel := context.WithCancel(context.Background()) cancel() done := make(chan struct{}) + t.Cleanup(func() { // an unbounded delete (the failing case) is released and joined + close(h.release) + select { + case <-done: + case <-time.After(5 * time.Second): + t.Error("cleanup: dropIntent did not return") + } + }) go func() { c.dropIntent(ctx, "/vols/v1", "op-1"); close(done) }() select { case ok := <-h.hadDeadline: diff --git a/seaweed-block/core/storage/chunk_hydrate_all_test.go b/seaweed-block/core/storage/chunk_hydrate_all_test.go index 5050c445d9..b184105503 100644 --- a/seaweed-block/core/storage/chunk_hydrate_all_test.go +++ b/seaweed-block/core/storage/chunk_hydrate_all_test.go @@ -117,6 +117,15 @@ func (r *payloadRecorder) Admit(ctx context.Context, payload int64, abort func(e return r.Budget.Admit(ctx, payload, abort) } +// boundedCtx bounds a synchronous walk so that a walker that never finishes +// fails the test instead of hanging it. +func boundedCtx(t *testing.T, parent context.Context) context.Context { + t.Helper() + ctx, cancel := context.WithTimeout(parent, ctLocalBound) + t.Cleanup(cancel) + return ctx +} + func bgFill() context.Context { return hostlimit.WithClass(context.Background(), hostlimit.BackgroundFill) } @@ -124,7 +133,7 @@ func bgFill() context.Context { func TestHydrateAll_FillsEveryChunkOnceAndReadsTheHeadImage(t *testing.T) { fastWalker(t) s, layer, remote, _ := hydrationFixture(t, 2, 64) - ctx := context.Background() + ctx := boundedCtx(t, context.Background()) p, err := layer.HydrateAll(ctx) if err != nil || p.Remaining != 0 || p.Filled != ctNumBlocks/ctChunkBlocks { t.Fatalf("hydrate all: %+v %v, want %d filled, 0 remaining", p, err, ctNumBlocks/ctChunkBlocks) @@ -161,7 +170,7 @@ func TestHydrateAll_NeverOverwritesALocalWrite(t *testing.T) { if _, err := s.Write(ctChunkBlocks+1, makeBlock(ctBlockSize, 0xEF)); err != nil { // one block of chunk 1 t.Fatal(err) } - p, err := layer.HydrateAll(context.Background()) + p, err := layer.HydrateAll(boundedCtx(t, context.Background())) if err != nil || p.Remaining != 0 { t.Fatalf("hydrate all: %+v %v", p, err) } @@ -255,7 +264,7 @@ func TestHydrateAll_AdmitsEachFillThroughTheLimiterWithTheCallersClass(t *testin _, layer, _, _ := hydrationFixture(t, 2, 64) budget := &walkerBudget{refuse: 2} layer.cfg.Budget = budget - ctx := context.WithValue(context.Background(), walkerClassKey{}, "fill") + ctx := boundedCtx(t, context.WithValue(context.Background(), walkerClassKey{}, "fill")) p, err := layer.HydrateAll(ctx) if err != nil || p.Remaining != 0 || p.Deferred != 2 { t.Fatalf("hydrate all: %+v %v, want 2 deferred then everything filled", p, err) @@ -390,7 +399,7 @@ func TestHydrateAll_StatsReportAbsentChunks(t *testing.T) { if n := layer.Stats().AbsentChunks; n != ctNumBlocks/ctChunkBlocks { t.Fatalf("absent before the walk: %d, want %d", n, ctNumBlocks/ctChunkBlocks) } - if _, err := layer.HydrateAll(context.Background()); err != nil { + if _, err := layer.HydrateAll(boundedCtx(t, context.Background())); err != nil { t.Fatal(err) } if n := layer.Stats().AbsentChunks; n != 0 { diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index 7c0e2cb485..8dd9bd5a26 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -296,6 +296,7 @@ HEAD states, snapshots, forks, intents, key bindings, GC and volume delete in th |---|---|---|---|---| | — | intent live | written before the first upload of a cut, a segment or a copy | `core/storage/chunk_layer.go` `captureLocked`, `stage`; `core/frontend/chunkstore/manifest.go` `PrepareManifest` | `INV-CUT-INTENT-FIRST-001` | | intent live | removed | the CAS outcome is known (success or a definite refusal) | `core/storage/chunk_layer.go` `captureLocked`, `dropIntents` | `INV-CUT-INTENT-FIRST-001`, `INV-WS-CAS-UNKNOWN-001` | +| intent live | removed (catalog) | the operation's create is confirmed or can never apply; the delete outlives the caller's cancellation but is bounded by `dropIntentTimeout` | `core/frontend/chunkstore/catalog.go` `dropIntent` | `INV-CAT-INTENT-DROP-BOUNDED-001` | | intent live | expired | older than the TTL, blockmaster has issued a higher epoch, and remote HEAD has a higher epoch | `core/frontend/chunkstore/gc.go` `Collector.run` | `INV-GC-INTENT-EXPIRY-001` | | — | bound | exclusive create of the key; a key that exists or once existed is refused | `core/frontend/chunkstore/chunk_remote.go` `putObject` | `INV-CHUNK-WRITE-ONCE-001` | | bound | tombstoned, needle deleted | unrooted in two complete marks at least one grace period apart, older than the grace period, binding unchanged; an unreadable segment aborts the pass; the collector requires grace greater than the operation deadline | `core/frontend/chunkstore/gc.go` `Collector.run`, `NewCollector` | `INV-GC-REACHABLE-001`, `INV-GC-UNREADABLE-SEGMENT-001`, `INV-GC-GRACE-DEADLINE-001` | @@ -533,8 +534,9 @@ Notes: | `INV-HEAD-DELETE-FROZEN-001` | Once HEAD is `deleting`, `purging` or `deleted`, `CASHead` refuses every claim or cut; only the delete's own transitions move it. | M6 HEAD: deleting \| purging \| deleted | `core/frontend/chunkstore/chunk_remote.go` `CASHead` | `TestCatalog_DeletedHeadCannotBeRevivedByAClaim` (`core/frontend/chunkstore/volume_delete_test.go`) | `ChunkLifecycleFixed` `NoPublishAfterCommit` | ACTIVE | | `INV-RESTORE-INDEPENDENT-001` | `RESTORE-INDEPENDENT` is written once, only for a restore volume with a live HEAD whose resolved image holds no ref owned by another volume; a restore without a live HEAD (absent or aborted) never attaches and is never marked. | M6 snapshot: restore → independent | `core/frontend/chunkstore/restore.go` `MarkIndependent`, `foreignRefsOf`; `core/frontend/chunkstore/catalog.go` `EnsureVolume` | `TestRestore_MarkIndependentOnlyWithoutQualifiedRefs`, `TestRestore_WithoutLiveHeadNeverAttaches`, `TestRestore_SourceGoneIsNormal` (`core/frontend/chunkstore/restore_test.go`) | `RestoreIndependence` `Independent`, `NoForeignList` (simulation) | ACTIVE | | `INV-RESTORE-SNAPSHOT-001` | A snapshot of a restore volume is refused until it is independent, and the snapshot image itself must hold no ref owned by another volume (a cut saved before the marker is refused after it); a refused request leaves no name or attempt. | M6 snapshot: restore (not independent) → name held (refused) | `core/frontend/chunkstore/catalog.go` `CreateSnapshot` | `TestRestore_SnapshotsRefusedUntilIndependent`, `TestRestore_SnapshotOfAStalePreIndependenceImageRefused` (`core/frontend/chunkstore/restore_test.go`) | `RestoreIndependence` `Independent` (`red_snap_during_restore`) | ACTIVE | -| `INV-RESTORE-PARENT-HOLD-001` | A restore target with an aborted first HEAD no longer has a live fork claim (any HEAD ends the claim), but retention still keeps the parent snapshot as `fork_parent` (the fork-record scan treats an aborted HEAD as no HEAD) until the target volume is deleted. | M6 snapshot: published (parent of an aborted restore) | `core/frontend/chunkstore/retention.go` `liveClaims`, `forkParentsNeeded` | `TestRestore_AbortedFirstHeadKeepsTheParentUntilTheTargetIsDeleted` (`core/frontend/chunkstore/restore_test.go`) | `RestoreIndependence` `ClaimUntilHead_Inv` (simulation) | ACTIVE | +| `INV-RESTORE-PARENT-HOLD-001` | A restore target with an aborted first HEAD no longer has a live fork claim (any HEAD ends the claim), but retention still keeps the parent snapshot as `fork_parent` (the fork-record scan treats an aborted HEAD as no HEAD) until the target volume is deleted. | M6 snapshot: published (parent of an aborted restore) | `core/frontend/chunkstore/retention.go` `liveClaims`, `forkParentsNeeded` | `TestRestore_AbortedFirstHeadKeepsTheParentUntilTheTargetIsDeleted` (`core/frontend/chunkstore/restore_test.go`) | — (Go only: the model's `ClaimUntilHead` does not cover retention of an aborted target) | ACTIVE | | `INV-RESTORE-HYDRATE-001` | The background hydrate walker fills only through the shared fill flights (one fetch per chunk, never over a local write), yields while a guest waits, and holds one host fill permit (no cut payload, the caller's class) per flight until that flight's I/O is over, also when it returns early on cancel or revocation. | M4 Holder (background hydrate) | `core/storage/chunk_hydrate_all.go` `HydrateAll`, `startWalkerFill`, `admitFill` | `TestHydrateAll_YieldsToGuestReadsAndNeverFetchesTwice`, `TestHydrateAll_NeverOverwritesALocalWrite`, `TestHydrateAll_RealBackgroundFillLimiterAdmitsEveryFill`, `TestHydrateAll_PermitIsHeldUntilTheOwnedFetchSettles`, `TestHydrateAll_RevocationStopsTheWalker`, `TestHydrateAll_CancelReturnsWhileTheFetchIsHeld` (`core/storage/chunk_hydrate_all_test.go`) | — | ACTIVE | +| `INV-CAT-INTENT-DROP-BOUNDED-001` | Dropping an operation's intent outlives the caller's cancellation (cleanup after a refused or cancelled request) but is bounded by `dropIntentTimeout`; a failed or timed-out delete leaves the intent, which only over-protects. | M6 intent: Live → Removed | `core/frontend/chunkstore/catalog.go` `dropIntent` | `TestCatalog_DropIntentIsBoundedAfterTheCallerIsCancelled` (`core/frontend/chunkstore/restore_test.go`) | — | ACTIVE | Notes: