storage: report the persisted checkpoint and its last advance

CheckpointPersistenceStatus now carries the persisted CheckpointLSN and
the count and time of the checkpoint calls that moved it. A success that
was clamped or left the checkpoint unchanged is not an advance, so a
starved checkpoint shows Successes rising while LastAdvance stays frozen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-19 10:26:54 -08:00
1 parent a31c73f138
commit a2deedc49a
5 files changed
+85 -12

No files matched your search

@@ -46,15 +46,20 @@ func TestDurableProvider_CheckpointPersistenceStatus(t *testing.T) {
}
var wire []struct {
CheckpointPersistence *struct {
Successes uint64
Refusals uint64
Failures uint64
Successes uint64
Refusals uint64
Failures uint64
CheckpointLSN uint64
Advances uint64
LastAdvance time.Time
}
}
if err := json.Unmarshal(raw, &wire); err != nil {
t.Fatal(err)
}
if len(wire) == 1 && wire[0].CheckpointPersistence != nil && wire[0].CheckpointPersistence.Successes > 0 {
if len(wire) == 1 && wire[0].CheckpointPersistence != nil && wire[0].CheckpointPersistence.Successes > 0 &&
wire[0].CheckpointPersistence.CheckpointLSN > 0 && wire[0].CheckpointPersistence.Advances > 0 &&
!wire[0].CheckpointPersistence.LastAdvance.IsZero() {
return
}
if time.Now().After(deadline) {
@@ -8,25 +8,48 @@ import (
// 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.
// count. Advances counts only the successes that moved the persisted
// CheckpointLSN, and LastAdvance is when the latest one did (zero: none in
// this open), so a starved checkpoint shows Successes rising while
// LastAdvance stays frozen. Fields are cumulative atomic observations, not a
// coherent frontier.
type CheckpointPersistenceStatus struct {
Successes uint64
Refusals uint64
Failures uint64
Successes uint64
Refusals uint64
Failures uint64
CheckpointLSN uint64
Advances uint64
LastAdvance time.Time
}
func (s *WALStore) CheckpointPersistence() CheckpointPersistenceStatus {
if s.flusher == nil {
return CheckpointPersistenceStatus{}
return CheckpointPersistenceStatus{CheckpointLSN: s.CheckpointLSN()}
}
i := &s.flusher.instr
return CheckpointPersistenceStatus{i.checkpointSuccesses.Load(), i.checkpointRefusals.Load(), i.checkpointFailures.Load()}
st := CheckpointPersistenceStatus{
Successes: i.checkpointSuccesses.Load(),
Refusals: i.checkpointRefusals.Load(),
Failures: i.checkpointFailures.Load(),
CheckpointLSN: s.CheckpointLSN(),
Advances: i.checkpointAdvances.Load(),
}
if ns := i.checkpointLastAdvanceNanos.Load(); ns != 0 {
st.LastAdvance = time.Unix(0, ns)
}
return st
}
func (i *flusherInstrumentation) recordCheckpoint(err error) {
// recordCheckpoint classifies one flusher checkpoint call; before and after
// are the persisted CheckpointLSN around it.
func (i *flusherInstrumentation) recordCheckpoint(err error, before, after uint64, now time.Time) {
switch {
case err == nil:
i.checkpointSuccesses.Add(1)
if after > before {
i.checkpointAdvances.Add(1)
i.checkpointLastAdvanceNanos.Store(now.UnixNano())
}
case errors.Is(err, ErrMapCoverageStale):
i.checkpointRefusals.Add(1)
default:
@@ -116,3 +116,45 @@ func TestCheckpointDiagnostics_LogRateLimit(t *testing.T) {
t.Fatalf("logs: %s", got)
}
}
// A starved checkpoint: flush cycles keep succeeding (clamped to the
// acknowledged frontier, nothing persisted moves) while the persisted
// CheckpointLSN and the last advance stay frozen. Only a real move of the
// persisted checkpoint counts as an advance.
func TestCheckpointDiagnostics_StarvedCheckpointIsVisible(t *testing.T) {
s, _ := newMappedCheckpointStoreV3(t)
mustAttach(t, s, newFakeChunkRemote(), ctConfig(1))
writeSynced(t, s, 1, 1)
if err := s.flusher.flushOnce(); err != nil {
t.Fatal(err)
}
first := s.CheckpointPersistence()
if first.CheckpointLSN != 1 || first.Advances != 1 || first.LastAdvance.IsZero() || first.Successes != 1 {
t.Fatalf("first checkpoint: %+v", first)
}
for i := uint32(0); i < 3; i++ { // written, never synced: the checkpoint is clamped at 1
if _, err := s.Write(2+i, blk(byte(2+i))); err != nil {
t.Fatal(err)
}
if err := s.flusher.flushOnce(); err != nil {
t.Fatal(err)
}
}
starved := s.CheckpointPersistence()
if starved.Successes <= first.Successes {
t.Fatalf("flush cycles did not keep succeeding: %+v", starved)
}
if starved.CheckpointLSN != 1 || starved.Advances != 1 || !starved.LastAdvance.Equal(first.LastAdvance) {
t.Fatalf("a clamped success counted as an advance: first %+v starved %+v", first, starved)
}
if _, err := s.Sync(); err != nil {
t.Fatal(err)
}
if err := s.flusher.flushOnce(); err != nil {
t.Fatal(err)
}
moved := s.CheckpointPersistence()
if moved.CheckpointLSN != s.CheckpointLSN() || moved.CheckpointLSN <= 1 || moved.Advances != 2 || moved.LastAdvance.Before(first.LastAdvance) {
t.Fatalf("after sync the checkpoint advance was not recorded: %+v", moved)
}
}
+2 -1
View File
@@ -278,12 +278,13 @@ func (f *flusher) flushOnceInternal(allowClosed bool) (err error) {
// The released tail is persisted with the checkpoint, before the
// writer may reuse those bytes.
var publishErr error
cpBefore := store.CheckpointLSN()
if store.v3 != nil {
publishErr = store.persistCheckpointV3(maxLSN, recoveryGen)
} else {
publishErr = store.persistCheckpointAndRelease(maxLSN, maxLSNPhys, maxLSNEntrySize)
}
f.instr.recordCheckpoint(publishErr)
f.instr.recordCheckpoint(publishErr, cpBefore, store.CheckpointLSN(), time.Now())
if err := publishErr; err != nil {
return fmt.Errorf("flusher: persist checkpoint %d: %w", maxLSN, err)
}
@@ -69,6 +69,8 @@ type flusherInstrumentation struct {
checkpointSuccesses atomic.Uint64
checkpointRefusals atomic.Uint64
checkpointFailures atomic.Uint64
checkpointAdvances atomic.Uint64
checkpointLastAdvanceNanos atomic.Int64
cyclesStarted atomic.Uint64
cyclesSucceeded atomic.Uint64
cyclesFailed atomic.Uint64