Skip to content

Commit

Permalink
Use different option for max mem-table size and max table-file size. (#…
Browse files Browse the repository at this point in the history
…167)

So we can set the table file to be much smaller to reduce compaction cost.
  • Loading branch information
coocood authored Feb 24, 2020
1 parent d33a0d1 commit 264a464
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 19 deletions.
2 changes: 2 additions & 0 deletions blob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestBlob(t *testing.T) {
opts := getTestOptions(dir)
opts.ValueThreshold = 20
opts.MaxTableSize = 4 * 1024
opts.MaxMemTableSize = 4 * 1024
opts.NumMemtables = 2
opts.NumLevelZeroTables = 1
opts.NumLevelZeroTablesStall = 2
Expand Down Expand Up @@ -70,6 +71,7 @@ func TestBlobGC(t *testing.T) {
opts := getTestOptions(dir)
opts.ValueThreshold = 20
opts.MaxTableSize = 6 * 1024
opts.MaxMemTableSize = 6 * 1024
opts.NumMemtables = 2
opts.NumLevelZeroTables = 1
opts.NumLevelZeroTablesStall = 2
Expand Down
10 changes: 5 additions & 5 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func replayFunction(out *DB) func(Entry) error {
toLSM := func(nk []byte, vs y.ValueStruct) {
e := table.Entry{Key: nk, Value: vs}
mTbls := out.mtbls.Load().(*memTables)
if out.ensureRoomForWrite(mTbls.getMutable(), e.EstimateSize()) == out.opt.MaxTableSize {
if out.ensureRoomForWrite(mTbls.getMutable(), e.EstimateSize()) == out.opt.MaxMemTableSize {
mTbls = out.mtbls.Load().(*memTables)
}
mTbls.getMutable().PutToSkl(nk, vs)
Expand Down Expand Up @@ -190,7 +190,7 @@ func replayFunction(out *DB) func(Entry) error {

// Open returns a new DB object.
func Open(opt Options) (db *DB, err error) {
opt.maxBatchSize = (15 * opt.MaxTableSize) / 100
opt.maxBatchSize = (15 * opt.MaxMemTableSize) / 100
opt.maxBatchCount = opt.maxBatchSize / int64(skl.MaxNodeSize)

if opt.ValueThreshold > math.MaxUint16-16 {
Expand Down Expand Up @@ -773,12 +773,12 @@ func (db *DB) batchSetAsync(entries []*Entry, f func(error)) error {

// ensureRoomForWrite is always called serially.
func (db *DB) ensureRoomForWrite(mt *table.MemTable, minSize int64) int64 {
free := db.opt.MaxTableSize - mt.MemSize()
free := db.opt.MaxMemTableSize - mt.MemSize()
if free >= minSize {
return free
}
_ = db.flushMemTable()
return db.opt.MaxTableSize
return db.opt.MaxMemTableSize
}

func (db *DB) flushMemTable() *sync.WaitGroup {
Expand All @@ -795,7 +795,7 @@ func (db *DB) flushMemTable() *sync.WaitGroup {
}

func arenaSize(opt Options) int64 {
return opt.MaxTableSize + opt.maxBatchCount*int64(skl.MaxNodeSize)
return opt.MaxMemTableSize + opt.maxBatchCount*int64(skl.MaxNodeSize)
}

// WriteLevel0Table flushes memtable. It drops deleteValues.
Expand Down
7 changes: 5 additions & 2 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ func getTestCompression(tp options.CompressionType) []options.CompressionType {

func getTestOptions(dir string) Options {
opt := DefaultOptions
opt.MaxTableSize = 4 << 15 // Force more compaction.
opt.LevelOneSize = 4 << 15 // Force more compaction.
opt.MaxTableSize = 4 << 15 // Force more compaction.
opt.MaxMemTableSize = 4 << 15 // Force more compaction.
opt.LevelOneSize = 4 << 15 // Force more compaction.
opt.Dir = dir
opt.ValueDir = dir
opt.SyncWrites = false
Expand Down Expand Up @@ -1158,6 +1159,7 @@ func TestCompactionFilter(t *testing.T) {
opts := getTestOptions(dir)
opts.ValueThreshold = 8 * 1024
opts.MaxTableSize = 32 * 1024
opts.MaxMemTableSize = 32 * 1024
opts.NumMemtables = 2
opts.NumLevelZeroTables = 1
opts.NumLevelZeroTablesStall = 2
Expand Down Expand Up @@ -1293,6 +1295,7 @@ func TestIterateVLog(t *testing.T) {
defer os.RemoveAll(dir)
opts := getTestOptions(dir)
opts.MaxTableSize = 1 << 20
opts.MaxMemTableSize = 1 << 20
opts.ValueLogFileSize = 1 << 20
opts.ValueThreshold = 1000
opts.ValueLogMaxNumFiles = 1000
Expand Down
16 changes: 6 additions & 10 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ type Options struct {
// 3. Flags that user might want to review
// ----------------------------------------
// The following affect all levels of LSM tree.
MaxTableSize int64 // Each table (or file) is at most this size.
MaxMemTableSize int64 // Each mem table is at most this size.
MaxTableSize int64 // Each table file is at most this size.
// If value size >= this threshold, only store value offsets in tree.
// If set to 0, all values are stored in SST.
ValueThreshold int
Expand Down Expand Up @@ -78,9 +79,6 @@ type Options struct {
// Number of compaction workers to run concurrently.
NumCompactors int

// Max number of sub compaction, set 1 or 0 to disable sub compaction.
MaxSubCompaction int

// Transaction start and commit timestamps are manaVgedTxns by end-user. This
// is a private option used by ManagedDB.
managedTxns bool
Expand Down Expand Up @@ -145,13 +143,11 @@ const (
// DefaultOptions sets a list of recommended options for good performance.
// Feel free to modify these to suit your needs.
var DefaultOptions = Options{
DoNotCompact: false,
LevelOneSize: 256 << 20,
// table.MemoryMap to mmap() the tables.
// table.Nothing to not preload the tables.
MaxTableSize: 64 << 20,
DoNotCompact: false,
LevelOneSize: 256 << 20,
MaxMemTableSize: 64 << 20,
MaxTableSize: 8 << 20,
NumCompactors: 3,
MaxSubCompaction: 3,
NumLevelZeroTables: 5,
NumLevelZeroTablesStall: 10,
NumMemtables: 5,
Expand Down
2 changes: 1 addition & 1 deletion transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func (txn *Txn) checkSize(e *Entry) error {
}
// Extra bytes for version in key.
size := int64(e.estimateSize()) + 10
if size >= txn.db.opt.MaxTableSize {
if size >= txn.db.opt.MaxMemTableSize {
return ErrTxnTooBig
}
txn.count++
Expand Down
1 change: 1 addition & 0 deletions transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,7 @@ func TestArmV7Issue311Fix(t *testing.T) {
config.ValueLogFileSize = 16 << 20
config.LevelOneSize = 8 << 20
config.MaxTableSize = 2 << 20
config.MaxMemTableSize = 2 << 20
config.Dir = dir
config.ValueDir = dir
config.SyncWrites = false
Expand Down
2 changes: 1 addition & 1 deletion writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func (w *writeWorker) writeToLSM(entries []*Entry) error {
for len(entries) != 0 {
e := newEntry(entries[0])
free := w.ensureRoomForWrite(mTbls.getMutable(), e.EstimateSize())
if free == w.opt.MaxTableSize {
if free == w.opt.MaxMemTableSize {
mTbls = w.mtbls.Load().(*memTables)
}

Expand Down

0 comments on commit 264a464

Please sign in to comment.