forked from couchbase/gocbcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcpagent.go
535 lines (465 loc) · 16.6 KB
/
dcpagent.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
package gocbcore
import (
"errors"
"fmt"
"sync"
"time"
"github.com/couchbase/gocbcore/v10/memd"
)
// DCPAgent represents the base client handling DCP connections to a Couchbase Server.
type DCPAgent struct {
clientID string
bucketName string
initFn memdInitFunc
pollerController configPollerController
kvMux *kvMux
httpMux *httpMux
dialer *memdClientDialerComponent
cfgManager *configManagementComponent
errMap *errMapComponent
tracer *tracerComponent
diagnostics *diagnosticsComponent
dcp *dcpComponent
http *httpComponent
// These connection settings are only ever changed when ForceReconnect or ReconfigureSecurity are called.
connectionSettingsLock sync.Mutex
auth AuthProvider
authMechanisms []AuthMechanism
tlsConfig *dynTLSConfig
}
// CreateDcpAgent creates an agent for performing DCP operations.
func CreateDcpAgent(config *DCPAgentConfig, dcpStreamName string, openFlags memd.DcpOpenFlag) (*DCPAgent, error) {
logInfof("SDK Version: gocbcore/%s", goCbCoreVersionStr)
logInfof("Creating new dcp agent: %+v", config)
userAgent := config.UserAgent
disableDecompression := config.CompressionConfig.DisableDecompression
useCompression := config.CompressionConfig.Enabled
useCollections := config.IoConfig.UseCollections
useJSONHello := !config.IoConfig.DisableJSONHello
usePITRHello := config.IoConfig.EnablePITRHello
useXErrorHello := !config.IoConfig.DisableXErrorHello
useSyncReplicationHello := !config.IoConfig.DisableSyncReplicationHello
dcpBufferSize := 20 * 1024 * 1024
compressionMinSize := 32
compressionMinRatio := 0.83
dcpBackfillOrderStr := ""
dcpPriorityStr := ""
kvConnectTimeout := 7000 * time.Millisecond
if config.KVConfig.ConnectTimeout > 0 {
kvConnectTimeout = config.KVConfig.ConnectTimeout
}
serverWaitTimeout := 5 * time.Second
kvPoolSize := 1
if config.KVConfig.PoolSize > 0 {
kvPoolSize = config.KVConfig.PoolSize
}
maxQueueSize := 2048
if config.KVConfig.MaxQueueSize > 0 {
maxQueueSize = config.KVConfig.MaxQueueSize
}
kvBufferSize := uint(0)
if config.KVConfig.ConnectionBufferSize > 0 {
kvBufferSize = config.KVConfig.ConnectionBufferSize
}
confCccpMaxWait := 3 * time.Second
if config.ConfigPollerConfig.CccpMaxWait > 0 {
confCccpMaxWait = config.ConfigPollerConfig.CccpMaxWait
}
confCccpPollPeriod := 2500 * time.Millisecond
if config.ConfigPollerConfig.CccpPollPeriod > 0 {
confCccpPollPeriod = config.ConfigPollerConfig.CccpPollPeriod
}
confHTTPRetryDelay := 10 * time.Second
if config.ConfigPollerConfig.HTTPRetryDelay > 0 {
confHTTPRetryDelay = config.ConfigPollerConfig.HTTPRetryDelay
}
confHTTPRedialPeriod := 10 * time.Second
if config.ConfigPollerConfig.HTTPRedialPeriod > 0 {
confHTTPRedialPeriod = config.ConfigPollerConfig.HTTPRedialPeriod
}
confHTTPMaxWait := 5 * time.Second
if config.ConfigPollerConfig.HTTPMaxWait > 0 {
confHTTPMaxWait = config.ConfigPollerConfig.HTTPMaxWait
}
if config.CompressionConfig.MinSize > 0 {
compressionMinSize = config.CompressionConfig.MinSize
}
if config.CompressionConfig.MinRatio > 0 {
compressionMinRatio = config.CompressionConfig.MinRatio
if compressionMinRatio >= 1.0 {
compressionMinRatio = 1.0
}
}
if config.DCPConfig.BufferSize > 0 {
dcpBufferSize = config.DCPConfig.BufferSize
}
dcpQueueSize := (dcpBufferSize + 23) / 24
switch config.DCPConfig.AgentPriority {
case DcpAgentPriorityLow:
dcpPriorityStr = "low"
case DcpAgentPriorityMed:
dcpPriorityStr = "medium"
case DcpAgentPriorityHigh:
dcpPriorityStr = "high"
}
// If the user doesn't explicitly set the backfill order, the DCP control flag will not be sent to the cluster
// and the default will implicitly be used (which is 'round-robin').
switch config.DCPConfig.BackfillOrder {
case DCPBackfillOrderRoundRobin:
dcpBackfillOrderStr = "round-robin"
case DCPBackfillOrderSequential:
dcpBackfillOrderStr = "sequential"
}
tracerCmpt := newTracerComponent(noopTracer{}, config.BucketName, false, nil)
// We wrap the authorization system to force DCP channel opening
// as part of the "initialization" for any servers.
initFn := func(client *memdClient, deadline time.Time) error {
sclient := &syncClient{client: client}
if err := sclient.ExecOpenDcpConsumer(dcpStreamName, openFlags, deadline); err != nil {
return err
}
if err := sclient.ExecEnableDcpNoop(180*time.Second, deadline); err != nil {
return err
}
if dcpPriorityStr != "" {
if err := sclient.ExecDcpControl("set_priority", dcpPriorityStr, deadline); err != nil {
return err
}
}
if config.DCPConfig.UseExpiryOpcode {
if err := sclient.ExecDcpControl("enable_expiry_opcode", "true", deadline); err != nil {
return err
}
}
if config.DCPConfig.UseStreamID {
if err := sclient.ExecDcpControl("enable_stream_id", "true", deadline); err != nil {
return err
}
}
if config.DCPConfig.UseOSOBackfill {
if err := sclient.ExecDcpControl("enable_out_of_order_snapshots", "true", deadline); err != nil {
return err
}
}
if dcpBackfillOrderStr != "" {
if err := sclient.ExecDcpControl("backfill_order", dcpBackfillOrderStr, deadline); err != nil {
return err
}
}
if !config.DCPConfig.DisableBufferAcknowledgement {
if err := sclient.ExecEnableDcpBufferAck(dcpBufferSize, deadline); err != nil {
return err
}
}
return sclient.ExecEnableDcpClientEnd(deadline)
}
c := &DCPAgent{
clientID: formatCbUID(randomCbUID()),
bucketName: config.BucketName,
initFn: initFn,
tracer: tracerCmpt,
errMap: newErrMapManager(config.BucketName),
auth: config.SecurityConfig.Auth,
}
tlsConfig, err := setupTLSConfig(config.SeedConfig.MemdAddrs, config.SecurityConfig)
if err != nil {
return nil, err
}
c.tlsConfig = tlsConfig
c.authMechanisms = authMechanismsFromConfig(config.SecurityConfig.AuthMechanisms, config.SecurityConfig.UseTLS)
circuitBreakerConfig := CircuitBreakerConfig{
Enabled: false,
}
httpEpList := routeEndpoints{}
var srcHTTPAddrs []routeEndpoint
for _, hostPort := range config.SeedConfig.HTTPAddrs {
if config.SecurityConfig.UseTLS && !config.SecurityConfig.NoTLSSeedNode {
ep := routeEndpoint{
Address: fmt.Sprintf("https://%s", hostPort),
IsSeedNode: true,
}
httpEpList.SSLEndpoints = append(httpEpList.SSLEndpoints, ep)
srcHTTPAddrs = append(srcHTTPAddrs, ep)
} else {
ep := routeEndpoint{
Address: fmt.Sprintf("http://%s", hostPort),
IsSeedNode: true,
}
httpEpList.NonSSLEndpoints = append(httpEpList.NonSSLEndpoints, ep)
srcHTTPAddrs = append(srcHTTPAddrs, ep)
}
}
kvServerList := routeEndpoints{}
var srcMemdAddrs []routeEndpoint
for _, seed := range config.SeedConfig.MemdAddrs {
if config.SecurityConfig.UseTLS && !config.SecurityConfig.NoTLSSeedNode {
kvServerList.SSLEndpoints = append(kvServerList.SSLEndpoints, routeEndpoint{
Address: seed,
IsSeedNode: true,
})
srcMemdAddrs = kvServerList.SSLEndpoints
} else {
kvServerList.NonSSLEndpoints = append(kvServerList.NonSSLEndpoints, routeEndpoint{
Address: seed,
IsSeedNode: true,
})
srcMemdAddrs = kvServerList.NonSSLEndpoints
}
}
httpConnectTimeout := 30 * time.Second
if config.HTTPConfig.ConnectTimeout > 0 {
httpConnectTimeout = config.HTTPConfig.ConnectTimeout
}
c.cfgManager = newConfigManager(
configManagerProperties{
NetworkType: config.IoConfig.NetworkType,
SrcMemdAddrs: srcMemdAddrs,
SrcHTTPAddrs: srcHTTPAddrs,
UseTLS: tlsConfig != nil,
NoTLSSeedNode: config.SecurityConfig.NoTLSSeedNode,
},
)
c.dialer = newMemdClientDialerComponent(
memdClientDialerProps{
ServerWaitTimeout: serverWaitTimeout,
KVConnectTimeout: kvConnectTimeout,
ClientID: c.clientID,
DCPQueueSize: dcpQueueSize,
CompressionMinSize: compressionMinSize,
CompressionMinRatio: compressionMinRatio,
DisableDecompression: disableDecompression,
NoTLSSeedNode: config.SecurityConfig.NoTLSSeedNode,
ConnBufSize: kvBufferSize,
},
bootstrapProps{
HelloProps: helloProps{
CollectionsEnabled: useCollections,
CompressionEnabled: useCompression,
JSONFeatureEnabled: useJSONHello,
PITRFeatureEnabled: usePITRHello,
XErrorFeatureEnabled: useXErrorHello,
SyncReplicationEnabled: useSyncReplicationHello,
},
Bucket: c.bucketName,
UserAgent: userAgent,
ErrMapManager: c.errMap,
},
circuitBreakerConfig,
nil,
c.tracer,
initFn,
)
c.kvMux = newKVMux(
kvMuxProps{
QueueSize: maxQueueSize,
PoolSize: kvPoolSize,
CollectionsEnabled: useCollections,
NoTLSSeedNode: config.SecurityConfig.NoTLSSeedNode,
},
c.cfgManager,
c.errMap,
c.tracer,
c.dialer,
&kvMuxState{
tlsConfig: tlsConfig,
authMechanisms: c.authMechanisms,
auth: config.SecurityConfig.Auth,
},
)
c.httpMux = newHTTPMux(
circuitBreakerConfig,
c.cfgManager,
&httpClientMux{tlsConfig: tlsConfig, auth: config.SecurityConfig.Auth},
config.SecurityConfig.NoTLSSeedNode,
)
c.http = newHTTPComponent(
httpComponentProps{
UserAgent: userAgent,
},
httpClientProps{
maxIdleConns: config.HTTPConfig.MaxIdleConns,
maxIdleConnsPerHost: config.HTTPConfig.MaxIdleConnsPerHost,
idleTimeout: config.HTTPConfig.IdleConnectionTimeout,
connectTimeout: httpConnectTimeout,
},
c.httpMux,
c.tracer,
)
var poller configPollerController
if config.SecurityConfig.NoTLSSeedNode {
poller = newSeedConfigController(srcHTTPAddrs[0].Address, c.bucketName,
httpPollerProperties{
httpComponent: c.http,
confHTTPRetryDelay: confHTTPRetryDelay,
confHTTPRedialPeriod: confHTTPRedialPeriod,
confHTTPMaxWait: confHTTPMaxWait,
}, c.cfgManager)
} else {
var httpPoller *httpConfigController
if c.bucketName != "" {
httpPoller = newHTTPConfigController(
c.bucketName,
httpPollerProperties{
httpComponent: c.http,
confHTTPRetryDelay: confHTTPRetryDelay,
confHTTPRedialPeriod: confHTTPRedialPeriod,
confHTTPMaxWait: confHTTPMaxWait,
},
c.httpMux,
c.cfgManager,
)
}
poller = newPollerController(
newCCCPConfigController(
cccpPollerProperties{
confCccpMaxWait: confCccpMaxWait,
confCccpPollPeriod: confCccpPollPeriod,
},
c.kvMux,
c.cfgManager,
c.isPollingFallbackError,
),
httpPoller,
c.cfgManager,
c.isPollingFallbackError,
)
}
c.pollerController = poller
c.diagnostics = newDiagnosticsComponent(c.kvMux, nil, nil, c.bucketName, newFailFastRetryStrategy(), c.pollerController)
c.dcp = newDcpComponent(c.kvMux, config.DCPConfig.UseStreamID)
c.dialer.AddBootstrapFailHandler(c)
c.dialer.AddBootstrapFailHandler(c.diagnostics)
// Kick everything off.
cfg := &routeConfig{
kvServerList: kvServerList,
mgmtEpList: httpEpList,
revID: -1,
}
c.httpMux.OnNewRouteConfig(cfg)
c.kvMux.OnNewRouteConfig(cfg)
go c.pollerController.Start()
return c, nil
}
// IsSecure returns whether this client is connected via SSL.
func (agent *DCPAgent) IsSecure() bool {
return agent.kvMux.IsSecure()
}
// Close shuts down the agent, disconnecting from all servers and failing
// any outstanding operations with ErrShutdown.
func (agent *DCPAgent) Close() error {
routeCloseErr := agent.kvMux.Close()
agent.pollerController.Stop()
// Wait for our external looper goroutines to finish, note that if the
// specific looper wasn't used, it will be a nil value otherwise it
// will be an open channel till its closed to signal completion.
<-agent.pollerController.Done()
return routeCloseErr
}
// WaitUntilReady is used to verify that the SDK has been able to establish connections to the cluster.
// If no strategy is set then a fast fail retry strategy will be applied - only RetryReason that are set to always
// retry will be retried. This is includes for WaitUntilReady, that is the SDK will wait until connections succeed
// or report a connection error - as soon as a connection error is reported WaitUntilReady will fail and return that
// error.
// Connection time errors are also be subject to KvConfig.ServerWaitBackoff. This is the period of time that the SDK
// will wait before attempting to reconnect to a node.
func (agent *DCPAgent) WaitUntilReady(deadline time.Time, opts WaitUntilReadyOptions,
cb WaitUntilReadyCallback) (PendingOp, error) {
forceWait := true
if len(opts.ServiceTypes) == 0 {
forceWait = false
opts.ServiceTypes = []ServiceType{MemdService}
}
return agent.diagnostics.WaitUntilReady(deadline, forceWait, opts, cb)
}
// OpenStream opens a DCP stream for a particular VBucket and, optionally, filter.
func (agent *DCPAgent) OpenStream(vbID uint16, flags memd.DcpStreamAddFlag, vbUUID VbUUID, startSeqNo,
endSeqNo, snapStartSeqNo, snapEndSeqNo SeqNo, evtHandler StreamObserver, opts OpenStreamOptions,
cb OpenStreamCallback) (PendingOp, error) {
return agent.dcp.OpenStream(vbID, flags, vbUUID, startSeqNo, endSeqNo, snapStartSeqNo, snapEndSeqNo, evtHandler, opts, cb)
}
// CloseStream shuts down an open stream for the specified VBucket.
func (agent *DCPAgent) CloseStream(vbID uint16, opts CloseStreamOptions, cb CloseStreamCallback) (PendingOp, error) {
return agent.dcp.CloseStream(vbID, opts, cb)
}
// GetFailoverLog retrieves the fail-over log for a particular VBucket. This is used
// to resume an interrupted stream after a node fail-over has occurred.
func (agent *DCPAgent) GetFailoverLog(vbID uint16, cb GetFailoverLogCallback) (PendingOp, error) {
return agent.dcp.GetFailoverLog(vbID, cb)
}
// GetVbucketSeqnos returns the last checkpoint for a particular VBucket. This is useful
// for starting a DCP stream from wherever the server currently is.
func (agent *DCPAgent) GetVbucketSeqnos(serverIdx int, state memd.VbucketState, opts GetVbucketSeqnoOptions,
cb GetVBucketSeqnosCallback) (PendingOp, error) {
return agent.dcp.GetVbucketSeqnos(serverIdx, state, opts, cb)
}
// HasCollectionsSupport verifies whether or not collections are available on the agent.
func (agent *DCPAgent) HasCollectionsSupport() bool {
return agent.kvMux.SupportsCollections()
}
// ConfigSnapshot returns a snapshot of the underlying configuration currently in use.
func (agent *DCPAgent) ConfigSnapshot() (*ConfigSnapshot, error) {
return agent.kvMux.ConfigSnapshot()
}
// ForceReconnect gracefully rebuilds all connections being used by the agent.
// Any persistent in flight requests (e.g. DCP) will be terminated with ErrForcedReconnect.
//
// Internal: This should never be used and is not supported.
func (agent *DCPAgent) ForceReconnect() {
agent.connectionSettingsLock.Lock()
auth := agent.auth
mechs := agent.authMechanisms
tlsConfig := agent.tlsConfig
agent.connectionSettingsLock.Unlock()
agent.kvMux.ForceReconnect(tlsConfig, mechs, auth, true)
}
// ReconfigureSecurity updates the security configuration being used by the agent. This includes the ability to
// toggle TLS on and off.
//
// Calling this function will cause all underlying connections to be reconnected. The exception to this is the
// connection to the seed node (usually localhost), which will only be reconnected if the AuthProvider is provided
// on the options.
//
// This function can only be called when the seed poller is in use i.e. when the ns_server scheme is used.
// Internal: This should never be used and is not supported.
func (agent *DCPAgent) ReconfigureSecurity(opts ReconfigureSecurityOptions) error {
_, ok := agent.pollerController.(*seedConfigController)
if !ok {
return errors.New("reconfigure tls is only supported when the agent is in ns server mode")
}
var authProvided bool
auth := opts.Auth
mechs := opts.AuthMechanisms
agent.connectionSettingsLock.Lock()
if auth == nil {
auth = agent.auth
} else {
authProvided = true
}
if len(mechs) == 0 {
mechs = agent.authMechanisms
}
var tlsConfig *dynTLSConfig
if opts.UseTLS {
if opts.TLSRootCAProvider == nil {
return wrapError(errInvalidArgument, "must provide TLSRootCAProvider when UseTLS is true")
}
tlsConfig = createTLSConfig(auth, opts.TLSRootCAProvider)
}
agent.auth = auth
agent.authMechanisms = mechs
agent.tlsConfig = tlsConfig
agent.connectionSettingsLock.Unlock()
agent.cfgManager.UseTLS(tlsConfig != nil)
agent.kvMux.ForceReconnect(tlsConfig, mechs, auth, authProvided)
agent.httpMux.UpdateTLS(tlsConfig, auth)
return nil
}
func (agent *DCPAgent) onBootstrapFail(err error) {
// If this error is a legitimate fallback reason then we should immediately start the http poller.
if agent.pollerController != nil && agent.isPollingFallbackError(err) {
agent.pollerController.ForceHTTPPoller()
}
}
func (agent *DCPAgent) isPollingFallbackError(err error) bool {
return isPollingFallbackError(err, agent.bucketName)
}