From 1d732c1da55eba0243037ec1bfd07d4853201f04 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Mon, 30 Nov 2026 08:39:10 -0800 Subject: [PATCH] Keep prepared coverage immutable and bound eager map geometry --- seaweed-block/core/storage/chunk_coverage.go | 11 +++- .../core/storage/chunk_coverage_map.go | 27 +++++++++ .../core/storage/chunk_coverage_test.go | 57 +++++++++++++++++++ .../core/storage/chunk_lsn_map_v3.go | 3 + seaweed-block/docs/design/chunk-coverage.md | 10 +++- .../docs/design/model-code-trace-coverage.tsv | 2 +- 6 files changed, 105 insertions(+), 5 deletions(-) diff --git a/seaweed-block/core/storage/chunk_coverage.go b/seaweed-block/core/storage/chunk_coverage.go index 71df50b356..957a958e25 100644 --- a/seaweed-block/core/storage/chunk_coverage.go +++ b/seaweed-block/core/storage/chunk_coverage.go @@ -31,10 +31,19 @@ type ChunkCoverageSeed struct { // ChunkCoverageReader is the atomic read seam shared by the live store and an // immutable prepared bitmap. The prepared view lets the caller persist record // pages before the store publishes its one-way descriptor fence. +// A read costs O(1), performs no I/O and returns coverage and position together. +// The live store takes lifecycle/store read locks; the prepared reader takes no +// locks and remains immutable after the callback returns. Bounds, unavailable +// recovery/initialization and latched store faults are errors, never CoverageNone. type ChunkCoverageReader interface { Coverage(chunk uint64) (LocalCoverage, uint64, error) } +var ( + _ ChunkCoverageReader = (*WALStore)(nil) + _ ChunkCoverageReader = chunkCoverageSnapshot{} +) + type chunkCoverageSnapshot struct { entries []chunkLSNEntryV3 position uint64 @@ -196,7 +205,7 @@ func (s *WALStore) InitializeChunkCoverage(ctx context.Context, epoch uint64, se return err } prepared := &preparedMapPublicationV3{candidate: candidate, proof: proof} - if err := preparePages(chunkCoverageSnapshot{entries: entries, position: position}); err != nil { + if err := preparePages(chunkCoverageSnapshot{entries: append([]chunkLSNEntryV3(nil), entries...), position: position}); err != nil { prepared.abandonV3() return err } diff --git a/seaweed-block/core/storage/chunk_coverage_map.go b/seaweed-block/core/storage/chunk_coverage_map.go index f040ce5313..d4cb8140df 100644 --- a/seaweed-block/core/storage/chunk_coverage_map.go +++ b/seaweed-block/core/storage/chunk_coverage_map.go @@ -10,6 +10,33 @@ import ( const coveragePagePayload uint64 = 4000 +// Admission for the current eager runtime, not a disk-format limit. Count the +// logical bitmap and entry/reference tables before allocating any of them. +// A later paged runtime can lift this without another on-disk format change. +const coverageShapeBudget uint64 = 1 << 30 + +func coverageShapeAdmitted(shape mapShapeV3, cb uint64) bool { + remaining := coverageShapeBudget + take := func(count, width uint64) bool { + if width == 0 || count > remaining/width { + return false + } + remaining -= count * width + return true + } + if cb > counterMaxV3-63 || !take(shape.chunks, ((cb+63)/64)*8) || !take(shape.chunks, chunkMapEntryMemoryBytes) { + return false + } + for nodes := shape.pages; ; nodes = (nodes + coverageTreeFanout - 1) / coverageTreeFanout { + if !take(nodes, 40) { + return false + } + if nodes <= coverageTreeFanout { + return true + } + } +} + type coverageMapMetadata struct{ zeroPosition uint64 } type coverageMapPreparation struct { format uint32 diff --git a/seaweed-block/core/storage/chunk_coverage_test.go b/seaweed-block/core/storage/chunk_coverage_test.go index 2335bc633c..d6a74e242b 100644 --- a/seaweed-block/core/storage/chunk_coverage_test.go +++ b/seaweed-block/core/storage/chunk_coverage_test.go @@ -2,6 +2,7 @@ package storage import ( "context" + "encoding/binary" "errors" "fmt" "testing" @@ -515,3 +516,59 @@ func TestChunkCoverageHalfFenceReusedMapOpensTheIntactLegacySlot(t *testing.T) { t.Fatal("legacy fallback lost acknowledged bytes") } } + +func TestChunkCoveragePreparedReaderRemainsImmutableAfterWrites(t *testing.T) { + s, _ := newMappedCheckpointStoreV3(t) + s.HoldExtentDrainUntilChunkLayer() + if _, err := s.Recover(); err != nil { + t.Fatal(err) + } + var prepared ChunkCoverageReader + if err := s.InitializeChunkCoverage(context.Background(), 7, func(uint64, uint64) (ChunkCoverageSeed, error) { return ChunkCoverageSeed{}, nil }, func(view ChunkCoverageReader) error { prepared = view; return nil }); err != nil { + t.Fatal(err) + } + if _, err := s.Write(1, make([]byte, 4096)); err != nil { + t.Fatal(err) + } + if _, err := s.Sync(); err != nil { + t.Fatal(err) + } + if c, p, err := s.Coverage(0); err != nil || c != CoverageSome || p != 1 { + t.Fatalf("live %d/%d: %v", c, p, err) + } + if c, p, err := prepared.Coverage(0); err != nil || c != CoverageNone || p != 0 { + t.Fatalf("retained prepared view changed: %d/%d: %v", c, p, err) + } +} + +func TestChunkCoverageRejectsRehashedImpossibleBitmapGeometry(t *testing.T) { + s, _ := newMappedCheckpointStoreV3(t) + s.HoldExtentDrainUntilChunkLayer() + if _, err := s.Recover(); err != nil { + t.Fatal(err) + } + if err := s.InitializeChunkCoverage(context.Background(), 7, func(uint64, uint64) (ChunkCoverageSeed, error) { return ChunkCoverageSeed{}, nil }, coverageNoPages); err != nil { + t.Fatal(err) + } + d := s.v3.pub.current() + const impossibleChunkBlocks = uint64(1) << 63 + // Stop the red before exercising the allocator: the old admission accepts + // petabytes of references here. This assertion is the safe negative control. + if shape, err := mapShapeForV3(d, impossibleChunkBlocks); err == nil { + t.Fatalf("impossible allocation admitted: %+v", shape) + } + root := make([]byte, 4096) + if _, err := s.v3.ownedMap.fd.ReadAt(root, int64(d.MapRootLocator)); err != nil { + t.Fatal(err) + } + binary.LittleEndian.PutUint64(root[56:], impossibleChunkBlocks) + hash := mapDigestV3(mapRootDomainV3, root[:4064]) + copy(root[4064:], hash[:]) + d.MapRootHash = hash + if _, err := s.v3.ownedMap.fd.WriteAt(root, int64(d.MapRootLocator)); err != nil { + t.Fatal(err) + } + if _, _, err := readChunkLSNMapV3(s.v3.ownedMap.fd, d, impossibleChunkBlocks); !errors.Is(err, ErrMapInvalid) { + t.Fatalf("rehashed impossible root answered %v", err) + } +} diff --git a/seaweed-block/core/storage/chunk_lsn_map_v3.go b/seaweed-block/core/storage/chunk_lsn_map_v3.go index 40ddc5fceb..19461d7235 100644 --- a/seaweed-block/core/storage/chunk_lsn_map_v3.go +++ b/seaweed-block/core/storage/chunk_lsn_map_v3.go @@ -67,6 +67,9 @@ func mapShapeForV3(d descriptorV3, cb uint64) (mapShapeV3, error) { } if d.MapFormatID == mapFormatIDCoverage { s.raw, s.aligned = 4096, 4096 + if !coverageShapeAdmitted(s, cb) { + return s, ErrMapInvalid + } return s, nil } s.raw = 120 + 40*s.pages diff --git a/seaweed-block/docs/design/chunk-coverage.md b/seaweed-block/docs/design/chunk-coverage.md index 40dceb238c..d71fd16efa 100644 --- a/seaweed-block/docs/design/chunk-coverage.md +++ b/seaweed-block/docs/design/chunk-coverage.md @@ -53,8 +53,8 @@ domains. A zero locator AND zero hash is a virtual subtree: every entry has the root's default position, Unknown true, and no local bits. A physical page at locator zero has a nonzero hash. Virtual references pin no file extent. -Unchanged leaves and index branches are shared across roots. New pages are -synced before their root, and the root is synced before descriptor publication. +Unchanged leaves and index branches are shared across roots. New leaves are +synced first; index pages and the root share the final sync before descriptor publication. A captured bitmap's data file is synced before preparing its metadata pages. ## Qualification boundary @@ -64,7 +64,11 @@ are integrated and qualified. This draft implements recovery, initialization, WAL write/replay, and checkpoint observations. Fill/BASE bitmap updates and metadata-only persistence still need integration. The disk tree allows bounded fanout, but the current runtime retains full entry/reference arrays and scans -them during preparation. No proportional runtime cost or scale claim is made. +them during preparation. No proportional runtime cost or scale claim is made. Before allocation, eager +runtime admission limits the combined logical bitmap and entry/reference tables +to a 1-GiB shape estimate. This is a runtime guard, not an on-disk format limit +or a total resident-memory promise; larger geometry refuses with ErrMapInvalid +until a paged implementation is qualified. ## Open half-fence recovery premise diff --git a/seaweed-block/docs/design/model-code-trace-coverage.tsv b/seaweed-block/docs/design/model-code-trace-coverage.tsv index e7d0198c36..a7350d7c6b 100644 --- a/seaweed-block/docs/design/model-code-trace-coverage.tsv +++ b/seaweed-block/docs/design/model-code-trace-coverage.tsv @@ -1,5 +1,5 @@ model item go_function tests status -SlotFence prepare pages before publishing the field core/storage/chunk_coverage.go:WALStore.InitializeChunkCoverage core/storage/chunk_coverage_test.go:TestChunkCoveragePreparedViewPrecedesTheFence STUB +SlotFence prepare pages before publishing the field core/storage/chunk_coverage.go:WALStore.InitializeChunkCoverage core/storage/chunk_coverage_test.go:TestChunkCoveragePreparedViewPrecedesTheFence; core/storage/chunk_coverage_test.go:TestChunkCoveragePreparedReaderRemainsImmutableAfterWrites STUB MoveWide2 durable per-block map premise core/storage/chunk_coverage_map.go:readCoverageMapPagesV3; core/storage/walstore_map_checkpoint_v3.go:prepareOwnedMapCheckpointV3 core/storage/chunk_coverage_test.go:TestChunkCoverageCheckpointKeepsAllAcrossAPageBoundary STUB MoveWide2 WAL suffix restores local bits core/storage/walstore_map_checkpoint_v3.go:recordMapReplayV3 core/storage/chunk_coverage_test.go:TestChunkCoverageAcknowledgedWriteSurvivesReplay STUB MoveWide2 Drop before FinishAttach core/storage/chunk_coverage.go:WALStore.InitializeChunkCoverage; core/storage/walstore_map_checkpoint_v3.go:recordMapReplayV3 core/storage/chunk_coverage_test.go:TestChunkCoverageNewEpochDoesNotReplayClearedLocalWork STUB