forked from dgraph-io/dgraph
/
main.go
402 lines (356 loc) · 11 KB
/
main.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
/*
* 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 main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net"
"net/http"
"os"
"runtime"
"strings"
"time"
"golang.org/x/net/context"
"golang.org/x/net/trace"
"google.golang.org/grpc"
"github.com/dgraph-io/dgraph/commit"
"github.com/dgraph-io/dgraph/gql"
"github.com/dgraph-io/dgraph/posting"
"github.com/dgraph-io/dgraph/query"
"github.com/dgraph-io/dgraph/query/graph"
"github.com/dgraph-io/dgraph/rdf"
"github.com/dgraph-io/dgraph/store"
"github.com/dgraph-io/dgraph/uid"
"github.com/dgraph-io/dgraph/worker"
"github.com/dgraph-io/dgraph/x"
)
var (
postingDir = flag.String("postings", "p", "Directory to store posting lists")
uidDir = flag.String("uids", "u", "XID UID posting lists directory")
mutationDir = flag.String("mutations", "m", "Directory to store mutations")
port = flag.Int("port", 8080, "Port to run server on.")
numcpu = flag.Int("numCpu", runtime.NumCPU(),
"Number of cores to be used by the process")
instanceIdx = flag.Uint64("instanceIdx", 0,
"serves only entities whose Fingerprint % numInstance == instanceIdx.")
workers = flag.String("workers", "",
"Comma separated list of IP addresses of workers")
workerPort = flag.String("workerport", ":12345",
"Port used by worker for internal communication.")
nomutations = flag.Bool("nomutations", false, "Don't allow mutations on this server.")
tracing = flag.Float64("trace", 0.5, "The ratio of queries to trace.")
)
func addCorsHeaders(w http.ResponseWriter) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers",
"Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token,"+
"X-Auth-Token, Cache-Control, X-Requested-With")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Connection", "close")
}
func convertToEdges(ctx context.Context, mutation string) ([]x.DirectedEdge, error) {
var edges []x.DirectedEdge
var nquads []rdf.NQuad
r := strings.NewReader(mutation)
scanner := bufio.NewScanner(r)
// Scanning the mutation string, one line at a time.
for scanner.Scan() {
ln := strings.Trim(scanner.Text(), " \t")
if len(ln) == 0 {
continue
}
nq, err := rdf.Parse(ln)
if err != nil {
x.Trace(ctx, "Error while parsing RDF: %v", err)
return edges, err
}
nquads = append(nquads, nq)
}
// xidToUid is used to store ids which are not uids. It is sent to the instance
// which has the xid <-> uid mapping to get uids.
xidToUid := make(map[string]uint64)
for _, nq := range nquads {
if !strings.HasPrefix(nq.Subject, "_uid_:") {
xidToUid[nq.Subject] = 0
}
if len(nq.ObjectId) > 0 && !strings.HasPrefix(nq.ObjectId, "_uid_:") {
xidToUid[nq.ObjectId] = 0
}
}
if len(xidToUid) > 0 {
if err := worker.GetOrAssignUidsOverNetwork(ctx, xidToUid); err != nil {
x.Trace(ctx, "Error while GetOrAssignUidsOverNetwork: %v", err)
return edges, err
}
}
for _, nq := range nquads {
// Get edges from nquad using xidToUid.
edge, err := nq.ToEdgeUsing(xidToUid)
if err != nil {
x.Trace(ctx, "Error while converting to edge: %v %v", nq, err)
return edges, err
}
edges = append(edges, edge)
}
return edges, nil
}
func mutationHandler(ctx context.Context, mu *gql.Mutation) error {
if *nomutations {
return fmt.Errorf("Mutations are forbidden on this server.")
}
var m worker.Mutations
var err error
if m.Set, err = convertToEdges(ctx, mu.Set); err != nil {
return err
}
if m.Del, err = convertToEdges(ctx, mu.Del); err != nil {
return err
}
left, err := worker.MutateOverNetwork(ctx, m)
if err != nil {
x.Trace(ctx, "Error while MutateOverNetwork: %v", err)
return err
}
if len(left.Set) > 0 || len(left.Del) > 0 {
x.Trace(ctx, "%d edges couldn't be applied", len(left.Del)+len(left.Set))
for _, e := range left.Set {
x.Trace(ctx, "Unable to apply set mutation for edge: %v", e)
}
for _, e := range left.Del {
x.Trace(ctx, "Unable to apply delete mutation for edge: %v", e)
}
return fmt.Errorf("Unapplied mutations")
}
return nil
}
func queryHandler(w http.ResponseWriter, r *http.Request) {
addCorsHeaders(w)
if r.Method == "OPTIONS" {
return
}
if r.Method != "POST" {
x.SetStatus(w, x.ErrorInvalidMethod, "Invalid method")
return
}
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
if rand.Float64() < *tracing {
tr := trace.New("Dgraph", "Query")
defer tr.Finish()
ctx = trace.NewContext(ctx, tr)
}
var l query.Latency
l.Start = time.Now()
defer r.Body.Close()
q, err := ioutil.ReadAll(r.Body)
if err != nil || len(q) == 0 {
x.Trace(ctx, "Error while reading query: %v", err)
x.SetStatus(w, x.ErrorInvalidRequest, "Invalid request encountered.")
return
}
x.Trace(ctx, "Query received: %v", string(q))
gq, mu, err := gql.Parse(string(q))
if err != nil {
x.Trace(ctx, "Error while parsing query: %v", err)
x.SetStatus(w, x.ErrorInvalidRequest, err.Error())
return
}
// If we have mutations, run them first.
if mu != nil && (len(mu.Set) > 0 || len(mu.Del) > 0) {
if err = mutationHandler(ctx, mu); err != nil {
x.Trace(ctx, "Error while handling mutations: %v", err)
x.SetStatus(w, x.Error, err.Error())
return
}
}
if gq == nil || (gq.UID == 0 && len(gq.XID) == 0) {
x.SetStatus(w, x.ErrorOk, "Done")
return
}
sg, err := query.ToSubGraph(ctx, gq)
if err != nil {
x.Trace(ctx, "Error while conversion to internal format: %v", err)
x.SetStatus(w, x.ErrorInvalidRequest, err.Error())
return
}
l.Parsing = time.Since(l.Start)
x.Trace(ctx, "Query parsed")
rch := make(chan error)
go query.ProcessGraph(ctx, sg, rch)
err = <-rch
if err != nil {
x.Trace(ctx, "Error while executing query: %v", err)
x.SetStatus(w, x.Error, err.Error())
return
}
l.Processing = time.Since(l.Start) - l.Parsing
x.Trace(ctx, "Graph processed")
js, err := sg.ToJSON(&l)
if err != nil {
x.Trace(ctx, "Error while converting to Json: %v", err)
x.SetStatus(w, x.Error, err.Error())
return
}
x.Trace(ctx, "Latencies: Total: %v Parsing: %v Process: %v Json: %v",
time.Since(l.Start), l.Parsing, l.Processing, l.Json)
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
// server is used to implement graph.DgraphServer
type server struct{}
// This method is used to execute the query and return the response to the
// client as a protocol buffer message.
func (s *server) Query(ctx context.Context,
req *graph.Request) (*graph.Response, error) {
if rand.Float64() < *tracing {
tr := trace.New("Dgraph", "GrpcQuery")
defer tr.Finish()
ctx = trace.NewContext(ctx, tr)
}
resp := new(graph.Response)
if len(req.Query) == 0 {
x.Trace(ctx, "Empty query")
return resp, fmt.Errorf("Empty query")
}
var l query.Latency
l.Start = time.Now()
// TODO(pawan): Refactor query parsing and graph processing code to a common
// function used by Query and queryHandler
x.Trace(ctx, "Query received: %v", req.Query)
gq, mu, err := gql.Parse(req.Query)
if err != nil {
x.Trace(ctx, "Error while parsing query: %v", err)
return resp, err
}
// If we have mutations, run them first.
if mu != nil && (len(mu.Set) > 0 || len(mu.Del) > 0) {
if err = mutationHandler(ctx, mu); err != nil {
x.Trace(ctx, "Error while handling mutations: %v", err)
return resp, err
}
}
if gq == nil || (gq.UID == 0 && len(gq.XID) == 0) {
return resp, err
}
sg, err := query.ToSubGraph(ctx, gq)
if err != nil {
x.Trace(ctx, "Error while conversion to internal format: %v", err)
return resp, err
}
l.Parsing = time.Since(l.Start)
x.Trace(ctx, "Query parsed")
rch := make(chan error)
go query.ProcessGraph(ctx, sg, rch)
err = <-rch
if err != nil {
x.Trace(ctx, "Error while executing query: %v", err)
return resp, err
}
l.Processing = time.Since(l.Start) - l.Parsing
x.Trace(ctx, "Graph processed")
node, err := sg.ToProtocolBuffer(&l)
if err != nil {
x.Trace(ctx, "Error while converting to ProtocolBuffer: %v", err)
return resp, err
}
resp.N = node
gl := new(graph.Latency)
gl.Parsing, gl.Processing, gl.Pb = l.Parsing.String(), l.Processing.String(),
l.ProtocolBuffer.String()
resp.L = gl
return resp, err
}
// This function register a Dgraph grpc server on the address, which is used
// exchanging protocol buffer messages.
func runGrpcServer(address string) {
ln, err := net.Listen("tcp", address)
if err != nil {
log.Fatalf("While running server for client: %v", err)
return
}
log.Printf("Client worker listening: %v", ln.Addr())
s := grpc.NewServer(grpc.CustomCodec(&query.Codec{}))
graph.RegisterDgraphServer(s, &server{})
if err = s.Serve(ln); err != nil {
log.Fatalf("While serving gRpc requests: %v", err)
}
return
}
func main() {
x.Init()
numCpus := *numcpu
prev := runtime.GOMAXPROCS(numCpus)
log.Printf("num_cpu: %v. prev_maxprocs: %v. Set max procs to num cpus", numCpus, prev)
if *port%2 != 0 {
log.Fatalf("Port must be an even number: %v", *port)
}
// Create parent directories for postings, uids and mutations
var err error
err = os.MkdirAll(*postingDir, 0700)
if err != nil {
log.Fatalf("Error while creating the filepath for postings: %v", err)
}
err = os.MkdirAll(*mutationDir, 0700)
if err != nil {
log.Fatalf("Error while creating the filepath for mutations: %v", err)
}
err = os.MkdirAll(*uidDir, 0700)
if err != nil {
log.Fatalf("Error while creating the filepath for uids: %v", err)
}
ps, err := store.NewStore(*postingDir)
if err != nil {
log.Fatalf("error initializing postings store: %v", err)
}
defer ps.Close()
clog := commit.NewLogger(*mutationDir, "dgraph", 50<<20)
clog.SyncEvery = 1
clog.Init()
defer clog.Close()
addrs := strings.Split(*workers, ",")
lenAddr := uint64(len(addrs))
if lenAddr == 0 {
// If no worker is specified, then we're it.
lenAddr = 1
}
posting.Init(clog)
if *instanceIdx != 0 {
worker.InitState(ps, nil, *instanceIdx, lenAddr)
uid.Init(nil)
} else {
uidStore, err := store.NewStore(*uidDir)
if err != nil {
log.Fatalf("error initializing uid store: %s", err)
}
defer uidStore.Close()
// Only server instance 0 will have uidStore
worker.InitState(ps, uidStore, *instanceIdx, lenAddr)
uid.Init(uidStore)
}
worker.Connect(addrs, *workerPort)
// Grpc server runs on (port + 1)
go runGrpcServer(fmt.Sprintf(":%d", *port+1))
http.HandleFunc("/query", queryHandler)
log.Printf("Listening for requests at port: %v", *port)
if err := http.ListenAndServe(fmt.Sprintf(":%d", *port), nil); err != nil {
log.Fatalf("ListenAndServe: %v", err)
}
}