From fd2ea05307e8691c18fbcdec6978ea2ed93fffb1 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 17 Nov 2026 20:35:12 -0800 Subject: [PATCH] fix(durable): defer BASE retirement until local presence is durable --- .../core/frontend/durable/provider.go | 8 ++ .../durable/provider_format_v3_base_test.go | 103 ++++++++++++++++++ seaweed-block/core/storage/chunk_base_v3.go | 6 + .../core/storage/chunk_base_v3_test.go | 7 ++ seaweed-block/core/storage/chunk_layer.go | 3 + seaweed-block/docs/design/invariants.md | 2 +- 6 files changed, 128 insertions(+), 1 deletion(-) diff --git a/seaweed-block/core/frontend/durable/provider.go b/seaweed-block/core/frontend/durable/provider.go index f45d5a1c71..96e4536584 100644 --- a/seaweed-block/core/frontend/durable/provider.go +++ b/seaweed-block/core/frontend/durable/provider.go @@ -685,6 +685,10 @@ func (p *DurableProvider) openExisting(volumeID, path string, impl ImplName) (*v // G6 §1.A α: plumb operator-tunable retention into walstore. // Zero (pre-G6 default) preserves strict checkpoint-driven // recycle. + // Defer before Recover can publish a completed BASE away. + if impl == ImplWALStoreChunk && p.cfg.ChunkRemoteFor != nil { + ws.DeferBaseRetirementV3() + } if p.cfg.WALRetentionLSNs > 0 { ws.SetRecoveryRetentionLSNs(p.cfg.WALRetentionLSNs) } @@ -735,6 +739,10 @@ func (p *DurableProvider) createFresh(volumeID, path string, impl ImplName) (*vo return nil, fmt.Errorf("durable: CreateWALStore %s: %w", path, err) } // G6 §1.A α (see openExisting comment for semantics). + // Defer before Recover can publish a completed BASE away. + if impl == ImplWALStoreChunk && p.cfg.ChunkRemoteFor != nil { + ws.DeferBaseRetirementV3() + } if p.cfg.WALRetentionLSNs > 0 { ws.SetRecoveryRetentionLSNs(p.cfg.WALRetentionLSNs) } diff --git a/seaweed-block/core/frontend/durable/provider_format_v3_base_test.go b/seaweed-block/core/frontend/durable/provider_format_v3_base_test.go index 45667982ab..96ea3a6d40 100644 --- a/seaweed-block/core/frontend/durable/provider_format_v3_base_test.go +++ b/seaweed-block/core/frontend/durable/provider_format_v3_base_test.go @@ -90,6 +90,15 @@ func TestDurableProviderV3_AttachesCompletedChunkBASE(t *testing.T) { // A complete local BASE is not an empty fresh restore, even while the // provider's first-attach marker exists and HEAD still names an older image. func TestDurableProviderV3_CompletedBASEDoesNotRestoreOlderHEAD(t *testing.T) { + completedBASEDoesNotRestoreOlderHEAD(t, false) +} + +func TestDurableProviderV3_DefersBASERetirementUntilLocalPresence(t *testing.T) { + completedBASEDoesNotRestoreOlderHEAD(t, true) +} + +func completedBASEDoesNotRestoreOlderHEAD(t *testing.T, retire bool) { + t.Helper() ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) defer cancel() remote := storage.NewMemChunkRemote() @@ -136,9 +145,36 @@ func TestDurableProviderV3_CompletedBASEDoesNotRestoreOlderHEAD(t *testing.T) { if e = h.WriteBase(9, make([]byte, 4096)); e != nil { t.Fatal(e) } + if retire { + if e = h.ApplyInstallEntry(5, want, 8); e != nil { + t.Fatal(e) + } + } if e = h.MarkBaseComplete(); e != nil { t.Fatal(e) } + if retire { + if _, e = st.Sync(); e != nil { + t.Fatal(e) + } + if e = storage.FlushMappedWALStoreV3Test(st.(*storage.WALStore)); e != nil { + t.Fatal(e) + } + state, _ := storage.BaseStateV3Test(st.(*storage.WALStore)) + if _, ok := st.(*storage.WALStore).InstalledBaseLSN(); !ok || state != storage.BaseInstalled || st.(*storage.WALStore).CheckpointLSN() < 8 { + t.Fatalf("BASE retired before durable local presence: state=%v checkpoint=%d", state, st.(*storage.WALStore).CheckpointLSN()) + } + // The provider must restore the opt-in gate before recovery can + // publish a descriptor on a reopened store. + if e = target.Close(); e != nil { + t.Fatal(e) + } + target = mappedProviderV3(t, root, remote) + st, e = target.EnsureStorage("v1") + if e != nil { + t.Fatal(e) + } + } if _, e = target.RecoverVolume(ctx, "v1"); e != nil { t.Fatalf("completed local BASE was treated as empty remote restore: %v", e) } @@ -153,6 +189,18 @@ func TestDurableProviderV3_CompletedBASEDoesNotRestoreOlderHEAD(t *testing.T) { } check() + if retire { + if _, e = st.Sync(); e != nil { + t.Fatal(e) + } + if e = storage.FlushMappedWALStoreV3Test(st.(*storage.WALStore)); e != nil { + t.Fatal(e) + } + state, _ := storage.BaseStateV3Test(st.(*storage.WALStore)) + if state != storage.BaseNone { + t.Fatalf("durable local presence did not release retirement: %v", state) + } + } if e = target.Close(); e != nil { t.Fatal(e) } @@ -165,3 +213,58 @@ func TestDurableProviderV3_CompletedBASEDoesNotRestoreOlderHEAD(t *testing.T) { } check() } + +// A seeded checkpoint is not proof that any extent data was materialized. +func TestDurableProviderV3_SeedOnlyCheckpointStillRestoresHEAD(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + defer cancel() + remote := storage.NewMemChunkRemote() + source := mappedProviderV3(t, t.TempDir(), remote) + if _, err := source.EnsureStorage("v1"); err != nil { + t.Fatal(err) + } + if _, err := source.RecoverVolume(ctx, "v1"); err != nil { + t.Fatal(err) + } + want := bytes.Repeat([]byte{0x4d}, 4096) + if n, err := source.Backend("v1").Write(ctx, 5*4096, want); err != nil || n != len(want) { + t.Fatal(n, err) + } + if err := source.Backend("v1").Sync(ctx); err != nil { + t.Fatal(err) + } + if _, err := source.CaptureVolume(ctx, "v1"); err != nil { + t.Fatal(err) + } + if err := source.Close(); err != nil { + t.Fatal(err) + } + head, err := remote.GetHead(ctx) + if err != nil { + t.Fatal(err) + } + target := mappedProviderV3(t, t.TempDir(), remote) + st, err := target.EnsureStorage("v1") + if err != nil { + t.Fatal(err) + } + if _, err = st.Recover(); err != nil { + t.Fatal(err) + } + if err = st.(*storage.WALStore).SeedChunkFrontier(head.RemoteLSN); err != nil { + t.Fatal(err) + } + if st.(*storage.WALStore).CheckpointLSN() == 0 || st.NextLSN() <= 1 { + t.Fatal("seed did not establish the ambiguous frontier") + } + if _, ok := st.(*storage.WALStore).InstalledBaseLSN(); ok { + t.Fatal("seed became a completed BASE") + } + if _, err = target.RecoverVolume(ctx, "v1"); err != nil { + t.Fatal(err) + } + got := make([]byte, 4096) + if n, err := target.Backend("v1").Read(ctx, 5*4096, got); err != nil || n != len(got) || !bytes.Equal(got, want) { + t.Fatalf("seed-only restore lost remote bytes: n=%d err=%v equal=%v", n, err, bytes.Equal(got, want)) + } +} diff --git a/seaweed-block/core/storage/chunk_base_v3.go b/seaweed-block/core/storage/chunk_base_v3.go index a3a30503b4..671ca37347 100644 --- a/seaweed-block/core/storage/chunk_base_v3.go +++ b/seaweed-block/core/storage/chunk_base_v3.go @@ -56,7 +56,13 @@ func (l *chunkLayerImpl) holdBaseTransitionV3() (func(), error) { default: l.hdr.Base = chunkBaseNone } + // The barrier saved presence while installing; release sees the + // installed publication only after all lower locks were released. + presenceDurable := l.presenceVer == l.savedPresVer && len(l.absent) == 0 && len(l.localBlocks) == 0 l.stMu.Unlock() + if d.BaseState == BaseInstalled && presenceDurable { + l.s.AllowBaseRetirementV3() + } l.capMu.Unlock() done() }, nil diff --git a/seaweed-block/core/storage/chunk_base_v3_test.go b/seaweed-block/core/storage/chunk_base_v3_test.go index b982a28b75..07ac3461c6 100644 --- a/seaweed-block/core/storage/chunk_base_v3_test.go +++ b/seaweed-block/core/storage/chunk_base_v3_test.go @@ -193,6 +193,7 @@ func TestChunkLayerV3_BASEDrainJoinsClose(t *testing.T) { func TestChunkLayerV3_BASEPresenceBarrierFailureKeepsInstalling(t *testing.T) { s, f, l := mappedBASELayerV3(t) + s.DeferBaseRetirementV3() if _, e := s.Write(5, blk(0x11)); e != nil { t.Fatal(e) } @@ -229,10 +230,16 @@ func TestChunkLayerV3_BASEPresenceBarrierFailureKeepsInstalling(t *testing.T) { if _, e = l.Cut(ctTimeout(t)); !errors.Is(e, errChunkBaseInstalling) { t.Fatal("failed BASE became capturable", e) } + if gate := s.v3.pub.retireGate.Load(); gate != retireDeferredV3 { + t.Fatalf("failed presence barrier released gate: %d", gate) + } l.syncDirFn = realSync if e = h.MarkBaseComplete(); e != nil { t.Fatal("completion retry", e) } + if gate := s.v3.pub.retireGate.Load(); gate != retireAllowedV3 { + t.Fatalf("durable completion did not release gate: %d", gate) + } r, e := l.Cut(ctTimeout(t)) if e != nil { t.Fatal(e) diff --git a/seaweed-block/core/storage/chunk_layer.go b/seaweed-block/core/storage/chunk_layer.go index a09501d28f..9d86d4b287 100644 --- a/seaweed-block/core/storage/chunk_layer.go +++ b/seaweed-block/core/storage/chunk_layer.go @@ -294,6 +294,9 @@ func attachChunkLayerImpl(s *WALStore, remote chunkRemote, cfg chunkLayerConfig) l.cancel() return nil, err } + if localBase { + s.AllowBaseRetirementV3() + } // local presence is durable above if l.hashMode { l.startReconcile() } diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index c424b09376..7c55f6bf8c 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -463,4 +463,4 @@ Format v3 row 4 remains dark. Its typed scan result preserves trusted records, a - `INV-WAL-V3-CHUNK-BASE-001` (ACTIVE for dark mapped v3): begin/complete first take `installMu` exclusive, then the attached layer's whole-cut `capMu` drain, then existing capture/lifecycle/leader/store/descriptor/file-sync admission. The drain is registered with Close; no lower store lock is held while it waits. A pending unknown HEAD CAS refuses begin until settled. A newly published layer invalidates an outer hooks snapshot before descriptor mutation. Unsupported attached layers still refuse. No capture path takes installMu and no checkpoint callback takes capMu. - The release runs after lower locks, before installMu release. It derives the in-memory guard from the actual descriptor on success or failure, invalidates staging/reconciliation generations and keeps dirty coverage conservative. Installing capture has an early typed refusal. Presence becomes local before the existing completion barrier can publish installed; a failure retains installing. Completed BASE attach treats local bytes (including ZERO) as authoritative, persists presence, and captures through the owned UNKNOWN/max-LSN map without a legacy bitmap. A fresh provider marker does not authorize seeding an older HEAD over completed local BASE; the read-only installed-BASE query is revalidated by attachment admission. -- Controls: `TestChunkLayerV3_BASEIncompleteCaptureGuard`, `TestChunkLayerV3_BASEBeginDrainsWholeCut`, `TestChunkLayerV3_BASEBeginRefusesUnsettledCAS`, `TestChunkLayerV3_BASEDrainJoinsClose`, `TestChunkLayerV3_BASEPresenceBarrierFailureKeepsInstalling`, and the three actual provider controls in `provider_format_v3_base_test.go`. The unsupported-layer refusal remains in `TestBaseInstallV3BeginRefusesWithAnUnsupportedAttachedChunkLayer`. BASE descriptor states and retirement predicates are unchanged; this wires the existing M1/C1 transition boundary. The external format model does not prove the chunk publication drain; the deterministic caller controls cover that seam. +- Controls: `TestChunkLayerV3_BASEIncompleteCaptureGuard`, `TestChunkLayerV3_BASEBeginDrainsWholeCut`, `TestChunkLayerV3_BASEBeginRefusesUnsettledCAS`, `TestChunkLayerV3_BASEDrainJoinsClose`, `TestChunkLayerV3_BASEPresenceBarrierFailureKeepsInstalling`, and the three actual provider controls in `provider_format_v3_base_test.go`. The unsupported-layer refusal remains in `TestBaseInstallV3BeginRefusesWithAnUnsupportedAttachedChunkLayer`. BASE descriptor states remain unchanged. Chunk-managed providers opt into deferred retirement immediately after create/open, before Recover. Installed local presence must be durable before attach or the outer completion release allows retirement; the next ordinary publication still checks the existing retirement coverage conditions. The opt-in is re-established on reopen, and unmanaged/unmapped stores retain ordinary retirement. A seed-only checkpoint is not local-image evidence. Controls `TestDurableProviderV3_DefersBASERetirementUntilLocalPresence` and `TestDurableProviderV3_SeedOnlyCheckpointStillRestoresHEAD` distinguish these cases; the presence-failure control also requires the gate remain deferred until successful retry. The external format model does not prove the chunk publication drain; the deterministic caller controls cover that seam.