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 1 commit
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
39 changes: 24 additions & 15 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 @@ -477,7 +482,9 @@ func (db *DB) close() (err error) {
db.elog.Printf("Waiting for closer")
db.closers.updateSize.SignalAndWait()
db.orc.Stop()
db.blockCache.Close()
if db.blockCache != nil {
db.blockCache.Close()
}

db.elog.Finish()
if db.opt.InMemory {
Expand Down Expand Up @@ -1500,7 +1507,9 @@ func (db *DB) dropAll() (func(), error) {
db.vhead = valuePointer{} // Zero it out.
db.lc.nextFileID = 1
db.opt.Infof("Deleted %d value log files. DropAll done.\n", num)
db.blockCache.Clear()
if db.blockCache != nil {
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 @@ -289,6 +289,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