forked from dgraph-io/dgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
275 lines (242 loc) · 7.6 KB
/
task.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
/*
* Copyright 2016 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 worker
import (
"strings"
"golang.org/x/net/context"
"github.com/dgraph-io/dgraph/algo"
"github.com/dgraph-io/dgraph/group"
"github.com/dgraph-io/dgraph/posting"
"github.com/dgraph-io/dgraph/schema"
"github.com/dgraph-io/dgraph/task"
"github.com/dgraph-io/dgraph/types"
"github.com/dgraph-io/dgraph/x"
)
var (
emptyUIDList task.List
emptyResult task.Result
)
// ProcessTaskOverNetwork is used to process the query and get the result from
// the instance which stores posting list corresponding to the predicate in the
// query.
func ProcessTaskOverNetwork(ctx context.Context, q *task.Query) (*task.Result, error) {
attr := q.Attr
gid := group.BelongsTo(attr)
x.Trace(ctx, "attr: %v groupId: %v", attr, gid)
if groups().ServesGroup(gid) {
// No need for a network call, as this should be run from within this instance.
return processTask(q, gid)
}
// Send this over the network.
// TODO: Send the request to multiple servers as described in Jeff Dean's talk.
addr := groups().AnyServer(gid)
pl := pools().get(addr)
conn, err := pl.Get()
if err != nil {
return &emptyResult, x.Wrapf(err, "ProcessTaskOverNetwork: while retrieving connection.")
}
defer pl.Put(conn)
x.Trace(ctx, "Sending request to %v", addr)
c := NewWorkerClient(conn)
reply, err := c.ServeTask(ctx, q)
if err != nil {
x.TraceError(ctx, x.Wrapf(err, "Error while calling Worker.ServeTask"))
return &emptyResult, err
}
x.Trace(ctx, "Reply from server. length: %v Addr: %v Attr: %v",
len(reply.UidMatrix), addr, attr)
return reply, nil
}
// convertValue converts the data to the schema type of predicate.
func convertValue(attr, data string) (types.Val, error) {
// Parse given value and get token. There should be only one token.
t, err := schema.TypeOf(attr)
if err != nil || !t.IsScalar() {
return types.Val{}, x.Errorf("Attribute %s is not valid scalar type", attr)
}
src := types.Val{types.StringID, []byte(data)}
dst := types.ValueForType(t)
x.Check(types.Convert(src, &dst))
return dst, nil
}
// processTask processes the query, accumulates and returns the result.
func processTask(q *task.Query, gid uint32) (*task.Result, error) {
attr := q.Attr
useFunc := len(q.SrcFunc) != 0
var n int
var tokens []string
var geoQuery *types.GeoQueryData
var err error
var intersectDest bool
var ineqValue types.Val
var ineqValueToken string
var isGeq, isLeq bool
if useFunc {
f := q.SrcFunc[0]
isGeq = f == "geq"
isLeq = f == "leq"
switch {
case isGeq:
fallthrough
case isLeq:
if len(q.SrcFunc) != 2 {
return nil, x.Errorf("Function requires 2 arguments, but got %d %v",
len(q.SrcFunc), q.SrcFunc)
}
ineqValue, err = convertValue(attr, q.SrcFunc[1])
if err != nil {
return nil, err
}
// Tokenizing RHS value of inequality.
v := types.ValueForType(types.BinaryID)
x.Check(types.Marshal(ineqValue, &v))
ineqTokens, err := posting.IndexTokens(attr, types.Val{ineqValue.Tid, v.Value.([]byte)})
if err != nil {
return nil, err
}
if len(ineqTokens) != 1 {
return nil, x.Errorf("Expected only 1 token but got: %v", ineqTokens)
}
ineqValueToken = ineqTokens[0]
// Get tokens geq / leq ineqValueToken.
tokens, err = getInequalityTokens(attr, ineqValueToken, isGeq)
if err != nil {
return nil, err
}
case types.IsGeoFunc(q.SrcFunc[0]):
// For geo functions, we get extra information used for filtering.
tokens, geoQuery, err = types.GetGeoTokens(q.SrcFunc)
if err != nil {
return nil, err
}
default:
tokens, err = getTokens(q.SrcFunc)
if err != nil {
return nil, err
}
intersectDest = (strings.ToLower(q.SrcFunc[0]) == "allof")
}
n = len(tokens)
} else {
n = len(q.Uids)
}
var out task.Result
for i := 0; i < n; i++ {
var key []byte
if useFunc {
key = x.IndexKey(attr, tokens[i])
} else if q.Reverse {
key = x.ReverseKey(attr, q.Uids[i])
} else {
key = x.DataKey(attr, q.Uids[i])
}
// Get or create the posting list for an entity, attribute combination.
pl, decr := posting.GetOrCreate(key, gid)
defer decr()
// If a posting list contains a value, we store that or else we store a nil
// byte so that processing is consistent later.
val, err := pl.Value()
newValue := &task.Value{ValType: int32(val.Tid)}
if err == nil {
newValue.Val = val.Value.([]byte)
} else {
newValue.Val = x.Nilbyte
}
out.Values = append(out.Values, newValue)
if q.DoCount {
out.Counts = append(out.Counts, uint32(pl.Length(0)))
// Add an empty UID list to make later processing consistent
out.UidMatrix = append(out.UidMatrix, &emptyUIDList)
continue
}
// The more usual case: Getting the UIDs.
opts := posting.ListOptions{
AfterUID: uint64(q.AfterUid),
}
// If we have srcFunc and Uids, it means its a filter. So we intersect.
if useFunc && len(q.Uids) > 0 {
opts.Intersect = &task.List{Uids: q.Uids}
}
out.UidMatrix = append(out.UidMatrix, pl.Uids(opts))
}
if (isGeq || isLeq) && len(tokens) > 0 && ineqValueToken == tokens[0] {
// Need to evaluate inequality for entries in the first bucket.
typ, err := schema.TypeOf(attr)
if err != nil || !typ.IsScalar() {
return nil, x.Errorf("Attribute not scalar: %s %v", attr, typ)
}
x.AssertTrue(len(out.UidMatrix) > 0)
// Filter the first row of UidMatrix. Since ineqValue != nil, we may
// assume that ineqValue is equal to the first token found in TokensTable.
algo.ApplyFilter(out.UidMatrix[0], func(uid uint64, i int) bool {
sv, err := fetchValue(uid, attr, typ)
if sv.Value == nil || err != nil {
return false
}
if isGeq {
return !types.Less(sv, ineqValue)
}
return !types.Less(ineqValue, sv)
})
}
// If geo filter, do value check for correctness.
var values []*task.Value
if geoQuery != nil {
uids := algo.MergeSorted(out.UidMatrix)
for _, uid := range uids.Uids {
key := x.DataKey(attr, uid)
pl, decr := posting.GetOrCreate(key, gid)
val, err := pl.Value()
newValue := &task.Value{ValType: int32(val.Tid)}
if err == nil {
newValue.Val = val.Value.([]byte)
} else {
newValue.Val = x.Nilbyte
}
values = append(values, newValue)
decr() // Decrement the reference count of the pl.
}
filtered := types.FilterGeoUids(uids, values, geoQuery)
for i := 0; i < len(out.UidMatrix); i++ {
out.UidMatrix[i] = algo.IntersectSorted([]*task.List{out.UidMatrix[i], filtered})
}
}
out.IntersectDest = intersectDest
return &out, nil
}
// ServeTask is used to respond to a query.
func (w *grpcWorker) ServeTask(ctx context.Context, q *task.Query) (*task.Result, error) {
if ctx.Err() != nil {
return &emptyResult, ctx.Err()
}
gid := group.BelongsTo(q.Attr)
x.Trace(ctx, "Attribute: %q NumUids: %v groupId: %v ServeTask", q.Attr, len(q.Uids), gid)
var reply *task.Result
x.AssertTruef(groups().ServesGroup(gid),
"attr: %q groupId: %v Request sent to wrong server.", q.Attr, gid)
c := make(chan error, 1)
go func() {
var err error
reply, err = processTask(q, gid)
c <- err
}()
select {
case <-ctx.Done():
return reply, ctx.Err()
case err := <-c:
return reply, err
}
}