From 0a6d952817562149fa504e5b07dd1d19bd0e9d65 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 1 Dec 2026 13:05:27 -0800 Subject: [PATCH] Guard mixed snapshot stamps and surviving sibling identities --- .../chunkstore/layout/snapshot_generation.go | 2 +- .../layout/snapshot_generation_test.go | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/seaweed-block/core/frontend/chunkstore/layout/snapshot_generation.go b/seaweed-block/core/frontend/chunkstore/layout/snapshot_generation.go index f51b980b3f..9ebe018162 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/snapshot_generation.go +++ b/seaweed-block/core/frontend/chunkstore/layout/snapshot_generation.go @@ -47,7 +47,7 @@ func (h Head) StampSnapshotGenerations() (Head, bool, error) { // DeleteSnapshotAt removes only an already admitted incarnation. Admission // resolves initial absence separately; a retained target that has since gone // or been replaced succeeds without removing anything. Existing dependent -// and volume-state checks still apply to an actual removal. +// checks apply to actual removal; volume-state checks also apply to no-ops. func (h Head) DeleteSnapshotAt(name string, gen uint64) (Head, bool, error) { if gen == 0 { return Head{}, false, ErrSnapshotGenerationMissing diff --git a/seaweed-block/core/frontend/chunkstore/layout/snapshot_generation_test.go b/seaweed-block/core/frontend/chunkstore/layout/snapshot_generation_test.go index 48783a16bf..47aac90c73 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/snapshot_generation_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout/snapshot_generation_test.go @@ -180,3 +180,59 @@ func TestRunDeleteSnapshotAt_InterruptAtEveryStep(t *testing.T) { } }) } + +func TestMixedLegacyStampPreservesPublishedSibling(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) + } +} + +func TestSnapshotDeletePreservesPublishedSibling(t *testing.T) { + h := idleHead() + h.Version = HeadVersion1 + h.Snapshots = map[string]Snapshot{"remove": {Root: RootName{}, Gen: 7}, "keep": {Root: RootName{}, Gen: 4}} + st := &memHead{h: &h} + next, err := RunDeleteSnapshotAt(t.Context(), st, "remove", 7) + if err != nil { + t.Fatal(err) + } + if len(next.Snapshots) != 1 || next.Snapshots["keep"].Gen != 4 { + t.Fatalf("survivor identity lost: %+v", next.Snapshots) + } + if len(h.Snapshots) != 2 || h.Snapshots["keep"].Gen != 4 || h.Snapshots["remove"].Gen != 7 { + t.Fatal("source HEAD mutated") + } + raw, err := next.Encode() + if err != nil { + t.Fatal(err) + } + if !bytes.Contains(raw, []byte(`"gen":4`)) { + t.Fatalf("survivor bytes lost generation: %s", raw) + } + reread, err := DecodeHead(raw, next.Gen, h.VolumeID) + if err != nil || reread.Snapshots["keep"].Gen != 4 { + t.Fatalf("persisted survivor: %v", err) + } +}