|
| 1 | +package datasource |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/erigontech/erigon/cmd/integration/commands" |
| 8 | + "github.com/erigontech/erigon/execution/stagedsync/stages" |
| 9 | +) |
| 10 | + |
| 11 | +// SyncMetrics holds derived sync metrics computed from StagesInfo snapshots. |
| 12 | +type SyncMetrics struct { |
| 13 | + HeadBlock uint64 // highest block across all stages |
| 14 | + PipelineHead uint64 // Finish stage progress — last block that completed the full pipeline |
| 15 | + TipLagSeconds int64 // seconds since last stage movement (0 = unknown) |
| 16 | + ImportRate float64 // blocks per minute over 60-second window |
| 17 | + Status SyncStatus |
| 18 | +} |
| 19 | + |
| 20 | +// SyncStatus represents the overall sync health. |
| 21 | +type SyncStatus int |
| 22 | + |
| 23 | +const ( |
| 24 | + StatusUnknown SyncStatus = iota |
| 25 | + StatusInSync // tip lag < 30s |
| 26 | + StatusSyncing // stages are moving but lagging |
| 27 | + StatusStalled // no stage movement for > 60s |
| 28 | +) |
| 29 | + |
| 30 | +// String returns a human-readable label for the status. |
| 31 | +func (s SyncStatus) String() string { |
| 32 | + switch s { |
| 33 | + case StatusInSync: |
| 34 | + return "IN SYNC" |
| 35 | + case StatusSyncing: |
| 36 | + return "SYNCING" |
| 37 | + case StatusStalled: |
| 38 | + return "STALLED" |
| 39 | + default: |
| 40 | + return "UNKNOWN" |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// SyncTracker computes derived sync metrics from successive StagesInfo snapshots. |
| 45 | +type SyncTracker struct { |
| 46 | + mu sync.Mutex |
| 47 | + |
| 48 | + updateCount int // number of Update() calls received |
| 49 | + prevHead uint64 |
| 50 | + prevTime time.Time |
| 51 | + lastMoveTime time.Time // last time any stage progressed |
| 52 | + |
| 53 | + // Ring buffer for 60-second import rate calculation |
| 54 | + samples []rateSample |
| 55 | + sampleIdx int |
| 56 | + sampleFull bool |
| 57 | +} |
| 58 | + |
| 59 | +type rateSample struct { |
| 60 | + block uint64 |
| 61 | + ts time.Time |
| 62 | +} |
| 63 | + |
| 64 | +const rateSamples = 12 // 12 samples × ~5s poll interval ≈ 60 seconds |
| 65 | + |
| 66 | +// NewSyncTracker creates a new SyncTracker. |
| 67 | +func NewSyncTracker() *SyncTracker { |
| 68 | + return &SyncTracker{ |
| 69 | + samples: make([]rateSample, rateSamples), |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// Update computes sync metrics from the latest StagesInfo. |
| 74 | +func (t *SyncTracker) Update(info *commands.StagesInfo) SyncMetrics { |
| 75 | + t.mu.Lock() |
| 76 | + defer t.mu.Unlock() |
| 77 | + |
| 78 | + now := time.Now() |
| 79 | + t.updateCount++ |
| 80 | + m := SyncMetrics{} |
| 81 | + |
| 82 | + // Head block: max progress across all stages |
| 83 | + for _, sp := range info.StagesProgress { |
| 84 | + if sp.Progress > m.HeadBlock { |
| 85 | + m.HeadBlock = sp.Progress |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // Pipeline head: Finish stage progress (last block that completed the full pipeline) |
| 90 | + for _, sp := range info.StagesProgress { |
| 91 | + if sp.Stage == stages.Finish { |
| 92 | + m.PipelineHead = sp.Progress |
| 93 | + break |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // Detect stage movement |
| 98 | + if t.prevTime.IsZero() { |
| 99 | + // First update — initialise but do NOT set lastMoveTime to now, |
| 100 | + // so the first poll shows StatusUnknown instead of a false green. |
| 101 | + t.prevHead = m.HeadBlock |
| 102 | + t.prevTime = now |
| 103 | + } else if m.HeadBlock != t.prevHead { |
| 104 | + t.lastMoveTime = now |
| 105 | + t.prevHead = m.HeadBlock |
| 106 | + t.prevTime = now |
| 107 | + } |
| 108 | + |
| 109 | + // Tip lag: seconds since last stage movement |
| 110 | + if t.lastMoveTime.IsZero() { |
| 111 | + m.TipLagSeconds = -1 // no movement observed yet |
| 112 | + } else { |
| 113 | + m.TipLagSeconds = int64(now.Sub(t.lastMoveTime).Seconds()) |
| 114 | + } |
| 115 | + |
| 116 | + // Import rate: blocks/minute from 60-second ring buffer |
| 117 | + t.samples[t.sampleIdx] = rateSample{block: m.HeadBlock, ts: now} |
| 118 | + t.sampleIdx = (t.sampleIdx + 1) % rateSamples |
| 119 | + if t.sampleIdx == 0 { |
| 120 | + t.sampleFull = true |
| 121 | + } |
| 122 | + |
| 123 | + m.ImportRate = t.calcRate(now) |
| 124 | + |
| 125 | + // Status determination — need at least 2 samples to judge |
| 126 | + if t.updateCount < 2 || t.lastMoveTime.IsZero() { |
| 127 | + m.Status = StatusUnknown |
| 128 | + } else { |
| 129 | + switch { |
| 130 | + case m.TipLagSeconds < 30: |
| 131 | + m.Status = StatusInSync |
| 132 | + case m.TipLagSeconds >= 60: |
| 133 | + m.Status = StatusStalled |
| 134 | + default: |
| 135 | + m.Status = StatusSyncing |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return m |
| 140 | +} |
| 141 | + |
| 142 | +// calcRate computes blocks/minute from the ring buffer. |
| 143 | +func (t *SyncTracker) calcRate(now time.Time) float64 { |
| 144 | + var oldest rateSample |
| 145 | + |
| 146 | + if t.sampleFull { |
| 147 | + oldest = t.samples[t.sampleIdx] // oldest sample in full ring |
| 148 | + } else { |
| 149 | + oldest = t.samples[0] // first sample |
| 150 | + } |
| 151 | + |
| 152 | + if oldest.ts.IsZero() { |
| 153 | + return 0 |
| 154 | + } |
| 155 | + |
| 156 | + elapsed := now.Sub(oldest.ts).Seconds() |
| 157 | + if elapsed < 1 { |
| 158 | + return 0 |
| 159 | + } |
| 160 | + |
| 161 | + newest := t.samples[(t.sampleIdx-1+rateSamples)%rateSamples] |
| 162 | + if newest.block <= oldest.block { |
| 163 | + return 0 |
| 164 | + } |
| 165 | + |
| 166 | + blocksPerSec := float64(newest.block-oldest.block) / elapsed |
| 167 | + return blocksPerSec * 60 // blocks per minute |
| 168 | +} |
0 commit comments