eth/protocols/snap: improve metrics

This commit is contained in:
Gary Rong 2025-07-23 09:41:44 +08:00
parent 93481b2843
commit 3d80bed231
2 changed files with 14 additions and 10 deletions

View file

@ -67,6 +67,6 @@ var (
largeStorageDiscardGauge = metrics.NewRegisteredGauge("eth/protocols/snap/sync/storage/chunk/discard", nil) largeStorageDiscardGauge = metrics.NewRegisteredGauge("eth/protocols/snap/sync/storage/chunk/discard", nil)
largeStorageResumedGauge = metrics.NewRegisteredGauge("eth/protocols/snap/sync/storage/chunk/resume", nil) largeStorageResumedGauge = metrics.NewRegisteredGauge("eth/protocols/snap/sync/storage/chunk/resume", nil)
syncTimeGauge = metrics.NewRegisteredGauge("eth/protocols/snap/sync/time", nil) stateSyncTimeGauge = metrics.NewRegisteredGauge("eth/protocols/snap/sync/time/statesync", nil)
healTimeGauge = metrics.NewRegisteredGauge("eth/protocols/snap/heal/time", nil) stateHealTimeResettingTimer = metrics.NewRegisteredResettingTimer("eth/protocols/snap/sync/time/stateheal", nil)
) )

View file

@ -505,7 +505,6 @@ type Syncer struct {
startTime time.Time // Time instance when snapshot sync started startTime time.Time // Time instance when snapshot sync started
healStartTime time.Time // Time instance when the state healing started healStartTime time.Time // Time instance when the state healing started
syncTimeOnce sync.Once // Ensure that the state sync time is uploaded only once syncTimeOnce sync.Once // Ensure that the state sync time is uploaded only once
healTimeOnce sync.Once // Ensure that the state healing time is uploaded only once
logTime time.Time // Time instance when status was last reported logTime time.Time // Time instance when status was last reported
pend sync.WaitGroup // Tracks network request goroutines for graceful shutdown pend sync.WaitGroup // Tracks network request goroutines for graceful shutdown
@ -688,10 +687,13 @@ func (s *Syncer) Sync(root common.Hash, cancel chan struct{}) error {
s.cleanStorageTasks() s.cleanStorageTasks()
s.cleanAccountTasks() s.cleanAccountTasks()
if len(s.tasks) == 0 && s.healer.scheduler.Pending() == 0 { if len(s.tasks) == 0 && s.healer.scheduler.Pending() == 0 {
s.healTimeOnce.Do(func() { // State healing phase completed, record the elapsed time in metrics.
healTimeGauge.Update(int64(time.Since(s.healStartTime))) // Note: healing may be rerun in subsequent cycles to fill gaps between
log.Info("State healing phase is completed", "elapsed", common.PrettyDuration(time.Since(s.healStartTime))) // pivot states (e.g., if chain sync takes longer). The initial healing
}) // phase is more important for us.
stateHealTimeResettingTimer.Update(time.Since(s.healStartTime))
s.healStartTime = time.Time{} // zero the start time
log.Info("State healing phase is completed", "elapsed", common.PrettyDuration(time.Since(s.healStartTime)))
return nil return nil
} }
// Assign all the data retrieval tasks to any free peers // Assign all the data retrieval tasks to any free peers
@ -700,15 +702,17 @@ func (s *Syncer) Sync(root common.Hash, cancel chan struct{}) error {
s.assignStorageTasks(storageResps, storageReqFails, cancel) s.assignStorageTasks(storageResps, storageReqFails, cancel)
if len(s.tasks) == 0 { if len(s.tasks) == 0 {
// Sync phase done, push elapsed time to metrics // State sync phase completed, record the elapsed time in metrics.
// Note: the initial state sync runs only once, regardless of whether
// a new cycle is started later. Any state differences in subsequent
// cycles will be handled by the state healer.
s.syncTimeOnce.Do(func() { s.syncTimeOnce.Do(func() {
syncTimeGauge.Update(int64(time.Since(s.startTime))) stateSyncTimeGauge.Update(int64(time.Since(s.startTime)))
log.Info("State sync phase is completed", "elapsed", common.PrettyDuration(time.Since(s.startTime))) log.Info("State sync phase is completed", "elapsed", common.PrettyDuration(time.Since(s.startTime)))
}) })
if s.healStartTime.IsZero() { if s.healStartTime.IsZero() {
s.healStartTime = time.Now() s.healStartTime = time.Now()
} }
// run heal phase
s.assignTrienodeHealTasks(trienodeHealResps, trienodeHealReqFails, cancel) s.assignTrienodeHealTasks(trienodeHealResps, trienodeHealReqFails, cancel)
s.assignBytecodeHealTasks(bytecodeHealResps, bytecodeHealReqFails, cancel) s.assignBytecodeHealTasks(bytecodeHealResps, bytecodeHealReqFails, cancel)
} }