Skip to content

Commit

Permalink
feat(Dgraph): Add ability to change size of caches through the admin …
Browse files Browse the repository at this point in the history
…interface. (#6644)

The endpoints have been changed from lruMb to cacheMb to reflect that this
is changing the value of the cacheMb flag.

Fixes DGRAPH-2515
  • Loading branch information
martinmr authored Oct 8, 2020
1 parent a86b4d8 commit b6a9060
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 51 deletions.
12 changes: 6 additions & 6 deletions dgraph/cmd/alpha/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,14 @@ func memoryLimitPutHandler(w http.ResponseWriter, r *http.Request, adminServer w
}
gqlReq := &schema.Request{
Query: `
mutation config($lruMb: Float) {
config(input: {lruMb: $lruMb}) {
mutation config($cacheMb: Int) {
config(input: {cacheMb: $cacheMb}) {
response {
code
}
}
}`,
Variables: map[string]interface{}{"lruMb": memoryMB},
Variables: map[string]interface{}{"cacheMb": memoryMB},
}
resp := resolveWithAdminServer(gqlReq, r, adminServer)

Expand All @@ -196,19 +196,19 @@ func memoryLimitGetHandler(w http.ResponseWriter, r *http.Request, adminServer w
Query: `
query {
config {
lruMb
cacheMb
}
}`,
}
resp := resolveWithAdminServer(gqlReq, r, adminServer)
var data struct {
Config struct {
LruMb float64
CacheMb float64
}
}
x.Check(json.Unmarshal(resp.Data.Bytes(), &data))

if _, err := fmt.Fprintln(w, data.Config.LruMb); err != nil {
if _, err := fmt.Fprintln(w, data.Config.CacheMb); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
3 changes: 2 additions & 1 deletion dgraph/cmd/alpha/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ func setupServer(closer *z.Closer) {
exportHandler(w, r, adminServer)
}))))

http.Handle("/admin/config/lru_mb", allowedMethodsHandler(allowedMethods{
http.Handle("/admin/config/cache_mb", allowedMethodsHandler(allowedMethods{
http.MethodGet: true,
http.MethodPut: true,
}, adminAuthHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -605,6 +605,7 @@ func run() {
WALDir: Alpha.Conf.GetString("wal"),
PostingDirCompression: ctype,
PostingDirCompressionLevel: clevel,
CachePercentage: cachePercentage,
PBlockCacheSize: pstoreBlockCacheSize,
PIndexCacheSize: pstoreIndexCacheSize,
WalCache: walCache,
Expand Down
6 changes: 3 additions & 3 deletions ee/acl/acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2587,7 +2587,7 @@ func TestGuardianOnlyAccessForAdminEndpoints(t *testing.T) {
name: "config update has guardian auth",
query: `
mutation {
config(input: {lruMb: 1}) {
config(input: {cacheMb: -1}) {
response {
code
message
Expand All @@ -2597,7 +2597,7 @@ func TestGuardianOnlyAccessForAdminEndpoints(t *testing.T) {
queryName: "config",
testGuardianAccess: true,
guardianErrs: x.GqlErrorList{{
Message: "resolving config failed because lru_mb must be at least 1024\n",
Message: "resolving config failed because cache_mb must be non-negative",
Locations: []x.Location{{Line: 3, Column: 8}},
}},
guardianData: `{"config": null}`,
Expand All @@ -2607,7 +2607,7 @@ func TestGuardianOnlyAccessForAdminEndpoints(t *testing.T) {
query: `
query {
config {
lruMb
cacheMb
}
}`,
queryName: "config",
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ 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.0.1-rc1.0.20201007014141-e3a0d29f75d9
github.com/dgraph-io/badger/v2 v2.0.1-rc1.0.20201007220711-3b5f17cee813
github.com/dgraph-io/dgo/v200 v200.0.0-20200805103119-a3544c464dd6
github.com/dgraph-io/graphql-transport-ws v0.0.0-20200916064635-48589439591b
github.com/dgraph-io/ristretto v0.0.4-0.20201012160933-079c5f0d0daf
github.com/dgraph-io/ristretto v0.0.4-0.20201007164332-9739cfa2564b
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,14 @@ 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.0.1-rc1.0.20201007014141-e3a0d29f75d9 h1:jcCG8IgceDFY0eigR/Zp2NwRYowQAVYRkVnmBPJEw+0=
github.com/dgraph-io/badger/v2 v2.0.1-rc1.0.20201007014141-e3a0d29f75d9/go.mod h1:KGLxSlEzmRl4n00ZXty5ibkBgDNbFJkHfjkn3Do0o6I=
github.com/dgraph-io/badger/v2 v2.0.1-rc1.0.20201007220711-3b5f17cee813 h1:dYBCsSAR7oRPg18vdLWWwtOg9ojObRRfRXQbkZrx7aE=
github.com/dgraph-io/badger/v2 v2.0.1-rc1.0.20201007220711-3b5f17cee813/go.mod h1:jXV6noHsSocvT3cCY1V91XESeerqGagWUtIttgdo0Lg=
github.com/dgraph-io/dgo/v200 v200.0.0-20200805103119-a3544c464dd6 h1:toHzMCdCUgYsjM0cW9+wafnKFXfp1HizIJUyzihN+vk=
github.com/dgraph-io/dgo/v200 v200.0.0-20200805103119-a3544c464dd6/go.mod h1:rHa+h3kI4M8ASOirxyIyNeXBfHFgeskVUum2OrDMN3U=
github.com/dgraph-io/graphql-transport-ws v0.0.0-20200916064635-48589439591b h1:PDEhlwHpkEQ5WBfOOKZCNZTXFDGyCEWTYDhxGQbyIpk=
github.com/dgraph-io/graphql-transport-ws v0.0.0-20200916064635-48589439591b/go.mod h1:7z3c/5w0sMYYZF5bHsrh8IH4fKwG5O5Y70cPH1ZLLRQ=
github.com/dgraph-io/ristretto v0.0.4-0.20201007164332-9739cfa2564b h1:CfEwAh85rqfhhE/DrDCbFdcwVpZ8ESnFRhLatiKdDYM=
github.com/dgraph-io/ristretto v0.0.4-0.20201007164332-9739cfa2564b/go.mod h1:bDI4cDaalvYSji3vBVDKrn9ouDZrwN974u8ZO/AhYXs=
github.com/dgraph-io/ristretto v0.0.4-0.20201012160933-079c5f0d0daf h1:FccUHvo6MMTYz+SrPSjgFuwp7W2376H34YVSzm8qjUY=
github.com/dgraph-io/ristretto v0.0.4-0.20201012160933-079c5f0d0daf/go.mod h1:bDI4cDaalvYSji3vBVDKrn9ouDZrwN974u8ZO/AhYXs=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
Expand Down
10 changes: 5 additions & 5 deletions graphql/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,12 @@ const (
}
input ConfigInput {
"""
Estimated memory the LRU cache can take. Actual usage by the process would be
more than specified here. (default -1 means no set limit)
Estimated memory the caches can take. Actual usage by the process would be
more than specified here. The caches will be updated according to the
cache_percentage flag.
"""
lruMb: Float
cacheMb: Float
"""
True value of logRequest enables logging of all the requests coming to alphas.
Expand All @@ -268,7 +268,7 @@ const (
}
type Config {
lruMb: Float
cacheMb: Float
}
` + adminTypes + `
Expand Down
13 changes: 4 additions & 9 deletions graphql/admin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ import (

"github.com/dgraph-io/dgraph/graphql/resolve"
"github.com/dgraph-io/dgraph/graphql/schema"
"github.com/dgraph-io/dgraph/posting"
"github.com/dgraph-io/dgraph/worker"
"github.com/golang/glog"
)

type configInput struct {
LruMB float64
CacheMb float64
// LogRequest is used to update WorkerOptions.LogRequest. true value of LogRequest enables
// logging of all requests coming to alphas. LogRequest type has been kept as *bool instead of
// bool to avoid updating WorkerOptions.LogRequest when it has default value of false.
Expand All @@ -43,10 +42,8 @@ func resolveUpdateConfig(ctx context.Context, m schema.Mutation) (*resolve.Resol
return resolve.EmptyResult(m, err), false
}

if input.LruMB > 0 {
if err = worker.UpdateLruMb(input.LruMB); err != nil {
return resolve.EmptyResult(m, err), false
}
if err = worker.UpdateCacheMb(int64(input.CacheMb)); err != nil {
return resolve.EmptyResult(m, err), false
}

// input.LogRequest will be nil, when it is not specified explicitly in config request.
Expand All @@ -64,9 +61,7 @@ func resolveGetConfig(ctx context.Context, q schema.Query) *resolve.Resolved {
glog.Info("Got config query through GraphQL admin API")

conf := make(map[string]interface{})
posting.Config.Lock()
conf["lruMb"] = posting.Config.AllottedMemory
posting.Config.Unlock()
conf["cacheMb"] = float64(worker.Config.CacheMb)

return &resolve.Resolved{
Data: map[string]interface{}{q.Name(): conf},
Expand Down
2 changes: 1 addition & 1 deletion graphql/e2e/directives/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ services:
- type: bind
source: ./script.js
target: /app/script.js
read_only: true
read_only: true
1 change: 0 additions & 1 deletion posting/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import "sync"
// Options contains options for the postings package.
type Options struct {
sync.Mutex
AllottedMemory float64

CommitFraction float64
}
Expand Down
1 change: 0 additions & 1 deletion posting/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,6 @@ var ps *badger.DB

func TestMain(m *testing.M) {
x.Init()
Config.AllottedMemory = 1024.0
Config.CommitFraction = 0.10

dir, err := ioutil.TempDir("", "storetest_")
Expand Down
4 changes: 4 additions & 0 deletions posting/lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ func Init(ps *badger.DB, cacheSize int64) {
}()
}

func UpdateMaxCost(maxCost int64) {
lCache.UpdateMaxCost(maxCost)
}

// Cleanup waits until the closer has finished processing.
func Cleanup() {
closer.SignalAndWait()
Expand Down
10 changes: 5 additions & 5 deletions wiki/content/graphql/admin/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,12 @@ Here are the important types, queries, and mutations from the `admin` schema.
}

input ConfigInput {

"""
Estimated memory the LRU cache can take. Actual usage by the process would be
more than specified here. (default -1 means no set limit)
Estimated memory the caches can take. Actual usage by the process would be
more than specified here. The caches will be updated according to the
cache_percentage flag.
"""
lruMb: Float
cacheMb: Float

"""
True value of logRequest enables logging of all the requests coming to alphas.
Expand All @@ -271,7 +271,7 @@ Here are the important types, queries, and mutations from the `admin` schema.
}

type Config {
lruMb: Float
cacheMb: Float
}

type Query {
Expand Down
13 changes: 6 additions & 7 deletions worker/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

bo "github.com/dgraph-io/badger/v2/options"

"github.com/dgraph-io/dgraph/posting"
"github.com/dgraph-io/dgraph/x"
)

Expand Down Expand Up @@ -66,6 +65,12 @@ type Options struct {
AccessJwtTtl time.Duration
// RefreshJwtTtl is the TTL of the refresh JWT.
RefreshJwtTtl time.Duration

// CachePercentage is the comma-separated list of cache percentages
// used to split the total cache size among the multiple caches.
CachePercentage string
// CacheMb is the total memory allocated between all the caches.
CacheMb int64
}

// Config holds an instance of the server options..
Expand All @@ -78,14 +83,8 @@ func SetConfiguration(newConfig *Options) {
}
newConfig.validate()
Config = *newConfig

posting.Config.Lock()
defer posting.Config.Unlock()
}

// MinAllottedMemory is the minimum amount of memory needed for the LRU cache.
const MinAllottedMemory = 1024.0

// AvailableMemory is the total size of the memory we were able to identify.
var AvailableMemory int64

Expand Down
27 changes: 20 additions & 7 deletions worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,28 @@ func BlockingStop() {
workerServer.Stop()
}

// UpdateLruMb updates the value of lru_mb
func UpdateLruMb(memoryMB float64) error {
if memoryMB < MinAllottedMemory {
return errors.Errorf("lru_mb must be at least %.0f\n", MinAllottedMemory)
// UpdateCacheMb updates the value of cache_mb and updates the corresponding cache sizes.
func UpdateCacheMb(memoryMB int64) error {
glog.Infof("Updating cacheMb to %d", memoryMB)
if memoryMB < 0 {
return errors.Errorf("cache_mb must be non-negative")
}

posting.Config.Lock()
posting.Config.AllottedMemory = memoryMB
posting.Config.Unlock()
cachePercent, err := x.GetCachePercentages(Config.CachePercentage, 4)
if err != nil {
return err
}
plCacheSize := (cachePercent[0] * (memoryMB << 20)) / 100
blockCacheSize := (cachePercent[1] * (memoryMB << 20)) / 100
indexCacheSize := (cachePercent[2] * (memoryMB << 20)) / 100

posting.UpdateMaxCost(plCacheSize)
if _, err := pstore.CacheMaxCost(badger.BlockCache, blockCacheSize); err != nil {
return errors.Wrapf(err, "cannot update block cache size")
}
if _, err := pstore.CacheMaxCost(badger.IndexCache, indexCacheSize); err != nil {
return errors.Wrapf(err, "cannot update index cache size")
}
return nil
}

Expand Down
1 change: 0 additions & 1 deletion worker/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ func TestProcessTaskIndex(t *testing.T) {

func TestMain(m *testing.M) {
x.Init()
posting.Config.AllottedMemory = 1024.0
posting.Config.CommitFraction = 0.10
gr = new(groupi)
gr.gid = 1
Expand Down

0 comments on commit b6a9060

Please sign in to comment.