feat(storage): publish owned map proofs through real checkpoints

This commit is contained in:
pingqiu committed 2026-11-17 07:38:41 -08:00
1 parent 0193516bc0
commit 623ffedb69
4 files changed
+252 -13

No files matched your search

+27 -8
View File
@@ -233,14 +233,15 @@ func (c *chunkMapCandidateV3) descriptor(d descriptorV3) descriptorV3 {
}
type chunkLSNMapV3 struct {
mu sync.Mutex
fd *os.File
identity descriptorV3
chunkBlocks uint64
failed error
lastGeneration uint64
syncFile func() error
syncDirectory func() error
mu sync.Mutex
fd *os.File
identity descriptorV3
chunkBlocks uint64
failed error
lastGeneration uint64
syncFile func() error
syncDirectory func() error
latchPersistence func(error)
}
func createChunkLSNMapV3(path string, d descriptorV3, cb uint64) (*chunkLSNMapV3, error) {
@@ -294,6 +295,21 @@ func openChunkLSNMapV3(path string, d descriptorV3, cb uint64) (*chunkLSNMapV3,
func (m *chunkLSNMapV3) Prepare(g, C uint64, entries []chunkLSNEntryV3, prior *chunkMapCandidateV3) (out *chunkMapCandidateV3, err error) {
m.mu.Lock()
defer m.mu.Unlock()
return m.prepareLockedV3(g, C, entries, prior)
}
// Generation reservation and persistence share the existing sidecar mutex.
// Concurrent preparers cannot select the same generation; no new lock or
// scheduler is introduced, and aborted candidates remain immutable orphans.
func (m *chunkLSNMapV3) prepareNextV3(C uint64, entries []chunkLSNEntryV3, prior *chunkMapCandidateV3) (*chunkMapCandidateV3, error) {
m.mu.Lock()
defer m.mu.Unlock()
if m.lastGeneration >= counterMaxV3 {
return nil, ErrCounterExhausted
}
return m.prepareLockedV3(m.lastGeneration+1, C, entries, prior)
}
func (m *chunkLSNMapV3) prepareLockedV3(g, C uint64, entries []chunkLSNEntryV3, prior *chunkMapCandidateV3) (out *chunkMapCandidateV3, err error) {
if m.failed != nil {
return nil, m.failed
}
@@ -331,6 +347,9 @@ func (m *chunkLSNMapV3) Prepare(g, C uint64, entries []chunkLSNEntryV3, prior *c
defer func() {
if err != nil {
m.failed = err
if m.latchPersistence != nil {
m.latchPersistence(err)
}
}
}()
write := func(b []byte) (uint64, error) {
@@ -0,0 +1,100 @@
package storage
// Actual owned-map checkpoint path, still dark until the single activation.
// capture/lifecycle admission remains with the existing caller; preparation
// performs no I/O under store/descriptor/leader locks. Publication uses the
// existing syncMu -> store.mu -> descMu -> fileSyncMu graph.
// Caller holds WALStore.mu. Higher-LSN writes update the max; a reused or
// lower/equal effective LSN is conservative UNKNOWN, independent of the max.
// Capture is the only future owner allowed to clear UNKNOWN after a version
// check. Keeping it set here cannot manufacture a remotely clean chunk.
func (s *WALStore) recordMapWriteV3(lba, lsn uint64) {
v := s.v3
if v.ownedMap == nil {
return
}
e := &v.mapEntries[lba/v.ownedMap.chunkBlocks]
if lsn <= e.LSN {
e.Unknown = true
}
e.LSN = max(e.LSN, lsn)
}
func (s *WALStore) prepareOwnedMapCheckpointV3(cp uint64) (*preparedMapPublicationV3, error) {
v := s.v3
s.mu.RLock()
if err := s.stickyIOFault(); err != nil {
s.mu.RUnlock()
return nil, err
}
v.pub.descMu.Lock()
prior, domain := v.pub.mapCurrent, v.pub.mapDomain
if prior == nil || prior.owner != v.ownedMap || domain != v.mapProof {
v.pub.descMu.Unlock()
s.mu.RUnlock()
return nil, ErrMapCoverageStale
}
proof := domain.snapshotV3()
entries := append([]chunkLSNEntryV3(nil), v.mapEntries...)
v.pub.descMu.Unlock()
s.mu.RUnlock()
c, err := v.ownedMap.prepareNextV3(cp, entries, prior)
if err != nil {
return nil, err
}
if !proof.validV3(domain) {
return nil, ErrMapCoverageStale
}
return &preparedMapPublicationV3{candidate: c, proof: proof}, nil
}
func (s *WALStore) persistOwnedMapCheckpointV3(highestLSN uint64) error {
v := s.v3
cp := min(highestLSN, v.wack.Load())
s.mu.RLock()
floorSource := s.recycleFloorSrc
s.mu.RUnlock()
if floorSource != nil {
if floor, active := floorSource.MinPinAcrossActiveSessions(); active {
cp = min(cp, floor)
}
}
if cp <= v.pub.current().CheckpointLSN {
return nil
}
// The extent was fsynced by the flusher. Optional metadata and chunk
// callbacks settle before map persistence and the sole slot publisher.
if s.metadata != nil {
if err := s.metadata.BeforeCheckpointAdvance(cp); err != nil {
return err
}
}
if h := s.chunk.Load(); h != nil {
if err := h.sink.BeforeCheckpointAdvance(cp); err != nil {
return err
}
}
prepared, err := s.prepareOwnedMapCheckpointV3(cp)
if err != nil {
return err
}
v.syncMu.Lock()
defer v.syncMu.Unlock()
wack := v.wack.Load()
if wack < cp {
return ErrMapCoverageStale
}
d, err := v.pub.publishMapV3(&s.mu, nil, prepared, func(n *descriptorV3) error {
n.W = max(n.W, wack)
n.CheckpointLSN = cp
return nil
})
if err != nil {
return err
}
s.mu.Lock()
s.checkpointLSN = d.CheckpointLSN
s.mu.Unlock()
return nil
}
@@ -0,0 +1,95 @@
package storage
import (
"bytes"
"errors"
"testing"
)
func newMappedCheckpointStoreV3(t *testing.T) (*WALStore, string) {
t.Helper()
path := t.TempDir() + "/mapped"
s, err := createMappedWALStoreV3ForTest(path, 64, 4096, v3TestWAL, WModeStrict, 4)
if err != nil {
t.Fatal(err)
}
s.DisableAutoFlushForRecoveryTest()
t.Cleanup(func() { s.Close() })
return s, path
}
func TestWALStoreV3_AutomaticMapCheckpoint(t *testing.T) {
s, path := newMappedCheckpointStoreV3(t)
writeSynced(t, s, 1, 2)
if err := s.flusher.flushOnce(); err != nil {
t.Fatal(err)
}
d := s.v3.pub.current()
if d.CheckpointLSN != 2 || d.MapCoveredCheckpoint != 2 || d.W < 2 {
t.Fatalf("automatic map checkpoint stayed at cp=%d C=%d W=%d", d.CheckpointLSN, d.MapCoveredCheckpoint, d.W)
}
es, _, err := readChunkLSNMapV3(s.v3.ownedMap.fd, d, 4)
if err != nil {
t.Fatal(err)
}
if es[0].LSN != 2 || !es[0].Unknown {
t.Errorf("map lost effective write coverage: %+v", es[0])
}
if s.dm.len() != 0 {
t.Error("covered dirty entries not retired")
}
if err = s.Close(); err != nil {
t.Fatal(err)
}
r, err := openWALStoreV3ForTest(path)
if err != nil {
t.Fatal(err)
}
r.DisableAutoFlushForRecoveryTest()
t.Cleanup(func() { r.Close() })
if _, err = r.Recover(); err != nil {
t.Fatal(err)
}
for lba := uint32(1); lba <= 2; lba++ {
if !bytes.Equal(readV3(t, r, lba), blk(byte(lba))) {
t.Errorf("LBA%d lost after checkpoint reopen", lba)
}
}
}
func TestWALStoreV3_AutomaticMapPersistenceFailureLatchesStore(t *testing.T) {
for _, stage := range []string{"file", "directory"} {
t.Run(stage, func(t *testing.T) {
s, _ := newMappedCheckpointStoreV3(t)
writeSynced(t, s, 1, 1)
before := s.v3.pub.current()
injected := errors.New("injected map persistence failure")
if stage == "file" {
s.v3.ownedMap.syncFile = func() error { return injected }
} else {
s.v3.ownedMap.syncDirectory = func() error { return injected }
}
err := s.flusher.flushOnce()
if !errors.Is(err, injected) {
t.Errorf("automatic flusher did not surface map persistence failure: %v", err)
}
if s.stickyIOFault() == nil {
t.Error("map persistence failure did not latch the whole store")
}
if s.v3.pub.current() != before {
t.Error("failed map persistence published a descriptor")
}
if s.dm.len() != 1 {
t.Error("failed map persistence retired dirty retry ownership")
}
if _, err = s.Sync(); err == nil {
t.Error("Sync acknowledged after map persistence failure")
}
if _, err = s.Write(2, blk(2)); err == nil {
t.Error("Write accepted after map persistence failure")
}
if _, err = s.Recover(); err == nil {
t.Error("Recover accepted after map persistence failure")
}
// Close is expected to surface the latched failure and still owns cleanup.
})
}
}
+30 -5
View File
@@ -24,9 +24,11 @@ import (
// install paths (ApplyEntry, WriteExtentDirect, AdvanceFrontier; rows 9 to
// 11) and the checkpoint/tail advance, which needs the row-7 map candidate.
type walStoreV3State struct {
pub *descriptorPublisherV3
w *walWriterV3
ownedMap *chunkLSNMapV3
pub *descriptorPublisherV3
w *walWriterV3
ownedMap *chunkLSNMapV3
mapEntries []chunkLSNEntryV3 // guarded by WALStore.mu; UNKNOWN is never cleared here
mapProof *mapProofEpochV3
appended atomic.Uint64 // highest LSN pwritten to the WAL
wack atomic.Uint64 // highest acknowledged LSN (Sync completed)
@@ -110,7 +112,7 @@ var testSkipFenceOnRecoverV3 bool
func (s *WALStore) latchV3(err error) {
if err != nil {
s.ioFault.CompareAndSwap(nil, NewSubstrateIOFailure(err,
"v3 WAL append or descriptor publication failed; the open store refuses further Syncs, writes and checkpoints"))
"v3 durable persistence failed; the open store refuses further Syncs, writes and checkpoints"))
}
}
@@ -298,10 +300,13 @@ func openWALStoreV3MapInternalForTest(path string, requireMap bool) (*WALStore,
}
v.wack.Store(d.W)
if ownedMap != nil {
if err := v.pub.bindMapV3(mapCurrent, newMapProofEpochV3()); err != nil {
v.mapProof = newMapProofEpochV3()
if err := v.pub.bindMapV3(mapCurrent, v.mapProof); err != nil {
return err
}
v.ownedMap = ownedMap
v.mapEntries = append([]chunkLSNEntryV3(nil), mapCurrent.entries...)
ownedMap.latchPersistence = s.latchV3
}
s.v3 = v
return nil
@@ -351,6 +356,14 @@ func (s *WALStore) recoverV3() (uint64, error) {
if o.Installing {
return 0, installingErrorV3(o)
}
// Reconstruct volatile map coverage from the authoritative immutable root,
// then replay only the retained prefix. A same-open Recover must not keep
// speculative cache changes from a dropped suffix.
if v.ownedMap != nil {
v.pub.descMu.Lock()
v.mapEntries = append(v.mapEntries[:0], v.pub.mapCurrent.entries...)
v.pub.descMu.Unlock()
}
// Replay the records <= R into the dirty map (last writer wins); the
// rest are dropped and fenced below.
for _, r := range scan.Records {
@@ -365,6 +378,7 @@ func (s *WALStore) recoverV3() (uint64, error) {
size := uint64(walEntryHeaderSizeV3 + len(e.Data))
phys := (r.End - size) % d.WALSize
s.dm.putAtIfNewer(e.LBA, phys, 0, e.LSN, d.BlockSize)
s.recordMapWriteV3(e.LBA, e.LSN)
}
if h := v.testRecoverHold; h != nil {
h()
@@ -447,6 +461,13 @@ func (s *WALStore) appendWriteV3(lba uint32, data []byte, lsn uint64) (uint64, e
if s.v3.w == nil {
return 0, errWALBindingV3 // a half-published reset before its Recover
}
// Reserve proof invalidation before the WAL can change. This is under
// the existing store lock; a refused append only causes conservative retry.
if s.v3.mapProof != nil {
if err := s.v3.mapProof.mutatedV3(); err != nil {
return 0, err
}
}
phys, err := s.v3.w.append(e, s.v3.wack.Load())
if err != nil {
if errors.Is(err, errWALPwrite) {
@@ -454,6 +475,7 @@ func (s *WALStore) appendWriteV3(lba uint32, data []byte, lsn uint64) (uint64, e
}
return 0, err
}
s.recordMapWriteV3(uint64(lba), lsn)
s.v3.appended.Store(lsn)
return phys, nil
}
@@ -674,6 +696,9 @@ func (f *flusher) readDirtyRecordV3(e snapshotEntry) ([]byte, uint64, error) {
// prepared candidate meanwhile, this cycle publishes nothing.
func (s *WALStore) persistCheckpointV3(highestLSN uint64) error {
v := s.v3
if v.ownedMap != nil {
return s.persistOwnedMapCheckpointV3(highestLSN)
}
if v.mapCandidate == nil {
return nil
}