Skip to content

Commit

Permalink
release/v20.03 - add --cache_mb and --cache_percentage flag (#6287)
Browse files Browse the repository at this point in the history
  • Loading branch information
NamanJain8 authored Aug 27, 2020
1 parent 8c314bb commit 9104512
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 18 deletions.
33 changes: 30 additions & 3 deletions dgraph/cmd/alpha/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,16 @@ they form a Raft group and provide synchronous replication.

flag.Bool("graphql_introspection", true, "Set to false for no GraphQL schema introspection")
flag.Bool("ludicrous_mode", false, "Run alpha in ludicrous mode")

// Cache flags
flag.Int64("cache_mb", 0, "Total size of cache (in MB) to be used in alpha.")
// TODO(Naman): The PostingListCache is a no-op for now. Once the posting list cache is
// added in release branch, use it.
flag.String("cache_percentage", "0,65,25,0,10",
`Cache percentages summing up to 100 for various caches (FORMAT:
PostingListCache,PstoreBlockCache,PstoreIndexCache,WstoreBlockCache,WstoreIndexCache).
PostingListCache should be 0 and is a no-op.
`)
}

func setupCustomTokenizers() {
Expand Down Expand Up @@ -528,12 +538,29 @@ func run() {
}
bindall = Alpha.Conf.GetBool("bindall")

totalCache := int64(Alpha.Conf.GetInt("cache_mb"))
x.AssertTruef(totalCache >= 0, "ERROR: Cache size must be non-negative")

cachePercentage := Alpha.Conf.GetString("cache_percentage")
cachePercent, err := x.GetCachePercentages(cachePercentage, 5)
x.Check(err)
// TODO(Naman): PostingListCache doesn't exist now.
postingListCacheSize := (cachePercent[0] * (totalCache << 20)) / 100
x.AssertTruef(postingListCacheSize == 0, "ERROR: postingListCacheSize should be 0.")
pstoreBlockCacheSize := (cachePercent[1] * (totalCache << 20)) / 100
pstoreIndexCacheSize := (cachePercent[2] * (totalCache << 20)) / 100
wstoreBlockCacheSize := (cachePercent[3] * (totalCache << 20)) / 100
wstoreIndexCacheSize := (cachePercent[4] * (totalCache << 20)) / 100

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

PostingDir: Alpha.Conf.GetString("postings"),
WALDir: Alpha.Conf.GetString("wal"),
PostingDir: Alpha.Conf.GetString("postings"),
WALDir: Alpha.Conf.GetString("wal"),
PBlockCacheSize: pstoreBlockCacheSize,
PIndexCacheSize: pstoreIndexCacheSize,
WBlockCacheSize: wstoreBlockCacheSize,
WIndexCacheSize: wstoreIndexCacheSize,

MutationsMode: worker.AllowMutations,
AuthToken: Alpha.Conf.GetString("auth_token"),
Expand Down
8 changes: 4 additions & 4 deletions dgraph/cmd/bulk/reduce.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ func (r *reducer) run() error {
splitWriter := tmpDb.NewManagedWriteBatch()

ci := &countIndexer{
reducer: r,
writer: writer,
reducer: r,
writer: writer,
splitWriter: splitWriter,
tmpDb: tmpDb,
tmpDb: tmpDb,
}
sort.Slice(partitionKeys, func(i, j int) bool {
return bytes.Compare(partitionKeys[i], partitionKeys[j]) < 0
Expand Down Expand Up @@ -131,7 +131,7 @@ func (r *reducer) createBadgerInternal(dir string, compression bool) *badger.DB

opt := badger.DefaultOptions(dir).WithSyncWrites(false).
WithTableLoadingMode(bo.MemoryMap).WithValueThreshold(1 << 10 /* 1 KB */).
WithLogger(nil).WithMaxCacheSize(1 << 20).
WithLogger(nil).WithBlockCacheSize(1 << 20).
WithEncryptionKey(enc.ReadEncryptionKeyFile(r.opt.BadgerKeyFile)).WithCompression(bo.None)

// Overwrite badger options based on the options provided by the user.
Expand Down
27 changes: 24 additions & 3 deletions dgraph/cmd/zero/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package zero

import (
"context"
// "errors"
"fmt"
"log"
"net"
Expand Down Expand Up @@ -55,6 +54,9 @@ type options struct {
w string
rebalanceInterval time.Duration
LudicrousMode bool

totalCache int64
cachePercentage string
}

var opts options
Expand Down Expand Up @@ -101,6 +103,11 @@ instances to achieve high-availability.
" exporter does not support annotation logs and would discard them.")
flag.Bool("ludicrous_mode", false, "Run zero in ludicrous mode")
flag.String("enterprise_license", "", "Path to the enterprise license file.")

// Cache flags
flag.Int64("cache_mb", 0, "Total size of cache (in MB) to be used in zero.")
flag.String("cache_percentage", "100,0",
"Cache percentages summing up to 100 for various caches (FORMAT: blockCache,indexCache).")
}

func setupListener(addr string, port int, kind string) (listener net.Listener, err error) {
Expand Down Expand Up @@ -183,6 +190,8 @@ func run() {
w: Zero.Conf.GetString("wal"),
rebalanceInterval: Zero.Conf.GetDuration("rebalance_interval"),
LudicrousMode: Zero.Conf.GetBool("ludicrous_mode"),
totalCache: int64(Zero.Conf.GetInt("cache_mb")),
cachePercentage: Zero.Conf.GetString("cache_percentage"),
}

if opts.nodeId == 0 {
Expand Down Expand Up @@ -231,10 +240,22 @@ func run() {
httpListener, err := setupListener(addr, x.PortZeroHTTP+opts.portOffset, "http")
x.Check(err)

x.AssertTruef(opts.totalCache >= 0, "ERROR: Cache size must be non-negative")

cachePercent, err := x.GetCachePercentages(opts.cachePercentage, 2)
x.Check(err)
blockCacheSz := (cachePercent[0] * (opts.totalCache << 20)) / 100
indexCacheSz := (cachePercent[1] * (opts.totalCache << 20)) / 100

// Open raft write-ahead log and initialize raft node.
x.Checkf(os.MkdirAll(opts.w, 0700), "Error while creating WAL dir.")
kvOpt := badger.LSMOnlyOptions(opts.w).WithSyncWrites(false).WithTruncate(true).
WithValueLogFileSize(64 << 20).WithMaxCacheSize(10 << 20).WithLoadBloomsOnOpen(false)
kvOpt := badger.LSMOnlyOptions(opts.w).
WithSyncWrites(false).
WithTruncate(true).
WithValueLogFileSize(64 << 20).
WithBlockCacheSize(blockCacheSz).
WithIndexCacheSize(indexCacheSz).
WithLoadBloomsOnOpen(false)

kvOpt.ZSTDCompressionLevel = 3

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/blevesearch/segment v0.0.0-20160915185041-762005e7a34f // indirect
github.com/blevesearch/snowballstem v0.0.0-20180110192139-26b06a2c243d // indirect
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd
github.com/dgraph-io/badger/v2 v2.2007.1
github.com/dgraph-io/badger/v2 v2.2007.2-0.20200826122734-bc243f38bfe1
github.com/dgraph-io/dgo/v2 v2.2.1-0.20200319183917-53c7d5bc32a7
github.com/dgraph-io/ristretto v0.0.3-0.20200630154024-f66de99634de
github.com/dgrijalva/jwt-go v3.2.0+incompatible
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgraph-io/badger v1.6.0 h1:DshxFxZWXUcO0xX476VJC07Xsr6ZCBVRHKZ93Oh7Evo=
github.com/dgraph-io/badger v1.6.0/go.mod h1:zwt7syl517jmP8s94KqSxTlM6IMsdhYy6psNgSztDR4=
github.com/dgraph-io/badger/v2 v2.2007.1 h1:t36VcBCpo4SsmAD5M8wVv1ieVzcALyGfaJ92z4ccULM=
github.com/dgraph-io/badger/v2 v2.2007.1/go.mod h1:26P/7fbL4kUZVEVKLAKXkBXKOydDmM2p1e+NhhnBCAE=
github.com/dgraph-io/badger/v2 v2.2007.2-0.20200826122734-bc243f38bfe1 h1:vPPlQYByX3+Z3NOaB06i7VCb0bNOznVQ9eEnqLxbrH0=
github.com/dgraph-io/badger/v2 v2.2007.2-0.20200826122734-bc243f38bfe1/go.mod h1:26P/7fbL4kUZVEVKLAKXkBXKOydDmM2p1e+NhhnBCAE=
github.com/dgraph-io/dgo/v2 v2.2.1-0.20200319183917-53c7d5bc32a7 h1:9oFXHEReyRIB291rbdGwRk1PYegGO2XBtZ8muXPKqPA=
github.com/dgraph-io/dgo/v2 v2.2.1-0.20200319183917-53c7d5bc32a7/go.mod h1:LJCkLxm5fUMcU+yb8gHFjHt7ChgNuz3YnQQ6MQkmscI=
github.com/dgraph-io/ristretto v0.0.3-0.20200630154024-f66de99634de h1:t0UHb5vdojIDUqktM6+xJAfScFBsVpXZmqC9dsgJmeA=
Expand Down
9 changes: 9 additions & 0 deletions worker/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ type Options struct {
// AllottedMemory is the estimated size taken by the LRU cache.
AllottedMemory float64

// PBlockCacheSize is the size of block cache for pstore
PBlockCacheSize int64
// PIndexCacheSize is the size of index cache for pstore
PIndexCacheSize int64
// WBlockCacheSize is the size of block cache for wstore
WBlockCacheSize int64
// WIndexCacheSize is the size of index cache for wstore
WIndexCacheSize int64

// HmacSecret stores the secret used to sign JSON Web Tokens (JWT).
HmacSecret x.SensitiveByteSlice
// AccessJwtTtl is the TTL for the access JWT.
Expand Down
9 changes: 4 additions & 5 deletions worker/server_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ func (s *ServerState) initStorage() {
opt := badger.LSMOnlyOptions(Config.WALDir)
opt = setBadgerOptions(opt, true)
opt.ValueLogMaxEntries = 10000 // Allow for easy space reclamation.
opt.MaxCacheSize = 10 << 20 // 10 mb of cache size for WAL.
opt.BlockCacheSize = Config.WBlockCacheSize
opt.IndexCacheSize = Config.WIndexCacheSize

// Print the options w/o exposing key.
// TODO: Build a stringify interface in Badger options, which is used to print nicely here.
Expand All @@ -165,10 +166,8 @@ func (s *ServerState) initStorage() {
opt := badger.DefaultOptions(Config.PostingDir).
WithValueThreshold(1 << 10 /* 1KB */).
WithNumVersionsToKeep(math.MaxInt32).
WithMaxCacheSize(1 << 30).
WithKeepBlockIndicesInCache(true).
WithKeepBlocksInCache(true).
WithMaxBfCacheSize(500 << 20) // 500 MB of bloom filter cache.
WithBlockCacheSize(Config.PBlockCacheSize).
WithIndexCacheSize(Config.PIndexCacheSize)
opt = setBadgerOptions(opt, false)

// Print the options w/o exposing key.
Expand Down
32 changes: 32 additions & 0 deletions x/x.go
Original file line number Diff line number Diff line change
Expand Up @@ -935,3 +935,35 @@ func StoreSync(db DB, closer *y.Closer) {
}
}
}

// GetCachePercentages returns the slice of cache percentages given the "," (comma) separated
// cache percentages(integers) string and expected number of caches.
func GetCachePercentages(cpString string, numExpected int) ([]int64, error) {
cp := strings.Split(cpString, ",")
// Sanity checks
if len(cp) != numExpected {
return nil, errors.Errorf("ERROR: expected %d cache percentages, got %d",
numExpected, len(cp))
}

var cachePercent []int64
percentSum := 0
for _, percent := range cp {
x, err := strconv.Atoi(percent)
if err != nil {
return nil, errors.Errorf("ERROR: unable to parse cache percentage(%s)", percent)
}
if x < 0 {
return nil, errors.Errorf("ERROR: cache percentage(%s) cannot be negative", percent)
}
cachePercent = append(cachePercent, int64(x))
percentSum += x
}

if percentSum != 100 {
return nil, errors.Errorf("ERROR: cache percentages (%s) does not sum up to 100",
strings.Join(cp, "+"))
}

return cachePercent, nil
}

0 comments on commit 9104512

Please sign in to comment.