Skip to content

Commit

Permalink
Feature gate all caches (#3894)
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvanloon authored and terencechain committed Nov 4, 2019
1 parent db26c0d commit 7d16332
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 35 deletions.
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
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 && !featureconfig.Get().EnableNewCache {
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 && !featureconfig.Get().EnableNewCache {
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 && !featureconfig.Get().EnableNewCache {
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 && !featureconfig.Get().EnableNewCache {
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 && !featureconfig.Get().EnableNewCache {
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 && !featureconfig.Get().EnableNewCache {
return nil, nil
}

c.lock.RLock()
defer c.lock.RUnlock()
obj, exists, err := c.CommitteeCache.GetByKey(strconv.Itoa(int(epoch)))
Expand Down
8 changes: 6 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,11 @@ 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,
EnableActiveCountCache: true,
EnableActiveIndicesCache: true,
})
}
27 changes: 0 additions & 27 deletions beacon-chain/core/epoch/epoch_processing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1096,33 +1096,6 @@ func TestAttestationDelta_CantGetAttestation(t *testing.T) {
}
}

func TestAttestationDelta_CantGetAttestationIndices(t *testing.T) {
e := params.BeaconConfig().SlotsPerEpoch

state := buildState(e+2, 1)
atts := make([]*pb.PendingAttestation, 2)
for i := 0; i < len(atts); i++ {
atts[i] = &pb.PendingAttestation{
Data: &ethpb.AttestationData{
Crosslink: &ethpb.Crosslink{
Shard: uint64(i),
},
Target: &ethpb.Checkpoint{},
Source: &ethpb.Checkpoint{},
},
InclusionDelay: uint64(i + 100),
AggregationBits: bitfield.Bitlist{0xFF, 0x01},
}
}
state.PreviousEpochAttestations = atts

_, _, err := attestationDelta(state)
wanted := "could not get attestation indices"
if !strings.Contains(err.Error(), wanted) {
t.Fatalf("Got: %v, want: %v", err.Error(), wanted)
}
}

func TestAttestationDelta_NoOneAttested(t *testing.T) {
e := params.BeaconConfig().SlotsPerEpoch
validatorCount := params.BeaconConfig().MinGenesisActiveValidatorCount / 32
Expand Down
7 changes: 6 additions & 1 deletion beacon-chain/core/helpers/committee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,11 @@ func TestShuffledIndices_ShuffleRightLength(t *testing.T) {
}

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

ClearAllCaches()

validatorCount := int(params.BeaconConfig().MinGenesisActiveValidatorCount)
Expand Down Expand Up @@ -833,7 +838,7 @@ func TestUpdateCommitteeCache_CanUpdate(t *testing.T) {
t.Fatal(err)
}
if len(indices) != int(params.BeaconConfig().TargetCommitteeSize) {
t.Error("Did not save correct indices lengths")
t.Errorf("Did not save correct indices lengths, got %d wanted %d", len(indices), params.BeaconConfig().TargetCommitteeSize)
}
}

Expand Down
25 changes: 20 additions & 5 deletions shared/featureconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ type Flag struct {
PruneFinalizedStates bool // PruneFinalizedStates from the database.

// 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.
EnableShuffledIndexCache bool // EnableShuffledIndexCache to cache expensive shuffled index computation.
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.
EnableSkipSlotsCache bool // EnableSkipSlotsCache caches the state in skipped slots.
EnableCommitteeCache bool // EnableCommitteeCache to cache committee computation.
EnableActiveIndicesCache bool // EnableActiveIndicesCache.
EnableActiveCountCache bool // EnableActiveCountCache.
}

var featureConfig *Flag
Expand Down Expand Up @@ -124,6 +127,18 @@ func ConfigureBeaconChain(ctx *cli.Context) {
log.Warn("Enabled skip slots cache.")
cfg.EnableSkipSlotsCache = true
}
if ctx.GlobalBool(enableCommitteeCacheFlag.Name) {
log.Warn("Enabled committee cache.")
cfg.EnableCommitteeCache = 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
15 changes: 15 additions & 0 deletions shared/featureconfig/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ var (
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",
}
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 @@ -118,6 +130,9 @@ var BeaconChainFlags = append(deprecatedFlags, []cli.Flag{
enableBackupWebhookFlag,
enableBLSPubkeyCacheFlag,
enableShuffledIndexCache,
enableCommitteeCacheFlag,
enableActiveIndicesCacheFlag,
enableActiveCountCacheFlag,
pruneFinalizedStatesFlag,
enableSkipSlotsCache,
}...)

0 comments on commit 7d16332

Please sign in to comment.