Skip to content

Commit

Permalink
Diagnostics: fix unreasonable warns on start (#11046)
Browse files Browse the repository at this point in the history
fix - #11035
  • Loading branch information
dvovk authored Jul 5, 2024
1 parent a8d6387 commit 0985808
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 88 deletions.
22 changes: 15 additions & 7 deletions erigon-lib/diagnostics/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,23 @@ func ReadSavedData(db kv.RoDB) (hinfo HardwareInfo, ssinfo []SyncStage, snpdwl S
return HardwareInfo{}, []SyncStage{}, SnapshotDownloadStatistics{}, SnapshotIndexingStatistics{}, SnapshotFillDBStatistics{}
}

var ramInfo RAMInfo
var cpuInfo CPUInfo
var diskInfo DiskInfo
ParseData(ramBytes, &ramInfo)
ParseData(cpuBytes, &cpuInfo)
ParseData(diskBytes, &diskInfo)

hinfo = HardwareInfo{
RAM: ParseRamInfo(ramBytes),
CPU: ParseCPUInfo(cpuBytes),
Disk: ParseDiskInfo(diskBytes),
RAM: ramInfo,
CPU: cpuInfo,
Disk: diskInfo,
}
ssinfo = ParseStagesList(ssinfoData)
snpdwl = ParseSnapshotDownloadInfo(snpdwlData)
snpidx = ParseSnapshotIndexingInfo(snpidxData)
snpfd = ParseSnapshotFillDBInfo(snpfdData)

ParseData(ssinfoData, &ssinfo)
ParseData(snpdwlData, &snpdwl)
ParseData(snpidxData, &snpidx)
ParseData(snpfdData, &snpfd)

return hinfo, ssinfo, snpdwl, snpidx, snpfd
}
34 changes: 0 additions & 34 deletions erigon-lib/diagnostics/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package diagnostics

import (
"context"
"encoding/json"
"fmt"
"time"

Expand Down Expand Up @@ -462,17 +461,6 @@ func SnapshotDownloadInfoFromTx(tx kv.Tx) ([]byte, error) {
return common.CopyBytes(bytes), nil
}

func ParseSnapshotDownloadInfo(data []byte) (info SnapshotDownloadStatistics) {
err := json.Unmarshal(data, &info)

if err != nil {
log.Warn("[Diagnostics] Failed to parse snapshot download info", "err", err)
return SnapshotDownloadStatistics{}
} else {
return info
}
}

func SnapshotIndexingInfoFromTx(tx kv.Tx) ([]byte, error) {
bytes, err := ReadDataFromTable(tx, kv.DiagSyncStages, SnapshotIndexingStatisticsKey)
if err != nil {
Expand All @@ -482,17 +470,6 @@ func SnapshotIndexingInfoFromTx(tx kv.Tx) ([]byte, error) {
return common.CopyBytes(bytes), nil
}

func ParseSnapshotIndexingInfo(data []byte) (info SnapshotIndexingStatistics) {
err := json.Unmarshal(data, &info)

if err != nil {
log.Warn("[Diagnostics] Failed to parse snapshot indexing info", "err", err)
return SnapshotIndexingStatistics{}
} else {
return info
}
}

func SnapshotFillDBInfoFromTx(tx kv.Tx) ([]byte, error) {
bytes, err := ReadDataFromTable(tx, kv.DiagSyncStages, SnapshotFillDBStatisticsKey)
if err != nil {
Expand All @@ -502,17 +479,6 @@ func SnapshotFillDBInfoFromTx(tx kv.Tx) ([]byte, error) {
return common.CopyBytes(bytes), nil
}

func ParseSnapshotFillDBInfo(data []byte) (info SnapshotFillDBStatistics) {
err := json.Unmarshal(data, &info)

if err != nil {
log.Warn("[Diagnostics] Failed to parse snapshot fill db info", "err", err)
return SnapshotFillDBStatistics{}
} else {
return info
}
}

func SnapshotDownloadUpdater(info SnapshotDownloadStatistics) func(tx kv.RwTx) error {
return PutDataToTable(kv.DiagSyncStages, SnapshotDownloadStatisticsKey, info)
}
Expand Down
12 changes: 0 additions & 12 deletions erigon-lib/diagnostics/stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package diagnostics

import (
"context"
"encoding/json"
"fmt"

"github.com/ledgerwatch/erigon-lib/common"
Expand Down Expand Up @@ -325,17 +324,6 @@ func SyncStagesFromTX(tx kv.Tx) ([]byte, error) {
return common.CopyBytes(bytes), nil
}

func ParseStagesList(data []byte) (info []SyncStage) {
err := json.Unmarshal(data, &info)

if err != nil {
log.Warn("[Diagnostics] Failed to parse stages list", "err", err)
return []SyncStage{}
} else {
return info
}
}

func StagesListUpdater(info []SyncStage) func(tx kv.RwTx) error {
return PutDataToTable(kv.DiagSyncStages, StagesListKey, info)
}
Expand Down
35 changes: 0 additions & 35 deletions erigon-lib/diagnostics/sys_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package diagnostics

import (
"encoding/json"

"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/mem"
Expand Down Expand Up @@ -155,17 +153,6 @@ func ReadRAMInfoFromTx(tx kv.Tx) ([]byte, error) {
return common.CopyBytes(bytes), nil
}

func ParseRamInfo(data []byte) (info RAMInfo) {
err := json.Unmarshal(data, &info)

if err != nil {
log.Warn("[Diagnostics] Failed to parse RAM info", "err", err)
return RAMInfo{}
} else {
return info
}
}

func ReadCPUInfoFromTx(tx kv.Tx) ([]byte, error) {
bytes, err := ReadDataFromTable(tx, kv.DiagSystemInfo, SystemCpuInfoKey)
if err != nil {
Expand All @@ -175,17 +162,6 @@ func ReadCPUInfoFromTx(tx kv.Tx) ([]byte, error) {
return common.CopyBytes(bytes), nil
}

func ParseCPUInfo(data []byte) (info CPUInfo) {
err := json.Unmarshal(data, &info)

if err != nil {
log.Warn("[Diagnostics] Failed to parse CPU info", "err", err)
return CPUInfo{}
} else {
return info
}
}

func ReadDiskInfoFromTx(tx kv.Tx) ([]byte, error) {
bytes, err := ReadDataFromTable(tx, kv.DiagSystemInfo, SystemDiskInfoKey)
if err != nil {
Expand All @@ -195,17 +171,6 @@ func ReadDiskInfoFromTx(tx kv.Tx) ([]byte, error) {
return common.CopyBytes(bytes), nil
}

func ParseDiskInfo(data []byte) (info DiskInfo) {
err := json.Unmarshal(data, &info)

if err != nil {
log.Warn("[Diagnostics] Failed to parse Disk info", "err", err)
return DiskInfo{}
} else {
return info
}
}

func RAMInfoUpdater(info RAMInfo) func(tx kv.RwTx) error {
return PutDataToTable(kv.DiagSystemInfo, SystemRamInfoKey, info)
}
Expand Down
13 changes: 13 additions & 0 deletions erigon-lib/diagnostics/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package diagnostics
import (
"encoding/json"
"fmt"
"reflect"

"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/log/v3"
)

func ReadDataFromTable(tx kv.Tx, table string, key []byte) ([]byte, error) {
Expand Down Expand Up @@ -97,3 +99,14 @@ func SecondsToHHMMString(seconds uint64) string {

return fmt.Sprintf("%dhrs:%dm", hours, minutes)
}

func ParseData(data []byte, v interface{}) {
if len(data) == 0 {
return
}

err := json.Unmarshal(data, &v)
if err != nil {
log.Warn("[Diagnostics] Failed to parse data", "data", string(data), "type", reflect.TypeOf(v))
}
}
27 changes: 27 additions & 0 deletions erigon-lib/diagnostics/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package diagnostics_test

import (
"encoding/json"
"testing"

"github.com/ledgerwatch/erigon-lib/diagnostics"
"github.com/stretchr/testify/require"
)

func TestParseData(t *testing.T) {
var data []byte
var v diagnostics.RAMInfo
diagnostics.ParseData(data, v)
require.Equal(t, diagnostics.RAMInfo{}, v)

newv := diagnostics.RAMInfo{
Total: 1,
Free: 2,
}

data, err := json.Marshal(newv)
require.NoError(t, err)

diagnostics.ParseData(data, &v)
require.Equal(t, newv, v)
}

0 comments on commit 0985808

Please sign in to comment.