forked from couchbase/gocbcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemdclient.go
1229 lines (1046 loc) · 33.9 KB
/
memdclient.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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package gocbcore
import (
"encoding/binary"
"errors"
"io"
"strings"
"sync"
"sync/atomic"
"time"
"unsafe"
"github.com/couchbase/gocbcore/v10/memd"
"github.com/golang/snappy"
)
func isCompressibleOp(command memd.CmdCode) bool {
switch command {
case memd.CmdSet:
fallthrough
case memd.CmdAdd:
fallthrough
case memd.CmdReplace:
fallthrough
case memd.CmdAppend:
fallthrough
case memd.CmdPrepend:
return true
}
return false
}
type postCompleteErrorHandler func(resp *memdQResponse, req *memdQRequest, err error) (bool, error)
type memdClient struct {
lastActivity int64
dcpAckSize int
dcpFlowRecv int
closeNotify chan bool
connReleaseNotify chan struct{}
connReleasedNotify chan struct{}
connID string
closed bool
conn memdConn
opList *memdOpMap
features []memd.HelloFeature
lock sync.Mutex
streamEndNotSupported bool
breaker circuitBreaker
postErrHandler postCompleteErrorHandler
tracer *tracerComponent
zombieLogger *zombieLoggerComponent
dcpQueueSize int
// When a close request comes in, we need to immediately stop processing all requests. This
// includes immediately stopping the DCP queue rather than waiting for the application to
// flush that queue. This means that we lose packets that were read but not processed, but
// this is not fundamentally different to if we had just not read them at all. As a side
// effect of this, we need to use a separate kill signal on top of closing the queue.
// We need this to be owned by the client because we only use it when the client is closed,
// when the connection is closed from an external actor (e.g. server) we want to flush the queue.
shutdownDCP uint32
compressionMinSize int
compressionMinRatio float64
disableDecompression bool
cancelBootstrapSig <-chan struct{}
gracefulCloseTriggered uint32
}
type dcpBuffer struct {
resp *memdQResponse
packetLen int
isInternal bool
}
type memdClientProps struct {
ClientID string
DCPQueueSize int
CompressionMinSize int
CompressionMinRatio float64
DisableDecompression bool
}
func newMemdClient(props memdClientProps, conn memdConn, breakerCfg CircuitBreakerConfig, postErrHandler postCompleteErrorHandler,
tracer *tracerComponent, zombieLogger *zombieLoggerComponent) *memdClient {
client := memdClient{
closeNotify: make(chan bool),
connReleaseNotify: make(chan struct{}),
connReleasedNotify: make(chan struct{}),
connID: props.ClientID + "/" + formatCbUID(randomCbUID()),
postErrHandler: postErrHandler,
tracer: tracer,
zombieLogger: zombieLogger,
conn: conn,
opList: newMemdOpMap(),
dcpQueueSize: props.DCPQueueSize,
compressionMinRatio: props.CompressionMinRatio,
compressionMinSize: props.CompressionMinSize,
disableDecompression: props.DisableDecompression,
}
if breakerCfg.Enabled {
client.breaker = newLazyCircuitBreaker(breakerCfg, client.sendCanary)
} else {
client.breaker = newNoopCircuitBreaker()
}
client.run()
return &client
}
func (client *memdClient) SupportsFeature(feature memd.HelloFeature) bool {
return checkSupportsFeature(client.features, feature)
}
func (client *memdClient) EnableDcpBufferAck(bufferAckSize int) {
client.dcpAckSize = bufferAckSize
}
func (client *memdClient) maybeSendDcpBufferAck(packetLen int) {
client.dcpFlowRecv += packetLen
if client.dcpFlowRecv < client.dcpAckSize {
return
}
ackAmt := client.dcpFlowRecv
extrasBuf := make([]byte, 4)
binary.BigEndian.PutUint32(extrasBuf, uint32(ackAmt))
err := client.conn.WritePacket(&memd.Packet{
Magic: memd.CmdMagicReq,
Command: memd.CmdDcpBufferAck,
Extras: extrasBuf,
})
if err != nil {
logWarnf("Failed to dispatch DCP buffer ack: %s", err)
}
client.dcpFlowRecv -= ackAmt
}
func (client *memdClient) Address() string {
return client.conn.RemoteAddr()
}
func (client *memdClient) CloseNotify() chan bool {
return client.closeNotify
}
func (client *memdClient) takeRequestOwnership(req *memdQRequest) error {
client.lock.Lock()
defer client.lock.Unlock()
if client.closed {
logDebugf("Attempted to put dispatched op OP=0x%x, Opaque=%d in drained opmap", req.Command, req.Opaque)
return errMemdClientClosed
}
if atomic.LoadUint32(&client.gracefulCloseTriggered) == 1 {
logDebugf("Attempted to dispatch op OP=0x%x, Opaque=%d from gracefully closing memdclient", req.Command, req.Opaque)
return errMemdClientClosed
}
if !atomic.CompareAndSwapPointer(&req.waitingIn, nil, unsafe.Pointer(client)) {
logDebugf("Attempted to put dispatched op OP=0x%x, Opaque=%d in new opmap", req.Command, req.Opaque)
return errRequestAlreadyDispatched
}
if req.isCancelled() {
atomic.CompareAndSwapPointer(&req.waitingIn, unsafe.Pointer(client), nil)
return errRequestCanceled
}
connInfo := memdQRequestConnInfo{
lastDispatchedTo: client.Address(),
lastDispatchedFrom: client.conn.LocalAddr(),
lastConnectionID: client.connID,
}
req.SetConnectionInfo(connInfo)
client.opList.Add(req)
return nil
}
func (client *memdClient) CancelRequest(req *memdQRequest, err error) bool {
client.lock.Lock()
defer client.lock.Unlock()
if client.closed {
logDebugf("Attempted to remove op OP=0x%x, Opaque=%d from drained opmap", req.Command, req.Opaque)
return false
}
removed := client.opList.Remove(req)
if removed {
atomic.CompareAndSwapPointer(&req.waitingIn, unsafe.Pointer(client), nil)
}
if client.breaker.CompletionCallback(err) {
client.breaker.MarkSuccessful()
} else {
client.breaker.MarkFailure()
}
return removed
}
func (client *memdClient) SendRequest(req *memdQRequest) error {
if !client.breaker.AllowsRequest() {
logSchedf("Circuit breaker interrupting request. %s to %s OP=0x%x. Opaque=%d", client.conn.LocalAddr(), client.Address(), req.Command, req.Opaque)
req.cancelWithCallback(errCircuitBreakerOpen)
return nil
}
return client.internalSendRequest(req)
}
func (client *memdClient) internalSendRequest(req *memdQRequest) error {
if err := client.takeRequestOwnership(req); err != nil {
return err
}
packet := &req.Packet
if client.SupportsFeature(memd.FeatureSnappy) {
isCompressed := (packet.Datatype & uint8(memd.DatatypeFlagCompressed)) != 0
packetSize := len(packet.Value)
if !isCompressed && packetSize > client.compressionMinSize && isCompressibleOp(packet.Command) {
compressedValue := snappy.Encode(nil, packet.Value)
if float64(len(compressedValue))/float64(packetSize) <= client.compressionMinRatio {
newPacket := *packet
newPacket.Value = compressedValue
newPacket.Datatype = newPacket.Datatype | uint8(memd.DatatypeFlagCompressed)
packet = &newPacket
}
}
}
logSchedf("Writing request. %s to %s OP=0x%x. Opaque=%d", client.conn.LocalAddr(), client.Address(), req.Command, req.Opaque)
client.tracer.StartNetTrace(req)
err := client.conn.WritePacket(packet)
if err != nil {
logDebugf("memdClient write failure: %v", err)
return err
}
return nil
}
func (client *memdClient) resolveRequest(resp *memdQResponse) {
defer memd.ReleasePacket(resp.Packet)
logSchedf("Handling response data. OP=0x%x. Opaque=%d. Status:%d", resp.Command, resp.Opaque, resp.Status)
client.lock.Lock()
// Find the request that goes with this response, don't check if the client is
// closed so that we can handle orphaned responses.
req := client.opList.FindAndMaybeRemove(resp.Opaque, resp.Status != memd.StatusSuccess)
client.lock.Unlock()
if atomic.LoadUint32(&client.gracefulCloseTriggered) == 1 {
client.lock.Lock()
size := client.opList.Size()
client.lock.Unlock()
if size == 0 {
// Let's make sure that we don't somehow slow down returning to the user here.
go func() {
// We use the Close function rather than closeConn to ensure that we don't try to close the
// connection/client if someone else has already closed it.
err := client.Close()
if err != nil {
logDebugf("failed to shutdown memdclient during graceful close: %s", err)
}
}()
}
}
if req == nil {
// There is no known request that goes with this response. Ignore it.
logDebugf("Received response with no corresponding request.")
if client.zombieLogger != nil {
client.zombieLogger.RecordZombieResponse(resp, client.connID, client.LocalAddress(), client.Address())
}
return
}
if !req.Persistent || resp.Status != memd.StatusSuccess {
atomic.CompareAndSwapPointer(&req.waitingIn, unsafe.Pointer(client), nil)
}
req.processingLock.Lock()
if !req.Persistent {
stopNetTrace(req, resp, client.conn.LocalAddr(), client.conn.RemoteAddr())
}
isCompressed := (resp.Datatype & uint8(memd.DatatypeFlagCompressed)) != 0
if isCompressed && !client.disableDecompression {
newValue, err := snappy.Decode(nil, resp.Value)
if err != nil {
req.processingLock.Unlock()
logDebugf("Failed to decompress value from the server for key `%s`.", req.Key)
return
}
resp.Value = newValue
resp.Datatype = resp.Datatype & ^uint8(memd.DatatypeFlagCompressed)
}
// Give the agent an opportunity to intercept the response first
var err error
if resp.Magic == memd.CmdMagicRes && resp.Status != memd.StatusSuccess {
err = getKvStatusCodeError(resp.Status)
}
if client.breaker.CompletionCallback(err) {
client.breaker.MarkSuccessful()
} else {
client.breaker.MarkFailure()
}
if !req.Persistent {
stopCmdTrace(req)
}
req.processingLock.Unlock()
if err != nil {
shortCircuited, routeErr := client.postErrHandler(resp, req, err)
if shortCircuited {
logSchedf("Routing callback intercepted response")
return
}
err = routeErr
}
// Call the requests callback handler...
logSchedf("Dispatching response callback. OP=0x%x. Opaque=%d", resp.Command, resp.Opaque)
req.tryCallback(resp, err)
}
func (client *memdClient) run() {
var (
// A queue for DCP commands so we can execute them out-of-band from packet receiving. This
// is integral to allow the higher level application to back-pressure against the DCP packet
// processing without interfeering with the SDKs control commands (like config fetch).
dcpBufferQ = make(chan *dcpBuffer, client.dcpQueueSize)
// After we signal that DCP processing should stop, we need a notification so we know when
// it has been completed, we do this to prevent leaving the goroutine around, and we need to
// ensure that the application has finished with the last packet it received before we stop.
dcpProcDoneCh = make(chan struct{})
)
go func() {
defer close(dcpProcDoneCh)
for {
// If the client has been told to close then we need to finish ASAP, otherwise if the dcpBufferQ has been
// closed then we'll flush the queue first.
q, stillOpen := <-dcpBufferQ
if !stillOpen || atomic.LoadUint32(&client.shutdownDCP) != 0 {
return
}
logSchedf("Resolving response OP=0x%x. Opaque=%d", q.resp.Command, q.resp.Opaque)
client.resolveRequest(q.resp)
// See below for information on MB-26363 for why this is here.
if !q.isInternal && client.dcpAckSize > 0 {
client.maybeSendDcpBufferAck(q.packetLen)
}
}
}()
go func() {
for {
packet, n, err := client.conn.ReadPacket()
if err != nil {
client.lock.Lock()
if !client.closed {
logWarnf("memdClient read failure on conn `%v` : %v", client.connID, err)
}
client.lock.Unlock()
break
}
resp := &memdQResponse{
sourceAddr: client.conn.RemoteAddr(),
sourceConnID: client.connID,
Packet: packet,
}
atomic.StoreInt64(&client.lastActivity, time.Now().UnixNano())
// We handle DCP no-op's directly here so we can reply immediately.
if resp.Packet.Command == memd.CmdDcpNoop {
err := client.conn.WritePacket(&memd.Packet{
Magic: memd.CmdMagicRes,
Command: memd.CmdDcpNoop,
Opaque: resp.Opaque,
})
if err != nil {
logWarnf("Failed to dispatch DCP noop reply: %s", err)
}
continue
}
// This is a fix for a bug in the server DCP implementation (MB-26363). This
// bug causes the server to fail to send a stream-end notification. The server
// does however synchronously stop the stream, and thus we can assume no more
// packets will be received following the close response.
if resp.Magic == memd.CmdMagicRes && resp.Command == memd.CmdDcpCloseStream && client.streamEndNotSupported {
closeReq := client.opList.Find(resp.Opaque)
if closeReq != nil {
vbID := closeReq.Vbucket
streamReq := client.opList.FindOpenStream(vbID)
if streamReq != nil {
endExtras := make([]byte, 4)
binary.BigEndian.PutUint32(endExtras, uint32(memd.StreamEndClosed))
endResp := &memdQResponse{
Packet: &memd.Packet{
Magic: memd.CmdMagicReq,
Command: memd.CmdDcpStreamEnd,
Vbucket: vbID,
Opaque: streamReq.Opaque,
Extras: endExtras,
},
}
dcpBufferQ <- &dcpBuffer{
resp: endResp,
packetLen: n,
isInternal: true,
}
}
}
}
switch resp.Packet.Command {
case memd.CmdDcpDeletion, memd.CmdDcpExpiration, memd.CmdDcpMutation, memd.CmdDcpSnapshotMarker,
memd.CmdDcpEvent, memd.CmdDcpOsoSnapshot, memd.CmdDcpSeqNoAdvanced, memd.CmdDcpStreamEnd:
dcpBufferQ <- &dcpBuffer{
resp: resp,
packetLen: n,
}
default:
logSchedf("Resolving response OP=0x%x. Opaque=%d", resp.Command, resp.Opaque)
client.resolveRequest(resp)
}
}
client.lock.Lock()
if !client.closed {
client.closed = true
client.lock.Unlock()
err := client.closeConn(true)
if err != nil {
// Lets log a warning, as this is non-fatal
logWarnf("Failed to shut down client connection (%s)", err)
}
} else {
client.lock.Unlock()
}
// We close the buffer channel to wake the processor if its asleep (queue was empty).
// We then wait to ensure it is finished with whatever packet (or packets if the connection was closed by the
// server) was being processed.
close(dcpBufferQ)
<-dcpProcDoneCh
close(client.connReleaseNotify)
client.opList.Drain(func(req *memdQRequest) {
if !atomic.CompareAndSwapPointer(&req.waitingIn, unsafe.Pointer(client), nil) {
logWarnf("Encountered an unowned request in a client opMap")
}
shortCircuited, routeErr := client.postErrHandler(nil, req, io.EOF)
if shortCircuited {
return
}
req.tryCallback(nil, routeErr)
})
<-client.connReleasedNotify
close(client.closeNotify)
}()
}
func (client *memdClient) LocalAddress() string {
return client.conn.LocalAddr()
}
func (client *memdClient) GracefulClose(err error) {
if atomic.CompareAndSwapUint32(&client.gracefulCloseTriggered, 0, 1) {
client.lock.Lock()
if client.closed {
client.lock.Unlock()
return
}
persistentReqs := client.opList.FindAndRemoveAllPersistent()
client.lock.Unlock()
if err == nil {
err = io.EOF
}
for _, req := range persistentReqs {
req.cancelWithCallback(err)
}
// Close down the DCP worker, there can't be any future DCP messages. We don't
// strictly need to do this, as connection close will trigger it to close anyway.
atomic.StoreUint32(&client.shutdownDCP, 1)
client.lock.Lock()
size := client.opList.Size()
if size > 0 {
// If there are items in the op list then we need to go into graceful shutdown mode, so don't close anything
// yet.
client.lock.Unlock()
return
}
// If there are no items in the oplist then it's safe to close down the client and connection now.
if client.closed {
client.lock.Unlock()
return
}
client.closed = true
client.lock.Unlock()
err := client.closeConn(false)
if err != nil {
// Lets log a warning, as this is non-fatal
logWarnf("Failed to shut down client connection (%s)", err)
}
}
}
func (client *memdClient) closeConn(internalTrigger bool) error {
logDebugf("%s/%p memdclient closing connection, internal close: %t", client.Address(), client, internalTrigger)
if err := client.conn.Close(); err != nil {
logDebugf("Failed to close memdconn: %v", err)
client.conn.Release()
close(client.connReleasedNotify)
return err
}
if !internalTrigger {
<-client.connReleaseNotify
}
client.conn.Release()
close(client.connReleasedNotify)
return nil
}
func (client *memdClient) Close() error {
// We mark that we are shutting down to stop the DCP processor from running any
// additional packets up to the application. We do this before the closed check to
// force stop flushing. Rebalance etc... uses GracefulClose so if we received this Close
// then we do need to shutdown in a timely manner.
atomic.StoreUint32(&client.shutdownDCP, 1)
client.lock.Lock()
if client.closed {
client.lock.Unlock()
return nil
}
client.closed = true
client.lock.Unlock()
return client.closeConn(false)
}
func (client *memdClient) sendCanary() {
errChan := make(chan error)
handler := func(resp *memdQResponse, req *memdQRequest, err error) {
errChan <- err
}
req := &memdQRequest{
Packet: memd.Packet{
Magic: memd.CmdMagicReq,
Command: memd.CmdNoop,
Datatype: 0,
Cas: 0,
Key: nil,
Value: nil,
},
Callback: handler,
RetryStrategy: newFailFastRetryStrategy(),
}
logDebugf("Sending NOOP request for %p/%s", client, client.Address())
err := client.internalSendRequest(req)
if err != nil {
client.breaker.MarkFailure()
}
timer := AcquireTimer(client.breaker.CanaryTimeout())
select {
case <-timer.C:
if !req.internalCancel(errRequestCanceled) {
err := <-errChan
if err == nil {
logDebugf("NOOP request successful for %p/%s", client, client.Address())
client.breaker.MarkSuccessful()
} else {
logDebugf("NOOP request failed for %p/%s", client, client.Address())
client.breaker.MarkFailure()
}
}
client.breaker.MarkFailure()
case err := <-errChan:
if err == nil {
client.breaker.MarkSuccessful()
} else {
client.breaker.MarkFailure()
}
}
}
func (client *memdClient) helloFeatures(props helloProps) []memd.HelloFeature {
var features []memd.HelloFeature
// Send the TLS flag, which has unknown effects.
features = append(features, memd.FeatureTLS)
// Indicate that we understand XATTRs
features = append(features, memd.FeatureXattr)
// Indicates that we understand select buckets.
features = append(features, memd.FeatureSelectBucket)
// If the user wants to use KV Error maps, lets enable them
if props.XErrorFeatureEnabled {
features = append(features, memd.FeatureXerror)
}
// Indicate that we understand JSON
if props.JSONFeatureEnabled {
features = append(features, memd.FeatureJSON)
}
// Indicate that we understand Point in Time
if props.PITRFeatureEnabled {
features = append(features, memd.FeaturePITR)
}
// If the user wants to use mutation tokens, lets enable them
if props.MutationTokensEnabled {
features = append(features, memd.FeatureSeqNo)
}
// If the user wants on-the-wire compression, lets try to enable it
if props.CompressionEnabled {
features = append(features, memd.FeatureSnappy)
}
if props.DurationsEnabled {
features = append(features, memd.FeatureDurations)
}
if props.CollectionsEnabled {
features = append(features, memd.FeatureCollections)
}
if props.OutOfOrderEnabled {
features = append(features, memd.FeatureUnorderedExec)
}
// These flags are informational so don't actually enable anything
features = append(features, memd.FeatureAltRequests)
features = append(features, memd.FeatureCreateAsDeleted)
features = append(features, memd.FeatureReplaceBodyWithXattr)
features = append(features, memd.FeaturePreserveExpiry)
if props.SyncReplicationEnabled {
features = append(features, memd.FeatureSyncReplication)
}
return features
}
type helloProps struct {
MutationTokensEnabled bool
CollectionsEnabled bool
CompressionEnabled bool
DurationsEnabled bool
OutOfOrderEnabled bool
JSONFeatureEnabled bool
XErrorFeatureEnabled bool
SyncReplicationEnabled bool
PITRFeatureEnabled bool
}
type bootstrapProps struct {
Bucket string
UserAgent string
ErrMapManager *errMapComponent
HelloProps helloProps
}
type memdInitFunc func(*memdClient, time.Time) error
func (client *memdClient) Bootstrap(cancelSig <-chan struct{}, settings bootstrapProps, deadline time.Time,
authMechanisms []AuthMechanism, authHandler authFuncHandler, cb memdInitFunc) error {
logDebugf("Memdclient `%s/%p` Fetching cluster client data", client.Address(), client)
bucket := settings.Bucket
features := client.helloFeatures(settings.HelloProps)
clientInfoStr := clientInfoString(client.connID, settings.UserAgent)
client.cancelBootstrapSig = cancelSig
helloCh, err := client.ExecHello(clientInfoStr, features, deadline)
if err != nil {
logDebugf("Memdclient `%s/%p` Failed to execute HELLO (%v)", client.Address(), client, err)
return err
}
errMapCh, err := client.ExecGetErrorMap(2, deadline)
if err != nil {
// GetErrorMap isn't integral to bootstrap succeeding
logDebugf("Memdclient `%s/%p`Failed to execute Get error map (%v)", client.Address(), client, err)
}
var listMechsCh chan SaslListMechsCompleted
firstAuthMethod := authHandler(client, deadline, authMechanisms[0])
// If the auth method is nil then we don't actually need to do any auth so no need to Get the mechanisms.
if firstAuthMethod != nil {
listMechsCh = make(chan SaslListMechsCompleted, 1)
err = client.SaslListMechs(deadline, func(mechs []AuthMechanism, err error) {
if err != nil {
logDebugf("Memdclient `%s/%p` Failed to fetch list auth mechs (%v)", client.Address(), client, err)
}
listMechsCh <- SaslListMechsCompleted{
Err: err,
Mechs: mechs,
}
})
if err != nil {
logDebugf("Memdclient `%s/%p` Failed to execute list auth mechs (%v)", client.Address(), client, err)
}
}
var completedAuthCh chan BytesAndError
var continueAuthCh chan bool
if firstAuthMethod != nil {
completedAuthCh, continueAuthCh, err = firstAuthMethod()
if err != nil {
logDebugf("Memdclient `%s/%p` Failed to execute auth (%v)", client.Address(), client, err)
return err
}
}
var selectCh chan BytesAndError
if continueAuthCh == nil {
if bucket != "" {
selectCh, err = client.ExecSelectBucket([]byte(bucket), deadline)
if err != nil {
logDebugf("Memdclient `%s/%p` Failed to execute select bucket (%v)", client.Address(), client, err)
return err
}
}
} else {
selectCh = client.continueAfterAuth(bucket, continueAuthCh, deadline)
}
helloResp := <-helloCh
if helloResp.Err != nil {
logDebugf("Memdclient `%s/%p` Failed to hello with server (%v)", client.Address(), client, helloResp.Err)
return helloResp.Err
}
if errMapCh != nil {
errMapResp := <-errMapCh
if errMapResp.Err == nil {
settings.ErrMapManager.StoreErrorMap(errMapResp.Bytes)
} else {
logDebugf("Memdclient `%s/%p` Failed to fetch kv error map (%s)", client.Address(), client, errMapResp.Err)
}
}
var serverAuthMechanisms []AuthMechanism
if listMechsCh != nil {
listMechsResp := <-listMechsCh
if listMechsResp.Err == nil {
serverAuthMechanisms = listMechsResp.Mechs
logDebugf("Memdclient `%s/%p` Server supported auth mechanisms: %v", client.Address(), client, serverAuthMechanisms)
} else {
logDebugf("Memdclient `%s/%p` Failed to fetch auth mechs from server (%v)", client.Address(), client, listMechsResp.Err)
}
}
// If completedAuthCh isn't nil then we have attempted to do auth so we need to wait on the result of that.
if completedAuthCh != nil {
authResp := <-completedAuthCh
if authResp.Err != nil {
logDebugf("Memdclient `%s/%p` Failed to perform auth against server (%v)", client.Address(), client, authResp.Err)
if errors.Is(authResp.Err, ErrRequestCanceled) {
// There's no point in us trying different mechanisms if something has cancelled bootstrapping.
return authResp.Err
} else if errors.Is(authResp.Err, ErrAuthenticationFailure) {
// If there's only one auth mechanism then we can just fail.
if len(authMechanisms) == 1 {
return authResp.Err
}
// If the server supports the mechanism we've tried then this auth error can't be due to an unsupported
// mechanism.
for _, mech := range serverAuthMechanisms {
if mech == authMechanisms[0] {
return authResp.Err
}
}
// If we've got here then the auth mechanism we tried is unsupported so let's keep trying with the next
// supported mechanism.
logInfof("Memdclient `%p` Unsupported authentication mechanism, will attempt to find next supported mechanism", client)
}
for {
var found bool
var mech AuthMechanism
found, mech, authMechanisms = findNextAuthMechanism(authMechanisms, serverAuthMechanisms)
if !found {
logDebugf("Memdclient `%s/%p` Failed to authenticate, all options exhausted", client.Address(), client)
return authResp.Err
}
logDebugf("Memdclient `%s/%p` Retrying authentication with found supported mechanism: %s", client.Address(), client, mech)
nextAuthFunc := authHandler(client, deadline, mech)
if nextAuthFunc == nil {
// This can't really happen but just in case it somehow does.
logInfof("Memdclient `%p` Failed to authenticate, no available credentials", client)
return authResp.Err
}
completedAuthCh, continueAuthCh, err = nextAuthFunc()
if err != nil {
logDebugf("Memdclient `%s/%p` Failed to execute auth (%v)", client.Address(), client, err)
return err
}
if continueAuthCh == nil {
if bucket != "" {
selectCh, err = client.ExecSelectBucket([]byte(bucket), deadline)
if err != nil {
logDebugf("Memdclient `%s/%p` Failed to execute select bucket (%v)", client.Address(), client, err)
return err
}
}
} else {
selectCh = client.continueAfterAuth(bucket, continueAuthCh, deadline)
}
authResp = <-completedAuthCh
if authResp.Err == nil {
break
}
logDebugf("Memdclient `%s/%p` Failed to perform auth against server (%v)", client.Address(), client, authResp.Err)
if errors.Is(authResp.Err, ErrAuthenticationFailure) || errors.Is(err, ErrRequestCanceled) {
return authResp.Err
}
}
}
logDebugf("Memdclient `%s/%p` Authenticated successfully", client.Address(), client)
}
if selectCh != nil {
selectResp := <-selectCh
if selectResp.Err != nil {
logDebugf("Memdclient `%s/%p` Failed to perform select bucket against server (%v)", client.Address(), client, selectResp.Err)
return selectResp.Err
}
}
client.features = helloResp.SrvFeatures
logDebugf("Memdclient `%s/%p` Client Features: %+v", client.Address(), client, features)
logDebugf("Memdclient `%s/%p` Server Features: %+v", client.Address(), client, client.features)
for _, feature := range client.features {
client.conn.EnableFeature(feature)
}
err = cb(client, deadline)
if err != nil {
return err
}
return nil
}
// BytesAndError contains the raw bytes of the result of an operation, and/or the error that occurred.
type BytesAndError struct {
Err error
Bytes []byte
}
func (client *memdClient) SaslAuth(k, v []byte, deadline time.Time, cb func(b []byte, err error)) error {
err := client.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 (client *memdClient) SaslStep(k, v []byte, deadline time.Time, cb func(err error)) error {
err := client.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 (client *memdClient) ExecSelectBucket(b []byte, deadline time.Time) (chan BytesAndError, error) {
completedCh := make(chan BytesAndError, 1)
err := client.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 priviledges to access the bucket but that the bucket
// is in some way not existing right now (e.g. in warmup).
err = errBucketNotFound
}
completedCh <- BytesAndError{
Err: err,
}
return
}
completedCh <- BytesAndError{
Bytes: resp.Value,
}
},
RetryStrategy: newFailFastRetryStrategy(),
},