Plumb through the ability to set the storage read cache size. (#1784)

Plumb through the ability to set the storage read cache size.

Fixes #1772
This commit is contained in:
Jeff Mitchell 2016-08-26 10:27:06 -04:00 committed by GitHub
parent 8612b5203d
commit 2ce4397deb
8 changed files with 30 additions and 11 deletions

View File

@ -219,6 +219,7 @@ func (c *ServerCommand) Run(args []string) int {
MaxLeaseTTL: config.MaxLeaseTTL,
DefaultLeaseTTL: config.DefaultLeaseTTL,
ClusterName: config.ClusterName,
CacheSize: config.CacheSize,
}
var disableClustering bool

View File

@ -26,6 +26,7 @@ type Config struct {
Backend *Backend `hcl:"-"`
HABackend *Backend `hcl:"-"`
CacheSize int `hcl:"cache_size"`
DisableCache bool `hcl:"disable_cache"`
DisableMlock bool `hcl:"disable_mlock"`
@ -200,6 +201,11 @@ func (c *Config) Merge(c2 *Config) *Config {
result.Telemetry = c2.Telemetry
}
result.CacheSize = c.CacheSize
if c2.CacheSize != 0 {
result.CacheSize = c2.CacheSize
}
// merging these booleans via an OR operation
result.DisableCache = c.DisableCache
if c2.DisableCache {
@ -289,6 +295,7 @@ func ParseConfig(d string, logger log.Logger) (*Config, error) {
"backend",
"ha_backend",
"listener",
"cache_size",
"disable_cache",
"disable_mlock",
"telemetry",

View File

@ -176,6 +176,8 @@ func TestLoadConfigFile_json2(t *testing.T) {
},
},
CacheSize: 45678,
Telemetry: &Telemetry{
StatsiteAddr: "foo",
StatsdAddr: "bar",

View File

@ -22,6 +22,7 @@
"disable_clustering": "false"
}
},
"cache_size": 45678,
"telemetry":{
"statsd_address":"bar",
"statsite_address":"foo",

View File

@ -4,11 +4,12 @@ import (
"strings"
"github.com/hashicorp/golang-lru"
log "github.com/mgutz/logxi/v1"
)
const (
// DefaultCacheSize is used if no cache size is specified for NewCache
DefaultCacheSize = 32 * 1024
DefaultCacheSize = 1024 * 1024
)
// Cache is used to wrap an underlying physical backend
@ -22,10 +23,13 @@ type Cache struct {
// NewCache returns a physical cache of the given size.
// If no size is provided, the default size is used.
func NewCache(b Backend, size int) *Cache {
func NewCache(b Backend, size int, logger log.Logger) *Cache {
if size <= 0 {
size = DefaultCacheSize
}
if logger.IsTrace() {
logger.Trace("physical/cache: creating LRU cache", "size", size)
}
cache, _ := lru.New2Q(size)
c := &Cache{
backend: b,

View File

@ -11,7 +11,7 @@ func TestCache(t *testing.T) {
logger := logformat.NewVaultLogger(log.LevelTrace)
inm := NewInmem(logger)
cache := NewCache(inm, 0)
cache := NewCache(inm, 0, logger)
testBackend(t, cache)
testBackend_ListPrefix(t, cache)
}
@ -20,7 +20,7 @@ func TestCache_Purge(t *testing.T) {
logger := logformat.NewVaultLogger(log.LevelTrace)
inm := NewInmem(logger)
cache := NewCache(inm, 0)
cache := NewCache(inm, 0, logger)
ent := &Entry{
Key: "foo",

View File

@ -308,7 +308,7 @@ type CoreConfig struct {
// Disables mlock syscall
DisableMlock bool `json:"disable_mlock" structs:"disable_mlock" mapstructure:"disable_mlock"`
// Custom cache size of zero for default
// Custom cache size for the LRU cache on the physical backend, or zero for default
CacheSize int `json:"cache_size" structs:"cache_size" mapstructure:"cache_size"`
// Set as the leader address for HA
@ -354,12 +354,17 @@ func NewCore(conf *CoreConfig) (*Core, error) {
}
}
// Make a default logger if not provided
if conf.Logger == nil {
conf.Logger = logformat.NewVaultLogger(log.LevelTrace)
}
// Wrap the backend in a cache unless disabled
if !conf.DisableCache {
_, isCache := conf.Physical.(*physical.Cache)
_, isInmem := conf.Physical.(*physical.InmemBackend)
if !isCache && !isInmem {
cache := physical.NewCache(conf.Physical, conf.CacheSize)
cache := physical.NewCache(conf.Physical, conf.CacheSize, conf.Logger)
conf.Physical = cache
}
}
@ -386,11 +391,6 @@ func NewCore(conf *CoreConfig) (*Core, error) {
return nil, fmt.Errorf("barrier setup failed: %v", err)
}
// Make a default logger if not provided
if conf.Logger == nil {
conf.Logger = logformat.NewVaultLogger(log.LevelTrace)
}
// Setup the core
c := &Core{
redirectAddr: conf.RedirectAddr,

View File

@ -50,6 +50,10 @@ sending a SIGHUP to the server process. These are denoted below.
"tcp" is currently the only option available. A full reference for the
inner syntax is below.
* `cache_size` (optional) - If set, the size of the read cache used
by the physical storage subsystem will be set to this value, in bytes.
Defaults to 1048576 (1MB).
* `disable_cache` (optional) - A boolean. If true, this will disable all caches
within Vault, including the read cache used by the physical storage
subsystem. This will very significantly impact performance.