Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change slasher cache to LRU cache (#5037)
* Change cache to LRU cache * fixes * REduce db usage * Fix function name * Merge issues * Save on eviction * Fixes * Fix Co-authored-by: terence tsao <terence@prysmaticlabs.com>
- Loading branch information
1 parent
962fe85
commit d4cd51f
Showing
9 changed files
with
137 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = ["span_cache.go"], | ||
importpath = "github.com/prysmaticlabs/prysm/slasher/cache", | ||
visibility = ["//slasher:__subpackages__"], | ||
deps = [ | ||
"//slasher/detection/attestations/types:go_default_library", | ||
"@com_github_hashicorp_golang_lru//:go_default_library", | ||
"@com_github_prometheus_client_golang//prometheus:go_default_library", | ||
"@com_github_prometheus_client_golang//prometheus/promauto:go_default_library", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package cache | ||
|
||
import ( | ||
lru "github.com/hashicorp/golang-lru" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
"github.com/prysmaticlabs/prysm/slasher/detection/attestations/types" | ||
) | ||
|
||
var ( | ||
// epochSpansCacheSize defines the max number of epoch spans the cache can hold. | ||
epochSpansCacheSize = 256 | ||
// Metrics | ||
epochSpansCacheHit = promauto.NewCounter(prometheus.CounterOpts{ | ||
Name: "epoch_spans_cache_hit", | ||
Help: "The total number of cache hits on the epoch spans cache.", | ||
}) | ||
epochSpansCacheMiss = promauto.NewCounter(prometheus.CounterOpts{ | ||
Name: "epoch_spans_cache_miss", | ||
Help: "The total number of cache misses on the epoch spans cache.", | ||
}) | ||
) | ||
|
||
// EpochSpansCache is used to store the spans needed on a per-epoch basis for slashing detection. | ||
type EpochSpansCache struct { | ||
cache *lru.Cache | ||
} | ||
|
||
// NewEpochSpansCache initializes the map and underlying cache. | ||
func NewEpochSpansCache(onEvicted func(key interface{}, value interface {})) (*EpochSpansCache, error) { | ||
cache, err := lru.NewWithEvict(epochSpansCacheSize, onEvicted) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &EpochSpansCache{cache: cache}, nil | ||
} | ||
|
||
// Get returns an ok bool and the cached value for the requested epoch key, if any. | ||
func (c *EpochSpansCache) Get(epoch uint64) (map[uint64]types.Span, bool) { | ||
item, exists := c.cache.Get(epoch) | ||
if exists && item != nil { | ||
epochSpansCacheHit.Inc() | ||
return item.(map[uint64]types.Span), true | ||
} | ||
|
||
epochSpansCacheMiss.Inc() | ||
return make(map[uint64]types.Span), false | ||
} | ||
|
||
// Set the response in the cache. | ||
func (c *EpochSpansCache) Set(epoch uint64, epochSpans map[uint64]types.Span) { | ||
_ = c.cache.Add(epoch, epochSpans) | ||
} | ||
|
||
// Delete removes an epoch from the cache and returns if it existed or not. | ||
// Performs the onEviction function before removal. | ||
func (c *EpochSpansCache) Delete(epoch uint64) bool { | ||
return c.cache.Remove(epoch) | ||
} | ||
|
||
// Has returns true if the key exists in the cache. | ||
func (c *EpochSpansCache) Has(epoch uint64) bool { | ||
return c.cache.Contains(epoch) | ||
} | ||
|
||
// Clear removes all keys from the SpanCache. | ||
func (c *EpochSpansCache) Clear() { | ||
c.cache.Purge() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.