Add feature flag to enable shuffled index cache (#3893)

* add feature flag to enable shuffled index cache

* gaz'
This commit is contained in:
Preston Van Loon
2019-10-29 22:26:05 -04:00
committed by Nishant Das
parent ae89cce593
commit 2b444ea954
6 changed files with 36 additions and 4 deletions

View File

@@ -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"
)
@@ -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))
@@ -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 {

View File

@@ -4,8 +4,16 @@ import (
"reflect"
"strconv"
"testing"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
)
func init() {
fc := featureconfig.Get()
fc.EnableShuffledIndexCache = true
featureconfig.Init(fc)
}
func TestShuffleKeyFn_OK(t *testing.T) {
sInfo := &IndicesByIndexSeed{
Index: 999,

View File

@@ -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",

View File

@@ -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"
@@ -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

View File

@@ -36,10 +36,11 @@ 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.
}
var featureConfig *Flag
@@ -114,6 +115,10 @@ 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
}
Init(cfg)
}

View File

@@ -29,6 +29,10 @@ 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",
}
// InitSyncNoVerifyFlag enables the initial sync no verify configuration.
InitSyncNoVerifyFlag = cli.BoolFlag{
Name: "init-sync-no-verify",
@@ -99,6 +103,7 @@ var BeaconChainFlags = append(deprecatedFlags, []cli.Flag{
OptimizeProcessEpoch,
enableBackupWebhookFlag,
enableBLSPubkeyCacheFlag,
enableShuffledIndexCache,
pruneFinalizedStatesFlag,
enableFinalizedBlockRootIndexFlag,
}...)