forked from dgraph-io/badger
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New tools, new option (dgraph-io#645)
- Add a new flatten command, which would flatten out all the LSM tree levels into a single level. It ensures that that level does not exceed its max size limits, as specified in the options. - Add a new fill command, which would random key-values to Badger. Useful for testing. ## Option updates - Add a new option to skip force compaction of Level 0 on DB.Close. - Decrease the NumCompactors default value from 3 to 2. Commits: * Add two new commands: flatten and fill. * Add an option to skip compacting Level 0 for performance reasons. * Allow more compactors to run for flatten. Performance wise I didn't see any gains in latency while using multiple compactors. And there's a lot of resource consumption if multiple compactors are running concurrently. Could be due to all compactors working on the same level during flatten. In a live setting, compactors could be working on different levels, so the result might not directly apply. * Decreased the NumCompactors default option to 2. Didn't set it to 1 to avoid a straggler compactor blocking all compactions. * Add licenses.
- Loading branch information
1 parent
2d7e014
commit 7e41bba
Showing
7 changed files
with
246 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright 2018 Dgraph Labs, Inc. and Contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"crypto/rand" | ||
"time" | ||
|
||
"github.com/dgraph-io/badger" | ||
"github.com/dgraph-io/badger/y" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var fillCmd = &cobra.Command{ | ||
Use: "fill", | ||
Short: "Fill Badger with random data.", | ||
Long: ` | ||
This command would fill Badger with random data. Useful for testing and performance analysis. | ||
`, | ||
RunE: fill, | ||
} | ||
|
||
var keySz, valSz int | ||
var numKeys float64 | ||
var force bool | ||
|
||
const mil float64 = 1e6 | ||
|
||
func init() { | ||
RootCmd.AddCommand(fillCmd) | ||
fillCmd.Flags().IntVarP(&keySz, "key-size", "k", 32, "Size of key") | ||
fillCmd.Flags().IntVarP(&valSz, "val-size", "v", 128, "Size of value") | ||
fillCmd.Flags().Float64VarP(&numKeys, "keys-mil", "m", 10.0, | ||
"Number of keys to add in millions") | ||
fillCmd.Flags().BoolVarP(&force, "force-compact", "f", true, "Force compact level 0 on close.") | ||
} | ||
|
||
func fill(cmd *cobra.Command, args []string) error { | ||
opts := badger.DefaultOptions | ||
opts.Dir = sstDir | ||
opts.ValueDir = vlogDir | ||
opts.Truncate = truncate | ||
opts.SyncWrites = false | ||
opts.CompactL0OnClose = force | ||
|
||
db, err := badger.Open(opts) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
start := time.Now() | ||
err := db.Close() | ||
badger.Infof("DB.Close. Error: %v. Time taken: %s", err, time.Since(start)) | ||
}() | ||
|
||
start := time.Now() | ||
batch := db.NewWriteBatch() | ||
num := int64(numKeys * mil) | ||
for i := int64(1); i <= num; i++ { | ||
k := make([]byte, keySz) | ||
v := make([]byte, valSz) | ||
y.Check2(rand.Read(k)) | ||
y.Check2(rand.Read(v)) | ||
if err := batch.Set(k, v, 0); err != nil { | ||
return err | ||
} | ||
if i%1e5 == 0 { | ||
badger.Infof("Written keys: %d\n", i) | ||
} | ||
} | ||
if err := batch.Flush(); err != nil { | ||
return err | ||
} | ||
badger.Infof("%d keys written. Time taken: %s\n", num, time.Since(start)) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2018 Dgraph Labs, Inc. and Contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/dgraph-io/badger" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var flattenCmd = &cobra.Command{ | ||
Use: "flatten", | ||
Short: "Flatten the LSM tree.", | ||
Long: ` | ||
This command would compact all the LSM tables into one level. | ||
`, | ||
RunE: flatten, | ||
} | ||
|
||
var numWorkers int | ||
|
||
func init() { | ||
RootCmd.AddCommand(flattenCmd) | ||
flattenCmd.Flags().IntVarP(&numWorkers, "num-workers", "w", 1, | ||
"Number of concurrent compactors to run. More compactors would use more"+ | ||
" server resources to potentially achieve faster compactions.") | ||
} | ||
|
||
func flatten(cmd *cobra.Command, args []string) error { | ||
opts := badger.DefaultOptions | ||
opts.Dir = sstDir | ||
opts.ValueDir = vlogDir | ||
opts.Truncate = truncate | ||
opts.NumCompactors = 0 | ||
|
||
db, err := badger.Open(opts) | ||
if err != nil { | ||
return err | ||
} | ||
defer db.Close() | ||
|
||
return db.Flatten(numWorkers) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.