From fc222d2386216fded7696f46ae83bf38f64a7950 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Mon, 30 Nov 2026 14:36:31 -0800 Subject: [PATCH] storage: v1.1 format build guard; the mutation hook carries the write's LSN The draft v1.1 store format runs only in lab builds and in this package's tests (chunkV11Format). The mutation hook passes the write's store position, so the chunk record knows the position through which it has applied every write. Co-Authored-By: Claude Opus 5.5 --- seaweed-block/core/storage/base_install_v3.go | 2 +- seaweed-block/core/storage/chunk_layer.go | 8 +++---- .../storage/chunk_mutation_note_guard_test.go | 20 ++++++++-------- .../core/storage/chunk_record_shadow.go | 23 ++++++++++++++++++- .../core/storage/chunk_record_shadow_test.go | 2 +- .../core/storage/chunk_v11_format.go | 7 ++++++ .../core/storage/chunk_v11_format_lab.go | 8 +++++++ .../core/storage/chunk_v11_format_release.go | 10 ++++++++ seaweed-block/core/storage/walstore.go | 6 ++--- seaweed-block/core/storage/walstore_v3.go | 4 ++-- 10 files changed, 68 insertions(+), 22 deletions(-) create mode 100644 seaweed-block/core/storage/chunk_v11_format.go create mode 100644 seaweed-block/core/storage/chunk_v11_format_lab.go create mode 100644 seaweed-block/core/storage/chunk_v11_format_release.go diff --git a/seaweed-block/core/storage/base_install_v3.go b/seaweed-block/core/storage/base_install_v3.go index a067d570a7..5f9d8d2188 100644 --- a/seaweed-block/core/storage/base_install_v3.go +++ b/seaweed-block/core/storage/base_install_v3.go @@ -443,7 +443,7 @@ func (s *WALStore) applyEntryV3(lba uint32, data []byte, lsn uint64, installLane return err } s.dm.put(uint64(lba), phys, lsn, uint32(len(dataCopy))) - s.noteChunkMutation(uint64(lba)) + s.noteChunkMutation(uint64(lba), lsn) if lsn >= s.nextLSN { s.nextLSN = lsn + 1 } diff --git a/seaweed-block/core/storage/chunk_layer.go b/seaweed-block/core/storage/chunk_layer.go index c06713cc56..4bd1e554d8 100644 --- a/seaweed-block/core/storage/chunk_layer.go +++ b/seaweed-block/core/storage/chunk_layer.go @@ -44,7 +44,7 @@ type chunkHooks struct { closer func() error // mutated runs after every dirty-map insert (a write, a batch write, // a replica apply) with the LBA: O(1), lock-free, on the write path. - mutated func(lba uint64) + mutated func(lba, lsn uint64) // recovered runs when Recover republished the dirty map (any chunk // may have changed), before reads resume. recovered func() @@ -453,7 +453,7 @@ func (l *chunkLayerImpl) OnExtentWrite(lba uint64, lsn uint64) { } } if lsn == 0 { // a BASE block lands in the extent directly - l.noteMutation(lba) + l.noteMutation(lba, 0) } l.dirtyVer++ } @@ -461,10 +461,10 @@ func (l *chunkLayerImpl) OnExtentWrite(lba uint64, lsn uint64) { // noteMutation counts a mutation of lba's chunk. It runs after the // mutation is visible to reads, so a staging read that sampled the count // first either sees the new bytes or a stale count (never reused). -func (l *chunkLayerImpl) noteMutation(lba uint64) { +func (l *chunkLayerImpl) noteMutation(lba, lsn uint64) { if idx := l.chunkOf(lba); idx < uint64(len(l.chunkMut)) { if sh := l.shadow; sh != nil { - sh.write(idx, func() { l.chunkMut[idx].Add(1) }) + sh.write(idx, lsn, func() { l.chunkMut[idx].Add(1) }) return } l.chunkMut[idx].Add(1) diff --git a/seaweed-block/core/storage/chunk_mutation_note_guard_test.go b/seaweed-block/core/storage/chunk_mutation_note_guard_test.go index 34d1fa59aa..c6a5503555 100644 --- a/seaweed-block/core/storage/chunk_mutation_note_guard_test.go +++ b/seaweed-block/core/storage/chunk_mutation_note_guard_test.go @@ -16,7 +16,7 @@ import ( // dirtyMapInsertSites parses src and returns, for every call of // .dm.put / putAt / putAtIfNewer, "file func method noted|unnoted". A put // or putAt is noted when one of the next two statements of the same block -// is the call .noteChunkMutation() itself, on the insert's receiver +// is the call .noteChunkMutation(, ) itself, on the insert's receiver // and with the insert's first argument : not a method value, not a // call inside a condition or a defer, not a note of another receiver or LBA. func dirtyMapInsertSites(t *testing.T, file, src string) []string { @@ -53,14 +53,14 @@ func dirtyMapInsertSites(t *testing.T, file, src string) []string { } return "", "", "" } - // notes reports whether s is the statement owner.noteChunkMutation(lba). + // notes reports whether s is the statement owner.noteChunkMutation(lba, lsn). notes := func(s ast.Stmt, owner, lba string) bool { es, ok := s.(*ast.ExprStmt) if !ok { return false } call, ok := es.X.(*ast.CallExpr) - if !ok || len(call.Args) != 1 { + if !ok || len(call.Args) != 2 { // (lba, lsn) return false } sel, ok := call.Fun.(*ast.SelectorExpr) @@ -229,11 +229,11 @@ func TestEveryDirtyMapInsertNotesTheChunkMutation(t *testing.T) { "x.go w put unnoted", }, "a note three statements later": { - "package storage\nfunc w(s *WALStore) { s.dm.putAt(1, 2, 0, 3, 4); a(); b(); s.noteChunkMutation(1) }\n", + "package storage\nfunc w(s *WALStore) { s.dm.putAt(1, 2, 0, 3, 4); a(); b(); s.noteChunkMutation(1, 3) }\n", "x.go w putAt unnoted", }, "an insert in a defer": { - "package storage\nfunc w(s *WALStore) { defer s.dm.put(1, 2, 3, 4); s.noteChunkMutation(1) }\n", + "package storage\nfunc w(s *WALStore) { defer s.dm.put(1, 2, 3, 4); s.noteChunkMutation(1, 3) }\n", "x.go w put unnoted", }, "a method value, not a call": { @@ -241,23 +241,23 @@ func TestEveryDirtyMapInsertNotesTheChunkMutation(t *testing.T) { "x.go w put unnoted", }, "a note inside a condition": { - "package storage\nfunc w(s *WALStore) { s.dm.put(1, 2, 3, 4); if false { s.noteChunkMutation(1) } }\n", + "package storage\nfunc w(s *WALStore) { s.dm.put(1, 2, 3, 4); if false { s.noteChunkMutation(1, 3) } }\n", "x.go w put unnoted", }, "a deferred note": { - "package storage\nfunc w(s *WALStore) { s.dm.put(1, 2, 3, 4); defer s.noteChunkMutation(1) }\n", + "package storage\nfunc w(s *WALStore) { s.dm.put(1, 2, 3, 4); defer s.noteChunkMutation(1, 3) }\n", "x.go w put unnoted", }, "a note on another store": { - "package storage\nfunc w(s, o *WALStore) { s.dm.put(1, 2, 3, 4); o.noteChunkMutation(1) }\n", + "package storage\nfunc w(s, o *WALStore) { s.dm.put(1, 2, 3, 4); o.noteChunkMutation(1, 3) }\n", "x.go w put unnoted", }, "a note of another LBA": { - "package storage\nfunc w(s *WALStore, a, b uint64) { s.dm.put(a, 2, 3, 4); s.noteChunkMutation(b) }\n", + "package storage\nfunc w(s *WALStore, a, b uint64) { s.dm.put(a, 2, 3, 4); s.noteChunkMutation(b, 3) }\n", "x.go w put unnoted", }, "the matching note after one other statement": { - "package storage\nfunc w(s *WALStore, a uint64) { s.dm.put(uint64(a), 2, 3, 4); x(); s.noteChunkMutation(uint64(a)) }\n", + "package storage\nfunc w(s *WALStore, a uint64) { s.dm.put(uint64(a), 2, 3, 4); x(); s.noteChunkMutation(uint64(a), 3) }\n", "x.go w put noted", }, } { diff --git a/seaweed-block/core/storage/chunk_record_shadow.go b/seaweed-block/core/storage/chunk_record_shadow.go index 6da4bd59d1..66545fc830 100644 --- a/seaweed-block/core/storage/chunk_record_shadow.go +++ b/seaweed-block/core/storage/chunk_record_shadow.go @@ -6,6 +6,7 @@ import ( "fmt" "log" "sync" + "sync/atomic" "github.com/seaweedfs/seaweed-block/core/chunkkernel" ) @@ -48,6 +49,9 @@ type chunkRecordShadow struct { settled map[uint64]uint64 // fullScans counts the test-only scans of the whole record. fullScans int + // applied is the store position through which every write has reached + // the record (the runtime's applied position). + applied atomic.Uint64 } // settled: chunks a delayed settlement committed, with their captured @@ -149,7 +153,14 @@ func (s *chunkRecordShadow) defect(chunk uint64, ev chunkkernel.Event, err error // write is E1: a write's version bump and its Write, in one step. The // argument is the resulting chunk's coverage: some while its absence stands // (A, P), all once its bytes are local. -func (s *chunkRecordShadow) write(chunk uint64, bump func()) { +// +// lsn is the write's store position. Once its Write is applied the shell's +// applied position rises to it (THE APPLIED POSITION of the runtime): writes +// reach the hook serially under the store's lock, so every write at or below +// it has been applied. A write with no position (0, a BASE block landing +// directly) leaves it. +func (s *chunkRecordShadow) write(chunk, lsn uint64, bump func()) { + defer s.raiseApplied(lsn) _, _, err := s.rec.ApplyIf(chunk, chunkkernel.Write, func(k chunkkernel.Kind) chunkkernel.Arg { if k == chunkkernel.Absent || k == chunkkernel.Partial { return chunkkernel.Arg{Value: "some"} @@ -161,6 +172,16 @@ func (s *chunkRecordShadow) write(chunk uint64, bump func()) { } } +// raiseApplied raises the applied position to lsn, never lowering it. +func (s *chunkRecordShadow) raiseApplied(lsn uint64) { + for { + cur := s.applied.Load() + if lsn <= cur || s.applied.CompareAndSwap(cur, lsn) { + return + } + } +} + // landed is E1b: the engine's per-block bitmap of an absent chunk now // covers every block, so the chunk's bytes are all local. Caller holds stMu // (the bitmap's lock). diff --git a/seaweed-block/core/storage/chunk_record_shadow_test.go b/seaweed-block/core/storage/chunk_record_shadow_test.go index f3a81ce5ae..29fe172c32 100644 --- a/seaweed-block/core/storage/chunk_record_shadow_test.go +++ b/seaweed-block/core/storage/chunk_record_shadow_test.go @@ -293,7 +293,7 @@ func TestShadowEvictEdges(t *testing.T) { _ = f // The record meets an evict after a write it saw and the engine's clean // check did not: skipped and counted. - l.shadow.write(5, func() {}) + l.shadow.write(5, 0, func() {}) l.shadow.evict(5) l.shadow.mu.Lock() skipped := l.shadow.skippedEvicts diff --git a/seaweed-block/core/storage/chunk_v11_format.go b/seaweed-block/core/storage/chunk_v11_format.go new file mode 100644 index 0000000000..161b8ffd89 --- /dev/null +++ b/seaweed-block/core/storage/chunk_v11_format.go @@ -0,0 +1,7 @@ +package storage + +// chunkV11Format says whether a chunk layer upgrades its store to the draft +// v1.1 format at attach and opens it from the chunk record. It is the +// build's constant; this package's tests turn it on for the tests that +// exercise the format. +var chunkV11Format = chunkV11FormatBuild diff --git a/seaweed-block/core/storage/chunk_v11_format_lab.go b/seaweed-block/core/storage/chunk_v11_format_lab.go new file mode 100644 index 0000000000..16b2b66ffc --- /dev/null +++ b/seaweed-block/core/storage/chunk_v11_format_lab.go @@ -0,0 +1,8 @@ +//go:build swblock_lab_bigvol + +package storage + +// chunkV11FormatBuild is true in a lab build: the draft v1.1 store format is +// exercised on lab volumes. Never a release build (CI refuses the tag in +// every image and release build). +const chunkV11FormatBuild = true diff --git a/seaweed-block/core/storage/chunk_v11_format_release.go b/seaweed-block/core/storage/chunk_v11_format_release.go new file mode 100644 index 0000000000..558e51b4f2 --- /dev/null +++ b/seaweed-block/core/storage/chunk_v11_format_release.go @@ -0,0 +1,10 @@ +//go:build !swblock_lab_bigvol + +package storage + +// chunkV11FormatBuild is false in a release build: the v1.1 store format +// (chunk record pages, the coverage map and the chunk-state marker) is not +// final, so no release binary upgrades a user's store to it; the v1.0 path +// (.chunkpresence) stays. One later change declares the format, removes this +// guard and fixes the version. +const chunkV11FormatBuild = false diff --git a/seaweed-block/core/storage/walstore.go b/seaweed-block/core/storage/walstore.go index 9cb1b55a15..7ea138066f 100644 --- a/seaweed-block/core/storage/walstore.go +++ b/seaweed-block/core/storage/walstore.go @@ -571,7 +571,7 @@ func (s *WALStore) WriteContext(ctx context.Context, lba uint32, data []byte) (u } dirtyStart := time.Now() s.dm.put(uint64(lba), walRelOff, lsn, uint32(len(data))) - s.noteChunkMutation(uint64(lba)) + s.noteChunkMutation(uint64(lba), lsn) s.instr.recordDirtyMapUpdate(1, time.Since(dirtyStart)) s.nextLSN++ @@ -1015,9 +1015,9 @@ func (s *WALStore) noteChunkRecovery() { // noteChunkMutation tells an attached chunk layer that lba changed; it // runs after the dirty-map insert that made the change visible. -func (s *WALStore) noteChunkMutation(lba uint64) { +func (s *WALStore) noteChunkMutation(lba, lsn uint64) { if h := s.chunk.Load(); h != nil && h.mutated != nil { - h.mutated(lba) + h.mutated(lba, lsn) } } diff --git a/seaweed-block/core/storage/walstore_v3.go b/seaweed-block/core/storage/walstore_v3.go index 1af9b55246..3b63043589 100644 --- a/seaweed-block/core/storage/walstore_v3.go +++ b/seaweed-block/core/storage/walstore_v3.go @@ -828,7 +828,7 @@ func (s *WALStore) writeBatchV3Locked(startLBA uint32, blocks [][]byte, firstLSN dirtyStart := time.Now() s.dm.put(uint64(startLBA), phys, firstLSN, uint32(len(blocks[0]))) s.instr.recordDirtyMapUpdate(1, time.Since(dirtyStart)) - s.noteChunkMutation(uint64(startLBA)) + s.noteChunkMutation(uint64(startLBA), firstLSN) s.advanceAfterAppendV3(firstLSN, firstLSN) return []uint64{firstLSN}, nil } @@ -870,7 +870,7 @@ func (s *WALStore) writeBatchV3Locked(startLBA uint32, blocks [][]byte, firstLSN lsns[i] = firstLSN + i s.recordMapWriteV3(lba, lsns[i]) s.dm.putAt(lba, phys, uint32(i)*uint32(bs), lsns[i], uint32(bs)) - s.noteChunkMutation(lba) + s.noteChunkMutation(lba, lsns[i]) } s.instr.recordDirtyMapUpdate(len(blocks), time.Since(dirtyStart)) s.v3.appended.Store(lastLSN)