-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
mvcc.go
378 lines (342 loc) · 10.6 KB
/
mvcc.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* 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 posting
import (
"bytes"
"encoding/hex"
"math"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/dgraph-io/badger/v2"
bpb "github.com/dgraph-io/badger/v2/pb"
"github.com/dgraph-io/badger/v2/y"
"github.com/dgraph-io/dgo/v200/protos/api"
"github.com/dgraph-io/dgraph/protos/pb"
"github.com/dgraph-io/dgraph/x"
"github.com/dgraph-io/ristretto/z"
"github.com/golang/glog"
"github.com/pkg/errors"
)
// incrRollupi is used to batch keys for rollup incrementally.
type incrRollupi struct {
// keysCh is populated with batch of 64 keys that needs to be rolled up during reads
keysCh chan *[][]byte
// keysPool is sync.Pool to share the batched keys to rollup.
keysPool *sync.Pool
count uint64
}
var (
// ErrTsTooOld is returned when a transaction is too old to be applied.
ErrTsTooOld = errors.Errorf("Transaction is too old")
// ErrInvalidKey is returned when trying to read a posting list using
// an invalid key (e.g the key to a single part of a larger multi-part list).
ErrInvalidKey = errors.Errorf("cannot read posting list using multi-part list key")
// ErrBadgerClosed is returned when Badger is closed and then we try to
// read something from it.
ErrBadgerClosed = errors.Errorf("read after closing badger")
// BadgerClosed denotes if the badger store is closed. It should be accessed atomically.
// We set it to 1 just before closing badger DB.
BadgerClosed uint32
// IncrRollup is used to batch keys for rollup incrementally.
IncrRollup = &incrRollupi{
keysCh: make(chan *[][]byte),
keysPool: &sync.Pool{
New: func() interface{} {
return new([][]byte)
},
},
}
)
// rollUpKey takes the given key's posting lists, rolls it up and writes back to badger
func (ir *incrRollupi) rollUpKey(writer *TxnWriter, key []byte) error {
l, err := GetNoStore(key, math.MaxUint64)
if err != nil {
return err
}
kvs, err := l.Rollup()
if err != nil {
return err
}
const N = uint64(1000)
if glog.V(2) {
if count := atomic.AddUint64(&ir.count, 1); count%N == 0 {
glog.V(2).Infof("Rolled up %d keys", count)
}
}
return writer.Write(&bpb.KVList{Kv: kvs})
}
func (ir *incrRollupi) addKeyToBatch(key []byte) {
batch := ir.keysPool.Get().(*[][]byte)
*batch = append(*batch, key)
if len(*batch) < 64 {
ir.keysPool.Put(batch)
return
}
select {
case ir.keysCh <- batch:
default:
// Drop keys and build the batch again. Lossy behavior.
*batch = (*batch)[:0]
ir.keysPool.Put(batch)
}
}
// Process will rollup batches of 64 keys in a go routine.
func (ir *incrRollupi) Process(closer *y.Closer) {
defer closer.Done()
writer := NewTxnWriter(pstore)
defer writer.Flush()
m := make(map[uint64]int64) // map hash(key) to ts. hash(key) to limit the size of the map.
limiter := time.NewTicker(100 * time.Millisecond)
defer limiter.Stop()
cleanupTick := time.NewTicker(5 * time.Minute)
defer cleanupTick.Stop()
for {
select {
case <-closer.HasBeenClosed():
return
case <-cleanupTick.C:
currTs := time.Now().UnixNano()
for hash, ts := range m {
// Remove entries from map which have been there for there more than 10 seconds.
if currTs-ts >= int64(10*time.Second) {
delete(m, hash)
}
}
case batch := <-ir.keysCh:
currTs := time.Now().Unix()
for _, key := range *batch {
hash := z.MemHash(key)
if elem := m[hash]; currTs-elem >= 10 {
// Key not present or Key present but last roll up was more than 10 sec ago.
// Add/Update map and rollup.
m[hash] = currTs
if err := ir.rollUpKey(writer, key); err != nil {
glog.Warningf("Error %v rolling up key %v\n", err, key)
}
}
}
// clear the batch and put it back in Sync keysPool
*batch = (*batch)[:0]
ir.keysPool.Put(batch)
// throttle to 1 batch = 64 rollups per 100 ms.
<-limiter.C
}
}
}
// ShouldAbort returns whether the transaction should be aborted.
func (txn *Txn) ShouldAbort() bool {
if txn == nil {
return false
}
return atomic.LoadUint32(&txn.shouldAbort) > 0
}
func (txn *Txn) addConflictKey(conflictKey uint64) {
txn.Lock()
defer txn.Unlock()
if txn.conflicts == nil {
txn.conflicts = make(map[uint64]struct{})
}
if conflictKey > 0 {
txn.conflicts[conflictKey] = struct{}{}
}
}
// FillContext updates the given transaction context with data from this transaction.
func (txn *Txn) FillContext(ctx *api.TxnContext, gid uint32) {
txn.Lock()
ctx.StartTs = txn.StartTs
for key := range txn.conflicts {
// We don'txn need to send the whole conflict key to Zero. Solving #2338
// should be done by sending a list of mutating predicates to Zero,
// along with the keys to be used for conflict detection.
fps := strconv.FormatUint(key, 36)
ctx.Keys = append(ctx.Keys, fps)
}
ctx.Keys = x.Unique(ctx.Keys)
txn.Unlock()
txn.Update()
txn.cache.fillPreds(ctx, gid)
}
// CommitToDisk commits a transaction to disk.
// This function only stores deltas to the commit timestamps. It does not try to generate a state.
// State generation is done via rollups, which happen when a snapshot is created.
// Don't call this for schema mutations. Directly commit them.
func (txn *Txn) CommitToDisk(writer *TxnWriter, commitTs uint64) error {
if commitTs == 0 {
return nil
}
cache := txn.cache
cache.Lock()
defer cache.Unlock()
var keys []string
for key := range cache.deltas {
keys = append(keys, key)
}
var idx int
for idx < len(keys) {
// writer.update can return early from the loop in case we encounter badger.ErrTxnTooBig. On
// that error, writer.update would still commit the transaction and return any error. If
// nil, we continue to process the remaining keys.
err := writer.update(commitTs, func(btxn *badger.Txn) error {
for ; idx < len(keys); idx++ {
key := keys[idx]
data := cache.deltas[key]
if len(data) == 0 {
continue
}
if ts := cache.maxVersions[key]; ts >= commitTs {
// Skip write because we already have a write at a higher ts.
// Logging here can cause a lot of output when doing Raft log replay. So, let's
// not output anything here.
continue
}
err := btxn.SetEntry(&badger.Entry{
Key: []byte(key),
Value: data,
UserMeta: BitDeltaPosting,
})
if err != nil {
return err
}
}
return nil
})
if err != nil {
return err
}
}
return nil
}
func unmarshalOrCopy(plist *pb.PostingList, item *badger.Item) error {
if plist == nil {
return errors.Errorf("cannot unmarshal value to a nil posting list of key %s",
hex.Dump(item.Key()))
}
return item.Value(func(val []byte) error {
if len(val) == 0 {
// empty pl
return nil
}
return plist.Unmarshal(val)
})
}
// ReadPostingList constructs the posting list from the disk using the passed iterator.
// Use forward iterator with allversions enabled in iter options.
// key would now be owned by the posting list. So, ensure that it isn't reused elsewhere.
func ReadPostingList(key []byte, it *badger.Iterator) (*List, error) {
// Previously, ReadPostingList was not checking that a multi-part list could only
// be read via the main key. This lead to issues during rollup because multi-part
// lists ended up being rolled-up multiple times. This issue was caught by the
// uid-set Jepsen test.
pk, err := x.Parse(key)
if err != nil {
return nil, errors.Wrapf(err, "while reading posting list with key [%v]", key)
}
if pk.HasStartUid {
// Trying to read a single part of a multi part list. This type of list
// should be read using using the main key because the information needed
// to access the whole list is stored there.
// The function returns a nil list instead. This is safe to do because all
// public methods of the List object are no-ops and the list is being already
// accessed via the main key in the places where this code is reached (e.g rollups).
return nil, ErrInvalidKey
}
l := new(List)
l.key = key
l.plist = new(pb.PostingList)
// We use the following block of code to trigger incremental rollup on this key.
deltaCount := 0
defer func() {
if deltaCount > 0 {
IncrRollup.addKeyToBatch(key)
}
}()
// Iterates from highest Ts to lowest Ts
for it.Valid() {
item := it.Item()
if !bytes.Equal(item.Key(), l.key) {
break
}
l.maxTs = x.Max(l.maxTs, item.Version())
if item.IsDeletedOrExpired() {
// Don't consider any more versions.
break
}
switch item.UserMeta() {
case BitEmptyPosting:
l.minTs = item.Version()
return l, nil
case BitCompletePosting:
if err := unmarshalOrCopy(l.plist, item); err != nil {
return nil, err
}
l.minTs = item.Version()
// No need to do Next here. The outer loop can take care of skipping
// more versions of the same key.
return l, nil
case BitDeltaPosting:
err := item.Value(func(val []byte) error {
pl := &pb.PostingList{}
if err := pl.Unmarshal(val); err != nil {
return err
}
pl.CommitTs = item.Version()
for _, mpost := range pl.Postings {
// commitTs, startTs are meant to be only in memory, not
// stored on disk.
mpost.CommitTs = item.Version()
}
if l.mutationMap == nil {
l.mutationMap = make(map[uint64]*pb.PostingList)
}
l.mutationMap[pl.CommitTs] = pl
return nil
})
if err != nil {
return nil, err
}
deltaCount++
case BitSchemaPosting:
return nil, errors.Errorf(
"Trying to read schema in ReadPostingList for key: %s", hex.Dump(key))
default:
return nil, errors.Errorf(
"Unexpected meta: %d for key: %s", item.UserMeta(), hex.Dump(key))
}
if item.DiscardEarlierVersions() {
break
}
it.Next()
}
return l, nil
}
func getNew(key []byte, pstore *badger.DB, readTs uint64) (*List, error) {
if atomic.LoadUint32(&BadgerClosed) == 1 {
return nil, ErrBadgerClosed
}
txn := pstore.NewTransactionAt(readTs, false)
defer txn.Discard()
// When we do rollups, an older version would go to the top of the LSM tree, which can cause
// issues during txn.Get. Therefore, always iterate.
iterOpts := badger.DefaultIteratorOptions
iterOpts.AllVersions = true
iterOpts.PrefetchValues = false
itr := txn.NewKeyIterator(key, iterOpts)
defer itr.Close()
itr.Seek(key)
return ReadPostingList(key, itr)
}