-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathconfig.go
119 lines (102 loc) · 3.11 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Copyright 2017-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 edgraph
import (
"expvar"
"path/filepath"
"time"
"github.com/dgraph-io/dgraph/posting"
"github.com/dgraph-io/dgraph/x"
)
const (
AllowMutations int = iota
DisallowMutations
StrictMutations
)
type Options struct {
PostingDir string
BadgerTables string
BadgerVlog string
WALDir string
MutationsMode int
AuthToken string
AllottedMemory float64
HmacSecret []byte
AccessJwtTtl time.Duration
RefreshJwtTtl time.Duration
AclRefreshInterval time.Duration
}
var Config Options
// Sometimes users use config.yaml flag so /debug/vars doesn't have information about the
// value of the flags. Hence we dump conf options we care about to the conf map.
func setConfVar(conf Options) {
newStr := func(s string) *expvar.String {
v := new(expvar.String)
v.Set(s)
return v
}
newFloat := func(f float64) *expvar.Float {
v := new(expvar.Float)
v.Set(f)
return v
}
newInt := func(i int) *expvar.Int {
v := new(expvar.Int)
v.Set(int64(i))
return v
}
// Expvar doesn't have bool type so we use an int.
newIntFromBool := func(b bool) *expvar.Int {
v := new(expvar.Int)
if b {
v.Set(1)
} else {
v.Set(0)
}
return v
}
// This is so we can find these options in /debug/vars.
x.Conf.Set("badger.tables", newStr(conf.BadgerTables))
x.Conf.Set("badger.vlog", newStr(conf.BadgerVlog))
x.Conf.Set("posting_dir", newStr(conf.PostingDir))
x.Conf.Set("wal_dir", newStr(conf.WALDir))
x.Conf.Set("allotted_memory", newFloat(conf.AllottedMemory))
// Set some vars from worker.Config.
x.Conf.Set("tracing", newFloat(x.WorkerConfig.Tracing))
x.Conf.Set("num_pending_proposals", newInt(x.WorkerConfig.NumPendingProposals))
x.Conf.Set("expand_edge", newIntFromBool(x.WorkerConfig.ExpandEdge))
}
func SetConfiguration(newConfig Options) {
newConfig.validate()
setConfVar(newConfig)
Config = newConfig
posting.Config.Mu.Lock()
posting.Config.AllottedMemory = Config.AllottedMemory
posting.Config.Mu.Unlock()
}
const MinAllottedMemory = 1024.0
func (o *Options) validate() {
pd, err := filepath.Abs(o.PostingDir)
x.Check(err)
wd, err := filepath.Abs(o.WALDir)
x.Check(err)
x.AssertTruef(pd != wd, "Posting and WAL directory cannot be the same ('%s').", o.PostingDir)
x.AssertTruefNoTrace(o.AllottedMemory != -1,
"LRU memory (--lru_mb) must be specified. (At least 1024 MB)")
x.AssertTruefNoTrace(o.AllottedMemory >= MinAllottedMemory,
"LRU memory (--lru_mb) must be at least %.0f MB. Currently set to: %f",
MinAllottedMemory, o.AllottedMemory)
}