-
Notifications
You must be signed in to change notification settings - Fork 104
/
client_ps.go
526 lines (414 loc) · 12.6 KB
/
client_ps.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
package gocb
import (
"context"
"errors"
"fmt"
"github.com/couchbaselabs/gocbconnstr/v2"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
"sync"
"sync/atomic"
"time"
"google.golang.org/grpc"
"github.com/couchbase/gocbcore/v10"
"github.com/couchbase/gocbcoreps"
)
type psConnectionMgr struct {
host string
lock sync.Mutex
config *gocbcoreps.DialOptions
agent *gocbcoreps.RoutingClient
timeouts TimeoutsConfig
tracer *tracerWrapper
meter *meterWrapper
defaultRetry RetryStrategy
closed atomic.Bool
activeOpsWg sync.WaitGroup
}
func (c *psConnectionMgr) connect() error {
c.lock.Lock()
defer c.lock.Unlock()
client, err := gocbcoreps.Dial(c.host, c.config)
if err != nil {
return err
}
c.agent = client
return nil
}
func (c *psConnectionMgr) openBucket(bucketName string) error {
return nil
}
func (c *psConnectionMgr) buildConfig(cluster *Cluster) error {
resolved, err := gocbconnstr.Resolve(cluster.connSpec())
if err != nil {
return err
}
c.host = fmt.Sprintf("%s:%d", resolved.Couchbase2Host.Host, resolved.Couchbase2Host.Port)
creds, err := cluster.authenticator().Credentials(AuthCredsRequest{})
if err != nil {
return err
}
logger := newZapLogger()
var tp trace.TracerProvider
if c.tracer != nil {
if tracer, ok := c.tracer.tracer.(OtelAwareRequestTracer); ok {
tp = tracer.Provider()
}
}
var mp metric.MeterProvider
if c.meter != nil {
if meter, ok := c.meter.meter.(OtelAwareMeter); ok {
mp = meter.Provider()
}
}
c.config = &gocbcoreps.DialOptions{
Username: creds[0].Username,
Password: creds[0].Password,
InsecureSkipVerify: cluster.securityConfig.TLSSkipVerify,
ClientCertificate: cluster.securityConfig.TLSRootCAs,
Logger: logger,
TracerProvider: tp,
MeterProvider: mp,
}
return nil
}
func (c *psConnectionMgr) canPerformOp() error {
if c.closed.Load() {
return ErrShutdown
}
return nil
}
func (c *psConnectionMgr) MarkOpBeginning() {
c.activeOpsWg.Add(1)
}
func (c *psConnectionMgr) MarkOpCompleted() {
c.activeOpsWg.Done()
}
func (c *psConnectionMgr) getKvProvider(bucketName string) (kvProvider, error) {
if err := c.canPerformOp(); err != nil {
return nil, err
}
kv := c.agent.KvV1()
return &kvProviderPs{
client: kv,
tracer: c.tracer,
}, nil
}
func (c *psConnectionMgr) getKvBulkProvider(bucketName string) (kvBulkProvider, error) {
if err := c.canPerformOp(); err != nil {
return nil, err
}
kv := c.agent.KvV1()
return &kvBulkProviderPs{
client: kv,
tracer: c.tracer,
meter: c.meter,
}, nil
}
func (c *psConnectionMgr) getKvCapabilitiesProvider(bucketName string) (kvCapabilityVerifier, error) {
return nil, ErrFeatureNotAvailable
}
func (c *psConnectionMgr) getViewProvider(bucketName string) (viewProvider, error) {
return nil, ErrFeatureNotAvailable
}
func (c *psConnectionMgr) getViewIndexProvider(bucketName string) (viewIndexProvider, error) {
return nil, ErrFeatureNotAvailable
}
func (c *psConnectionMgr) getQueryProvider() (queryProvider, error) {
if err := c.canPerformOp(); err != nil {
return nil, err
}
provider := c.agent.QueryV1()
return &queryProviderPs{
provider: provider,
managerProvider: newPsOpManagerProvider(c.defaultRetry, c.tracer, c.timeouts.QueryTimeout, c.meter, serviceValueQuery),
}, nil
}
func (c *psConnectionMgr) getQueryIndexProvider() (queryIndexProvider, error) {
if err := c.canPerformOp(); err != nil {
return nil, err
}
provider := c.agent.QueryAdminV1()
return &queryIndexProviderPs{
provider: provider,
managerProvider: newPsOpManagerProvider(c.defaultRetry, c.tracer, c.timeouts.QueryTimeout, c.meter, serviceValueManagement),
}, nil
}
func (c *psConnectionMgr) getSearchIndexProvider() (searchIndexProvider, error) {
if err := c.canPerformOp(); err != nil {
return nil, err
}
provider := c.agent.SearchAdminV1()
return &searchIndexProviderPs{
provider: provider,
managerProvider: newPsOpManagerProvider(c.defaultRetry, c.tracer, c.timeouts.QueryTimeout, c.meter, serviceValueManagement),
}, nil
}
func (c *psConnectionMgr) getSearchCapabilitiesProvider() (searchCapabilityVerifier, error) {
return nil, ErrFeatureNotAvailable
}
func (c *psConnectionMgr) getCollectionsManagementProvider(bucketName string) (collectionsManagementProvider, error) {
if err := c.canPerformOp(); err != nil {
return nil, err
}
return &collectionsManagementProviderPs{
provider: c.agent.CollectionV1(),
bucketName: bucketName,
managerProvider: newPsOpManagerProvider(c.defaultRetry, c.tracer, c.timeouts.QueryTimeout, c.meter, serviceValueManagement),
}, nil
}
func (c *psConnectionMgr) getBucketManagementProvider() (bucketManagementProvider, error) {
if err := c.canPerformOp(); err != nil {
return nil, err
}
return &bucketManagementProviderPs{
provider: c.agent.BucketV1(),
managerProvider: newPsOpManagerProvider(c.defaultRetry, c.tracer, c.timeouts.QueryTimeout, c.meter, serviceValueManagement),
}, nil
}
func (c *psConnectionMgr) getAnalyticsProvider() (analyticsProvider, error) {
return nil, ErrFeatureNotAvailable
}
func (c *psConnectionMgr) getAnalyticsIndexProvider() (analyticsIndexProvider, error) {
return nil, ErrFeatureNotAvailable
}
func (c *psConnectionMgr) getSearchProvider() (searchProvider, error) {
if err := c.canPerformOp(); err != nil {
return nil, err
}
return &searchProviderPs{
provider: c.agent.SearchV1(),
managerProvider: newPsOpManagerProvider(c.defaultRetry, c.tracer, c.timeouts.QueryTimeout, c.meter, serviceValueSearch),
}, nil
}
func (c *psConnectionMgr) getHTTPProvider(bucketName string) (httpProvider, error) {
return nil, ErrFeatureNotAvailable
}
func (c *psConnectionMgr) getDiagnosticsProvider(bucketName string) (diagnosticsProvider, error) {
return nil, ErrFeatureNotAvailable
}
func (c *psConnectionMgr) getWaitUntilReadyProvider(bucketName string) (waitUntilReadyProvider, error) {
if err := c.canPerformOp(); err != nil {
return nil, err
}
return &waitUntilReadyProviderPs{
defaultRetryStrategy: c.defaultRetry,
client: c.agent,
}, nil
}
func (c *psConnectionMgr) getEventingManagementProvider() (eventingManagementProvider, error) {
return nil, ErrFeatureNotAvailable
}
func (c *psConnectionMgr) getUserManagerProvider() (userManagerProvider, error) {
return nil, ErrFeatureNotAvailable
}
func (c *psConnectionMgr) getInternalProvider() (internalProvider, error) {
return nil, ErrFeatureNotAvailable
}
func (c *psConnectionMgr) initTransactions(config TransactionsConfig, cluster *Cluster) error {
// We don't return feature not available here as this function gets called from initTransactions which is part of
// cluster setup, if we returned error here then cluster setup would always fail in couchbase2 mode.
return nil
}
func (c *psConnectionMgr) getTransactionsProvider() (transactionsProvider, error) {
if err := c.canPerformOp(); err != nil {
return nil, err
}
return &transactionsProviderPs{}, nil
}
func (c *psConnectionMgr) connection(bucketName string) (*gocbcore.Agent, error) {
return nil, ErrFeatureNotAvailable
}
func (c *psConnectionMgr) close() error {
if !c.closed.CompareAndSwap(false, true) {
return ErrShutdown
}
err := c.agent.Close()
logDebugf("Waiting for any active requests to complete")
c.activeOpsWg.Wait()
if c.tracer != nil {
tracerDecRef(c.tracer.tracer)
c.tracer = nil
}
if c.meter != nil {
if meter, ok := c.meter.meter.(*LoggingMeter); ok {
meter.close()
}
c.meter = nil
}
return err
}
func (c *psConnectionMgr) getMeter() *meterWrapper {
return c.meter
}
type psOpManagerProvider struct {
defaultRetryStrategy RetryStrategy
tracer *tracerWrapper
defaultTimeout time.Duration
meter *meterWrapper
service string
}
func newPsOpManagerProvider(retry RetryStrategy, tracer *tracerWrapper, timeout time.Duration, meter *meterWrapper,
service string) *psOpManagerProvider {
return &psOpManagerProvider{
defaultRetryStrategy: retry,
tracer: tracer,
defaultTimeout: timeout,
meter: meter,
service: service,
}
}
func (p *psOpManagerProvider) NewManager(parentSpan RequestSpan, opName string, attribs map[string]interface{}) *psOpManagerDefault {
m := &psOpManagerDefault{
defaultRetryStrategy: p.defaultRetryStrategy,
tracer: p.tracer,
defaultTimeout: p.defaultTimeout,
service: p.service,
createdTime: time.Now(),
span: p.tracer.createSpan(parentSpan, opName, p.service),
opName: opName,
}
for key, value := range attribs {
m.span.SetAttribute(key, value)
}
return m
}
type psOpManager interface {
IsIdempotent() bool
TraceSpan() RequestSpan
OperationID() string
RetryStrategy() RetryStrategy
OpName() string
CreatedAt() time.Time
Tracer() *tracerWrapper
RetryInfo() retriedRequestInfo
SetRetryRequest(ps *retriableRequestPs)
RetryReasonFor(error) RetryReason
Timeout() time.Duration
Context() context.Context
}
type psOpManagerDefault struct {
defaultRetryStrategy RetryStrategy
tracer *tracerWrapper
defaultTimeout time.Duration
service string
createdTime time.Time
span RequestSpan
opName string
operationID string
retryStrategy RetryStrategy
timeout time.Duration
ctx context.Context
isIdempotent bool
req *retriableRequestPs
}
func (m *psOpManagerDefault) SetTimeout(timeout time.Duration) {
m.timeout = timeout
}
func (m *psOpManagerDefault) SetRetryStrategy(retryStrategy RetryStrategy) {
strat := m.defaultRetryStrategy
if retryStrategy != nil {
strat = retryStrategy
}
m.retryStrategy = strat
}
func (m *psOpManagerDefault) SetContext(ctx context.Context) {
if ctx == nil {
ctx = context.Background()
}
m.ctx = ctx
}
func (m *psOpManagerDefault) SetIsIdempotent(idempotent bool) {
m.isIdempotent = idempotent
}
func (m *psOpManagerDefault) SetOperationID(id string) {
m.operationID = id
}
func (m *psOpManagerDefault) NewSpan(name string) RequestSpan {
return m.tracer.createSpan(m.span, name, m.service)
}
func (m *psOpManagerDefault) Finish() {
retries := m.RetryInfo().RetryAttempts()
m.span.SetAttribute(spanAttribRetries, retries)
m.span.End()
}
func (m *psOpManagerDefault) TraceSpan() RequestSpan {
return m.span
}
func (m *psOpManagerDefault) RetryStrategy() RetryStrategy {
return m.retryStrategy
}
func (m *psOpManagerDefault) IsIdempotent() bool {
return m.isIdempotent
}
func (m *psOpManagerDefault) Timeout() time.Duration {
if m.timeout > 0 {
return m.timeout
}
return m.defaultTimeout
}
func (m *psOpManagerDefault) CheckReadyForOp() error {
if m.Timeout() == 0 {
return errors.New("op manager had no timeout specified")
}
if m.span == nil {
return errors.New("op manager had no span specified")
}
return nil
}
func (m *psOpManagerDefault) ElapsedTime() time.Duration {
return time.Since(m.createdTime)
}
func (m *psOpManagerDefault) OperationID() string {
return m.operationID
}
func (m *psOpManagerDefault) OpName() string {
return m.opName
}
func (m *psOpManagerDefault) RetryInfo() retriedRequestInfo {
return m.req
}
func (m *psOpManagerDefault) SetRetryRequest(req *retriableRequestPs) {
m.req = req
}
func (m *psOpManagerDefault) Tracer() *tracerWrapper {
return m.tracer
}
func (m *psOpManagerDefault) CreatedAt() time.Time {
return m.createdTime
}
func (m *psOpManagerDefault) RetryReasonFor(err error) RetryReason {
if errors.Is(err, ErrServiceNotAvailable) {
return ServiceNotAvailableRetryReason
}
return nil
}
func (m *psOpManagerDefault) Context() context.Context {
return m.ctx
}
func wrapPSOpCtx[ReqT any, RespT any](ctx context.Context, m psOpManager,
req ReqT,
fn func(context.Context, ReqT, ...grpc.CallOption) (RespT, error)) (RespT, error) {
return wrapPSOpCtxWithPeek(ctx, m, req, m.TraceSpan(), fn, nil)
}
func wrapPSOp[ReqT any, RespT any](m psOpManager, req ReqT,
fn func(context.Context, ReqT, ...grpc.CallOption) (RespT, error)) (RespT, error) {
ctx, cancel := context.WithTimeout(m.Context(), m.Timeout())
defer cancel()
return wrapPSOpCtx(ctx, m, req, fn)
}
func wrapPSOpCtxWithPeek[ReqT any, RespT any](ctx context.Context,
m psOpManager,
req ReqT,
parentSpan RequestSpan,
fn func(context.Context, ReqT, ...grpc.CallOption) (RespT, error),
peekResult func(RespT) error) (RespT, error) {
retryReq := newRetriableRequestPS(m.OpName(), m.IsIdempotent(), parentSpan, m.OperationID(), m.RetryStrategy())
m.SetRetryRequest(retryReq)
res, err := handleRetriableRequest(ctx, m.CreatedAt(), m.Tracer(), req, retryReq, fn, m.RetryReasonFor, peekResult)
if err != nil {
var emptyResp RespT
return emptyResp, err
}
return res, nil
}