2015-04-14 18:00:51 +00:00
|
|
|
package physical
|
|
|
|
|
2015-04-15 20:48:49 +00:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2017-12-06 17:25:17 +00:00
|
|
|
iradix "github.com/hashicorp/go-immutable-radix"
|
2015-04-15 20:48:49 +00:00
|
|
|
"github.com/hashicorp/golang-lru"
|
2017-02-17 14:15:35 +00:00
|
|
|
"github.com/hashicorp/vault/helper/locksutil"
|
2016-08-26 14:27:06 +00:00
|
|
|
log "github.com/mgutz/logxi/v1"
|
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
|
2016-11-01 14:24:35 +00:00
|
|
|
DefaultCacheSize = 32 * 1024
|
2015-04-14 18:00:51 +00:00
|
|
|
)
|
|
|
|
|
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 {
|
2017-12-06 17:25:17 +00:00
|
|
|
backend Backend
|
|
|
|
lru *lru.TwoQueueCache
|
|
|
|
locks []*locksutil.LockEntry
|
|
|
|
exceptions *iradix.Tree
|
|
|
|
logger log.Logger
|
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
|
|
|
}
|
|
|
|
|
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.
|
2017-12-06 17:25:17 +00:00
|
|
|
func NewCache(b Backend, size int, coreExceptions []string, logger log.Logger) *Cache {
|
|
|
|
if logger.IsTrace() {
|
|
|
|
logger.Trace("physical/cache: creating LRU cache", "size", size)
|
|
|
|
}
|
2015-04-14 18:00:51 +00:00
|
|
|
if size <= 0 {
|
|
|
|
size = DefaultCacheSize
|
|
|
|
}
|
2017-12-06 17:25:17 +00:00
|
|
|
cacheExceptions := iradix.New()
|
|
|
|
for _, key := range coreExceptions {
|
|
|
|
cacheValue := true
|
|
|
|
if strings.HasPrefix(key, "!") {
|
|
|
|
key = strings.TrimPrefix(key, "!")
|
|
|
|
cacheValue = false
|
|
|
|
}
|
|
|
|
cacheExceptions, _, _ = cacheExceptions.Insert([]byte(key), cacheValue)
|
2016-08-26 14:27:06 +00:00
|
|
|
}
|
2017-12-06 17:25:17 +00:00
|
|
|
|
2016-01-07 14:21:33 +00:00
|
|
|
cache, _ := lru.New2Q(size)
|
2015-04-14 18:03:18 +00:00
|
|
|
c := &Cache{
|
2017-12-06 17:25:17 +00:00
|
|
|
backend: b,
|
|
|
|
lru: cache,
|
|
|
|
locks: locksutil.CreateLocks(),
|
|
|
|
exceptions: cacheExceptions,
|
|
|
|
logger: logger,
|
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
|
|
|
|
2017-12-06 17:25:17 +00:00
|
|
|
func NewTransactionalCache(b Backend, size int, coreExceptions []string, logger log.Logger) *TransactionalCache {
|
2017-08-09 00:47:14 +00:00
|
|
|
c := &TransactionalCache{
|
2017-12-06 17:25:17 +00:00
|
|
|
Cache: NewCache(b, size, coreExceptions, logger),
|
2017-08-09 00:47:14 +00:00
|
|
|
Transactional: b.(Transactional),
|
|
|
|
}
|
2015-04-14 18:00:51 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Purge is used to clear the cache
|
2015-04-14 18:03:18 +00:00
|
|
|
func (c *Cache) Purge() {
|
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()
|
|
|
|
}
|
|
|
|
|
2015-04-14 18:03:18 +00:00
|
|
|
func (c *Cache) Put(entry *Entry) error {
|
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()
|
|
|
|
defer lock.Unlock()
|
|
|
|
|
2015-04-14 18:00:51 +00:00
|
|
|
err := c.backend.Put(entry)
|
2017-12-06 17:25:17 +00:00
|
|
|
if err == nil && c.shouldCache(entry.Key) {
|
2016-10-28 16:55:56 +00:00
|
|
|
c.lru.Add(entry.Key, entry)
|
|
|
|
}
|
2015-04-14 18:00:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-14 18:03:18 +00:00
|
|
|
func (c *Cache) Get(key string) (*Entry, error) {
|
2017-03-07 16:21:32 +00:00
|
|
|
lock := locksutil.LockForKey(c.locks, key)
|
2017-02-17 14:15:35 +00:00
|
|
|
lock.RLock()
|
|
|
|
defer lock.RUnlock()
|
|
|
|
|
2017-03-03 21:04:31 +00:00
|
|
|
// We do NOT cache negative results for keys in the 'core/' prefix
|
|
|
|
// otherwise we risk certain race conditions upstream. The primary issue is
|
|
|
|
// with the HA mode, we could potentially negatively cache the leader entry
|
|
|
|
// and cause leader discovery to fail.
|
2017-12-06 17:25:17 +00:00
|
|
|
if !c.shouldCache(key) {
|
2017-03-03 21:04:31 +00:00
|
|
|
return c.backend.Get(key)
|
|
|
|
}
|
|
|
|
|
2015-04-14 18:00:51 +00:00
|
|
|
// Check the LRU first
|
|
|
|
if raw, ok := c.lru.Get(key); ok {
|
|
|
|
if raw == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2017-12-06 17:25:17 +00:00
|
|
|
return raw.(*Entry), nil
|
2015-04-14 18:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read from the underlying backend
|
|
|
|
ent, err := c.backend.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-03-03 21:04:31 +00:00
|
|
|
// Cache the result
|
|
|
|
if ent != nil {
|
2015-04-15 20:48:49 +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
|
|
|
}
|
|
|
|
|
2015-04-14 18:03:18 +00:00
|
|
|
func (c *Cache) Delete(key string) error {
|
2017-03-07 16:21:32 +00:00
|
|
|
lock := locksutil.LockForKey(c.locks, key)
|
2017-02-17 14:15:35 +00:00
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
|
2015-04-14 18:00:51 +00:00
|
|
|
err := c.backend.Delete(key)
|
2017-12-06 17:25:17 +00:00
|
|
|
if err == nil && c.shouldCache(key) {
|
2016-10-28 16:55:56 +00:00
|
|
|
c.lru.Remove(key)
|
|
|
|
}
|
2015-04-14 18:00:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-14 18:03:18 +00:00
|
|
|
func (c *Cache) List(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.
|
2015-04-14 18:00:51 +00:00
|
|
|
return c.backend.List(prefix)
|
|
|
|
}
|
2017-02-17 14:15:35 +00:00
|
|
|
|
2017-10-23 20:42:56 +00:00
|
|
|
func (c *TransactionalCache) Transaction(txns []*TxnEntry) error {
|
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()
|
|
|
|
defer l.Unlock()
|
2017-02-17 14:15:35 +00:00
|
|
|
}
|
|
|
|
|
2017-08-09 00:47:14 +00:00
|
|
|
if err := c.Transactional.Transaction(txns); err != nil {
|
2017-02-17 14:15:35 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, txn := range txns {
|
2017-12-06 17:25:17 +00:00
|
|
|
if c.shouldCache(txn.Entry.Key) {
|
|
|
|
switch txn.Operation {
|
|
|
|
case PutOperation:
|
|
|
|
c.lru.Add(txn.Entry.Key, txn.Entry)
|
|
|
|
case DeleteOperation:
|
|
|
|
c.lru.Remove(txn.Entry.Key)
|
|
|
|
}
|
2017-02-17 14:15:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2017-12-06 17:25:17 +00:00
|
|
|
|
|
|
|
// shouldCache checks for any cache exceptions
|
|
|
|
func (c *Cache) shouldCache(key string) bool {
|
|
|
|
// prefix match if nested under core/
|
|
|
|
if strings.HasPrefix(key, "core/") {
|
|
|
|
if prefix, val, found := c.exceptions.Root().LongestPrefix([]byte(key)); found {
|
|
|
|
strPrefix := string(prefix)
|
|
|
|
if strings.HasSuffix(strPrefix, "/") || strPrefix == key {
|
|
|
|
return val.(bool)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// default for core/ values is false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// default is true
|
|
|
|
return true
|
|
|
|
}
|