forked from dgraph-io/badger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream_writer.go
357 lines (327 loc) · 9.9 KB
/
stream_writer.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* Copyright 2019 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 badger
import (
"math"
"github.com/dgraph-io/badger/pb"
"github.com/dgraph-io/badger/table"
"github.com/dgraph-io/badger/y"
humanize "github.com/dustin/go-humanize"
"github.com/pkg/errors"
)
const headStreamId uint32 = math.MaxUint32
// StreamWriter is used to write data coming from multiple streams. The streams must not have any
// overlapping key ranges. Within each stream, the keys must be sorted. Badger Stream framework is
// capable of generating such an output. So, this StreamWriter can be used at the other end to build
// BadgerDB at a much faster pace by writing SSTables (and value logs) directly to LSM tree levels
// without causing any compactions at all. This is way faster than using batched writer or using
// transactions, but only applicable in situations where the keys are pre-sorted and the DB is being
// bootstrapped. Existing data would get deleted when using this writer. So, this is only useful
// when restoring from backup or replicating DB across servers.
//
// StreamWriter should not be called on in-use DB instances. It is designed only to bootstrap new
// DBs.
type StreamWriter struct {
db *DB
done func()
throttle *y.Throttle
maxVersion uint64
writers map[uint32]*sortedWriter
closer *y.Closer
}
// NewStreamWriter creates a StreamWriter. Right after creating StreamWriter, Prepare must be
// called. The memory usage of a StreamWriter is directly proportional to the number of streams
// possible. So, efforts must be made to keep the number of streams low. Stream framework would
// typically use 16 goroutines and hence create 16 streams.
func (db *DB) NewStreamWriter() *StreamWriter {
return &StreamWriter{
db: db,
// throttle shouldn't make much difference. Memory consumption is based on the number of
// concurrent streams being processed.
throttle: y.NewThrottle(16),
writers: make(map[uint32]*sortedWriter),
closer: y.NewCloser(0),
}
}
// Prepare should be called before writing any entry to StreamWriter. It deletes all data present in
// existing DB, stops compactions and any writes being done by other means. Be very careful when
// calling Prepare, because it could result in permanent data loss. Not calling Prepare would result
// in a corrupt Badger instance.
func (sw *StreamWriter) Prepare() error {
var err error
sw.done, err = sw.db.dropAll()
return err
}
// Write writes KVList to DB. Each KV within the list contains the stream id which StreamWriter
// would use to demux the writes. Write is not thread safe and it should NOT be called concurrently.
func (sw *StreamWriter) Write(kvs *pb.KVList) error {
if len(kvs.GetKv()) == 0 {
return nil
}
streamReqs := make(map[uint32]*request)
for _, kv := range kvs.Kv {
var meta, userMeta byte
if len(kv.Meta) > 0 {
meta = kv.Meta[0]
}
if len(kv.UserMeta) > 0 {
userMeta = kv.UserMeta[0]
}
if sw.maxVersion < kv.Version {
sw.maxVersion = kv.Version
}
e := &Entry{
Key: y.KeyWithTs(kv.Key, kv.Version),
Value: kv.Value,
UserMeta: userMeta,
ExpiresAt: kv.ExpiresAt,
meta: meta,
}
// If the value can be colocated with the key in LSM tree, we can skip
// writing the value to value log.
e.skipVlog = sw.db.shouldWriteValueToLSM(*e)
req := streamReqs[kv.StreamId]
if req == nil {
req = &request{}
streamReqs[kv.StreamId] = req
}
req.Entries = append(req.Entries, e)
}
var all []*request
for _, req := range streamReqs {
all = append(all, req)
}
if err := sw.db.vlog.write(all); err != nil {
return err
}
for streamId, req := range streamReqs {
writer, ok := sw.writers[streamId]
if !ok {
writer = sw.newWriter(streamId)
sw.writers[streamId] = writer
}
writer.reqCh <- req
}
return nil
}
// Flush is called once we are done writing all the entries. It syncs DB directories. It also
// updates Oracle with maxVersion found in all entries (if DB is not managed).
func (sw *StreamWriter) Flush() error {
defer sw.done()
sw.closer.SignalAndWait()
var maxHead valuePointer
for _, writer := range sw.writers {
if err := writer.Done(); err != nil {
return err
}
if maxHead.Less(writer.head) {
maxHead = writer.head
}
}
// Encode and write the value log head into a new table.
data := make([]byte, vptrSize)
maxHead.Encode(data)
headWriter := sw.newWriter(headStreamId)
if err := headWriter.Add(
y.KeyWithTs(head, sw.maxVersion),
y.ValueStruct{Value: data}); err != nil {
return err
}
if err := headWriter.Done(); err != nil {
return err
}
if !sw.db.opt.managedTxns {
if sw.db.orc != nil {
sw.db.orc.Stop()
}
sw.db.orc = newOracle(sw.db.opt)
sw.db.orc.nextTxnTs = sw.maxVersion
sw.db.orc.txnMark.Done(sw.maxVersion)
sw.db.orc.readMark.Done(sw.maxVersion)
sw.db.orc.incrementNextTs()
}
// Wait for all files to be written.
if err := sw.throttle.Finish(); err != nil {
return err
}
// Now sync the directories, so all the files are registered.
if sw.db.opt.ValueDir != sw.db.opt.Dir {
if err := syncDir(sw.db.opt.ValueDir); err != nil {
return err
}
}
if err := syncDir(sw.db.opt.Dir); err != nil {
return err
}
return sw.db.lc.validate()
}
type sortedWriter struct {
db *DB
throttle *y.Throttle
builder *table.Builder
lastKey []byte
streamId uint32
reqCh chan *request
head valuePointer
}
func (sw *StreamWriter) newWriter(streamId uint32) *sortedWriter {
w := &sortedWriter{
db: sw.db,
streamId: streamId,
throttle: sw.throttle,
builder: table.NewTableBuilder(),
reqCh: make(chan *request, 3),
}
sw.closer.AddRunning(1)
go w.handleRequests(sw.closer)
return w
}
// ErrUnsortedKey is returned when any out of order key arrives at sortedWriter during call to Add.
var ErrUnsortedKey = errors.New("Keys not in sorted order")
func (w *sortedWriter) handleRequests(closer *y.Closer) {
defer closer.Done()
process := func(req *request) {
for i, e := range req.Entries {
vptr := req.Ptrs[i]
if !vptr.IsZero() {
y.AssertTrue(w.head.Less(vptr))
w.head = vptr
}
var vs y.ValueStruct
if e.skipVlog {
vs = y.ValueStruct{
Value: e.Value,
Meta: e.meta,
UserMeta: e.UserMeta,
ExpiresAt: e.ExpiresAt,
}
} else {
vbuf := make([]byte, vptrSize)
vs = y.ValueStruct{
Value: vptr.Encode(vbuf),
Meta: e.meta | bitValuePointer,
UserMeta: e.UserMeta,
ExpiresAt: e.ExpiresAt,
}
}
if err := w.Add(e.Key, vs); err != nil {
panic(err)
}
}
}
for {
select {
case req := <-w.reqCh:
process(req)
case <-closer.HasBeenClosed():
close(w.reqCh)
for req := range w.reqCh {
process(req)
}
return
}
}
}
// Add adds key and vs to sortedWriter.
func (w *sortedWriter) Add(key []byte, vs y.ValueStruct) error {
if len(w.lastKey) > 0 && y.CompareKeys(key, w.lastKey) <= 0 {
return ErrUnsortedKey
}
sameKey := y.SameKey(key, w.lastKey)
// Same keys should go into the same SSTable.
if !sameKey && w.builder.ReachedCapacity(w.db.opt.MaxTableSize) {
if err := w.send(); err != nil {
return err
}
}
w.lastKey = y.SafeCopy(w.lastKey, key)
return w.builder.Add(key, vs)
}
func (w *sortedWriter) send() error {
if err := w.throttle.Do(); err != nil {
return err
}
go func(builder *table.Builder) {
data := builder.Finish()
err := w.createTable(data)
w.throttle.Done(err)
}(w.builder)
w.builder = table.NewTableBuilder()
return nil
}
// Done is called once we are done writing all keys and valueStructs
// to sortedWriter. It completes writing current SST to disk.
func (w *sortedWriter) Done() error {
if w.builder.Empty() {
return nil
}
return w.send()
}
func (w *sortedWriter) createTable(data []byte) error {
if len(data) == 0 {
return nil
}
fileID := w.db.lc.reserveFileID()
fd, err := y.CreateSyncedFile(table.NewFilename(fileID, w.db.opt.Dir), true)
if err != nil {
return err
}
if _, err := fd.Write(data); err != nil {
return err
}
tbl, err := table.OpenTable(fd, w.db.opt.TableLoadingMode, w.db.opt.ChecksumVerificationMode)
if err != nil {
return err
}
lc := w.db.lc
var lhandler *levelHandler
// We should start the levels from 1, because we need level 0 to set the !badger!head key. We
// cannot mix up this key with other keys from the DB, otherwise we would introduce a range
// overlap violation.
y.AssertTrue(len(lc.levels) > 1)
for _, l := range lc.levels[1:] {
ratio := float64(l.getTotalSize()) / float64(l.maxTotalSize)
if ratio < 1.0 {
lhandler = l
break
}
}
if lhandler == nil {
// If we're exceeding the size of the lowest level, shove it in the lowest level. Can't do
// better than that.
lhandler = lc.levels[len(lc.levels)-1]
}
if w.streamId == headStreamId {
// This is a special !badger!head key. We should store it at level 0, separate from all the
// other keys to avoid an overlap.
lhandler = lc.levels[0]
}
// Now that table can be opened successfully, let's add this to the MANIFEST.
change := &pb.ManifestChange{
Id: tbl.ID(),
Op: pb.ManifestChange_CREATE,
Level: uint32(lhandler.level),
}
if err := w.db.manifest.addChanges([]*pb.ManifestChange{change}); err != nil {
return err
}
if err := lhandler.replaceTables([]*table.Table{}, []*table.Table{tbl}); err != nil {
return err
}
w.db.opt.Infof("Table created: %d at level: %d for stream: %d. Size: %s\n",
fileID, lhandler.level, w.streamId, humanize.Bytes(uint64(tbl.Size())))
return nil
}