remove old cache for active indices. this is not used in production and will soon be replaced (#4264)

This commit is contained in:
Preston Van Loon
2019-12-11 13:48:48 -08:00
committed by Raul Jordan
parent e72ff1bb4f
commit c0b3767757
10 changed files with 7 additions and 275 deletions

View File

@@ -4,7 +4,6 @@ go_library(
name = "go_default_library",
srcs = [
"active_count.go",
"active_indices.go",
"attestation_data.go",
"checkpoint_state.go",
"committee.go",
@@ -33,9 +32,7 @@ go_test(
size = "small",
srcs = [
"active_count_test.go",
"active_indices_test.go",
"attestation_data_test.go",
"benchmarks_test.go",
"checkpoint_state_test.go",
"committee_test.go",
"eth1_data_test.go",

View File

@@ -1,106 +0,0 @@
package cache
import (
"errors"
"strconv"
"sync"
"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"
)
var (
// ErrNotActiveIndicesInfo will be returned when a cache object is not a pointer to
// a ActiveIndicesByEpoch struct.
ErrNotActiveIndicesInfo = errors.New("object is not a active indices list")
// maxActiveIndicesListSize defines the max number of active indices can cache.
maxActiveIndicesListSize = 4
// Metrics.
activeIndicesCacheMiss = promauto.NewCounter(prometheus.CounterOpts{
Name: "active_validator_indices_cache_miss",
Help: "The number of active validator indices requests that aren't present in the cache.",
})
activeIndicesCacheHit = promauto.NewCounter(prometheus.CounterOpts{
Name: "active_validator_indices_cache_hit",
Help: "The number of active validator indices requests that are present in the cache.",
})
)
// ActiveIndicesByEpoch defines the active validator indices per epoch.
type ActiveIndicesByEpoch struct {
Epoch uint64
ActiveIndices []uint64
}
// ActiveIndicesCache is a struct with 1 queue for looking up active indices by epoch.
type ActiveIndicesCache struct {
activeIndicesCache *cache.FIFO
lock sync.RWMutex
}
// activeIndicesKeyFn takes the epoch as the key for the active indices of a given epoch.
func activeIndicesKeyFn(obj interface{}) (string, error) {
aInfo, ok := obj.(*ActiveIndicesByEpoch)
if !ok {
return "", ErrNotActiveIndicesInfo
}
return strconv.Itoa(int(aInfo.Epoch)), nil
}
// NewActiveIndicesCache creates a new active indices cache for storing/accessing active validator indices.
func NewActiveIndicesCache() *ActiveIndicesCache {
return &ActiveIndicesCache{
activeIndicesCache: cache.NewFIFO(activeIndicesKeyFn),
}
}
// 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)))
if err != nil {
return nil, err
}
if exists {
activeIndicesCacheHit.Inc()
} else {
activeIndicesCacheMiss.Inc()
return nil, nil
}
aInfo, ok := obj.(*ActiveIndicesByEpoch)
if !ok {
return nil, ErrNotActiveIndicesInfo
}
return aInfo.ActiveIndices, nil
}
// AddActiveIndicesList adds ActiveIndicesByEpoch object to the cache. This method also trims the least
// recently added ActiveIndicesByEpoch object if the cache size has ready the max cache size limit.
func (c *ActiveIndicesCache) AddActiveIndicesList(activeIndices *ActiveIndicesByEpoch) error {
c.lock.Lock()
defer c.lock.Unlock()
if err := c.activeIndicesCache.AddIfNotPresent(activeIndices); err != nil {
return err
}
trim(c.activeIndicesCache, maxActiveIndicesListSize)
return nil
}
// ActiveIndicesKeys returns the keys of the active indices cache.
func (c *ActiveIndicesCache) ActiveIndicesKeys() []string {
return c.activeIndicesCache.ListKeys()
}

View File

@@ -1,82 +0,0 @@
package cache
import (
"reflect"
"strconv"
"testing"
)
func TestActiveIndicesKeyFn_OK(t *testing.T) {
aInfo := &ActiveIndicesByEpoch{
Epoch: 999,
ActiveIndices: []uint64{1, 2, 3, 4, 5},
}
key, err := activeIndicesKeyFn(aInfo)
if err != nil {
t.Fatal(err)
}
if key != strconv.Itoa(int(aInfo.Epoch)) {
t.Errorf("Incorrect hash key: %s, expected %s", key, strconv.Itoa(int(aInfo.Epoch)))
}
}
func TestActiveIndicesKeyFn_InvalidObj(t *testing.T) {
_, err := activeIndicesKeyFn("bad")
if err != ErrNotActiveIndicesInfo {
t.Errorf("Expected error %v, got %v", ErrNotActiveIndicesInfo, err)
}
}
func TestActiveIndicesCache_ActiveIndicesByEpoch(t *testing.T) {
cache := NewActiveIndicesCache()
aInfo := &ActiveIndicesByEpoch{
Epoch: 99,
ActiveIndices: []uint64{1, 2, 3, 4},
}
activeIndices, err := cache.ActiveIndicesInEpoch(aInfo.Epoch)
if err != nil {
t.Fatal(err)
}
if activeIndices != nil {
t.Error("Expected active indices not to exist in empty cache")
}
if err := cache.AddActiveIndicesList(aInfo); err != nil {
t.Fatal(err)
}
activeIndices, err = cache.ActiveIndicesInEpoch(aInfo.Epoch)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(activeIndices, aInfo.ActiveIndices) {
t.Errorf(
"Expected fetched active indices to be %v, got %v",
aInfo.ActiveIndices,
activeIndices,
)
}
}
func TestActiveIndices_MaxSize(t *testing.T) {
cache := NewActiveIndicesCache()
for i := uint64(0); i < 100; i++ {
aInfo := &ActiveIndicesByEpoch{
Epoch: i,
}
if err := cache.AddActiveIndicesList(aInfo); err != nil {
t.Fatal(err)
}
}
if len(cache.activeIndicesCache.ListKeys()) != maxActiveIndicesListSize {
t.Errorf(
"Expected hash cache key size to be %d, got %d",
maxActiveIndicesListSize,
len(cache.activeIndicesCache.ListKeys()),
)
}
}

View File

@@ -1,45 +0,0 @@
package cache
import (
"testing"
)
var indices300k = createIndices(300000)
var epoch = uint64(1)
func createIndices(count int) *ActiveIndicesByEpoch {
indices := make([]uint64, 0, count)
for i := 0; i < count; i++ {
indices = append(indices, uint64(i))
}
return &ActiveIndicesByEpoch{
Epoch: epoch,
ActiveIndices: indices,
}
}
func BenchmarkCachingAddRetrieve(b *testing.B) {
c := NewActiveIndicesCache()
b.Run("ADD300K", func(b *testing.B) {
b.N = 10
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := c.AddActiveIndicesList(indices300k); err != nil {
b.Fatal(err)
}
}
})
b.Run("RETR300K", func(b *testing.B) {
b.N = 10
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := c.ActiveIndicesInEpoch(epoch); err != nil {
b.Fatal(err)
}
}
})
}

View File

@@ -9,6 +9,5 @@ func init() {
EnableShuffledIndexCache: true,
EnableCommitteeCache: true,
EnableActiveCountCache: true,
EnableActiveIndicesCache: true,
})
}

View File

@@ -757,7 +757,6 @@ func TestProcessAttestations_InclusionDelayFailure(t *testing.T) {
}
func TestProcessAttestations_NeitherCurrentNorPrevEpoch(t *testing.T) {
helpers.ClearActiveIndicesCache()
helpers.ClearActiveCountCache()
att := &ethpb.Attestation{

View File

@@ -9,18 +9,7 @@ func ClearActiveCountCache() {
activeCountCache = cache.NewActiveCountCache()
}
// ClearActiveIndicesCache restarts the active validator indices cache from scratch.
func ClearActiveIndicesCache() {
activeIndicesCache = cache.NewActiveIndicesCache()
}
// ActiveIndicesKeys returns the keys of the active indices cache.
func ActiveIndicesKeys() []string {
return activeIndicesCache.ActiveIndicesKeys()
}
// ClearAllCaches clears all the helpers caches from scratch.
func ClearAllCaches() {
ClearActiveIndicesCache()
ClearActiveCountCache()
}

View File

@@ -12,7 +12,6 @@ import (
"github.com/prysmaticlabs/prysm/shared/params"
)
var activeIndicesCache = cache.NewActiveIndicesCache()
var activeCountCache = cache.NewActiveCountCache()
// IsActiveValidator returns the boolean value on whether the validator
@@ -71,27 +70,13 @@ func ActiveValidatorIndices(state *pb.BeaconState, epoch uint64) ([]uint64, erro
}
}
indices, err := activeIndicesCache.ActiveIndicesInEpoch(epoch)
if err != nil {
return nil, errors.Wrap(err, "could not retrieve active indices from cache")
}
if indices != nil {
return indices, nil
}
var indices []uint64
for i, v := range state.Validators {
if IsActiveValidator(v, epoch) {
indices = append(indices, uint64(i))
}
}
if err := activeIndicesCache.AddActiveIndicesList(&cache.ActiveIndicesByEpoch{
Epoch: epoch,
ActiveIndices: indices,
}); err != nil {
return nil, errors.Wrap(err, "could not save active indices for cache")
}
return indices, nil
}

View File

@@ -45,7 +45,6 @@ type Flags struct {
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.
}
@@ -131,10 +130,6 @@ func ConfigureBeaconChain(ctx *cli.Context) {
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

View File

@@ -42,10 +42,6 @@ var (
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",
@@ -133,6 +129,11 @@ var (
Usage: deprecatedUsage,
Hidden: true,
}
deprecatedEnableActiveIndicesCacheFlag = cli.BoolFlag{
Name: "enable-active-indices-cache",
Usage: deprecatedUsage,
Hidden: true,
}
)
var deprecatedFlags = []cli.Flag{
@@ -142,6 +143,7 @@ var deprecatedFlags = []cli.Flag{
deprecatedPruneFinalizedStatesFlag,
deprecatedOptimizeProcessEpoch,
deprecatedInitSyncNoVerifyFlag,
deprecatedEnableActiveIndicesCacheFlag,
}
// ValidatorFlags contains a list of all the feature flags that apply to the validator client.
@@ -166,7 +168,6 @@ var BeaconChainFlags = append(deprecatedFlags, []cli.Flag{
enableBLSPubkeyCacheFlag,
enableShuffledIndexCache,
enableCommitteeCacheFlag,
enableActiveIndicesCacheFlag,
enableActiveCountCacheFlag,
enableSkipSlotsCache,
enableSnappyDBCompressionFlag,