-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathbatch.go
More file actions
490 lines (428 loc) · 12.1 KB
/
batch.go
File metadata and controls
490 lines (428 loc) · 12.1 KB
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
* SPDX-FileCopyrightText: © Hypermode Inc. <[email protected]>
* SPDX-License-Identifier: Apache-2.0
*/
package live
import (
"context"
"fmt"
"math"
"math/rand"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/dgryski/go-farm"
"github.com/dustin/go-humanize"
"github.com/dustin/go-humanize/english"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/dgraph-io/badger/v4"
"github.com/dgraph-io/dgo/v250"
"github.com/dgraph-io/dgo/v250/protos/api"
"github.com/hypermodeinc/dgraph/v25/dql"
"github.com/hypermodeinc/dgraph/v25/protos/pb"
"github.com/hypermodeinc/dgraph/v25/tok"
"github.com/hypermodeinc/dgraph/v25/types"
"github.com/hypermodeinc/dgraph/v25/x"
"github.com/hypermodeinc/dgraph/v25/xidmap"
)
// batchMutationOptions sets the clients batch mode to Pending number of buffers each of Size.
// Running counters of number of rdfs processed, total time and mutations per second are printed
// if PrintCounters is set true. See Counter.
type batchMutationOptions struct {
Size int
Pending int
PrintCounters bool
MaxRetries uint32
// BufferSize is the number of requests that a live loader thread can store at a time
bufferSize int
// User could pass a context so that we can stop retrying requests once context is done
Ctx context.Context
}
// loader is the data structure held by the user program for all interactions with the Dgraph
// server. After making grpc connection a new Dgraph is created by function NewDgraphClient.
type loader struct {
opts batchMutationOptions
dc *dgo.Dgraph
alloc *xidmap.XidMap
ticker *time.Ticker
db *badger.DB
requestsWg sync.WaitGroup
// If we retry a request, we add one to retryRequestsWg.
retryRequestsWg sync.WaitGroup
// Miscellaneous information to print counters.
// Num of N-Quads sent
nquads uint64
// Num of txns sent
txns uint64
// Num of aborts
aborts uint64
// To get time elapsed
start time.Time
inflight int32
conc int32
conflicts map[uint64]struct{}
uidsLock sync.RWMutex
reqs chan *request
zeroconn *grpc.ClientConn
schema *Schema
namespaces map[uint64]struct{}
upsertLock sync.RWMutex
}
// Counter keeps a track of various parameters about a batch mutation. Running totals are printed
// if BatchMutationOptions PrintCounters is set to true.
type Counter struct {
// Number of N-Quads processed by server.
Nquads uint64
// Number of mutations processed by the server.
TxnsDone uint64
// Number of Aborts
Aborts uint64
// Time elapsed since the batch started.
Elapsed time.Duration
}
// handleError inspects errors and terminates if the errors are non-recoverable.
// A gRPC code is Internal if there is an unforeseen issue that needs attention.
// A gRPC code is Unavailable when we can't possibly reach the remote server, most likely the
// server expects TLS and our certificate does not match or the host name is not verified. When
// the node certificate is created the name much match the request host name. e.g., localhost not
// 127.0.0.1.
func handleError(err error, isRetry bool) {
s := status.Convert(err)
switch {
case s.Code() == codes.Internal, s.Code() == codes.Unavailable:
// Let us not crash live loader due to this. Instead, we should infinitely retry to
// reconnect and retry the request.
//nolint:gosec // random generator in closed set does not require cryptographic precision
dur := time.Duration(1+rand.Intn(60)) * time.Second
fmt.Printf("Connection has been possibly interrupted. Got error: %v."+
" Will retry after %s.\n", err, dur.Round(time.Second))
time.Sleep(dur)
case strings.Contains(s.Message(), "x509"):
x.Fatalf(s.Message())
case s.Code() == codes.Aborted:
if !isRetry && opt.verbose {
fmt.Printf("Transaction aborted. Will retry in background.\n")
}
case strings.Contains(s.Message(), "Server overloaded."):
//nolint:gosec // random generator in closed set does not require cryptographic precision
dur := time.Duration(1+rand.Intn(10)) * time.Minute
fmt.Printf("Server is overloaded. Will retry after %s.\n", dur.Round(time.Minute))
time.Sleep(dur)
case err != x.ErrConflict && err != dgo.ErrAborted:
fmt.Printf("Error while mutating: %v s.Code %v\n", s.Message(), s.Code())
}
}
func (l *loader) infinitelyRetry(req *request) {
defer l.retryRequestsWg.Done()
defer l.deregister(req)
nretries := 1
for i := time.Millisecond; ; i *= 2 {
err := l.mutate(req)
if err == nil {
if opt.verbose {
fmt.Printf("Transaction succeeded after %s.\n",
english.Plural(nretries, "retry", "retries"))
}
atomic.AddUint64(&l.nquads, uint64(len(req.Set)))
atomic.AddUint64(&l.txns, 1)
return
}
nretries++
handleError(err, true)
atomic.AddUint64(&l.aborts, 1)
if i >= 10*time.Second {
i = 10 * time.Second
}
l.deregister(req)
time.Sleep(i)
l.addConflictKeys(req)
}
}
func (l *loader) mutate(req *request) error {
atomic.AddInt32(&l.inflight, 1)
defer atomic.AddInt32(&l.inflight, -1)
txn := l.dc.NewTxn()
req.CommitNow = true
request := &api.Request{
CommitNow: true,
Mutations: []*api.Mutation{req.Mutation},
}
_, err := txn.Do(l.opts.Ctx, request)
return err
}
func (l *loader) request(req *request) {
err := l.mutate(req)
if err == nil {
atomic.AddUint64(&l.nquads, uint64(len(req.Set)))
atomic.AddUint64(&l.txns, 1)
l.deregister(req)
return
}
handleError(err, false)
atomic.AddUint64(&l.aborts, 1)
l.retryRequestsWg.Add(1)
go l.infinitelyRetry(req)
}
func getTypeVal(val *api.Value) (types.Val, error) {
p := dql.TypeValFrom(val)
// Convert value to bytes
if p.Tid == types.GeoID || p.Tid == types.DateTimeID {
// Already in bytes format
p.Value = p.Value.([]byte)
return p, nil
}
p1 := types.ValueForType(types.BinaryID)
if err := types.Marshal(p, &p1); err != nil {
return p1, err
}
p1.Value = p1.Value.([]byte)
p1.Tid = p.Tid
return p1, nil
}
func createUidEdge(nq *api.NQuad, sid, oid uint64) *pb.DirectedEdge {
return &pb.DirectedEdge{
Entity: sid,
Attr: nq.Predicate,
Namespace: nq.Namespace,
Lang: nq.Lang,
Facets: nq.Facets,
ValueId: oid,
ValueType: pb.Posting_UID,
}
}
func createValueEdge(nq *api.NQuad, sid uint64) (*pb.DirectedEdge, error) {
p := &pb.DirectedEdge{
Entity: sid,
Attr: nq.Predicate,
Namespace: nq.Namespace,
Lang: nq.Lang,
Facets: nq.Facets,
}
val, err := getTypeVal(nq.ObjectValue)
if err != nil {
return p, err
}
p.Value = val.Value.([]byte)
p.ValueType = val.Tid.Enum()
return p, nil
}
func fingerprintEdge(t *pb.DirectedEdge, pred *Predicate) uint64 {
var id uint64 = math.MaxUint64
// Value with a lang type.
if len(t.Lang) > 0 {
id = farm.Fingerprint64([]byte(t.Lang))
} else if pred.List {
id = farm.Fingerprint64(t.Value)
}
return id
}
func (l *loader) conflictKeysForNQuad(nq *api.NQuad) ([]uint64, error) {
attr := x.NamespaceAttr(nq.Namespace, nq.Predicate)
pred, found := l.schema.preds[attr]
// We don't need to generate conflict keys for predicate with noconflict directive.
if found && pred.NoConflict {
return nil, nil
}
keys := make([]uint64, 0)
// Calculates the conflict keys, inspired by the logic in
// addMutationInteration in posting/list.go.
sid, err := strconv.ParseUint(nq.Subject, 0, 64)
if err != nil {
return nil, err
}
var oid uint64
var de *pb.DirectedEdge
if nq.ObjectValue == nil {
oid, _ = strconv.ParseUint(nq.ObjectId, 0, 64)
de = createUidEdge(nq, sid, oid)
} else {
var err error
de, err = createValueEdge(nq, sid)
x.Check(err)
}
// If the predicate is not found in schema then we don't have to generate any more keys.
if !found {
return keys, nil
}
if pred.List {
key := fingerprintEdge(de, pred)
keys = append(keys, farm.Fingerprint64(x.DataKey(attr, sid))^key)
} else {
keys = append(keys, farm.Fingerprint64(x.DataKey(attr, sid)))
}
if pred.Reverse {
oi, err := strconv.ParseUint(nq.ObjectId, 0, 64)
if err != nil {
return keys, err
}
keys = append(keys, farm.Fingerprint64(x.DataKey(attr, oi)))
}
if nq.ObjectValue == nil || !(pred.Count || pred.Index) {
return keys, nil
}
val := sid
if pred.Upsert {
val = 0
}
errs := make([]string, 0)
for _, tokName := range pred.Tokenizer {
token, ok := tok.GetTokenizer(tokName)
if !ok {
fmt.Printf("unknown tokenizer %q", tokName)
continue
}
storageVal := types.Val{
Tid: types.TypeID(de.GetValueType()),
Value: de.GetValue(),
}
schemaVal, err := types.Convert(storageVal, pred.ValueType)
if err != nil {
errs = append(errs, err.Error())
}
toks, err := tok.BuildTokens(schemaVal.Value, tok.GetTokenizerForLang(token, nq.Lang))
if err != nil {
errs = append(errs, err.Error())
}
for _, t := range toks {
keys = append(keys, farm.Fingerprint64(x.IndexKey(attr, t))^val)
}
}
if len(errs) > 0 {
return keys, fmt.Errorf(strings.Join(errs, "\n"))
}
return keys, nil
}
func (l *loader) conflictKeysForReq(req *request) []uint64 {
// Live loader only needs to look at sets and not deletes
keys := make([]uint64, 0, len(req.Set))
for _, nq := range req.Set {
conflicts, err := l.conflictKeysForNQuad(nq)
if err != nil {
fmt.Println(err)
continue
}
keys = append(keys, conflicts...)
}
return keys
}
//lint:ignore U1000 Ignore unused function temporarily for debugging
func (l *loader) print(req *request) {
m := make(map[string]struct{})
for _, i := range req.Set {
m[i.Predicate] = struct{}{}
}
fmt.Println(m)
}
func (l *loader) addConflictKeys(req *request) bool {
l.uidsLock.Lock()
defer l.uidsLock.Unlock()
for _, key := range req.conflicts {
if _, ok := l.conflicts[key]; ok {
return false
}
}
for _, key := range req.conflicts {
l.conflicts[key] = struct{}{}
}
return true
}
func (l *loader) deregister(req *request) {
l.uidsLock.Lock()
defer l.uidsLock.Unlock()
for _, i := range req.conflicts {
delete(l.conflicts, i)
}
}
// makeRequests can receive requests from batchNquads or directly from BatchSetWithMark.
// It doesn't need to batch the requests anymore. Batching is already done for it by the
// caller functions.
func (l *loader) makeRequests() {
defer l.requestsWg.Done()
atomic.AddInt32(&l.conc, 1)
defer atomic.AddInt32(&l.conc, -1)
buffer := make([]*request, 0, l.opts.bufferSize)
var loops int
drain := func() {
i := 0
for _, req := range buffer {
loops++
// If there is no conflict in req, we will use it
// and then it would shift all the other reqs in buffer
if !l.addConflictKeys(req) {
buffer[i] = req
i++
continue
}
// Req will no longer be part of a buffer
l.request(req)
}
buffer = buffer[:i]
}
t := time.NewTicker(5 * time.Second)
defer t.Stop()
loop:
for {
select {
case req, ok := <-l.reqs:
if !ok {
break loop
}
req.conflicts = l.conflictKeysForReq(req)
if l.addConflictKeys(req) {
l.request(req)
} else {
buffer = append(buffer, req)
}
case <-t.C:
for {
drain()
if len(buffer) < l.opts.bufferSize {
break
}
}
}
}
for req := range l.reqs {
req.conflicts = l.conflictKeysForReq(req)
if l.addConflictKeys(req) {
l.request(req)
} else {
buffer = append(buffer, req)
}
drain()
time.Sleep(100 * time.Millisecond)
}
fmt.Printf("Looped %d times over buffered requests.\n", loops)
drain()
}
func (l *loader) printCounters() {
period := 5 * time.Second
l.ticker = time.NewTicker(period)
start := time.Now()
var last Counter
for range l.ticker.C {
counter := l.Counter()
rate := float64(counter.Nquads-last.Nquads) / period.Seconds()
elapsed := time.Since(start).Round(time.Second)
timestamp := time.Now().Format("15:04:05Z0700")
fmt.Printf("[%s] Elapsed: %s Txns: %d N-Quads: %s N-Quads/s: %s"+
" Inflight: %2d/%2d Aborts: %d\n",
timestamp, x.FixedDuration(elapsed), counter.TxnsDone,
humanize.Comma(int64(counter.Nquads)), humanize.Comma(int64(rate)),
atomic.LoadInt32(&l.inflight), atomic.LoadInt32(&l.conc), counter.Aborts)
last = counter
}
}
// Counter returns the current state of the BatchMutation.
func (l *loader) Counter() Counter {
return Counter{
Nquads: atomic.LoadUint64(&l.nquads),
TxnsDone: atomic.LoadUint64(&l.txns),
Elapsed: time.Since(l.start),
Aborts: atomic.LoadUint64(&l.aborts),
}
}