From 751fe4ad0e9fb3ce26a7801edbcf89a31fd2e27f Mon Sep 17 00:00:00 2001 From: pingqiu Date: Thu, 19 Nov 2026 00:52:26 -0800 Subject: [PATCH] storage: report checkpoint retry reasons and cumulative outcomes --- .../durable/checkpoint_status_test.go | 3 + .../core/frontend/durable/provider.go | 31 ++++----- .../core/frontend/durable/storage_adapter.go | 42 +++++++----- .../core/storage/checkpoint_diagnostics.go | 64 ++++++++++++++++++ .../storage/checkpoint_diagnostics_test.go | 67 +++++++++++++++++++ .../core/storage/chunk_map_publication_v3.go | 20 +++--- seaweed-block/core/storage/flusher.go | 9 +-- .../core/storage/flusher_instrumentation.go | 9 +++ .../storage/walstore_map_checkpoint_v3.go | 10 +-- 9 files changed, 204 insertions(+), 51 deletions(-) create mode 100644 seaweed-block/core/storage/checkpoint_diagnostics.go diff --git a/seaweed-block/core/frontend/durable/checkpoint_status_test.go b/seaweed-block/core/frontend/durable/checkpoint_status_test.go index 7b46ce5f64..b3ad4cb9ce 100644 --- a/seaweed-block/core/frontend/durable/checkpoint_status_test.go +++ b/seaweed-block/core/frontend/durable/checkpoint_status_test.go @@ -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) } diff --git a/seaweed-block/core/frontend/durable/provider.go b/seaweed-block/core/frontend/durable/provider.go index 2e89b88c45..2fa5faf8bb 100644 --- a/seaweed-block/core/frontend/durable/provider.go +++ b/seaweed-block/core/frontend/durable/provider.go @@ -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 diff --git a/seaweed-block/core/frontend/durable/storage_adapter.go b/seaweed-block/core/frontend/durable/storage_adapter.go index 69b2b7ad8a..c7ec6169e9 100644 --- a/seaweed-block/core/frontend/durable/storage_adapter.go +++ b/seaweed-block/core/frontend/durable/storage_adapter.go @@ -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, } } diff --git a/seaweed-block/core/storage/checkpoint_diagnostics.go b/seaweed-block/core/storage/checkpoint_diagnostics.go new file mode 100644 index 0000000000..2fc3fd3af5 --- /dev/null +++ b/seaweed-block/core/storage/checkpoint_diagnostics.go @@ -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 +} diff --git a/seaweed-block/core/storage/checkpoint_diagnostics_test.go b/seaweed-block/core/storage/checkpoint_diagnostics_test.go index d811d47dde..2670beb977 100644 --- a/seaweed-block/core/storage/checkpoint_diagnostics_test.go +++ b/seaweed-block/core/storage/checkpoint_diagnostics_test.go @@ -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) + } +} diff --git a/seaweed-block/core/storage/chunk_map_publication_v3.go b/seaweed-block/core/storage/chunk_map_publication_v3.go index 8869949475..2ef10237b2 100644 --- a/seaweed-block/core/storage/chunk_map_publication_v3.go +++ b/seaweed-block/core/storage/chunk_map_publication_v3.go @@ -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 { diff --git a/seaweed-block/core/storage/flusher.go b/seaweed-block/core/storage/flusher.go index 6d52d7bf24..930e057a89 100644 --- a/seaweed-block/core/storage/flusher.go +++ b/seaweed-block/core/storage/flusher.go @@ -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 { diff --git a/seaweed-block/core/storage/flusher_instrumentation.go b/seaweed-block/core/storage/flusher_instrumentation.go index 3d7cf587fb..ec3fcf1827 100644 --- a/seaweed-block/core/storage/flusher_instrumentation.go +++ b/seaweed-block/core/storage/flusher_instrumentation.go @@ -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(), diff --git a/seaweed-block/core/storage/walstore_map_checkpoint_v3.go b/seaweed-block/core/storage/walstore_map_checkpoint_v3.go index 029d0ba210..c8273bb740 100644 --- a/seaweed-block/core/storage/walstore_map_checkpoint_v3.go +++ b/seaweed-block/core/storage/walstore_map_checkpoint_v3.go @@ -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