-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathkvmux.go
1070 lines (892 loc) · 28 KB
/
kvmux.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 (
"bytes"
"container/list"
"errors"
"fmt"
"io"
"sort"
"strings"
"sync"
"sync/atomic"
"time"
"unsafe"
"github.com/couchbase/gocbcore/v10/memd"
)
type bucketCapabilityVerifier interface {
HasBucketCapabilityStatus(cap BucketCapability, status CapabilityStatus) bool
}
type dispatcher interface {
DispatchDirect(req *memdQRequest) (PendingOp, error)
RequeueDirect(req *memdQRequest, isRetry bool)
DispatchDirectToAddress(req *memdQRequest, address string) (PendingOp, error)
CollectionsEnabled() bool
SupportsCollections() bool
SetPostCompleteErrorHandler(handler postCompleteErrorHandler)
PipelineSnapshot() (*pipelineSnapshot, error)
}
type clientProvider interface {
GetByConnID(connID string) (*memdClient, error)
}
type kvMux struct {
muxPtr unsafe.Pointer
bucketName string
collectionsEnabled bool
queueSize int
poolSize int
cfgMgr *configManagementComponent
errMapMgr *errMapComponent
tracer *tracerComponent
dialer *memdClientDialerComponent
postCompleteErrHandler postCompleteErrorHandler
// muxStateWriteLock is necessary for functions which update the muxPtr, due to the scenario where ForceReconnect and
// OnNewRouteConfig could race. ForceReconnect must succeed and cannot fail because OnNewRouteConfig has updated
// the mux state whilst force is attempting to update it. We could also end up in a situation where a full reconnect
// is occurring at the same time as a pipeline takeover and scenarios like that, including missing a config update because
// ForceReconnect has won the race.
// There is no need for read side locks as we are locking around an atomic and it is only the write sides that present
// a potential issue.
muxStateWriteLock sync.Mutex
shutdownSig chan struct{}
clientCloseWg sync.WaitGroup
noTLSSeedNode bool
hasSeenConfigCh chan struct{}
}
type kvMuxProps struct {
CollectionsEnabled bool
QueueSize int
PoolSize int
NoTLSSeedNode bool
}
func newKVMux(props kvMuxProps, cfgMgr *configManagementComponent, errMapMgr *errMapComponent, tracer *tracerComponent,
dialer *memdClientDialerComponent, muxState *kvMuxState) *kvMux {
mux := &kvMux{
queueSize: props.QueueSize,
poolSize: props.PoolSize,
collectionsEnabled: props.CollectionsEnabled,
cfgMgr: cfgMgr,
errMapMgr: errMapMgr,
tracer: tracer,
dialer: dialer,
shutdownSig: make(chan struct{}),
noTLSSeedNode: props.NoTLSSeedNode,
muxPtr: unsafe.Pointer(muxState),
hasSeenConfigCh: make(chan struct{}),
bucketName: muxState.expectedBucketName,
}
cfgMgr.AddConfigWatcher(mux)
return mux
}
func (mux *kvMux) getState() *kvMuxState {
muxPtr := atomic.LoadPointer(&mux.muxPtr)
if muxPtr == nil {
return nil
}
return (*kvMuxState)(muxPtr)
}
func (mux *kvMux) updateState(old, new *kvMuxState) bool {
if new == nil {
logErrorf("Attempted to update to nil kvMuxState")
return false
}
if old != nil {
return atomic.CompareAndSwapPointer(&mux.muxPtr, unsafe.Pointer(old), unsafe.Pointer(new))
}
if atomic.SwapPointer(&mux.muxPtr, unsafe.Pointer(new)) != nil {
logErrorf("Updated from nil attempted on initialized kvMuxState")
return false
}
return true
}
func (mux *kvMux) clear() *kvMuxState {
mux.muxStateWriteLock.Lock()
val := atomic.SwapPointer(&mux.muxPtr, nil)
mux.muxStateWriteLock.Unlock()
return (*kvMuxState)(val)
}
func (mux *kvMux) OnNewRouteConfig(cfg *routeConfig) {
mux.muxStateWriteLock.Lock()
defer mux.muxStateWriteLock.Unlock()
oldMuxState := mux.getState()
if oldMuxState == nil {
// We can get here if we're shutting down and a NMVB comes in from an in flight request.
logWarnf("Received new config whilst shutting down kvmux")
return
}
newMuxState := mux.newKVMuxState(cfg, oldMuxState.tlsConfig, oldMuxState.authMechanisms, oldMuxState.auth)
// Attempt to atomically update the routing data
if !mux.updateState(oldMuxState, newMuxState) {
logWarnf("Someone preempted the config update, skipping update")
return
}
if oldMuxState.RevID() == -1 && newMuxState.RevID() > -1 {
if cfg.name != "" && mux.collectionsEnabled && !newMuxState.collectionsSupported {
logDebugf("Collections disabled as unsupported")
}
close(mux.hasSeenConfigCh)
}
if !mux.collectionsEnabled {
// If collections just aren't enabled then we never need to refresh the connections because collections
// have come online.
mux.pipelineTakeover(oldMuxState, newMuxState)
} else if oldMuxState.RevID() == -1 || oldMuxState.collectionsSupported == newMuxState.collectionsSupported {
// Get the new muxer to takeover the pipelines from the older one
mux.pipelineTakeover(oldMuxState, newMuxState)
} else {
// Collections support has changed so we need to reconnect all connections in order to support the new
// state.
mux.reconnectPipelines(oldMuxState, newMuxState, true)
}
mux.requeueRequests(oldMuxState)
}
func (mux *kvMux) SetPostCompleteErrorHandler(handler postCompleteErrorHandler) {
mux.postCompleteErrHandler = handler
}
func (mux *kvMux) ConfigRev() (int64, error) {
clientMux := mux.getState()
if clientMux == nil {
return 0, errShutdown
}
return clientMux.RevID(), nil
}
func (mux *kvMux) ConfigUUID() string {
clientMux := mux.getState()
if clientMux == nil {
return ""
}
return clientMux.UUID()
}
func (mux *kvMux) KeyToVbucket(key []byte) (uint16, error) {
clientMux := mux.getState()
if clientMux == nil || clientMux.VBMap() == nil {
return 0, errShutdown
}
return clientMux.VBMap().VbucketByKey(key), nil
}
func (mux *kvMux) NumReplicas() int {
clientMux := mux.getState()
if clientMux == nil {
return 0
}
if clientMux.VBMap() == nil {
return 0
}
return clientMux.VBMap().NumReplicas()
}
func (mux *kvMux) BucketType() bucketType {
clientMux := mux.getState()
if clientMux == nil {
return bktTypeInvalid
}
return clientMux.BucketType()
}
func (mux *kvMux) SupportsGCCCP() bool {
clientMux := mux.getState()
if clientMux == nil {
return false
}
return clientMux.BucketType() == bktTypeNone
}
func (mux *kvMux) NumPipelines() int {
clientMux := mux.getState()
if clientMux == nil {
return 0
}
return clientMux.NumPipelines()
}
// CollectionsEnaled returns whether or not the kv mux was created with collections enabled.
func (mux *kvMux) CollectionsEnabled() bool {
return mux.collectionsEnabled
}
func (mux *kvMux) IsSecure() bool {
return mux.getState().tlsConfig != nil
}
// SupportsCollections returns whether or not collections are enabled AND supported by the server.
func (mux *kvMux) SupportsCollections() bool {
if !mux.collectionsEnabled {
return false
}
clientMux := mux.getState()
if clientMux == nil {
return false
}
return clientMux.collectionsSupported
}
func (mux *kvMux) HasBucketCapabilityStatus(cap BucketCapability, status CapabilityStatus) bool {
clientMux := mux.getState()
if clientMux == nil {
return status == CapabilityStatusUnknown
}
return clientMux.HasBucketCapabilityStatus(cap, status)
}
func (mux *kvMux) BucketCapabilityStatus(cap BucketCapability) CapabilityStatus {
clientMux := mux.getState()
if clientMux == nil || clientMux.RevID() == -1 {
return CapabilityStatusUnknown
}
return clientMux.BucketCapabilityStatus(cap)
}
func (mux *kvMux) RouteRequest(req *memdQRequest) (*memdPipeline, error) {
clientMux := mux.getState()
if clientMux == nil {
return nil, errShutdown
}
// We haven't seen a valid config yet so put this in the dead pipeline so
// it'll get requeued once we do get a config.
if clientMux.RevID() == -1 {
return clientMux.deadPipe, nil
}
var srvIdx int
repIdx := req.ReplicaIdx
// Route to specific server
if repIdx < 0 {
srvIdx = -repIdx - 1
} else {
var err error
bktType := clientMux.BucketType()
if bktType == bktTypeCouchbase {
if req.Key != nil {
req.Vbucket = clientMux.VBMap().VbucketByKey(req.Key)
}
srvIdx, err = clientMux.VBMap().NodeByVbucket(req.Vbucket, uint32(repIdx))
if err != nil {
return nil, err
}
} else if bktType == bktTypeMemcached {
if repIdx > 0 {
// Error. Memcached buckets don't understand replicas!
return nil, errInvalidReplica
}
if len(req.Key) == 0 {
// Non-broadcast keyless Memcached bucket request
return nil, errInvalidArgument
}
srvIdx, err = clientMux.KetamaMap().NodeByKey(req.Key)
if err != nil {
return nil, err
}
} else if bktType == bktTypeNone {
// This means that we're using GCCCP and not connected to a bucket
return nil, errGCCCPInUse
}
}
pipeline := clientMux.GetPipeline(srvIdx)
if req.ServerGroup != "" && pipeline.serverGroup != req.ServerGroup {
return nil, ErrServerGroupMismatch
}
return clientMux.GetPipeline(srvIdx), nil
}
func (mux *kvMux) DispatchDirect(req *memdQRequest) (PendingOp, error) {
mux.tracer.StartCmdTrace(req)
req.dispatchTime = time.Now()
for {
pipeline, err := mux.RouteRequest(req)
if err != nil {
return nil, err
}
err = pipeline.SendRequest(req)
if err == errPipelineClosed {
continue
} else if err != nil {
if err == errPipelineFull {
err = errOverload
}
shortCircuit, routeErr := mux.handleOpRoutingResp(nil, req, err)
if shortCircuit {
return req, nil
}
return nil, routeErr
}
break
}
return req, nil
}
func (mux *kvMux) RequeueDirect(req *memdQRequest, isRetry bool) {
mux.requeueDirect(nil, req, isRetry)
}
func (mux *kvMux) requeueDirect(pipeline *memdPipeline, req *memdQRequest, isRetry bool) {
mux.tracer.StartCmdTrace(req)
handleError := func(err error) {
// We only want to log an error on retries if the error isn't cancelled.
if !isRetry || (isRetry && !errors.Is(err, ErrRequestCanceled)) {
logErrorf("Reschedule failed, failing request, Opaque=%d, Opcode=0x%x, (%s)", req.Opaque, req.Command, err)
}
req.tryCallback(nil, err)
}
logDebugf("Request being requeued, Opaque=%d, Opcode=0x%x", req.Opaque, req.Command)
if pipeline == nil {
var err error
pipeline, err = mux.RouteRequest(req)
if err != nil {
handleError(err)
return
}
}
for {
err := pipeline.RequeueRequest(req)
if err == nil {
return
}
if !errors.Is(err, errPipelineClosed) {
handleError(err)
return
}
pipeline, err = mux.RouteRequest(req)
if err != nil {
handleError(err)
return
}
}
}
func (mux *kvMux) GetByConnID(connID string) (*memdClient, error) {
clientMux := mux.getState()
if clientMux == nil {
return nil, errShutdown
}
for _, p := range clientMux.pipelines {
p.clientsLock.Lock()
for _, pipeCli := range p.clients {
pipeCli.lock.Lock()
if pipeCli.client.connID == connID {
pipeCli.lock.Unlock()
p.clientsLock.Unlock()
return pipeCli.client, nil
}
pipeCli.lock.Unlock()
}
p.clientsLock.Unlock()
}
return nil, errConnectionIDInvalid
}
func (mux *kvMux) DispatchDirectToAddress(req *memdQRequest, address string) (PendingOp, error) {
mux.tracer.StartCmdTrace(req)
req.dispatchTime = time.Now()
// We set the ReplicaIdx to a negative number to ensure it is not redispatched
// and we check that it was 0 to begin with to ensure it wasn't miss-used.
if req.ReplicaIdx != 0 {
return nil, errInvalidReplica
}
req.ReplicaIdx = -999999999
for {
clientMux := mux.getState()
if clientMux == nil {
return nil, errShutdown
}
var pipeline *memdPipeline
for _, p := range clientMux.pipelines {
if p.Address() == address {
pipeline = p
break
}
}
if pipeline == nil {
return nil, errInvalidServer
}
err := pipeline.SendRequest(req)
if err == errPipelineClosed {
continue
} else if err != nil {
if err == errPipelineFull {
err = errOverload
}
shortCircuit, routeErr := mux.handleOpRoutingResp(nil, req, err)
if shortCircuit {
return req, nil
}
return nil, routeErr
}
break
}
return req, nil
}
func (mux *kvMux) Close() error {
logInfof("KV Mux closing")
mux.cfgMgr.RemoveConfigWatcher(mux)
clientMux := mux.clear()
if clientMux == nil {
return errShutdown
}
// Trigger any memdclients that are in graceful close to forcibly close.
close(mux.shutdownSig)
var muxErr error
// Shut down the client multiplexer which will close all its queues
// effectively causing all the clients to shut down.
for _, pipeline := range clientMux.pipelines {
err := pipeline.Close()
if err != nil {
logErrorf("failed to shut down pipeline: %s", err)
muxErr = errCliInternalError
}
}
if clientMux.deadPipe != nil {
err := clientMux.deadPipe.Close()
if err != nil {
logErrorf("failed to shut down deadpipe: %s", err)
muxErr = errCliInternalError
}
}
// Drain all the pipelines and error their requests, then
// drain the dead queue and error those requests.
cb := func(req *memdQRequest) {
req.tryCallback(nil, errShutdown)
}
mux.drainPipelines(clientMux, cb)
mux.clientCloseWg.Wait()
logInfof("KV Mux closed")
return muxErr
}
func (mux *kvMux) ForceReconnect(tlsConfig *dynTLSConfig, authMechanisms []AuthMechanism, auth AuthProvider,
reconnectLocal bool) {
logDebugf("Forcing reconnect of all connections")
mux.muxStateWriteLock.Lock()
muxState := mux.getState()
newMuxState := mux.newKVMuxState(muxState.RouteConfig(), tlsConfig, authMechanisms, auth)
atomic.SwapPointer(&mux.muxPtr, unsafe.Pointer(newMuxState))
mux.reconnectPipelines(muxState, newMuxState, reconnectLocal)
mux.muxStateWriteLock.Unlock()
}
func (mux *kvMux) PipelineSnapshot() (*pipelineSnapshot, error) {
clientMux := mux.getState()
if clientMux == nil {
return nil, errShutdown
}
return &pipelineSnapshot{
state: clientMux,
}, nil
}
type waitForConfigSnapshotOp struct {
cancelCh chan struct{}
}
func (w *waitForConfigSnapshotOp) Cancel() {
close(w.cancelCh)
}
func (mux *kvMux) WaitForConfigSnapshot(deadline time.Time, cb WaitForConfigSnapshotCallback) (PendingOp, error) {
// No point in doing anything if we're shutdown.
clientMux := mux.getState()
if clientMux == nil {
return nil, errShutdown
}
op := &waitForConfigSnapshotOp{
cancelCh: make(chan struct{}),
}
var deadlineCh <-chan time.Time
if !deadline.IsZero() {
deadlineCh = time.After(time.Until(deadline))
}
start := time.Now()
go func() {
select {
case <-mux.shutdownSig:
cb(nil, errShutdown)
case <-op.cancelCh:
cb(nil, errRequestCanceled)
case <-deadlineCh:
cb(nil, &TimeoutError{
InnerError: errUnambiguousTimeout,
OperationID: "WaitForConfigSnapshot",
TimeObserved: time.Since(start),
})
case <-mux.hasSeenConfigCh:
// Just in case.
clientMux := mux.getState()
if clientMux == nil {
cb(nil, errShutdown)
return
}
cb(&WaitForConfigSnapshotResult{
Snapshot: &ConfigSnapshot{
state: clientMux,
},
}, nil)
}
}()
return op, nil
}
func (mux *kvMux) ConfigSnapshot() (*ConfigSnapshot, error) {
clientMux := mux.getState()
if clientMux == nil {
return nil, errShutdown
}
return &ConfigSnapshot{
state: clientMux,
}, nil
}
func (mux *kvMux) handleOpRoutingResp(resp *memdQResponse, req *memdQRequest, originalErr error) (bool, error) {
// If there is no error, we should return immediately
if originalErr == nil {
return false, nil
}
// If this operation has been cancelled, we just fail immediately.
if errors.Is(originalErr, ErrRequestCanceled) || errors.Is(originalErr, ErrTimeout) {
return false, originalErr
}
err := translateMemdError(originalErr, req)
if err == originalErr {
if errors.Is(err, io.EOF) && !mux.closed() {
// The connection has gone away.
if req.Command == memd.CmdGetClusterConfig {
return false, err
}
// If the request is idempotent or not written yet then we should retry.
if req.Idempotent() || req.ConnectionInfo().lastDispatchedTo == "" {
if mux.waitAndRetryOperation(req, SocketNotAvailableRetryReason) {
return true, nil
}
} else {
// If the request has been dispatched then the retry reason is in flight.
// For not causing a breaking change reasons we use socket not available for all idempotent
// requests.
if mux.waitAndRetryOperation(req, SocketCloseInFlightRetryReason) {
return true, nil
}
}
} else if errors.Is(err, ErrMemdClientClosed) && !mux.closed() {
if req.Command == memd.CmdGetClusterConfig {
return false, err
}
// The request can't have been dispatched yet.
if mux.waitAndRetryOperation(req, SocketNotAvailableRetryReason) {
return true, nil
}
} else if errors.Is(err, io.ErrShortWrite) {
// This is a special case where the write has failed on the underlying connection and not all the bytes
// were written to the network.
if mux.waitAndRetryOperation(req, MemdWriteFailure) {
return true, nil
}
} else if errors.Is(err, ErrMemdConfigOnly) {
logWarnf("Received config-only status, will attempt to refresh config map and retry operation")
if mux.handleConfigOnly(resp, req) {
return true, nil
}
} else if resp != nil && resp.Magic == memd.CmdMagicRes {
// We don't know anything about this error so send it to the error map
shouldRetry := mux.errMapMgr.ShouldRetry(resp.Status)
if shouldRetry {
if mux.waitAndRetryOperation(req, KVErrMapRetryReason) {
return true, nil
}
}
}
} else {
// Handle potentially retrying the operation
if errors.Is(err, ErrNotMyVBucket) {
if mux.handleNotMyVbucket(resp, req) {
return true, nil
}
} else if errors.Is(err, ErrDocumentLocked) {
if mux.waitAndRetryOperation(req, KVLockedRetryReason) {
return true, nil
}
} else if errors.Is(err, ErrTemporaryFailure) {
if mux.waitAndRetryOperation(req, KVTemporaryFailureRetryReason) {
return true, nil
}
} else if errors.Is(err, ErrDurableWriteInProgress) {
if mux.waitAndRetryOperation(req, KVSyncWriteInProgressRetryReason) {
return true, nil
}
} else if errors.Is(err, ErrDurableWriteReCommitInProgress) {
if mux.waitAndRetryOperation(req, KVSyncWriteRecommitInProgressRetryReason) {
return true, nil
}
}
// If an error isn't in this list then we know what this error is but we don't support retries for it.
}
err = mux.errMapMgr.EnhanceKvError(err, resp, req)
if mux.postCompleteErrHandler == nil {
return false, err
}
return mux.postCompleteErrHandler(resp, req, err)
}
func (mux *kvMux) closed() bool {
return mux.getState() == nil
}
func (mux *kvMux) waitAndRetryOperation(req *memdQRequest, reason RetryReason) bool {
shouldRetry, retryTime := retryOrchMaybeRetry(req, reason)
if shouldRetry {
go func() {
time.Sleep(time.Until(retryTime))
mux.RequeueDirect(req, true)
}()
return true
}
return false
}
func (mux *kvMux) parseNotMyVbucketValue(value []byte, sourceAddr string) *cfgBucket {
// Grab just the hostname from the source address
sourceHost, err := hostFromHostPort(sourceAddr)
if err != nil {
logErrorf("NMV response source address was invalid, skipping config update")
return nil
}
// Try to parse the value as a bucket configuration
logDebugf("Got NMV Block: %v", string(value))
bk, err := parseConfig(value, sourceHost)
if err != nil {
return nil
}
return bk
}
func (mux *kvMux) handleNotMyVbucket(resp *memdQResponse, req *memdQRequest) bool {
// For range scan continue we never want to retry, the range scan is now invalid.
isRetryableReq := req.Command != memd.CmdRangeScanContinue
logSchedf("Received NMV for request. OP=0x%x. Opaque=%d. Vbid: %d", req.Command, req.Opaque, req.Vbucket)
if len(resp.Value) == 0 {
logDebugf("NMV response containing no new config")
if !isRetryableReq {
return false
}
} else {
bk := mux.parseNotMyVbucketValue(resp.Value, resp.sourceAddr)
if bk == nil {
if !isRetryableReq {
return false
}
} else {
// We need to push this upstream which will then internal update the state with a new config.
mux.cfgMgr.OnNewConfig(bk)
if !isRetryableReq {
return false
}
originalVBID := req.Vbucket
pipeline, err := mux.RouteRequest(req)
if err == nil {
// If the address or vbucket has changed then just redispatch directly.
if pipeline.Address() != resp.sourceAddr || originalVBID != req.Vbucket {
mux.requeueDirect(pipeline, req, true)
return true
}
}
}
}
// Redirect it! This may actually come back to this server, but I won't tell
// if you don't ;)
return mux.waitAndRetryOperation(req, KVNotMyVBucketRetryReason)
}
func (mux *kvMux) handleConfigOnly(resp *memdQResponse, req *memdQRequest) bool {
snapshot, err := mux.PipelineSnapshot()
if err != nil {
logInfof("Failed to get pipeline snapshot: %s", err)
// Not much we can do here, attempt a retry.
mux.RequeueDirect(req, true)
return true
}
go func() {
// Don't block the client read loop whilst we apply the config and redispatch.
// For a start if the node this status has originated from is now not in the config then
// calling RefreshConfig will end up blocking because we're holding the client read thread open
// whilst also trying to shutdown the client.
mux.cfgMgr.RefreshConfig(snapshot)
mux.RequeueDirect(req, true)
}()
return true
}
func (mux *kvMux) drainPipelines(clientMux *kvMuxState, cb func(req *memdQRequest)) {
for _, pipeline := range clientMux.pipelines {
logDebugf("Draining queue. Address=`%s`. Num Clients=%d. Server Group=`%s`. Op Queue={%s}",
pipeline.Address(),
len(pipeline.Clients()),
pipeline.ServerGroup(),
strings.ReplaceAll(pipeline.queue.debugString(), "\n", ", "))
pipeline.Drain(cb)
}
if clientMux.deadPipe != nil {
clientMux.deadPipe.Drain(cb)
}
}
func (mux *kvMux) newKVMuxState(cfg *routeConfig, tlsConfig *dynTLSConfig, authMechanisms []AuthMechanism,
auth AuthProvider) *kvMuxState {
poolSize := 1
if !cfg.IsGCCCPConfig() {
poolSize = mux.poolSize
}
useTls := tlsConfig != nil
var kvServerList []routeEndpoint
if mux.noTLSSeedNode {
// The order of the kv server list matters, so we need to maintain the same order and just replace the seed
// node.
if useTls {
kvServerList = make([]routeEndpoint, len(cfg.kvServerList.SSLEndpoints))
copy(kvServerList, cfg.kvServerList.SSLEndpoints)
for i, ep := range cfg.kvServerList.NonSSLEndpoints {
if ep.IsSeedNode {
kvServerList[i] = ep
}
}
} else {
kvServerList = cfg.kvServerList.NonSSLEndpoints
}
} else {
if useTls {
kvServerList = cfg.kvServerList.SSLEndpoints
} else {
kvServerList = cfg.kvServerList.NonSSLEndpoints
}
}
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintln("KV muxer applying endpoints:"))
buffer.WriteString(fmt.Sprintf("Bucket: %s\n", cfg.name))
for _, ep := range kvServerList {
buffer.WriteString(fmt.Sprintf(" - %s\n", ep.Address))
}
logDebugf(buffer.String())
pipelines := make([]*memdPipeline, len(kvServerList))
for i, hostPort := range kvServerList {
trimmedHostPort := routeEndpoint{
Address: trimSchemePrefix(hostPort.Address),
IsSeedNode: hostPort.IsSeedNode,
ServerGroup: hostPort.ServerGroup,
}
getCurClientFn := func(cancelSig <-chan struct{}) (*memdClient, error) {
return mux.dialer.SlowDialMemdClient(cancelSig, trimmedHostPort, tlsConfig, auth, authMechanisms,
mux.handleOpRoutingResp, mux.handleServerRequest)
}
pipeline := newPipeline(trimmedHostPort, poolSize, mux.queueSize, getCurClientFn)
pipelines[i] = pipeline
}
return newKVMuxState(cfg, kvServerList, tlsConfig, authMechanisms, auth, mux.bucketName, pipelines,
newDeadPipeline(mux.queueSize))
}
func (mux *kvMux) reconnectPipelines(oldMuxState *kvMuxState, newMuxState *kvMuxState, reconnectSeed bool) {
oldPipelines := list.New()
for _, pipeline := range oldMuxState.pipelines {
oldPipelines.PushBack(pipeline)
}
for _, pipeline := range newMuxState.pipelines {
// If we aren't reconnecting the seed node then we need to take its clients and make sure we don't
// end up closing it down.
if pipeline.isSeedNode && !reconnectSeed {
oldPipeline := mux.stealPipeline(pipeline.Address(), oldPipelines)
if oldPipeline != nil {
pipeline.Takeover(oldPipeline)
}
}
pipeline.StartClients()
}
for e := oldPipelines.Front(); e != nil; e = e.Next() {
pipeline, ok := e.Value.(*memdPipeline)
if !ok {
logErrorf("Failed to cast old pipeline")
continue
}
clients := pipeline.GracefulClose()
for _, client := range clients {
mux.closeMemdClient(client, errForcedReconnect)
}
}
}
func (mux *kvMux) requeueRequests(oldMuxState *kvMuxState) {
// Gather all the requests from all the old pipelines and then
// sort and redispatch them (which will use the new pipelines)
var requestList []*memdQRequest
mux.drainPipelines(oldMuxState, func(req *memdQRequest) {
requestList = append(requestList, req)
})
sort.Sort(memdQRequestSorter(requestList))
for _, req := range requestList {
req.processingLock.Lock()
stopCmdTraceLocked(req)
req.processingLock.Unlock()
// If the command is a get cluster config then we cancel it rather than requeuing.
// Get cluster config is explicitly sent a specific pipeline so we do not want to requeue.
// This may seem like it'll cause the poller to take longer to fetch a config but that's
// OK because we can only have here by something fetching a new config anyway.
if req.Command == memd.CmdGetClusterConfig {
req.tryCallback(nil, ErrRequestCanceled)
continue
}
mux.RequeueDirect(req, false)
}
}
// closeMemdClient will gracefully close the memdclient, spinning up a goroutine to watch for when the client
// shuts down. The error provided is the error sent to any callback handlers for persistent operations which are
// currently live in the client.
func (mux *kvMux) closeMemdClient(client *memdClient, err error) {
mux.clientCloseWg.Add(1)
client.GracefulClose(err)
go func(client *memdClient) {
select {
case <-client.CloseNotify():
logDebugf("Memdclient %s/%p completed graceful shutdown", client.Address(), client)
case <-mux.shutdownSig:
logDebugf("Memdclient %s/%p being forcibly shutdown", client.Address(), client)
// Force the client to close even if there are requests in flight.
err := client.Close()
if err != nil {
logErrorf("failed to shutdown memdclient: %s", err)
}
<-client.CloseNotify()
logDebugf("Memdclient %s/%p completed shutdown", client.Address(), client)
}
mux.clientCloseWg.Done()
}(client)
}
func (mux *kvMux) stealPipeline(address string, oldPipelines *list.List) *memdPipeline {
for e := oldPipelines.Front(); e != nil; e = e.Next() {
pipeline, ok := e.Value.(*memdPipeline)
if !ok {
logErrorf("Failed to cast old pipeline")
continue
}
if pipeline.Address() == address {