Skip to content

Commit

Permalink
Alpha: Expose compression_level option (#5278) (#5279)
Browse files Browse the repository at this point in the history
This PR exposes the `compression_level` flag in `alpha`. The flag can be set as
```
dgraph alpha --badger.compression_level=xxx
```
A higher value of the compression level is more CPU intensive but
offers a better compression ratio. The default value is `3`.

`compression_level=0` disables compression.

(cherry picked from commit 7442993)
  • Loading branch information
Ibrahim Jarif authored Apr 23, 2020
1 parent 0028bdd commit 061ce2a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
9 changes: 6 additions & 3 deletions dgraph/cmd/alpha/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ they form a Raft group and provide synchronous replication.
flag.String("badger.vlog", "mmap",
"[mmap, disk] Specifies how Badger Value log is stored."+
" mmap consumes more RAM, but provides better performance.")
flag.Int("badger.compression_level", 3,
"The compression level for Badger. A higher value uses more resources.")
flag.String("encryption_key_file", "",
"The file that stores the encryption key. The key size must be 16, 24, or 32 bytes long. "+
"The key size determines the corresponding block size for AES encryption "+
Expand Down Expand Up @@ -510,9 +512,10 @@ func run() {
bindall = Alpha.Conf.GetBool("bindall")

opts := worker.Options{
BadgerTables: Alpha.Conf.GetString("badger.tables"),
BadgerVlog: Alpha.Conf.GetString("badger.vlog"),
BadgerKeyFile: Alpha.Conf.GetString("encryption_key_file"),
BadgerTables: Alpha.Conf.GetString("badger.tables"),
BadgerVlog: Alpha.Conf.GetString("badger.vlog"),
BadgerKeyFile: Alpha.Conf.GetString("encryption_key_file"),
BadgerCompressionLevel: Alpha.Conf.GetInt("badger.compression_level"),

PostingDir: Alpha.Conf.GetString("postings"),
WALDir: Alpha.Conf.GetString("wal"),
Expand Down
4 changes: 4 additions & 0 deletions worker/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ type Options struct {
BadgerVlog string
// BadgerKeyFile is the file containing the key used for encryption. Enterprise only feature.
BadgerKeyFile string
// BadgerCompressionLevel is the ZSTD compression level used by badger. A
// higher value means more CPU intensive compression and better compression
// ratio.
BadgerCompressionLevel int
// WALDir is the path to the directory storing the write-ahead log.
WALDir string
// MutationsMode is the mode used to handle mutation requests.
Expand Down
22 changes: 12 additions & 10 deletions worker/server_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,18 @@ func (s *ServerState) runVlogGC(store *badger.DB) {
func setBadgerOptions(opt badger.Options) badger.Options {
opt = opt.WithSyncWrites(false).WithTruncate(true).WithLogger(&x.ToGlog{}).
WithEncryptionKey(enc.ReadEncryptionKeyFile(Config.BadgerKeyFile))
// TODO(ibrahim): Remove this once badger is updated in dgraph.
opt.ZSTDCompressionLevel = 1

// Do not load bloom filters on DB open.
opt.LoadBloomsOnOpen = false

glog.Infof("Setting Badger Compression Level: %d", Config.BadgerCompressionLevel)
// Default value of badgerCompressionLevel is 3 so compression will always
// be enabled, unless it is explicitly disabled by setting the value to 0.
if Config.BadgerCompressionLevel != 0 {
// By default, compression is disabled in badger.
opt.Compression = options.ZSTD
opt.ZSTDCompressionLevel = Config.BadgerCompressionLevel
}

glog.Infof("Setting Badger table load option: %s", Config.BadgerTables)
switch Config.BadgerTables {
Expand Down Expand Up @@ -160,10 +170,6 @@ func (s *ServerState) initStorage() {
glog.Infof("Opening write-ahead log BadgerDB with options: %+v\n", opt)
opt.EncryptionKey = key

opt.ZSTDCompressionLevel = 3
opt.Compression = options.ZSTD
opt.LoadBloomsOnOpen = false

s.WALstore, err = badger.Open(opt)
x.Checkf(err, "Error while creating badger KV WAL store")
}
Expand All @@ -183,10 +189,6 @@ func (s *ServerState) initStorage() {
glog.Infof("Opening postings BadgerDB with options: %+v\n", opt)
opt.EncryptionKey = key

opt.ZSTDCompressionLevel = 3
opt.Compression = options.ZSTD
opt.LoadBloomsOnOpen = false

s.Pstore, err = badger.OpenManaged(opt)
x.Checkf(err, "Error while creating badger KV posting store")

Expand Down

0 comments on commit 061ce2a

Please sign in to comment.