Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support disabling the cache completely. (#1183) #1185

Merged
merged 3 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,17 +278,6 @@ func Open(opt Options) (db *DB, err error) {
elog = trace.NewEventLog("Badger", "DB")
}

config := ristretto.Config{
// Use 5% of cache memory for storing counters.
NumCounters: int64(float64(opt.MaxCacheSize) * 0.05 * 2),
MaxCost: int64(float64(opt.MaxCacheSize) * 0.95),
BufferItems: 64,
Metrics: true,
}
cache, err := ristretto.NewCache(&config)
if err != nil {
return nil, errors.Wrap(err, "failed to create cache")
}
db = &DB{
imm: make([]*skl.Skiplist, 0, opt.NumMemtables),
flushChan: make(chan flushTask, opt.NumMemtables),
Expand All @@ -300,7 +289,20 @@ func Open(opt Options) (db *DB, err error) {
valueDirGuard: valueDirLockGuard,
orc: newOracle(opt),
pub: newPublisher(),
blockCache: cache,
}

if opt.MaxCacheSize > 0 {
config := ristretto.Config{
// Use 5% of cache memory for storing counters.
NumCounters: int64(float64(opt.MaxCacheSize) * 0.05 * 2),
MaxCost: int64(float64(opt.MaxCacheSize) * 0.95),
BufferItems: 64,
Metrics: true,
}
db.blockCache, err = ristretto.NewCache(&config)
if err != nil {
return nil, errors.Wrap(err, "failed to create cache")
}
}

if db.opt.InMemory {
Expand Down Expand Up @@ -386,7 +388,10 @@ func Open(opt Options) (db *DB, err error) {

// CacheMetrics returns the metrics for the underlying cache.
func (db *DB) CacheMetrics() *ristretto.Metrics {
return db.blockCache.Metrics
if db.blockCache != nil {
return db.blockCache.Metrics
}
return nil
}

// Close closes a DB. It's crucial to call it to ensure all the pending updates make their way to
Expand Down Expand Up @@ -1501,6 +1506,7 @@ func (db *DB) dropAll() (func(), error) {
db.lc.nextFileID = 1
db.opt.Infof("Deleted %d value log files. DropAll done.\n", num)
db.blockCache.Clear()

return resume, nil
}

Expand Down
7 changes: 7 additions & 0 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,13 @@ func TestGet(t *testing.T) {
test(t, db)
require.NoError(t, db.Close())
})
t.Run("cache disabled", func(t *testing.T) {
opts := DefaultOptions("").WithInMemory(true).WithMaxCacheSize(0)
db, err := Open(opts)
require.NoError(t, err)
test(t, db)
require.NoError(t, db.Close())
})
}

func TestGetAfterDelete(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,8 @@ func (opt Options) WithChecksumVerificationMode(cvMode options.ChecksumVerificat
// WithMaxCacheSize returns a new Options value with MaxCacheSize set to the given value.
//
// This value specifies how much data cache should hold in memory. A small size of cache means lower
// memory consumption and lookups/iterations would take longer.
// memory consumption and lookups/iterations would take longer. Setting size to zero disables the
// cache altogether.
func (opt Options) WithMaxCacheSize(size int64) Options {
opt.MaxCacheSize = size
return opt
Expand Down