fix: stamp snapshot incarnations on every modern HEAD publication

This commit is contained in:
pingqiu committed 2026-12-01 20:42:06 -08:00
1 parent b5e940da0c
commit 3e16fc500c
14 files changed
+145 -33

No files matched your search

@@ -64,14 +64,14 @@ func chunkAdmitCatalogSnapshotDelete(ctx context.Context, cat *chunkstore.BCatal
return 0, layout.ErrHeadAbsent
}
if h.Version != layout.HeadVersion1 {
return 0, &chunkstore.CatalogError{Code: "snapshot_generation_unavailable", Message: "attach with a v1.1 holder to stamp snapshot generations before operation-based deletion", Retryable: true}
return 0, &chunkstore.CatalogError{Code: "snapshot_generation_unavailable", Message: "make the first v1.1 HEAD write with a v1.1 holder to stamp snapshot generations before operation-based deletion", Retryable: true}
}
target, found := h.Snapshots[name]
if !found {
return 0, nil
}
if target.Gen == 0 {
return 0, &chunkstore.CatalogError{Code: "snapshot_generation_unavailable", Message: "snapshot has no publishing generation; attach with a v1.1 holder first", Retryable: true}
return 0, &chunkstore.CatalogError{Code: "snapshot_generation_unavailable", Message: "snapshot has no publishing generation; make the first v1.1 HEAD write with a v1.1 holder first", Retryable: true}
}
return target.Gen, nil
})
@@ -297,3 +297,68 @@ func TestSnapshotDeletePublicInitiallyAbsentIsRetainedRefusal(t *testing.T) {
t.Fatalf("absence not retained: %+v %v", op, err)
}
}
func TestSnapshotDeletePublicAfterV11CommitStampsEveryIncarnation(t *testing.T) {
for _, snapshot := range []string{"modern", ""} {
t.Run("snapshot="+snapshot, func(t *testing.T) {
ctx := context.Background()
mem := newMemChunkStore()
useMemChunkStore(t, mem)
v := cliVolume(t, mem, "v1", "legacy-one")
addSnapshot(t, mem, v, "legacy-two", layout.SnapshotMeta{}, "legacy-second", 2)
cat, _ := chunkstore.NewBCatalog(mem, mem, defaultChunkPathCLI)
st, _ := cat.Store(v)
h, _, err := st.ReadHead(ctx)
if err != nil {
t.Fatal(err)
}
reader, _ := layout.NewReader(defaultChunkPathCLI, v.Dir(), st.Fetch)
image, err := reader.Open(ctx, *h.Current)
if err != nil {
t.Fatal(err)
}
ref, _, err := image.Resolve(ctx, 1)
if err != nil {
t.Fatal(err)
}
entries := map[uint64]layout.Ref{1: ref}
me := layout.HolderID{Epoch: 1, Holder: "s/r", ClaimID: "k"}
begun, err := layout.RunBegin(ctx, st, me, "modern-cut")
if err != nil {
t.Fatal(err)
}
root, segments, _ := layout.Build(entries)
rn, rb, _ := layout.NameRoot(root)
objects := map[string][]byte{rn.Path(): rb}
for name, data := range segments {
objects[name.Path()] = data
}
for name, data := range objects {
if err := layout.EnsureObject(ctx, st, name, data, "modern-cut"); err != nil {
t.Fatal(err)
}
}
committed, err := layout.RunCommit(ctx, st, st, v.Name, me, layout.CommitRequest{Session: "modern-cut", Prev: entries, Next: entries, Position: 3, Snapshot: snapshot, V11: true}, layout.CauseCommit)
if err != nil {
t.Fatal(err)
}
for name, entry := range committed.Snapshots {
if entry.Gen != begun.Gen+1 {
t.Fatalf("snapshot %s generation=%d want=%d", name, entry.Gen, begun.Gen+1)
}
args := cliArgs("snapshot-delete", volArgs("volume", v), []string{"--name", name, "--request-id", "delete-" + name})
code, out, logs := runChunk(args...)
if code != 0 {
t.Fatalf("delete %s: code=%d %s %s", name, code, out, logs)
}
if _, err := layout.RunConsumeSnapshotDeleteIntent(ctx, st, reader); err != nil {
t.Fatal(err)
}
}
after, _, err := st.ReadHead(ctx)
if err != nil || len(after.Snapshots) != 0 {
t.Fatalf("snapshots remain: %+v err=%v", after, err)
}
})
}
}
@@ -167,7 +167,14 @@ func (h Head) Commit(me HolderID, session string, root RootName, position uint64
meta.Operation.Epoch = h.Epoch
next.Version = 1
}
next.Snapshots[snapshot] = Snapshot{Root: root, Position: position, Meta: meta}
var publishingGen uint64
if next.Version == HeadVersion1 {
if h.Gen == ^uint64(0) {
return Head{}, false, ErrHeadGenerationExhausted
}
publishingGen = h.Gen + 1
}
next.Snapshots[snapshot] = Snapshot{Root: root, Position: position, Gen: publishingGen, Meta: meta}
}
}
rn := root
@@ -374,6 +374,9 @@ func (h Head) check() error {
return fmt.Errorf("%w: %d dependents in HEAD, at most %d", ErrMalformed, n, MaxDependentsPerHead)
}
for n, s := range h.Snapshots {
if h.Version == HeadVersion1 && s.Gen == 0 {
return fmt.Errorf("%w: v1.1 snapshot %q lacks a publishing generation", ErrMalformed, n)
}
if s.Gen != 0 && h.Version == 0 {
return fmt.Errorf("%w: v1.0 snapshot %q carries a publishing generation", ErrMalformed, n)
}
@@ -714,6 +717,20 @@ func runTransition(ctx context.Context, st HeadStore, cause HeadWriteCause, t fu
if err != nil || !changed {
return next, err
}
// Every modern publication stamps inherited legacy entries at this CAS,
// before validation and the receipt guard observe the proposed HEAD.
if next.Version == HeadVersion1 {
if h.Gen == ^uint64(0) {
return Head{}, ErrHeadGenerationExhausted
}
next = next.clone()
for name, snapshot := range next.Snapshots {
if snapshot.Gen == 0 {
snapshot.Gen = h.Gen + 1
next.Snapshots[name] = snapshot
}
}
}
if err := next.check(); err != nil {
return Head{}, err
}
@@ -38,6 +38,7 @@ func fullestHead(sized bool) Head {
s.Dependents = deps(root, targets...)
if sized {
s.ManifestBytes = u64(math.MaxUint64)
s.Gen = math.MaxUint64
}
h.Snapshots[wide(MaxSnapshotNameBytes, i)] = s
}
@@ -49,7 +50,7 @@ func fullestHead(sized bool) Head {
// The v1.1 fields fit HEAD's inline limit with room to spare: the fullest
// HEAD the bounds allow, with every size at its widest, encodes under
// MaxHeadBytes; v1.1 adds at most 512 bytes to it; and at least 16 KiB stays
// MaxHeadBytes; v1.1 adds at most 1024 bytes to it; and at least 16 KiB stays
// free for ext (the ext contract's budget: a writer's own ext must keep the
// fullest HEAD under the limit, which Encode enforces).
func TestTheFullestHeadFitsWithTheV11Sizes(t *testing.T) {
@@ -63,8 +64,8 @@ func TestTheFullestHeadFitsWithTheV11Sizes(t *testing.T) {
}
added, free := len(v1)-len(v0), MaxHeadBytes-len(v1)
t.Logf("fullest HEAD: v1.0 %d bytes, v1.1 %d (+%d), %d free of %d", len(v0), len(v1), added, free, MaxHeadBytes)
if added > 512 {
t.Fatalf("v1.1 adds %d bytes to the fullest HEAD, want at most 512", added)
if added > 1024 {
t.Fatalf("v1.1 adds %d bytes to the fullest HEAD, want at most 1024", added)
}
if free < 16<<10 {
t.Fatalf("the fullest v1.1 HEAD leaves %d bytes, want at least 16 KiB for ext", free)
@@ -20,7 +20,7 @@ func TestHeadVersion1RoundTripsWithItsExtVerbatim(t *testing.T) {
h.Current, h.Position = &root, 9
h.Version, h.CurrentBytes = HeadVersion1, u64(4096)
h.Ext = json.RawMessage(`{"later":{"a":1}}`)
h.Snapshots = map[string]Snapshot{"s1": {Root: root, Position: 9, ManifestBytes: u64(4096)}}
h.Snapshots = map[string]Snapshot{"s1": {Root: root, Gen: 7, Position: 9, ManifestBytes: u64(4096)}}
b, err := h.Encode()
if err != nil {
t.Fatal(err)
@@ -145,6 +145,12 @@ func kernelCell(t *testing.T, state, entry string) string {
cursor := ListCursor{Ref: ref, Kind: p.Kind, Producer: p.Producer, Generation: p.Generation, Target: p.Target}
if nv.memHead.h != nil {
nv.memHead.h.Version = HeadVersion1
for name, snapshot := range nv.memHead.h.Snapshots {
if snapshot.Gen == 0 {
snapshot.Gen = h.Gen
nv.memHead.h.Snapshots[name] = snapshot
}
}
if sn, found := nv.memHead.h.Snapshots["daily"]; found {
sn.Gen = 4
sn.Dependents = nil
@@ -173,6 +179,12 @@ func kernelCell(t *testing.T, state, entry string) string {
} else {
if nv.memHead.h != nil {
nv.memHead.h.Version = HeadVersion1
for name, snapshot := range nv.memHead.h.Snapshots {
if snapshot.Gen == 0 {
snapshot.Gen = h.Gen
nv.memHead.h.Snapshots[name] = snapshot
}
}
nv.memHead.h.List = cursor.clone()
}
_, err = RunAdvanceList(ctx, cs, me, "c1", func(context.Context, ListPage) error { return nil })
@@ -131,7 +131,7 @@ func (s *chainStore) ReadListPage(_ context.Context, ref ListRef) ([]byte, bool,
func chainFixture(t *testing.T) (*chainStore, ListCursor, ListPage) {
t.Helper()
h := snapHead("s1")
h.Version = HeadVersion1
h, _, _ = h.StampSnapshotGenerations()
s := &chainStore{memHead: &memHead{h: &h}, memObjects: newMemObjects(volRoot)}
s.volume = h.VolumeID
p := ListPage{VolumeID: h.VolumeID, Kind: ListDrop, Producer: "s1", Generation: h.Gen + 1, Pages: 1,
@@ -8,7 +8,7 @@ import (
// ErrSnapshotGenerationMissing refuses admission against an unstamped legacy
// snapshot. Zero is not an incarnation and cannot be a delete target.
var ErrSnapshotGenerationMissing = errors.New("layout: snapshot needs its v1.1 publishing generation: start a v1.1 holder to upgrade HEAD")
var ErrSnapshotGenerationMissing = errors.New("layout: snapshot needs its v1.1 publishing generation: the first v1.1 HEAD write stamps legacy snapshots")
// ErrHeadGenerationExhausted refuses publication instead of wrapping an
// incarnation to zero.
@@ -181,31 +181,12 @@ func TestRunDeleteSnapshotAt_InterruptAtEveryStep(t *testing.T) {
})
}
func TestMixedLegacyStampPreservesPublishedSibling(t *testing.T) {
func TestMixedLegacyVersionOneHeadIsRefused(t *testing.T) {
h := idleHead()
h.Version = HeadVersion1
h.Snapshots = map[string]Snapshot{"old": {Root: RootName{}, Gen: 4}, "legacy": {Root: RootName{}}}
next, changed, err := h.StampSnapshotGenerations()
if err != nil || !changed {
t.Fatalf("mixed stamp: %v %v", changed, err)
}
if next.Snapshots["old"].Gen != 4 || next.Snapshots["legacy"].Gen != 8 {
t.Fatalf("published identity reminted: %+v", next.Snapshots)
}
if h.Snapshots["old"].Gen != 4 || h.Snapshots["legacy"].Gen != 0 {
t.Fatal("pre-CAS input changed")
}
raw, err := next.Encode()
if err != nil {
t.Fatal(err)
}
reread, err := DecodeHead(raw, 8, h.VolumeID)
if err != nil {
t.Fatal(err)
}
retry, changed, err := reread.StampSnapshotGenerations()
if err != nil || changed || retry.Snapshots["old"].Gen != 4 || retry.Snapshots["legacy"].Gen != 8 {
t.Fatalf("mixed stamp reply-loss retry: %+v %v %v", retry.Snapshots, changed, err)
if _, _, err := h.StampSnapshotGenerations(); err == nil {
t.Fatal("invalid persisted modern HEAD repaired instead of refused")
}
}
@@ -236,3 +217,28 @@ func TestSnapshotDeletePreservesPublishedSibling(t *testing.T) {
t.Fatalf("persisted survivor: %v", err)
}
}
func TestVersionOneHeadRefusesUnstampedSnapshot(t *testing.T) {
h := idleHead()
h.Version = HeadVersion1
h.Snapshots = map[string]Snapshot{"legacy": {Root: RootName{}}}
if _, err := h.Encode(); err == nil {
t.Fatal("encoded version-1 HEAD with gen-0 snapshot")
}
}
func TestSnapshotOperationEntryUsesBoundPublishingGeneration(t *testing.T) {
_, h, req := commitFixture(t)
meta := SnapshotMeta{Operation: &SnapshotOperationBinding{ID: "create", Instance: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", Owner: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", Sequence: 1, Fingerprint: "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"}}
next, changed, err := h.Commit(me, req.Session, RootName{}, req.Position, req.Snapshot, meta, StateLive)
if err != nil || !changed {
t.Fatalf("commit: changed=%t err=%v", changed, err)
}
snapshot := next.Snapshots[req.Snapshot]
if snapshot.Gen != h.Gen+1 || snapshot.Gen != snapshot.Meta.Operation.Generation {
t.Fatalf("entry and receipt generation diverge: %+v", snapshot)
}
if h.Snapshots[req.Snapshot].Gen != 0 {
t.Fatal("source HEAD mutated")
}
}
@@ -451,6 +451,7 @@ func TestSnapshotOperationBoundReceiptCanBeRebuiltAndRetiredBeforeRemoval(t *tes
}
sn := h.Snapshots["s"]
sn.Meta.Operation = &layout.SnapshotOperationBinding{ID: op.ID, Instance: op.Instance, Owner: op.Owner, Sequence: op.Sequence, Fingerprint: op.Fingerprint, Generation: h.Gen + 1, Epoch: h.Epoch}
sn.Gen = sn.Meta.Operation.Generation
h.Snapshots["s"] = sn
h.Version = 1
if err := store.UpdateHead(ctx, h.Gen, h, layout.CauseCommit); err != nil {
@@ -112,6 +112,7 @@ func TestSnapshotOperationSplitPublishReadmissionSettlesOneBinding(t *testing.T)
next.Version = 1
sn := next.Snapshots["s"]
sn.Meta.Operation = &layout.SnapshotOperationBinding{ID: op.ID, Instance: op.Instance, Owner: op.Owner, Sequence: op.Sequence, Fingerprint: op.Fingerprint, Generation: h.Gen + 1, Epoch: h.Epoch}
sn.Gen = sn.Meta.Operation.Generation
next.Snapshots["s"] = sn
if err := st.GuardSnapshotOperations(ctx, h, next); err != nil {
t.Fatal(err)
@@ -183,6 +184,7 @@ func TestSnapshotOperationBindingCanonicalRoundTrip(t *testing.T) {
}
sn := h.Snapshots["s"]
sn.Meta.Operation = &layout.SnapshotOperationBinding{ID: "original-op", Instance: strings.Repeat("a", 32), Owner: strings.Repeat("b", 32), Sequence: 1, Fingerprint: strings.Repeat("c", 64), Generation: h.Gen, Epoch: h.Epoch}
sn.Gen = sn.Meta.Operation.Generation
h.Snapshots["s"] = sn
h.Version = 1
raw, err := h.Encode()
+1 -1
View File
@@ -1374,7 +1374,7 @@ Notes:
| ID | Statement | Transition | Enforced in | Guard | Model | Status |
|---|---|---|---|---|---|---|
| `INV-SNAPSHOT-INCARNATION-001` | A snapshot identity is its name and immutable publishing HEAD generation within the volume ID. A resumed delete removes only its admitted incarnation; a recreated name is preserved. Zero requires legacy stamping. | M9 snapshot publication and target-qualified removal | `layout/head.go`, `layout/snapshot_generation.go`; producer integration pending | `TestSnapshotPublishingGenerationGolden`, `TestLegacySnapshotStampKeepsOneIdentityAcrossRetry`, `TestSnapshotDeleteTargetSparesAnUnchangedImageRecreation`, `TestSnapshotDeleteCASConflictKeepsCapturedIncarnation` | SnapDelete rows 241-245 | STUB |
| `INV-SNAPSHOT-INCARNATION-001` | A snapshot identity is its name and immutable publishing HEAD generation within the volume ID. A resumed delete removes only its admitted incarnation; a recreated name is preserved. Every version-1 publication stamps legacy entries at base+1; zero is refused. | M9 snapshot publication and target-qualified removal | `layout/head.go` `runTransition`, `check`; `layout/commit.go` `Commit`; `layout/snapshot_generation.go` | `TestSnapshotPublishingGenerationGolden`, `TestLegacySnapshotStampKeepsOneIdentityAcrossRetry`, `TestSnapshotDeleteTargetSparesAnUnchangedImageRecreation`, `TestSnapshotDeleteCASConflictKeepsCapturedIncarnation`, `TestSnapshotDeletePublicAfterV11CommitStampsEveryIncarnation`, `TestVersionOneHeadRefusesUnstampedSnapshot` | SnapDelete rows 241-245 | ACTIVE |
### Immutable list continuation integration
+2 -1
View File
@@ -491,7 +491,8 @@ invocation with the same ID and without `--no-wait` executes or resumes it.
The CLI is the offline executor: no daemon continues a no-wait delete. Success
means the logical unlink and durable reclamation intent committed; physical
reclamation continues independently. A legacy snapshot must first be stamped by
a v1.1 holder; `snapshot_generation_unavailable` is retryable and does not delete.
the first v1.1 HEAD write by a v1.1 holder (a cut or snapshot commit stamps
every legacy entry in that write); `snapshot_generation_unavailable` is retryable and does not delete.
The name-only example above applies to legacy v1.0 HEADs.
`snapshots` answers a JSON array with one entry per published snapshot, in name