forked from dgraph-io/dgraph
-
Notifications
You must be signed in to change notification settings - Fork 1
/
predicate.go
344 lines (313 loc) · 8.76 KB
/
predicate.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
/*
* Copyright (C) 2017 Dgraph Labs, Inc. and Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package worker
import (
"bytes"
"context"
"crypto/md5"
"io"
"github.com/dgraph-io/badger"
"golang.org/x/net/trace"
"gopkg.in/adibiarsotp/dgraph.v83/conn"
"gopkg.in/adibiarsotp/dgraph.v83/posting"
"gopkg.in/adibiarsotp/dgraph.v83/protos"
"gopkg.in/adibiarsotp/dgraph.v83/x"
)
const (
// MB represents a megabyte.
MB = 1 << 20
)
// writeBatch performs a batch write of key value pairs to RocksDB.
func writeBatch(ctx context.Context, pstore *badger.KV, kv chan *protos.KV, che chan error) {
wb := make([]*badger.Entry, 0, 100)
batchSize := 0
batchWriteNum := 1
for i := range kv {
if len(i.Val) == 0 {
wb = badger.EntriesDelete(wb, i.Key)
} else {
entry := &badger.Entry{
Key: i.Key,
Value: i.Val,
UserMeta: i.UserMeta[0],
}
wb = append(wb, entry)
}
batchSize += len(i.Key) + len(i.Val)
// We write in batches of size 32MB.
if batchSize >= 32*MB {
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("SNAPSHOT: Doing batch write num: %d", batchWriteNum)
}
if err := pstore.BatchSet(wb); err != nil {
che <- err
return
}
batchWriteNum++
// Resetting batch size after a batch write.
batchSize = 0
// Since we are writing data in batches, we need to clear up items enqueued
// for batch write after every successful write.
wb = wb[:0]
}
}
if batchSize > 0 {
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("Doing batch write %d.", batchWriteNum)
}
if err := pstore.BatchSet(wb); err != nil {
che <- err
return
}
}
che <- nil
}
func streamKeys(pstore *badger.KV, stream protos.Worker_PredicateAndSchemaDataClient) error {
it := pstore.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
g := &protos.GroupKeys{
GroupId: groups().groupId(),
}
// Do NOT go to next by default. Be careful when you "continue" in loop.
for it.Rewind(); it.Valid(); {
iterItem := it.Item()
k := iterItem.Key()
pk := x.Parse(k)
if pk == nil {
it.Next()
continue
}
// No need to send KC for schema keys, since we won't save anything
// by sending checksum of schema key
if pk.IsSchema() {
it.Seek(pk.SkipSchema())
// Do not go next.
continue
}
if !groups().ServesTablet(pk.Attr) {
it.Seek(pk.SkipPredicate())
continue
}
kdup := make([]byte, len(k))
copy(kdup, k)
key := &protos.KC{
Key: kdup,
}
err := iterItem.Value(func(val []byte) error {
checksum := md5.Sum(val)
key.Checksum = checksum[:]
return nil
})
if err != nil {
return err
}
g.Keys = append(g.Keys, key)
if len(g.Keys) >= 1000 {
if err := stream.Send(g); err != nil {
return x.Wrapf(err, "While sending group keys to server.")
}
g.Keys = g.Keys[:0]
}
it.Next()
}
if err := stream.Send(g); err != nil {
return x.Wrapf(err, "While sending group keys to server.")
}
return stream.CloseSend()
}
// PopulateShard gets data for predicate pred from server with id serverId and
// writes it to RocksDB.
func populateShard(ctx context.Context, ps *badger.KV, pl *conn.Pool, group uint32) (int, error) {
conn := pl.Get()
c := protos.NewWorkerClient(conn)
stream, err := c.PredicateAndSchemaData(context.Background())
if err != nil {
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf(err.Error())
}
return 0, err
}
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("Streaming data for group: %v", group)
}
if err := streamKeys(ps, stream); err != nil {
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf(err.Error())
}
return 0, x.Wrapf(err, "While streaming keys group")
}
kvs := make(chan *protos.KV, 1000)
che := make(chan error)
go writeBatch(ctx, ps, kvs, che)
// We can use count to check the number of posting lists returned in tests.
count := 0
for {
kv, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf(err.Error())
}
close(kvs)
return count, err
}
count++
// We check for errors, if there are no errors we send value to channel.
select {
case kvs <- kv:
// OK
case <-ctx.Done():
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("Context timed out while streaming group: %v", group)
}
close(kvs)
return 0, ctx.Err()
case err := <-che:
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("Error while doing a batch write for group: %v", group)
}
close(kvs)
// Important: Don't put return count, err
return 0, err
}
}
close(kvs)
if err := <-che; err != nil {
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("Error while doing a batch write for group: %v", group)
}
return count, err
}
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("Streaming complete for group: %v", group)
}
return count, nil
}
// PredicateAndSchemaData can be used to return data corresponding to a predicate over
// a stream.
func (w *grpcWorker) PredicateAndSchemaData(stream protos.Worker_PredicateAndSchemaDataServer) error {
gkeys := &protos.GroupKeys{}
// Receive all keys from client first.
// TODO: Weird that we batch keys in batches of 1000 on the client, but but not here.
// TODO: Don't fetch all at once, use stream and iterate in parallel
for {
keys, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
return x.Wrap(err)
}
if gkeys.GroupId == 0 {
gkeys.GroupId = keys.GroupId
}
x.AssertTruef(gkeys.GroupId == keys.GroupId,
"Group ids don't match [%v] v/s [%v]", gkeys.GroupId, keys.GroupId)
// Do we need to check if keys are sorted? They should already be.
gkeys.Keys = append(gkeys.Keys, keys.Keys...)
}
if tr, ok := trace.FromContext(stream.Context()); ok {
tr.LazyPrintf("Got %d keys from client\n", len(gkeys.Keys))
}
if !x.IsTestRun() {
if !groups().ServesGroup(gkeys.GroupId) {
return x.Errorf("Group %d not served.", gkeys.GroupId)
}
n := groups().Node
if !n.AmLeader() {
return x.Errorf("Not leader of group: %d", gkeys.GroupId)
}
}
it := pstore.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
var count int
var gidx int
// Do NOT it.Next() by default. Be careful when you "continue" in loop!
for it.Rewind(); it.Valid(); {
iterItem := it.Item()
k := iterItem.Key()
pk := x.Parse(k)
if pk == nil {
it.Next()
continue
}
// No checksum check for schema keys
var v []byte
err := iterItem.Value(func(val []byte) error {
v = make([]byte, len(val))
// Copy it as we have to access it outside.
copy(v, val)
return nil
})
if err != nil {
return err
}
if !pk.IsSchema() {
var pl protos.PostingList
posting.UnmarshalOrCopy(v, iterItem.UserMeta(), &pl)
// If a key is present in follower but not in leader, send a kv with empty value
// so that the follower can delete it
for gidx < len(gkeys.Keys) && bytes.Compare(gkeys.Keys[gidx].Key, k) < 0 {
kv := &protos.KV{Key: gkeys.Keys[gidx].Key}
gidx++
if err := stream.Send(kv); err != nil {
return err
}
}
// Found a match.
if gidx < len(gkeys.Keys) && bytes.Compare(k, gkeys.Keys[gidx].Key) == 0 {
t := gkeys.Keys[gidx]
gidx++
// Different keys would have the same prefix. So, check Checksum first,
// it would be cheaper when there's no match.
// TODO: What if checksum collides ?
checksum := md5.Sum(v)
if bytes.Equal(checksum[:], t.Checksum) {
it.Next()
continue
}
}
}
// TODO - Verify that schema key is deleted from follower after leader deletes it.
// We just need to stream this kv. So, we can directly use the key
// and val without any copying.
kv := &protos.KV{
Key: k,
Val: v,
UserMeta: []byte{iterItem.UserMeta()},
}
count++
if err := stream.Send(kv); err != nil {
return err
}
it.Next()
} // end of iterator
// All these keys are not present in leader, so mark them for deletion
for gidx < len(gkeys.Keys) {
kv := &protos.KV{Key: gkeys.Keys[gidx].Key}
gidx++
if err := stream.Send(kv); err != nil {
return err
}
}
if tr, ok := trace.FromContext(stream.Context()); ok {
tr.LazyPrintf("Sent %d keys to client. Done.\n", count)
}
return nil
}