-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster_query.go
459 lines (380 loc) · 12 KB
/
cluster_query.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
package gocb
import (
"context"
"encoding/json"
"net/url"
"time"
"github.com/pkg/errors"
"github.com/couchbaselabs/gocbcore8alpha"
"github.com/opentracing/opentracing-go"
)
type n1qlCache struct {
name string
encodedPlan string
}
type n1qlResponseMetrics 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"`
}
type n1qlResponse struct {
RequestID string `json:"requestID"`
ClientContextID string `json:"clientContextID"`
Results []json.RawMessage `json:"results,omitempty"`
Errors []queryError `json:"errors,omitempty"`
Status string `json:"status"`
Metrics n1qlResponseMetrics `json:"metrics"`
}
// QueryResultMetrics encapsulates various metrics gathered during a queries execution.
type QueryResultMetrics struct {
ElapsedTime time.Duration
ExecutionTime time.Duration
ResultCount uint
ResultSize uint
MutationCount uint
SortCount uint
ErrorCount uint
WarningCount uint
}
// QueryResults allows access to the results of a N1QL query.
type QueryResults struct {
closed bool
index int
rows []json.RawMessage
err error
requestID string
clientContextID string
metrics QueryResultMetrics
sourceAddr string
}
// Next assigns the next result from the results into the value pointer, returning whether the read was successful.
func (r *QueryResults) 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
}
// NextBytes returns the next result from the results as a byte array.
func (r *QueryResults) NextBytes() []byte {
if r.err != nil {
return nil
}
if r.index+1 >= len(r.rows) {
r.closed = true
return nil
}
r.index++
return r.rows[r.index]
}
// Close marks the results as closed, returning any errors that occurred during reading the results.
func (r *QueryResults) Close() error {
r.closed = true
return r.err
}
// One assigns the first value from the results into the value pointer.
func (r *QueryResults) One(valuePtr interface{}) error {
if !r.Next(valuePtr) {
err := r.Close()
if err != nil {
return err
}
// return ErrNoResults TODO
}
// 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
}
// SourceEndpoint returns the endpoint used for execution of this query.
// VOLATILE
func (r *QueryResults) SourceEndpoint() string {
return r.sourceAddr
}
// RequestID returns the request ID used for this query.
func (r *QueryResults) RequestID() string {
if !r.closed {
panic("Result must be closed before accessing meta-data")
}
return r.requestID
}
// ClientContextID returns the context ID used for this query.
func (r *QueryResults) ClientContextID() string {
if !r.closed {
panic("Result must be closed before accessing meta-data")
}
return r.clientContextID
}
// Metrics returns metrics about execution of this result.
func (r *QueryResults) Metrics() QueryResultMetrics {
if !r.closed {
panic("Result must be closed before accessing meta-data")
}
return r.metrics
}
type httpProvider interface {
DoHttpRequest(req *gocbcore.HttpRequest) (*gocbcore.HttpResponse, error)
}
// Query executes the N1QL query statement on the server n1qlEp.
// This function assumes that `opts` already contains all the required
// settings. This function will inject any additional connection or request-level
// settings into the `opts` map (currently this is only the timeout).
func (c *Cluster) Query(statement string, opts *QueryOptions) (*QueryResults, error) {
if opts == nil {
opts = &QueryOptions{}
}
ctx := opts.Context
if ctx == nil {
ctx = context.Background()
}
var span opentracing.Span
if opts.ParentSpanContext == nil {
span = opentracing.GlobalTracer().StartSpan("ExecuteN1QLQuery",
opentracing.Tag{Key: "couchbase.service", Value: "n1ql"})
} else {
span = opentracing.GlobalTracer().StartSpan("ExecuteN1QLQuery",
opentracing.Tag{Key: "couchbase.service", Value: "n1ql"}, opentracing.ChildOf(opts.ParentSpanContext))
}
defer span.Finish()
provider, err := c.getHTTPProvider()
if err != nil {
return nil, err
}
return c.query(ctx, span.Context(), statement, opts, provider)
}
func (c *Cluster) query(ctx context.Context, traceCtx opentracing.SpanContext, statement string, opts *QueryOptions,
provider httpProvider) (*QueryResults, error) {
queryOpts, err := opts.toMap(statement)
if err != nil {
return nil, errors.Wrap(err, "could not parse query options")
}
// Work out which timeout to use, the cluster level default or query specific one
timeout := c.sb.QueryTimeout
var optTimeout time.Duration
tmostr, castok := queryOpts["timeout"].(string)
if castok {
optTimeout, err = time.ParseDuration(tmostr)
if err != nil {
return nil, errors.Wrap(err, "could not parse timeout value")
}
}
if optTimeout > 0 && optTimeout < timeout {
timeout = optTimeout
}
now := time.Now()
d, ok := ctx.Deadline()
// If we don't need to then we don't touch the original ctx value so that the Done channel is set
// in a predictable manner.
if !ok || now.Add(timeout).Before(d) {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
} else {
timeout = d.Sub(now)
}
queryOpts["timeout"] = timeout.String()
var retries uint
var res *QueryResults
for {
retries++
if opts.Prepared {
etrace := opentracing.GlobalTracer().StartSpan("execute", opentracing.ChildOf(traceCtx))
res, err = c.doPreparedN1qlQuery(ctx, traceCtx, queryOpts, provider)
etrace.Finish()
} else {
res, err = c.executeN1qlQuery(ctx, traceCtx, queryOpts, provider)
}
if err == nil {
return res, err
}
if !isRetryableError(err) || c.sb.N1qlRetryBehavior == nil || !c.sb.N1qlRetryBehavior.CanRetry(retries) {
return res, err
}
time.Sleep(c.sb.N1qlRetryBehavior.NextInterval(retries))
}
}
func (c *Cluster) doPreparedN1qlQuery(ctx context.Context, traceCtx opentracing.SpanContext, queryOpts map[string]interface{},
provider httpProvider) (*QueryResults, error) {
stmtStr, isStr := queryOpts["statement"].(string)
if !isStr {
return nil, configurationError{message: "query statement could not be parsed"}
}
c.clusterLock.RLock()
cachedStmt := c.queryCache[stmtStr]
c.clusterLock.RUnlock()
if cachedStmt != nil {
// Attempt to execute our cached query plan
delete(queryOpts, "statement")
queryOpts["prepared"] = cachedStmt.name
queryOpts["encoded_plan"] = cachedStmt.encodedPlan
etrace := opentracing.GlobalTracer().StartSpan("execute", opentracing.ChildOf(traceCtx))
results, err := c.executeN1qlQuery(ctx, etrace.Context(), queryOpts, provider)
if err == nil {
etrace.Finish()
return results, nil
}
etrace.Finish()
// If we get error 4050, 4070 or 5000, we should attempt
// to re-prepare the statement immediately before failing.
if !isRetryableError(err) {
return nil, err
}
}
// Prepare the query
ptrace := opentracing.GlobalTracer().StartSpan("prepare", opentracing.ChildOf(traceCtx))
var err error
cachedStmt, err = c.prepareN1qlQuery(ctx, ptrace.Context(), queryOpts, provider)
if err != nil {
ptrace.Finish()
return nil, err
}
ptrace.Finish()
// Save new cached statement
c.clusterLock.Lock()
c.queryCache[stmtStr] = cachedStmt
c.clusterLock.Unlock()
// Update with new prepared data
delete(queryOpts, "statement")
queryOpts["prepared"] = cachedStmt.name
queryOpts["encoded_plan"] = cachedStmt.encodedPlan
etrace := opentracing.GlobalTracer().StartSpan("execute", opentracing.ChildOf(traceCtx))
defer etrace.Finish()
return c.executeN1qlQuery(ctx, etrace.Context(), queryOpts, provider)
}
func (c *Cluster) prepareN1qlQuery(ctx context.Context, traceCtx opentracing.SpanContext, opts map[string]interface{},
provider httpProvider) (*n1qlCache, error) {
prepOpts := make(map[string]interface{})
for k, v := range opts {
prepOpts[k] = v
}
prepOpts["statement"] = "PREPARE " + opts["statement"].(string)
prepRes, err := c.executeN1qlQuery(ctx, traceCtx, prepOpts, provider)
if err != nil {
return nil, err
}
var preped n1qlPrepData
err = prepRes.One(&preped)
if err != nil {
return nil, err
}
return &n1qlCache{
name: preped.Name,
encodedPlan: preped.EncodedPlan,
}, nil
}
type n1qlPrepData struct {
EncodedPlan string `json:"encoded_plan"`
Name string `json:"name"`
}
// Executes the N1QL query (in opts) on the server n1qlEp.
// This function assumes that `opts` already contains all the required
// settings. This function will inject any additional connection or request-level
// settings into the `opts` map.
func (c *Cluster) executeN1qlQuery(ctx context.Context, traceCtx opentracing.SpanContext, opts map[string]interface{},
provider httpProvider) (*QueryResults, error) {
reqJSON, err := json.Marshal(opts)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal query request body")
}
req := &gocbcore.HttpRequest{
Service: gocbcore.N1qlService,
Path: "/query/service",
Method: "POST",
Context: ctx,
Body: reqJSON,
}
dtrace := opentracing.GlobalTracer().StartSpan("dispatch", opentracing.ChildOf(traceCtx))
resp, err := provider.DoHttpRequest(req)
if err != nil {
dtrace.Finish()
if err == gocbcore.ErrNoN1qlService {
return nil, serviceNotFoundError{}
}
if err == context.DeadlineExceeded {
return nil, timeoutError{}
}
return nil, errors.Wrap(err, "could not complete query http request")
}
dtrace.Finish()
strace := opentracing.GlobalTracer().StartSpan("streaming", opentracing.ChildOf(traceCtx))
n1qlResp := n1qlResponse{}
jsonDec := json.NewDecoder(resp.Body)
err = jsonDec.Decode(&n1qlResp)
if err != nil {
strace.Finish()
return nil, errors.Wrap(err, "failed to decode query response body")
}
err = resp.Body.Close()
if err != nil {
logDebugf("Failed to close socket (%s)", err)
}
// TODO(brett19): place the server_duration in the right place...
// srvDuration, _ := time.ParseDuration(n1qlResp.Metrics.ExecutionTime)
// strace.SetTag("server_duration", srvDuration)
strace.SetTag("couchbase.operation_id", n1qlResp.RequestID)
strace.Finish()
epInfo, err := url.Parse(resp.Endpoint)
if err != nil {
logWarnf("Failed to parse N1QL source address")
epInfo = &url.URL{
Host: "",
}
}
if len(n1qlResp.Errors) > 0 {
errs := make([]QueryError, len(n1qlResp.Errors))
for i, e := range n1qlResp.Errors {
errs[i] = e
}
return nil, queryMultiError{
errors: errs,
endpoint: epInfo.Host,
httpStatus: resp.StatusCode,
contextID: n1qlResp.ClientContextID,
}
}
if resp.StatusCode != 200 {
return nil, &httpError{
statusCode: resp.StatusCode,
}
}
elapsedTime, err := time.ParseDuration(n1qlResp.Metrics.ElapsedTime)
if err != nil {
logDebugf("Failed to parse elapsed time duration (%s)", err)
}
executionTime, err := time.ParseDuration(n1qlResp.Metrics.ExecutionTime)
if err != nil {
logDebugf("Failed to parse execution time duration (%s)", err)
}
return &QueryResults{
sourceAddr: epInfo.Host,
requestID: n1qlResp.RequestID,
clientContextID: n1qlResp.ClientContextID,
index: -1,
rows: n1qlResp.Results,
metrics: QueryResultMetrics{
ElapsedTime: elapsedTime,
ExecutionTime: executionTime,
ResultCount: n1qlResp.Metrics.ResultCount,
ResultSize: n1qlResp.Metrics.ResultSize,
MutationCount: n1qlResp.Metrics.MutationCount,
SortCount: n1qlResp.Metrics.SortCount,
ErrorCount: n1qlResp.Metrics.ErrorCount,
WarningCount: n1qlResp.Metrics.WarningCount,
},
}, nil
}