/
subs.go
591 lines (491 loc) · 11.2 KB
/
subs.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
package core
import (
"bytes"
"context"
"crypto/sha256"
"database/sql"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"math/rand"
"strconv"
"strings"
"sync"
"time"
"github.com/avast/retry-go"
"github.com/dosco/graphjin/core/internal/qcode"
"github.com/rs/xid"
)
const (
maxMembersPerWorker = 2000
errSubs = "subscription: %s: %s"
)
var (
minPollDuration = (200 * time.Millisecond)
)
type sub struct {
name string
role string
qc *queryComp
js json.RawMessage
add chan *Member
del chan *Member
updt chan mmsg
mval
sync.Once
}
type mval struct {
params []json.RawMessage
mi []minfo
res []chan *Result
ids []xid.ID
}
type minfo struct {
dh [sha256.Size]byte
values []interface{}
// index of cursor value in the arguments array
cindx int
}
type mmsg struct {
id xid.ID
dh [sha256.Size]byte
cursor string
}
type Member struct {
params json.RawMessage
sub *sub
Result chan *Result
done bool
id xid.ID
vl []interface{}
mm mmsg
// index of cursor value in the arguments array
cindx int
}
// GraphQLEx is the extended version of the Subscribe function allowing for request specific config.
func (g *GraphJin) Subscribe(
c context.Context,
query string,
vars json.RawMessage,
rc *ReqConfig) (*Member, error) {
var err error
gj := g.Load().(*graphjin)
op, name := qcode.GetQType(query)
if op != qcode.QTSubscription {
return nil, errors.New("subscription: not a subscription query")
}
if name == "" {
if gj.allowList != nil && gj.prod {
return nil, errors.New("subscription: query name is required")
} else {
h := sha256.Sum256([]byte(query))
name = base64.StdEncoding.EncodeToString(h[:])
}
}
var role string
if v, ok := c.Value(UserRoleKey).(string); ok {
role = v
} else if c.Value(UserIDKey) != nil {
role = "user"
} else {
role = "anon"
}
if role == "user" && gj.abacEnabled {
if role, err = gj.executeRoleQuery(c, nil, vars, rc); err != nil {
return nil, err
}
}
v, _ := gj.subs.LoadOrStore((name + role), &sub{
name: name,
role: role,
add: make(chan *Member),
del: make(chan *Member),
updt: make(chan mmsg, 10),
})
s := v.(*sub)
s.Do(func() {
err = gj.newSub(c, s, query, vars, rc)
})
if err != nil {
gj.subs.Delete((name + role))
return nil, err
}
args, err := gj.argList(c, s.qc.st.md, vars, rc)
if err != nil {
return nil, err
}
var params json.RawMessage
if len(args.values) != 0 {
if params, err = json.Marshal(args.values); err != nil {
return nil, err
}
}
m := &Member{
id: xid.New(),
Result: make(chan *Result, 10),
sub: s,
vl: args.values,
params: params,
cindx: args.cindx,
}
m.mm, err = gj.subFirstQuery(s, m, params)
if err != nil {
return nil, err
}
s.add <- m
return m, nil
}
func (gj *graphjin) newSub(c context.Context,
s *sub, query string, vars json.RawMessage, rc *ReqConfig) error {
var err error
qr := queryReq{
ns: gj.namespace,
op: qcode.QTSubscription,
name: s.name,
query: []byte(query),
vars: vars,
}
if rc != nil && rc.Namespace.Set {
qr.ns = rc.Namespace.Name
}
if s.qc, err = gj.compileQuery(qr, s.role); err != nil {
return err
}
if gj.allowList != nil && !gj.prod {
err := gj.allowList.Set(
vars,
query,
s.qc.st.qc.Metadata,
qr.ns)
if err != nil {
return err
}
}
if len(s.qc.st.md.Params()) != 0 {
s.qc.st.sql = renderSubWrap(s.qc.st, gj.schema.DBType())
}
go gj.subController(s)
return nil
}
func (gj *graphjin) subController(s *sub) {
defer gj.subs.Delete((s.name + s.role))
ps := gj.conf.SubsPollDuration
if ps < minPollDuration {
ps = minPollDuration
}
for {
select {
case m := <-s.add:
if err := s.addMember(m); err != nil {
gj.log.Printf(errSubs, "add-sub", err)
return
}
case m := <-s.del:
s.deleteMember(m)
if len(s.ids) == 0 {
return
}
case msg := <-s.updt:
if err := s.updateMember(msg); err != nil {
gj.log.Printf(errSubs, "update-sub", err)
return
}
case <-time.After(ps):
s.fanOutJobs(gj)
}
}
}
func (s *sub) addMember(m *Member) error {
mi := minfo{cindx: m.cindx}
if mi.cindx != -1 {
mi.values = m.vl
}
mi.dh = m.mm.dh
// if cindex is not -1 then this query contains
// a cursor that must be updated with the new
// cursor value so subscriptions can paginate.
if mi.cindx != -1 && m.mm.cursor != "" {
mi.values[mi.cindx] = m.mm.cursor
// values is a pre-generated json value that
// must be re-created.
if v, err := json.Marshal(mi.values); err != nil {
return nil
} else {
m.params = v
}
}
s.params = append(s.params, m.params)
s.mi = append(s.mi, mi)
s.res = append(s.res, m.Result)
s.ids = append(s.ids, m.id)
return nil
}
func (s *sub) deleteMember(m *Member) {
i, ok := s.findByID(m.id)
if !ok {
return
}
s.params[i] = s.params[len(s.params)-1]
s.params = s.params[:len(s.params)-1]
s.mi[i] = s.mi[len(s.mi)-1]
s.mi = s.mi[:len(s.mi)-1]
s.res[i] = s.res[len(s.res)-1]
s.res = s.res[:len(s.res)-1]
s.ids[i] = s.ids[len(s.ids)-1]
s.ids = s.ids[:len(s.ids)-1]
}
func (s *sub) updateMember(msg mmsg) error {
i, ok := s.findByID(msg.id)
if !ok {
return nil
}
s.mi[i].dh = msg.dh
// if cindex is not -1 then this query contains
// a cursor that must be updated with the new
// cursor value so subscriptions can paginate.
if s.mi[i].cindx != -1 && msg.cursor != "" {
s.mi[i].values[s.mi[i].cindx] = msg.cursor
// values is a pre-generated json value that
// must be re-created.
if v, err := json.Marshal(s.mi[i].values); err != nil {
return nil
} else {
s.params[i] = v
}
}
return nil
}
func (s *sub) fanOutJobs(gj *graphjin) {
switch {
case len(s.ids) == 0:
return
case len(s.ids) <= maxMembersPerWorker:
go gj.subCheckUpdates(s, s.mval, 0)
default:
// fan out chunks of work to multiple routines
// separated by a random duration
for i := 0; i < len(s.ids); i += maxMembersPerWorker {
go gj.subCheckUpdates(s, s.mval, i)
}
}
}
func (gj *graphjin) subCheckUpdates(s *sub, mv mval, start int) {
// Do not use the `mval` embedded inside sub since
// its not thread safe use the copy `mv mval`.
// random wait to prevent multiple queries hitting the db
// at the same time.
ps := gj.conf.SubsPollDuration
if ps < minPollDuration {
ps = minPollDuration
}
rt := rand.Int63n(ps.Milliseconds()) // #nosec F404
time.Sleep(time.Duration(rt) * time.Millisecond)
end := start + maxMembersPerWorker
if len(mv.ids) < end {
end = start + (len(mv.ids) - start)
}
hasParams := len(s.qc.st.md.Params()) != 0
var rows *sql.Rows
var err error
c := context.Background()
// when params are not available we use a more optimized
// codepath that does not use a join query
// more details on this optimization are towards the end
// of the function
var params json.RawMessage
if hasParams {
params = renderJSONArray(mv.params[start:end])
}
err = retry.Do(
func() error {
if hasParams {
//nolint: sqlclosecheck
rows, err = gj.db.QueryContext(c, s.qc.st.sql, params)
} else {
//nolint: sqlclosecheck
rows, err = gj.db.QueryContext(c, s.qc.st.sql)
}
return err
},
retry.Context(c),
retry.RetryIf(retryIfDBError),
retry.Attempts(3),
retry.LastErrorOnly(true),
)
if err != nil {
gj.log.Printf(errSubs, "query", err)
return
}
defer rows.Close()
var js json.RawMessage
i := 0
for rows.Next() {
if err := rows.Scan(&js); err != nil {
gj.log.Printf(errSubs, "scan", err)
return
}
j := start + i
i++
if hasParams {
gj.subNotifyMember(s, mv, j, js)
continue
}
for k := start; k < end; k++ {
gj.subNotifyMember(s, mv, k, js)
}
s.js = js
}
}
func (gj *graphjin) subFirstQuery(s *sub, m *Member, params json.RawMessage) (mmsg, error) {
c := context.Background()
// when params are not available we use a more optimized
// codepath that does not use a join query
// more details on this optimization are towards the end
// of the function
var js json.RawMessage
var mm mmsg
var err error
if s.js != nil {
js = s.js
} else {
err = retry.Do(
func() error {
switch {
case params != nil:
err = gj.db.
QueryRowContext(c, s.qc.st.sql, renderJSONArray([]json.RawMessage{params})).
Scan(&js)
default:
err = gj.db.
QueryRowContext(c, s.qc.st.sql).
Scan(&js)
}
return err
},
retry.Context(c),
retry.RetryIf(retryIfDBError),
retry.Attempts(3),
retry.LastErrorOnly(true),
)
if err != nil {
return mm, fmt.Errorf(errSubs, "scan", err)
}
}
mm, err = gj.subNotifyMemberEx(s,
[32]byte{},
m.cindx,
m.id,
m.Result, js, false)
return mm, err
}
func (gj *graphjin) subNotifyMember(s *sub, mv mval, j int, js json.RawMessage) {
_, err := gj.subNotifyMemberEx(s,
mv.mi[j].dh,
mv.mi[j].cindx,
mv.ids[j],
mv.res[j], js, true)
if err != nil {
gj.log.Print(err.Error())
}
}
func (gj *graphjin) subNotifyMemberEx(s *sub,
dh [32]byte, cindx int, id xid.ID, rc chan *Result, js json.RawMessage, update bool) (mmsg, error) {
mm := mmsg{id: id}
mm.dh = sha256.Sum256(js)
if dh == mm.dh {
return mm, nil
}
cur, err := gj.encryptCursor(s.qc.st.qc, js)
if err != nil {
return mm, fmt.Errorf(errSubs, "cursor", err)
}
// we're expecting a cursor but the cursor was null
// so we skip this one.
if cindx != -1 && cur.value == "" {
return mm, nil
}
mm.cursor = cur.value
if update {
s.updt <- mm
}
res := &Result{
op: qcode.QTQuery,
name: s.name,
sql: s.qc.st.sql,
role: s.qc.st.role.Name,
Data: cur.data,
}
// if parameters exists then each response is unique
// so each channel should be notified only with it's own
// result value
select {
case rc <- res:
case <-time.After(250 * time.Millisecond):
}
return mm, nil
}
func renderSubWrap(st stmt, ct string) string {
var w strings.Builder
switch ct {
case "mysql":
w.WriteString(`WITH _gj_sub AS (SELECT * FROM JSON_TABLE(?, "$[*]" COLUMNS(`)
for i, p := range st.md.Params() {
if i != 0 {
w.WriteString(`, `)
}
w.WriteString("`" + p.Name + "`")
w.WriteString(` INT PATH "$[`)
w.WriteString(strconv.FormatInt(int64(i), 10))
w.WriteString(`]" ERROR ON ERROR`)
}
w.WriteString(`)) AS _gj_jt`)
default:
w.WriteString(`WITH _gj_sub AS (SELECT `)
for i, p := range st.md.Params() {
if i != 0 {
w.WriteString(`, `)
}
w.WriteString(`CAST(x->>`)
w.WriteString(strconv.FormatInt(int64(i), 10))
w.WriteString(` AS `)
w.WriteString(p.Type)
w.WriteString(`) as `)
w.WriteString(p.Name)
}
w.WriteString(` FROM json_array_elements($1::json) AS x`)
}
w.WriteString(`) SELECT _gj_sub_data.__root FROM _gj_sub LEFT OUTER JOIN LATERAL (`)
w.WriteString(st.sql)
w.WriteString(`) AS _gj_sub_data ON true`)
return w.String()
}
func renderJSONArray(v []json.RawMessage) json.RawMessage {
w := bytes.Buffer{}
w.WriteRune('[')
for i := range v {
if i != 0 {
w.WriteRune(',')
}
w.Write(v[i])
}
w.WriteRune(']')
return json.RawMessage(w.Bytes())
}
func (s *sub) findByID(id xid.ID) (int, bool) {
for i := range s.ids {
if s.ids[i] == id {
return i, true
}
}
return 0, false
}
func (m *Member) Unsubscribe() {
if m != nil && !m.done {
m.sub.del <- m
m.done = true
}
}
func (m *Member) String() string {
return m.id.String()
}