From cbe6e8e1503fcb718136451b02c4be86040becb3 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 15 Nov 2026 16:03:35 -0800 Subject: [PATCH] chunkstore: RED control: claim cleanup never undoes a delete Review of 02d0631 (P2): dropForkClaim conditioned its write on the claims only, so a completed fork's cleanup that read the parent, paused while an explicit delete marked it, then wrote back its stale copy, resurrecting an unnamed active snapshot. TestCatalog_ClaimCleanupNever UndoesADelete fails against the old cleanup condition and the old (claim-blind) delete mark, restored as stubs: state "active", deleted_ns gone. Co-Authored-By: Claude Opus 5.5 --- .../core/frontend/chunkstore/catalog.go | 21 ++++++++++ .../core/frontend/chunkstore/retention.go | 3 ++ .../chunkstore/retention_review_test.go | 41 +++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/seaweed-block/core/frontend/chunkstore/catalog.go b/seaweed-block/core/frontend/chunkstore/catalog.go index 289d8b841f..722ff151a8 100644 --- a/seaweed-block/core/frontend/chunkstore/catalog.go +++ b/seaweed-block/core/frontend/chunkstore/catalog.go @@ -1055,6 +1055,22 @@ func (c *Catalog) DeleteSnapshot(ctx context.Context, vol, name string) (string, case ref.info.State == SnapActive: // 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 "", err @@ -1342,6 +1358,11 @@ func (c *Catalog) dropForkClaim(ctx context.Context, req ForkRequest, attempt st if len(kept) == len(claims) { return } + c.hook(hookClaimDrop, req.Snapshot) + // RED: conditioned on the claims only + // Conditioned on the state read too (review of f42e723): a + // delete that marked the entry meanwhile must never be undone by + // this stale copy. A lost condition re-reads a fresh copy. raw := e.Extended[extSnapClaims] next := proto.Clone(e).(*filerpb.Entry) setClaims(next, kept) diff --git a/seaweed-block/core/frontend/chunkstore/retention.go b/seaweed-block/core/frontend/chunkstore/retention.go index a17a3a5795..5818ccd88d 100644 --- a/seaweed-block/core/frontend/chunkstore/retention.go +++ b/seaweed-block/core/frontend/chunkstore/retention.go @@ -516,6 +516,9 @@ const ( hookPlanned = "planned" // after planning, before any deletion hookChecked = "checked" // an attempt's claims were read and none is live, before its mark hookMarked = "marked" // an attempt is marked deleting, before its name is freed + // hookClaimDrop: a completed fork read its parent entry to drop its + // claim, before the conditional write. + hookClaimDrop = "claim-drop" ) func (c *Catalog) hook(stage, name string) { diff --git a/seaweed-block/core/frontend/chunkstore/retention_review_test.go b/seaweed-block/core/frontend/chunkstore/retention_review_test.go index 750b224d3f..d9c1baad78 100644 --- a/seaweed-block/core/frontend/chunkstore/retention_review_test.go +++ b/seaweed-block/core/frontend/chunkstore/retention_review_test.go @@ -3,6 +3,7 @@ package chunkstore import ( "context" "errors" + "path" "strings" "testing" "time" @@ -395,3 +396,43 @@ func TestCatalog_DeleteRefusesAClaimedParent(t *testing.T) { t.Fatalf("delete after the fork was aborted: %q %v", got, err) } } + +// Review of f42e723: a completed fork's claim cleanup that read the parent +// entry, then paused while an explicit delete marked it, must not write +// its stale copy back (resurrecting an unnamed snapshot as a GC root). +func TestCatalog_ClaimCleanupNeverUndoesADelete(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: %v", err) + } + parent, err := c.readSnap(ctx, "v1", "s01") + if err != nil { + t.Fatal(err) + } + ran := false + c.retainHook = func(stage, name string) { + if stage != hookClaimDrop || name != "s01" || ran { + return + } + ran = true // the fork's HEAD exists now: its claim is dead + if _, err := c.DeleteSnapshot(ctx, "v1", "s01"); err != nil { + t.Fatalf("delete during the claim cleanup: %v", err) + } + } + if _, err := c.Fork(ctx, ForkRequest{Source: "v1", Snapshot: "s01", Target: "v2", RequestID: "f"}); err != nil { + t.Fatalf("fork retry: %v", err) + } + c.retainHook = nil + e, err := f.LookupEntry(ctx, path.Join(catBase, "v1", snapsDir, "s01"), parent.attempt) + if err != nil { + t.Fatal(err) + } + if !ran || string(e.Extended[extSnapState]) != SnapDeleting || len(e.Extended[extSnapDeletedNs]) == 0 { + t.Fatalf("ran=%t state=%q deleted_ns=%q; want the delete mark kept", ran, e.Extended[extSnapState], e.Extended[extSnapDeletedNs]) + } + if _, ok, err := c.resolveSnap(ctx, "v1", "s01"); ok || err != nil { + t.Fatalf("s01's name is back (%v)", err) + } +}