/
dcpagent.go
372 lines (319 loc) · 10.4 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
package gocbcore
import (
"crypto/tls"
"fmt"
"time"
"github.com/couchbase/gocbcore/v9/memd"
)
// DCPAgent represents the base client handling DCP connections to a Couchbase Server.
type DCPAgent struct {
clientID string
bucketName string
tlsConfig *dynTLSConfig
initFn memdInitFunc
pollerController *pollerController
kvMux *kvMux
httpMux *httpMux
cfgManager *configManagementComponent
errMap *errMapComponent
tracer *tracerComponent
diagnostics *diagnosticsComponent
dcp *dcpComponent
http *httpComponent
}
// CreateDcpAgent creates an agent for performing DCP operations.
func CreateDcpAgent(config *DCPAgentConfig, dcpStreamName string, openFlags memd.DcpOpenFlag) (*DCPAgent, error) {
// 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
}
var priority string
switch config.AgentPriority {
case DcpAgentPriorityLow:
priority = "low"
case DcpAgentPriorityMed:
priority = "medium"
case DcpAgentPriorityHigh:
priority = "high"
}
if err := sclient.ExecDcpControl("set_priority", priority, deadline); err != nil {
return err
}
if config.UseExpiryOpcode {
if err := sclient.ExecDcpControl("enable_expiry_opcode", "true", deadline); err != nil {
return err
}
}
if config.UseStreamID {
if err := sclient.ExecDcpControl("enable_stream_id", "true", deadline); err != nil {
return err
}
}
if config.UseOSOBackfill {
if err := sclient.ExecDcpControl("enable_out_of_order_snapshots", "true", deadline); err != nil {
return err
}
}
// 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').
var backfillOrder string
switch config.BackfillOrder {
case DCPBackfillOrderRoundRobin:
backfillOrder = "round-robin"
case DCPBackfillOrderSequential:
backfillOrder = "sequential"
}
if backfillOrder != "" {
if err := sclient.ExecDcpControl("backfill_order", backfillOrder, deadline); err != nil {
return err
}
}
if err := sclient.ExecEnableDcpClientEnd(deadline); err != nil {
return err
}
return sclient.ExecEnableDcpBufferAck(8*1024*1024, deadline)
}
return createDCPAgent(config, initFn)
}
func createDCPAgent(config *DCPAgentConfig, initFn memdInitFunc) (*DCPAgent, error) {
logInfof("SDK Version: gocbcore/%s", goCbCoreVersionStr)
logInfof("Creating new dcp agent: %+v", config)
var tlsConfig *dynTLSConfig
if config.UseTLS {
tlsConfig = &dynTLSConfig{
BaseConfig: &tls.Config{
GetClientCertificate: func(info *tls.CertificateRequestInfo) (*tls.Certificate, error) {
cert, err := config.Auth.Certificate(AuthCertRequest{})
if err != nil {
return nil, err
}
if cert == nil {
return &tls.Certificate{}, nil
}
return cert, nil
},
},
Provider: config.TLSRootCAProvider,
}
}
httpCli := createHTTPClient(config.HTTPMaxIdleConns, config.HTTPMaxIdleConnsPerHost,
config.HTTPIdleConnectionTimeout, tlsConfig)
tracerCmpt := newTracerComponent(noopTracer{}, config.BucketName, false)
c := &DCPAgent{
clientID: formatCbUID(randomCbUID()),
bucketName: config.BucketName,
tlsConfig: tlsConfig,
initFn: initFn,
tracer: tracerCmpt,
errMap: newErrMapManager(config.BucketName),
}
circuitBreakerConfig := CircuitBreakerConfig{
Enabled: false,
}
auth := config.Auth
userAgent := config.UserAgent
disableDecompression := config.DisableDecompression
useCompression := config.UseCompression
useCollections := config.UseCollections
compressionMinSize := 32
compressionMinRatio := 0.83
kvConnectTimeout := 7000 * time.Millisecond
if config.KVConnectTimeout > 0 {
kvConnectTimeout = config.KVConnectTimeout
}
serverWaitTimeout := 5 * time.Second
kvPoolSize := 1
if config.KvPoolSize > 0 {
kvPoolSize = config.KvPoolSize
}
maxQueueSize := 2048
if config.MaxQueueSize > 0 {
maxQueueSize = config.MaxQueueSize
}
confCccpMaxWait := 3 * time.Second
if config.CccpMaxWait > 0 {
confCccpMaxWait = config.CccpMaxWait
}
confCccpPollPeriod := 2500 * time.Millisecond
if config.CccpPollPeriod > 0 {
confCccpPollPeriod = config.CccpPollPeriod
}
confHTTPRetryDelay := 10 * time.Second
if config.HTTPRetryDelay > 0 {
confHTTPRetryDelay = config.HTTPRetryDelay
}
confHTTPRedialPeriod := 10 * time.Second
if config.HTTPRedialPeriod > 0 {
confHTTPRedialPeriod = config.HTTPRedialPeriod
}
if config.CompressionMinSize > 0 {
compressionMinSize = config.CompressionMinSize
}
if config.CompressionMinRatio > 0 {
compressionMinRatio = config.CompressionMinRatio
if compressionMinRatio >= 1.0 {
compressionMinRatio = 1.0
}
}
authMechanisms := []AuthMechanism{
ScramSha512AuthMechanism,
ScramSha256AuthMechanism,
ScramSha1AuthMechanism}
// PLAIN authentication is only supported over TLS
if config.UseTLS {
authMechanisms = append(authMechanisms, PlainAuthMechanism)
}
authHandler := buildAuthHandler(auth)
var httpEpList []string
for _, hostPort := range config.HTTPAddrs {
if !c.IsSecure() {
httpEpList = append(httpEpList, fmt.Sprintf("http://%s", hostPort))
} else {
httpEpList = append(httpEpList, fmt.Sprintf("https://%s", hostPort))
}
}
c.cfgManager = newConfigManager(
configManagerProperties{
NetworkType: config.NetworkType,
UseSSL: config.UseTLS,
SrcMemdAddrs: config.MemdAddrs,
SrcHTTPAddrs: []string{},
},
)
dialer := newMemdClientDialerComponent(
memdClientDialerProps{
ServerWaitTimeout: serverWaitTimeout,
KVConnectTimeout: kvConnectTimeout,
ClientID: c.clientID,
TLSConfig: c.tlsConfig,
CompressionMinSize: compressionMinSize,
CompressionMinRatio: compressionMinRatio,
DisableDecompression: disableDecompression,
},
bootstrapProps{
HelloProps: helloProps{
CollectionsEnabled: useCollections,
CompressionEnabled: useCompression,
},
Bucket: c.bucketName,
UserAgent: userAgent,
AuthMechanisms: authMechanisms,
AuthHandler: authHandler,
ErrMapManager: c.errMap,
},
circuitBreakerConfig,
nil,
c.tracer,
initFn,
)
c.kvMux = newKVMux(
kvMuxProps{
QueueSize: maxQueueSize,
PoolSize: kvPoolSize,
CollectionsEnabled: useCollections,
},
c.cfgManager,
c.errMap,
c.tracer,
dialer,
)
c.httpMux = newHTTPMux(circuitBreakerConfig, c.cfgManager)
c.http = newHTTPComponent(
httpComponentProps{
UserAgent: userAgent,
DefaultRetryStrategy: &failFastRetryStrategy{},
},
httpCli,
c.httpMux,
auth,
c.tracer,
)
c.pollerController = newPollerController(
newCCCPConfigController(
cccpPollerProperties{
confCccpMaxWait: confCccpMaxWait,
confCccpPollPeriod: confCccpPollPeriod,
},
c.kvMux,
c.cfgManager,
),
newHTTPConfigController(
c.bucketName,
httpPollerProperties{
httpComponent: c.http,
confHTTPRetryDelay: confHTTPRetryDelay,
confHTTPRedialPeriod: confHTTPRedialPeriod,
},
c.httpMux,
c.cfgManager,
),
c.cfgManager,
)
c.diagnostics = newDiagnosticsComponent(c.kvMux, nil, nil, c.bucketName, newFailFastRetryStrategy(), c.pollerController)
c.dcp = newDcpComponent(c.kvMux, config.UseStreamID)
// Kick everything off.
cfg := &routeConfig{
kvServerList: config.MemdAddrs,
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.tlsConfig != nil
}
// 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 returns whether or not the Agent has seen a valid cluster config.
func (agent *DCPAgent) WaitUntilReady(deadline time.Time, opts WaitUntilReadyOptions,
cb WaitUntilReadyCallback) (PendingOp, error) {
return agent.diagnostics.WaitUntilReady(deadline, 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()
}