-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathx.go
505 lines (447 loc) · 12 KB
/
x.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
/*
* Copyright 2015-2018 Dgraph Labs, Inc. and Contributors
*
* 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 x
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"log"
"math"
"math/rand"
"net"
"net/http"
"os"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/dgraph-io/dgo"
"github.com/dgraph-io/dgo/protos/api"
"go.opencensus.io/trace"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/encoding/gzip"
)
// Error constants representing different types of errors.
var (
ErrNotSupported = fmt.Errorf("Feature available only in Dgraph Enterprise Edition")
)
const (
Success = "Success"
ErrorUnauthorized = "ErrorUnauthorized"
ErrorInvalidMethod = "ErrorInvalidMethod"
ErrorInvalidRequest = "ErrorInvalidRequest"
Error = "Error"
ErrorNoData = "ErrorNoData"
ValidHostnameRegex = "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])$"
// When changing this value also remember to change in in client/client.go:DeleteEdges.
Star = "_STAR_ALL"
// Use the max possible grpc msg size for the most flexibility (4GB - equal
// to the max grpc frame size). Users will still need to set the max
// message sizes allowable on the client size when dialing.
GrpcMaxSize = 4 << 30
// The attr used to store list of predicates for a node.
PredicateListAttr = "_predicate_"
PortZeroGrpc = 5080
PortZeroHTTP = 6080
PortInternal = 7080
PortHTTP = 8080
PortGrpc = 9080
// If the difference between AppliedUntil - TxnMarks.DoneUntil() is greater than this, we
// start aborting old transactions.
ForceAbortDifference = 5000
TlsClientCert = "client.crt"
TlsClientKey = "client.key"
GrootId = "groot"
)
var (
// Useful for running multiple servers on the same machine.
regExpHostName = regexp.MustCompile(ValidHostnameRegex)
Nilbyte []byte
)
func ShouldCrash(err error) bool {
if err == nil {
return false
}
errStr := grpc.ErrorDesc(err)
return strings.Contains(errStr, "REUSE_RAFTID") ||
strings.Contains(errStr, "REUSE_ADDR") ||
strings.Contains(errStr, "NO_ADDR")
}
// WhiteSpace Replacer removes spaces and tabs from a string.
var WhiteSpace = strings.NewReplacer(" ", "", "\t", "")
type errRes struct {
Code string `json:"code"`
Message string `json:"message"`
}
type queryRes struct {
Errors []errRes `json:"errors"`
}
// SetStatus sets the error code, message and the newly assigned uids
// in the http response.
func SetStatus(w http.ResponseWriter, code, msg string) {
var qr queryRes
qr.Errors = append(qr.Errors, errRes{Code: code, Message: msg})
if js, err := json.Marshal(qr); err == nil {
w.Write(js)
} else {
panic(fmt.Sprintf("Unable to marshal: %+v", qr))
}
}
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, X-Dgraph-CommitNow, X-Dgraph-Vars, "+
"X-Dgraph-MutationType, X-Dgraph-IgnoreIndexConflict")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Connection", "close")
}
type QueryResWithData struct {
Errors []errRes `json:"errors"`
Data *string `json:"data"`
}
// In case an error was encountered after the query execution started, we have to return data
// key with null value according to GraphQL spec.
func SetStatusWithData(w http.ResponseWriter, code, msg string) {
var qr QueryResWithData
qr.Errors = append(qr.Errors, errRes{Code: code, Message: msg})
// This would ensure that data key is present with value null.
if js, err := json.Marshal(qr); err == nil {
w.Write(js)
} else {
panic(fmt.Sprintf("Unable to marshal: %+v", qr))
}
}
func Reply(w http.ResponseWriter, rep interface{}) {
if js, err := json.Marshal(rep); err == nil {
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(js))
} else {
SetStatus(w, Error, "Internal server error")
}
}
func ParseRequest(w http.ResponseWriter, r *http.Request, data interface{}) bool {
defer r.Body.Close()
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&data); err != nil {
SetStatus(w, Error, fmt.Sprintf("While parsing request: %v", err))
return false
}
return true
}
func Min(a, b uint64) uint64 {
if a < b {
return a
}
return b
}
func Max(a, b uint64) uint64 {
if a > b {
return a
}
return b
}
func RetryUntilSuccess(maxRetries int, sleepDurationOnFailure time.Duration,
f func() error) error {
var err error
for retry := maxRetries; retry != 0; retry-- {
if err = f(); err == nil {
return nil
}
if sleepDurationOnFailure > 0 {
time.Sleep(sleepDurationOnFailure)
}
}
return err
}
func HasString(a []string, b string) bool {
for _, k := range a {
if k == b {
return true
}
}
return false
}
// Reads a single line from a buffered reader. The line is read into the
// passed in buffer to minimize allocations. This is the preferred
// method for loading long lines which could be longer than the buffer
// size of bufio.Scanner.
func ReadLine(r *bufio.Reader, buf *bytes.Buffer) error {
isPrefix := true
var err error
buf.Reset()
for isPrefix && err == nil {
var line []byte
// The returned line is an pb.buffer in bufio and is only
// valid until the next call to ReadLine. It needs to be copied
// over to our own buffer.
line, isPrefix, err = r.ReadLine()
if err == nil {
buf.Write(line)
}
}
return err
}
func FixedDuration(d time.Duration) string {
str := fmt.Sprintf("%02ds", int(d.Seconds())%60)
if d >= time.Minute {
str = fmt.Sprintf("%02dm", int(d.Minutes())%60) + str
}
if d >= time.Hour {
str = fmt.Sprintf("%02dh", int(d.Hours())) + str
}
return str
}
// PageRange returns start and end indices given pagination params. Note that n
// is the size of the input list.
func PageRange(count, offset, n int) (int, int) {
if n == 0 {
return 0, 0
}
if count == 0 && offset == 0 {
return 0, n
}
if count < 0 {
// Items from the back of the array, like Python arrays. Do a positive mod n.
if count*-1 > n {
count = -n
}
return (((n + count) % n) + n) % n, n
}
start := offset
if start < 0 {
start = 0
}
if start > n {
return n, n
}
if count == 0 { // No count specified. Just take the offset parameter.
return start, n
}
end := start + count
if end > n {
end = n
}
return start, end
}
// ValidateAddress checks whether given address can be used with grpc dial function
func ValidateAddress(addr string) bool {
host, port, err := net.SplitHostPort(addr)
if err != nil {
return false
}
if p, err := strconv.Atoi(port); err != nil || p <= 0 || p >= 65536 {
return false
}
if ip := net.ParseIP(host); ip != nil {
return true
}
// try to parse as hostname as per hostname RFC
if len(strings.Replace(host, ".", "", -1)) > 255 {
return false
}
return regExpHostName.MatchString(host)
}
// sorts the slice of strings and removes duplicates. changes the input slice.
// this function should be called like: someSlice = x.RemoveDuplicates(someSlice)
func RemoveDuplicates(s []string) (out []string) {
sort.Strings(s)
out = s[:0]
for i := range s {
if i > 0 && s[i] == s[i-1] {
continue
}
out = append(out, s[i])
}
return
}
type BytesBuffer struct {
data [][]byte
off int
sz int
}
func (b *BytesBuffer) grow(n int) {
if n < 128 {
n = 128
}
if len(b.data) == 0 {
b.data = append(b.data, make([]byte, n, n))
}
last := len(b.data) - 1
// Return if we have sufficient space
if len(b.data[last])-b.off >= n {
return
}
sz := len(b.data[last]) * 2
if sz > 512<<10 {
sz = 512 << 10 // 512 KB
}
if sz < n {
sz = n
}
b.data[last] = b.data[last][:b.off]
b.sz += len(b.data[last])
b.data = append(b.data, make([]byte, sz, sz))
b.off = 0
}
// returns a slice of length n to be used to writing
func (b *BytesBuffer) Slice(n int) []byte {
b.grow(n)
last := len(b.data) - 1
b.off += n
b.sz += n
return b.data[last][b.off-n : b.off]
}
func (b *BytesBuffer) Length() int {
return b.sz
}
// Caller should ensure that o is of appropriate length
func (b *BytesBuffer) CopyTo(o []byte) int {
offset := 0
for i, d := range b.data {
if i == len(b.data)-1 {
copy(o[offset:], d[:b.off])
offset += b.off
} else {
copy(o[offset:], d)
offset += len(d)
}
}
return offset
}
// Always give back <= touched bytes
func (b *BytesBuffer) TruncateBy(n int) {
b.off -= n
b.sz -= n
AssertTrue(b.off >= 0 && b.sz >= 0)
}
type Timer struct {
start time.Time
last time.Time
records []time.Duration
}
func (t *Timer) Start() {
t.start = time.Now()
t.last = t.start
t.records = t.records[:0]
}
func (t *Timer) Record() {
now := time.Now()
t.records = append(t.records, now.Sub(t.last))
t.last = now
}
func (t *Timer) Total() time.Duration {
return time.Since(t.start)
}
func (t *Timer) All() []time.Duration {
return t.records
}
// PredicateLang extracts the language from a predicate (or facet) name.
// Returns the predicate and the language tag, if any.
func PredicateLang(s string) (string, string) {
i := strings.LastIndex(s, "@")
if i <= 0 {
return s, ""
}
return s[0:i], s[i+1:]
}
func DivideAndRule(num int) (numGo, width int) {
numGo, width = 64, 0
for ; numGo >= 1; numGo /= 2 {
widthF := math.Ceil(float64(num) / float64(numGo))
if numGo == 1 || widthF >= 256.0 {
width = int(widthF)
return
}
}
return
}
func SetupConnection(host string, tlsConf *TLSHelperConfig, useGz bool) (*grpc.ClientConn, error) {
callOpts := append([]grpc.CallOption{},
grpc.MaxCallRecvMsgSize(GrpcMaxSize),
grpc.MaxCallSendMsgSize(GrpcMaxSize))
if useGz {
fmt.Fprintf(os.Stderr, "Using compression with %s\n", host)
callOpts = append(callOpts, grpc.UseCompressor(gzip.Name))
}
dialOpts := append([]grpc.DialOption{},
grpc.WithDefaultCallOptions(callOpts...),
grpc.WithBlock(),
grpc.WithTimeout(10*time.Second))
if tlsConf != nil && tlsConf.CertRequired {
tlsConf.ConfigType = TLSClientConfig
tlsCfg, _, err := GenerateTLSConfig(*tlsConf)
if err != nil {
return nil, err
}
dialOpts = append(dialOpts, grpc.WithTransportCredentials(credentials.NewTLS(tlsCfg)))
} else {
dialOpts = append(dialOpts, grpc.WithInsecure())
}
return grpc.Dial(host, dialOpts...)
}
func Diff(dst map[string]struct{}, src map[string]struct{}) ([]string, []string) {
var add []string
var del []string
for g := range dst {
if _, ok := src[g]; !ok {
add = append(add, g)
}
}
for g := range src {
if _, ok := dst[g]; !ok {
del = append(del, g)
}
}
return add, del
}
func SpanTimer(span *trace.Span, name string) func() {
if span == nil {
return func() {}
}
uniq := int64(rand.Int31())
attrs := []trace.Attribute{
trace.Int64Attribute("funcId", uniq),
trace.StringAttribute("funcName", name),
}
span.Annotate(attrs, "Start.")
start := time.Now()
return func() {
span.Annotatef(attrs, "End. Took %s", time.Since(start))
// TODO: We can look into doing a latency record here.
}
}
type CancelFunc func()
const DgraphAlphaPort = 9180
func GetDgraphClient() (*dgo.Dgraph, CancelFunc) {
return GetDgraphClientOnPort(DgraphAlphaPort)
}
func GetDgraphClientOnPort(alphaPort int) (*dgo.Dgraph, CancelFunc) {
conn, err := grpc.Dial(fmt.Sprintf("127.0.0.1:%d", alphaPort), grpc.WithInsecure())
if err != nil {
log.Fatal("While trying to dial gRPC")
}
dc := api.NewDgraphClient(conn)
return dgo.NewDgraphClient(dc), func() {
if err := conn.Close(); err != nil {
log.Printf("Error while closing connection:%v", err)
}
}
}