-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathserver_state.go
261 lines (227 loc) · 7.33 KB
/
server_state.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* 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 worker
import (
"context"
"math"
"os"
"time"
"github.com/dgraph-io/badger/v2"
"github.com/dgraph-io/badger/v2/options"
"github.com/dgraph-io/badger/v2/y"
"github.com/dgraph-io/dgraph/protos/pb"
"github.com/dgraph-io/dgraph/x"
"github.com/golang/glog"
)
// ServerState holds the state of the Dgraph server.
type ServerState struct {
FinishCh chan struct{} // channel to wait for all pending reqs to finish.
ShutdownCh chan struct{} // channel to signal shutdown.
Pstore *badger.DB
WALstore *badger.DB
gcCloser *y.Closer // closer for valueLogGC
needTs chan tsReq
}
// State is the instance of ServerState used by the current server.
var State ServerState
// InitServerState initializes this server's state.
func InitServerState() {
Config.validate()
State.FinishCh = make(chan struct{})
State.ShutdownCh = make(chan struct{})
State.needTs = make(chan tsReq, 100)
State.initStorage()
go State.fillTimestampRequests()
groupId, err := x.ReadGroupIdFile(Config.PostingDir)
if err != nil {
glog.Warningf("Could not read %s file inside posting directory %s.", x.GroupIdFileName,
Config.PostingDir)
}
x.WorkerConfig.ProposedGroupId = groupId
}
func setBadgerOptions(opt badger.Options) badger.Options {
opt = opt.WithSyncWrites(false).WithTruncate(true).WithLogger(&x.ToGlog{}).
WithEncryptionKey(x.WorkerConfig.EncryptionKey)
// 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 {
case "mmap":
opt.TableLoadingMode = options.MemoryMap
case "ram":
opt.TableLoadingMode = options.LoadToRAM
case "disk":
opt.TableLoadingMode = options.FileIO
default:
x.Fatalf("Invalid Badger Tables options")
}
glog.Infof("Setting Badger value log load option: %s", Config.BadgerVlog)
switch Config.BadgerVlog {
case "mmap":
opt.ValueLogLoadingMode = options.MemoryMap
case "disk":
opt.ValueLogLoadingMode = options.FileIO
default:
x.Fatalf("Invalid Badger Value log options")
}
return opt
}
func (s *ServerState) initStorage() {
var err error
if x.WorkerConfig.EncryptionKey != nil {
// non-nil key file
if !EnterpriseEnabled() {
// not licensed --> crash.
glog.Fatal("Valid Enterprise License needed for the Encryption feature.")
} else {
// licensed --> OK.
glog.Infof("Encryption feature enabled.")
}
}
{
// Write Ahead Log directory
x.Checkf(os.MkdirAll(Config.WALDir, 0700), "Error while creating WAL dir.")
opt := badger.LSMOnlyOptions(Config.WALDir)
opt = setBadgerOptions(opt)
opt.ValueLogMaxEntries = 10000 // Allow for easy space reclamation.
opt.MaxCacheSize = 10 << 20 // 10 mb of cache size for WAL.
// We should always force load LSM tables to memory, disregarding user settings, because
// Raft.Advance hits the WAL many times. If the tables are not in memory, retrieval slows
// down way too much, causing cluster membership issues. Because of prefix compression and
// value separation provided by Badger, this is still better than using the memory based WAL
// storage provided by the Raft library.
opt.TableLoadingMode = options.LoadToRAM
// Print the options w/o exposing key.
// TODO: Build a stringify interface in Badger options, which is used to print nicely here.
key := opt.EncryptionKey
opt.EncryptionKey = nil
glog.Infof("Opening write-ahead log BadgerDB with options: %+v\n", opt)
opt.EncryptionKey = key
s.WALstore, err = badger.Open(opt)
x.Checkf(err, "Error while creating badger KV WAL store")
}
{
// Postings directory
// All the writes to posting store should be synchronous. We use batched writers
// for posting lists, so the cost of sync writes is amortized.
x.Check(os.MkdirAll(Config.PostingDir, 0700))
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.
opt = setBadgerOptions(opt)
// Print the options w/o exposing key.
// TODO: Build a stringify interface in Badger options, which is used to print nicely here.
key := opt.EncryptionKey
opt.EncryptionKey = nil
glog.Infof("Opening postings BadgerDB with options: %+v\n", opt)
opt.EncryptionKey = key
s.Pstore, err = badger.OpenManaged(opt)
x.Checkf(err, "Error while creating badger KV posting store")
// zero out from memory
opt.EncryptionKey = nil
}
s.gcCloser = y.NewCloser(2)
go x.RunVlogGC(s.Pstore, s.gcCloser)
go x.RunVlogGC(s.WALstore, s.gcCloser)
}
// Dispose stops and closes all the resources inside the server state.
func (s *ServerState) Dispose() {
s.gcCloser.SignalAndWait()
if err := s.Pstore.Close(); err != nil {
glog.Errorf("Error while closing postings store: %v", err)
}
if err := s.WALstore.Close(); err != nil {
glog.Errorf("Error while closing WAL store: %v", err)
}
}
func (s *ServerState) GetTimestamp(readOnly bool) uint64 {
tr := tsReq{readOnly: readOnly, ch: make(chan uint64)}
s.needTs <- tr
return <-tr.ch
}
func (s *ServerState) fillTimestampRequests() {
const (
initDelay = 10 * time.Millisecond
maxDelay = time.Second
)
var reqs []tsReq
for {
// Reset variables.
reqs = reqs[:0]
delay := initDelay
req := <-s.needTs
slurpLoop:
for {
reqs = append(reqs, req)
select {
case req = <-s.needTs:
default:
break slurpLoop
}
}
// Generate the request.
num := &pb.Num{}
for _, r := range reqs {
if r.readOnly {
num.ReadOnly = true
} else {
num.Val++
}
}
// Execute the request with infinite retries.
retry:
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ts, err := Timestamps(ctx, num)
cancel()
if err != nil {
glog.Warningf("Error while retrieving timestamps: %v with delay: %v."+
" Will retry...\n", err, delay)
time.Sleep(delay)
delay *= 2
if delay > maxDelay {
delay = maxDelay
}
goto retry
}
var offset uint64
for _, req := range reqs {
if req.readOnly {
req.ch <- ts.ReadOnly
} else {
req.ch <- ts.StartId + offset
offset++
}
}
x.AssertTrue(ts.StartId == 0 || ts.StartId+offset-1 == ts.EndId)
}
}
type tsReq struct {
readOnly bool
// A one-shot chan which we can send a txn timestamp upon.
ch chan uint64
}