Skip to content
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

Feature gate all caches #3894

Merged
merged 19 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions beacon-chain/cache/active_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"github.com/prysmaticlabs/prysm/shared/params"
"k8s.io/client-go/tools/cache"
)
Expand Down Expand Up @@ -62,6 +63,9 @@ func NewActiveCountCache() *ActiveCountCache {
// ActiveCountInEpoch fetches ActiveCountByEpoch by epoch. Returns true with a
// reference to the ActiveCountInEpoch info, if exists. Otherwise returns false, nil.
func (c *ActiveCountCache) ActiveCountInEpoch(epoch uint64) (uint64, error) {
if !featureconfig.Get().EnableActiveCountCache {
return params.BeaconConfig().FarFutureEpoch, nil
}
c.lock.RLock()
defer c.lock.RUnlock()
obj, exists, err := c.activeCountCache.GetByKey(strconv.Itoa(int(epoch)))
Expand Down
4 changes: 4 additions & 0 deletions beacon-chain/cache/active_indices.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"k8s.io/client-go/tools/cache"
)

Expand Down Expand Up @@ -61,6 +62,9 @@ func NewActiveIndicesCache() *ActiveIndicesCache {
// ActiveIndicesInEpoch fetches ActiveIndicesByEpoch by epoch. Returns true with a
// reference to the ActiveIndicesInEpoch info, if exists. Otherwise returns false, nil.
func (c *ActiveIndicesCache) ActiveIndicesInEpoch(epoch uint64) ([]uint64, error) {
if !featureconfig.Get().EnableActiveIndicesCache {
return nil, nil
}
c.lock.RLock()
defer c.lock.RUnlock()
obj, exists, err := c.activeIndicesCache.GetByKey(strconv.Itoa(int(epoch)))
Expand Down
9 changes: 9 additions & 0 deletions beacon-chain/cache/checkpoint_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promauto"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"k8s.io/client-go/tools/cache"
)
Expand Down Expand Up @@ -68,6 +69,10 @@ func NewCheckpointStateCache() *CheckpointStateCache {
// StateByCheckpoint fetches state by checkpoint. Returns true with a
// reference to the CheckpointState info, if exists. Otherwise returns false, nil.
func (c *CheckpointStateCache) StateByCheckpoint(cp *ethpb.Checkpoint) (*pb.BeaconState, error) {
if !featureconfig.Get().EnableCheckpointStateCache {
return nil, nil
}

c.lock.RLock()
defer c.lock.RUnlock()
h, err := hashutil.HashProto(cp)
Expand Down Expand Up @@ -98,6 +103,10 @@ func (c *CheckpointStateCache) StateByCheckpoint(cp *ethpb.Checkpoint) (*pb.Beac
// AddCheckpointState adds CheckpointState object to the cache. This method also trims the least
// recently added CheckpointState object if the cache size has ready the max cache size limit.
func (c *CheckpointStateCache) AddCheckpointState(cp *CheckpointState) error {
if !featureconfig.Get().EnableCheckpointStateCache {
return nil
}

c.lock.Lock()
defer c.lock.Unlock()
if err := c.cache.AddIfNotPresent(cp); err != nil {
Expand Down
23 changes: 23 additions & 0 deletions beacon-chain/cache/committee.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/sliceutil"
"k8s.io/client-go/tools/cache"
Expand Down Expand Up @@ -67,6 +68,9 @@ func NewCommitteeCache() *CommitteeCache {
// ShuffledIndices fetches the shuffled indices by epoch and shard. Every list of indices
// represent one committee. Returns true if the list exists with epoch and shard. Otherwise returns false, nil.
func (c *CommitteeCache) ShuffledIndices(epoch uint64, shard uint64) ([]uint64, error) {
if !featureconfig.Get().EnableShuffledIndexCache {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the committee cache is already gated by the new-cache flag if I am not wrong.
@terencechain can you confirm ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's correct, we don't need this flag

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then that flag needs to be moved here. Not putting the flag inside of the cache allows for misuse without proper flag control.

return nil, nil
}
c.lock.RLock()
defer c.lock.RUnlock()
obj, exists, err := c.CommitteeCache.GetByKey(strconv.Itoa(int(epoch)))
Expand Down Expand Up @@ -94,6 +98,9 @@ func (c *CommitteeCache) ShuffledIndices(epoch uint64, shard uint64) ([]uint64,
// AddCommitteeShuffledList adds Committee shuffled list object to the cache. T
// his method also trims the least recently list if the cache size has ready the max cache size limit.
func (c *CommitteeCache) AddCommitteeShuffledList(committee *Committee) error {
if !featureconfig.Get().EnableShuffledIndexCache {
return nil
}
c.lock.Lock()
defer c.lock.Unlock()
if err := c.CommitteeCache.AddIfNotPresent(committee); err != nil {
Expand All @@ -105,6 +112,9 @@ func (c *CommitteeCache) AddCommitteeShuffledList(committee *Committee) error {

// Epochs returns the epochs stored in the committee cache. These are the keys to the cache.
func (c *CommitteeCache) Epochs() ([]uint64, error) {
if !featureconfig.Get().EnableShuffledIndexCache {
return nil, nil
}
c.lock.RLock()
defer c.lock.RUnlock()

Expand All @@ -121,6 +131,9 @@ func (c *CommitteeCache) Epochs() ([]uint64, error) {

// EpochInCache returns true if an input epoch is part of keys in cache.
func (c *CommitteeCache) EpochInCache(wantedEpoch uint64) (bool, error) {
if !featureconfig.Get().EnableShuffledIndexCache {
return false, nil
}
c.lock.RLock()
defer c.lock.RUnlock()

Expand All @@ -138,6 +151,9 @@ func (c *CommitteeCache) EpochInCache(wantedEpoch uint64) (bool, error) {

// CommitteeCount returns the total number of committees in a given epoch as stored in cache.
func (c *CommitteeCache) CommitteeCount(epoch uint64) (uint64, bool, error) {
if !featureconfig.Get().EnableShuffledIndexCache {
return 0, false, nil
}
c.lock.RLock()
defer c.lock.RUnlock()
obj, exists, err := c.CommitteeCache.GetByKey(strconv.Itoa(int(epoch)))
Expand All @@ -162,6 +178,9 @@ func (c *CommitteeCache) CommitteeCount(epoch uint64) (uint64, bool, error) {

// StartShard returns the start shard number in a given epoch as stored in cache.
func (c *CommitteeCache) StartShard(epoch uint64) (uint64, bool, error) {
if !featureconfig.Get().EnableShuffledIndexCache {
return 0, false, nil
}
c.lock.RLock()
defer c.lock.RUnlock()
obj, exists, err := c.CommitteeCache.GetByKey(strconv.Itoa(int(epoch)))
Expand All @@ -186,6 +205,10 @@ func (c *CommitteeCache) StartShard(epoch uint64) (uint64, bool, error) {

// ActiveIndices returns the active indices of a given epoch stored in cache.
func (c *CommitteeCache) ActiveIndices(epoch uint64) ([]uint64, error) {
if !featureconfig.Get().EnableShuffledIndexCache {
return nil, nil
}

c.lock.RLock()
defer c.lock.RUnlock()
obj, exists, err := c.CommitteeCache.GetByKey(strconv.Itoa(int(epoch)))
Expand Down
10 changes: 8 additions & 2 deletions beacon-chain/cache/feature_flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import "github.com/prysmaticlabs/prysm/shared/featureconfig"

func init() {
featureconfig.Init(&featureconfig.Flag{
EnableAttestationCache: true,
EnableEth1DataVoteCache: true,
EnableAttestationCache: true,
EnableEth1DataVoteCache: true,
EnableShuffledIndexCache: true,
EnableCommitteeCache: true,
EnableCheckpointStateCache: true,
EnableActiveCountCache: true,
EnableFinalizedBlockRootIndex: true,
EnableActiveIndicesCache: true,
})
}
7 changes: 7 additions & 0 deletions beacon-chain/cache/shuffled_indices.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"k8s.io/client-go/tools/cache"
)

Expand Down Expand Up @@ -62,6 +63,9 @@ func NewShuffledIndicesCache() *ShuffledIndicesCache {
// IndicesByIndexSeed fetches IndicesByIndexSeed by epoch and seed. Returns true with a
// reference to the ShuffledIndicesInEpoch info, if exists. Otherwise returns false, nil.
func (c *ShuffledIndicesCache) IndicesByIndexSeed(index uint64, seed []byte) ([]uint64, error) {
if !featureconfig.Get().EnableShuffledIndexCache {
return nil, nil
}
c.lock.RLock()
defer c.lock.RUnlock()
key := string(seed) + strconv.Itoa(int(index))
Expand All @@ -88,6 +92,9 @@ func (c *ShuffledIndicesCache) IndicesByIndexSeed(index uint64, seed []byte) ([]
// AddShuffledValidatorList adds IndicesByIndexSeed object to the cache. This method also trims the least
// recently added IndicesByIndexSeed object if the cache size has ready the max cache size limit.
func (c *ShuffledIndicesCache) AddShuffledValidatorList(shuffledIndices *IndicesByIndexSeed) error {
if !featureconfig.Get().EnableShuffledIndexCache {
return nil
}
c.lock.Lock()
defer c.lock.Unlock()
if err := c.shuffledIndicesCache.AddIfNotPresent(shuffledIndices); err != nil {
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/core/helpers/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ go_test(
"//proto/eth/v1alpha1:go_default_library",
"//shared/bls:go_default_library",
"//shared/bytesutil:go_default_library",
"//shared/featureconfig:go_default_library",
"//shared/params:go_default_library",
"//shared/sliceutil:go_default_library",
"//shared/testutil:go_default_library",
Expand Down
6 changes: 6 additions & 0 deletions beacon-chain/core/helpers/committee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/prysmaticlabs/go-bitfield"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/sliceutil"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -176,6 +177,11 @@ func TestComputeCommittee_WithoutCache(t *testing.T) {
}

func TestComputeCommittee_WithCache(t *testing.T) {
fc := featureconfig.Get()
fc.EnableShuffledIndexCache = true
featureconfig.Init(fc)
defer featureconfig.Init(nil)

// Create 10 committees
committeeCount := uint64(10)
validatorCount := committeeCount * params.BeaconConfig().TargetCommitteeSize
Expand Down
33 changes: 29 additions & 4 deletions shared/featureconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ type Flag struct {
EnableFinalizedBlockRootIndex bool // EnableFinalizedBlockRootIndex in the database. This operation may be expensive at runtime.

// Cache toggles.
EnableAttestationCache bool // EnableAttestationCache; see https://github.com/prysmaticlabs/prysm/issues/3106.
EnableEth1DataVoteCache bool // EnableEth1DataVoteCache; see https://github.com/prysmaticlabs/prysm/issues/3106.
EnableNewCache bool // EnableNewCache enables the node to use the new caching scheme.
EnableBLSPubkeyCache bool // EnableBLSPubkeyCache to improve wall time of PubkeyFromBytes.
EnableAttestationCache bool // EnableAttestationCache; see https://github.com/prysmaticlabs/prysm/issues/3106.
EnableEth1DataVoteCache bool // EnableEth1DataVoteCache; see https://github.com/prysmaticlabs/prysm/issues/3106.
EnableNewCache bool // EnableNewCache enables the node to use the new caching scheme.
EnableBLSPubkeyCache bool // EnableBLSPubkeyCache to improve wall time of PubkeyFromBytes.
EnableShuffledIndexCache bool // EnableShuffledIndexCache to cache expensive shuffled index computation.
EnableCommitteeCache bool // EnableCommitteeCache to cache committee computation.
EnableCheckpointStateCache bool // EnableCheckpointStateCache.
EnableActiveIndicesCache bool // EnableActiveIndicesCache.
EnableActiveCountCache bool // EnableActiveCountCache.
}

var featureConfig *Flag
Expand Down Expand Up @@ -114,6 +119,26 @@ func ConfigureBeaconChain(ctx *cli.Context) {
log.Warn("Enabled finalized block root index")
cfg.EnableFinalizedBlockRootIndex = true
}
if ctx.GlobalBool(enableShuffledIndexCache.Name) {
log.Warn("Enabled shuffled index cache.")
cfg.EnableShuffledIndexCache = true
}
if ctx.GlobalBool(enableCommitteeCacheFlag.Name) {
log.Warn("Enabled committee cache.")
cfg.EnableCommitteeCache = true
}
if ctx.GlobalBool(enableCheckpointStateCacheFlag.Name) {
log.Warn("Enabled checkpoint state cache.")
cfg.EnableCheckpointStateCache = true
}
if ctx.GlobalBool(enableActiveIndicesCacheFlag.Name) {
log.Warn("Enabled active indices cache.")
cfg.EnableActiveIndicesCache = true
}
if ctx.GlobalBool(enableActiveCountCacheFlag.Name) {
log.Warn("Enabled active count cache.")
cfg.EnableActiveCountCache = true
}
Init(cfg)
}

Expand Down
25 changes: 25 additions & 0 deletions shared/featureconfig/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ var (
Name: "enable-eth1-data-vote-cache",
Usage: "Enable unsafe cache mechanism. See https://github.com/prysmaticlabs/prysm/issues/3106",
}
enableShuffledIndexCache = cli.BoolFlag{
Name: "enable-shuffled-index-cache",
Usage: "Enable unsafe cache mechanism. See https://github.com/prysmaticlabs/prysm/issues/3106",
}
enableCommitteeCacheFlag = cli.BoolFlag{
Name: "enable-committee-cache",
Usage: "Enable unsafe cache mechanism. See https://github.com/prysmaticlabs/prysm/issues/3106",
}
enableCheckpointStateCacheFlag = cli.BoolFlag{
Name: "enable-checkpoint-state-cache",
Usage: "Enable unsafe cache mechanism. See https://github.com/prysmaticlabs/prysm/issues/3106",
}
enableActiveIndicesCacheFlag = cli.BoolFlag{
Name: "enable-active-indices-cache",
Usage: "Enable unsafe cache mechanism. See https://github.com/prysmaticlabs/prysm/issues/3106",
}
enableActiveCountCacheFlag = cli.BoolFlag{
Name: "enable-active-count-cache",
Usage: "Enable unsafe cache mechanism. See https://github.com/prysmaticlabs/prysm/issues/3106",
}
// InitSyncNoVerifyFlag enables the initial sync no verify configuration.
InitSyncNoVerifyFlag = cli.BoolFlag{
Name: "init-sync-no-verify",
Expand Down Expand Up @@ -99,6 +119,11 @@ var BeaconChainFlags = append(deprecatedFlags, []cli.Flag{
OptimizeProcessEpoch,
enableBackupWebhookFlag,
enableBLSPubkeyCacheFlag,
enableShuffledIndexCache,
enableCommitteeCacheFlag,
enableCheckpointStateCacheFlag,
enableActiveIndicesCacheFlag,
enableActiveCountCacheFlag,
pruneFinalizedStatesFlag,
enableFinalizedBlockRootIndexFlag,
}...)