-
Notifications
You must be signed in to change notification settings - Fork 21
/
memdbootstrap_client.go
391 lines (343 loc) · 9.44 KB
/
memdbootstrap_client.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
package gocbcore
import (
"encoding/binary"
"errors"
"strings"
"time"
"github.com/couchbase/gocbcore/v10/memd"
)
type bootstrapableClient interface {
SendRequest(*memdQRequest) error
Address() string
ConnID() string
SupportsFeature(feature memd.HelloFeature) bool
Features([]memd.HelloFeature)
loggerID() string
}
type bootstrapClient interface {
Address() string
ConnID() string
Features(features []memd.HelloFeature)
SupportsFeature(feature memd.HelloFeature) bool
SaslAuth(k, v []byte, deadline time.Time, cb func(b []byte, err error)) error
SaslStep(k, v []byte, deadline time.Time, cb func(err error)) error
ExecSelectBucket(b []byte, deadline time.Time) (chan error, error)
ExecGetErrorMap(version uint16, deadline time.Time) (chan errorMapResponse, error)
SaslListMechs(deadline time.Time, cb func(mechs []AuthMechanism, err error)) error
ExecHello(clientID string, features []memd.HelloFeature, deadline time.Time) (chan ExecHelloResponse, error)
ExecGetConfig(deadline time.Time) (chan getConfigResponse, error)
LoggerID() string
}
// Due to AuthProvider we are currently tied to bootstrapping passing around a deadline and the bootstrap
// "owner" has to hold onto a cancel sig for use at request time.
// In the future we can combine deadline and cancellation into a context.Context and pass that everywhere as a parameter,
// we will then able to expose utility functions to allow user to build their own bootstrap from existing building
// blocks.
func newMemdBootstrapClient(client bootstrapableClient, cancelSig <-chan struct{}) *memdBootstrapClient {
return &memdBootstrapClient{
cancelSig: cancelSig,
client: client,
}
}
type memdBootstrapClient struct {
client bootstrapableClient
cancelSig <-chan struct{}
}
func (bc *memdBootstrapClient) Address() string {
return bc.client.Address()
}
func (bc *memdBootstrapClient) ConnID() string {
return bc.client.ConnID()
}
func (bc *memdBootstrapClient) Features(features []memd.HelloFeature) {
bc.client.Features(features)
}
func (bc *memdBootstrapClient) SupportsFeature(feature memd.HelloFeature) bool {
return bc.client.SupportsFeature(feature)
}
func (client *memdBootstrapClient) LoggerID() string {
return client.client.loggerID()
}
func (bc *memdBootstrapClient) SaslAuth(k, v []byte, deadline time.Time, cb func(b []byte, err error)) error {
err := bc.doBootstrapRequest(
&memdQRequest{
Packet: memd.Packet{
Magic: memd.CmdMagicReq,
Command: memd.CmdSASLAuth,
Key: k,
Value: v,
},
Callback: func(resp *memdQResponse, _ *memdQRequest, err error) {
// Auth is special, auth continue is surfaced as an error
var val []byte
if resp != nil {
val = resp.Value
}
cb(val, err)
},
RetryStrategy: newFailFastRetryStrategy(),
},
deadline,
)
if err != nil {
return err
}
return nil
}
func (bc *memdBootstrapClient) SaslStep(k, v []byte, deadline time.Time, cb func(err error)) error {
err := bc.doBootstrapRequest(
&memdQRequest{
Packet: memd.Packet{
Magic: memd.CmdMagicReq,
Command: memd.CmdSASLStep,
Key: k,
Value: v,
},
Callback: func(resp *memdQResponse, _ *memdQRequest, err error) {
if err != nil {
cb(err)
return
}
cb(nil)
},
RetryStrategy: newFailFastRetryStrategy(),
},
deadline,
)
if err != nil {
return err
}
return nil
}
func (bc *memdBootstrapClient) ExecSelectBucket(b []byte, deadline time.Time) (chan error, error) {
completedCh := make(chan error, 1)
err := bc.doBootstrapRequest(
&memdQRequest{
Packet: memd.Packet{
Magic: memd.CmdMagicReq,
Command: memd.CmdSelectBucket,
Key: b,
},
Callback: func(resp *memdQResponse, _ *memdQRequest, err error) {
if err != nil {
if errors.Is(err, ErrDocumentNotFound) {
// Bucket not found means that the user has privileges to access the bucket but that the bucket
// is in some way not existing right now (e.g. in warmup).
err = errBucketNotFound
}
completedCh <- err
return
}
completedCh <- nil
},
RetryStrategy: newFailFastRetryStrategy(),
},
deadline,
)
if err != nil {
return nil, err
}
return completedCh, nil
}
type errorMapResponse struct {
Err error
Bytes []byte
}
func (bc *memdBootstrapClient) ExecGetErrorMap(version uint16, deadline time.Time) (chan errorMapResponse, error) {
completedCh := make(chan errorMapResponse, 1)
valueBuf := make([]byte, 2)
binary.BigEndian.PutUint16(valueBuf, version)
err := bc.doBootstrapRequest(
&memdQRequest{
Packet: memd.Packet{
Magic: memd.CmdMagicReq,
Command: memd.CmdGetErrorMap,
Value: valueBuf,
},
Callback: func(resp *memdQResponse, _ *memdQRequest, err error) {
if err != nil {
completedCh <- errorMapResponse{
Err: err,
}
return
}
completedCh <- errorMapResponse{
Bytes: resp.Value,
}
},
RetryStrategy: newFailFastRetryStrategy(),
},
deadline,
)
if err != nil {
return nil, err
}
return completedCh, nil
}
func (bc *memdBootstrapClient) SaslListMechs(deadline time.Time, cb func(mechs []AuthMechanism, err error)) error {
err := bc.doBootstrapRequest(
&memdQRequest{
Packet: memd.Packet{
Magic: memd.CmdMagicReq,
Command: memd.CmdSASLListMechs,
},
Callback: func(resp *memdQResponse, _ *memdQRequest, err error) {
if err != nil {
cb(nil, err)
return
}
mechs := strings.Split(string(resp.Value), " ")
var authMechs []AuthMechanism
for _, mech := range mechs {
authMechs = append(authMechs, AuthMechanism(mech))
}
cb(authMechs, nil)
},
RetryStrategy: newFailFastRetryStrategy(),
},
deadline,
)
if err != nil {
return err
}
return nil
}
// ExecHelloResponse contains the features and/or error from an ExecHello operation.
type ExecHelloResponse struct {
SrvFeatures []memd.HelloFeature
Err error
}
func (bc *memdBootstrapClient) ExecHello(clientID string, features []memd.HelloFeature, deadline time.Time) (chan ExecHelloResponse, error) {
appendFeatureCode := func(bytes []byte, feature memd.HelloFeature) []byte {
bytes = append(bytes, 0, 0)
binary.BigEndian.PutUint16(bytes[len(bytes)-2:], uint16(feature))
return bytes
}
var featureBytes []byte
for _, feature := range features {
featureBytes = appendFeatureCode(featureBytes, feature)
}
completedCh := make(chan ExecHelloResponse, 1)
err := bc.doBootstrapRequest(
&memdQRequest{
Packet: memd.Packet{
Magic: memd.CmdMagicReq,
Command: memd.CmdHello,
Key: []byte(clientID),
Value: featureBytes,
},
Callback: func(resp *memdQResponse, _ *memdQRequest, err error) {
if err != nil {
completedCh <- ExecHelloResponse{
Err: err,
}
return
}
var srvFeatures []memd.HelloFeature
for i := 0; i < len(resp.Value); i += 2 {
feature := binary.BigEndian.Uint16(resp.Value[i:])
srvFeatures = append(srvFeatures, memd.HelloFeature(feature))
}
completedCh <- ExecHelloResponse{
SrvFeatures: srvFeatures,
}
},
RetryStrategy: newFailFastRetryStrategy(),
},
deadline,
)
if err != nil {
return nil, err
}
return completedCh, nil
}
type getConfigResponse struct {
Err error
Config *cfgBucket
}
func (bc *memdBootstrapClient) ExecGetConfig(deadline time.Time) (chan getConfigResponse, error) {
completedCh := make(chan getConfigResponse, 1)
// Note that the revid/revepoch do not matter here, we only send GetConfig on bootstrap if we haven't actually
// seen a config yet.
err := bc.doBootstrapRequest(
&memdQRequest{
Packet: memd.Packet{
Magic: memd.CmdMagicReq,
Command: memd.CmdGetClusterConfig,
},
Callback: func(resp *memdQResponse, _ *memdQRequest, err error) {
if err != nil {
completedCh <- getConfigResponse{
Err: err,
}
return
}
hostName, err := hostFromHostPort(bc.Address())
if err != nil {
logWarnf("Boostrap client: Failed to parse source address. %s", err)
completedCh <- getConfigResponse{
Err: err,
}
return
}
bk, err := parseConfig(resp.Value, hostName)
if err != nil {
logWarnf("Boostrap client: Failed to parse CCCP config. %v", err)
completedCh <- getConfigResponse{
Err: err,
}
return
}
completedCh <- getConfigResponse{
Config: bk,
}
},
RetryStrategy: newFailFastRetryStrategy(),
},
deadline,
)
if err != nil {
return nil, err
}
return completedCh, nil
}
func (bc *memdBootstrapClient) doBootstrapRequest(req *memdQRequest, deadline time.Time) error {
origCb := req.Callback
doneCh := make(chan struct{})
handler := func(resp *memdQResponse, req *memdQRequest, err error) {
close(doneCh)
origCb(resp, req, err)
}
req.Callback = handler
err := bc.client.SendRequest(req)
if err != nil {
return err
}
start := time.Now()
req.SetTimer(time.AfterFunc(deadline.Sub(start), func() {
connInfo := req.ConnectionInfo()
count, reasons := req.Retries()
req.cancelWithCallback(&TimeoutError{
InnerError: errAmbiguousTimeout,
OperationID: req.Command.Name(),
Opaque: req.Identifier(),
TimeObserved: time.Since(start),
RetryReasons: reasons,
RetryAttempts: count,
LastDispatchedTo: connInfo.lastDispatchedTo,
LastDispatchedFrom: connInfo.lastDispatchedFrom,
LastConnectionID: connInfo.lastConnectionID,
})
}))
go func() {
select {
case <-doneCh:
return
case <-bc.cancelSig:
req.Cancel()
<-doneCh
return
}
}()
return nil
}