Skip to content

Commit

Permalink
go/storage: Make maximum in-memory storage cache size configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Dec 28, 2019
1 parent dbb0c72 commit 067af88
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changelog/2494.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Make maximum in-memory cache size for runtime storage configurable.

Previously the value of 64mb was always used as the size of the in-memory storage cache. This adds a
new configuration parameter/command-line flag `--storage.max_cache_size` which configures the
maximum size of the in-memory runtime storage cache.
1 change: 1 addition & 0 deletions go/oasis-node/cmd/debug/storage/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ func newDirectStorageBackend(dataDir string, namespace common.Namespace) (storag
DB: dataDir,
ApplyLockLRUSlots: uint64(viper.GetInt(storage.CfgLRUSlots)),
Namespace: namespace,
MaxCacheSize: int64(viper.GetSizeInBytes(storage.CfgMaxCacheSize)),
}

b := strings.ToLower(viper.GetString(storage.CfgBackend))
Expand Down
8 changes: 6 additions & 2 deletions go/storage/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,17 @@ type Config struct {

// Namespace is the namespace contained within the database.
Namespace common.Namespace

// MaxCacheSize is the maximum in-memory cache size for the database.
MaxCacheSize int64
}

// ToNodeDB converts from a Config to a node DB Config.
func (cfg *Config) ToNodeDB() *nodedb.Config {
return &nodedb.Config{
DB: cfg.DB,
Namespace: cfg.Namespace,
DB: cfg.DB,
Namespace: cfg.Namespace,
MaxCacheSize: cfg.MaxCacheSize,
}
}

Expand Down
5 changes: 3 additions & 2 deletions go/storage/crashing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ func TestCrashingBackendDoNotInterfere(t *testing.T) {

var (
cfg = api.Config{
Backend: database.BackendNameBadgerDB,
Namespace: testNs,
Backend: database.BackendNameBadgerDB,
Namespace: testNs,
MaxCacheSize: 16 * 1024 * 1024,
}
err error
)
Expand Down
1 change: 1 addition & 0 deletions go/storage/database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func doTestImpl(t *testing.T, backend string) {
Backend: backend,
ApplyLockLRUSlots: 100,
Namespace: testNs,
MaxCacheSize: 16 * 1024 * 1024,
}
err error
)
Expand Down
13 changes: 10 additions & 3 deletions go/storage/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ import (

const (
// CfgBackend configures the storage backend flag.
CfgBackend = "storage.backend"
cfgCrashEnabled = "storage.crash.enabled"
CfgBackend = "storage.backend"

// CfgLRUSlots configures the LRU apply lock slots.
CfgLRUSlots = "storage.root_cache.apply_lock_lru_slots"
CfgLRUSlots = "storage.root_cache.apply_lock_lru_slots"

// CfgMaxCacheSize configures the maximum in-memory cache size.
CfgMaxCacheSize = "storage.max_cache_size"

cfgCrashEnabled = "storage.crash.enabled"
cfgInsecureSkipChecks = "storage.debug.insecure_skip_checks"
)

Expand All @@ -48,6 +53,7 @@ func New(
ApplyLockLRUSlots: uint64(viper.GetInt(CfgLRUSlots)),
InsecureSkipChecks: viper.GetBool(cfgInsecureSkipChecks) && cmdFlags.DebugDontBlameOasis(),
Namespace: namespace,
MaxCacheSize: int64(viper.GetSizeInBytes(CfgMaxCacheSize)),
}

var (
Expand Down Expand Up @@ -80,6 +86,7 @@ func init() {
Flags.String(CfgBackend, database.BackendNameBadgerDB, "Storage backend")
Flags.Bool(cfgCrashEnabled, false, "Enable the crashing storage wrapper")
Flags.Int(CfgLRUSlots, 1000, "How many LRU slots to use for Apply call locks in the MKVS tree root cache")
Flags.String(CfgMaxCacheSize, "64mb", "Maximum in-memory cache size")

Flags.Bool(cfgInsecureSkipChecks, false, "INSECURE: Skip known root checks")

Expand Down
3 changes: 3 additions & 0 deletions go/storage/mkvs/urkel/db/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ type Config struct {

// Namespace is the namespace contained within the database.
Namespace common.Namespace

// MaxCacheSize is the maximum in-memory cache size for the database.
MaxCacheSize int64
}

// NodeDB is the persistence layer used for persisting the in-memory tree.
Expand Down
3 changes: 1 addition & 2 deletions go/storage/mkvs/urkel/db/badger/badger.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ func New(cfg *api.Config) (api.NodeDB, error) {
opts = opts.WithLogger(cmnBadger.NewLogAdapter(db.logger))
opts = opts.WithSyncWrites(!cfg.DebugNoFsync)
opts = opts.WithCompression(options.None)
// Reduce cache size to 64 MiB as the default is 1 GiB.
opts = opts.WithMaxCacheSize(64 * 1024 * 1024)
opts = opts.WithMaxCacheSize(cfg.MaxCacheSize)

var err error
if db.db, err = badger.Open(opts); err != nil {
Expand Down
1 change: 1 addition & 0 deletions go/storage/mkvs/urkel/interop/cmd/protocol_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func doProtoServer(cmd *cobra.Command, args []string) {
Signer: ident.NodeSigner,
ApplyLockLRUSlots: 1,
InsecureSkipChecks: false,
MaxCacheSize: 16 * 1024 * 1024,
}
backend, err := database.New(&storageCfg)
if err != nil {
Expand Down
17 changes: 10 additions & 7 deletions go/storage/mkvs/urkel/urkel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1887,6 +1887,7 @@ func TestUrkelBadgerBackend(t *testing.T) {
DB: dir,
DebugNoFsync: true,
Namespace: testNs,
MaxCacheSize: 16 * 1024 * 1024,
})
require.NoError(t, err, "New")

Expand Down Expand Up @@ -1942,7 +1943,9 @@ func benchmarkInsertBatch(b *testing.B, numValues int, commit bool) {
require.NoError(b, err, "TempDir")
defer os.RemoveAll(dir)
ndb, err := badgerDb.New(&db.Config{
DB: dir,
DB: dir,
Namespace: testNs,
MaxCacheSize: 16 * 1024 * 1024,
})
require.NoError(b, err, "New")
tree := New(nil, ndb)
Expand Down Expand Up @@ -1975,12 +1978,6 @@ func generateKeyValuePairs() ([][]byte, [][]byte) {
return generateKeyValuePairsEx("", insertItems)
}

func init() {
var ns hash.Hash
ns.FromBytes([]byte("oasis urkel test ns"))
copy(testNs[:], ns[:])
}

func generateLongKeyValuePairs() ([][]byte, [][]byte) {
keys := make([][]byte, len(longKey))
values := make([][]byte, len(longKey))
Expand Down Expand Up @@ -2013,3 +2010,9 @@ func generatePopulatedTree(t *testing.T, ndb db.NodeDB) ([][]byte, [][]byte, nod
}
return keys, values, root, tree
}

func init() {
var ns hash.Hash
ns.FromBytes([]byte("oasis urkel test ns"))
copy(testNs[:], ns[:])
}

0 comments on commit 067af88

Please sign in to comment.