Skip to content

Commit 9316c6e

Browse files
diag: race in updateIndexingStatus (#11274)
for #11268
1 parent 3f9d0d6 commit 9316c6e

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

erigon-lib/diagnostics/snapshots.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (d *DiagnosticClient) runSnapshotListener(rootCtx context.Context) {
7676
downloadTimeLeft := CalculateTime(remainingBytes, info.DownloadRate)
7777
totalDownloadTimeString := time.Duration(info.TotalTime) * time.Second
7878

79-
d.updateSnapshotStageStats(SyncStageStats{
79+
d.UpdateSnapshotStageStats(SyncStageStats{
8080
TimeElapsed: totalDownloadTimeString.String(),
8181
TimeLeft: downloadTimeLeft,
8282
Progress: downloadedPercent,
@@ -105,10 +105,14 @@ func GetShanpshotsPercentDownloaded(downloaded uint64, total uint64, torrentMeta
105105
return fmt.Sprintf("%.2f%%", percent)
106106
}
107107

108-
func (d *DiagnosticClient) updateSnapshotStageStats(stats SyncStageStats, subStageInfo string) {
108+
func (d *DiagnosticClient) UpdateSnapshotStageStats(stats SyncStageStats, subStageInfo string) {
109109
d.mu.Lock()
110110
defer d.mu.Unlock()
111-
idxs := d.GetCurrentSyncIdxs()
111+
d.updateSnapshotStageStats(stats, subStageInfo)
112+
}
113+
114+
func (d *DiagnosticClient) updateSnapshotStageStats(stats SyncStageStats, subStageInfo string) {
115+
idxs := d.getCurrentSyncIdxs()
112116
if idxs.Stage == -1 || idxs.SubStage == -1 {
113117
log.Debug("[Diagnostics] Can't find running stage or substage while updating Snapshots stage stats.", "stages:", d.syncStages, "stats:", stats, "subStageInfo:", subStageInfo)
114118
return
@@ -181,8 +185,8 @@ func (d *DiagnosticClient) runSegmentIndexingListener(rootCtx context.Context) {
181185
return
182186
case info := <-ch:
183187
d.addOrUpdateSegmentIndexingState(info)
184-
d.updateIndexingStatus()
185-
if d.syncStats.SnapshotIndexing.IndexingFinished {
188+
indexingFinished := d.UpdateIndexingStatus()
189+
if indexingFinished {
186190
d.SaveData()
187191
return
188192
}
@@ -222,20 +226,20 @@ func (d *DiagnosticClient) runSegmentIndexingFinishedListener(rootCtx context.Co
222226

223227
d.mu.Unlock()
224228

225-
d.updateIndexingStatus()
229+
d.UpdateIndexingStatus()
226230
}
227231
}
228232
}()
229233
}
230234

231-
func (d *DiagnosticClient) updateIndexingStatus() {
235+
func (d *DiagnosticClient) UpdateIndexingStatus() (indexingFinished bool) {
232236
totalProgressPercent := 0
233237
d.mu.Lock()
238+
defer d.mu.Unlock()
234239

235240
for _, seg := range d.syncStats.SnapshotIndexing.Segments {
236241
totalProgressPercent += seg.Percent
237242
}
238-
d.mu.Unlock()
239243

240244
totalProgress := totalProgressPercent / len(d.syncStats.SnapshotIndexing.Segments)
241245

@@ -248,6 +252,7 @@ func (d *DiagnosticClient) updateIndexingStatus() {
248252
if totalProgress >= 100 {
249253
d.syncStats.SnapshotIndexing.IndexingFinished = true
250254
}
255+
return d.syncStats.SnapshotIndexing.IndexingFinished
251256
}
252257

253258
func (d *DiagnosticClient) addOrUpdateSegmentIndexingState(upd SnapshotIndexingStatistics) {
@@ -399,7 +404,7 @@ func (d *DiagnosticClient) runFillDBListener(rootCtx context.Context) {
399404

400405
totalTimeString := time.Duration(info.TimeElapsed) * time.Second
401406

402-
d.updateSnapshotStageStats(SyncStageStats{
407+
d.UpdateSnapshotStageStats(SyncStageStats{
403408
TimeElapsed: totalTimeString.String(),
404409
TimeLeft: "unknown",
405410
Progress: fmt.Sprintf("%d%%", (info.Stage.Current*100)/info.Stage.Total),

erigon-lib/diagnostics/stages.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ func (d *DiagnosticClient) runSubStageListener(rootCtx context.Context) {
177177
}
178178

179179
func (d *DiagnosticClient) GetCurrentSyncIdxs() CurrentSyncStagesIdxs {
180+
d.mu.Lock()
181+
defer d.mu.Unlock()
182+
return d.getCurrentSyncIdxs()
183+
}
184+
185+
func (d *DiagnosticClient) getCurrentSyncIdxs() CurrentSyncStagesIdxs {
180186
currentIdxs := CurrentSyncStagesIdxs{
181187
Stage: -1,
182188
SubStage: -1,

0 commit comments

Comments
 (0)