Skip to content

Commit

Permalink
update state recreation metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
magicxyyz committed Feb 21, 2024
1 parent 9790d80 commit cf61e26
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
28 changes: 8 additions & 20 deletions arbitrum/apibackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -49,8 +47,6 @@ type APIBackend struct {
fallbackClient types.FallbackClient
sync SyncProgressBackend

// TODO remove
liveStateFinalizers atomic.Int64
recreatedStateFinalizers atomic.Int64
}

Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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
}

Expand Down
11 changes: 10 additions & 1 deletion eth/state_accessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {})
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit cf61e26

Please sign in to comment.