forked from couchbase/gocbcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent_config.go
590 lines (493 loc) · 18.3 KB
/
agent_config.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
package gocbcore
import (
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"strconv"
"time"
"github.com/couchbase/gocbcore/v10/connstr"
)
func parseDurationOrInt(valStr string) (time.Duration, error) {
dur, err := time.ParseDuration(valStr)
if err != nil {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return 0, err
}
dur = time.Duration(val) * time.Millisecond
}
return dur, nil
}
// AgentConfig specifies the configuration options for creation of an Agent.
type AgentConfig struct {
BucketName string
UserAgent string
SeedConfig SeedConfig
SecurityConfig SecurityConfig
CompressionConfig CompressionConfig
ConfigPollerConfig ConfigPollerConfig
IoConfig IoConfig
KVConfig KVConfig
HTTPConfig HTTPConfig
DefaultRetryStrategy RetryStrategy
CircuitBreakerConfig CircuitBreakerConfig
OrphanReporterConfig OrphanReporterConfig
TracerConfig TracerConfig
MeterConfig MeterConfig
}
// 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 {
Enabled bool
// ReportInterval is the time period used for how often a report is logged.
ReportInterval time.Duration
// SampleSize is the number of requests which will be reported.
SampleSize int
}
func (config OrphanReporterConfig) fromSpec(spec connstr.ResolvedConnSpec) (OrphanReporterConfig, error) {
if valStr, ok := fetchOption(spec, "orphaned_response_logging"); ok {
val, err := strconv.ParseBool(valStr)
if err != nil {
return OrphanReporterConfig{}, fmt.Errorf("orphaned_response_logging option must be a boolean")
}
config.Enabled = val
}
if valStr, ok := fetchOption(spec, "orphaned_response_logging_interval"); ok {
val, err := parseDurationOrInt(valStr)
if err != nil {
return OrphanReporterConfig{}, fmt.Errorf("orphaned_response_logging_interval option must be a number")
}
config.ReportInterval = val
}
if valStr, ok := fetchOption(spec, "orphaned_response_logging_sample_size"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return OrphanReporterConfig{}, fmt.Errorf("orphaned_response_logging_sample_size option must be a number")
}
config.SampleSize = int(val)
}
return config, nil
}
// SecurityConfig specifies options for controlling security related
// items such as TLS root certificates and verification skipping.
type SecurityConfig struct {
UseTLS bool
TLSRootCAProvider func() *x509.CertPool
// NoTLSSeedNode indicates that, even with UseTLS set to true, the SDK should always connect to the seed node
// over a non TLS connection. This means that the seed node should ALWAYS be localhost.
// This option must be used with the ConfigPollerConfig UseSeedPoller set to true.
// Internal: This should never be used and is not supported.
NoTLSSeedNode bool
Auth AuthProvider
// AuthMechanisms 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.
AuthMechanisms []AuthMechanism
}
func (config SecurityConfig) fromSpec(spec connstr.ResolvedConnSpec) (SecurityConfig, error) {
if spec.UseSsl {
cacertpaths := spec.Options["ca_cert_path"]
if len(cacertpaths) > 0 {
roots := x509.NewCertPool()
for _, path := range cacertpaths {
cacert, err := ioutil.ReadFile(path)
if err != nil {
return SecurityConfig{}, err
}
ok := roots.AppendCertsFromPEM(cacert)
if !ok {
return SecurityConfig{}, errInvalidCertificate
}
}
config.TLSRootCAProvider = func() *x509.CertPool {
return roots
}
}
config.UseTLS = true
}
if spec.NSServerHost != nil {
config.NoTLSSeedNode = true
}
return config, nil
}
// CompressionConfig specifies options for controlling compression applied to documents using KV.
type CompressionConfig struct {
Enabled bool
DisableDecompression bool
MinSize int
MinRatio float64
}
func (config CompressionConfig) fromSpec(spec connstr.ResolvedConnSpec) (CompressionConfig, error) {
if valStr, ok := fetchOption(spec, "compression"); ok {
val, err := strconv.ParseBool(valStr)
if err != nil {
return CompressionConfig{}, fmt.Errorf("compression option must be a boolean")
}
config.Enabled = val
}
if valStr, ok := fetchOption(spec, "compression_min_size"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return CompressionConfig{}, fmt.Errorf("compression_min_size option must be an int")
}
config.MinSize = int(val)
}
if valStr, ok := fetchOption(spec, "compression_min_ratio"); ok {
val, err := strconv.ParseFloat(valStr, 64)
if err != nil {
return CompressionConfig{}, fmt.Errorf("compression_min_size option must be an int")
}
config.MinRatio = val
}
return config, nil
}
// ConfigPollerConfig specifies options for controlling the cluster configuration pollers.
type ConfigPollerConfig struct {
HTTPRedialPeriod time.Duration
HTTPRetryDelay time.Duration
HTTPMaxWait time.Duration
CccpMaxWait time.Duration
CccpPollPeriod time.Duration
}
func (config ConfigPollerConfig) fromSpec(spec connstr.ResolvedConnSpec) (ConfigPollerConfig, error) {
if valStr, ok := fetchOption(spec, "config_poll_timeout"); ok {
val, err := parseDurationOrInt(valStr)
if err != nil {
return ConfigPollerConfig{}, fmt.Errorf("config poll timeout option must be a duration or a number")
}
config.CccpMaxWait = val
}
if valStr, ok := fetchOption(spec, "config_poll_interval"); ok {
val, err := parseDurationOrInt(valStr)
if err != nil {
return ConfigPollerConfig{}, fmt.Errorf("config pool interval option must be duration or a number")
}
config.CccpPollPeriod = val
}
// This option is experimental
if valStr, ok := fetchOption(spec, "http_redial_period"); ok {
val, err := parseDurationOrInt(valStr)
if err != nil {
return ConfigPollerConfig{}, fmt.Errorf("http redial period option must be a duration or a number")
}
config.HTTPRedialPeriod = val
}
// This option is experimental
if valStr, ok := fetchOption(spec, "http_retry_delay"); ok {
val, err := parseDurationOrInt(valStr)
if err != nil {
return ConfigPollerConfig{}, fmt.Errorf("http retry delay option must be a duration or a number")
}
config.HTTPRetryDelay = val
}
if valStr, ok := fetchOption(spec, "http_config_poll_timeout"); ok {
val, err := parseDurationOrInt(valStr)
if err != nil {
return ConfigPollerConfig{}, fmt.Errorf("http_config_poll_timeout option must be a duration or a number")
}
config.HTTPMaxWait = val
}
return config, nil
}
// IoConfig specifies IO related configuration options such as HELLO flags and the network type to use.
type IoConfig struct {
// NetworkType defines which network to use from the cluster config.
NetworkType string
UseMutationTokens bool
UseDurations bool
UseOutOfOrderResponses bool
DisableXErrorHello bool
DisableJSONHello bool
DisableSyncReplicationHello bool
EnablePITRHello bool
UseCollections bool
}
func (config IoConfig) fromSpec(spec connstr.ResolvedConnSpec) (IoConfig, error) {
if valStr, ok := fetchOption(spec, "network"); ok {
config.NetworkType = valStr
}
if valStr, ok := fetchOption(spec, "enable_mutation_tokens"); ok {
val, err := strconv.ParseBool(valStr)
if err != nil {
return IoConfig{}, fmt.Errorf("enable_mutation_tokens option must be a boolean")
}
config.UseMutationTokens = val
}
if valStr, ok := fetchOption(spec, "enable_server_durations"); ok {
val, err := strconv.ParseBool(valStr)
if err != nil {
return IoConfig{}, fmt.Errorf("server_duration option must be a boolean")
}
config.UseDurations = val
}
// This option is experimental
if valStr, ok := fetchOption(spec, "unordered_execution_enabled"); ok {
val, err := strconv.ParseBool(valStr)
if err != nil {
return IoConfig{}, fmt.Errorf("unordered_execution_enabled option must be a boolean")
}
config.UseOutOfOrderResponses = val
}
return config, nil
}
// TracerConfig specifies tracer related configuration options.
type TracerConfig struct {
Tracer RequestTracer
NoRootTraceSpans bool
}
// MeterConfig specifies meter related configuration options.
type MeterConfig struct {
Meter Meter
}
// HTTPConfig specifies http related configuration options.
type HTTPConfig struct {
// MaxIdleConns controls the maximum number of idle (keep-alive) connections across all hosts.
MaxIdleConns int
// MaxIdleConnsPerHost controls the maximum idle (keep-alive) connections to keep per-host.
MaxIdleConnsPerHost int
ConnectTimeout time.Duration
// IdleConnTimeout is the maximum amount of time an idle (keep-alive) connection will remain idle before closing
// itself.
IdleConnectionTimeout time.Duration
}
func (config HTTPConfig) fromSpec(spec connstr.ResolvedConnSpec) (HTTPConfig, error) {
if valStr, ok := fetchOption(spec, "max_idle_http_connections"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return HTTPConfig{}, fmt.Errorf("http max idle connections option must be a number")
}
config.MaxIdleConns = int(val)
}
if valStr, ok := fetchOption(spec, "max_perhost_idle_http_connections"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return HTTPConfig{}, fmt.Errorf("max_perhost_idle_http_connections option must be a number")
}
config.MaxIdleConnsPerHost = int(val)
}
if valStr, ok := fetchOption(spec, "idle_http_connection_timeout"); ok {
val, err := parseDurationOrInt(valStr)
if err != nil {
return HTTPConfig{}, fmt.Errorf("idle_http_connection_timeout option must be a duration or a number")
}
config.IdleConnectionTimeout = val
}
if valStr, ok := fetchOption(spec, "http_connect_timeout"); ok {
val, err := parseDurationOrInt(valStr)
if err != nil {
return HTTPConfig{}, fmt.Errorf("http_connect_timeout option must be a duration or a number")
}
config.ConnectTimeout = val
}
return config, nil
}
// KVConfig specifies kv related configuration options.
type KVConfig struct {
// ConnectTimeout is the timeout value to apply when dialling tcp connections.
ConnectTimeout time.Duration
// ServerWaitBackoff is the period of time that the SDK will wait before reattempting connection to a node after
// bootstrap fails against that node.
ServerWaitBackoff time.Duration
// The number of connections to create to each node.
PoolSize int
// The maximum number of requests that can be queued waiting to be sent to a node.
MaxQueueSize int
// Note: if you create multiple agents with different buffer sizes within the same environment then you will
// get indeterminate behaviour, the connections may not even use the provided buffer size.
ConnectionBufferSize uint
}
func (config KVConfig) fromSpec(spec connstr.ResolvedConnSpec) (KVConfig, error) {
if valStr, ok := fetchOption(spec, "kv_connect_timeout"); ok {
val, err := parseDurationOrInt(valStr)
if err != nil {
return KVConfig{}, fmt.Errorf("kv_connect_timeout option must be a duration or a number")
}
config.ConnectTimeout = val
}
// This option is experimental
if valStr, ok := fetchOption(spec, "kv_pool_size"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return KVConfig{}, fmt.Errorf("kv pool size option must be a number")
}
config.PoolSize = int(val)
}
// This option is experimental
if valStr, ok := fetchOption(spec, "max_queue_size"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return KVConfig{}, fmt.Errorf("max queue size option must be a number")
}
config.MaxQueueSize = int(val)
}
// This option is experimental
if valStr, ok := fetchOption(spec, "kv_buffer_size"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return KVConfig{}, fmt.Errorf("kv buffer size option must be a number")
}
config.ConnectionBufferSize = uint(val)
}
return config, nil
}
// SeedConfig specifies initial seed configuration options such as addresses.
type SeedConfig struct {
HTTPAddrs []string
MemdAddrs []string
}
func (config SeedConfig) fromSpec(spec connstr.ResolvedConnSpec) (SeedConfig, error) {
// Grab the resolved hostnames into a set of string arrays
var httpHosts []string
for _, specHost := range spec.HttpHosts {
httpHosts = append(httpHosts, fmt.Sprintf("%s:%d", specHost.Host, specHost.Port))
}
var memdHosts []string
for _, specHost := range spec.MemdHosts {
memdHosts = append(memdHosts, fmt.Sprintf("%s:%d", specHost.Host, specHost.Port))
}
var nsServerHost string
if spec.NSServerHost != nil {
nsServerHost = fmt.Sprintf("%s:%d", spec.NSServerHost.Host, spec.NSServerHost.Port)
}
if nsServerHost != "" {
if len(httpHosts) > 0 || len(memdHosts) > 0 {
return SeedConfig{}, errors.New("ns_server host cannot be used alongside http or memd hosts")
}
httpHosts = append(httpHosts, nsServerHost)
}
// Get bootstrap_on option to determine which, if any, of the bootstrap nodes should be cleared
switch val, _ := fetchOption(spec, "bootstrap_on"); val {
case "http":
memdHosts = nil
if len(httpHosts) == 0 {
return SeedConfig{}, errors.New("bootstrap_on=http but no HTTP hosts in connection string")
}
case "cccp":
httpHosts = nil
if len(memdHosts) == 0 {
return SeedConfig{}, errors.New("bootstrap_on=cccp but no CCCP/Memcached hosts in connection string")
}
case "both":
if nsServerHost != "" {
return SeedConfig{}, errors.New("bootstrap_on=both but ns_server host in connection string")
}
case "":
// Do nothing
break
default:
// Don't advertise ns_server as an option
return SeedConfig{}, errors.New("bootstrap_on={http,cccp,both}")
}
config.MemdAddrs = memdHosts
config.HTTPAddrs = httpHosts
return config, nil
}
func (config SeedConfig) redacted() SeedConfig {
newConfig := SeedConfig{
HTTPAddrs: config.HTTPAddrs,
MemdAddrs: config.MemdAddrs,
}
// The slices here are still pointing at config's underlying arrays
// so we need to make them not do that.
newConfig.HTTPAddrs = append([]string(nil), newConfig.HTTPAddrs...)
for i, addr := range newConfig.HTTPAddrs {
newConfig.HTTPAddrs[i] = redactSystemData(addr)
}
newConfig.MemdAddrs = append([]string(nil), newConfig.MemdAddrs...)
for i, addr := range newConfig.MemdAddrs {
newConfig.MemdAddrs[i] = redactSystemData(addr)
}
return newConfig
}
func (config *AgentConfig) redacted() interface{} {
newConfig := *config
if isLogRedactionLevelFull() {
newConfig.SeedConfig = newConfig.SeedConfig.redacted()
if newConfig.BucketName != "" {
newConfig.BucketName = redactMetaData(newConfig.BucketName)
}
}
return newConfig
}
func fetchOption(spec connstr.ResolvedConnSpec, name string) (string, bool) {
optValue := spec.Options[name]
if len(optValue) == 0 {
return "", false
}
return optValue[len(optValue)-1], true
}
// FromConnStr populates the AgentConfig with information from a
// Couchbase Connection String.
// Supported options are:
// bootstrap_on (bool) - Specifies what protocol to bootstrap on (cccp, http).
// ca_cert_path (string) - Specifies the path to a CA certificate.
// network (string) - The network type to use.
// kv_connect_timeout (duration) - Maximum period to attempt to connect to cluster in ms.
// config_poll_interval (duration) - Period to wait between CCCP config polling in ms.
// config_poll_timeout (duration) - Maximum period of time to wait for a CCCP request.
// compression (bool) - Whether to enable network-wise compression of documents.
// compression_min_size (int) - The minimal size of the document in bytes to consider compression.
// compression_min_ratio (float64) - The minimal compress ratio (compressed / original) for the document to be sent compressed.
// enable_server_durations (bool) - Whether to enable fetching server operation durations.
// max_idle_http_connections (int) - Maximum number of idle http connections in the pool.
// max_perhost_idle_http_connections (int) - Maximum number of idle http connections in the pool per host.
// idle_http_connection_timeout (duration) - Maximum length of time for an idle connection to stay in the pool in ms.
// orphaned_response_logging (bool) - Whether to enable orphaned response logging.
// orphaned_response_logging_interval (duration) - How often to print the orphan log records.
// orphaned_response_logging_sample_size (int) - The maximum number of orphan log records to track.
// dcp_priority (int) - Specifies the priority to request from the Cluster when connecting for DCP.
// enable_dcp_expiry (bool) - Whether to enable the feature to distinguish between explicit delete and expired delete on DCP.
// http_redial_period (duration) - The maximum length of time for the HTTP poller to stay connected before reconnecting.
// http_retry_delay (duration) - The length of time to wait between HTTP poller retries if connecting fails.
// kv_pool_size (int) - The number of connections to create to each kv node.
// max_queue_size (int) - The maximum number of requests that can be queued for sending per connection.
// unordered_execution_enabled (bool) - Whether to enabled the "out of order responses" feature.
func (config *AgentConfig) FromConnStr(connStr string) error {
baseSpec, err := connstr.Parse(connStr)
if err != nil {
return err
}
spec, err := connstr.Resolve(baseSpec)
if err != nil {
return err
}
if spec.Bucket != "" {
config.BucketName = spec.Bucket
}
config.SeedConfig, err = config.SeedConfig.fromSpec(spec)
if err != nil {
return err
}
config.SecurityConfig, err = config.SecurityConfig.fromSpec(spec)
if err != nil {
return err
}
config.OrphanReporterConfig, err = config.OrphanReporterConfig.fromSpec(spec)
if err != nil {
return err
}
config.CompressionConfig, err = config.CompressionConfig.fromSpec(spec)
if err != nil {
return err
}
config.ConfigPollerConfig, err = config.ConfigPollerConfig.fromSpec(spec)
if err != nil {
return err
}
config.IoConfig, err = config.IoConfig.fromSpec(spec)
if err != nil {
return err
}
config.HTTPConfig, err = config.HTTPConfig.fromSpec(spec)
if err != nil {
return err
}
config.KVConfig, err = config.KVConfig.fromSpec(spec)
if err != nil {
return err
}
return nil
}