-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Switch to lazy state balance cache #9822
Changes from 30 commits
594a853
628d2f3
f29d0d8
3c71522
5499b9f
aa9004b
5832ae4
ca992c9
151283d
95da658
9e9622d
e308933
b5f56b4
54768dc
748bfd3
067eba9
a18f5b6
868bc0e
a89e0f5
ff41c28
54607c3
6fbb6f6
3acc951
5451054
a4a00f2
63bf9f9
aededf8
4af1335
44eebee
fa5f07e
56e9445
bf8f9b6
75b6473
e4da535
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,6 +130,14 @@ var ( | |
Name: "sync_head_state_hit", | ||
Help: "The number of sync head state requests that are present in the cache.", | ||
}) | ||
stateBalanceCacheHit = promauto.NewCounter(prometheus.CounterOpts{ | ||
Name: "balance_cache_hit", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there names are too similar to the other metrics like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why dont define these metrics closer to its usages inside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm following the conventions of the blockchain package here, all the prom metric values are defined in
how about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for some reason i thought the cache resides in
|
||
Help: "Count the number of state balance cache hits.", | ||
}) | ||
stateBalanceCacheMiss = promauto.NewCounter(prometheus.CounterOpts{ | ||
Name: "balance_cache_miss", | ||
Help: "Count the number of state balance cache hits.", | ||
}) | ||
) | ||
|
||
// reportSlotMetrics reports slot related metrics. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package blockchain | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" | ||
"github.com/prysmaticlabs/prysm/beacon-chain/forkchoice/protoarray" | ||
"github.com/prysmaticlabs/prysm/beacon-chain/state" | ||
"github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" | ||
) | ||
|
||
func testServiceOptsWithDB(t *testing.T) []Option { | ||
beaconDB := testDB.SetupDB(t) | ||
fcs := protoarray.New(0, 0, [32]byte{'a'}) | ||
return []Option{ | ||
WithDatabase(beaconDB), | ||
WithStateGen(stategen.New(beaconDB)), | ||
WithForkChoiceStore(fcs), | ||
} | ||
} | ||
|
||
// warning: only use these opts when you are certain there are no db calls | ||
// in your code path. this is a lightweight way to satisfy the stategen/beacondb | ||
// initialization requirements w/o the overhead of db init. | ||
func testServiceOptsNoDB() []Option { | ||
return []Option{ | ||
withStateBalanceCache(satisfactoryStateBalanceCache()), | ||
} | ||
} | ||
|
||
type mockStateByRooter struct { | ||
state state.BeaconState | ||
err error | ||
} | ||
|
||
var _ stateByRooter = &mockStateByRooter{} | ||
|
||
func (m mockStateByRooter) StateByRoot(_ context.Context, _ [32]byte) (state.BeaconState, error) { | ||
return m.state, m.err | ||
} | ||
|
||
// returns an instance of the state balance cache that can be used | ||
// to satisfy the requirement for one in NewService, but which will | ||
// always return an error if used. | ||
func satisfactoryStateBalanceCache() *stateBalanceCache { | ||
err := errors.New("satisfactoryStateBalanceCache doesn't perform real caching") | ||
return &stateBalanceCache{stateGen: mockStateByRooter{err: err}} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this condition necessary in the new scheme?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when we have a cache miss and go back to stategen for the lookup, it will attempt to read the state from the db. If the root is equal to genesisRoot, I believe that we can assume it will be present in the database. correct? In other words I believe that get state by root using the genesis root is equivalent to calling db.GenesisState.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My instinct says this exists because for some reason at genesis both
justifiedRoot
ands.genesisRoot
can be0x000...
.To be safe, maybe we should apply
ensureRootNotZeros
toc.stateGen.StateByRoot(ctx, justifiedRoot)
inget
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot this detail --
stategen.StateByRoot
internally checks for the zero hash and in that case loads from the DB:https://github.com/prysmaticlabs/prysm/blob/develop/beacon-chain/state/stategen/getter.go#L45
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah perfect. Thanks!