2023-11-07 12:50:36 +00:00
|
|
|
package diagnostics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/ledgerwatch/erigon-lib/common"
|
|
|
|
diaglib "github.com/ledgerwatch/erigon-lib/diagnostics"
|
|
|
|
"github.com/ledgerwatch/erigon/turbo/node"
|
|
|
|
"github.com/ledgerwatch/log/v3"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DiagnosticClient struct {
|
|
|
|
ctx *cli.Context
|
|
|
|
metricsMux *http.ServeMux
|
|
|
|
node *node.ErigonNode
|
|
|
|
|
2024-01-08 09:43:04 +00:00
|
|
|
syncStats diaglib.SyncStatistics
|
2023-11-07 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDiagnosticClient(ctx *cli.Context, metricsMux *http.ServeMux, node *node.ErigonNode) *DiagnosticClient {
|
2024-01-08 09:43:04 +00:00
|
|
|
return &DiagnosticClient{ctx: ctx, metricsMux: metricsMux, node: node, syncStats: diaglib.SyncStatistics{}}
|
2023-11-07 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DiagnosticClient) Setup() {
|
|
|
|
d.runSnapshotListener()
|
2023-12-15 00:23:26 +00:00
|
|
|
d.runSegmentDownloadingListener()
|
|
|
|
d.runSegmentIndexingListener()
|
2023-12-22 11:25:55 +00:00
|
|
|
d.runSegmentIndexingFinishedListener()
|
2024-01-08 09:43:04 +00:00
|
|
|
d.runCurrentSyncStageListener()
|
|
|
|
d.runSyncStagesListListener()
|
2023-11-07 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DiagnosticClient) runSnapshotListener() {
|
|
|
|
go func() {
|
2023-12-08 14:07:59 +00:00
|
|
|
ctx, ch, cancel := diaglib.Context[diaglib.SnapshotDownloadStatistics](context.Background(), 1)
|
2023-11-08 14:02:30 +00:00
|
|
|
defer cancel()
|
2023-11-07 12:50:36 +00:00
|
|
|
|
|
|
|
rootCtx, _ := common.RootContext()
|
|
|
|
|
2023-12-08 14:07:59 +00:00
|
|
|
diaglib.StartProviders(ctx, diaglib.TypeOf(diaglib.SnapshotDownloadStatistics{}), log.Root())
|
2023-11-07 12:50:36 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-rootCtx.Done():
|
2023-11-08 14:02:30 +00:00
|
|
|
cancel()
|
2023-11-07 12:50:36 +00:00
|
|
|
return
|
|
|
|
case info := <-ch:
|
2024-01-08 09:43:04 +00:00
|
|
|
d.syncStats.SnapshotDownload.Downloaded = info.Downloaded
|
|
|
|
d.syncStats.SnapshotDownload.Total = info.Total
|
|
|
|
d.syncStats.SnapshotDownload.TotalTime = info.TotalTime
|
|
|
|
d.syncStats.SnapshotDownload.DownloadRate = info.DownloadRate
|
|
|
|
d.syncStats.SnapshotDownload.UploadRate = info.UploadRate
|
|
|
|
d.syncStats.SnapshotDownload.Peers = info.Peers
|
|
|
|
d.syncStats.SnapshotDownload.Files = info.Files
|
|
|
|
d.syncStats.SnapshotDownload.Connections = info.Connections
|
|
|
|
d.syncStats.SnapshotDownload.Alloc = info.Alloc
|
|
|
|
d.syncStats.SnapshotDownload.Sys = info.Sys
|
|
|
|
d.syncStats.SnapshotDownload.DownloadFinished = info.DownloadFinished
|
|
|
|
d.syncStats.SnapshotDownload.TorrentMetadataReady = info.TorrentMetadataReady
|
2023-12-08 14:07:59 +00:00
|
|
|
|
2023-11-07 12:50:36 +00:00
|
|
|
if info.DownloadFinished {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2024-01-08 09:43:04 +00:00
|
|
|
func (d *DiagnosticClient) SyncStatistics() diaglib.SyncStatistics {
|
|
|
|
return d.syncStats
|
2023-11-07 12:50:36 +00:00
|
|
|
}
|
2023-12-08 11:08:38 +00:00
|
|
|
|
2023-12-15 00:23:26 +00:00
|
|
|
func (d *DiagnosticClient) runSegmentDownloadingListener() {
|
2023-12-08 11:08:38 +00:00
|
|
|
go func() {
|
2023-12-08 14:07:59 +00:00
|
|
|
ctx, ch, cancel := diaglib.Context[diaglib.SegmentDownloadStatistics](context.Background(), 1)
|
2023-12-08 11:08:38 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
rootCtx, _ := common.RootContext()
|
|
|
|
|
2023-12-08 14:07:59 +00:00
|
|
|
diaglib.StartProviders(ctx, diaglib.TypeOf(diaglib.SegmentDownloadStatistics{}), log.Root())
|
2023-12-08 11:08:38 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-rootCtx.Done():
|
|
|
|
cancel()
|
|
|
|
return
|
|
|
|
case info := <-ch:
|
2024-01-08 09:43:04 +00:00
|
|
|
if d.syncStats.SnapshotDownload.SegmentsDownloading == nil {
|
|
|
|
d.syncStats.SnapshotDownload.SegmentsDownloading = map[string]diaglib.SegmentDownloadStatistics{}
|
2023-12-08 14:07:59 +00:00
|
|
|
}
|
|
|
|
|
2024-01-08 09:43:04 +00:00
|
|
|
d.syncStats.SnapshotDownload.SegmentsDownloading[info.Name] = info
|
2023-12-15 00:23:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DiagnosticClient) runSegmentIndexingListener() {
|
|
|
|
go func() {
|
|
|
|
ctx, ch, cancel := diaglib.Context[diaglib.SnapshotIndexingStatistics](context.Background(), 1)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
rootCtx, _ := common.RootContext()
|
|
|
|
|
|
|
|
diaglib.StartProviders(ctx, diaglib.TypeOf(diaglib.SnapshotIndexingStatistics{}), log.Root())
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-rootCtx.Done():
|
|
|
|
cancel()
|
|
|
|
return
|
|
|
|
case info := <-ch:
|
2023-12-22 11:25:55 +00:00
|
|
|
d.addOrUpdateSegmentIndexingState(info)
|
2023-12-08 11:08:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2023-12-22 11:25:55 +00:00
|
|
|
|
|
|
|
func (d *DiagnosticClient) runSegmentIndexingFinishedListener() {
|
|
|
|
go func() {
|
|
|
|
ctx, ch, cancel := diaglib.Context[diaglib.SnapshotSegmentIndexingFinishedUpdate](context.Background(), 1)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
rootCtx, _ := common.RootContext()
|
|
|
|
|
|
|
|
diaglib.StartProviders(ctx, diaglib.TypeOf(diaglib.SnapshotSegmentIndexingFinishedUpdate{}), log.Root())
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-rootCtx.Done():
|
|
|
|
cancel()
|
|
|
|
return
|
|
|
|
case info := <-ch:
|
|
|
|
found := false
|
2024-01-08 09:43:04 +00:00
|
|
|
for i := range d.syncStats.SnapshotIndexing.Segments {
|
|
|
|
if d.syncStats.SnapshotIndexing.Segments[i].SegmentName == info.SegmentName {
|
2023-12-22 11:25:55 +00:00
|
|
|
found = true
|
2024-01-08 09:43:04 +00:00
|
|
|
d.syncStats.SnapshotIndexing.Segments[i].Percent = 100
|
2023-12-22 11:25:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
2024-01-08 09:43:04 +00:00
|
|
|
d.syncStats.SnapshotIndexing.Segments = append(d.syncStats.SnapshotIndexing.Segments, diaglib.SnapshotSegmentIndexingStatistics{
|
2023-12-22 11:25:55 +00:00
|
|
|
SegmentName: info.SegmentName,
|
|
|
|
Percent: 100,
|
|
|
|
Alloc: 0,
|
|
|
|
Sys: 0,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DiagnosticClient) addOrUpdateSegmentIndexingState(upd diaglib.SnapshotIndexingStatistics) {
|
2024-01-08 09:43:04 +00:00
|
|
|
if d.syncStats.SnapshotIndexing.Segments == nil {
|
|
|
|
d.syncStats.SnapshotIndexing.Segments = []diaglib.SnapshotSegmentIndexingStatistics{}
|
2023-12-22 11:25:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range upd.Segments {
|
|
|
|
found := false
|
2024-01-08 09:43:04 +00:00
|
|
|
for j := range d.syncStats.SnapshotIndexing.Segments {
|
|
|
|
if d.syncStats.SnapshotIndexing.Segments[j].SegmentName == upd.Segments[i].SegmentName {
|
|
|
|
d.syncStats.SnapshotIndexing.Segments[j].Percent = upd.Segments[i].Percent
|
|
|
|
d.syncStats.SnapshotIndexing.Segments[j].Alloc = upd.Segments[i].Alloc
|
|
|
|
d.syncStats.SnapshotIndexing.Segments[j].Sys = upd.Segments[i].Sys
|
2023-12-22 11:25:55 +00:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
2024-01-08 09:43:04 +00:00
|
|
|
d.syncStats.SnapshotIndexing.Segments = append(d.syncStats.SnapshotIndexing.Segments, upd.Segments[i])
|
2023-12-22 11:25:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-08 09:43:04 +00:00
|
|
|
d.syncStats.SnapshotIndexing.TimeElapsed = upd.TimeElapsed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DiagnosticClient) runSyncStagesListListener() {
|
|
|
|
go func() {
|
|
|
|
ctx, ch, cancel := diaglib.Context[diaglib.SyncStagesList](context.Background(), 1)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
rootCtx, _ := common.RootContext()
|
|
|
|
|
|
|
|
diaglib.StartProviders(ctx, diaglib.TypeOf(diaglib.SyncStagesList{}), log.Root())
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-rootCtx.Done():
|
|
|
|
cancel()
|
|
|
|
return
|
|
|
|
case info := <-ch:
|
|
|
|
d.syncStats.SyncStages.StagesList = info.Stages
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DiagnosticClient) runCurrentSyncStageListener() {
|
|
|
|
go func() {
|
|
|
|
ctx, ch, cancel := diaglib.Context[diaglib.CurrentSyncStage](context.Background(), 1)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
rootCtx, _ := common.RootContext()
|
|
|
|
|
|
|
|
diaglib.StartProviders(ctx, diaglib.TypeOf(diaglib.CurrentSyncStage{}), log.Root())
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-rootCtx.Done():
|
|
|
|
cancel()
|
|
|
|
return
|
|
|
|
case info := <-ch:
|
|
|
|
d.syncStats.SyncStages.CurrentStage = info.Stage
|
|
|
|
if int(d.syncStats.SyncStages.CurrentStage) >= len(d.syncStats.SyncStages.StagesList) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2023-12-22 11:25:55 +00:00
|
|
|
}
|