-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcolumnaragent.go
318 lines (264 loc) · 8.06 KB
/
columnaragent.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
package gocbcore
import (
"context"
"errors"
"time"
)
// ColumnarAgent represents the base client handling connections to the Couchbase Columnar service.
// This is used internally by the higher level classes for communicating with the cluster,
// it can also be used to perform more advanced operations with a cluster.
// Internal: This should never be used and is not supported.
type ColumnarAgent struct {
clientID string
poller *cccpConfigController
kvMux *kvMux
httpMux *columnarMux
dialer *memdClientDialerComponent
cfgManager *configManagementComponent
errMap *errMapComponent
columnar *columnarComponent
auth AuthProvider
tlsConfig *dynTLSConfig
// not really necessary but without this various things will end up panicking
tracer *tracerComponent
srvDetails *srvDetails
shutdownSig chan struct{}
}
func CreateColumnarAgent(config *ColumnarAgentConfig) (*ColumnarAgent, error) {
logInfof("SDK Version: gocbcore/%s", goCbCoreVersionStr)
logInfof("Creating new columnar agent: %+v", config)
if config.SecurityConfig.TLSRootCAProvider == nil {
return nil, wrapError(ErrInvalidArgument, "tlsRootCAProvider cannot be nil")
}
tlsConfig := createTLSConfig(config.SecurityConfig.Auth, config.SecurityConfig.CipherSuite, config.SecurityConfig.TLSRootCAProvider)
c := &ColumnarAgent{
clientID: formatCbUID(randomCbUID()),
errMap: newErrMapManager(""),
auth: config.SecurityConfig.Auth,
tlsConfig: tlsConfig,
shutdownSig: make(chan struct{}),
}
serverWaitTimeout := 5 * time.Second
if config.KVConfig.ServerWaitBackoff > 0 {
serverWaitTimeout = config.KVConfig.ServerWaitBackoff
}
httpIdleConnTimeout := 1000 * time.Millisecond
if config.HTTPConfig.IdleConnectionTimeout > 0 {
httpIdleConnTimeout = config.HTTPConfig.IdleConnectionTimeout
}
connectTimeout := 10 * time.Second
if config.ConnectTimeout > 0 {
connectTimeout = config.ConnectTimeout
}
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
}
userAgent := config.UserAgent
kvPoolSize := 1
maxQueueSize := 2048
kvBufferSize := uint(1024)
kvServerList := routeEndpoints{}
var srcMemdAddrs []routeEndpoint
for _, seed := range config.SeedConfig.MemdAddrs {
kvServerList.SSLEndpoints = append(kvServerList.SSLEndpoints, routeEndpoint{
Address: seed,
IsSeedNode: true,
})
srcMemdAddrs = kvServerList.SSLEndpoints
}
if config.SeedConfig.SRVRecord != nil {
c.srvDetails = &srvDetails{
Addrs: kvServerList,
Record: *config.SeedConfig.SRVRecord,
}
}
c.cfgManager = newConfigManager(
configManagerProperties{
NetworkType: "",
SrcMemdAddrs: srcMemdAddrs,
SrcHTTPAddrs: []routeEndpoint{},
UseTLS: true,
SeedNodeAddr: "",
},
)
c.tracer = &tracerComponent{
tracer: noopTracer{},
}
c.dialer = newMemdClientDialerComponent(
memdClientDialerProps{
ServerWaitTimeout: serverWaitTimeout,
KVConnectTimeout: connectTimeout,
ClientID: c.clientID,
CompressionMinSize: 0,
CompressionMinRatio: 0,
DisableDecompression: false,
NoTLSSeedNode: false,
ConnBufSize: kvBufferSize,
},
bootstrapProps{
HelloProps: helloProps{
CollectionsEnabled: true,
MutationTokensEnabled: false,
CompressionEnabled: false,
DurationsEnabled: false,
OutOfOrderEnabled: false,
JSONFeatureEnabled: false,
XErrorFeatureEnabled: false,
SyncReplicationEnabled: false,
PITRFeatureEnabled: false,
ResourceUnitsEnabled: false,
ClusterMapNotificationsEnabled: true,
},
Bucket: "",
UserAgent: userAgent,
ErrMapManager: c.errMap,
},
CircuitBreakerConfig{Enabled: false},
nil,
c.tracer,
c.cfgManager,
)
c.kvMux = newKVMux(
kvMuxProps{
QueueSize: maxQueueSize,
PoolSize: kvPoolSize,
CollectionsEnabled: true,
NoTLSSeedNode: false,
},
c.cfgManager,
c.errMap,
c.tracer,
nil,
c.dialer,
&kvMuxState{
tlsConfig: tlsConfig,
authMechanisms: []AuthMechanism{PlainAuthMechanism},
auth: config.SecurityConfig.Auth,
expectedBucketName: "",
},
)
c.httpMux = newColumnarMux(c.cfgManager, &columnarClientMux{
tlsConfig: tlsConfig,
auth: config.SecurityConfig.Auth,
})
c.columnar = newColumnarComponent(columnarComponentProps{
UserAgent: userAgent,
}, columnarHTTPClientProps{
MaxIdleConns: config.HTTPConfig.MaxIdleConns,
MaxIdleConnsPerHost: config.HTTPConfig.MaxIdleConnsPerHost,
IdleTimeout: httpIdleConnTimeout,
ConnectTimeout: connectTimeout,
MaxConnsPerHost: config.HTTPConfig.MaxConnsPerHost,
}, c.httpMux)
cccpFetcher := newCCCPConfigFetcher(confCccpMaxWait)
c.poller = newCCCPConfigController(
cccpPollerProperties{
confCccpPollPeriod: confCccpPollPeriod,
cccpConfigFetcher: cccpFetcher,
},
c.kvMux,
c.cfgManager,
nil,
c.onCCCPNoConfigFromAnyNode,
)
c.cfgManager.SetConfigFetcher(cccpFetcher)
c.cfgManager.AddConfigWatcher(c.dialer)
// Kick everything off.
cfg := &routeConfig{
kvServerList: kvServerList,
revID: -1,
}
c.httpMux.OnNewRouteConfig(cfg)
c.kvMux.OnNewRouteConfig(cfg)
go func() {
lastReset := time.Now()
resetRetriesAfter := 30 * time.Second
backoff := ExponentialBackoff(1*time.Millisecond, 1*time.Second, 2)
var retries uint32
for {
err := c.poller.DoLoop()
if err == nil {
return
}
if time.Now().Sub(lastReset) > resetRetriesAfter {
retries = 0
lastReset = time.Now()
}
if errors.Is(err, errNoCCCPHosts) {
logInfof("poller exited with no hosts, attempting srv refresh: %v", err)
c.attemptSRVRefresh()
} else {
logInfof("poller exited with error: %v", err)
}
dura := backoff(retries)
retries++
select {
case <-c.shutdownSig:
return
case <-time.After(dura):
}
}
}()
return c, nil
}
func (agent *ColumnarAgent) Query(ctx context.Context, opts ColumnarQueryOptions) (*ColumnarRowReader, error) {
return agent.columnar.Query(ctx, opts)
}
// Close shuts down the agent, disconnecting from all servers and failing
// any outstanding operations with ErrShutdown.
func (agent *ColumnarAgent) Close() error {
logInfof("Columnar agent closing")
poller := agent.poller
if poller != nil {
poller.Stop()
}
routeCloseErr := agent.kvMux.Close()
agent.cfgManager.Close()
// Close the transports so that they don't hold open goroutines.
agent.columnar.Close()
close(agent.shutdownSig)
logInfof("Columnar agent close complete")
return routeCloseErr
}
// Bits to support onCCCPNoConfigFromAnyNode.
// IsSecure returns whether this client is connected via SSL.
func (agent *ColumnarAgent) IsSecure() bool {
return true
}
func (agent *ColumnarAgent) onCCCPNoConfigFromAnyNode(err error) {
agent.columnar.SetBootstrapError(err)
onCCCPNoConfigFromAnyNode(agent, err)
}
func (agent *ColumnarAgent) attemptSRVRefresh() {
srvDetails := agent.srv()
if srvDetails == nil {
logInfof("no srv record in use, aborting srv refresh")
return
}
attemptSRVRefresh(agent, srvDetails)
}
func (agent *ColumnarAgent) srv() *srvDetails {
return agent.srvDetails
}
func (agent *ColumnarAgent) setSRVAddrs(addrs routeEndpoints) {
agent.srvDetails.Addrs = addrs
}
func (agent *ColumnarAgent) routeConfigWatchers() []routeConfigWatcher {
return agent.cfgManager.Watchers()
}
func (agent *ColumnarAgent) stopped() <-chan struct{} {
return agent.shutdownSig
}
func (agent *ColumnarAgent) resetConfig() {
// Reset the config manager to accept the next config that the poller fetches.
// This is safe to do here, we're blocking the poller from fetching a config and if we're here then
// we can't be performing ops.
agent.cfgManager.ResetConfig()
// Reset the dialer so that the next connections to bootstrap fetch a config and kick off the poller again.
agent.dialer.ResetConfig()
}