storage: report checkpoint retry reasons and cumulative outcomes

This commit is contained in:
pingqiu committed 2026-11-19 09:38:18 -08:00
1 parent 88cfb04f71
commit 751fe4ad0e
9 files changed
+204 -51

No files matched your search

@@ -21,6 +21,9 @@ func TestDurableProvider_CheckpointPersistenceStatus(t *testing.T) {
t.Fatal(err)
}
t.Cleanup(func() { p.Close() })
if _, err := p.EnsureStorage("v1"); err != nil {
t.Fatal(err)
}
if _, err := p.RecoverVolume(context.Background(), "v1"); err != nil {
t.Fatal(err)
}
+16 -15
View File
@@ -253,21 +253,22 @@ type volHandle struct {
// handle. It intentionally exposes lineage and readiness facts without giving
// callers a control surface.
type VolumeStatus struct {
VolumeID string
Path string
Impl string
ReplicaID string
Epoch uint64
EndpointVersion uint64
Latched bool
Operational bool
FrontierKnown bool
DurableLSN uint64
RetainedLSN uint64
HeadLSN uint64
Evidence string
Closed bool
WriteProfile WriteProfileStatus
VolumeID string
Path string
Impl string
ReplicaID string
Epoch uint64
EndpointVersion uint64
Latched bool
Operational bool
FrontierKnown bool
DurableLSN uint64
RetainedLSN uint64
HeadLSN uint64
Evidence string
Closed bool
WriteProfile WriteProfileStatus
CheckpointPersistence *storage.CheckpointPersistenceStatus `json:",omitempty"`
// Chunk is the chunk layer's state and cut counters; walstore-chunk
// volumes only.
Chunk *ChunkStatus
@@ -288,6 +288,13 @@ func (b *StorageBackend) durableStatus(volumeID, path, impl string) VolumeStatus
if c, ok := b.storage.(interface{ HolesSuspected() uint64 }); ok {
holes = c.HolesSuspected()
}
var checkpoint *storage.CheckpointPersistenceStatus
if reporter, ok := b.storage.(interface {
CheckpointPersistence() storage.CheckpointPersistenceStatus
}); ok {
snapshot := reporter.CheckpointPersistence()
checkpoint = &snapshot
}
writeProfile := b.WriteProfile()
if instrumented, ok := b.storage.(storage.WriteInstrumented); ok {
storageProfile := instrumented.WriteInstrumentation()
@@ -316,23 +323,24 @@ func (b *StorageBackend) durableStatus(volumeID, path, impl string) VolumeStatus
writeProfile.DirtyMapUpdateDurationNanos = storageProfile.DirtyMapUpdateDurationNanos
}
return VolumeStatus{
Warnings: warnings,
HolesSuspected: holes,
VolumeID: volumeID,
Path: path,
Impl: impl,
ReplicaID: id.ReplicaID,
Epoch: id.Epoch,
EndpointVersion: id.EndpointVersion,
Latched: id.Epoch > 0 && id.EndpointVersion > 0,
Operational: b.operational.Load(),
FrontierKnown: true,
DurableLSN: durableLSN,
RetainedLSN: retainedLSN,
HeadLSN: headLSN,
Evidence: evidence,
Closed: closed,
WriteProfile: writeProfile,
Warnings: warnings,
HolesSuspected: holes,
VolumeID: volumeID,
Path: path,
Impl: impl,
ReplicaID: id.ReplicaID,
Epoch: id.Epoch,
EndpointVersion: id.EndpointVersion,
Latched: id.Epoch > 0 && id.EndpointVersion > 0,
Operational: b.operational.Load(),
FrontierKnown: true,
DurableLSN: durableLSN,
RetainedLSN: retainedLSN,
HeadLSN: headLSN,
Evidence: evidence,
Closed: closed,
WriteProfile: writeProfile,
CheckpointPersistence: checkpoint,
}
}
@@ -0,0 +1,64 @@
package storage
import (
"errors"
"log"
"time"
)
// CheckpointPersistenceStatus counts flusher checkpoint calls in this open.
// Success includes a safely clamped/unchanged checkpoint; it is not a slot-write
// count. Fields are cumulative atomic observations, not a coherent frontier.
type CheckpointPersistenceStatus struct {
Successes uint64
Refusals uint64
Failures uint64
}
func (s *WALStore) CheckpointPersistence() CheckpointPersistenceStatus {
if s.flusher == nil {
return CheckpointPersistenceStatus{}
}
i := &s.flusher.instr
return CheckpointPersistenceStatus{i.checkpointSuccesses.Load(), i.checkpointRefusals.Load(), i.checkpointFailures.Load()}
}
func (i *flusherInstrumentation) recordCheckpoint(err error) {
switch {
case err == nil:
i.checkpointSuccesses.Add(1)
case errors.Is(err, ErrMapCoverageStale):
i.checkpointRefusals.Add(1)
default:
i.checkpointFailures.Add(1)
}
}
type mapCoverageRefusal string
func (r mapCoverageRefusal) Error() string { return ErrMapCoverageStale.Error() + ": " + string(r) }
func (r mapCoverageRefusal) Unwrap() error { return ErrMapCoverageStale }
// Owned exclusively by the flusher run loop; no storage lock or I/O ordering
// changes. Terminal errors are never rate limited.
type checkpointLogLimiter struct {
last time.Time
suppressed uint64
}
func (l *checkpointLogLimiter) report(err error, now time.Time) {
if !errors.Is(err, ErrMapCoverageStale) {
log.Printf("storage: flusher: %v", err)
return
}
if !l.last.IsZero() && now.Sub(l.last) < 30*time.Second {
l.suppressed++
return
}
var reason mapCoverageRefusal
if !errors.As(err, &reason) {
reason = "unspecified"
}
log.Printf("storage: flusher: checkpoint deferred reason=%s suppressed=%d: %v", reason, l.suppressed, err)
l.last, l.suppressed = now, 0
}
@@ -1,14 +1,19 @@
package storage
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"log"
"strings"
"testing"
"time"
)
func TestCheckpointDiagnostics_RefusalThenSuccess(t *testing.T) {
s, _ := newMappedCheckpointStoreV3(t)
mustAttach(t, s, newFakeChunkRemote(), ctConfig(1))
writeSynced(t, s, 1, 1)
s.v3.testMapPrepared = func() {
if _, err := s.Write(2, blk(2)); err != nil {
@@ -49,3 +54,65 @@ func TestCheckpointDiagnostics_RefusalThenSuccess(t *testing.T) {
t.Fatalf("retry counters/state: %s cp=%d", raw, s.CheckpointLSN())
}
}
func TestCheckpointDiagnostics_PrepareRefusalAndIOFailure(t *testing.T) {
for _, fault := range []bool{false, true} {
t.Run(fmt.Sprint(fault), func(t *testing.T) {
s, _ := newMappedCheckpointStoreV3(t)
mustAttach(t, s, newFakeChunkRemote(), ctConfig(1))
writeSynced(t, s, 1, 1)
realSync := s.v3.ownedMap.syncFile
injected := errors.New("checkpoint map fsync fault")
once := false
s.v3.ownedMap.syncFile = func() error {
if fault {
return injected
}
if !once {
once = true
if _, err := s.Write(2, blk(2)); err != nil {
t.Fatal(err)
}
}
return realSync()
}
err := s.flusher.flushOnce()
got := s.CheckpointPersistence()
if fault {
if !errors.Is(err, injected) || got.Failures != 1 || got.Refusals != 0 || s.stickyIOFault() == nil {
t.Fatalf("fault %v counts=%+v", err, got)
}
} else {
if !errors.Is(err, ErrMapCoverageStale) || !strings.Contains(err.Error(), "proof_changed_during_prepare") || got.Refusals != 1 || got.Failures != 0 || s.stickyIOFault() != nil {
t.Fatalf("refusal %v counts=%+v", err, got)
}
}
if got.Successes != 0 || s.CheckpointLSN() != 0 || s.dm.len() == 0 {
t.Fatal("failed checkpoint advanced or lost retry ownership")
}
})
}
}
func TestCheckpointDiagnostics_LogRateLimit(t *testing.T) {
var out bytes.Buffer
old := log.Writer()
log.SetOutput(&out)
t.Cleanup(func() { log.SetOutput(old) })
var limiter checkpointLogLimiter
now := time.Unix(100, 0)
stale := fmt.Errorf("persist checkpoint: %w", mapCoverageRefusal("proof_changed"))
limiter.report(stale, now)
for i := 0; i < 5; i++ {
limiter.report(stale, now.Add(time.Second))
}
// A terminal failure must remain visible while refusals are suppressed.
terminal := errors.New("real fsync fault")
limiter.report(terminal, now.Add(2*time.Second))
limiter.report(terminal, now.Add(3*time.Second))
limiter.report(stale, now.Add(30*time.Second))
got := out.String()
if strings.Count(got, "checkpoint deferred") != 2 || strings.Count(got, "real fsync fault") != 2 || !strings.Contains(got, "reason=proof_changed suppressed=5") {
t.Fatalf("logs: %s", got)
}
}
@@ -57,7 +57,7 @@ type preparedMapPublicationV3 struct {
// Preparation takes the sidecar mutex and I/O only, before store/desc locks.
func prepareMapPublicationV3(m *chunkLSNMapV3, d *mapProofEpochV3, g, C uint64, entries []chunkLSNEntryV3, prior *chunkMapCandidateV3) (*preparedMapPublicationV3, error) {
if m == nil || d == nil {
return nil, ErrMapCoverageStale
return nil, mapCoverageRefusal("map_binding_missing")
}
proof := d.snapshotV3()
c, e := m.Prepare(g, C, entries, prior)
@@ -66,7 +66,7 @@ func prepareMapPublicationV3(m *chunkLSNMapV3, d *mapProofEpochV3, g, C uint64,
}
if !proof.validV3(d) {
m.abandonCandidateV3(c) // prepared but never publishable
return nil, ErrMapCoverageStale
return nil, mapCoverageRefusal("proof_changed_during_prepare")
}
return &preparedMapPublicationV3{candidate: c, proof: proof}, nil
}
@@ -84,13 +84,13 @@ func sameMapTupleV3(d descriptorV3, c *chunkMapCandidateV3) bool {
// even when none of its pages belong to the selected generation.
func (p *descriptorPublisherV3) bindMapV3(c *chunkMapCandidateV3, d *mapProofEpochV3) error {
if c == nil || c.owner == nil || d == nil {
return ErrMapCoverageStale
return mapCoverageRefusal("map_binding_missing")
}
p.descMu.Lock()
current, other, otherValid := p.cur, p.other, p.otherValid
p.descMu.Unlock()
if !sameMapTupleV3(current, c) {
return ErrMapCoverageStale
return mapCoverageRefusal("map_tuple_changed")
}
if _, _, e := readChunkLSNMapV3(c.owner.fd, current, c.owner.chunkBlocks); e != nil {
return e
@@ -106,7 +106,7 @@ func (p *descriptorPublisherV3) bindMapV3(c *chunkMapCandidateV3, d *mapProofEpo
p.descMu.Lock()
defer p.descMu.Unlock()
if p.cur != current || p.otherValid != otherValid || (otherValid && p.other != other) {
return ErrMapCoverageStale
return mapCoverageRefusal("descriptor_changed_during_bind")
}
p.mapCurrent = c
p.mapDomain = d
@@ -115,7 +115,7 @@ func (p *descriptorPublisherV3) bindMapV3(c *chunkMapCandidateV3, d *mapProofEpo
func (p *descriptorPublisherV3) validateMapPublicationV3(next descriptorV3, prepared *preparedMapPublicationV3) error {
if p.mapCurrent == nil {
if prepared != nil {
return ErrMapCoverageStale
return mapCoverageRefusal("map_not_bound")
}
return nil
} // dark core not bound yet
@@ -123,20 +123,20 @@ func (p *descriptorPublisherV3) validateMapPublicationV3(next descriptorV3, prep
if prepared != nil {
c = prepared.candidate
if c == nil || c.owner != p.mapCurrent.owner || !prepared.proof.validV3(p.mapDomain) {
return ErrMapCoverageStale
return mapCoverageRefusal("proof_changed")
}
if c.generation <= p.mapCurrent.generation || c.covered < p.mapCurrent.covered {
return ErrMapCoverageStale
return mapCoverageRefusal("candidate_superseded")
}
}
if !sameMapTupleV3(next, c) || c.covered < next.CheckpointLSN || c.covered > next.W {
return ErrMapCoverageStale
return mapCoverageRefusal("map_coverage_mismatch")
}
return nil
}
func (p *descriptorPublisherV3) publishMapV3(storeMu sync.Locker, snap func(), prepared *preparedMapPublicationV3, mutate func(*descriptorV3) error) (descriptorV3, error) {
if prepared == nil || prepared.candidate == nil {
return descriptorV3{}, ErrMapCoverageStale
return descriptorV3{}, mapCoverageRefusal("candidate_missing")
}
d, err := p.publishWithMapV3(storeMu, snap, func(d *descriptorV3) error {
if mutate != nil {
+5 -4
View File
@@ -3,7 +3,6 @@ package storage
import (
"encoding/binary"
"fmt"
"log"
"sort"
"sync"
"sync/atomic"
@@ -111,6 +110,7 @@ func (f *flusher) runWithStartSignal(started chan<- struct{}) {
if started != nil {
close(started)
}
var diagnostics checkpointLogLimiter
for {
select {
case <-f.stopCh:
@@ -120,11 +120,11 @@ func (f *flusher) runWithStartSignal(started chan<- struct{}) {
return
case <-ticker.C:
if err := f.flushOnce(); err != nil {
log.Printf("storage: flusher: %v", err)
diagnostics.report(err, time.Now())
}
case <-f.notifyCh:
if err := f.flushOnce(); err != nil {
log.Printf("storage: flusher (notify): %v", err)
diagnostics.report(err, time.Now())
}
}
}
@@ -283,6 +283,7 @@ func (f *flusher) flushOnceInternal(allowClosed bool) (err error) {
} else {
publishErr = store.persistCheckpointAndRelease(maxLSN, maxLSNPhys, maxLSNEntrySize)
}
f.instr.recordCheckpoint(publishErr)
if err := publishErr; err != nil {
return fmt.Errorf("flusher: persist checkpoint %d: %w", maxLSN, err)
}
@@ -313,7 +314,7 @@ func (f *flusher) flushOnceInternal(allowClosed bool) (err error) {
store.mu.Lock()
if !store.v3.flushLifetimeValidV3(recoveryGen) {
store.mu.Unlock()
return ErrMapCoverageStale
return mapCoverageRefusal("recovery_lifetime_changed")
}
checkpoint = store.checkpointLSN
if checkpoint >= maxLSN && maxLSN+1 > store.walTail {
@@ -12,6 +12,9 @@ const maxCheckpointWriteBytes = 1 << 20
// checkpoint work. Operation counters include failed attempts; byte counters
// include only successful writes.
type FlusherInstrumentationStatus struct {
CheckpointSuccesses uint64
CheckpointRefusals uint64
CheckpointFailures uint64
CyclesStarted uint64
CyclesSucceeded uint64
CyclesFailed uint64
@@ -63,6 +66,9 @@ type FlusherInstrumentationStatus struct {
}
type flusherInstrumentation struct {
checkpointSuccesses atomic.Uint64
checkpointRefusals atomic.Uint64
checkpointFailures atomic.Uint64
cyclesStarted atomic.Uint64
cyclesSucceeded atomic.Uint64
cyclesFailed atomic.Uint64
@@ -284,6 +290,9 @@ func updateAtomicMax(target *atomic.Uint64, value uint64) {
func (i *flusherInstrumentation) snapshot() FlusherInstrumentationStatus {
return FlusherInstrumentationStatus{
CheckpointSuccesses: i.checkpointSuccesses.Load(),
CheckpointRefusals: i.checkpointRefusals.Load(),
CheckpointFailures: i.checkpointFailures.Load(),
CyclesStarted: i.cyclesStarted.Load(),
CyclesSucceeded: i.cyclesSucceeded.Load(),
CyclesFailed: i.cyclesFailed.Load(),
@@ -33,7 +33,7 @@ func (s *WALStore) prepareOwnedMapCheckpointV3(cp uint64) (*preparedMapPublicati
if prior == nil || prior.owner != v.ownedMap || domain != v.mapProof {
v.pub.descMu.Unlock()
s.mu.RUnlock()
return nil, ErrMapCoverageStale
return nil, mapCoverageRefusal("map_binding_changed")
}
proof := domain.snapshotV3()
entries := append([]chunkLSNEntryV3(nil), v.mapEntries...)
@@ -45,7 +45,7 @@ func (s *WALStore) prepareOwnedMapCheckpointV3(cp uint64) (*preparedMapPublicati
}
if !proof.validV3(domain) {
v.ownedMap.abandonCandidateV3(c) // prepared but never publishable
return nil, ErrMapCoverageStale
return nil, mapCoverageRefusal("proof_changed_during_prepare")
}
return &preparedMapPublicationV3{candidate: c, proof: proof}, nil
}
@@ -70,7 +70,7 @@ func (s *WALStore) persistOwnedMapCheckpointV3(highestLSN, recoveryGen uint64) e
s.mu.Lock()
defer s.mu.Unlock()
if !v.flushLifetimeValidV3(recoveryGen) {
return ErrMapCoverageStale
return mapCoverageRefusal("recovery_lifetime_changed")
}
return s.rolloverCheckpointV3Locked()
}
@@ -118,7 +118,7 @@ func (s *WALStore) publishCheckpointV3(cp uint64, seed bool, recoveryGen uint64)
// I/O and could belong to a newer Recover lifetime than those bytes.
if !v.flushLifetimeValidV3(recoveryGen) {
prepared.abandonV3()
return descriptorV3{}, ErrMapCoverageStale
return descriptorV3{}, mapCoverageRefusal("recovery_lifetime_changed")
}
wack := v.wack.Load()
if seed {
@@ -126,7 +126,7 @@ func (s *WALStore) publishCheckpointV3(cp uint64, seed bool, recoveryGen uint64)
}
if wack < cp {
prepared.abandonV3()
return descriptorV3{}, ErrMapCoverageStale
return descriptorV3{}, mapCoverageRefusal("checkpoint_above_wack")
}
s.mu.RLock()
retention := s.recoveryRetentionLSNs