From 6db62358fe94c685db6303ff516e5a5d4f6ccacd Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 15 Nov 2026 16:01:54 -0800 Subject: [PATCH] chunkstore: RED control: delete refuses a fork-claimed parent claude05's ruling (2026-11-15 23:58Z): an explicit DeleteSnapshot of a snapshot an incomplete fork claims is refused with snapshot_claimed (409, not retryable, naming the fork volume); a completed or aborted fork no longer blocks. Controls, failing against the old mark (blind to claims) restored as a stub: TestCatalog_DeleteRefusesAClaimedParent, the 409 mapping, the real-filer refusal in TestIntegration_RetainHeldSchedules, and TestFork_IncompleteRetryBoundToOriginalImage, which now expects the refusal and drops the claim (as a pre-claim fork) to keep testing the image binding. Co-Authored-By: Claude Opus 5.5 --- .../core/frontend/chunkstore/catalog.go | 35 ++++++--- .../chunkstore/catalog_settle_test.go | 17 +++++ .../core/frontend/chunkstore/retention.go | 71 +++++++++++++++---- .../chunkstore/retention_integration_test.go | 4 ++ .../chunkstore/retention_review_test.go | 41 +++++++++++ .../host/volume/chunk_control_class_test.go | 3 + 6 files changed, 147 insertions(+), 24 deletions(-) diff --git a/seaweed-block/core/frontend/chunkstore/catalog.go b/seaweed-block/core/frontend/chunkstore/catalog.go index 39f5dc737a..61e9ae8bf0 100644 --- a/seaweed-block/core/frontend/chunkstore/catalog.go +++ b/seaweed-block/core/frontend/chunkstore/catalog.go @@ -1053,18 +1053,33 @@ func (c *Catalog) DeleteSnapshot(ctx context.Context, vol, name string) (string, } continue // re-read: the path now holds a tombstone or the late snapshot case ref.info.State == SnapActive: - e := ref.entry - e.Extended[extSnapState] = []byte(SnapDeleting) - e.Extended[extSnapDeletedNs] = []byte(strconv.FormatInt(c.now().UnixNano(), 10)) - err := c.f.UpdateEntry(ctx, dir, e, map[string][]byte{ - extSnapState: []byte(SnapActive), - extSnapGen: []byte(strconv.FormatUint(ref.info.HeadGen, 10)), - }) - if errors.Is(err, ErrPrecondition) || errors.Is(err, ErrNotFound) { - continue + // The one mark path shared with retention: a live fork claim + // refuses (claude05's ruling, 23:58Z). + if red := true; red { // RED: the old mark, blind to claims + e := ref.entry + e.Extended[extSnapState] = []byte(SnapDeleting) + e.Extended[extSnapDeletedNs] = []byte(strconv.FormatInt(c.now().UnixNano(), 10)) + err := c.f.UpdateEntry(ctx, dir, e, map[string][]byte{ + extSnapState: []byte(SnapActive), + extSnapGen: []byte(strconv.FormatUint(ref.info.HeadGen, 10)), + }) + if errors.Is(err, ErrPrecondition) || errors.Is(err, ErrNotFound) { + continue + } + if err != nil { + return "", remoteErr(err, "mark snapshot %s/%s deleting", vol, name) + } + break } + m, live, _, err := c.markDeleting(ctx, vol, name, ref.rec.Attempt) if err != nil { - return "", remoteErr(err, "mark snapshot %s/%s deleting", vol, name) + return "", err + } + switch m { + case markClaimed: + return "", claimedErr(vol, name, live) + case markGone: + continue } } // The attempt is deleting or aborted: free the name. diff --git a/seaweed-block/core/frontend/chunkstore/catalog_settle_test.go b/seaweed-block/core/frontend/chunkstore/catalog_settle_test.go index 1600b696d1..e746498279 100644 --- a/seaweed-block/core/frontend/chunkstore/catalog_settle_test.go +++ b/seaweed-block/core/frontend/chunkstore/catalog_settle_test.go @@ -4,6 +4,7 @@ import ( "context" "crypto/sha256" "errors" + "path" "sort" "strings" "testing" @@ -166,6 +167,22 @@ func TestFork_IncompleteRetryBoundToOriginalImage(t *testing.T) { t.Fatalf("request id reused for another snapshot: %v, want target_exists", err) } + // The fork claims s1, so an explicit delete is refused (claude05's + // ruling, 2026-11-15 23:58Z). The image binding below still guards a + // fork made before claims existed: drop the claim as such a fork would + // never have written one. + if _, err := c.DeleteSnapshot(ctx, "v1", "s1"); CodeOf(err) != CodeSnapshotClaimed { + t.Fatalf("delete of a claimed parent: %v, want snapshot_claimed", err) + } + ref, _, err := c.resolveSnap(ctx, "v1", "s1") + if err != nil || ref.entry == nil { + t.Fatalf("read s1: %v", err) + } + delete(ref.entry.Extended, extSnapClaims) + if err := f.UpdateEntry(ctx, path.Join(catBase, "v1", snapsDir, "s1"), ref.entry, nil); err != nil { + t.Fatal(err) + } + // s1 is deleted and recreated over a newer source image. if _, err := c.DeleteSnapshot(ctx, "v1", "s1"); err != nil { // frees the name t.Fatal(err) diff --git a/seaweed-block/core/frontend/chunkstore/retention.go b/seaweed-block/core/frontend/chunkstore/retention.go index bff617f678..a17a3a5795 100644 --- a/seaweed-block/core/frontend/chunkstore/retention.go +++ b/seaweed-block/core/frontend/chunkstore/retention.go @@ -72,6 +72,9 @@ import ( // settled by AbortFork. Forks published before claims existed are still // found by the fork-record scan (forkParentsNeeded). // +// An explicit DeleteSnapshot goes through the same check and mark +// (markDeleting): it refuses a live claim with snapshot_claimed. +// // A pass stopped after a mark (e.g. a lost response) leaves the attempt // deleting with its name still held; the next pass (and its dry run) lists // it and frees the name. A deleting attempt cannot gain claims. @@ -82,6 +85,10 @@ const ( CodeInvalidClass = "invalid_class" CodeInvalidPolicy = "invalid_policy" + // CodeSnapshotClaimed: an explicit delete of a snapshot a fork still + // claims (claude05's ruling, 23:58Z). Not retryable as such: it clears + // when the fork publishes its HEAD or is aborted. + CodeSnapshotClaimed = "snapshot_claimed" DefaultKeepNewest = 10 DefaultKeepYounger = time.Hour @@ -413,27 +420,56 @@ func (c *Catalog) deleteRetainedAttempt(ctx context.Context, vol, name, attempt if err != nil { return 0, false, err } + m, _, uncertain, err := c.markDeleting(ctx, vol, name, attempt) + if err != nil { + return 0, uncertain, err + } + if m == markClaimed { + return retainForkHeld, false, nil + } + return c.freeName(ctx, root, name, attempt) +} + +type markResult int + +const ( + markDone markResult = iota // this call marked the attempt deleting + markGone // the attempt's entry is gone + markSettled // already deleting or aborted (no fork can claim it) + markClaimed // a live fork claim is on it: nothing written +) + +// markDeleting marks attempt of vol/name deleting under the fork-claim +// rule, the one path for retention and explicit delete: the mark is +// conditioned on {state active, gen, no claims}; dead claims are cleared +// conditionally first; a lost condition re-reads and decides again. live +// lists the live claims when the result is markClaimed. uncertain +// reports, with an error, a mutation whose outcome is unknown. +func (c *Catalog) markDeleting(ctx context.Context, vol, name, attempt string) (markResult, []string, bool, error) { + root, err := c.volRoot(vol) + if err != nil { + return 0, nil, false, err + } dir := path.Join(root, snapsDir, name) for try := 0; try < 5; try++ { e, err := c.f.LookupEntry(ctx, dir, attempt) if errors.Is(err, ErrNotFound) { - return c.freeName(ctx, root, name, attempt) + return markGone, nil, false, nil } if err != nil { - return 0, false, remoteErr(err, "read snapshot %s/%s attempt %s", vol, name, attempt) + return 0, nil, false, remoteErr(err, "read snapshot %s/%s attempt %s", vol, name, attempt) } if string(e.Extended[extSnapState]) != SnapActive { - // Deleting (a stopped pass) or aborted: no fork can claim it. - return c.freeName(ctx, root, name, attempt) + return markSettled, nil, false, nil } raw := string(e.Extended[extSnapClaims]) claims := decodeClaims(e) live, err := c.liveClaims(ctx, vol, name, claims) if err != nil { - return 0, false, err + return 0, nil, false, err } if len(live) > 0 { - return retainForkHeld, false, nil + return markClaimed, live, false, nil } c.hook(hookChecked, name) next := proto.Clone(e).(*filerpb.Entry) @@ -441,11 +477,8 @@ func (c *Catalog) deleteRetainedAttempt(ctx context.Context, vol, name, attempt // Every claim is dead: clear them, then decide again. delete(next.Extended, extSnapClaims) err := c.f.UpdateEntry(ctx, dir, next, map[string][]byte{extSnapState: []byte(SnapActive), extSnapClaims: []byte(raw)}) - if errors.Is(err, ErrPrecondition) || errors.Is(err, ErrNotFound) { - continue - } - if err != nil { - return 0, true, remoteErr(err, "clear the dead fork claims of %s/%s attempt %s", vol, name, attempt) + if err != nil && !errors.Is(err, ErrPrecondition) && !errors.Is(err, ErrNotFound) { + return 0, nil, true, remoteErr(err, "clear the dead fork claims of %s/%s attempt %s", vol, name, attempt) } continue } @@ -460,12 +493,22 @@ func (c *Catalog) deleteRetainedAttempt(ctx context.Context, vol, name, attempt continue // a claim, a delete or a replacement landed: decide again } if err != nil { - return 0, true, remoteErr(err, "mark snapshot %s/%s attempt %s deleting", vol, name, attempt) + return 0, nil, true, remoteErr(err, "mark snapshot %s/%s attempt %s deleting", vol, name, attempt) } c.hook(hookMarked, name) - return c.freeName(ctx, root, name, attempt) + return markDone, nil, false, nil } - return 0, false, remoteErr(nil, "snapshot %s/%s attempt %s kept changing", vol, name, attempt) + return 0, nil, false, remoteErr(nil, "snapshot %s/%s attempt %s kept changing", vol, name, attempt) +} + +// claimedErr is snapshot_claimed naming the claiming fork volumes. +func claimedErr(vol, name string, live []string) error { + targets := make([]string, 0, len(live)) + for _, cl := range live { + t, _, _ := strings.Cut(cl, "/") + targets = append(targets, t) + } + return catErr(CodeSnapshotClaimed, false, nil, "snapshot %s/%s is the parent of incomplete fork %s; it can be deleted once that fork completes or is aborted", vol, name, strings.Join(targets, ", ")) } // Test seams: hook stages in a Retain pass. diff --git a/seaweed-block/core/frontend/chunkstore/retention_integration_test.go b/seaweed-block/core/frontend/chunkstore/retention_integration_test.go index 9563341961..abb287aad8 100644 --- a/seaweed-block/core/frontend/chunkstore/retention_integration_test.go +++ b/seaweed-block/core/frontend/chunkstore/retention_integration_test.go @@ -229,6 +229,10 @@ func TestIntegration_RetainHeldSchedules(t *testing.T) { if err != nil || b.attempt != pinnedAttempt || b.State != SnapActive || b.Class != SnapshotClassPinned { t.Fatalf("pinned replacement: %+v %v (attempt %s, want %s)", b, err, b.attempt, pinnedAttempt) } + // An explicit delete of a claimed parent is refused (claude05, 23:58Z). + if _, err := cat.DeleteSnapshot(ctx, "v1", "s01"); CodeOf(err) != CodeSnapshotClaimed { + t.Fatalf("delete of the claimed s01: %v; want snapshot_claimed", err) + } for _, fk := range []struct{ snap, target string }{{"s01", "v2"}, {"s02", "v3"}} { if s, err := cat.readSnap(ctx, "v1", fk.snap); err != nil || s.State != SnapActive { t.Fatalf("fork parent %s: %+v %v; want active", fk.snap, s, err) diff --git a/seaweed-block/core/frontend/chunkstore/retention_review_test.go b/seaweed-block/core/frontend/chunkstore/retention_review_test.go index be0ac93094..750b224d3f 100644 --- a/seaweed-block/core/frontend/chunkstore/retention_review_test.go +++ b/seaweed-block/core/frontend/chunkstore/retention_review_test.go @@ -354,3 +354,44 @@ func TestCatalog_UsageCountsForkHolders(t *testing.T) { t.Fatalf("s1 after v1's HEAD moved, v2 forked from it: %+v; want 0 exclusive (v2 holds both chunks)", u.Snapshots) } } + +// claude05's ruling (23:58Z): an explicit delete of a snapshot an +// incomplete fork claims is refused (snapshot_claimed, not retryable, +// naming the fork); once the fork completes or is aborted the delete goes +// through. Retention and delete share the check (markDeleting). +func TestCatalog_DeleteRefusesAClaimedParent(t *testing.T) { + f, c := forkFixture(t) + ctx := context.Background() + holdFirst(f, catBase+"/v2/HEAD") + if _, err := c.Fork(ctx, ForkRequest{Source: "v1", Snapshot: "s01", Target: "v2", RequestID: "f"}); CodeOf(err) != CodeOutcomeUnknown { + t.Fatalf("fork v2: %v", err) + } + holdFirst(f, catBase+"/v3/HEAD") + if _, err := c.Fork(ctx, ForkRequest{Source: "v1", Snapshot: "s02", Target: "v3", RequestID: "g"}); CodeOf(err) != CodeOutcomeUnknown { + t.Fatalf("fork v3: %v", err) + } + for _, tc := range []struct{ snap, target string }{{"s01", "v2"}, {"s02", "v3"}} { + _, err := c.DeleteSnapshot(ctx, "v1", tc.snap) + var ce *CatalogError + if !errors.As(err, &ce) || ce.Code != CodeSnapshotClaimed || ce.Retryable || !strings.Contains(ce.Detail(), tc.target) { + t.Fatalf("delete %s claimed by %s: %v; want snapshot_claimed, not retryable, naming %s", tc.snap, tc.target, err, tc.target) + } + if s, err := c.readSnap(ctx, "v1", tc.snap); err != nil || s.State != SnapActive { + t.Fatalf("%s after the refused delete: %+v %v; want active", tc.snap, s, err) + } + } + // Completed fork: its claim no longer blocks. + if _, err := c.Fork(ctx, ForkRequest{Source: "v1", Snapshot: "s01", Target: "v2", RequestID: "f"}); err != nil { + t.Fatalf("fork v2 retry: %v", err) + } + if got, err := c.DeleteSnapshot(ctx, "v1", "s01"); err != nil || got != SnapDeleting { + t.Fatalf("delete after the fork completed: %q %v", got, err) + } + // Aborted fork (a sweep's tombstone): neither does its claim. + if claimed, err := c.AbortFork(ctx, "v3"); err != nil || !claimed { + t.Fatalf("abort v3: claimed=%t %v", claimed, err) + } + if got, err := c.DeleteSnapshot(ctx, "v1", "s02"); err != nil || got != SnapDeleting { + t.Fatalf("delete after the fork was aborted: %q %v", got, err) + } +} diff --git a/seaweed-block/core/host/volume/chunk_control_class_test.go b/seaweed-block/core/host/volume/chunk_control_class_test.go index 89d333a9bc..918ead3a02 100644 --- a/seaweed-block/core/host/volume/chunk_control_class_test.go +++ b/seaweed-block/core/host/volume/chunk_control_class_test.go @@ -29,6 +29,9 @@ func TestChunkControl_InvalidClassRefusedBeforeCut(t *testing.T) { t.Fatalf("%s maps to %d, want 400", code, s) } } + if s := catalogStatus(chunkstore.CodeSnapshotClaimed); s != http.StatusConflict { + t.Fatalf("snapshot_claimed maps to %d, want 409", s) + } info, err := ctl.Snapshot(ctx, "v1", ChunkSnapshotRequest{Name: "s1", RequestID: "r1", Class: chunkstore.SnapshotClassPinned}) if err != nil || info.Class != chunkstore.SnapshotClassPinned || fp.cuts != 1 {