diff --git a/seaweed-block/core/frontend/chunkstore/catalog.go b/seaweed-block/core/frontend/chunkstore/catalog.go index 0ffd60eb65..8c60e8bff6 100644 --- a/seaweed-block/core/frontend/chunkstore/catalog.go +++ b/seaweed-block/core/frontend/chunkstore/catalog.go @@ -982,6 +982,9 @@ func (c *Catalog) CreateSnapshot(ctx context.Context, vol string, req SnapshotRe if info.State == SnapAborted { return SnapshotInfo{}, catErr(CodeSnapshotExists, false, nil, "snapshot %s/%s: this request's attempt was aborted; delete the name to reuse it", vol, req.Name) } + if info.State == SnapDeleting { + return SnapshotInfo{}, catErr(CodeSnapshotDeleting, false, nil, "snapshot %s is being deleted; wait for the delete to finish, then reuse the name", req.Name) + } return info, nil case got != nil: // The attempt path holds a sweep's tombstone, which is permanent: @@ -1059,6 +1062,8 @@ func (c *Catalog) SnapshotReplay(ctx context.Context, vol string, req SnapshotRe return SnapshotInfo{}, false, nil // this request's attempt has not published: publish it case ref.info.State == SnapAborted: return SnapshotInfo{}, false, catErr(CodeSnapshotExists, false, nil, "snapshot %s/%s: this request's attempt was aborted; delete the name to reuse it", vol, req.Name) + case ref.info.State == SnapDeleting: + return SnapshotInfo{}, false, catErr(CodeSnapshotDeleting, false, nil, "snapshot %s is being deleted; wait for the delete to finish, then reuse the name", req.Name) } ref.info.Replayed = true return ref.info, true, nil diff --git a/seaweed-block/core/frontend/chunkstore/catalog_replay_deleting_test.go b/seaweed-block/core/frontend/chunkstore/catalog_replay_deleting_test.go new file mode 100644 index 0000000000..ca695c8c45 --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/catalog_replay_deleting_test.go @@ -0,0 +1,128 @@ +package chunkstore + +import ( + "context" + "path" + "testing" + + "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/filerpb" +) + +// TestCatalogReplayOfASnapshotBeingDeletedIsNotSuccess: s1 is created by +// request r1, then deleted by a process that dies after marking it deleting +// and before freeing its name (the name record still names r1's attempt). +// A retry of r1, through a restarted catalog, must not answer success for +// the snapshot being deleted, neither from the replay check nor from the +// create: both refuse it as snapshot_deleting, and the snapshot stays +// deleting and unlisted. +func TestCatalogReplayOfASnapshotBeingDeletedIsNotSuccess(t *testing.T) { + f, b, c, h := catalogFixture(t) + ctx := context.Background() + req := SnapshotRequest{Name: "s1", RequestID: "r1"} + if _, err := c.CreateSnapshot(ctx, "v1", req, h); err != nil { + t.Fatal(err) + } + + nameRecord := path.Join(catBase, "v1", snapNamesDir, "s1") + crashed := &crashFiler{manifestFilerBlobs: manifestFilerBlobs{f, gcBlobs{b}}, + match: func(op, p string) bool { return op == "delete" && p == nameRecord }} + dying, err := NewCatalog(crashed, catBase) + if err != nil { + t.Fatal(err) + } + if _, err := dying.DeleteSnapshot(ctx, "v1", "s1"); err == nil { + t.Fatal("the delete returned success although its process died before freeing the name") + } + if _, err := f.LookupEntry(ctx, path.Dir(nameRecord), path.Base(nameRecord)); err != nil { + t.Fatalf("the name record should still exist after the crash: %v", err) + } + + r, err := NewCatalog(manifestFilerBlobs{f, gcBlobs{b}}, catBase) // the restarted process + if err != nil { + t.Fatal(err) + } + if info, replayed, err := r.SnapshotReplay(ctx, "v1", req); replayed || CodeOf(err) != CodeSnapshotDeleting { + t.Errorf("the replay check of a snapshot being deleted: %+v replayed %v, %v; want snapshot_deleting", info, replayed, err) + } + if info, err := r.CreateSnapshot(ctx, "v1", req, h); CodeOf(err) != CodeSnapshotDeleting { + t.Errorf("a retry of the create of a snapshot being deleted: %+v, %v; want snapshot_deleting", info, err) + } + if l, _, err := r.ListSnapshots(ctx, "v1", ListOptions{}); err != nil || len(l) != 0 { + t.Fatalf("the snapshot being deleted is listed: %+v %v", l, err) + } + if l, _, err := r.ListSnapshots(ctx, "v1", ListOptions{IncludeDeleting: true}); err != nil || len(l) != 1 || l[0].State != SnapDeleting { + t.Fatalf("the snapshot must stay deleting: %+v %v", l, err) + } +} + +// peerPublishFiler models a peer (an earlier attempt of the same request) +// that, just before this create writes the attempt entry, publishes that +// same attempt and has it deleted: the create's initial replay check saw the +// attempt unpublished, and its exclusive create then finds the peer's entry, +// now deleting. +type peerPublishFiler struct { + manifestFilerBlobs + attemptDir string + peer func(ctx context.Context, dir string, e *filerpb.Entry) + done bool +} + +func (p *peerPublishFiler) CreateEntry(ctx context.Context, dir string, e *filerpb.Entry, oExcl bool) error { + if !p.done && dir == p.attemptDir { + p.done = true + p.peer(ctx, dir, e) + } + return p.manifestFilerBlobs.CreateEntry(ctx, dir, e, oExcl) +} + +// TestCatalogCreateFindingItsAttemptDeletingIsNotSuccess: r1's first attempt +// at s1 died after its name record. A retry of r1 passes the replay check +// (the attempt is unpublished) and, as it writes the attempt entry, a peer +// with the same request publishes that attempt and deletes it. The retry's +// exclusive create finds the peer's entry, now deleting: it must refuse it +// as snapshot_deleting, not answer success. +func TestCatalogCreateFindingItsAttemptDeletingIsNotSuccess(t *testing.T) { + f, b, _, h := catalogFixture(t) + ctx := context.Background() + req := SnapshotRequest{Name: "s1", RequestID: "r1"} + nameRecord := path.Join(catBase, "v1", snapNamesDir, "s1") + crashed := &crashFiler{manifestFilerBlobs: manifestFilerBlobs{f, gcBlobs{b}}, after: true, + match: func(op, p string) bool { return op == "create" && p == nameRecord }} + dying, err := NewCatalog(crashed, catBase) + if err != nil { + t.Fatal(err) + } + if _, err := dying.CreateSnapshot(ctx, "v1", req, h); err == nil { + t.Fatal("the first attempt returned success although its process died after the name record") + } + + inner := manifestFilerBlobs{f, gcBlobs{b}} + peer, err := NewCatalog(inner, catBase) + if err != nil { + t.Fatal(err) + } + pf := &peerPublishFiler{manifestFilerBlobs: inner, attemptDir: path.Join(catBase, "v1", snapsDir, "s1")} + pf.peer = func(ctx context.Context, dir string, e *filerpb.Entry) { + if err := inner.CreateEntry(ctx, dir, e, true); err != nil { + t.Errorf("the peer's publish of the same attempt: %v", err) + return + } + if st, err := peer.DeleteSnapshot(ctx, "v1", "s1"); err != nil || st != SnapDeleting { + t.Errorf("the peer's delete: %q %v", st, err) + } + } + r, err := NewCatalog(pf, catBase) + if err != nil { + t.Fatal(err) + } + if info, replayed, err := r.SnapshotReplay(ctx, "v1", req); err != nil || replayed { + t.Fatalf("before the retry the replay check must see the attempt unpublished: %+v %v %v", info, replayed, err) + } + info, err := r.CreateSnapshot(ctx, "v1", req, h) + if !pf.done { + t.Fatal("the retry never reached the exclusive create of its attempt") + } + if CodeOf(err) != CodeSnapshotDeleting { + t.Fatalf("the retry found its attempt deleting: %+v, %v; want snapshot_deleting", info, err) + } +} diff --git a/seaweed-block/docs/design/agent-workspace-design.md b/seaweed-block/docs/design/agent-workspace-design.md index 1877eaed89..6efab16f51 100644 --- a/seaweed-block/docs/design/agent-workspace-design.md +++ b/seaweed-block/docs/design/agent-workspace-design.md @@ -230,6 +230,7 @@ stateDiagram-v2 NameHeld --> Published: exclusive create snaps/name/attempt NameHeld --> Aborted: DeleteSnapshot or GC tombstones the attempt path Published --> Deleting: DeleteSnapshot or retention (forks do not hold it) + Deleting --> Deleting: a retry of the same request is refused (snapshot_deleting), never answered as success Published --> ForkTarget: deadline live before VOLUME, legacy preparation, copy intent and HEAD ForkTarget --> ActiveTarget: exclusive HEAD create before deadline (fork or restore) Deleting --> NameFreed: name record removed diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index 67a89ea97d..e903387775 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -371,7 +371,7 @@ HEAD states, snapshots, forks, intents, key bindings, GC and volume delete in th | From | To | Guard | Enforced in | Protects | |---|---|---|---|---| -| — | name held | exclusive name record `{request, attempt}`; a retry with the same request id gets its own attempt back | `core/frontend/chunkstore/catalog.go` `CreateSnapshot`, `claimName` | `INV-SNAP-EXPLICIT-DELETE-001` | +| — | name held | exclusive name record `{request, attempt}`; a retry with the same request id gets its own attempt back, unless that attempt is being deleted: then the retry is refused (`snapshot_deleting`), never answered as success, by the replay check and by the create | `core/frontend/chunkstore/catalog.go` `CreateSnapshot`, `claimName`, `SnapshotReplay` | `INV-SNAP-EXPLICIT-DELETE-001` | | name held | published | the volume is not being deleted; an intent with a deadline is written and the deadline is re-checked; exclusive create of the attempt path | `core/frontend/chunkstore/catalog.go` `CreateSnapshot` | `INV-SNAP-EXPLICIT-DELETE-001` | | published | deleting | `DeleteSnapshot` or retention for this attempt, whatever forks or restores read it (no claim is read); the mark records its delete time and GC keeps the attempt rooted for its grace | `core/frontend/chunkstore/catalog.go` `DeleteSnapshot`; `core/frontend/chunkstore/retention.go` `markDeleting` | `INV-SNAP-EXPLICIT-DELETE-001`, `INV-SNAP-DELETE-ANY-ORDER-001` | | published | deleting (retention, class `scheduled`) | the snapshot is not among the newest K of class `scheduled` (K from the newest one's `swb.schedule.keep`, default 7); other classes are planned without it | `core/frontend/chunkstore/retention.go` `planRetention` | `INV-SNAP-SCHEDULED-KEEP-001` | @@ -897,7 +897,7 @@ Notes: | `INV-GC-GRACE-DEADLINE-001` | A GC collector cannot be built unless its grace period is at least the catalog operation deadline plus the clock skew bound plus the CAS apply bound (default: the operation deadline). | M6 GC configuration | `core/frontend/chunkstore/gc.go` `NewCollector` | `TestGC_GraceConstructorRequiresDeadlineSkewAndApplyBound` (`core/frontend/chunkstore/gc_volume_test.go`) | `RetentionGCTiming` `NoLiveDelete`; `ForkDelete` `NoGap` (G >= D + E + P) | ACTIVE | | `INV-GC-DELETE-EXACT-FID-001` | GC deletes a needle only after tombstoning its key binding, and only by the exact fid recorded in that binding (volume id, needle key and cookie), never a rebuilt one. | M6 key: Bound → Tombstoned, needle deleted | `core/frontend/chunkstore/gc.go` `Collector.run`; `core/frontend/chunkstore/swfs.go` `SWFS.DeleteBlob` | `TestSWFS_DeleteBlobStatusesAndExactPath` (`core/frontend/chunkstore/gc_test.go`); `TestGCPermanentDeleteDoesNotStarveLaterBindings` (`core/frontend/chunkstore/gc_delete_progress_test.go`) | — | ACTIVE | | `INV-GC-GRACE-RESTART-001` | A binding's grace counts from the first complete mark that finds it unrooted and past its age bound, and restarts when a mark finds it rooted or its binding changed; a deleting snapshot attempt roots its keys until its deleting time is past the grace. | M6 key: Bound → Tombstoned; M6 snapshot: Deleting | `core/frontend/chunkstore/gc.go` `Collector.run`; `core/frontend/chunkstore/gc_copy.go` `snapshotAttemptHeads`; `core/frontend/chunkstore/retention.go` `markDeleting` | `TestGC_PublishedUnnamedAttemptRootsUntilDeletingGrace` (`core/frontend/chunkstore/gc_copy_test.go`) | `RetentionGCTiming` `NoLiveDelete` | ACTIVE | -| `INV-SNAP-EXPLICIT-DELETE-001` | A published snapshot is removed only by `DeleteSnapshot` or retention for its own attempt. | M6 snapshot: Published → Deleting | `core/frontend/chunkstore/catalog.go` `DeleteSnapshot`; `core/frontend/chunkstore/retention.go` `markDeleting` | `TestCatalog_DeleteMarksThenIdempotent` (`core/frontend/chunkstore/catalog_test.go`); `TestCatalog_RetainSkipsASameNameReplacement` (`core/frontend/chunkstore/retention_review_test.go`) | `ChunkLifecycleFixed` `SnapOnlyExplicitDelete` | ACTIVE | +| `INV-SNAP-EXPLICIT-DELETE-001` | A published snapshot is removed only by `DeleteSnapshot` or retention for its own attempt. | M6 snapshot: Published → Deleting | `core/frontend/chunkstore/catalog.go` `DeleteSnapshot`, `SnapshotReplay`, `CreateSnapshot`; `core/frontend/chunkstore/retention.go` `markDeleting` | `TestCatalog_DeleteMarksThenIdempotent` (`core/frontend/chunkstore/catalog_test.go`); `TestCatalog_RetainSkipsASameNameReplacement` (`core/frontend/chunkstore/retention_review_test.go`); `TestCatalogReplayOfASnapshotBeingDeletedIsNotSuccess`, `TestCatalogCreateFindingItsAttemptDeletingIsNotSuccess` (`core/frontend/chunkstore/catalog_replay_deleting_test.go`) | `ChunkLifecycleFixed` `SnapOnlyExplicitDelete` | ACTIVE | | `INV-SNAP-SEAL-POINT-001` | A named snapshot request for a free name runs exactly one cut and stores the HEAD that cut installed as the snapshot; a retry of a recorded request replays that snapshot without cutting; if the cut fails (including a COW window abort) the request fails with that error and records no snapshot. | M5 Admit → Settled, then M6 snapshot: name held → published | `core/host/volume/chunk_control.go` `chunkControl.Snapshot`; `core/frontend/chunkstore/catalog.go` `CreateSnapshot` | `TestNamedSnapshotWindowAbortNeverSubstitutesPoint` (`core/host/volume/chunk_review271_test.go`); `TestChunkControl_SnapshotStoresTheCutHead` (`core/host/volume/chunk_control_adapter_test.go`) | — | ACTIVE | | `INV-SNAP-SEAL-DURABLE-001` | A named snapshot is reported done only after its cut's HEAD CAS is confirmed (every referenced chunk uploaded first) and its attempt entry is confirmed by its exclusive create or a read-back; an unknown outcome of either reports failure, and an unknown create keeps the snapshot's intent. | M5 CAS → Settled; M6 snapshot: name held → published | `core/host/volume/chunk_control.go` `chunkControl.Snapshot`; `core/storage/chunk_layer.go` `captureLocked`; `core/frontend/chunkstore/catalog.go` `CreateSnapshot`, `createResolved` | `TestCatalog_SnapshotUncertainCreateKeepsIntent`, `TestCatalog_SnapshotAppliedResponseLostIsConfirmed` (`core/frontend/chunkstore/catalog_settle_test.go`); `TestNamedSnapshotWindowAbortNeverSubstitutesPoint` (`core/host/volume/chunk_review271_test.go`) | — | ACTIVE | | `INV-FORK-FROM-SEALED-001` | A fork reads only a published, active snapshot: a name with no published attempt or an aborted one is `snapshot_not_found`, a deleting one is `snapshot_deleting`, and every attempt of a fork, a retry included, reads the parent again. | M6 snapshot: published → fork | `core/frontend/chunkstore/catalog.go` `Fork`, `readForkParent`, `resolveSnap` | `TestFork_RefusalsAndDeadline` (`core/frontend/chunkstore/catalog_test.go`); `TestCatalog_SweepTombstonesADeadSnapshotAttempt` (`core/frontend/chunkstore/catalog_held_create_test.go`); `TestCatalog_ForkAfterTheMarkRefuses` (`core/frontend/chunkstore/retention_review_test.go`); `TestSnapshotDelete_ForkRetryAfterDeleteRereadsTheParent` (`core/frontend/chunkstore/snapshot_delete_test.go`) | `ChunkLifecycleFixed` models the snapshot as a name record plus an attempt path; no fork-from-unpublished property | ACTIVE |