forked from couchbase/gocb
/
cluster_analyticsquery.go
619 lines (517 loc) · 14 KB
/
cluster_analyticsquery.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
package gocb
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/opentracing/opentracing-go"
)
type analyticsError struct {
Code uint32 `json:"code"`
Message string `json:"msg"`
}
func (e *analyticsError) Error() string {
return fmt.Sprintf("[%d] %s", e.Code, e.Message)
}
// AnalyticsWarning represents any warning generating during the execution of an Analytics query.
type AnalyticsWarning struct {
Code uint32 `json:"code"`
Message string `json:"msg"`
}
type analyticsResponse struct {
RequestId string `json:"requestID"`
ClientContextId string `json:"clientContextID"`
Results []json.RawMessage `json:"results,omitempty"`
Errors []analyticsError `json:"errors,omitempty"`
Warnings []AnalyticsWarning `json:"warnings,omitempty"`
Status string `json:"status,omitempty"`
Signature interface{} `json:"signature,omitempty"`
Metrics analyticsResponseMetrics `json:"metrics,omitempty"`
Handle string `json:"handle,omitempty"`
}
type analyticsResponseMetrics struct {
ElapsedTime string `json:"elapsedTime"`
ExecutionTime string `json:"executionTime"`
ResultCount uint `json:"resultCount"`
ResultSize uint `json:"resultSize"`
MutationCount uint `json:"mutationCount,omitempty"`
SortCount uint `json:"sortCount,omitempty"`
ErrorCount uint `json:"errorCount,omitempty"`
WarningCount uint `json:"warningCount,omitempty"`
ProcessedObjects uint `json:"processedObjects,omitempty"`
}
type analyticsResponseHandle struct {
Status string `json:"status,omitempty"`
Handle string `json:"handle,omitempty"`
}
type analyticsMultiError []analyticsError
func (e *analyticsMultiError) Error() string {
return (*e)[0].Error()
}
func (e *analyticsMultiError) Code() uint32 {
return (*e)[0].Code
}
// AnalyticsResultMetrics encapsulates various metrics gathered during a queries execution.
type AnalyticsResultMetrics struct {
ElapsedTime time.Duration
ExecutionTime time.Duration
ResultCount uint
ResultSize uint
MutationCount uint
SortCount uint
ErrorCount uint
WarningCount uint
ProcessedObjects uint
}
// AnalyticsDeferredResultHandle allows access to the handle of a deferred Analytics query.
//
// Experimental: This API is subject to change at any time.
type AnalyticsDeferredResultHandle interface {
One(valuePtr interface{}) error
Next(valuePtr interface{}) bool
NextBytes() []byte
Close() error
Status() (string, error)
}
type analyticsDeferredResultHandle struct {
handleUri string
status string
rows *analyticsRows
err error
client *http.Client
creds []UserPassPair
hasResult bool
timeout time.Duration
}
type analyticsRows struct {
closed bool
index int
rows []json.RawMessage
}
// AnalyticsResults allows access to the results of a Analytics query.
type AnalyticsResults interface {
One(valuePtr interface{}) error
Next(valuePtr interface{}) bool
NextBytes() []byte
Close() error
RequestId() string
ClientContextId() string
Status() string
Warnings() []AnalyticsWarning
Signature() interface{}
Metrics() AnalyticsResultMetrics
Handle() AnalyticsDeferredResultHandle
}
type analyticsResults struct {
rows *analyticsRows
err error
requestId string
clientContextId string
status string
warnings []AnalyticsWarning
signature interface{}
metrics AnalyticsResultMetrics
handle AnalyticsDeferredResultHandle
}
func (r *analyticsResults) Next(valuePtr interface{}) bool {
if r.err != nil {
return false
}
row := r.NextBytes()
if row == nil {
return false
}
r.err = json.Unmarshal(row, valuePtr)
if r.err != nil {
return false
}
return true
}
func (r *analyticsResults) NextBytes() []byte {
if r.err != nil {
return nil
}
return r.rows.NextBytes()
}
func (r *analyticsResults) Close() error {
r.rows.Close()
return r.err
}
func (r *analyticsResults) One(valuePtr interface{}) error {
if !r.Next(valuePtr) {
err := r.Close()
if err != nil {
return err
}
return ErrNoResults
}
// Ignore any errors occurring after we already have our result
err := r.Close()
if err != nil {
// Return no error as we got the one result already.
return nil
}
return nil
}
func (r *analyticsResults) Warnings() []AnalyticsWarning {
return r.warnings
}
func (r *analyticsResults) Status() string {
return r.status
}
func (r *analyticsResults) Signature() interface{} {
return r.signature
}
func (r *analyticsResults) Metrics() AnalyticsResultMetrics {
return r.metrics
}
func (r *analyticsResults) RequestId() string {
if !r.rows.closed {
panic("Result must be closed before accessing meta-data")
}
return r.requestId
}
func (r *analyticsResults) ClientContextId() string {
if !r.rows.closed {
panic("Result must be closed before accessing meta-data")
}
return r.clientContextId
}
// Experimental: This API is subject to change at any time.
func (r *analyticsResults) Handle() AnalyticsDeferredResultHandle {
return r.handle
}
func (r *analyticsRows) NextBytes() []byte {
if r.index+1 >= len(r.rows) {
r.closed = true
return nil
}
r.index++
return r.rows[r.index]
}
func (r *analyticsRows) Close() {
r.closed = true
}
func (r *analyticsDeferredResultHandle) Next(valuePtr interface{}) bool {
if r.err != nil {
return false
}
row := r.NextBytes()
if row == nil {
return false
}
r.err = json.Unmarshal(row, valuePtr)
if r.err != nil {
return false
}
return true
}
func (r *analyticsDeferredResultHandle) NextBytes() []byte {
if r.err != nil {
return nil
}
if r.status == "success" && !r.hasResult {
req, err := http.NewRequest("GET", r.handleUri, nil)
if err != nil {
r.err = err
return nil
}
if len(r.creds) == 1 {
req.SetBasicAuth(r.creds[0].Username, r.creds[0].Password)
}
err = r.executeHandle(r.client, req, r.timeout, &r.rows.rows)
if err != nil {
r.err = err
return nil
}
r.hasResult = true
} else if r.status != "success" {
return nil
}
return r.rows.NextBytes()
}
func (r *analyticsDeferredResultHandle) Close() error {
r.rows.Close()
return r.err
}
func (r *analyticsDeferredResultHandle) One(valuePtr interface{}) error {
if !r.Next(valuePtr) {
err := r.Close()
if err != nil {
return err
}
return ErrNoResults
}
// Ignore any errors occurring after we already have our result
err := r.Close()
if err != nil {
// Return no error as we got the one result already.
return nil
}
return nil
}
func (r *analyticsDeferredResultHandle) Status() (string, error) {
req, err := http.NewRequest("GET", r.handleUri, nil)
if err != nil {
r.err = err
return "", err
}
if len(r.creds) == 1 {
req.SetBasicAuth(r.creds[0].Username, r.creds[0].Password)
}
var analyticsResponse *analyticsResponseHandle
err = r.executeHandle(r.client, req, r.timeout, &analyticsResponse)
if err != nil {
r.err = err
return "", err
}
r.status = analyticsResponse.Status
r.handleUri = analyticsResponse.Handle
return r.status, nil
}
func (r *analyticsDeferredResultHandle) executeHandle(client *http.Client, req *http.Request, timeout time.Duration, valuePtr interface{}) error {
resp, err := doHttpWithTimeout(r.client, req, r.timeout)
if err != nil {
return err
}
jsonDec := json.NewDecoder(resp.Body)
err = jsonDec.Decode(valuePtr)
if err != nil {
return err
}
err = resp.Body.Close()
if err != nil {
logDebugf("Failed to close socket (%s)", err)
}
return nil
}
func (c *Cluster) executeAnalyticsQuery(tracectx opentracing.SpanContext, analyticsEp string, opts map[string]interface{}, creds []UserPassPair, timeout time.Duration, client *http.Client) (AnalyticsResults, error) {
reqUri := fmt.Sprintf("%s/analytics/service", analyticsEp)
priority, priorityCastok := opts["priority"].(int)
if priorityCastok {
delete(opts, "priority")
}
if len(creds) > 1 {
opts["creds"] = creds
}
reqJson, err := json.Marshal(opts)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", reqUri, bytes.NewBuffer(reqJson))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
if priorityCastok {
req.Header.Set("Analytics-Priority", strconv.Itoa(priority))
}
if len(creds) == 1 {
req.SetBasicAuth(creds[0].Username, creds[0].Password)
}
dtrace := c.agentConfig.Tracer.StartSpan("dispatch",
opentracing.ChildOf(tracectx))
resp, err := doHttpWithTimeout(client, req, timeout)
if err != nil {
dtrace.Finish()
return nil, err
}
dtrace.Finish()
strace := c.agentConfig.Tracer.StartSpan("streaming",
opentracing.ChildOf(tracectx))
analyticsResp := analyticsResponse{}
jsonDec := json.NewDecoder(resp.Body)
err = jsonDec.Decode(&analyticsResp)
if err != nil {
strace.Finish()
return nil, err
}
err = resp.Body.Close()
if err != nil {
logDebugf("Failed to close socket (%s)", err)
}
strace.SetTag("couchbase.operation_id", analyticsResp.RequestId)
strace.Finish()
elapsedTime, err := time.ParseDuration(analyticsResp.Metrics.ElapsedTime)
if err != nil {
logDebugf("Failed to parse elapsed time duration (%s)", err)
}
executionTime, err := time.ParseDuration(analyticsResp.Metrics.ExecutionTime)
if err != nil {
logDebugf("Failed to parse execution time duration (%s)", err)
}
if len(analyticsResp.Errors) > 0 {
return nil, (*analyticsMultiError)(&analyticsResp.Errors)
}
if resp.StatusCode != 200 {
return nil, &viewError{
Message: "HTTP Error",
Reason: fmt.Sprintf("Status code was %d.", resp.StatusCode),
}
}
return &analyticsResults{
requestId: analyticsResp.RequestId,
clientContextId: analyticsResp.ClientContextId,
rows: &analyticsRows{
rows: analyticsResp.Results,
index: -1,
},
signature: analyticsResp.Signature,
status: analyticsResp.Status,
warnings: analyticsResp.Warnings,
metrics: AnalyticsResultMetrics{
ElapsedTime: elapsedTime,
ExecutionTime: executionTime,
ResultCount: analyticsResp.Metrics.ResultCount,
ResultSize: analyticsResp.Metrics.ResultSize,
MutationCount: analyticsResp.Metrics.MutationCount,
SortCount: analyticsResp.Metrics.SortCount,
ErrorCount: analyticsResp.Metrics.ErrorCount,
WarningCount: analyticsResp.Metrics.WarningCount,
ProcessedObjects: analyticsResp.Metrics.ProcessedObjects,
},
handle: &analyticsDeferredResultHandle{
handleUri: analyticsResp.Handle,
rows: &analyticsRows{
index: -1,
},
status: analyticsResp.Status,
client: client,
creds: creds,
timeout: timeout,
},
}, nil
}
// Performs a spatial query and returns a list of rows or an error.
func (c *Cluster) doAnalyticsQuery(tracectx opentracing.SpanContext, b *Bucket, q *AnalyticsQuery, params interface{}) (AnalyticsResults, error) {
var err error
var cbasEp string
var timeout time.Duration
var creds []UserPassPair
var selectedB *Bucket
if b != nil {
if b.analyticsTimeout < c.analyticsTimeout {
timeout = b.analyticsTimeout
} else {
timeout = c.analyticsTimeout
}
selectedB = b
} else {
if c.auth == nil {
panic("Cannot perform cluster level queries without Cluster Authenticator.")
}
tmpB, err := c.randomBucket()
if err != nil {
return nil, err
}
timeout = c.analyticsTimeout
selectedB = tmpB
}
client := selectedB.client.HttpClient()
clientContextId := selectedB.client.ClientId()
retryBehavior := selectedB.analyticsQueryRetryBehavior
execOpts := make(map[string]interface{})
for k, v := range q.options {
execOpts[k] = v
}
if params != nil {
args, isArray := params.([]interface{})
if isArray {
execOpts["args"] = args
} else {
mapArgs, isMap := params.(map[string]interface{})
if isMap {
for key, value := range mapArgs {
if !strings.HasPrefix(key, "$") {
key = "$" + key
}
execOpts[key] = value
}
} else {
panic("Invalid params argument passed")
}
}
}
_, castok := execOpts["client_context_id"]
if !castok {
execOpts["client_context_id"] = clientContextId
}
tmostr, castok := execOpts["timeout"].(string)
if castok {
var err error
timeout, err = time.ParseDuration(tmostr)
if err != nil {
return nil, err
}
} else {
// Set the timeout string to its default variant
execOpts["timeout"] = timeout.String()
}
var retries uint
var res AnalyticsResults
start := time.Now()
for time.Now().Sub(start) <= time.Duration(timeout) {
retries++
cbasEp, err = selectedB.getCbasEp()
if err != nil {
return nil, err
}
if b != nil {
if c.auth != nil {
creds, err = c.auth.Credentials(AuthCredsRequest{
Service: CbasService,
Endpoint: cbasEp,
Bucket: b.name,
})
if err != nil {
return nil, err
}
} else {
creds = []UserPassPair{
{
Username: b.name,
Password: b.password,
},
}
}
} else {
creds, err = c.auth.Credentials(AuthCredsRequest{
Service: CbasService,
Endpoint: cbasEp,
})
if err != nil {
return nil, err
}
}
etrace := c.agentConfig.Tracer.StartSpan("execute",
opentracing.ChildOf(tracectx))
res, err = c.executeAnalyticsQuery(tracectx, cbasEp, execOpts, creds, timeout, client)
if err == nil {
etrace.Finish()
return res, nil
}
etrace.Finish()
analyticsErr, isAnalyticsErr := err.(*analyticsMultiError)
if !isAnalyticsErr {
return nil, err
}
if analyticsErr.Code() != 21002 && analyticsErr.Code() != 23000 && analyticsErr.Code() != 23003 && analyticsErr.Code() != 23007 {
return nil, err
}
if retryBehavior == nil || !retryBehavior.CanRetry(retries) {
break
}
time.Sleep(retryBehavior.NextInterval(retries))
}
return res, err
}
// ExecuteAnalyticsQuery performs an analytics query and returns a list of rows or an error.
func (c *Cluster) ExecuteAnalyticsQuery(q *AnalyticsQuery, params interface{}) (AnalyticsResults, error) {
span := c.agentConfig.Tracer.StartSpan("ExecuteAnalyticsQuery",
opentracing.Tag{Key: "couchbase.service", Value: "cbas"})
defer span.Finish()
return c.doAnalyticsQuery(span.Context(), nil, q, params)
}