2015-04-14 18:00:51 +00:00
|
|
|
package physical
|
|
|
|
|
2015-04-15 20:48:49 +00:00
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2018-01-26 03:21:51 +00:00
|
|
|
"sync/atomic"
|
2015-04-15 20:48:49 +00:00
|
|
|
|
2020-10-13 15:11:54 +00:00
|
|
|
metrics "github.com/armon/go-metrics"
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2019-01-09 00:48:57 +00:00
|
|
|
lru "github.com/hashicorp/golang-lru"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/locksutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/helper/pathmanager"
|
2015-04-15 20:48:49 +00:00
|
|
|
)
|
2015-04-14 18:00:51 +00:00
|
|
|
|
|
|
|
const (
|
2015-04-14 18:03:18 +00:00
|
|
|
// DefaultCacheSize is used if no cache size is specified for NewCache
|
2018-01-26 03:21:51 +00:00
|
|
|
DefaultCacheSize = 128 * 1024
|
2019-10-08 20:23:43 +00:00
|
|
|
|
|
|
|
// refreshCacheCtxKey is a ctx value that denotes the cache should be
|
|
|
|
// refreshed during a Get call.
|
|
|
|
refreshCacheCtxKey = "refresh_cache"
|
2015-04-14 18:00:51 +00:00
|
|
|
)
|
|
|
|
|
2018-05-10 17:29:26 +00:00
|
|
|
// These paths don't need to be cached by the LRU cache. This should
|
|
|
|
// particularly help memory pressure when unsealing.
|
|
|
|
var cacheExceptionsPaths = []string{
|
|
|
|
"wal/logs/",
|
|
|
|
"index/pages/",
|
|
|
|
"index-dr/pages/",
|
|
|
|
"sys/expire/",
|
2018-09-18 03:03:00 +00:00
|
|
|
"core/poison-pill",
|
2019-07-03 20:56:30 +00:00
|
|
|
"core/raft/tls",
|
2020-12-02 17:21:14 +00:00
|
|
|
"core/license",
|
2018-05-10 17:29:26 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 20:23:43 +00:00
|
|
|
// CacheRefreshContext returns a context with an added value denoting if the
|
|
|
|
// cache should attempt a refresh.
|
|
|
|
func CacheRefreshContext(ctx context.Context, r bool) context.Context {
|
|
|
|
return context.WithValue(ctx, refreshCacheCtxKey, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// cacheRefreshFromContext is a helper to look up if the provided context is
|
|
|
|
// requesting a cache refresh.
|
|
|
|
func cacheRefreshFromContext(ctx context.Context) bool {
|
|
|
|
r, ok := ctx.Value(refreshCacheCtxKey).(bool)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2015-04-14 18:03:18 +00:00
|
|
|
// Cache is used to wrap an underlying physical backend
|
2015-04-14 18:00:51 +00:00
|
|
|
// and provide an LRU cache layer on top. Most of the reads done by
|
|
|
|
// Vault are for policy objects so there is a large read reduction
|
|
|
|
// by using a simple write-through cache.
|
2015-04-14 18:03:18 +00:00
|
|
|
type Cache struct {
|
2018-05-10 17:29:26 +00:00
|
|
|
backend Backend
|
|
|
|
lru *lru.TwoQueueCache
|
|
|
|
locks []*locksutil.LockEntry
|
|
|
|
logger log.Logger
|
|
|
|
enabled *uint32
|
|
|
|
cacheExceptions *pathmanager.PathManager
|
2020-10-13 15:11:54 +00:00
|
|
|
metricSink metrics.MetricSink
|
2017-08-09 00:47:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TransactionalCache is a Cache that wraps the physical that is transactional
|
|
|
|
type TransactionalCache struct {
|
|
|
|
*Cache
|
|
|
|
Transactional
|
2015-04-14 18:00:51 +00:00
|
|
|
}
|
|
|
|
|
2018-01-20 01:44:24 +00:00
|
|
|
// Verify Cache satisfies the correct interfaces
|
2018-01-26 03:21:51 +00:00
|
|
|
var _ ToggleablePurgemonster = (*Cache)(nil)
|
|
|
|
var _ ToggleablePurgemonster = (*TransactionalCache)(nil)
|
2018-01-20 01:44:24 +00:00
|
|
|
var _ Backend = (*Cache)(nil)
|
|
|
|
var _ Transactional = (*TransactionalCache)(nil)
|
2018-01-19 22:00:13 +00:00
|
|
|
|
2015-04-14 18:03:18 +00:00
|
|
|
// NewCache returns a physical cache of the given size.
|
2015-04-14 18:00:51 +00:00
|
|
|
// If no size is provided, the default size is used.
|
2020-10-13 15:11:54 +00:00
|
|
|
func NewCache(b Backend, size int, logger log.Logger, metricSink metrics.MetricSink) *Cache {
|
2018-04-03 00:46:59 +00:00
|
|
|
if logger.IsDebug() {
|
|
|
|
logger.Debug("creating LRU cache", "size", size)
|
2017-12-06 17:25:17 +00:00
|
|
|
}
|
2015-04-14 18:00:51 +00:00
|
|
|
if size <= 0 {
|
|
|
|
size = DefaultCacheSize
|
|
|
|
}
|
2017-12-06 17:25:17 +00:00
|
|
|
|
2018-05-10 17:29:26 +00:00
|
|
|
pm := pathmanager.New()
|
|
|
|
pm.AddPaths(cacheExceptionsPaths)
|
|
|
|
|
2016-01-07 14:21:33 +00:00
|
|
|
cache, _ := lru.New2Q(size)
|
2015-04-14 18:03:18 +00:00
|
|
|
c := &Cache{
|
2018-01-26 03:21:51 +00:00
|
|
|
backend: b,
|
|
|
|
lru: cache,
|
|
|
|
locks: locksutil.CreateLocks(),
|
|
|
|
logger: logger,
|
|
|
|
// This fails safe.
|
2018-05-10 17:29:26 +00:00
|
|
|
enabled: new(uint32),
|
|
|
|
cacheExceptions: pm,
|
2020-10-13 15:11:54 +00:00
|
|
|
metricSink: metricSink,
|
2015-04-14 18:00:51 +00:00
|
|
|
}
|
2017-08-09 00:47:14 +00:00
|
|
|
return c
|
|
|
|
}
|
2017-02-17 14:15:35 +00:00
|
|
|
|
2020-10-13 15:11:54 +00:00
|
|
|
func NewTransactionalCache(b Backend, size int, logger log.Logger, metricSink metrics.MetricSink) *TransactionalCache {
|
2017-08-09 00:47:14 +00:00
|
|
|
c := &TransactionalCache{
|
2020-10-13 15:11:54 +00:00
|
|
|
Cache: NewCache(b, size, logger, metricSink),
|
2017-08-09 00:47:14 +00:00
|
|
|
Transactional: b.(Transactional),
|
|
|
|
}
|
2015-04-14 18:00:51 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-04-13 03:13:14 +00:00
|
|
|
func (c *Cache) ShouldCache(key string) bool {
|
2018-05-10 17:29:26 +00:00
|
|
|
if atomic.LoadUint32(c.enabled) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return !c.cacheExceptions.HasPath(key)
|
|
|
|
}
|
|
|
|
|
2018-01-26 03:21:51 +00:00
|
|
|
// SetEnabled is used to toggle whether the cache is on or off. It must be
|
|
|
|
// called with true to actually activate the cache after creation.
|
|
|
|
func (c *Cache) SetEnabled(enabled bool) {
|
|
|
|
if enabled {
|
|
|
|
atomic.StoreUint32(c.enabled, 1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
atomic.StoreUint32(c.enabled, 0)
|
|
|
|
}
|
|
|
|
|
2015-04-14 18:00:51 +00:00
|
|
|
// Purge is used to clear the cache
|
2018-01-19 22:00:13 +00:00
|
|
|
func (c *Cache) Purge(ctx context.Context) {
|
2017-02-17 14:15:35 +00:00
|
|
|
// Lock the world
|
2017-03-07 16:21:32 +00:00
|
|
|
for _, lock := range c.locks {
|
2017-02-17 14:15:35 +00:00
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-04-14 18:00:51 +00:00
|
|
|
c.lru.Purge()
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (c *Cache) Put(ctx context.Context, entry *Entry) error {
|
2019-04-13 03:13:14 +00:00
|
|
|
if entry != nil && !c.ShouldCache(entry.Key) {
|
2018-01-26 03:21:51 +00:00
|
|
|
return c.backend.Put(ctx, entry)
|
|
|
|
}
|
|
|
|
|
2017-03-07 16:21:32 +00:00
|
|
|
lock := locksutil.LockForKey(c.locks, entry.Key)
|
2017-02-17 14:15:35 +00:00
|
|
|
lock.Lock()
|
2018-06-14 17:44:13 +00:00
|
|
|
defer lock.Unlock()
|
2017-02-17 14:15:35 +00:00
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
err := c.backend.Put(ctx, entry)
|
2018-01-26 03:21:51 +00:00
|
|
|
if err == nil {
|
2016-10-28 16:55:56 +00:00
|
|
|
c.lru.Add(entry.Key, entry)
|
2020-10-13 15:11:54 +00:00
|
|
|
c.metricSink.IncrCounter([]string{"cache", "write"}, 1)
|
2016-10-28 16:55:56 +00:00
|
|
|
}
|
2015-04-14 18:00:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (c *Cache) Get(ctx context.Context, key string) (*Entry, error) {
|
2019-04-13 03:13:14 +00:00
|
|
|
if !c.ShouldCache(key) {
|
2018-01-26 03:21:51 +00:00
|
|
|
return c.backend.Get(ctx, key)
|
|
|
|
}
|
|
|
|
|
2017-03-07 16:21:32 +00:00
|
|
|
lock := locksutil.LockForKey(c.locks, key)
|
2017-02-17 14:15:35 +00:00
|
|
|
lock.RLock()
|
2018-06-14 17:44:13 +00:00
|
|
|
defer lock.RUnlock()
|
2017-02-17 14:15:35 +00:00
|
|
|
|
2015-04-14 18:00:51 +00:00
|
|
|
// Check the LRU first
|
2019-10-08 20:23:43 +00:00
|
|
|
if !cacheRefreshFromContext(ctx) {
|
|
|
|
if raw, ok := c.lru.Get(key); ok {
|
|
|
|
if raw == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-10-13 15:11:54 +00:00
|
|
|
c.metricSink.IncrCounter([]string{"cache", "hit"}, 1)
|
2019-10-08 20:23:43 +00:00
|
|
|
return raw.(*Entry), nil
|
2015-04-14 18:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 15:11:54 +00:00
|
|
|
c.metricSink.IncrCounter([]string{"cache", "miss"}, 1)
|
2015-04-14 18:00:51 +00:00
|
|
|
// Read from the underlying backend
|
2018-01-19 06:44:44 +00:00
|
|
|
ent, err := c.backend.Get(ctx, key)
|
2015-04-14 18:00:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-03-03 21:04:31 +00:00
|
|
|
// Cache the result
|
2018-09-07 18:56:33 +00:00
|
|
|
c.lru.Add(key, ent)
|
2017-03-03 21:04:31 +00:00
|
|
|
|
|
|
|
return ent, nil
|
2015-04-14 18:00:51 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (c *Cache) Delete(ctx context.Context, key string) error {
|
2019-04-13 03:13:14 +00:00
|
|
|
if !c.ShouldCache(key) {
|
2018-01-26 03:21:51 +00:00
|
|
|
return c.backend.Delete(ctx, key)
|
|
|
|
}
|
|
|
|
|
2017-03-07 16:21:32 +00:00
|
|
|
lock := locksutil.LockForKey(c.locks, key)
|
2017-02-17 14:15:35 +00:00
|
|
|
lock.Lock()
|
2018-06-14 17:44:13 +00:00
|
|
|
defer lock.Unlock()
|
2017-02-17 14:15:35 +00:00
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
err := c.backend.Delete(ctx, key)
|
2018-01-26 03:21:51 +00:00
|
|
|
if err == nil {
|
2016-10-28 16:55:56 +00:00
|
|
|
c.lru.Remove(key)
|
|
|
|
}
|
2015-04-14 18:00:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (c *Cache) List(ctx context.Context, prefix string) ([]string, error) {
|
2017-02-17 14:15:35 +00:00
|
|
|
// Always pass-through as this would be difficult to cache. For the same
|
|
|
|
// reason we don't lock as we can't reasonably know which locks to readlock
|
|
|
|
// ahead of time.
|
2018-01-19 06:44:44 +00:00
|
|
|
return c.backend.List(ctx, prefix)
|
2015-04-14 18:00:51 +00:00
|
|
|
}
|
2017-02-17 14:15:35 +00:00
|
|
|
|
2019-04-13 03:13:14 +00:00
|
|
|
func (c *TransactionalCache) Locks() []*locksutil.LockEntry {
|
|
|
|
return c.locks
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TransactionalCache) LRU() *lru.TwoQueueCache {
|
|
|
|
return c.lru
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (c *TransactionalCache) Transaction(ctx context.Context, txns []*TxnEntry) error {
|
2018-05-10 17:29:26 +00:00
|
|
|
// Bypass the locking below
|
|
|
|
if atomic.LoadUint32(c.enabled) == 0 {
|
|
|
|
return c.Transactional.Transaction(ctx, txns)
|
|
|
|
}
|
|
|
|
|
2017-12-06 17:25:17 +00:00
|
|
|
// Collect keys that need to be locked
|
|
|
|
var keys []string
|
|
|
|
for _, curr := range txns {
|
|
|
|
keys = append(keys, curr.Entry.Key)
|
|
|
|
}
|
|
|
|
// Lock the keys
|
|
|
|
for _, l := range locksutil.LocksForKeys(c.locks, keys) {
|
|
|
|
l.Lock()
|
2018-06-14 17:44:13 +00:00
|
|
|
defer l.Unlock()
|
2017-02-17 14:15:35 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := c.Transactional.Transaction(ctx, txns); err != nil {
|
2017-02-17 14:15:35 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-10 17:29:26 +00:00
|
|
|
for _, txn := range txns {
|
2019-04-13 03:13:14 +00:00
|
|
|
if !c.ShouldCache(txn.Entry.Key) {
|
2018-05-10 17:29:26 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch txn.Operation {
|
|
|
|
case PutOperation:
|
|
|
|
c.lru.Add(txn.Entry.Key, txn.Entry)
|
2020-10-13 15:11:54 +00:00
|
|
|
c.metricSink.IncrCounter([]string{"cache", "write"}, 1)
|
2018-05-10 17:29:26 +00:00
|
|
|
case DeleteOperation:
|
|
|
|
c.lru.Remove(txn.Entry.Key)
|
2020-10-13 15:11:54 +00:00
|
|
|
c.metricSink.IncrCounter([]string{"cache", "delete"}, 1)
|
2017-02-17 14:15:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|