-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathclusteragent.go
More file actions
298 lines (250 loc) · 9.33 KB
/
clusteragent.go
File metadata and controls
298 lines (250 loc) · 9.33 KB
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
package gocbcore
import (
"fmt"
"sync"
"time"
)
type clusterAgent struct {
defaultRetryStrategy RetryStrategy
connectionSettingsLock sync.Mutex
auth AuthProvider
tlsConfig *dynTLSConfig
httpMux *httpMux
tracer *tracerComponent
telemetry *telemetryComponent
http *httpComponent
diagnostics *diagnosticsComponent
n1ql *n1qlQueryComponent
analytics *analyticsQueryComponent
search *searchQueryComponent
views *viewQueryComponent
revLock sync.Mutex
revID int64
revEpoch int64
configWatchLock sync.Mutex
configWatchers []routeConfigWatcher
}
func createClusterAgent(config *clusterAgentConfig) (*clusterAgent, error) {
c := &clusterAgent{
defaultRetryStrategy: config.DefaultRetryStrategy,
}
userAgent := config.UserAgent
c.tracer = newTracerComponent(
config.TracerConfig.Tracer,
config.ObservabilityConfig.SemanticConventionOptIn,
"",
config.TracerConfig.NoRootTraceSpans,
config.MeterConfig.Meter,
c)
tlsConfig, err := setupTLSConfig(config.SeedConfig.MemdAddrs, config.SecurityConfig)
if err != nil {
return nil, err
}
c.tlsConfig = tlsConfig
// App telemetry not supported in ns_server mode
if !config.SecurityConfig.NoTLSSeedNode {
c.telemetry = newTelemetryComponent(telemetryComponentProps{
reporter: config.TelemetryConfig.TelemetryReporter,
auth: config.SecurityConfig.Auth,
tlsConfig: tlsConfig,
agent: agentName(userAgent),
cfgMgr: c,
})
}
if c.defaultRetryStrategy == nil {
c.defaultRetryStrategy = newFailFastRetryStrategy()
}
httpIdleConnTimeout := 1000 * time.Millisecond
if config.HTTPConfig.IdleConnectionTimeout > 0 {
httpIdleConnTimeout = config.HTTPConfig.IdleConnectionTimeout
}
httpConnectTimeout := 30 * time.Second
if config.HTTPConfig.ConnectTimeout > 0 {
httpConnectTimeout = config.HTTPConfig.ConnectTimeout
}
circuitBreakerConfig := config.CircuitBreakerConfig
httpEpList := routeEndpoints{}
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)
} else {
ep := routeEndpoint{
Address: fmt.Sprintf("http://%s", hostPort),
IsSeedNode: true,
}
httpEpList.NonSSLEndpoints = append(httpEpList.NonSSLEndpoints, ep)
}
}
c.httpMux = newHTTPMux(
circuitBreakerConfig,
c,
&httpClientMux{tlsConfig: tlsConfig, auth: config.SecurityConfig.Auth},
config.SecurityConfig.NoTLSSeedNode,
)
c.http = newHTTPComponent(
httpComponentProps{
UserAgent: userAgent,
DefaultRetryStrategy: c.defaultRetryStrategy,
AllowEnterpriseAnalytics: config.InternalConfig.AllowEnterpriseAnalytics,
},
httpClientProps{
maxIdleConns: config.HTTPConfig.MaxIdleConns,
maxIdleConnsPerHost: config.HTTPConfig.MaxIdleConnsPerHost,
idleTimeout: httpIdleConnTimeout,
connectTimeout: httpConnectTimeout,
},
c.httpMux,
c.tracer,
c.telemetry,
)
c.n1ql = newN1QLQueryComponent(c.http, c, c.tracer)
c.analytics = newAnalyticsQueryComponent(c.http, c.tracer)
c.search = newSearchQueryComponent(c.http, c, c.tracer)
c.views = newViewQueryComponent(c.http, c.tracer)
// diagnostics at this level will never need to hook KV. There are no persistent connections
// so Diagnostics calls should be blocked. Ping and WaitUntilReady will only try HTTP services.
c.diagnostics = newDiagnosticsComponent(nil, c.httpMux, c.http, "", c.defaultRetryStrategy, nil)
// Kick everything off.
cfg := &routeConfig{
mgmtEpList: httpEpList,
revID: -1,
}
c.httpMux.OnNewRouteConfig(cfg)
return c, nil
}
func (agent *clusterAgent) RegisterWith(cfgMgr configManager, dialer *memdClientDialerComponent) {
cfgMgr.AddConfigWatcher(agent)
dialer.AddBootstrapFailHandler(agent.diagnostics)
}
func (agent *clusterAgent) UnregisterWith(cfgMgr configManager, dialer *memdClientDialerComponent) {
cfgMgr.RemoveConfigWatcher(agent)
dialer.RemoveBootstrapFailHandler(agent.diagnostics)
}
func (agent *clusterAgent) AddConfigWatcher(watcher routeConfigWatcher) {
agent.configWatchLock.Lock()
agent.configWatchers = append(agent.configWatchers, watcher)
agent.configWatchLock.Unlock()
}
func (agent *clusterAgent) RemoveConfigWatcher(watcher routeConfigWatcher) {
var idx int
agent.configWatchLock.Lock()
for i, w := range agent.configWatchers {
if w == watcher {
idx = i
}
}
if idx == len(agent.configWatchers) {
agent.configWatchers = agent.configWatchers[:idx]
} else {
agent.configWatchers = append(agent.configWatchers[:idx], agent.configWatchers[idx+1:]...)
}
agent.configWatchLock.Unlock()
}
func (agent *clusterAgent) OnNewRouteConfig(cfg *routeConfig) {
agent.revLock.Lock()
// This could be coming from multiple agents so we need to make sure that it's up to date with what we've seen.
// We allow a config rev of -1 to be applied to halt sending http requests.
if cfg.revID > -1 && !revIsNewer(cfg.revID, cfg.revEpoch, agent.revID, agent.revEpoch) {
agent.revLock.Unlock()
return
}
logDebugf("Cluster agent applying config rev id: %d, rev epoch: %d\n", cfg.revID, cfg.revEpoch)
agent.revID = cfg.revID
agent.revEpoch = cfg.revEpoch
agent.revLock.Unlock()
agent.configWatchLock.Lock()
watchers := make([]routeConfigWatcher, len(agent.configWatchers))
copy(watchers, agent.configWatchers)
agent.configWatchLock.Unlock()
for _, watcher := range watchers {
watcher.OnNewRouteConfig(cfg)
}
}
// N1QLQuery executes a N1QL query against a random connected agent.
func (agent *clusterAgent) N1QLQuery(opts N1QLQueryOptions, cb N1QLQueryCallback) (PendingOp, error) {
return agent.n1ql.N1QLQuery(opts, cb)
}
// PreparedN1QLQuery executes a prepared N1QL query against a random connected agent.
func (agent *clusterAgent) PreparedN1QLQuery(opts N1QLQueryOptions, cb N1QLQueryCallback) (PendingOp, error) {
return agent.n1ql.PreparedN1QLQuery(opts, cb)
}
// AnalyticsQuery executes an analytics query against a random connected agent.
func (agent *clusterAgent) AnalyticsQuery(opts AnalyticsQueryOptions, cb AnalyticsQueryCallback) (PendingOp, error) {
return agent.analytics.AnalyticsQuery(opts, cb)
}
// SearchQuery executes a Search query against a random connected agent.
func (agent *clusterAgent) SearchQuery(opts SearchQueryOptions, cb SearchQueryCallback) (PendingOp, error) {
return agent.search.SearchQuery(opts, cb)
}
// ViewQuery executes a view query against a random connected agent.
func (agent *clusterAgent) ViewQuery(opts ViewQueryOptions, cb ViewQueryCallback) (PendingOp, error) {
return agent.views.ViewQuery(opts, cb)
}
// DoHTTPRequest will perform an HTTP request against one of the HTTP
// services which are available within the SDK, using a random connected agent.
func (agent *clusterAgent) DoHTTPRequest(req *HTTPRequest, cb DoHTTPRequestCallback) (PendingOp, error) {
return agent.http.DoHTTPRequest(req, cb)
}
// Ping pings all of the servers we are connected to and returns
// a report regarding the pings that were performed.
func (agent *clusterAgent) Ping(opts PingOptions, cb PingCallback) (PendingOp, error) {
for _, srv := range opts.ServiceTypes {
if srv == MemdService {
return nil, wrapError(errInvalidArgument, "memd service is not valid for use with clusterAgent")
} else if srv == CapiService {
return nil, wrapError(errInvalidArgument, "capi service is not valid for use with clusterAgent")
}
}
if len(opts.ServiceTypes) == 0 {
opts.ServiceTypes = []ServiceType{CbasService, FtsService, N1qlService, MgmtService}
opts.ignoreMissingServices = true
}
return agent.diagnostics.Ping(opts, cb)
}
// WaitUntilReady returns whether or not the Agent has seen a valid cluster config.
func (agent *clusterAgent) WaitUntilReady(deadline time.Time, opts WaitUntilReadyOptions, cb WaitUntilReadyCallback) (PendingOp, error) {
for _, srv := range opts.ServiceTypes {
if srv == MemdService {
return nil, wrapError(errInvalidArgument, "memd service is not valid for use with clusterAgent")
} else if srv == CapiService {
return nil, wrapError(errInvalidArgument, "capi service is not valid for use with clusterAgent")
}
}
forceWait := true
if len(opts.ServiceTypes) == 0 {
forceWait = false
opts.ServiceTypes = []ServiceType{CbasService, FtsService, N1qlService, MgmtService}
}
return agent.diagnostics.WaitUntilReady(deadline, forceWait, opts, cb)
}
// Close shuts down the agent, closing the underlying http client. This does not cause the agent
// to unregister itself with any configuration providers so be sure to do that first.
func (agent *clusterAgent) Close() error {
// Close the transports so that they don't hold open goroutines.
agent.http.Close()
return nil
}
func (agent *clusterAgent) ReconfigureSecurity(opts ReconfigureSecurityOptions) error {
auth := opts.Auth
agent.connectionSettingsLock.Lock()
if auth == nil {
auth = agent.auth
}
var tlsConfig *dynTLSConfig
if opts.UseTLS {
if opts.TLSRootCAProvider == nil {
return wrapError(errInvalidArgument, "must provide TLSRootCAProvider when UseTLS is true")
}
tlsConfig = createTLSConfig(auth, nil, opts.TLSRootCAProvider)
}
agent.auth = auth
agent.tlsConfig = tlsConfig
agent.connectionSettingsLock.Unlock()
agent.httpMux.UpdateTLS(tlsConfig, auth)
agent.telemetry.UpdateTLS(tlsConfig, auth)
return nil
}