From 5bfeed5a22364c4a8ae38cf68497cbd9045f764b Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 14 Feb 2023 22:24:24 +1000 Subject: [PATCH 1/2] single measureDbDir() process --- cmd/opera/launcher/metrics/metrics.go | 29 +++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/cmd/opera/launcher/metrics/metrics.go b/cmd/opera/launcher/metrics/metrics.go index 6249b4954..067cca71d 100644 --- a/cmd/opera/launcher/metrics/metrics.go +++ b/cmd/opera/launcher/metrics/metrics.go @@ -4,27 +4,40 @@ import ( "os" "path/filepath" "sync/atomic" + "time" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/metrics" ) var ( - // TODO: refactor it dbDir atomic.Value - dbSizeMetric = metrics.NewRegisteredFunctionalGauge("db_size", nil, measureDbDir) + dbSize atomic.Int64 + dbSizeMetric = metrics.NewRegisteredFunctionalGauge("db_size", nil, func() int64 { + return dbSize.Load() + }) ) func SetDataDir(datadir string) { - dbDir.Store(datadir) + was := dbDir.Swap(datadir) + if was != nil { + panic("SetDataDir() only once!") + } + go measureDbDir() } -func measureDbDir() (size int64) { - datadir, ok := dbDir.Load().(string) - if !ok || datadir == "" || datadir == "inmemory" { - return +func measureDbDir() { + for { + time.Sleep(time.Second) + + datadir, ok := dbDir.Load().(string) + if !ok || len(datadir) == 0 || datadir == "inmemory" { + dbSize.Store(0) + } else { + size := sizeOfDir(datadir) + dbSize.Store(size) + } } - return sizeOfDir(datadir) } func sizeOfDir(dir string) (size int64) { From cb20db791bb28733fa4e00a6c2280356593f8f66 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 21 Feb 2023 20:10:18 +1000 Subject: [PATCH 2/2] don't show zero 'db_size' on start --- cmd/opera/launcher/metrics/metrics.go | 39 +++++++++++++++------------ demo/start.sh | 1 + 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/cmd/opera/launcher/metrics/metrics.go b/cmd/opera/launcher/metrics/metrics.go index 067cca71d..bab964d68 100644 --- a/cmd/opera/launcher/metrics/metrics.go +++ b/cmd/opera/launcher/metrics/metrics.go @@ -3,6 +3,7 @@ package metrics import ( "os" "path/filepath" + "sync" "sync/atomic" "time" @@ -10,33 +11,37 @@ import ( "github.com/ethereum/go-ethereum/metrics" ) -var ( - dbDir atomic.Value - dbSize atomic.Int64 - dbSizeMetric = metrics.NewRegisteredFunctionalGauge("db_size", nil, func() int64 { - return dbSize.Load() - }) -) +var once sync.Once func SetDataDir(datadir string) { - was := dbDir.Swap(datadir) - if was != nil { - panic("SetDataDir() only once!") - } - go measureDbDir() + once.Do(func() { + go measureDbDir("db_size", datadir) + }) } -func measureDbDir() { +func measureDbDir(name, datadir string) { + var ( + dbSize atomic.Int64 + gauge metrics.Gauge + rescan = (len(datadir) > 0 && datadir != "inmemory") + ) for { time.Sleep(time.Second) - datadir, ok := dbDir.Load().(string) - if !ok || len(datadir) == 0 || datadir == "inmemory" { - dbSize.Store(0) - } else { + if rescan { size := sizeOfDir(datadir) dbSize.Store(size) } + + if gauge == nil { + gauge = metrics.NewRegisteredFunctionalGauge(name, nil, func() int64 { + return dbSize.Load() + }) + } + + if !rescan { + break + } } } diff --git a/demo/start.sh b/demo/start.sh index 5ecc72bcc..763c6a529 100755 --- a/demo/start.sh +++ b/demo/start.sh @@ -25,6 +25,7 @@ do --nat extip:127.0.0.1 \ --http --http.addr="127.0.0.1" --http.port=${RPCP} --http.corsdomain="*" --http.api="eth,debug,net,admin,web3,personal,txpool,ftm,dag" \ --ws --ws.addr="127.0.0.1" --ws.port=${WSP} --ws.origins="*" --ws.api="eth,debug,net,admin,web3,personal,txpool,ftm,dag" \ + --metrics --metrics.addr=127.0.0.1 --metrics.port=$(($RPCP+1100)) \ --verbosity=3 --tracing >> opera$i.log 2>&1)& echo -e "\tnode$i ok"