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 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-15 16:03:35 -08:00
1 parent e7fc9a5254
commit cbe6e8e150
3 files changed
+65

No files matched your search

@@ -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)
@@ -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) {
@@ -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)
}
}