forked from dgraph-io/dgraph
/
query.go
685 lines (609 loc) · 16.7 KB
/
query.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
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/*
* Copyright 2015 DGraph Labs, Inc.
*
* 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 query
import (
"bytes"
"container/heap"
"encoding/json"
"errors"
"fmt"
"log"
"strconv"
"sync"
"time"
"github.com/google/flatbuffers/go"
"golang.org/x/net/context"
"github.com/dgraph-io/dgraph/gql"
"github.com/dgraph-io/dgraph/query/graph"
"github.com/dgraph-io/dgraph/task"
"github.com/dgraph-io/dgraph/worker"
"github.com/dgraph-io/dgraph/x"
)
/*
* QUERY:
* Let's take this query from GraphQL as example:
* {
* me {
* id
* firstName
* lastName
* birthday {
* month
* day
* }
* friends {
* name
* }
* }
* }
*
* REPRESENTATION:
* This would be represented in SubGraph format internally, as such:
* SubGraph [result uid = me]
* |
* Children
* |
* --> SubGraph [Attr = "xid"]
* --> SubGraph [Attr = "firstName"]
* --> SubGraph [Attr = "lastName"]
* --> SubGraph [Attr = "birthday"]
* |
* Children
* |
* --> SubGraph [Attr = "month"]
* --> SubGraph [Attr = "day"]
* --> SubGraph [Attr = "friends"]
* |
* Children
* |
* --> SubGraph [Attr = "name"]
*
* ALGORITHM:
* This is a rough and simple algorithm of how to process this SubGraph query
* and populate the results:
*
* For a given entity, a new SubGraph can be started off with NewGraph(id).
* Given a SubGraph, is the Query field empty? [Step a]
* - If no, run (or send it to server serving the attribute) query
* and populate result.
* Iterate over children and copy Result Uids to child Query Uids.
* Set Attr. Then for each child, use goroutine to run Step:a.
* Wait for goroutines to finish.
* Return errors, if any.
*/
type Latency struct {
Start time.Time `json:"-"`
Parsing time.Duration `json:"query_parsing"`
Processing time.Duration `json:"processing"`
Json time.Duration `json:"json_conversion"`
ProtocolBuffer time.Duration `json:"pb_conversion"`
}
func (l *Latency) ToMap() map[string]string {
m := make(map[string]string)
j := time.Since(l.Start) - l.Processing - l.Parsing
m["parsing"] = l.Parsing.String()
m["processing"] = l.Processing.String()
m["json"] = j.String()
m["total"] = time.Since(l.Start).String()
return m
}
// SubGraph is the way to represent data internally. It contains both the
// query and the response. Once generated, this can then be encoded to other
// client convenient formats, like GraphQL / JSON.
type SubGraph struct {
Attr string
Count int
Offset int
AfterUid uint64
GetCount uint16
Children []*SubGraph
IsRoot bool
GetUid bool
isDebug bool
Query []byte
Result []byte
}
func mergeInterfaces(i1 interface{}, i2 interface{}) interface{} {
switch i1.(type) {
case map[string]interface{}:
m1 := i1.(map[string]interface{})
if m2, ok := i2.(map[string]interface{}); ok {
for k1, v1 := range m1 {
m2[k1] = v1
}
return m2
}
break
}
return []interface{}{i1, i2}
}
func postTraverse(g *SubGraph) (result map[uint64]interface{}, rerr error) {
if len(g.Query) == 0 {
return result, nil
}
result = make(map[uint64]interface{})
// Get results from all children first.
cResult := make(map[uint64]interface{})
for _, child := range g.Children {
m, err := postTraverse(child)
if err != nil {
return result, err
}
// Merge results from all children, one by one.
for k, v := range m {
if val, present := cResult[k]; !present {
cResult[k] = v
} else {
cResult[k] = mergeInterfaces(val, v)
}
}
}
// Now read the query and results at current node.
uo := flatbuffers.GetUOffsetT(g.Query)
q := new(task.Query)
q.Init(g.Query, uo)
ro := flatbuffers.GetUOffsetT(g.Result)
r := new(task.Result)
r.Init(g.Result, ro)
if q.UidsLength() != r.UidmatrixLength() {
log.Fatalf("Result uidmatrixlength: %v. Query uidslength: %v",
r.UidmatrixLength(), q.UidsLength())
}
if q.UidsLength() != r.ValuesLength() {
log.Fatalf("Result valuelength: %v. Query uidslength: %v",
r.ValuesLength(), q.UidsLength())
}
// Generate a matrix of maps
// Row -> .....
// Col
// |
// v
// map{_uid_ = uid}
// If some result is present from children results, then merge.
// Otherwise, this would only contain the _uid_ property.
// result[uid in row] = map[cur attribute ->
// list of maps of {uid, uid + children result}]
//
for i := 0; i < r.CountLength(); i++ {
co := r.Count(i)
m := make(map[string]interface{})
m["_count_"] = co
mp := make(map[string]interface{})
mp[g.Attr] = m
result[q.Uids(i)] = mp
}
var ul task.UidList
for i := 0; i < r.UidmatrixLength(); i++ {
if ok := r.Uidmatrix(&ul, i); !ok {
return result, fmt.Errorf("While parsing UidList")
}
l := make([]interface{}, ul.UidsLength())
for j := 0; j < ul.UidsLength(); j++ {
uid := ul.Uids(j)
m := make(map[string]interface{})
if g.GetUid || g.isDebug {
m["_uid_"] = fmt.Sprintf("%#x", uid)
}
if ival, present := cResult[uid]; !present {
l[j] = m
} else {
l[j] = mergeInterfaces(m, ival)
}
}
if len(l) > 0 {
m := make(map[string]interface{})
m[g.Attr] = l
result[q.Uids(i)] = m
}
// TODO(manish): Check what happens if we handle len(l) == 1 separately.
}
var tv task.Value
for i := 0; i < r.ValuesLength(); i++ {
if ok := r.Values(&tv, i); !ok {
return result, fmt.Errorf("While parsing value")
}
val := tv.ValBytes()
if bytes.Equal(val, nil) {
// We do this, because we typically do set values, even though
// they might be nil. This is to ensure that the index of the query uids
// and the index of the results can remain in sync.
continue
}
if pval, present := result[q.Uids(i)]; present {
log.Fatalf("prev: %v _uid_: %v new: %v"+
" Previous value detected. A uid -> list of uids / value. Not both",
pval, q.Uids(i), val)
}
m := make(map[string]interface{})
if g.GetUid || g.isDebug {
m["_uid_"] = fmt.Sprintf("%#x", q.Uids(i))
}
m[g.Attr] = string(val)
result[q.Uids(i)] = m
}
return result, nil
}
func (g *SubGraph) ToJson(l *Latency) (js []byte, rerr error) {
r, err := postTraverse(g)
if err != nil {
return js, err
}
l.Json = time.Since(l.Start) - l.Parsing - l.Processing
if len(r) != 1 {
log.Fatal("We don't currently support more than 1 uid at root.")
}
// r is a map, and we don't know it's key. So iterate over it, even though it only has 1 result.
for _, ival := range r {
var m map[string]interface{}
if ival != nil {
m = ival.(map[string]interface{})
} else {
m = make(map[string]interface{})
}
if g.isDebug {
m["server_latency"] = l.ToMap()
}
return json.Marshal(m)
}
log.Fatal("Runtime should never reach here.")
return []byte(""), fmt.Errorf("Runtime should never reach here.")
}
// This function performs a binary search on the uids slice and returns the
// index at which it finds the uid, else returns -1
func indexOf(uid uint64, q *task.Query) int {
low, mid, high := 0, 0, q.UidsLength()-1
for low <= high {
mid = (low + high) / 2
if q.Uids(mid) == uid {
return mid
} else if q.Uids(mid) > uid {
high = mid - 1
} else {
low = mid + 1
}
}
return -1
}
var nodePool = sync.Pool{
New: func() interface{} {
return &graph.Node{}
},
}
var nodeCh chan *graph.Node
func release() {
for n := range nodeCh {
// In case of mutations, n is nil
if n == nil {
continue
}
for i := 0; i < len(n.Children); i++ {
nodeCh <- n.Children[i]
}
*n = graph.Node{}
nodePool.Put(n)
}
}
func init() {
nodeCh = make(chan *graph.Node, 1000)
go release()
}
// This method gets the values and children for a subgraph.
func (g *SubGraph) preTraverse(uid uint64, dst *graph.Node) error {
var properties []*graph.Property
var children []*graph.Node
// We go through all predicate children of the subgraph.
for _, pc := range g.Children {
ro := flatbuffers.GetUOffsetT(pc.Result)
r := new(task.Result)
r.Init(pc.Result, ro)
uo := flatbuffers.GetUOffsetT(pc.Query)
q := new(task.Query)
q.Init(pc.Query, uo)
idx := indexOf(uid, q)
if idx == -1 {
log.Fatal("Attribute with uid not found in child Query uids.")
return fmt.Errorf("Attribute with uid not found")
}
var ul task.UidList
var tv task.Value
if ok := r.Uidmatrix(&ul, idx); !ok {
return fmt.Errorf("While parsing UidList")
}
if r.CountLength() > 0 {
uc := new(graph.Node)
uc.Attribute = pc.Attr
count := strconv.Itoa(int(r.Count(idx)))
p := &graph.Property{Prop: "_count_", Val: []byte(count)}
uc.Properties = []*graph.Property{p}
children = append(children, uc)
} else if ul.UidsLength() > 0 {
// We create as many predicate entity children as the length of uids for
// this predicate.
for i := 0; i < ul.UidsLength(); i++ {
uid := ul.Uids(i)
uc := nodePool.Get().(*graph.Node)
uc.Attribute = pc.Attr
uc.Uid = uid
if rerr := pc.preTraverse(uid, uc); rerr != nil {
log.Printf("Error while traversal: %v", rerr)
return rerr
}
children = append(children, uc)
}
} else {
if ok := r.Values(&tv, idx); !ok {
return fmt.Errorf("While parsing value")
}
v := tv.ValBytes()
if pc.Attr == "_xid_" {
dst.Xid = string(v)
} else {
p := &graph.Property{Prop: pc.Attr, Val: v}
properties = append(properties, p)
}
}
}
dst.Properties, dst.Children = properties, children
return nil
}
// This method transforms the predicate based subgraph to an
// predicate-entity based protocol buffer subgraph.
func (g *SubGraph) ToProtocolBuffer(l *Latency) (n *graph.Node, rerr error) {
n = &graph.Node{}
n.Attribute = g.Attr
if len(g.Query) == 0 {
return n, nil
}
ro := flatbuffers.GetUOffsetT(g.Result)
r := new(task.Result)
r.Init(g.Result, ro)
var ul task.UidList
r.Uidmatrix(&ul, 0)
n.Uid = ul.Uids(0)
if rerr = g.preTraverse(n.Uid, n); rerr != nil {
return n, rerr
}
l.ProtocolBuffer = time.Since(l.Start) - l.Parsing - l.Processing
return n, nil
}
func treeCopy(gq *gql.GraphQuery, sg *SubGraph) error {
// Typically you act on the current node, and leave recursion to deal with
// children. But, in this case, we don't want to muck with the current
// node, because of the way we're dealing with the root node.
// So, we work on the children, and then recurse for grand children.
for _, gchild := range gq.Children {
if gchild.Attr == "_count_" {
if len(gq.Children) > 1 {
return errors.New("Cannot have other attributes with count")
}
if gchild.Children != nil {
return errors.New("Count cannot have other attributes")
}
sg.GetCount = 1
break
}
if gchild.Attr == "_uid_" {
sg.GetUid = true
}
dst := new(SubGraph)
if sg.isDebug {
dst.isDebug = true
}
dst.Attr = gchild.Attr
dst.Offset = gchild.Offset
dst.AfterUid = gchild.After
dst.Count = gchild.First
sg.Children = append(sg.Children, dst)
err := treeCopy(gchild, dst)
if err != nil {
return err
}
}
return nil
}
func ToSubGraph(ctx context.Context, gq *gql.GraphQuery) (*SubGraph, error) {
sg, err := newGraph(ctx, gq)
if err != nil {
return nil, err
}
err = treeCopy(gq, sg)
return sg, err
}
func newGraph(ctx context.Context, gq *gql.GraphQuery) (*SubGraph, error) {
euid, exid := gq.UID, gq.XID
// This would set the Result field in SubGraph,
// and populate the children for attributes.
if len(exid) > 0 {
xidToUid := make(map[string]uint64)
xidToUid[exid] = 0
if err := worker.GetOrAssignUidsOverNetwork(ctx, &xidToUid); err != nil {
x.Trace(ctx, "Error while getting uids over network: %v", err)
return nil, err
}
euid = xidToUid[exid]
x.Trace(ctx, "Xid: %v Uid: %v", exid, euid)
}
if euid == 0 {
err := fmt.Errorf("Query internal id is zero")
x.Trace(ctx, "Invalid query: %v", err)
return nil, err
}
// Encode uid into result flatbuffer.
b := flatbuffers.NewBuilder(0)
omatrix := x.UidlistOffset(b, []uint64{euid})
// Also need to add nil value to keep this consistent.
var voffset flatbuffers.UOffsetT
{
bvo := b.CreateByteVector(x.Nilbyte)
task.ValueStart(b)
task.ValueAddVal(b, bvo)
voffset = task.ValueEnd(b)
}
task.ResultStartUidmatrixVector(b, 1)
b.PrependUOffsetT(omatrix)
mend := b.EndVector(1)
task.ResultStartValuesVector(b, 1)
b.PrependUOffsetT(voffset)
vend := b.EndVector(1)
task.ResultStart(b)
task.ResultAddUidmatrix(b, mend)
task.ResultAddValues(b, vend)
rend := task.ResultEnd(b)
b.Finish(rend)
sg := new(SubGraph)
if gq.Attr == "debug" {
sg.isDebug = true
}
sg.Attr = gq.Attr
sg.IsRoot = true
sg.Result = b.Bytes[b.Head():]
// Also add query for consistency and to allow for ToJson() later.
sg.Query = createTaskQuery(sg, []uint64{euid})
return sg, nil
}
// createTaskQuery generates the query buffer.
func createTaskQuery(sg *SubGraph, sorted []uint64) []byte {
b := flatbuffers.NewBuilder(0)
ao := b.CreateString(sg.Attr)
task.QueryStartUidsVector(b, len(sorted))
for i := len(sorted) - 1; i >= 0; i-- {
b.PrependUint64(sorted[i])
}
vend := b.EndVector(len(sorted))
task.QueryStart(b)
task.QueryAddAttr(b, ao)
task.QueryAddUids(b, vend)
task.QueryAddCount(b, int32(sg.Count))
task.QueryAddOffset(b, int32(sg.Offset))
task.QueryAddAfterUid(b, uint64(sg.AfterUid))
task.QueryAddGetCount(b, sg.GetCount)
qend := task.QueryEnd(b)
b.Finish(qend)
return b.Bytes[b.Head():]
}
type ListChannel struct {
TList *task.UidList
Idx int
}
func sortedUniqueUids(r *task.Result) (sorted []uint64, rerr error) {
// Let's serialize the matrix of uids in result to a
// sorted unique list of uids.
h := &x.Uint64Heap{}
heap.Init(h)
channels := make([]*ListChannel, r.UidmatrixLength())
for i := 0; i < r.UidmatrixLength(); i++ {
tlist := new(task.UidList)
if ok := r.Uidmatrix(tlist, i); !ok {
return sorted, fmt.Errorf("While parsing Uidmatrix")
}
if tlist.UidsLength() > 0 {
e := x.Elem{
Uid: tlist.Uids(0),
Idx: i,
}
heap.Push(h, e)
}
channels[i] = &ListChannel{TList: tlist, Idx: 1}
}
// The resulting list of uids will be stored here.
sorted = make([]uint64, 100)
sorted = sorted[:0]
var last uint64
last = 0
// Itearate over the heap.
for h.Len() > 0 {
me := (*h)[0] // Peek at the top element in heap.
if me.Uid != last {
sorted = append(sorted, me.Uid) // Add if unique.
last = me.Uid
}
lc := channels[me.Idx]
if lc.Idx >= lc.TList.UidsLength() {
heap.Pop(h)
} else {
uid := lc.TList.Uids(lc.Idx)
lc.Idx += 1
me.Uid = uid
(*h)[0] = me
heap.Fix(h, 0) // Faster than Pop() followed by Push().
}
}
return sorted, nil
}
func ProcessGraph(ctx context.Context, sg *SubGraph, rch chan error) {
var err error
if len(sg.Query) > 0 && !sg.IsRoot {
sg.Result, err = worker.ProcessTaskOverNetwork(ctx, sg.Query)
if err != nil {
x.Trace(ctx, "Error while processing task: %v", err)
rch <- err
return
}
}
uo := flatbuffers.GetUOffsetT(sg.Result)
r := new(task.Result)
r.Init(sg.Result, uo)
if r.ValuesLength() > 0 {
var v task.Value
if r.Values(&v, 0) {
x.Trace(ctx, "Sample value for attr: %v Val: %v", sg.Attr, string(v.ValBytes()))
}
}
if sg.GetCount == 1 {
x.Trace(ctx, "Zero uids. Only count requested")
rch <- nil
return
}
sorted, err := sortedUniqueUids(r)
if err != nil {
x.Trace(ctx, "Error while processing task: %v", err)
rch <- err
return
}
if len(sorted) == 0 {
// Looks like we're done here.
x.Trace(ctx, "Zero uids. Num attr children: %v", len(sg.Children))
rch <- nil
return
}
// Let's execute it in a tree fashion. Each SubGraph would break off
// as many goroutines as it's children; which would then recursively
// do the same thing.
// Buffered channel to ensure no-blockage.
childchan := make(chan error, len(sg.Children))
for i := 0; i < len(sg.Children); i++ {
child := sg.Children[i]
child.Query = createTaskQuery(child, sorted)
go ProcessGraph(ctx, child, childchan)
}
// Now get all the results back.
for i := 0; i < len(sg.Children); i++ {
select {
case err = <-childchan:
x.Trace(ctx, "Reply from child. Index: %v Attr: %v", i, sg.Children[i].Attr)
if err != nil {
x.Trace(ctx, "Error while processing child task: %v", err)
rch <- err
return
}
case <-ctx.Done():
x.Trace(ctx, "Context done before full execution: %v", ctx.Err())
rch <- ctx.Err()
return
}
}
rch <- nil
}