2020-01-21 02:19:42 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
lru "github.com/hashicorp/golang-lru"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
2021-03-08 22:37:33 +00:00
|
|
|
iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface"
|
2020-03-25 01:15:00 +00:00
|
|
|
"go.opencensus.io/trace"
|
2020-01-21 02:19:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Metrics
|
|
|
|
skipSlotCacheHit = promauto.NewCounter(prometheus.CounterOpts{
|
|
|
|
Name: "skip_slot_cache_hit",
|
|
|
|
Help: "The total number of cache hits on the skip slot cache.",
|
|
|
|
})
|
|
|
|
skipSlotCacheMiss = promauto.NewCounter(prometheus.CounterOpts{
|
|
|
|
Name: "skip_slot_cache_miss",
|
|
|
|
Help: "The total number of cache misses on the skip slot cache.",
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
// SkipSlotCache is used to store the cached results of processing skip slots in state.ProcessSlots.
|
|
|
|
type SkipSlotCache struct {
|
|
|
|
cache *lru.Cache
|
|
|
|
lock sync.RWMutex
|
2020-04-02 02:09:54 +00:00
|
|
|
disabled bool // Allow for programmatic toggling of the cache, useful during initial sync.
|
2020-10-28 03:09:05 +00:00
|
|
|
inProgress map[[32]byte]bool
|
2020-01-21 02:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSkipSlotCache initializes the map and underlying cache.
|
|
|
|
func NewSkipSlotCache() *SkipSlotCache {
|
|
|
|
cache, err := lru.New(8)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return &SkipSlotCache{
|
|
|
|
cache: cache,
|
2020-10-28 03:09:05 +00:00
|
|
|
inProgress: make(map[[32]byte]bool),
|
2020-01-21 02:19:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 02:09:54 +00:00
|
|
|
// Enable the skip slot cache.
|
|
|
|
func (c *SkipSlotCache) Enable() {
|
|
|
|
c.disabled = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable the skip slot cache.
|
|
|
|
func (c *SkipSlotCache) Disable() {
|
|
|
|
c.disabled = true
|
|
|
|
}
|
|
|
|
|
2020-01-21 02:19:42 +00:00
|
|
|
// Get waits for any in progress calculation to complete before returning a
|
|
|
|
// cached response, if any.
|
2021-03-08 22:37:33 +00:00
|
|
|
func (c *SkipSlotCache) Get(ctx context.Context, r [32]byte) (iface.BeaconState, error) {
|
2020-03-25 01:15:00 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "skipSlotCache.Get")
|
|
|
|
defer span.End()
|
2020-04-02 02:09:54 +00:00
|
|
|
if c.disabled {
|
2020-01-21 02:19:42 +00:00
|
|
|
// Return a miss result if cache is not enabled.
|
|
|
|
skipSlotCacheMiss.Inc()
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
delay := minDelay
|
|
|
|
|
|
|
|
// Another identical request may be in progress already. Let's wait until
|
|
|
|
// any in progress request resolves or our timeout is exceeded.
|
2020-03-25 01:15:00 +00:00
|
|
|
inProgress := false
|
2020-01-21 02:19:42 +00:00
|
|
|
for {
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
c.lock.RLock()
|
2020-10-28 03:09:05 +00:00
|
|
|
if !c.inProgress[r] {
|
2020-01-21 02:19:42 +00:00
|
|
|
c.lock.RUnlock()
|
|
|
|
break
|
|
|
|
}
|
2020-03-25 01:15:00 +00:00
|
|
|
inProgress = true
|
2020-01-21 02:19:42 +00:00
|
|
|
c.lock.RUnlock()
|
|
|
|
|
|
|
|
// This increasing backoff is to decrease the CPU cycles while waiting
|
|
|
|
// for the in progress boolean to flip to false.
|
|
|
|
time.Sleep(time.Duration(delay) * time.Nanosecond)
|
|
|
|
delay *= delayFactor
|
|
|
|
delay = math.Min(delay, maxDelay)
|
|
|
|
}
|
2020-03-25 01:15:00 +00:00
|
|
|
span.AddAttributes(trace.BoolAttribute("inProgress", inProgress))
|
2020-01-21 02:19:42 +00:00
|
|
|
|
2020-10-28 03:09:05 +00:00
|
|
|
item, exists := c.cache.Get(r)
|
2020-01-21 02:19:42 +00:00
|
|
|
|
|
|
|
if exists && item != nil {
|
|
|
|
skipSlotCacheHit.Inc()
|
2020-03-25 01:15:00 +00:00
|
|
|
span.AddAttributes(trace.BoolAttribute("hit", true))
|
2021-03-08 22:37:33 +00:00
|
|
|
return item.(iface.BeaconState).Copy(), nil
|
2020-01-21 02:19:42 +00:00
|
|
|
}
|
|
|
|
skipSlotCacheMiss.Inc()
|
2020-03-25 01:15:00 +00:00
|
|
|
span.AddAttributes(trace.BoolAttribute("hit", false))
|
2020-01-21 02:19:42 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarkInProgress a request so that any other similar requests will block on
|
|
|
|
// Get until MarkNotInProgress is called.
|
2020-10-28 03:09:05 +00:00
|
|
|
func (c *SkipSlotCache) MarkInProgress(r [32]byte) error {
|
2020-04-02 02:09:54 +00:00
|
|
|
if c.disabled {
|
2020-01-21 02:19:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
|
2020-10-28 03:09:05 +00:00
|
|
|
if c.inProgress[r] {
|
2020-01-21 02:19:42 +00:00
|
|
|
return ErrAlreadyInProgress
|
|
|
|
}
|
2020-10-28 03:09:05 +00:00
|
|
|
c.inProgress[r] = true
|
2020-01-21 02:19:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarkNotInProgress will release the lock on a given request. This should be
|
|
|
|
// called after put.
|
2020-10-28 03:09:05 +00:00
|
|
|
func (c *SkipSlotCache) MarkNotInProgress(r [32]byte) error {
|
2020-04-02 02:09:54 +00:00
|
|
|
if c.disabled {
|
2020-01-21 02:19:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
|
2020-10-28 03:09:05 +00:00
|
|
|
delete(c.inProgress, r)
|
2020-01-21 02:19:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put the response in the cache.
|
2021-03-08 22:37:33 +00:00
|
|
|
func (c *SkipSlotCache) Put(_ context.Context, r [32]byte, state iface.BeaconState) error {
|
2020-04-02 02:09:54 +00:00
|
|
|
if c.disabled {
|
2020-01-21 02:19:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-31 20:57:01 +00:00
|
|
|
// Copy state so cached value is not mutated.
|
2020-10-28 03:09:05 +00:00
|
|
|
c.cache.Add(r, state.Copy())
|
2020-01-21 02:19:42 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|