From cf61e26fb7c6b81eb8a1782543ecec489b498faa Mon Sep 17 00:00:00 2001 From: Maciej Kulawik Date: Wed, 21 Feb 2024 12:48:18 +0000 Subject: [PATCH] update state recreation metrics --- arbitrum/apibackend.go | 28 ++++++++-------------------- eth/state_accessor.go | 11 ++++++++++- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/arbitrum/apibackend.go b/arbitrum/apibackend.go index 0a329da2cf..8d74000e4f 100644 --- a/arbitrum/apibackend.go +++ b/arbitrum/apibackend.go @@ -37,10 +37,8 @@ import ( ) var ( - referencedLiveStatesCounter = metrics.NewRegisteredCounter("arb/apibackend/states/live/referenced", nil) - releasedLiveStatesCounter = metrics.NewRegisteredCounter("arb/apibackend/states/live/released", nil) - referencedEphemermalStatesCounter = metrics.NewRegisteredCounter("arb/apibackend/states/ephemeral/referenced", nil) - releasedEphemermalStatesCounter = metrics.NewRegisteredCounter("arb/apibackend/states/ephemeral/released", nil) + liveStatesCounter = metrics.NewRegisteredCounter("arb/apibackend/states/live", nil) + recreatedStatesCounter = metrics.NewRegisteredCounter("arb/apibackend/states/recreated", nil) ) type APIBackend struct { @@ -49,8 +47,6 @@ type APIBackend struct { fallbackClient types.FallbackClient sync SyncProgressBackend - // TODO remove - liveStateFinalizers atomic.Int64 recreatedStateFinalizers atomic.Int64 } @@ -479,14 +475,10 @@ func (a *APIBackend) stateAndHeaderFromHeader(ctx context.Context, header *types } liveState, liveStateRelease, err := stateFor(bc.StateCache(), bc.Snapshots())(header) if err == nil { - referencedLiveStatesCounter.Inc(1) + liveStatesCounter.Inc(1) liveState.SetRelease(func() { - // TODO remove logs and counters - log.Debug("Live state release called", "recreatedStateFinalizers", a.recreatedStateFinalizers.Load(), "liveStateFinalizers", a.liveStateFinalizers.Load()) liveStateRelease() - releasedLiveStatesCounter.Inc(1) }) - log.Debug("Live state release set", "recreatedStateFinalizers", a.recreatedStateFinalizers.Load(), "liveStateFinalizers", a.liveStateFinalizers.Load()) return liveState, header, nil } // else err != nil => we don't need to call liveStateRelease @@ -502,14 +494,10 @@ func (a *APIBackend) stateAndHeaderFromHeader(ctx context.Context, header *types } // make sure that we haven't found the state in diskdb if lastHeader == header { - referencedEphemermalStatesCounter.Inc(1) + liveStatesCounter.Inc(1) lastState.SetRelease(func() { - // TODO remove logs and counters - log.Debug("Live state release called", "recreatedStateFinalizers", a.recreatedStateFinalizers.Load(), "liveStateFinalizers", a.liveStateFinalizers.Load()) lastStateRelease() - releasedEphemermalStatesCounter.Inc(1) }) - log.Debug("Live state release set", "recreatedStateFinalizers", a.recreatedStateFinalizers.Load(), "liveStateFinalizers", a.liveStateFinalizers.Load()) return lastState, header, nil } defer lastStateRelease() @@ -529,15 +517,15 @@ func (a *APIBackend) stateAndHeaderFromHeader(ctx context.Context, header *types return nil, nil, fmt.Errorf("failed to recreate state: %w", err) } // we are setting finalizer instead of returning a StateReleaseFunc to avoid changing ethapi.Backend interface to minimize diff to upstream - referencedEphemermalStatesCounter.Inc(1) + recreatedStatesCounter.Inc(1) + a.recreatedStateFinalizers.Add(1) statedb.SetRelease(func() { // TODO remove logs and counters a.recreatedStateFinalizers.Add(-1) - log.Debug("Recreated state release called", "recreatedStateFinalizers", a.recreatedStateFinalizers.Load(), "liveStateFinalizers", a.liveStateFinalizers.Load()) + log.Warn("Recreated state release called", "recreatedStateFinalizers", a.recreatedStateFinalizers.Load()) release() - releasedEphemermalStatesCounter.Inc(1) }) - log.Debug("Recreated state release set", "recreatedStateFinalizers", a.recreatedStateFinalizers.Load(), "liveStateFinalizers", a.liveStateFinalizers.Load()) + log.Warn("Recreated state release set", "recreatedStateFinalizers", a.recreatedStateFinalizers.Load()) return statedb, header, err } diff --git a/eth/state_accessor.go b/eth/state_accessor.go index b96a585ffd..439d88c2d8 100644 --- a/eth/state_accessor.go +++ b/eth/state_accessor.go @@ -30,9 +30,15 @@ import ( "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/eth/tracers" "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/trie" ) +var ( + recreatedStatesCounter = metrics.NewRegisteredCounter("eth/stateaccessor/recreated/states", nil) + recreatedBytesMeter = metrics.NewRegisteredMeter("eth/stateaccessor/recreated/bytes", nil) +) + // noopReleaser is returned in case there is no operation expected // for releasing state. var noopReleaser = tracers.StateReleaseFunc(func() {}) @@ -171,10 +177,13 @@ func (eth *Ethereum) hashState(ctx context.Context, block *types.Block, reexec u } parent = root } + _, nodes, imgs := triedb.Size() // all memory is contained within the nodes return in hashdb if report { - _, nodes, imgs := triedb.Size() // all memory is contained within the nodes return in hashdb log.Info("Historical state regenerated", "block", current.NumberU64(), "elapsed", time.Since(start), "nodes", nodes, "preimages", imgs) } + recreatedStatesCounter.Inc(1) + recreatedBytesMeter.Mark(int64(nodes)) + return statedb, func() { triedb.Dereference(block.Root()) }, nil }