forked from couchbase/gocbcore
/
agent.go
997 lines (837 loc) · 26.2 KB
/
agent.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
// Package gocbcore implements methods for low-level communication
// with a Couchbase Server cluster.
package gocbcore
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"github.com/opentracing/opentracing-go"
"golang.org/x/net/http2"
"gopkg.in/couchbaselabs/gocbconnstr.v1"
"io/ioutil"
"math/rand"
"net"
"net/http"
"strconv"
"sync"
"time"
)
// Agent represents the base client handling connections to a Couchbase Server.
// 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.
type Agent struct {
clientId string
userString string
auth AuthProvider
bucket string
tlsConfig *tls.Config
initFn memdInitFunc
useMutationTokens bool
useKvErrorMaps bool
useEnhancedErrors bool
useCompression bool
useDurations bool
configLock sync.Mutex
routingInfo routeDataPtr
kvErrorMap kvErrorMapPtr
numVbuckets int
tracer opentracing.Tracer
noRootTraceSpans bool
serverFailuresLock sync.Mutex
serverFailures map[string]time.Time
httpCli *http.Client
confHttpRedialPeriod time.Duration
confHttpRetryDelay time.Duration
confCccpMaxWait time.Duration
confCccpPollPeriod time.Duration
serverConnectTimeout time.Duration
serverWaitTimeout time.Duration
nmvRetryDelay time.Duration
kvPoolSize int
maxQueueSize int
}
// ServerConnectTimeout gets the timeout for each server connection, including all authentication steps.
func (agent *Agent) ServerConnectTimeout() time.Duration {
return agent.serverConnectTimeout
}
// SetServerConnectTimeout sets the timeout for each server connection.
func (agent *Agent) SetServerConnectTimeout(timeout time.Duration) {
agent.serverConnectTimeout = timeout
}
// HttpClient returns a pre-configured HTTP Client for communicating with
// Couchbase Server. You must still specify authentication information
// for any dispatched requests.
func (agent *Agent) HttpClient() *http.Client {
return agent.httpCli
}
func (agent *Agent) getErrorMap() *kvErrorMap {
return agent.kvErrorMap.Get()
}
// AuthFunc is invoked by the agent to authenticate a client.
type AuthFunc func(client AuthClient, deadline time.Time) error
// AgentConfig specifies the configuration options for creation of an Agent.
type AgentConfig struct {
UserString string
MemdAddrs []string
HttpAddrs []string
TlsConfig *tls.Config
BucketName string
AuthHandler AuthFunc
Auth AuthProvider
UseMutationTokens bool
UseKvErrorMaps bool
UseEnhancedErrors bool
UseCompression bool
UseDurations bool
HttpRedialPeriod time.Duration
HttpRetryDelay time.Duration
CccpMaxWait time.Duration
CccpPollPeriod time.Duration
ConnectTimeout time.Duration
ServerConnectTimeout time.Duration
NmvRetryDelay time.Duration
KvPoolSize int
MaxQueueSize int
HttpMaxIdleConns int
HttpMaxIdleConnsPerHost int
HttpIdleConnTimeout time.Duration
Tracer opentracing.Tracer
NoRootTraceSpans bool
// Username specifies the username to use when connecting.
// DEPRECATED
Username string
// Password specifies the password to use when connecting.
// DEPRECATED
Password string
}
// FromConnStr populates the AgentConfig with information from a
// Couchbase Connection String.
// Supported options are:
// cacertpath (string) - Path to the CA certificate
// certpath (string) - Path to your authentication certificate
// keypath (string) - Path to your authentication key
// config_total_timeout (int) - Maximum period to attempt to connect to cluster in ms.
// config_node_timeout (int) - Maximum period to attempt to connect to a node in ms.
// http_redial_period (int) - Maximum period to keep HTTP config connections open in ms.
// http_retry_delay (int) - Period to wait between retrying nodes for HTTP config in ms.
// config_poll_floor_interval (int) - Minimum time to wait between fetching configs via CCCP in ms.
// config_poll_interval (int) - Period to wait between CCCP config polling in ms.
// kv_pool_size (int) - The number of connections to establish per node.
// max_queue_size (int) - The maximum size of the operation queues per node.
// use_kverrmaps (bool) - Whether to enable error maps from the server.
// use_enhanced_errors (bool) - Whether to enable enhanced error information.
// fetch_mutation_tokens (bool) - Whether to fetch mutation tokens for operations.
// compression (bool) - Whether to enable network-wise compression of documents.
// server_duration (bool) - Whether to enable fetching server operation durations.
// http_max_idle_conns (int) - Maximum number of idle http connections in the pool.
// http_max_idle_conns_per_host (int) - Maximum number of idle http connections in the pool per host.
// http_idle_conn_timeout (int) - Maximum length of time for an idle connection to stay in the pool in ms.
func (config *AgentConfig) FromConnStr(connStr string) error {
baseSpec, err := gocbconnstr.Parse(connStr)
if err != nil {
return err
}
spec, err := gocbconnstr.Resolve(baseSpec)
if err != nil {
return err
}
fetchOption := func(name string) (string, bool) {
optValue := spec.Options[name]
if len(optValue) == 0 {
return "", false
}
return optValue[len(optValue)-1], true
}
// Grab the resolved hostnames into a set of string arrays
var httpHosts []string
for _, specHost := range spec.HttpHosts {
httpHosts = append(httpHosts, fmt.Sprintf("%s:%d", specHost.Host, specHost.Port))
}
var memdHosts []string
for _, specHost := range spec.MemdHosts {
memdHosts = append(memdHosts, fmt.Sprintf("%s:%d", specHost.Host, specHost.Port))
}
// Get bootstrap_on option to determine which, if any, of the bootstrap nodes should be cleared
switch val, _ := fetchOption("bootstrap_on"); val {
case "http":
memdHosts = nil
if len(httpHosts) == 0 {
return errors.New("bootstrap_on=http but no HTTP hosts in connection string")
}
case "cccp":
httpHosts = nil
if len(memdHosts) == 0 {
return errors.New("bootstrap_on=cccp but no CCCP/Memcached hosts in connection string")
}
case "both":
case "":
// Do nothing
break
default:
return errors.New("bootstrap_on={http,cccp,both}")
}
config.MemdAddrs = memdHosts
config.HttpAddrs = httpHosts
var tlsConfig *tls.Config
if spec.UseSsl {
var certpath string
var keypath string
var cacertpaths []string
if len(spec.Options["cacertpath"]) > 0 || len(spec.Options["keypath"]) > 0 {
cacertpaths = spec.Options["cacertpath"]
certpath, _ = fetchOption("certpath")
keypath, _ = fetchOption("keypath")
} else {
cacertpaths = spec.Options["certpath"]
}
tlsConfig = &tls.Config{}
if len(cacertpaths) > 0 {
roots := x509.NewCertPool()
for _, path := range cacertpaths {
cacert, err := ioutil.ReadFile(path)
if err != nil {
return err
}
ok := roots.AppendCertsFromPEM(cacert)
if !ok {
return ErrInvalidCert
}
}
tlsConfig.RootCAs = roots
} else {
tlsConfig.InsecureSkipVerify = true
}
if certpath != "" && keypath != "" {
cert, err := tls.LoadX509KeyPair(certpath, keypath)
if err != nil {
return err
}
tlsConfig.Certificates = []tls.Certificate{cert}
}
}
config.TlsConfig = tlsConfig
if spec.Bucket != "" {
config.BucketName = spec.Bucket
}
if valStr, ok := fetchOption("config_total_timeout"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return fmt.Errorf("config_total_timeout option must be a number")
}
config.ConnectTimeout = time.Duration(val) * time.Millisecond
}
if valStr, ok := fetchOption("config_node_timeout"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return fmt.Errorf("config_node_timeout option must be a number")
}
config.ServerConnectTimeout = time.Duration(val) * time.Millisecond
}
// This option is experimental
if valStr, ok := fetchOption("http_redial_period"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return fmt.Errorf("http redial period option must be a number")
}
config.HttpRedialPeriod = time.Duration(val) * time.Millisecond
}
// This option is experimental
if valStr, ok := fetchOption("http_retry_delay"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return fmt.Errorf("http retry delay option must be a number")
}
config.HttpRetryDelay = time.Duration(val) * time.Millisecond
}
// This option is deprecated (see config_poll_floor_interval)
if valStr, ok := fetchOption("cccp_max_wait"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return fmt.Errorf("cccp max wait option must be a number")
}
config.CccpMaxWait = time.Duration(val) * time.Millisecond
}
if valStr, ok := fetchOption("config_poll_floor_interval"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return fmt.Errorf("config pool floor interval option must be a number")
}
config.CccpMaxWait = time.Duration(val) * time.Millisecond
}
// This option is deprecated (see config_poll_interval)
if valStr, ok := fetchOption("cccp_poll_period"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return fmt.Errorf("cccp pool period option must be a number")
}
config.CccpPollPeriod = time.Duration(val) * time.Millisecond
}
if valStr, ok := fetchOption("config_poll_interval"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return fmt.Errorf("config pool interval option must be a number")
}
config.CccpPollPeriod = time.Duration(val) * time.Millisecond
}
// This option is experimental
if valStr, ok := fetchOption("kv_pool_size"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return fmt.Errorf("kv pool size option must be a number")
}
config.KvPoolSize = int(val)
}
// This option is experimental
if valStr, ok := fetchOption("max_queue_size"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return fmt.Errorf("max queue size option must be a number")
}
config.MaxQueueSize = int(val)
}
if valStr, ok := fetchOption("use_kverrmaps"); ok {
val, err := strconv.ParseBool(valStr)
if err != nil {
return fmt.Errorf("use_kverrmaps option must be a boolean")
}
config.UseKvErrorMaps = val
}
if valStr, ok := fetchOption("use_enhanced_errors"); ok {
val, err := strconv.ParseBool(valStr)
if err != nil {
return fmt.Errorf("use_enhanced_errors option must be a boolean")
}
config.UseEnhancedErrors = val
}
if valStr, ok := fetchOption("fetch_mutation_tokens"); ok {
val, err := strconv.ParseBool(valStr)
if err != nil {
return fmt.Errorf("fetch_mutation_tokens option must be a boolean")
}
config.UseMutationTokens = val
}
if valStr, ok := fetchOption("compression"); ok {
val, err := strconv.ParseBool(valStr)
if err != nil {
return fmt.Errorf("compression option must be a boolean")
}
config.UseCompression = val
}
if valStr, ok := fetchOption("server_duration"); ok {
val, err := strconv.ParseBool(valStr)
if err != nil {
return fmt.Errorf("server_duration option must be a boolean")
}
config.UseDurations = val
}
if valStr, ok := fetchOption("http_max_idle_conns"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return fmt.Errorf("http max idle connections option must be a number")
}
config.HttpMaxIdleConns = int(val)
}
if valStr, ok := fetchOption("http_max_idle_conns_per_host"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return fmt.Errorf("http max idle connections per host option must be a number")
}
config.HttpMaxIdleConnsPerHost = int(val)
}
if valStr, ok := fetchOption("http_idle_conn_timeout"); ok {
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return fmt.Errorf("http idle connection timeout option must be a number")
}
config.HttpIdleConnTimeout = time.Duration(val) * time.Millisecond
}
return nil
}
func makeDefaultAuthHandler(authProvider AuthProvider, bucketName string) AuthFunc {
return func(client AuthClient, deadline time.Time) error {
creds, err := getKvAuthCreds(authProvider, client.Address())
if err != nil {
return err
}
if creds.Username != "" || creds.Password != "" {
if err := SaslAuthPlain(creds.Username, creds.Password, client, deadline); err != nil {
return err
}
}
if client.SupportsFeature(FeatureSelectBucket) {
if err := client.ExecSelectBucket([]byte(bucketName), deadline); err != nil {
return err
}
}
return nil
}
}
func normalizeAgentConfig(configIn *AgentConfig) *AgentConfig {
config := *configIn
// If the user does not provide an authentication provider, we should use
// the deprecated username/password fields to create one.
if config.Auth == nil {
config.Auth = &PasswordAuthProvider{
Username: config.Username,
Password: config.Password,
}
}
// TODO: The location of this happening is a bit strange
if config.AuthHandler == nil {
config.AuthHandler = makeDefaultAuthHandler(config.Auth, config.BucketName)
}
return &config
}
// CreateAgent creates an agent for performing normal operations.
func CreateAgent(configIn *AgentConfig) (*Agent, error) {
config := normalizeAgentConfig(configIn)
initFn := func(client *syncClient, deadline time.Time) error {
return config.AuthHandler(client, deadline)
}
return createAgent(config, initFn)
}
// CreateDcpAgent creates an agent for performing DCP operations.
func CreateDcpAgent(configIn *AgentConfig, dcpStreamName string, openFlags DcpOpenFlag) (*Agent, error) {
config := normalizeAgentConfig(configIn)
// We wrap the authorization system to force DCP channel opening
// as part of the "initialization" for any servers.
initFn := func(client *syncClient, deadline time.Time) error {
if err := config.AuthHandler(client, deadline); err != nil {
return err
}
if err := client.ExecOpenDcpConsumer(dcpStreamName, openFlags, deadline); err != nil {
return err
}
if err := client.ExecEnableDcpNoop(180*time.Second, deadline); err != nil {
return err
}
return client.ExecEnableDcpBufferAck(8*1024*1024, deadline)
}
return createAgent(config, initFn)
}
func createAgent(config *AgentConfig, initFn memdInitFunc) (*Agent, error) {
// TODO(brett19): Put all configurable options in the AgentConfig
logDebugf("SDK Version: gocb/%s", goCbCoreVersionStr)
logDebugf("Creating new agent: %+v", config)
httpTransport := &http.Transport{
TLSClientConfig: config.TlsConfig,
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
MaxIdleConns: config.HttpMaxIdleConns,
MaxIdleConnsPerHost: config.HttpMaxIdleConnsPerHost,
IdleConnTimeout: config.HttpIdleConnTimeout,
}
err := http2.ConfigureTransport(httpTransport)
if err != nil {
logDebugf("failed to configure http2: %s", err)
}
tracer := config.Tracer
if tracer == nil {
tracer = opentracing.NoopTracer{}
}
c := &Agent{
clientId: formatCbUid(randomCbUid()),
userString: config.UserString,
bucket: config.BucketName,
auth: config.Auth,
tlsConfig: config.TlsConfig,
initFn: initFn,
httpCli: &http.Client{
Transport: httpTransport,
},
tracer: tracer,
useMutationTokens: config.UseMutationTokens,
useKvErrorMaps: config.UseKvErrorMaps,
useEnhancedErrors: config.UseEnhancedErrors,
useCompression: config.UseCompression,
useDurations: config.UseDurations,
noRootTraceSpans: config.NoRootTraceSpans,
serverFailures: make(map[string]time.Time),
serverConnectTimeout: 7000 * time.Millisecond,
serverWaitTimeout: 5 * time.Second,
nmvRetryDelay: 100 * time.Millisecond,
kvPoolSize: 1,
maxQueueSize: 2048,
confHttpRetryDelay: 10 * time.Second,
confHttpRedialPeriod: 10 * time.Second,
confCccpMaxWait: 3 * time.Second,
confCccpPollPeriod: 2500 * time.Millisecond,
}
connectTimeout := 60000 * time.Millisecond
if config.ConnectTimeout > 0 {
connectTimeout = config.ConnectTimeout
}
if config.ServerConnectTimeout > 0 {
c.serverConnectTimeout = config.ServerConnectTimeout
}
if config.NmvRetryDelay > 0 {
c.nmvRetryDelay = config.NmvRetryDelay
}
if config.KvPoolSize > 0 {
c.kvPoolSize = config.KvPoolSize
}
if config.MaxQueueSize > 0 {
c.maxQueueSize = config.MaxQueueSize
}
if config.HttpRetryDelay > 0 {
c.confHttpRetryDelay = config.HttpRetryDelay
}
if config.HttpRedialPeriod > 0 {
c.confHttpRedialPeriod = config.HttpRedialPeriod
}
if config.CccpMaxWait > 0 {
c.confCccpMaxWait = config.CccpMaxWait
}
if config.CccpPollPeriod > 0 {
c.confCccpPollPeriod = config.CccpPollPeriod
}
deadline := time.Now().Add(connectTimeout)
if err := c.connect(config.MemdAddrs, config.HttpAddrs, deadline); err != nil {
return nil, err
}
return c, nil
}
func (agent *Agent) cccpLooper() {
tickTime := agent.confCccpPollPeriod
maxWaitTime := agent.confCccpMaxWait
logDebugf("CCCP Looper starting.")
nodeIdx := -1
for {
// Wait 10 seconds
time.Sleep(tickTime)
routingInfo := agent.routingInfo.Get()
if routingInfo == nil {
// If we have a blank routingInfo, it indicates the client is shut down.
break
}
numNodes := routingInfo.clientMux.NumPipelines()
if numNodes == 0 {
logDebugf("CCCPPOLL: No nodes available to poll")
continue
}
if nodeIdx < 0 {
nodeIdx = rand.Intn(numNodes)
}
var foundConfig *cfgBucket
for nodeOff := 0; nodeOff < numNodes; nodeOff++ {
nodeIdx = (nodeIdx + 1) % numNodes
pipeline := routingInfo.clientMux.GetPipeline(nodeIdx)
client := syncClient{
client: &memdPipelineSenderWrap{
pipeline: pipeline,
},
}
cccpBytes, err := client.ExecCccpRequest(time.Now().Add(maxWaitTime))
if err != nil {
logDebugf("CCCPPOLL: Failed to retrieve CCCP config. %v", err)
continue
}
hostName, err := hostFromHostPort(pipeline.Address())
if err != nil {
logErrorf("CCCPPOLL: Failed to parse source address. %v", err)
continue
}
bk, err := parseConfig(cccpBytes, hostName)
if err != nil {
logDebugf("CCCPPOLL: Failed to parse CCCP config. %v", err)
continue
}
foundConfig = bk
break
}
if foundConfig == nil {
logDebugf("CCCPPOLL: Failed to retrieve config from any node.")
continue
}
logDebugf("CCCPPOLL: Received new config")
agent.updateConfig(foundConfig)
}
}
func (agent *Agent) connect(memdAddrs, httpAddrs []string, deadline time.Time) error {
logDebugf("Attempting to connect...")
for _, thisHostPort := range memdAddrs {
logDebugf("Trying server at %s", thisHostPort)
srvDeadlineTm := time.Now().Add(agent.serverConnectTimeout)
if srvDeadlineTm.After(deadline) {
srvDeadlineTm = deadline
}
logDebugf("Trying to connect")
client, err := agent.dialMemdClient(thisHostPort)
if IsErrorStatus(err, StatusAuthError) ||
IsErrorStatus(err, StatusAccessError) {
return err
} else if err != nil {
logDebugf("Connecting failed! %v", err)
continue
}
disconnectClient := func() {
err := client.Close()
if err != nil {
logErrorf("Failed to shut down client connection (%s)", err)
}
}
syncCli := syncClient{
client: client,
}
logDebugf("Attempting to request CCCP configuration")
cccpBytes, err := syncCli.ExecCccpRequest(srvDeadlineTm)
if err != nil {
logDebugf("Failed to retrieve CCCP config. %v", err)
disconnectClient()
continue
}
hostName, err := hostFromHostPort(thisHostPort)
if err != nil {
logErrorf("Failed to parse CCCP source address. %v", err)
disconnectClient()
continue
}
bk, err := parseConfig(cccpBytes, hostName)
if err != nil {
logDebugf("Failed to parse CCCP configuration. %v", err)
disconnectClient()
continue
}
if !bk.supportsCccp() {
logDebugf("Bucket does not support CCCP")
disconnectClient()
break
}
routeCfg := buildRouteConfig(bk, agent.IsSecure())
if !routeCfg.IsValid() {
logDebugf("Configuration was deemed invalid")
disconnectClient()
continue
}
logDebugf("Successfully connected")
// Build some fake routing data, this is used to indicate that
// client is "alive". A nil routeData causes immediate shutdown.
agent.routingInfo.Update(nil, &routeData{
revId: -1,
})
// TODO(brett19): Save the client that we build for bootstrap
disconnectClient()
if routeCfg.vbMap != nil {
agent.numVbuckets = routeCfg.vbMap.NumVbuckets()
} else {
agent.numVbuckets = 0
}
agent.applyConfig(routeCfg)
go agent.cccpLooper()
return nil
}
signal := make(chan error, 1)
var epList []string
for _, hostPort := range httpAddrs {
if !agent.IsSecure() {
epList = append(epList, fmt.Sprintf("http://%s", hostPort))
} else {
epList = append(epList, fmt.Sprintf("https://%s", hostPort))
}
}
agent.routingInfo.Update(nil, &routeData{
revId: -1,
mgmtEpList: epList,
})
var routeCfg *routeConfig
logDebugf("Starting HTTP looper! %v", epList)
go agent.httpLooper(func(cfg *cfgBucket, err error) bool {
if err != nil {
signal <- err
return true
}
newRouteCfg := buildRouteConfig(cfg, agent.IsSecure())
if !newRouteCfg.IsValid() {
// Something is invalid about this config, keep trying
return false
}
routeCfg = newRouteCfg
signal <- nil
return true
})
err := <-signal
if err != nil {
return err
}
if routeCfg.vbMap != nil {
agent.numVbuckets = routeCfg.vbMap.NumVbuckets()
} else {
agent.numVbuckets = 0
}
agent.applyConfig(routeCfg)
return nil
}
// Close shuts down the agent, disconnecting from all servers and failing
// any outstanding operations with ErrShutdown.
func (agent *Agent) Close() error {
agent.configLock.Lock()
// Clear the routingInfo so no new operations are performed
// and retrieve the last active routing configuration
routingInfo := agent.routingInfo.Clear()
if routingInfo == nil {
agent.configLock.Unlock()
return ErrShutdown
}
// Shut down the client multiplexer which will close all its queues
// effectively causing all the clients to shut down.
muxCloseErr := routingInfo.clientMux.Close()
// Drain all the pipelines and error their requests, then
// drain the dead queue and error those requests.
routingInfo.clientMux.Drain(func(req *memdQRequest) {
req.tryCallback(nil, ErrShutdown)
})
agent.configLock.Unlock()
return muxCloseErr
}
// IsSecure returns whether this client is connected via SSL.
func (agent *Agent) IsSecure() bool {
return agent.tlsConfig != nil
}
// BucketUUID returns the UUID of the bucket we are connected to.
func (agent *Agent) BucketUUID() string {
routingInfo := agent.routingInfo.Get()
if routingInfo == nil {
return ""
}
return routingInfo.uuid
}
// KeyToVbucket translates a particular key to its assigned vbucket.
func (agent *Agent) KeyToVbucket(key []byte) uint16 {
// TODO(brett19): The KeyToVbucket Bucket API should return an error
routingInfo := agent.routingInfo.Get()
if routingInfo == nil {
return 0
}
if routingInfo.vbMap == nil {
return 0
}
return routingInfo.vbMap.VbucketByKey(key)
}
// KeyToServer translates a particular key to its assigned server index.
func (agent *Agent) KeyToServer(key []byte, replicaIdx uint32) int {
routingInfo := agent.routingInfo.Get()
if routingInfo == nil {
return -1
}
if routingInfo.vbMap != nil {
serverIdx, err := routingInfo.vbMap.NodeByKey(key, replicaIdx)
if err != nil {
return -1
}
return serverIdx
}
if routingInfo.ketamaMap != nil {
serverIdx, err := routingInfo.ketamaMap.NodeByKey(key)
if err != nil {
return -1
}
return serverIdx
}
return -1
}
// VbucketToServer returns the server index for a particular vbucket.
func (agent *Agent) VbucketToServer(vbID uint16, replicaIdx uint32) int {
routingInfo := agent.routingInfo.Get()
if routingInfo == nil {
return -1
}
if routingInfo.vbMap == nil {
return -1
}
serverIdx, err := routingInfo.vbMap.NodeByVbucket(vbID, replicaIdx)
if err != nil {
return -1
}
return serverIdx
}
// NumVbuckets returns the number of VBuckets configured on the
// connected cluster.
func (agent *Agent) NumVbuckets() int {
return agent.numVbuckets
}
func (agent *Agent) bucketType() bucketType {
routingInfo := agent.routingInfo.Get()
if routingInfo == nil {
return bktTypeInvalid
}
return routingInfo.bktType
}
// NumReplicas returns the number of replicas configured on the
// connected cluster.
func (agent *Agent) NumReplicas() int {
routingInfo := agent.routingInfo.Get()
if routingInfo == nil {
return 0
}
if routingInfo.vbMap == nil {
return 0
}
return routingInfo.vbMap.NumReplicas()
}
// NumServers returns the number of servers accessible for K/V.
func (agent *Agent) NumServers() int {
routingInfo := agent.routingInfo.Get()
if routingInfo == nil {
return 0
}
return routingInfo.clientMux.NumPipelines()
}
// TODO(brett19): Update VbucketsOnServer to return all servers.
// Otherwise, we could race the route map update and get a
// non-continuous list of vbuckets for each server.
// VbucketsOnServer returns the list of VBuckets for a server.
func (agent *Agent) VbucketsOnServer(index int) []uint16 {
routingInfo := agent.routingInfo.Get()
if routingInfo == nil {
return nil
}
if routingInfo.vbMap == nil {
return nil
}
vbList := routingInfo.vbMap.VbucketsByServer(0)
if len(vbList) <= index {
// Invalid server index
return nil
}
return vbList[index]
}
// CapiEps returns all the available endpoints for performing
// map-reduce queries.
func (agent *Agent) CapiEps() []string {
routingInfo := agent.routingInfo.Get()
if routingInfo == nil {
return nil
}
return routingInfo.capiEpList
}
// MgmtEps returns all the available endpoints for performing
// management queries.
func (agent *Agent) MgmtEps() []string {
routingInfo := agent.routingInfo.Get()
if routingInfo == nil {
return nil
}
return routingInfo.mgmtEpList
}
// N1qlEps returns all the available endpoints for performing
// N1QL queries.
func (agent *Agent) N1qlEps() []string {
routingInfo := agent.routingInfo.Get()
if routingInfo == nil {
return nil
}
return routingInfo.n1qlEpList
}
// FtsEps returns all the available endpoints for performing
// FTS queries.
func (agent *Agent) FtsEps() []string {
routingInfo := agent.routingInfo.Get()
if routingInfo == nil {
return nil
}
return routingInfo.ftsEpList
}