diff --git a/seaweed-block/core/storage/base_install_v3_test.go b/seaweed-block/core/storage/base_install_v3_test.go index 8479271a72..43e2ddf6e6 100644 --- a/seaweed-block/core/storage/base_install_v3_test.go +++ b/seaweed-block/core/storage/base_install_v3_test.go @@ -427,7 +427,7 @@ func TestBaseInstallV3CompletePersistsTheMapUnknownOfBaseWrites(t *testing.T) { if err := s.flusher.flushOnceInternal(false); err != nil { t.Fatal(err) } - if e := s.v3.pub.mapCurrent.entries[2]; e.Unknown { + if e := s.v3.pub.mapCurrent.entries.at(int(2)); e.Unknown { t.Fatal("setup: chunk 2 is not clean in the durable root") } h := beginV3(t, s, 1, idA) @@ -447,7 +447,7 @@ func TestBaseInstallV3CompletePersistsTheMapUnknownOfBaseWrites(t *testing.T) { if st := r.v3.pub.current().BaseState; st != BaseInstalled { t.Fatalf("reopen state %d, want installed", st) } - if e := r.v3.pub.mapCurrent.entries[2]; !e.Unknown { + if e := r.v3.pub.mapCurrent.entries.at(int(2)); !e.Unknown { t.Fatal("chunk 2 reopened clean: the BASE write's UNKNOWN was lost at complete") } } diff --git a/seaweed-block/core/storage/checkpoint_append_progress_v3_test.go b/seaweed-block/core/storage/checkpoint_append_progress_v3_test.go index 246dbcd8ab..78ff83eac9 100644 --- a/seaweed-block/core/storage/checkpoint_append_progress_v3_test.go +++ b/seaweed-block/core/storage/checkpoint_append_progress_v3_test.go @@ -493,7 +493,7 @@ func TestCheckpointV3_SpeculativeMaximumAfterDroppedSuffix(t *testing.T) { t.Fatal(err) } d := s.v3.pub.current() - e := prepared.candidate.entries[0] + e := prepared.candidate.entries.at(int(0)) if e.LSN != 3 || e.Unknown || d.CheckpointLSN != 1 || d.W != 1 { t.Fatalf("speculative maximum: entry%+v cp%d W%d", e, d.CheckpointLSN, d.W) } @@ -524,7 +524,7 @@ func TestCheckpointV3_SpeculativeMaximumAfterDroppedSuffix(t *testing.T) { if err != nil { t.Fatal(err) } - if held.candidate.entries[0].Unknown { + if held.candidate.entries.at(int(0)).Unknown { t.Fatal("setup: held candidate already UNKNOWN") } if lsn, err := r.Write(1, blk(0xB2)); err != nil || lsn != 2 { diff --git a/seaweed-block/core/storage/chunk_birth_export_test.go b/seaweed-block/core/storage/chunk_birth_export_test.go index 08795edc5e..8c5561b6bf 100644 --- a/seaweed-block/core/storage/chunk_birth_export_test.go +++ b/seaweed-block/core/storage/chunk_birth_export_test.go @@ -34,7 +34,7 @@ func DroppedSuffixBirthStoreForTest(t *testing.T, remote ChunkRemote) *WALStore t.Fatal(err) } d := s.v3.pub.current() - if e := prepared.candidate.entries[0]; e.LSN != 3 || d.CheckpointLSN != 1 || d.W != 1 { + if e := prepared.candidate.entries.at(int(0)); e.LSN != 3 || d.CheckpointLSN != 1 || d.W != 1 { t.Fatalf("setup entry%+v cp%d W%d", e, d.CheckpointLSN, d.W) } if _, err := s.v3.pub.publishMapV3(&s.mu, nil, prepared, nil); err != nil { diff --git a/seaweed-block/core/storage/chunk_coverage_map.go b/seaweed-block/core/storage/chunk_coverage_map.go index 82a9cf2428..4ce5e55221 100644 --- a/seaweed-block/core/storage/chunk_coverage_map.go +++ b/seaweed-block/core/storage/chunk_coverage_map.go @@ -27,6 +27,18 @@ func coverageShapeAdmitted(shape mapShapeV3, cb uint64) bool { if cb > counterMaxV3-63 || !take(shape.chunks, ((cb+63)/64)*8) || !take(shape.chunks, chunkMapEntryMemoryBytes) { return false } + // Candidate entry values have immutable leaf headers and bounded-fanout + // branch arrays in addition to the entry storage accounted above. + entryPages := (shape.chunks + mapEntryLeafSize - 1) / mapEntryLeafSize + if !take(entryPages, mapEntryNodeMemoryBytes) { + return false + } + for entryPages > 1 { + entryPages = (entryPages + mapEntryFanout - 1) / mapEntryFanout + if !take(entryPages, mapEntryNodeMemoryBytes+mapEntryBranchMemoryBytes) { + return false + } + } for nodes := shape.pages; ; nodes = (nodes + coverageTreeFanout - 1) / coverageTreeFanout { if !take(nodes, 40) { return false diff --git a/seaweed-block/core/storage/chunk_coverage_test.go b/seaweed-block/core/storage/chunk_coverage_test.go index e1e934b7d5..44b8091aa8 100644 --- a/seaweed-block/core/storage/chunk_coverage_test.go +++ b/seaweed-block/core/storage/chunk_coverage_test.go @@ -321,7 +321,7 @@ func TestChunkCoverageBaselineCannotOutliveItsRetainedWAL(t *testing.T) { if _, err := s.v3.pub.publishMapV3(&s.mu, nil, prepared, nil); err != nil { t.Fatal(err) } - if s.v3.pub.current().W != 0 || s.v3.pub.mapCurrent.entries[0].LSN != 1 { + if s.v3.pub.current().W != 0 || s.v3.pub.mapCurrent.entries.at(int(0)).LSN != 1 { t.Fatal("control did not persist a bitmap ahead of W") } if damaged { @@ -611,7 +611,7 @@ func TestChunkCoverageHalfFenceReusedMapOpensTheIntactLegacySlot(t *testing.T) { if err != nil { t.Fatal(err) } - entries := append([]chunkLSNEntryV3(nil), prior.entries...) + entries := prior.entries.all() entries[0].Unknown = !entries[0].Unknown _, err = oldMap.Prepare(prior.generation+1, prior.covered, entries, prior) if err != nil { diff --git a/seaweed-block/core/storage/chunk_layer_map_v3.go b/seaweed-block/core/storage/chunk_layer_map_v3.go index dc9f7e354b..17177c3724 100644 --- a/seaweed-block/core/storage/chunk_layer_map_v3.go +++ b/seaweed-block/core/storage/chunk_layer_map_v3.go @@ -44,7 +44,7 @@ func (l *chunkLayerImpl) persistCapturedMapV3(versions map[uint64]uint64) { cp := v.pub.cur.CheckpointLSN need := false for i, e := range v.mapEntries { - if e != v.pub.mapCurrent.entries[i] { + if e != v.pub.mapCurrent.entries.at(i) { need = true break } diff --git a/seaweed-block/core/storage/chunk_lsn_map_v3.go b/seaweed-block/core/storage/chunk_lsn_map_v3.go index 2c03561170..13b32cc9ca 100644 --- a/seaweed-block/core/storage/chunk_lsn_map_v3.go +++ b/seaweed-block/core/storage/chunk_lsn_map_v3.go @@ -19,6 +19,8 @@ import ( ) const chunkMapEntryMemoryBytes = uint64(unsafe.Sizeof(chunkLSNEntryV3{})) +const mapEntryNodeMemoryBytes = uint64(unsafe.Sizeof(mapEntryNode{})) +const mapEntryBranchMemoryBytes = uint64(unsafe.Sizeof([mapEntryFanout]*mapEntryNode{})) const mapPageDomainV3 = "swblock:chunk-map:page:v1\x00" const mapRootDomainV3 = "swblock:chunk-map:root:v1\x00" @@ -258,7 +260,7 @@ type chunkMapCandidateV3 struct { generation, covered, rootOffset uint64 hash [32]byte refs []mapPageRefV3 - entries []chunkLSNEntryV3 + entries mapEntrySnapshot format uint32 zeroPosition uint64 } @@ -346,7 +348,7 @@ func openChunkLSNMapV3Progress(path string, d descriptorV3, cb uint64, progress } m.syncFile = f.Sync m.syncDirectory = func() error { return syncDir(filepath.Dir(path)) } - c := &chunkMapCandidateV3{owner: m, generation: d.MapGeneration, covered: d.MapCoveredCheckpoint, rootOffset: d.MapRootLocator, hash: d.MapRootHash, format: d.MapFormatID, zeroPosition: metadata.zeroPosition, refs: refs, entries: es} + c := &chunkMapCandidateV3{owner: m, generation: d.MapGeneration, covered: d.MapCoveredCheckpoint, rootOffset: d.MapRootLocator, hash: d.MapRootHash, format: d.MapFormatID, zeroPosition: metadata.zeroPosition, refs: refs, entries: snapshotMapEntries(es, mapEntrySnapshot{})} return m, c, nil } @@ -489,10 +491,10 @@ func (m *chunkLSNMapV3) prepareLockedV3(g, C uint64, entries []chunkLSNEntryV3, refs := make([]mapPageRefV3, s.pages) for j := uint64(0); j < s.pages; j++ { start, end := mapPageEntrySpanV3(identity, m.chunkBlocks, j, s.chunks) - same := prior != nil && prior.formatID() == identity.MapFormatID && len(prior.entries) == len(entries) + same := prior != nil && prior.formatID() == identity.MapFormatID && prior.entries.len() == len(entries) if same { for i := start; i < end; i++ { - if prior.entries[i] != entries[i] { + if prior.entries.at(int(i)) != entries[i] { same = false break } @@ -548,7 +550,11 @@ func (m *chunkLSNMapV3) prepareLockedV3(g, C uint64, entries []chunkLSNEntryV3, return nil, e } m.lastGeneration = g - out = &chunkMapCandidateV3{owner: m, generation: g, covered: C, rootOffset: at, hash: mapDigestV3(mapRootDomainV3, root[:s.raw-32]), format: identity.MapFormatID, zeroPosition: zeroPosition, refs: refs, entries: append([]chunkLSNEntryV3(nil), entries...)} + previous := mapEntrySnapshot{} + if prior != nil { + previous = prior.entries + } + out = &chunkMapCandidateV3{owner: m, generation: g, covered: C, rootOffset: at, hash: mapDigestV3(mapRootDomainV3, root[:s.raw-32]), format: identity.MapFormatID, zeroPosition: zeroPosition, refs: refs, entries: snapshotMapEntries(entries, previous)} // The candidate is live (in flight) until a publication names it or // supersedes it. x := rootExtentsV3(at, s.aligned, refs) diff --git a/seaweed-block/core/storage/chunk_lsn_map_v3_test.go b/seaweed-block/core/storage/chunk_lsn_map_v3_test.go index 37d3e62d63..a350e591ab 100644 --- a/seaweed-block/core/storage/chunk_lsn_map_v3_test.go +++ b/seaweed-block/core/storage/chunk_lsn_map_v3_test.go @@ -233,7 +233,7 @@ func TestChunkLSNMapV3_OrderOwnedOpenAndClippedPage(t *testing.T) { t.Fatal(e) } defer reopened.Close() - if len(loaded.entries) != 251 || !loaded.entries[250].Unknown { + if loaded.entries.len() != 251 || !loaded.entries.at(int(250)).Unknown { t.Fatal("clipped final chunk lost") } // The last page has precisely one entry even though the last chunk is clipped. 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 bc2ad22a53..1267a93c31 100644 --- a/seaweed-block/core/storage/chunk_map_extents_v3_test.go +++ b/seaweed-block/core/storage/chunk_map_extents_v3_test.go @@ -122,8 +122,8 @@ func TestChunkMapExtentsV3CrashBeforePublicationOfAReusedExtent(t *testing.T) { t.Fatalf("reopen of the selected slot after the crash: %v", err) } defer r.Close() - if rc.entries[0].LSN != 100+cur.generation { - t.Fatalf("selected slot reads LSN %d", rc.entries[0].LSN) + if rc.entries.at(int(0)).LSN != 100+cur.generation { + t.Fatalf("selected slot reads LSN %d", rc.entries.at(int(0)).LSN) } if _, _, err := readChunkLSNMapV3(r.fd, prev.descriptor(d), 1); err != nil { t.Fatalf("fallback after the crash: %v", err) @@ -335,8 +335,8 @@ func TestChunkMapExtentsV3TruncationFaultKeepsTheSlotRoots(t *testing.T) { t.Fatalf("reopen after the truncation fault: %v", err) } defer r.Close() - if rc.entries[0].LSN != 100+next.generation { - t.Fatalf("selected root reads LSN %d", rc.entries[0].LSN) + if rc.entries.at(int(0)).LSN != 100+next.generation { + t.Fatalf("selected root reads LSN %d", rc.entries.at(int(0)).LSN) } if _, _, err := readChunkLSNMapV3(r.fd, low.descriptor(d), 1); err != nil { t.Fatalf("fallback after the truncation fault: %v", err) diff --git a/seaweed-block/core/storage/coverage_wal_before_page_test.go b/seaweed-block/core/storage/coverage_wal_before_page_test.go index e5ff77c89f..c72acbe61c 100644 --- a/seaweed-block/core/storage/coverage_wal_before_page_test.go +++ b/seaweed-block/core/storage/coverage_wal_before_page_test.go @@ -57,7 +57,7 @@ func TestCoveragePublicationSyncsItsSpeculativeWALBeforeMap(t *testing.T) { if err != nil { t.Fatal(err) } - if !walSynced || prepared.candidate.entries[0].LSN != maxLSN { + if !walSynced || prepared.candidate.entries.at(int(0)).LSN != maxLSN { t.Fatal("snapshot did not preserve and sync its speculative maximum") } if _, err := s.v3.pub.publishMapV3(&s.mu, nil, prepared, nil); err != nil { diff --git a/seaweed-block/core/storage/format_v3_test_support.go b/seaweed-block/core/storage/format_v3_test_support.go index ad932eae5c..4f58d0c885 100644 --- a/seaweed-block/core/storage/format_v3_test_support.go +++ b/seaweed-block/core/storage/format_v3_test_support.go @@ -77,7 +77,8 @@ func MapCoverageV3Test(s *WALStore) (covered uint64, allUnknown bool) { s.v3.pub.descMu.Lock() defer s.v3.pub.descMu.Unlock() allUnknown = true - for _, e := range s.v3.pub.mapCurrent.entries { + for i := 0; i < s.v3.pub.mapCurrent.entries.len(); i++ { + e := s.v3.pub.mapCurrent.entries.at(i) if !e.Unknown { allUnknown = false } diff --git a/seaweed-block/core/storage/map_checkpoint_changed_pages_test.go b/seaweed-block/core/storage/map_checkpoint_changed_pages_test.go index 29bd9b3c0e..9203d0df46 100644 --- a/seaweed-block/core/storage/map_checkpoint_changed_pages_test.go +++ b/seaweed-block/core/storage/map_checkpoint_changed_pages_test.go @@ -46,11 +46,65 @@ func TestChunkMapCheckpointOneChangedPageDoesNotCopyTheWholeMap(t *testing.T) { if err != nil { t.Fatal(err) } - if prior.entries[0].LSN != 0 || next.entries[0].LSN != 1 { + if prior.entries.at(int(0)).LSN != 0 || next.entries.at(int(0)).LSN != 1 { t.Fatal("preparation changed the prior snapshot or lost the changed page") } const allocationBound = 2 << 20 + t.Logf("one changed page allocated %d bytes", after.TotalAlloc-before.TotalAlloc) if allocated := after.TotalAlloc - before.TotalAlloc; allocated > allocationBound { t.Fatalf("one changed page allocated %d bytes at 1 TiB; bound %d excludes a whole-map copy", allocated, allocationBound) } } + +func TestChunkMapEntrySnapshotsKeepOldValuesAcrossPageBoundaries(t *testing.T) { + d := mapDescriptorV3(t) + d.VolumeSize = 513 * 4 * 4096 + d.MapFormatID = mapFormatIDCoverage + d.W = 3 + m, err := createChunkLSNMapV3(filepath.Join(t.TempDir(), "map"), d, 4) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + if err := m.Close(); err != nil { + t.Error(err) + } + }) + entries := make([]chunkLSNEntryV3, 513) + for i := range entries { + entries[i] = chunkLSNEntryV3{Unknown: true, Local: noChunkLocalBits} + } + prior, err := m.Prepare(1, 0, entries, nil) + if err != nil { + t.Fatal(err) + } + positions := map[int]uint64{127: 1, 128: 2, 512: 3} + for index, position := range positions { + entries[index] = chunkLSNEntryV3{LSN: position, Unknown: true, Local: noChunkLocalBits.mark(0, 4)} + } + next, err := m.Prepare(2, 3, entries, prior) + if err != nil { + t.Fatal(err) + } + // A caller retaining the mutable source must not be able to change any + // prepared snapshot, including an unchanged shared entry page. + for i := range entries { + entries[i] = chunkLSNEntryV3{LSN: 99} + } + loaded, _, err := readChunkLSNMapV3(m.fd, next.descriptor(d), 4) + if err != nil { + t.Fatal(err) + } + for i := 0; i < 513; i++ { + want := positions[i] + if e := prior.entries.at(i); e.LSN != 0 || !e.Unknown || len(e.Local.words) != 0 { + t.Fatalf("prior entry %d changed: %+v", i, e) + } + if e := next.entries.at(i); e.LSN != want || !e.Unknown || loaded[i].LSN != want || !loaded[i].Unknown { + t.Fatalf("snapshot/disk entry %d lost value %d: %+v/%+v", i, want, e, loaded[i]) + } + if next.entries.at(i).Local.has(0) != (want != 0) || loaded[i].Local.has(0) != (want != 0) { + t.Fatalf("snapshot/disk entry %d lost local bit", i) + } + } +} diff --git a/seaweed-block/core/storage/map_entry_snapshot.go b/seaweed-block/core/storage/map_entry_snapshot.go new file mode 100644 index 0000000000..cf00834dc8 --- /dev/null +++ b/seaweed-block/core/storage/map_entry_snapshot.go @@ -0,0 +1,96 @@ +package storage + +// Map candidates own immutable entry pages. Reusing a page shares its values, +// never the caller's mutable entry slice. Updating a page copies only the path +// to that leaf; there is no full pointer-vector copy at the root either. +const mapEntryLeafSize = 128 +const mapEntryBranchBits = 6 +const mapEntryFanout = 1 << mapEntryBranchBits + +type mapEntryNode struct { + children *[mapEntryFanout]*mapEntryNode + values []chunkLSNEntryV3 +} + +type mapEntrySnapshot struct { + root *mapEntryNode + count int + depth uint +} + +func (s mapEntrySnapshot) len() int { return s.count } + +func (s mapEntrySnapshot) leaf(page int) *mapEntryNode { + n := s.root + for depth := s.depth; depth > 0 && n != nil; depth-- { + n = n.children[(uint(page)>>((depth-1)*mapEntryBranchBits))&(mapEntryFanout-1)] + } + return n +} + +func (s mapEntrySnapshot) at(index int) chunkLSNEntryV3 { + if index < 0 || index >= s.count { + panic("map entry index out of range") + } + return s.leaf(index / mapEntryLeafSize).values[index%mapEntryLeafSize] +} + +func replaceMapEntryLeaf(n *mapEntryNode, depth uint, page int, values []chunkLSNEntryV3) *mapEntryNode { + if depth == 0 { + return &mapEntryNode{values: values} + } + children := new([mapEntryFanout]*mapEntryNode) + if n != nil { + *children = *n.children + } + index := (uint(page) >> ((depth - 1) * mapEntryBranchBits)) & (mapEntryFanout - 1) + children[index] = replaceMapEntryLeaf(children[index], depth-1, page, values) + return &mapEntryNode{children: children} +} + +func snapshotMapEntries(entries []chunkLSNEntryV3, prior mapEntrySnapshot) mapEntrySnapshot { + s := prior + if prior.count != len(entries) { + s = mapEntrySnapshot{count: len(entries)} + pages := (len(entries) + mapEntryLeafSize - 1) / mapEntryLeafSize + for span := mapEntryFanout; pages > 1; pages = (pages + span - 1) / span { + s.depth++ + } + } + for start := 0; start < len(entries); start += mapEntryLeafSize { + end := min(start+mapEntryLeafSize, len(entries)) + page := start / mapEntryLeafSize + old := s.leaf(page) + same := old != nil && len(old.values) == end-start + if same { + for i, value := range old.values { + if value != entries[start+i] { + same = false + break + } + } + } + if same { + continue + } + values := append([]chunkLSNEntryV3(nil), entries[start:end]...) + s.root = replaceMapEntryLeaf(s.root, s.depth, page, values) + } + return s +} + +func (s mapEntrySnapshot) copyInto(dst []chunkLSNEntryV3, start int) { + for len(dst) > 0 { + page := s.leaf(start / mapEntryLeafSize).values + n := copy(dst, page[start%mapEntryLeafSize:]) + dst, start = dst[n:], start+n + } +} + +// Whole-map materialization belongs only to callers that inherently rebuild +// every entry, such as recovery and the legacy decoder controls. +func (s mapEntrySnapshot) all() []chunkLSNEntryV3 { + out := make([]chunkLSNEntryV3, s.count) + s.copyInto(out, 0) + return out +} diff --git a/seaweed-block/core/storage/recovery_progress.go b/seaweed-block/core/storage/recovery_progress.go index 5dd5cbc9b0..d7c7189fc5 100644 --- a/seaweed-block/core/storage/recovery_progress.go +++ b/seaweed-block/core/storage/recovery_progress.go @@ -108,16 +108,24 @@ func copyRecoveryMap(ctx context.Context, dst, src []chunkLSNEntryV3) ([]chunkLS } func copyRecoveryMapPhase(ctx context.Context, phase RecoveryPhase, dst, src []chunkLSNEntryV3) ([]chunkLSNEntryV3, error) { + return copyRecoveryEntrySource(ctx, phase, dst, len(src), func(out []chunkLSNEntryV3, start int) { copy(out, src[start:]) }) +} + +func copyRecoverySnapshotPhase(ctx context.Context, phase RecoveryPhase, dst []chunkLSNEntryV3, src mapEntrySnapshot) ([]chunkLSNEntryV3, error) { + return copyRecoveryEntrySource(ctx, phase, dst, src.len(), src.copyInto) +} + +func copyRecoveryEntrySource(ctx context.Context, phase RecoveryPhase, dst []chunkLSNEntryV3, count int, copyInto func([]chunkLSNEntryV3, int)) ([]chunkLSNEntryV3, error) { // A canceled copy must not change the live map, including its backing // array. Publish this private candidate only after every entry was copied. - candidate := make([]chunkLSNEntryV3, 0, len(src)) + candidate := make([]chunkLSNEntryV3, count) progress := newRecoveryReporter(ctx, phase) - for start := 0; start < len(src); start += 256 { + for start := 0; start < count; start += 256 { if err := ctx.Err(); err != nil { return dst, err } - end := min(start+256, len(src)) - candidate = append(candidate, src[start:end]...) + end := min(start+256, count) + copyInto(candidate[start:end], start) progress.advance(uint64(end - start)) } progress.finish() diff --git a/seaweed-block/core/storage/walstore_map_checkpoint_v3.go b/seaweed-block/core/storage/walstore_map_checkpoint_v3.go index 726e12e012..c76a9f69e3 100644 --- a/seaweed-block/core/storage/walstore_map_checkpoint_v3.go +++ b/seaweed-block/core/storage/walstore_map_checkpoint_v3.go @@ -39,7 +39,7 @@ func (s *WALStore) recordMapReplayV3(lba, lsn uint64) { } index := lba / v.ownedMap.chunkBlocks prior := v.mapEntries[index].Local - baseline := v.pub.mapCurrent.entries[index].LSN + baseline := v.pub.mapCurrent.entries.at(int(index)).LSN s.recordMapWriteV3(lba, lsn) if lsn <= baseline { v.mapEntries[index].Local = prior diff --git a/seaweed-block/core/storage/walstore_v3.go b/seaweed-block/core/storage/walstore_v3.go index 3b63043589..1753436199 100644 --- a/seaweed-block/core/storage/walstore_v3.go +++ b/seaweed-block/core/storage/walstore_v3.go @@ -485,7 +485,7 @@ func openWALStoreV3File(path string, f *os.File, requireMap bool, opts WALStoreO } openProgress.finish() v.ownedMap = ownedMap - v.mapEntries, err = copyRecoveryMapPhase(progressCtx, RecoveryOpenCopy, nil, mapCurrent.entries) + v.mapEntries, err = copyRecoverySnapshotPhase(progressCtx, RecoveryOpenCopy, nil, mapCurrent.entries) if err != nil { return err } @@ -650,7 +650,8 @@ func (s *WALStore) recoverV3(ctx context.Context) (uint64, error) { if v.ownedMap != nil { v.pub.descMu.Lock() if v.pub.mapCurrent.formatID() == mapFormatIDCoverage { - for index, entry := range v.pub.mapCurrent.entries { + for index := 0; index < v.pub.mapCurrent.entries.len(); index++ { + entry := v.pub.mapCurrent.entries.at(index) if index%256 == 0 { if err := ctx.Err(); err != nil { v.pub.descMu.Unlock() @@ -664,7 +665,7 @@ func (s *WALStore) recoverV3(ctx context.Context) (uint64, error) { } } var err error - v.mapEntries, err = copyRecoveryMap(ctx, v.mapEntries, v.pub.mapCurrent.entries) + v.mapEntries, err = copyRecoverySnapshotPhase(ctx, RecoveryMap, v.mapEntries, v.pub.mapCurrent.entries) v.pub.descMu.Unlock() if err != nil { return 0, err diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index 3438a69c8c..b084d56d0a 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -666,6 +666,7 @@ The "Machine / transition" column refers to Part A. Notes under a table record w | `INV-WAL-V3-MAP-PUBLISH-ROLLOVER-001` | A v3 publication without a prepared map candidate (a W-only publication or a rollover) carries the bound map root unchanged. | M1 Serving (v3 map publication) | `core/storage/chunk_map_publication_v3.go` `validateMapPublicationV3` | `TestMapPublicationV3_CoverageWAndNoUnpreparedRoot`, `TestCheckpointV3_AppendRolloverAfterPrepareKeepsRetainedSuffix` | — | STUB | | `INV-WAL-V3-FLUSH-LIFETIME-001` | A v3 flusher binds its dirty snapshot and extent materialization to the original even Recover generation. A cycle spanning Recover/reset publishes no checkpoint/map and retires no reused dirty entry. | M1 Recovering / checkpoint publication | `core/storage/flusher.go`; `walstore_map_checkpoint_v3.go`; `walstore_v3.go` `flushLifetimeValidV3` | `TestWALStoreV3_FlushMaterializationBoundToRecoveryLifetime` | Existing recovery/map proof lifetime; implementation correspondence | STUB | | `INV-WAL-V3-MAP-BYTES-001` | A v3 map candidate names immutable pages and root in the store's owned sidecar, checked against the descriptor's identity and geometry. | M1 (map preparation) | `core/storage/chunk_lsn_map_v3.go` `prepareLockedV3`, `encodeMapPageV3`, `encodeMapRootV3` | `TestChunkLSNMapV3_PersistReloadAndReuse`, `TestChunkLSNMapV3_OrderOwnedOpenAndClippedPage`, `TestChunkLSNMapV3_CorruptionAndBounds` | WALFormatR5 abstract map dependency; physical encoding/digest/order are code obligations | ACTIVE | +| `INV-WAL-V3-MAP-CANDIDATE-IMMUTABLE-001` | A prepared map candidate retains immutable entry values; later changes to the caller's mutable entry slice cannot change an earlier candidate. | M1 map preparation | `core/storage/map_entry_snapshot.go` `snapshotMapEntries`; `core/storage/chunk_lsn_map_v3.go` `prepareLockedV3` | `TestChunkMapEntrySnapshotsKeepOldValuesAcrossPageBoundaries`, `TestChunkMapCheckpointOneChangedPageDoesNotCopyTheWholeMap` | PageReuse.Prepare; entry-value representation only, dirty-version retirement pending | STUB | | `INV-WAL-V3-MAP-VALIDATE-001` | A v3 map read returns no entry before the root and every page pass their domain-separated body digests, zero reserved and unused bytes, exact page counts, file bounds and non-aliasing page intervals. | M1 (map preparation) | `core/storage/chunk_lsn_map_v3.go` `readChunkLSNMapV3`, `mapReadAtV3` | `TestChunkLSNMapV3_CanonicalVectors`, `TestChunkLSNMapV3_CorruptionAndBounds`, `TestChunkLSNMapV3_RejectsRehashedMalformedRecords`, `TestChunkLSNMapV3_EntryBoundBeforeRootIO`, `TestChunkLSNMapV3_WholePageHashIsNotBodyDigest` | — | STUB | | `INV-WAL-V3-MAP-PERSIST-ORDER-001` | A v3 map candidate exists only after its new pages are written and fsynced, then its root is written and fsynced, then the sidecar's directory is fsynced. | M1 (map preparation) | `core/storage/chunk_lsn_map_v3.go` `prepareLockedV3` | `TestChunkLSNMapV3_OrderOwnedOpenAndClippedPage` | — | ACTIVE | | `INV-WAL-V3-MAP-PERSIST-FAIL-001` | An error inside a v3 map persistence transaction yields no candidate and latches the store against later I/O; a preflight or Stat error latches nothing. | M1 (map preparation) | `core/storage/chunk_lsn_map_v3.go` `prepareLockedV3` | `TestChunkLSNMapV3_FailureDoesNotPublishOrReuse`, `TestChunkLSNMapV3_PreflightErrorDoesNotLatch`, `TestWALStoreV3_AutomaticMapPersistenceFailureLatchesStore` | — | STUB | diff --git a/seaweed-block/docs/design/map-checkpoint-cost.md b/seaweed-block/docs/design/map-checkpoint-cost.md index a0151d2feb..e6a9bab6a4 100644 --- a/seaweed-block/docs/design/map-checkpoint-cost.md +++ b/seaweed-block/docs/design/map-checkpoint-cost.md @@ -14,3 +14,5 @@ The format-1 checkpoint bound excludes the initial creation publication. Creatio Until the coverage format is declared, release user-facing creates above 256 GiB remain refused; the lab build retains its existing size override. This is not an additional v1.1 user capacity limit. The draft-format activation gate remains independent of checkpoint optimization. Production open continues to require the valid immutable root created above. Missing or corrupt map metadata must keep refusing; it cannot stand for a new empty store. No encoding prerequisite remains. The required creation/first-open regression is that a store above 256 GiB never publishes a format-1 map checkpoint after create; that control and its native qualification remain pending in this draft. The obsolete test requiring every direct store create above 256 GiB to refuse has been removed: it did not express this boundary. + +Candidate entries now use immutable leaf pages with shared bounded-fanout paths. A one-page change in the map preparer allocates 877,480 bytes at 1 TiB in the Windows Go1.25.6 control (previously 7,164,632). This is a candidate-copy result, not end-to-end checkpoint complexity: live entry capture, full input validation/comparison, reference-tree scans and extent live-set rebuilds remain O(S). Dirty-version retirement, incremental pins, typed phases, resource-grid cells and native stop qualification are still pending. No V10 completion claim. diff --git a/seaweed-block/docs/design/model-code-trace-coverage.tsv b/seaweed-block/docs/design/model-code-trace-coverage.tsv index 19745df806..afd868143c 100644 --- a/seaweed-block/docs/design/model-code-trace-coverage.tsv +++ b/seaweed-block/docs/design/model-code-trace-coverage.tsv @@ -12,3 +12,4 @@ SlotFence2 intact unmarked half-fence fallback only, typed event and process cou MoveWide2 Fill: durable bytes before local bitmap core/storage/chunk_local_mutation.go:WALStore.MarkChunksLocal core/storage/chunk_local_mutation_test.go:TestMarkChunksLocalPublishesOncePerRestoreBatch; core/storage/chunk_local_mutation_test.go:TestMarkChunkLocalSyncsBytesBeforePublishingCoverage; core/storage/chunk_local_mutation_test.go:TestMarkChunkLocalRefusesFailedExtentSyncBeforeMapIO STUB MoveWide2 Evict/Drop: durable clear before caller drops bytes core/storage/chunk_local_mutation.go:WALStore.ClearChunkLocal core/storage/chunk_local_mutation_test.go:TestCrashAfterAbsentPageBeforeClearKeepsBytesAndLaterWrite; core/storage/chunk_local_mutation_test.go:TestClearChunkLocalIsDurableBeforeReturn; core/storage/chunk_local_mutation_test.go:TestChunkLocalMutationRefusesUnavailableBoundsAndMapFailure STUB SlotFence2 WAL prefix durable before bitmap pages name speculative positions core/storage/walstore_map_checkpoint_v3.go:prepareOwnedMapCheckpointV3 core/storage/coverage_wal_before_page_test.go:TestCoveragePublicationSyncsItsSpeculativeWALBeforeMap; core/storage/coverage_wal_before_page_test.go:TestCoveragePublicationRefusesWALSyncFailureBeforeMap STUB +PageReuse immutable prepared entry values (partial V10, no dirty-version retirement yet) core/storage/map_entry_snapshot.go:snapshotMapEntries; core/storage/chunk_lsn_map_v3.go:prepareLockedV3 core/storage/map_checkpoint_changed_pages_test.go:TestChunkMapEntrySnapshotsKeepOldValuesAcrossPageBoundaries; core/storage/map_checkpoint_changed_pages_test.go:TestChunkMapCheckpointOneChangedPageDoesNotCopyTheWholeMap STUB