From 1fed134e5913fb0a1e23fd1db051021494184edb Mon Sep 17 00:00:00 2001 From: pingqiu Date: Mon, 16 Nov 2026 11:39:00 -0800 Subject: [PATCH 1/3] durable: RED: a move back onto an old root serves and publishes a stale image A workspace moves A -> B -> A and A's old durable root is still on disk. A attaches it as an existing (non-fresh) volume, so it never claims HEAD: it serves its own stale image instead of B's published writes, and its next cut (hash mode) overwrites B's acknowledged, synced and published data in HEAD. Co-Authored-By: Claude Opus 5.5 --- .../durable/provider_chunk_moveback_test.go | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 seaweed-block/core/frontend/durable/provider_chunk_moveback_test.go diff --git a/seaweed-block/core/frontend/durable/provider_chunk_moveback_test.go b/seaweed-block/core/frontend/durable/provider_chunk_moveback_test.go new file mode 100644 index 0000000000..f6bb031d2e --- /dev/null +++ b/seaweed-block/core/frontend/durable/provider_chunk_moveback_test.go @@ -0,0 +1,97 @@ +package durable_test + +import ( + "bytes" + "context" + "testing" + "time" + + "github.com/seaweedfs/seaweed-block/core/frontend" + "github.com/seaweedfs/seaweed-block/core/storage" +) + +func viewAs(replica string, epoch uint64) *stubView { + return newStubView(healthyProj(frontend.Identity{VolumeID: "v1", ReplicaID: replica, Epoch: epoch, EndpointVersion: 1})) +} + +// A workspace moves A -> B -> A, and A's old durable root is still on disk. +// B's acknowledged, synced and published writes must be what A serves after +// the move back, and A's next cut must not overwrite them with A's stale +// image. +func TestWALStoreChunk_MoveBackOntoAnOldRootServesTheNewerHead(t *testing.T) { + ctx := context.Background() + remote := storage.NewMemChunkRemote() + rootA, rootB := t.TempDir(), t.TempDir() + + // 1. A (epoch 3) cuts LBA 0 and LBA 16, then keeps writing without a cut. + pA := chunkProvider(t, rootA, remote, viewAs("rA", 3), 0) + sA := openRecovered(t, pA) + for _, w := range []struct { + lba uint32 + fill byte + }{{0, 0xA1}, {16, 0xA1}} { + if _, err := sA.Write(w.lba, block(w.fill)); err != nil { + t.Fatal(err) + } + } + if _, err := pA.CaptureVolume(ctx, "v1"); err != nil { + t.Fatalf("A capture: %v", err) + } + for _, lba := range []uint32{0, 16} { + if _, err := sA.Write(lba, block(0xA2)); err != nil { // never cut + t.Fatal(err) + } + } + if _, err := sA.Sync(); err != nil { + t.Fatal(err) + } + if err := pA.Close(); err != nil { // A is lost; its root stays on disk + t.Fatal(err) + } + + // 2. A forced move to B (epoch 4, fresh root): B claims HEAD, writes, + // syncs and cuts. + pB := chunkProvider(t, rootB, remote, viewAs("rB", 4), 0) + sB := openRecovered(t, pB) + if got, err := sB.Read(0); err != nil || !bytes.Equal(got, block(0xA1)) { + t.Fatalf("B reads A's published LBA 0: %v", err) + } + if _, err := sB.Write(0, block(0xB1)); err != nil { + t.Fatal(err) + } + if _, err := sB.Sync(); err != nil { + t.Fatal(err) + } + if _, err := pB.CaptureVolume(ctx, "v1"); err != nil { + t.Fatalf("B capture: %v", err) + } + if err := pB.Close(); err != nil { + t.Fatal(err) + } + if !bytes.Equal(headBlock(t, remote, 0), block(0xB1)) { + t.Fatal("HEAD does not hold B's write") + } + + // 3. Back to A (epoch 5) over its old root. + pA2 := chunkProvider(t, rootA, remote, viewAs("rA", 5), 0) + sA2 := openRecovered(t, pA2) + got, err := sA2.Read(0) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(got, block(0xB1)) { + t.Errorf("after the move back, A serves LBA 0 = %02x; want B's published %02x (A's stale image is %02x)", got[0], 0xB1, 0xA2) + } + // A's cut, retried while the layer reconciles (hash mode). + var capErr error + for i := 0; i < 100; i++ { + if _, capErr = pA2.CaptureVolume(ctx, "v1"); capErr == nil { + break + } + time.Sleep(50 * time.Millisecond) + } + t.Logf("A capture after the move back: %v", capErr) + if h := headBlock(t, remote, 0); !bytes.Equal(h, block(0xB1)) { + t.Errorf("after A's cut, HEAD LBA 0 = %02x; B's acked, synced and published %02x was overwritten", h[0], 0xB1) + } +} From 9828967392ff9ecf4e7c030bb1df9974bfbb9502 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Mon, 16 Nov 2026 11:45:08 -0800 Subject: [PATCH 2/3] durable: RED: a move back onto an old root must be refused, visibly Move-back tests for the chunk provider. The existing local root of a workspace that moved A -> B -> A must not attach: no reads or writes, no cut, HEAD unchanged, a typed ErrChunkStaleLocalRoot (fenced) and status evidence that says to remove or quarantine the root, also when the attach is deferred until the assignment arrives. After the root is removed the fresh path serves B's image. Controls that pass today: a same-holder reattach at a newer epoch, an existing root with no HEAD, and a holder whose HEAD a newer epoch took over never cuts over it. ErrChunkStaleLocalRoot is declared so the tests build; nothing returns it. Co-Authored-By: Claude Opus 5.5 --- .../core/frontend/durable/provider_chunk.go | 3 + .../durable/provider_chunk_moveback_test.go | 257 ++++++++++++++---- 2 files changed, 214 insertions(+), 46 deletions(-) diff --git a/seaweed-block/core/frontend/durable/provider_chunk.go b/seaweed-block/core/frontend/durable/provider_chunk.go index 6fe2779609..d9ac0d5150 100644 --- a/seaweed-block/core/frontend/durable/provider_chunk.go +++ b/seaweed-block/core/frontend/durable/provider_chunk.go @@ -43,6 +43,9 @@ var ( ErrChunkEpochStale = errors.New("durable: chunk layer lineage moved since attach") ) +// ErrChunkStaleLocalRoot: declared for the RED tests; nothing returns it yet. +var ErrChunkStaleLocalRoot = errors.New("durable: local volume predates a HEAD published by another holder") + var ( errProviderClosed = errors.New("durable: provider closed") errChunkNoAuthority = errors.New("durable: projection carries no master authority for the chunk layer") diff --git a/seaweed-block/core/frontend/durable/provider_chunk_moveback_test.go b/seaweed-block/core/frontend/durable/provider_chunk_moveback_test.go index f6bb031d2e..9ff6c8a33b 100644 --- a/seaweed-block/core/frontend/durable/provider_chunk_moveback_test.go +++ b/seaweed-block/core/frontend/durable/provider_chunk_moveback_test.go @@ -3,10 +3,15 @@ package durable_test import ( "bytes" "context" + "errors" + "os" + "path/filepath" + "strings" "testing" "time" "github.com/seaweedfs/seaweed-block/core/frontend" + "github.com/seaweedfs/seaweed-block/core/frontend/durable" "github.com/seaweedfs/seaweed-block/core/storage" ) @@ -14,11 +19,30 @@ func viewAs(replica string, epoch uint64) *stubView { return newStubView(healthyProj(frontend.Identity{VolumeID: "v1", ReplicaID: replica, Epoch: epoch, EndpointVersion: 1})) } -// A workspace moves A -> B -> A, and A's old durable root is still on disk. -// B's acknowledged, synced and published writes must be what A serves after -// the move back, and A's next cut must not overwrite them with A's stale -// image. -func TestWALStoreChunk_MoveBackOntoAnOldRootServesTheNewerHead(t *testing.T) { +func writeSync(t *testing.T, s storage.LogicalStorage, lba uint32, fill byte) { + t.Helper() + if _, err := s.Write(lba, block(fill)); err != nil { + t.Fatal(err) + } + if _, err := s.Sync(); err != nil { + t.Fatal(err) + } +} + +func headHolder(t *testing.T, remote *storage.MemChunkRemote) storage.ChunkHead { + t.Helper() + h, err := remote.GetHead(context.Background()) + if err != nil { + t.Fatal(err) + } + return h +} + +// movedAwayAndBack runs A (epoch 3) cut then uncut synced writes, A lost; +// B (epoch 4, fresh) claims, writes LBA 0 = 0xB1, syncs, cuts, closes. It +// returns the remote and A's surviving root. +func movedAwayAndBack(t *testing.T) (*storage.MemChunkRemote, string) { + t.Helper() ctx := context.Background() remote := storage.NewMemChunkRemote() rootA, rootB := t.TempDir(), t.TempDir() @@ -26,25 +50,12 @@ func TestWALStoreChunk_MoveBackOntoAnOldRootServesTheNewerHead(t *testing.T) { // 1. A (epoch 3) cuts LBA 0 and LBA 16, then keeps writing without a cut. pA := chunkProvider(t, rootA, remote, viewAs("rA", 3), 0) sA := openRecovered(t, pA) - for _, w := range []struct { - lba uint32 - fill byte - }{{0, 0xA1}, {16, 0xA1}} { - if _, err := sA.Write(w.lba, block(w.fill)); err != nil { - t.Fatal(err) - } - } + writeSync(t, sA, 0, 0xA1) + writeSync(t, sA, 16, 0xA1) if _, err := pA.CaptureVolume(ctx, "v1"); err != nil { t.Fatalf("A capture: %v", err) } - for _, lba := range []uint32{0, 16} { - if _, err := sA.Write(lba, block(0xA2)); err != nil { // never cut - t.Fatal(err) - } - } - if _, err := sA.Sync(); err != nil { - t.Fatal(err) - } + writeSync(t, sA, 0, 0xA2) // never cut if err := pA.Close(); err != nil { // A is lost; its root stays on disk t.Fatal(err) } @@ -53,45 +64,199 @@ func TestWALStoreChunk_MoveBackOntoAnOldRootServesTheNewerHead(t *testing.T) { // syncs and cuts. pB := chunkProvider(t, rootB, remote, viewAs("rB", 4), 0) sB := openRecovered(t, pB) - if got, err := sB.Read(0); err != nil || !bytes.Equal(got, block(0xA1)) { - t.Fatalf("B reads A's published LBA 0: %v", err) - } - if _, err := sB.Write(0, block(0xB1)); err != nil { - t.Fatal(err) - } - if _, err := sB.Sync(); err != nil { - t.Fatal(err) - } + writeSync(t, sB, 0, 0xB1) if _, err := pB.CaptureVolume(ctx, "v1"); err != nil { t.Fatalf("B capture: %v", err) } if err := pB.Close(); err != nil { t.Fatal(err) } - if !bytes.Equal(headBlock(t, remote, 0), block(0xB1)) { - t.Fatal("HEAD does not hold B's write") - } + return remote, rootA +} - // 3. Back to A (epoch 5) over its old root. +// A workspace moves A -> B -> A, and A's old durable root is still on disk. +// A must not serve its stale image or cut it over B's acknowledged, synced +// and published writes: the existing root is refused (typed, visible, not +// served, HEAD unchanged). Once the old root is removed, A attaches fresh +// from HEAD and reads B's data. +func TestWALStoreChunk_MoveBackOntoAnOldRootIsRefused(t *testing.T) { + ctx := context.Background() + remote, rootA := movedAwayAndBack(t) + before := headHolder(t, remote) + + // 3. Back to A (epoch 5) over its old root: refused. pA2 := chunkProvider(t, rootA, remote, viewAs("rA", 5), 0) - sA2 := openRecovered(t, pA2) - got, err := sA2.Read(0) + if _, err := pA2.EnsureStorage("v1"); err != nil { + t.Fatal(err) + } + _, err := pA2.RecoverVolume(ctx, "v1") + if !errors.Is(err, durable.ErrChunkStaleLocalRoot) || !errors.Is(err, storage.ErrChunkLayerFenced) { + t.Fatalf("move back onto the old root: %v; want ErrChunkStaleLocalRoot (fenced)", err) + } + b, err := pA2.Open(ctx, "v1") if err != nil { t.Fatal(err) } - if !bytes.Equal(got, block(0xB1)) { - t.Errorf("after the move back, A serves LBA 0 = %02x; want B's published %02x (A's stale image is %02x)", got[0], 0xB1, 0xA2) + if _, err := b.Read(ctx, 0, make([]byte, 4096)); !errors.Is(err, frontend.ErrNotReady) { + t.Errorf("read after a refused move back: %v; want ErrNotReady", err) } - // A's cut, retried while the layer reconciles (hash mode). - var capErr error - for i := 0; i < 100; i++ { - if _, capErr = pA2.CaptureVolume(ctx, "v1"); capErr == nil { - break + if _, err := b.Write(ctx, 0, block(0xA3)); !errors.Is(err, frontend.ErrNotReady) { + t.Errorf("write after a refused move back: %v; want ErrNotReady", err) + } + if _, err := pA2.CaptureVolume(ctx, "v1"); err == nil { + t.Error("a refused move back still cuts") + } + var evidence string + for _, st := range pA2.DurableStatuses() { + if st.VolumeID == "v1" { + evidence = st.Evidence } - time.Sleep(50 * time.Millisecond) } - t.Logf("A capture after the move back: %v", capErr) + if !strings.Contains(evidence, "remove or quarantine the local root") || !strings.Contains(evidence, rootA) { + t.Errorf("status evidence %q does not tell the operator what to do with root %s", evidence, rootA) + } + if after := headHolder(t, remote); after.Gen != before.Gen || after.Holder != before.Holder || after.Epoch != before.Epoch { + t.Errorf("HEAD changed by a refused move back: %+v -> %+v", before, after) + } if h := headBlock(t, remote, 0); !bytes.Equal(h, block(0xB1)) { - t.Errorf("after A's cut, HEAD LBA 0 = %02x; B's acked, synced and published %02x was overwritten", h[0], 0xB1) + t.Errorf("HEAD LBA 0 = %02x after a refused move back; want B's %02x", h[0], 0xB1) + } + if err := pA2.Close(); err != nil { + t.Fatal(err) + } + + // 4. The operator removes the old root: A attaches fresh and serves B's + // image (and A's own earlier published chunk). + entries, err := os.ReadDir(rootA) + if err != nil { + t.Fatal(err) + } + for _, e := range entries { + if err := os.RemoveAll(filepath.Join(rootA, e.Name())); err != nil { + t.Fatal(err) + } + } + pA3 := chunkProvider(t, rootA, remote, viewAs("rA", 5), 0) + sA3 := openRecovered(t, pA3) + if got, err := sA3.Read(0); err != nil || !bytes.Equal(got, block(0xB1)) { + t.Fatalf("fresh attach after removing the root: LBA 0 err %v; want B's %02x", err, 0xB1) + } + if got, err := sA3.Read(16); err != nil || !bytes.Equal(got, block(0xA1)) { + t.Fatalf("fresh attach after removing the root: LBA 16 err %v; want the published %02x", err, 0xA1) + } +} + +// The same holder reattaching its own root at a newer epoch is not a move +// back: it attaches, serves its data and cuts. +func TestWALStoreChunk_SameHolderReattachAtNewerEpoch(t *testing.T) { + ctx := context.Background() + remote := storage.NewMemChunkRemote() + root := t.TempDir() + p := chunkProvider(t, root, remote, viewAs("rA", 3), 0) + s := openRecovered(t, p) + writeSync(t, s, 0, 0xC1) + if _, err := p.CaptureVolume(ctx, "v1"); err != nil { + t.Fatal(err) + } + writeSync(t, s, 32, 0xC2) // local only + if err := p.Close(); err != nil { + t.Fatal(err) + } + p2 := chunkProvider(t, root, remote, viewAs("rA", 4), 0) + s2 := openRecovered(t, p2) + if got, _ := s2.Read(32); !bytes.Equal(got, block(0xC2)) { + t.Fatal("same-holder reattach lost its local write") + } + if _, err := p2.CaptureVolume(ctx, "v1"); err != nil { + t.Fatalf("same-holder reattach cannot cut: %v", err) + } + if !bytes.Equal(headBlock(t, remote, 32), block(0xC2)) { + t.Fatal("HEAD lacks the reattached holder's write") + } +} + +// An existing root with no HEAD at all (generation 0) is this holder's own +// image: it attaches. +func TestWALStoreChunk_ExistingRootWithoutHeadAttaches(t *testing.T) { + root := t.TempDir() + p := chunkProvider(t, root, storage.NewMemChunkRemote(), viewAs("rA", 3), 0) + s := openRecovered(t, p) + writeSync(t, s, 0, 0xD1) + if err := p.Close(); err != nil { + t.Fatal(err) + } + empty := storage.NewMemChunkRemote() // no HEAD + if h := headHolder(t, empty); h.Gen != 0 { + t.Fatalf("empty remote HEAD gen %d", h.Gen) + } + p2 := chunkProvider(t, root, empty, viewAs("rA", 3), 0) + s2 := openRecovered(t, p2) + if got, _ := s2.Read(0); !bytes.Equal(got, block(0xD1)) { + t.Fatal("existing root without a HEAD lost its data") + } +} + +// A holder whose HEAD a newer epoch has taken over never cuts over it. +func TestWALStoreChunk_CutRefusedAfterANewerEpochTakesOver(t *testing.T) { + ctx := context.Background() + remote := storage.NewMemChunkRemote() + pA := chunkProvider(t, t.TempDir(), remote, viewAs("rA", 3), 0) + sA := openRecovered(t, pA) + writeSync(t, sA, 0, 0xE1) + if _, err := pA.CaptureVolume(ctx, "v1"); err != nil { + t.Fatal(err) + } + pB := chunkProvider(t, t.TempDir(), remote, viewAs("rB", 4), 0) + openRecovered(t, pB) // takeover raises HEAD to epoch 4 + before := headHolder(t, remote) + if before.Epoch != 4 || before.Holder != "rB" { + t.Fatalf("takeover HEAD %+v", before) + } + writeSync(t, sA, 0, 0xE2) + if _, err := pA.CaptureVolume(ctx, "v1"); err == nil { + t.Fatal("the epoch-3 holder cut over the epoch-4 HEAD") + } + if after := headHolder(t, remote); after.Gen != before.Gen || after.Holder != "rB" { + t.Fatalf("HEAD moved after the stale holder's cut: %+v -> %+v", before, after) + } + if !bytes.Equal(headBlock(t, remote, 0), block(0xE1)) { + t.Fatal("HEAD lost the published image") + } +} + +// The refusal is visible on the deferred path too: A recovers before the +// master's assignment, and the attach that runs when it arrives is refused +// with the same evidence. +func TestWALStoreChunk_MoveBackRefusedOnDeferredAttach(t *testing.T) { + ctx := context.Background() + remote, rootA := movedAwayAndBack(t) + before := headHolder(t, remote) + view := newStubView(frontend.Projection{}) + pA2 := chunkProvider(t, rootA, remote, view, 0) + if _, err := pA2.EnsureStorage("v1"); err != nil { + t.Fatal(err) + } + if _, err := pA2.RecoverVolume(ctx, "v1"); err != nil { + t.Fatalf("recovery before the assignment: %v", err) + } + view.set(healthyProj(frontend.Identity{VolumeID: "v1", ReplicaID: "rA", Epoch: 5, EndpointVersion: 1})) + deadline := time.Now().Add(5 * time.Second) + var st durable.VolumeStatus + for time.Now().Before(deadline) { + for _, s := range pA2.DurableStatuses() { + if s.VolumeID == "v1" { + st = s + } + } + if strings.Contains(st.Evidence, "remove or quarantine the local root") { + break + } + time.Sleep(20 * time.Millisecond) + } + if st.Operational || !strings.Contains(st.Evidence, "remove or quarantine the local root") { + t.Fatalf("deferred attach of the old root: operational %t evidence %q", st.Operational, st.Evidence) + } + if after := headHolder(t, remote); after.Gen != before.Gen || after.Holder != before.Holder { + t.Fatalf("HEAD changed by a refused deferred attach: %+v -> %+v", before, after) } } From 068d30a6e074262d452b101cc3b3ca68cbbe970f Mon Sep 17 00:00:00 2001 From: pingqiu Date: Mon, 16 Nov 2026 11:45:08 -0800 Subject: [PATCH 3/3] durable: refuse to attach an old local root under another holder's HEAD A workspace that moved A -> B -> A reattached A's surviving local root as an existing volume without claiming HEAD: it served A's stale image and its next (hash-mode) cut overwrote B's acknowledged, synced and published writes. A non-fresh attach now reads HEAD first; if another holder published it (Gen != 0, Holder != this holder), the attach fails closed with ErrChunkStaleLocalRoot wrapping storage.ErrChunkLayerFenced: the volume is not served and the attach is not retried. One log line and the status evidence name the volume, the local root, HEAD's holder, generation and epoch, and say to remove or quarantine the root; the next attach then starts fresh from HEAD. The deferred (authority-wait) attach records the same evidence. Recreating the root automatically is a follow-up. Co-Authored-By: Claude Opus 5.5 --- .../core/frontend/durable/provider_chunk.go | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/seaweed-block/core/frontend/durable/provider_chunk.go b/seaweed-block/core/frontend/durable/provider_chunk.go index d9ac0d5150..3d0f8f45fd 100644 --- a/seaweed-block/core/frontend/durable/provider_chunk.go +++ b/seaweed-block/core/frontend/durable/provider_chunk.go @@ -43,7 +43,13 @@ var ( ErrChunkEpochStale = errors.New("durable: chunk layer lineage moved since attach") ) -// ErrChunkStaleLocalRoot: declared for the RED tests; nothing returns it yet. +// ErrChunkStaleLocalRoot refuses to attach an existing local volume whose +// HEAD another holder has published: the local image predates that HEAD +// (a workspace that moved away and back), so serving or cutting it would +// lose the other holder's acknowledged writes. It wraps +// storage.ErrChunkLayerFenced: the volume is not served and the attach is +// not retried. Removing the local volume lets the next attach start fresh +// from HEAD. var ErrChunkStaleLocalRoot = errors.New("durable: local volume predates a HEAD published by another holder") var ( @@ -174,6 +180,13 @@ func (p *DurableProvider) chunkAuthorityWait(volumeID string, h *volHandle, gen return case errors.Is(err, storage.ErrChunkLayerFenced): log.Printf("durable: chunk layer for volume=%s: %v (not serving)", volumeID, err) + if errors.Is(err, ErrChunkStaleLocalRoot) { + h.attachMu.Lock() + if p.current(h, gen) && h.chunk == nil { + h.backend.SetOperational(false, "chunk layer attach refused: "+err.Error()) + } + h.attachMu.Unlock() + } return case errors.Is(err, errChunkNoAuthority): continue @@ -286,6 +299,23 @@ func (p *DurableProvider) newChunkLayer(ctx context.Context, volumeID string, h return nil }, } + if !h.fresh { + // An existing local volume is this holder's image only while HEAD is + // still this holder's publication. Another holder's HEAD means the + // workspace moved away and back: refuse rather than serve, or cut + // over, the newer image with the stale one. + head, err := remote.GetHead(ctx) + if err != nil { + return nil, fmt.Errorf("durable: read HEAD before attaching existing volume %s: %w", volumeID, err) + } + if head.Gen != 0 && head.Holder != holder { + err := fmt.Errorf("%w: %w: volume %s local root %s, HEAD holder %q gen %d epoch %d, local holder %q; remove or quarantine the local root to attach fresh from HEAD", + ErrChunkStaleLocalRoot, storage.ErrChunkLayerFenced, volumeID, h.path, head.Holder, head.Gen, head.Epoch, holder) + log.Printf("durable: refusing to attach volume=%s root=%s: HEAD holder=%q gen=%d epoch=%d is not this holder %q (remove or quarantine the local root)", + volumeID, h.path, head.Holder, head.Gen, head.Epoch, holder) + return nil, err + } + } if h.fresh { // Attach preparation is gated. Seed the exact observed HEAD before // its takeover CAS; only then finalize lazy attachment and expose I/O.