-
Notifications
You must be signed in to change notification settings - Fork 104
/
cluster.go
622 lines (526 loc) · 18.6 KB
/
cluster.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
620
621
622
package gocb
import (
"context"
"crypto/x509"
"errors"
"fmt"
"strconv"
"time"
gocbconnstr "github.com/couchbaselabs/gocbconnstr/v2"
)
// Cluster represents a connection to a specific Couchbase cluster.
type Cluster struct {
cSpec gocbconnstr.ConnSpec
auth Authenticator
connectionManager connectionManager
useServerDurations bool
useMutationTokens bool
timeoutsConfig TimeoutsConfig
transcoder Transcoder
retryStrategyWrapper *coreRetryStrategyWrapper
orphanLoggerEnabled bool
orphanLoggerInterval time.Duration
orphanLoggerSampleSize uint32
circuitBreakerConfig CircuitBreakerConfig
securityConfig SecurityConfig
internalConfig InternalConfig
transactionsConfig TransactionsConfig
compressionConfig CompressionConfig
compressor *compressor
transactions *Transactions
keyspace keyspace
preferredServerGroup string
}
// IoConfig specifies IO related configuration options.
type IoConfig struct {
DisableMutationTokens bool
DisableServerDurations bool
}
// TimeoutsConfig specifies options for various operation timeouts.
type TimeoutsConfig struct {
ConnectTimeout time.Duration
KVTimeout time.Duration
KVDurableTimeout time.Duration
// Volatile: This option is subject to change at any time.
KVScanTimeout time.Duration
ViewTimeout time.Duration
QueryTimeout time.Duration
AnalyticsTimeout time.Duration
SearchTimeout time.Duration
ManagementTimeout time.Duration
}
// OrphanReporterConfig specifies options for controlling the orphan
// reporter which records when the SDK receives responses for requests
// that are no longer in the system (usually due to being timed out).
type OrphanReporterConfig struct {
Disabled bool
ReportInterval time.Duration
SampleSize uint32
}
// SecurityConfig specifies options for controlling security related
// items such as TLS root certificates and verification skipping.
type SecurityConfig struct {
TLSRootCAs *x509.CertPool
TLSSkipVerify bool
// AllowedSaslMechanisms is the list of mechanisms that the SDK can use to attempt authentication.
// Note that if you add PLAIN to the list, this will cause credential leakage on the network
// since PLAIN sends the credentials in cleartext. It is disabled by default to prevent downgrade attacks. We
// recommend using a TLS connection if using PLAIN.
AllowedSaslMechanisms []SaslMechanism
}
// CompressionConfig specifies options for controlling compression applied to documents before sending to Couchbase
// Server.
type CompressionConfig struct {
Disabled bool
// MinSize specifies the minimum size of the document to consider compression.
MinSize uint32
// MinRatio specifies the minimal compress ratio (compressed / original) for the document to be sent compressed.
MinRatio float64
}
// InternalConfig specifies options for controlling various internal
// items.
// Internal: This should never be used and is not supported.
type InternalConfig struct {
TLSRootCAProvider func() *x509.CertPool
ConnectionBufferSize uint
}
// ClusterOptions is the set of options available for creating a Cluster.
type ClusterOptions struct {
// Authenticator specifies the authenticator to use with the cluster.
Authenticator Authenticator
// Username & Password specifies the cluster username and password to
// authenticate with. This is equivalent to passing PasswordAuthenticator
// as the Authenticator parameter with the same values.
Username string
Password string
// Timeouts specifies various operation timeouts.
TimeoutsConfig TimeoutsConfig
// Transcoder is used for trancoding data used in KV operations.
Transcoder Transcoder
// RetryStrategy is used to automatically retry operations if they fail.
RetryStrategy RetryStrategy
// Tracer specifies the tracer to use for requests.
Tracer RequestTracer
Meter Meter
// OrphanReporterConfig specifies options for the orphan reporter.
OrphanReporterConfig OrphanReporterConfig
// CircuitBreakerConfig specifies options for the circuit breakers.
CircuitBreakerConfig CircuitBreakerConfig
// IoConfig specifies IO related configuration options.
IoConfig IoConfig
// SecurityConfig specifies security related configuration options.
SecurityConfig SecurityConfig
// TransactionsConfig specifies transactions related configuration options.
TransactionsConfig TransactionsConfig
// CompressionConfig specifies compression related configuration options.
CompressionConfig CompressionConfig
// PreferredServerGroup specifies the name of the server group to use with operations supporting ReadPreference.
// UNCOMMITTED: This API may change in the future.
PreferredServerGroup string
// Internal: This should never be used and is not supported.
InternalConfig InternalConfig
}
// ClusterCloseOptions is the set of options available when
// disconnecting from a Cluster.
type ClusterCloseOptions struct {
}
func clusterFromOptions(opts ClusterOptions) *Cluster {
if opts.Authenticator == nil {
opts.Authenticator = PasswordAuthenticator{
Username: opts.Username,
Password: opts.Password,
}
}
connectTimeout := 10000 * time.Millisecond
kvTimeout := 2500 * time.Millisecond
kvDurableTimeout := 10000 * time.Millisecond
kvScanTimeout := 10000 * time.Millisecond
viewTimeout := 75000 * time.Millisecond
queryTimeout := 75000 * time.Millisecond
analyticsTimeout := 75000 * time.Millisecond
searchTimeout := 75000 * time.Millisecond
managementTimeout := 75000 * time.Millisecond
if opts.TimeoutsConfig.ConnectTimeout > 0 {
connectTimeout = opts.TimeoutsConfig.ConnectTimeout
}
if opts.TimeoutsConfig.KVTimeout > 0 {
kvTimeout = opts.TimeoutsConfig.KVTimeout
}
if opts.TimeoutsConfig.KVDurableTimeout > 0 {
kvDurableTimeout = opts.TimeoutsConfig.KVDurableTimeout
}
if opts.TimeoutsConfig.KVScanTimeout > 0 {
kvScanTimeout = opts.TimeoutsConfig.KVScanTimeout
}
if opts.TimeoutsConfig.ViewTimeout > 0 {
viewTimeout = opts.TimeoutsConfig.ViewTimeout
}
if opts.TimeoutsConfig.QueryTimeout > 0 {
queryTimeout = opts.TimeoutsConfig.QueryTimeout
}
if opts.TimeoutsConfig.AnalyticsTimeout > 0 {
analyticsTimeout = opts.TimeoutsConfig.AnalyticsTimeout
}
if opts.TimeoutsConfig.SearchTimeout > 0 {
searchTimeout = opts.TimeoutsConfig.SearchTimeout
}
if opts.TimeoutsConfig.ManagementTimeout > 0 {
managementTimeout = opts.TimeoutsConfig.ManagementTimeout
}
if opts.Transcoder == nil {
opts.Transcoder = NewJSONTranscoder()
}
if opts.RetryStrategy == nil {
opts.RetryStrategy = NewBestEffortRetryStrategy(nil)
}
useMutationTokens := true
useServerDurations := true
if opts.IoConfig.DisableMutationTokens {
useMutationTokens = false
}
if opts.IoConfig.DisableServerDurations {
useServerDurations = false
}
return &Cluster{
auth: opts.Authenticator,
timeoutsConfig: TimeoutsConfig{
ConnectTimeout: connectTimeout,
QueryTimeout: queryTimeout,
AnalyticsTimeout: analyticsTimeout,
SearchTimeout: searchTimeout,
ViewTimeout: viewTimeout,
KVTimeout: kvTimeout,
KVDurableTimeout: kvDurableTimeout,
KVScanTimeout: kvScanTimeout,
ManagementTimeout: managementTimeout,
},
transcoder: opts.Transcoder,
useMutationTokens: useMutationTokens,
retryStrategyWrapper: newCoreRetryStrategyWrapper(opts.RetryStrategy),
orphanLoggerEnabled: !opts.OrphanReporterConfig.Disabled,
orphanLoggerInterval: opts.OrphanReporterConfig.ReportInterval,
orphanLoggerSampleSize: opts.OrphanReporterConfig.SampleSize,
useServerDurations: useServerDurations,
circuitBreakerConfig: opts.CircuitBreakerConfig,
securityConfig: opts.SecurityConfig,
internalConfig: opts.InternalConfig,
transactionsConfig: opts.TransactionsConfig,
compressionConfig: opts.CompressionConfig,
compressor: &compressor{
CompressionEnabled: !opts.CompressionConfig.Disabled,
CompressionMinSize: opts.CompressionConfig.MinSize,
CompressionMinRatio: opts.CompressionConfig.MinRatio,
},
preferredServerGroup: opts.PreferredServerGroup,
}
}
// Connect creates and returns a Cluster instance created using the
// provided options and a connection string.
func Connect(connStr string, opts ClusterOptions) (*Cluster, error) {
connSpec, err := gocbconnstr.Parse(connStr)
if err != nil {
return nil, err
}
if connSpec.Scheme == "http" {
return nil, errors.New("http scheme is not supported")
}
cluster := clusterFromOptions(opts)
cluster.cSpec = connSpec
err = cluster.parseExtraConnStrOptions(connSpec)
if err != nil {
return nil, err
}
var initialTracer RequestTracer
if opts.Tracer != nil {
initialTracer = opts.Tracer
} else {
initialTracer = NewThresholdLoggingTracer(nil)
}
tracerAddRef(initialTracer)
meter := opts.Meter
if meter == nil {
agMeter := NewLoggingMeter(nil)
meter = agMeter
}
cli := cluster.newConnectionMgr(connSpec.Scheme, &newConnectionMgrOptions{
tracer: newTracerWrapper(initialTracer),
meter: newMeterWrapper(meter),
preferredServerGroup: opts.PreferredServerGroup,
})
err = cli.buildConfig(cluster)
if err != nil {
return nil, err
}
err = cli.connect()
if err != nil {
return nil, err
}
cluster.connectionManager = cli
cluster.transactions, err = cluster.initTransactions(cluster.transactionsConfig)
if err != nil {
return nil, err
}
return cluster, nil
}
func (c *Cluster) parseExtraConnStrOptions(spec gocbconnstr.ConnSpec) error {
fetchOption := func(name string) (string, bool) {
optValue := spec.Options[name]
if len(optValue) == 0 {
return "", false
}
return optValue[len(optValue)-1], true
}
if valStr, ok := fetchOption("kv_timeout"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return fmt.Errorf("kv_timeout option must be a number")
}
c.timeoutsConfig.KVTimeout = time.Duration(val) * time.Millisecond
}
if valStr, ok := fetchOption("kv_durable_timeout"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return fmt.Errorf("kv_durable_timeout option must be a number")
}
c.timeoutsConfig.KVDurableTimeout = time.Duration(val) * time.Millisecond
}
// Volatile: This option is subject to change at any time.
if valStr, ok := fetchOption("kv_scan_timeout"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return fmt.Errorf("kv_scan_timeout option must be a number")
}
c.timeoutsConfig.KVScanTimeout = time.Duration(val) * time.Millisecond
}
if valStr, ok := fetchOption("query_timeout"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return fmt.Errorf("query_timeout option must be a number")
}
c.timeoutsConfig.QueryTimeout = time.Duration(val) * time.Millisecond
}
if valStr, ok := fetchOption("analytics_timeout"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return fmt.Errorf("analytics_timeout option must be a number")
}
c.timeoutsConfig.AnalyticsTimeout = time.Duration(val) * time.Millisecond
}
if valStr, ok := fetchOption("search_timeout"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return fmt.Errorf("search_timeout option must be a number")
}
c.timeoutsConfig.SearchTimeout = time.Duration(val) * time.Millisecond
}
if valStr, ok := fetchOption("view_timeout"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return fmt.Errorf("view_timeout option must be a number")
}
c.timeoutsConfig.ViewTimeout = time.Duration(val) * time.Millisecond
}
if valStr, ok := fetchOption("management_timeout"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return fmt.Errorf("management_timeout option must be a number")
}
c.timeoutsConfig.ManagementTimeout = time.Duration(val) * time.Millisecond
}
return nil
}
// Bucket connects the cluster to server(s) and returns a new Bucket instance.
func (c *Cluster) Bucket(bucketName string) *Bucket {
b := newBucket(c, bucketName)
err := c.connectionManager.openBucket(bucketName)
if err != nil {
b.setBootstrapError(err)
}
return b
}
func (c *Cluster) authenticator() Authenticator {
return c.auth
}
func (c *Cluster) connSpec() gocbconnstr.ConnSpec {
return c.cSpec
}
// WaitUntilReadyOptions is the set of options available to the WaitUntilReady operations.
type WaitUntilReadyOptions struct {
DesiredState ClusterState
ServiceTypes []ServiceType
// Using a deadlined Context with WaitUntilReady will cause the shorter of the provided timeout and context deadline
// to cause cancellation.
Context context.Context
// VOLATILE: This API is subject to change at any time.
RetryStrategy RetryStrategy
}
// WaitUntilReady will wait for the cluster object to be ready for use.
// At present this will wait until memd connections have been established with the server and are ready
// to be used before performing a ping against the specified services which also
// exist in the cluster map.
// If no services are specified then ServiceTypeManagement, ServiceTypeQuery, ServiceTypeSearch, ServiceTypeAnalytics
// will be pinged.
// Valid service types are: ServiceTypeManagement, ServiceTypeQuery, ServiceTypeSearch, ServiceTypeAnalytics.
func (c *Cluster) WaitUntilReady(timeout time.Duration, opts *WaitUntilReadyOptions) error {
return autoOpControlErrorOnly(c.waitUntilReadyController(), "", func(provider waitUntilReadyProvider) error {
if opts == nil {
opts = &WaitUntilReadyOptions{}
}
err := provider.WaitUntilReady(
opts.Context,
time.Now().Add(timeout),
opts,
)
if err != nil {
return maybeEnhanceCoreErr(err)
}
return nil
})
}
// Close shuts down all buckets in this cluster and invalidates any references this cluster has.
func (c *Cluster) Close(opts *ClusterCloseOptions) error {
var overallErr error
if c.connectionManager != nil {
err := c.connectionManager.close()
if err != nil {
logWarnf("Failed to close cluster connectionManager in cluster close: %s", err)
overallErr = err
}
}
return overallErr
}
func (c *Cluster) waitUntilReadyController() *providerController[waitUntilReadyProvider] {
return &providerController[waitUntilReadyProvider]{
get: func() (waitUntilReadyProvider, error) {
return c.connectionManager.getWaitUntilReadyProvider("")
},
opController: c.connectionManager,
}
}
func (c *Cluster) analyticsController() *providerController[analyticsProvider] {
return &providerController[analyticsProvider]{
get: c.connectionManager.getAnalyticsProvider,
opController: c.connectionManager,
meter: c.connectionManager.getMeter(),
keyspace: &c.keyspace,
service: serviceValueAnalytics,
}
}
func (c *Cluster) diagnosticsController() *providerController[diagnosticsProvider] {
return &providerController[diagnosticsProvider]{
get: func() (diagnosticsProvider, error) {
return c.connectionManager.getDiagnosticsProvider("")
},
opController: c.connectionManager,
}
}
func (c *Cluster) queryController() *providerController[queryProvider] {
return &providerController[queryProvider]{
get: c.connectionManager.getQueryProvider,
opController: c.connectionManager,
meter: c.connectionManager.getMeter(),
keyspace: &c.keyspace,
service: serviceValueQuery,
}
}
func (c *Cluster) searchController() *providerController[searchProvider] {
return &providerController[searchProvider]{
get: c.connectionManager.getSearchProvider,
opController: c.connectionManager,
meter: c.connectionManager.getMeter(),
keyspace: &c.keyspace,
service: serviceValueSearch,
}
}
func (c *Cluster) internalController() *providerController[internalProvider] {
return &providerController[internalProvider]{
get: c.connectionManager.getInternalProvider,
opController: c.connectionManager,
}
}
func (c *Cluster) transactionsController() *providerController[transactionsProvider] {
return &providerController[transactionsProvider]{
get: c.connectionManager.getTransactionsProvider,
opController: c.connectionManager,
}
}
// Users returns a UserManager for managing users.
func (c *Cluster) Users() *UserManager {
return &UserManager{
controller: &providerController[userManagerProvider]{
get: c.connectionManager.getUserManagerProvider,
opController: c.connectionManager,
meter: c.connectionManager.getMeter(),
keyspace: &c.keyspace,
service: serviceValueManagement,
},
}
}
// Buckets returns a BucketManager for managing buckets.
func (c *Cluster) Buckets() *BucketManager {
return &BucketManager{
controller: &providerController[bucketManagementProvider]{
get: c.connectionManager.getBucketManagementProvider,
opController: c.connectionManager,
meter: c.connectionManager.getMeter(),
keyspace: &c.keyspace,
service: serviceValueManagement,
},
}
}
// AnalyticsIndexes returns an AnalyticsIndexManager for managing analytics indexes.
func (c *Cluster) AnalyticsIndexes() *AnalyticsIndexManager {
return &AnalyticsIndexManager{
controller: &providerController[analyticsIndexProvider]{
get: c.connectionManager.getAnalyticsIndexProvider,
opController: c.connectionManager,
meter: c.connectionManager.getMeter(),
keyspace: &c.keyspace,
service: serviceValueManagement,
},
}
}
// QueryIndexes returns a QueryIndexManager for managing query indexes.
func (c *Cluster) QueryIndexes() *QueryIndexManager {
return &QueryIndexManager{
controller: &providerController[queryIndexProvider]{
get: c.connectionManager.getQueryIndexProvider,
opController: c.connectionManager,
meter: c.connectionManager.getMeter(),
keyspace: &c.keyspace,
service: serviceValueManagement,
},
}
}
// SearchIndexes returns a SearchIndexManager for managing cluster-level search indexes.
func (c *Cluster) SearchIndexes() *SearchIndexManager {
return &SearchIndexManager{
controller: &providerController[searchIndexProvider]{
get: c.connectionManager.getSearchIndexProvider,
opController: c.connectionManager,
meter: c.connectionManager.getMeter(),
keyspace: &c.keyspace,
service: serviceValueManagement,
},
}
}
// EventingFunctions returns a EventingFunctionManager for managing eventing functions.
//
// # UNCOMMITTED
//
// This API is UNCOMMITTED and may change in the future.
func (c *Cluster) EventingFunctions() *EventingFunctionManager {
return &EventingFunctionManager{
controller: &providerController[eventingManagementProvider]{
get: c.connectionManager.getEventingManagementProvider,
opController: c.connectionManager,
meter: c.connectionManager.getMeter(),
keyspace: &c.keyspace,
service: serviceValueManagement,
},
}
}
// Transactions returns a Transactions instance for performing transactions.
func (c *Cluster) Transactions() *Transactions {
return c.transactions
}