From 3ffface62abe3f55a1b9db09834b6834ae74e1a7 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Wed, 18 Nov 2026 23:40:53 -0800 Subject: [PATCH] fix(storage): keep map candidates pinned exactly while a slot may name them A candidate prepared successfully and then refused because its proof went stale during the prepare's I/O was left in flight until reopen; both preparation paths now release it before returning ErrMapCoverageStale. A publication whose slot write and fsync succeeded but which a fault latched meanwhile refuses at its success boundary may still be named by the slot on disk. It is now recorded as uncertain, so its candidate stays pinned until reopen instead of being released for reuse. The map's file-size counter now follows every write and truncation. Co-Authored-By: Claude Opus 5.5 --- .../core/storage/chunk_lsn_map_v3.go | 4 + .../core/storage/chunk_map_extents_v3_test.go | 122 +++++++++++++++++- .../core/storage/chunk_map_publication_v3.go | 1 + seaweed-block/core/storage/descriptor_v3.go | 6 +- .../storage/walstore_map_checkpoint_v3.go | 1 + 5 files changed, 129 insertions(+), 5 deletions(-) diff --git a/seaweed-block/core/storage/chunk_lsn_map_v3.go b/seaweed-block/core/storage/chunk_lsn_map_v3.go index 98b8def104..59b7ce6cde 100644 --- a/seaweed-block/core/storage/chunk_lsn_map_v3.go +++ b/seaweed-block/core/storage/chunk_lsn_map_v3.go @@ -392,6 +392,7 @@ func (m *chunkLSNMapV3) prepareLockedV3(g, C uint64, entries []chunkLSNEntryV3, return nil, e } fileBytes = keep + m.live.fileBytes.Store(keep) m.live.truncations.Add(1) } } @@ -402,6 +403,9 @@ func (m *chunkLSNMapV3) prepareLockedV3(g, C uint64, entries []chunkLSNEntryV3, return 0, ErrCounterExhausted } n, e := m.fd.WriteAt(b, int64(at)) + if end := at + uint64(n); end > m.live.fileBytes.Load() { + m.live.fileBytes.Store(end) // also on a failed or short write + } if e == nil && n != len(b) { e = io.ErrShortWrite } diff --git a/seaweed-block/core/storage/chunk_map_extents_v3_test.go b/seaweed-block/core/storage/chunk_map_extents_v3_test.go index 589cb07605..6236402157 100644 --- a/seaweed-block/core/storage/chunk_map_extents_v3_test.go +++ b/seaweed-block/core/storage/chunk_map_extents_v3_test.go @@ -288,9 +288,10 @@ func TestChunkMapExtentsV3ReopenRegistersTheFallbackRoot(t *testing.T) { } } -// A failed fsync after a tail truncation latches the map, and the slot -// roots stay readable: the truncation only ever removes extents above the -// highest live page. +// A failed fsync of the tail truncation itself (the first fsync of the +// prepare) latches the map: the prepare and every later one fail. The slot +// roots stay readable, because the truncation only removes extents above +// the highest live page. func TestChunkMapExtentsV3TruncationFaultKeepsTheSlotRoots(t *testing.T) { m, d, path := extentsMapV3(t) c := prepV3(t, m, 1, nil) @@ -304,10 +305,24 @@ func TestChunkMapExtentsV3TruncationFaultKeepsTheSlotRoots(t *testing.T) { publishV3(m, d, low, top) next := prepV3(t, m, 23, low) publishV3(m, d, next, low) // gen 21 released: the tail is free - m.syncFile = func() error { return os.ErrInvalid } + before := mapFileSizeV3(t, path) + orig, calls := m.syncFile, 0 + m.syncFile = func() error { + calls++ + if calls == 1 { + return os.ErrInvalid // the truncation's fsync + } + return orig() + } if _, err := m.Prepare(24, 7, []chunkLSNEntryV3{{LSN: 124}}, next); err == nil { t.Fatal("prepare across a failed truncation fsync succeeded") } + if calls != 1 || mapFileSizeV3(t, path) >= before { + t.Fatalf("setup: %d fsyncs, file %d B -> %d B; want the truncation's fsync to fail", calls, before, mapFileSizeV3(t, path)) + } + if _, err := m.Prepare(25, 7, []chunkLSNEntryV3{{LSN: 125}}, next); err == nil || calls != 1 { + t.Fatalf("the map did not latch: err %v, fsyncs %d", err, calls) + } if err := m.Close(); err != nil { t.Fatal(err) } @@ -389,3 +404,102 @@ func TestChunkMapExtentsV3StatsAfterReopen(t *testing.T) { t.Fatalf("stats after reopen: %+v, file %d B", st, fi.Size()) } } + +// A candidate that is prepared successfully but whose proof went stale +// during its file I/O (a guest write changed the map meanwhile) is +// refused before publication and must be released at once, so a steady +// stream of such refusals keeps the file bounded. Both preparation paths +// are covered: the checkpoint's and prepareMapPublicationV3. Mutant: not +// abandoning after the proof check pins every refused candidate. +func TestChunkMapExtentsV3ProofStalePreparesStayBounded(t *testing.T) { + s, path := newMappedCheckpointStoreV3(t) + writeSynced(t, s, 1, 1) + if err := s.flusher.flushOnceInternal(false); err != nil { + t.Fatal(err) + } + m := s.v3.ownedMap + orig := m.syncFile + m.syncFile = func() error { // the proof goes stale during the prepare's I/O + if err := s.v3.mapProof.mutatedV3(); err != nil { + return err + } + return orig() + } + cp := s.v3.pub.current().CheckpointLSN + for i := 0; i < 150; i++ { + if _, err := s.prepareOwnedMapCheckpointV3(cp); err != ErrMapCoverageStale { + t.Fatalf("checkpoint prepare %d: err = %v, want ErrMapCoverageStale", i, err) + } + } + g := s.v3.pub.current().MapGeneration + for i := 0; i < 150; i++ { + g += 1000 + entries := append([]chunkLSNEntryV3(nil), s.v3.mapEntries...) + if _, err := prepareMapPublicationV3(m, s.v3.mapProof, g, cp, entries, nil); err != ErrMapCoverageStale { + t.Fatalf("publication prepare %d: err = %v, want ErrMapCoverageStale", i, err) + } + } + m.syncFile = orig + fi, err := os.Stat(path + ".chunkmap") + if err != nil { + t.Fatal(err) + } + if st := s.ChunkMapStatsV3(); fi.Size() > 8*2*4096 || st.LiveBytes > 4*2*4096 { + t.Fatalf("after 300 proof-stale prepares: file %d B, live %d B", fi.Size(), st.LiveBytes) + } +} + +// A publication whose slot write and fsync both happened, but which a fault +// latched meanwhile refuses at its success boundary, may be named by the +// slot on disk. Its candidate stays pinned: an already admitted preparer +// does not reuse its extents, and after a crash the reopen selects that +// slot and its map validates. Mutant: treating that refusal like one before +// slot I/O abandons the candidate and the preparer overwrites it. +func TestChunkMapExtentsV3DurableButRefusedPublicationStaysPinned(t *testing.T) { + path := filepath.Join(t.TempDir(), "mapped") + s, err := createMappedWALStoreV3ForTest(path, 64, 4096, v3TestWAL, WModeStrict, 4) + if err != nil { + t.Fatal(err) + } + s.DisableAutoFlushForRecoveryTest() + for i := uint32(0); i < 4; i++ { + writeSynced(t, s, i, i) + if err := s.flusher.flushOnceInternal(false); err != nil { + t.Fatal(err) + } + } + writeSynced(t, s, 9, 9) + cp := s.v3.pub.current().CheckpointLSN + prepared, err := s.prepareOwnedMapCheckpointV3(cp) + if err != nil { + t.Fatal(err) + } + orig := s.v3.pub.io.fsync + s.v3.pub.io.fsync = func() error { // the slot reaches disk, then a fault latches + err := orig() + s.latchV3(os.ErrInvalid) + return err + } + if _, err := s.v3.pub.publishMapV3(&s.mu, nil, prepared, nil); err == nil { + t.Fatal("a publication refused at its success boundary returned no error") + } + s.v3.pub.io.fsync = orig + m := s.v3.ownedMap + for i := 0; i < 10; i++ { // an already admitted preparer keeps going + entries := append([]chunkLSNEntryV3(nil), s.v3.mapEntries...) + entries[0].LSN += uint64(i + 1) + if _, err := m.prepareNextV3(cp, entries, prepared.candidate); err != nil { + t.Fatalf("admitted prepare %d: %v", i, err) + } + } + crashRecoveryHardeningStore(t, s) + r, err := openWALStoreV3ForTest(path) + if err != nil { + t.Fatalf("reopen of the durable slot: %v", err) + } + r.DisableAutoFlushForRecoveryTest() + t.Cleanup(func() { crashRecoveryHardeningStore(t, r) }) + if g := r.v3.pub.current().MapGeneration; g != prepared.candidate.generation { + t.Fatalf("reopen selected map generation %d, want the durable one %d", g, prepared.candidate.generation) + } +} diff --git a/seaweed-block/core/storage/chunk_map_publication_v3.go b/seaweed-block/core/storage/chunk_map_publication_v3.go index b667906314..8869949475 100644 --- a/seaweed-block/core/storage/chunk_map_publication_v3.go +++ b/seaweed-block/core/storage/chunk_map_publication_v3.go @@ -65,6 +65,7 @@ func prepareMapPublicationV3(m *chunkLSNMapV3, d *mapProofEpochV3, g, C uint64, return nil, e } if !proof.validV3(d) { + m.abandonCandidateV3(c) // prepared but never publishable return nil, ErrMapCoverageStale } return &preparedMapPublicationV3{candidate: c, proof: proof}, nil diff --git a/seaweed-block/core/storage/descriptor_v3.go b/seaweed-block/core/storage/descriptor_v3.go index 64c83d0795..1300ef199a 100644 --- a/seaweed-block/core/storage/descriptor_v3.go +++ b/seaweed-block/core/storage/descriptor_v3.go @@ -257,8 +257,12 @@ func (p *descriptorPublisherV3) publishLockedCoreV3(mutate func(next *descriptor return descriptorV3{}, err // refused before any I/O (a latched fault) } // Success boundary: a fault latched by another path while this - // publication was in flight still refuses it (#172). + // publication was in flight still refuses it (#172). The slot write and + // its fsync did happen, so the slot on disk may name this generation: + // its dependencies stay pinned as an uncertain candidate until reopen. if err := p.io.sticky(); err != nil { + uncertain := next + p.uncertain = &uncertain return descriptorV3{}, err } p.other, p.otherValid = p.cur, true diff --git a/seaweed-block/core/storage/walstore_map_checkpoint_v3.go b/seaweed-block/core/storage/walstore_map_checkpoint_v3.go index 6449d469a0..029d0ba210 100644 --- a/seaweed-block/core/storage/walstore_map_checkpoint_v3.go +++ b/seaweed-block/core/storage/walstore_map_checkpoint_v3.go @@ -44,6 +44,7 @@ func (s *WALStore) prepareOwnedMapCheckpointV3(cp uint64) (*preparedMapPublicati return nil, err } if !proof.validV3(domain) { + v.ownedMap.abandonCandidateV3(c) // prepared but never publishable return nil, ErrMapCoverageStale } return &preparedMapPublicationV3{candidate: c, proof: proof}, nil