github.com/unigraph-dev/dgraph@v1.1.1-0.20200923154953-8b52b426f765/edgraph/config.go (about)

     1  /*
     2   * Copyright 2017-2018 Dgraph Labs, Inc. and Contributors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package edgraph
    18  
    19  import (
    20  	"fmt"
    21  	"path/filepath"
    22  	"time"
    23  
    24  	"github.com/golang/glog"
    25  
    26  	"github.com/dgraph-io/dgraph/posting"
    27  	"github.com/dgraph-io/dgraph/x"
    28  )
    29  
    30  const (
    31  	// AllowMutations is the mode allowing all mutations.
    32  	AllowMutations int = iota
    33  	// DisallowMutations is the mode that disallows all mutations.
    34  	DisallowMutations
    35  	// StrictMutations is the mode that allows mutations if and only if they contain known preds.
    36  	StrictMutations
    37  )
    38  
    39  // Options contains options for the Dgraph server.
    40  type Options struct {
    41  	// PostingDir is the path to the directory storing the postings..
    42  	PostingDir string
    43  	// BadgerTables is the name of the mode used to load the badger tables.
    44  	BadgerTables string
    45  	// BadgerVlog is the name of the mode used to load the badger value log.
    46  	BadgerVlog string
    47  	// WALDir is the path to the directory storing the write-ahead log.
    48  	WALDir string
    49  	// MutationsMode is the mode used to handle mutation requests.
    50  	MutationsMode int
    51  	// AuthToken is the token to be passed for Alter HTTP requests.
    52  	AuthToken string
    53  	// AllottedMemory is the estimated size taken by the LRU cache.
    54  	AllottedMemory float64
    55  
    56  	// HmacSecret stores the secret used to sign JSON Web Tokens (JWT).
    57  	HmacSecret []byte
    58  	// AccessJwtTtl is the TTL for the access JWT.
    59  	AccessJwtTtl time.Duration
    60  	// RefreshJwtTtl is the TTL of the refresh JWT.
    61  	RefreshJwtTtl time.Duration
    62  	// AclRefreshInterval is the interval used to refresh the ACL cache.
    63  	AclRefreshInterval time.Duration
    64  }
    65  
    66  // Config holds an instance of the server options..
    67  var Config Options
    68  
    69  // String will generate the string output an Options struct without including
    70  // the HmacSecret field, which prevents revealing the secret during logging
    71  func (opt Options) String() string {
    72  	//return fmt.Sprintf()
    73  	return fmt.Sprintf("{PostingDir:%s BadgerTables:%s BadgerVlog:%s WALDir:%s MutationsMode:%d "+
    74  		"AuthToken:%s AllottedMemory:%.1fMB AccessJwtTtl:%v RefreshJwtTtl:%v "+
    75  		"AclRefreshInterval:%v}", opt.PostingDir, opt.BadgerTables, opt.BadgerVlog, opt.WALDir,
    76  		opt.MutationsMode, opt.AuthToken, opt.AllottedMemory, opt.AccessJwtTtl, opt.RefreshJwtTtl,
    77  		opt.AclRefreshInterval)
    78  }
    79  
    80  // SetConfiguration sets the server configuration to the given config.
    81  func SetConfiguration(newConfig Options) {
    82  	newConfig.validate()
    83  	Config = newConfig
    84  
    85  	posting.Config.Mu.Lock()
    86  	posting.Config.AllottedMemory = Config.AllottedMemory
    87  	posting.Config.Mu.Unlock()
    88  }
    89  
    90  // MinAllottedMemory is the minimum amount of memory needed for the LRU cache.
    91  const MinAllottedMemory = 1024.0
    92  
    93  // availableMemory is the total size of the memory we were able to identify.
    94  var availableMemory int64
    95  
    96  func (opt *Options) validate() {
    97  	pd, err := filepath.Abs(opt.PostingDir)
    98  	x.Check(err)
    99  	wd, err := filepath.Abs(opt.WALDir)
   100  	x.Check(err)
   101  	x.AssertTruef(pd != wd, "Posting and WAL directory cannot be the same ('%s').", opt.PostingDir)
   102  	if opt.AllottedMemory < 0 {
   103  		if allottedMemory := 0.25 * float64(availableMemory); allottedMemory > MinAllottedMemory {
   104  			opt.AllottedMemory = allottedMemory
   105  			glog.Infof(
   106  				"LRU memory (--lru_mb) set to %vMB, 25%% of the total RAM found (%vMB)\n"+
   107  					"For more information on --lru_mb please read "+
   108  					"https://docs.dgraph.io/deploy/#config\n",
   109  				opt.AllottedMemory, availableMemory)
   110  		}
   111  	}
   112  	x.AssertTruefNoTrace(opt.AllottedMemory >= MinAllottedMemory,
   113  		"LRU memory (--lru_mb) must be at least %.0f MB. Currently set to: %f\n"+
   114  			"For more information on --lru_mb please read https://docs.dgraph.io/deploy/#config",
   115  		MinAllottedMemory, opt.AllottedMemory)
   116  }