diff --git a/seaweed-block/core/frontend/durable/checkpoint_status_test.go b/seaweed-block/core/frontend/durable/checkpoint_status_test.go index b3ad4cb9ce..e3eed2d4f2 100644 --- a/seaweed-block/core/frontend/durable/checkpoint_status_test.go +++ b/seaweed-block/core/frontend/durable/checkpoint_status_test.go @@ -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) { diff --git a/seaweed-block/core/storage/checkpoint_diagnostics.go b/seaweed-block/core/storage/checkpoint_diagnostics.go index 46aa71e185..68303be17a 100644 --- a/seaweed-block/core/storage/checkpoint_diagnostics.go +++ b/seaweed-block/core/storage/checkpoint_diagnostics.go @@ -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: diff --git a/seaweed-block/core/storage/checkpoint_diagnostics_test.go b/seaweed-block/core/storage/checkpoint_diagnostics_test.go index 2670beb977..6eb79badca 100644 --- a/seaweed-block/core/storage/checkpoint_diagnostics_test.go +++ b/seaweed-block/core/storage/checkpoint_diagnostics_test.go @@ -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) + } +} diff --git a/seaweed-block/core/storage/flusher.go b/seaweed-block/core/storage/flusher.go index 930e057a89..9c6175e044 100644 --- a/seaweed-block/core/storage/flusher.go +++ b/seaweed-block/core/storage/flusher.go @@ -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) } diff --git a/seaweed-block/core/storage/flusher_instrumentation.go b/seaweed-block/core/storage/flusher_instrumentation.go index ec3fcf1827..939da10058 100644 --- a/seaweed-block/core/storage/flusher_instrumentation.go +++ b/seaweed-block/core/storage/flusher_instrumentation.go @@ -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