-
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
Cache Active Validator Indices, Count, and Balances #2737
Merged
Merged
Changes from 35 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
130023e
benchmark process epoch
terencechain 7950dcf
revert prof.out
terencechain 3cb25ed
Add some optimizations
prestonvanloon 9a2b1ab
beware where we use ActiveValidatorIndices...
terencechain 7813441
revert extra file
terencechain f36b777
gaz
terencechain 63a769f
quick commit to get feedback
terencechain 079ffc5
revert extra file
terencechain 0383d4d
started fixing tests
terencechain 7e8ed6f
fixed broken TestProcessCrosslink_NoUpdate
terencechain 804c0a1
Merge branch 'spec-v0.6' of https://github.com/prysmaticlabs/prysm in…
terencechain 0192b9b
gaz
terencechain 4522f77
cache randao seed
terencechain 2e8bc92
fixed all the tests
terencechain 0190159
fmt and lint
terencechain 8a1aa71
Merge branch 'spec-v0.6' of https://github.com/prysmaticlabs/prysm in…
terencechain 2a65c55
spacing
terencechain a7b979f
Added todo
terencechain 9f88933
lint
terencechain 382077f
revert binary file
terencechain 904f251
started regression test
terencechain ff0daf8
basic tests done
terencechain f8e0cfc
using a fifo for active indices cache
terencechain 3902def
using a fifo for active count cache
terencechain bb29a22
using a fifo for total balance cache
terencechain dcbd1bc
using a fifo for active balance cache
terencechain 49f0b77
using a fifo for start shard cache
terencechain 3962f37
using a fifo for seed cache
terencechain 473f83e
gaz
terencechain a2b0b9e
clean up
terencechain ac37806
fixing tests
terencechain 351cd6c
fixed all the core tests
terencechain e3172b8
fixed all the tests!!!
terencechain 4c1306d
lint
terencechain d844766
comment
terencechain de858e4
rm'ed commented code
terencechain b714635
cache size to 1000 should be good enough
terencechain 30231b7
better metrics namings
terencechain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package cache | ||
|
||
import ( | ||
"errors" | ||
"strconv" | ||
"sync" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
"github.com/prysmaticlabs/prysm/shared/params" | ||
"k8s.io/client-go/tools/cache" | ||
) | ||
|
||
var ( | ||
// ErrNotActiveBalanceInfo will be returned when a cache object is not a pointer to | ||
// a ActiveBalanceByEpoch struct. | ||
ErrNotActiveBalanceInfo = errors.New("object is not a active balance obj") | ||
|
||
// maxActiveBalanceListSize defines the max number of active balance can cache. | ||
maxActiveBalanceListSize = int(params.BeaconConfig().LatestRandaoMixesLength) | ||
|
||
// Metrics. | ||
activeBalanceCacheMiss = promauto.NewCounter(prometheus.CounterOpts{ | ||
Name: "active_balance_cache_miss", | ||
Help: "The number of active balance requests that aren't present in the cache.", | ||
}) | ||
activeBalanceCacheHit = promauto.NewCounter(prometheus.CounterOpts{ | ||
Name: "active_balance_cache_hit", | ||
Help: "The number of active balance requests that are present in the cache.", | ||
}) | ||
) | ||
|
||
// ActiveBalanceByEpoch defines the active validator balance per epoch. | ||
type ActiveBalanceByEpoch struct { | ||
Epoch uint64 | ||
ActiveBalance uint64 | ||
} | ||
|
||
// ActiveBalanceCache is a struct with 1 queue for looking up active balance by epoch. | ||
type ActiveBalanceCache struct { | ||
activeBalanceCache *cache.FIFO | ||
lock sync.RWMutex | ||
} | ||
|
||
// activeBalanceKeyFn takes the epoch as the key for the active balance of a given epoch. | ||
func activeBalanceKeyFn(obj interface{}) (string, error) { | ||
tInfo, ok := obj.(*ActiveBalanceByEpoch) | ||
if !ok { | ||
return "", ErrNotActiveBalanceInfo | ||
} | ||
|
||
return strconv.Itoa(int(tInfo.Epoch)), nil | ||
} | ||
|
||
// NewActiveBalanceCache creates a new active balance cache for storing/accessing active validator balance. | ||
func NewActiveBalanceCache() *ActiveBalanceCache { | ||
return &ActiveBalanceCache{ | ||
activeBalanceCache: cache.NewFIFO(activeBalanceKeyFn), | ||
} | ||
} | ||
|
||
// ActiveBalanceInEpoch fetches ActiveBalanceByEpoch by epoch. Returns true with a | ||
// reference to the ActiveBalanceInEpoch info, if exists. Otherwise returns false, nil. | ||
func (c *ActiveBalanceCache) ActiveBalanceInEpoch(epoch uint64) (uint64, error) { | ||
c.lock.RLock() | ||
defer c.lock.RUnlock() | ||
obj, exists, err := c.activeBalanceCache.GetByKey(strconv.Itoa(int(epoch))) | ||
if err != nil { | ||
return params.BeaconConfig().FarFutureEpoch, err | ||
} | ||
|
||
if exists { | ||
activeBalanceCacheHit.Inc() | ||
} else { | ||
activeBalanceCacheMiss.Inc() | ||
return params.BeaconConfig().FarFutureEpoch, nil | ||
} | ||
|
||
tInfo, ok := obj.(*ActiveBalanceByEpoch) | ||
if !ok { | ||
return params.BeaconConfig().FarFutureEpoch, ErrNotActiveBalanceInfo | ||
} | ||
|
||
return tInfo.ActiveBalance, nil | ||
} | ||
|
||
// AddActiveBalance adds ActiveBalanceByEpoch object to the cache. This method also trims the least | ||
// recently added ActiveBalanceByEpoch object if the cache size has ready the max cache size limit. | ||
func (c *ActiveBalanceCache) AddActiveBalance(activeBalance *ActiveBalanceByEpoch) error { | ||
c.lock.Lock() | ||
defer c.lock.Unlock() | ||
if err := c.activeBalanceCache.AddIfNotPresent(activeBalance); err != nil { | ||
return err | ||
} | ||
|
||
trim(c.activeBalanceCache, maxActiveBalanceListSize) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package cache | ||
|
||
import ( | ||
"reflect" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/prysmaticlabs/prysm/shared/params" | ||
) | ||
|
||
func TestActiveBalanceKeyFn_OK(t *testing.T) { | ||
tInfo := &ActiveBalanceByEpoch{ | ||
Epoch: 45, | ||
ActiveBalance: 7456, | ||
} | ||
|
||
key, err := activeBalanceKeyFn(tInfo) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if key != strconv.Itoa(int(tInfo.Epoch)) { | ||
t.Errorf("Incorrect hash key: %s, expected %s", key, strconv.Itoa(int(tInfo.Epoch))) | ||
} | ||
} | ||
|
||
func TestActiveBalanceKeyFn_InvalidObj(t *testing.T) { | ||
_, err := activeBalanceKeyFn("bad") | ||
if err != ErrNotActiveBalanceInfo { | ||
t.Errorf("Expected error %v, got %v", ErrNotActiveBalanceInfo, err) | ||
} | ||
} | ||
|
||
func TestActiveBalanceCache_ActiveBalanceByEpoch(t *testing.T) { | ||
cache := NewActiveBalanceCache() | ||
|
||
tInfo := &ActiveBalanceByEpoch{ | ||
Epoch: 16511, | ||
ActiveBalance: 4456547, | ||
} | ||
activeBalance, err := cache.ActiveBalanceInEpoch(tInfo.Epoch) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if activeBalance != params.BeaconConfig().FarFutureEpoch { | ||
t.Error("Expected active balance not to exist in empty cache") | ||
} | ||
|
||
if err := cache.AddActiveBalance(tInfo); err != nil { | ||
t.Fatal(err) | ||
} | ||
activeBalance, err = cache.ActiveBalanceInEpoch(tInfo.Epoch) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !reflect.DeepEqual(activeBalance, tInfo.ActiveBalance) { | ||
t.Errorf( | ||
"Expected fetched active balance to be %v, got %v", | ||
tInfo.ActiveBalance, | ||
activeBalance, | ||
) | ||
} | ||
} | ||
|
||
func TestActiveBalance_MaxSize(t *testing.T) { | ||
cache := NewActiveBalanceCache() | ||
|
||
for i := uint64(0); i < params.BeaconConfig().LatestRandaoMixesLength+100; i++ { | ||
tInfo := &ActiveBalanceByEpoch{ | ||
Epoch: i, | ||
} | ||
if err := cache.AddActiveBalance(tInfo); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
if len(cache.activeBalanceCache.ListKeys()) != maxActiveBalanceListSize { | ||
t.Errorf( | ||
"Expected hash cache key size to be %d, got %d", | ||
maxActiveBalanceListSize, | ||
len(cache.activeBalanceCache.ListKeys()), | ||
) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Added tests to verify a new finalized block clears cache