-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
618 lines (513 loc) · 17.1 KB
/
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
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
// Copyright 2019-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included in
// the file licenses/BSL-Couchbase.txt. As of the Change Date specified in that
// file, in accordance with the Business Source License, use of this software
// will be governed by the Apache License, Version 2.0, included in the file
// licenses/APL2.txt.
package n1fty
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"fmt"
"math/rand"
"sync"
"sync/atomic"
"time"
"unsafe"
"github.com/couchbase/cbauth"
"github.com/couchbase/cbgt"
"github.com/couchbase/query/logging"
pb "github.com/couchbase/cbft/protobuf"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
)
// CBAUTH security/encryption config
type securityConfig struct {
encryptionEnabled bool
disableNonSSLPorts bool
certificate *tls.Certificate
clientAuthType *tls.ClientAuthType
clientCertificate *tls.Certificate
certInBytes []byte
tlsPreference *cbauth.TLSConfig
}
var secConfig = unsafe.Pointer(new(securityConfig))
func loadSecurityConfig() *securityConfig {
return (*securityConfig)(atomic.LoadPointer(&secConfig))
}
func updateSecurityConfig(sc *securityConfig) {
atomic.StorePointer(&secConfig, unsafe.Pointer(sc))
}
// default values same as that for http/rest connections
var DefaultGrpcConnectionIdleTimeout = time.Duration(60) * time.Second
var DefaultGrpcConnectionHeartBeatInterval = time.Duration(60) * time.Second
var DefaultGrpcMaxBackOffDelay = time.Duration(10) * time.Second
var DefaultGrpcMaxRecvMsgSize = 1024 * 1024 * 50 // 50 MB
var DefaultGrpcMaxSendMsgSize = 1024 * 1024 * 50 // 50 MB
// ErrFeatureUnavailable indicates the feature unavailability in cluster
var ErrFeatureUnavailable = fmt.Errorf("feature unavailable in cluster")
type ftsClient struct {
m sync.RWMutex // Protects the fields that follow.
// cache the list of host:port info for all the fts nodes in the cluster.
// Useful for reconcilation during topology change.
hosts []string
sslHostsAvailable bool // are there any grpc-ssl fts hosts in the cluster?
nonSslHostsAvailable bool // are there any grpc-non-ssl fts hosts in the cluster?
// cache the grpc.DialOption(s) for all the servers
// access to methods of grpcOpts is protected by ftsClient.m
connOpts *grpcOpts
// Depends on sslHostsAvailable, nonSslHostsAvailable and the security config
// params (like: encryptionEnabled, disabledNonSSLPorts)
//
// connType is __nilConnType when there are no fts nodes in the cluster.
// or, fts nodes are not able to satisfy the security config params requirement.
//
// Updated on TopologyChange and SecurityConfigChange events
connType connectionType
// A connPool per fts node
connPool map[string]*connPool
}
type connectionType int
const (
sslConn connectionType = iota
nonSslConn
__nilConnType
)
type connPoolUpdateEvent int
const (
// includes changes to cluster encryption settings and/or certs tls config
// settings
securityConfigChange connPoolUpdateEvent = iota
// tied to cbgt.Cfg notification corresponding to NodeDefsKnown key.
// includes addition/removal of fts nodes in the cluster
topologyChange
// signalled when indexers count goes from 0 to 1 and vice-versa
indexerAvailabilityChange
)
// event tags
var eventTags = map[connPoolUpdateEvent]string{
securityConfigChange: "SecurityConfigChange",
topologyChange: "TopologyChange",
indexerAvailabilityChange: "IndexerAvailabilityChange",
}
// Instance of ftsClient
var ftsClientInst *ftsClient
func init() {
rand.Seed(time.Now().UnixNano())
ftsClientInst = &ftsClient{
connPool: make(map[string]*connPool),
connType: __nilConnType,
}
ftsClientInst.connOpts = newGrpcOpts(ftsClientInst)
}
func (c *ftsClient) resetLOCKED() {
c.hosts = nil
c.sslHostsAvailable = false
c.nonSslHostsAvailable = false
c.connType = __nilConnType
c.connOpts.reset()
// close all conn pools
for host, _ := range c.connPool {
c.connPool[host].close("Reset")
delete(c.connPool, host)
}
}
func (c *ftsClient) updateConnTypeLOCKED() {
secConfig := loadSecurityConfig()
if secConfig.encryptionEnabled && c.sslHostsAvailable {
c.connType = sslConn
} else if !secConfig.disableNonSSLPorts && c.nonSslHostsAvailable {
c.connType = nonSslConn
} else {
logging.Errorf("n1fty: hosts unable to satisfy the security config "+
"{encryptionEnabled: %v, disableNonSSLPorts: %v}",
secConfig.encryptionEnabled, secConfig.disableNonSSLPorts)
c.connType = __nilConnType
}
}
// useful for updating connection options and/or creating new connection pools
func (c *ftsClient) createConnPoolsLOCKED(hosts []string,
updateConnOpts, createPools bool) error {
if updateConnOpts {
// create/update dial options for hosts
err := c.connOpts.updateHostOpts(hosts)
if err != nil {
c.resetLOCKED()
return fmt.Errorf("failed to update grpc opts for hosts, err: %v", err)
}
}
if createPools {
for _, host := range hosts {
c.connPool[host] = newConnPool(host)
}
}
return nil
}
// useful for cleaning up connection options and/or closing connection pools
func (c *ftsClient) closeConnPoolsLOCKED(hosts []string, reason string,
cleanupConnOpts bool, deletePools bool) {
if cleanupConnOpts {
c.connOpts.delete(hosts)
}
if deletePools {
for _, host := range hosts {
if connPool, ok := c.connPool[host]; ok {
connPool.close(reason)
delete(c.connPool, host)
}
}
}
}
func (c *ftsClient) updateConnPools(event connPoolUpdateEvent) error {
c.m.Lock()
defer c.m.Unlock()
return c.updateConnPoolsLOCKED(event)
}
func (c *ftsClient) updateConnPoolsLOCKED(event connPoolUpdateEvent) error {
logging.Infof("n1fty: updateConnPools start, event:%v", eventTags[event])
defer func() {
logging.Infof("n1fty: updateConnPools done, hosts:%v", c.hosts)
}()
switch event {
case securityConfigChange:
return c.onSecurityConfigChange()
case topologyChange:
return c.onTopologyChange()
case indexerAvailabilityChange:
c.onIndexerAvailabilityChange()
default:
return fmt.Errorf("unknown event")
}
return nil
}
func (c *ftsClient) onSecurityConfigChange() error {
indexerAvail := indexersExist()
logging.Infof("n1fty: onSecurityConfigChange, indexerAvail:%v", indexerAvail)
if len(c.hosts) == 0 {
return nil
}
c.updateConnTypeLOCKED()
if c.connType == __nilConnType {
c.resetLOCKED()
return fmt.Errorf("nil connection type")
}
err := c.connOpts.updateCommonOpts()
if err != nil {
return fmt.Errorf("failed to update common grpc options, err: %v", err)
}
// # Update Connection Pools
// SecurityConfigChange is involved in the book-keeping of connection options.
// Whether to materialize conn pool creation/deletion is decided by the
// indexer availability.
//
// Close existing stale connection pools and create new ones.
c.closeConnPoolsLOCKED(c.hosts, eventTags[securityConfigChange], true, indexerAvail)
if err := c.createConnPoolsLOCKED(c.hosts, true, indexerAvail); err != nil {
c.resetLOCKED()
return fmt.Errorf("failed to create connection pools, err: %v", err)
}
return nil
}
func (c *ftsClient) onTopologyChange() error {
indexerAvail := indexersExist()
logging.Infof("n1fty: onTopologyChange, indexerAvail:%v", indexerAvail)
// ## Fetch new topology and reconcile with existing topology
nodeDefs, err := GetNodeDefs(srvConfig)
if err != nil {
c.resetLOCKED()
return fmt.Errorf("GetNodeDefs failed, err: %v", err)
}
if nodeDefs == nil || len(nodeDefs.NodeDefs) == 0 {
c.resetLOCKED()
return fmt.Errorf("GetNodeDefs, err: hosts unavailable")
}
nonSslHosts, sslHosts := extractHosts(nodeDefs)
// no fts nodes in the cluster support grpc
if len(nonSslHosts) == 0 && len(sslHosts) == 0 {
c.resetLOCKED()
return ErrFeatureUnavailable
}
c.sslHostsAvailable = len(sslHosts) != 0
c.nonSslHostsAvailable = len(nonSslHosts) != 0
c.updateConnTypeLOCKED()
if c.connType == __nilConnType {
c.resetLOCKED()
return fmt.Errorf("nil connection type")
}
var newHosts []string
if c.connType == sslConn {
newHosts = sslHosts
} else {
newHosts = nonSslHosts
}
// reconcile topology
keepHosts := cbgt.StringsIntersectStrings(c.hosts, newHosts)
removedHosts := cbgt.StringsRemoveStrings(c.hosts, newHosts)
addedHosts := cbgt.StringsRemoveStrings(newHosts, keepHosts)
logging.Infof("n1fty: onTopologyChange, keepHosts:%v, removedHosts:%v, "+
"addedHosts:%v", keepHosts, removedHosts, addedHosts)
// update hosts
c.hosts = newHosts
// # Update Conn Pools
// Topology Change is involved in the book-keeping of connection options.
// Whether to materialize conn pool creation/deletion is decided by the
// indexer availability.
//
// close pools corresponding to removed hosts and create new ones for added hosts.
c.closeConnPoolsLOCKED(removedHosts, eventTags[topologyChange], true,
indexerAvail)
if err := c.createConnPoolsLOCKED(addedHosts, true, indexerAvail); err != nil {
c.resetLOCKED()
return fmt.Errorf("failed to create connection pools, err: %v", err)
}
return nil
}
// Handler for indexer availability change event.
// The mutex n1fty.monitor.m is expected to be locked for the entire duration of this
// function execution. This is to handle multiple concurrent indexer availability
// change events.
func (c *ftsClient) onIndexerAvailabilityChange() {
indexerAvail := indexersExist()
logging.Infof("n1fty: onIndexerAvailabilityChange, indexer available:%v",
indexerAvail)
// # Update Conn Pools
// We don't need to update the connection options here, as book-keeping of it
// is already done in onTopologyChange and onSecurityConfigChange.
//
// On IndexerAvailabilityChange, we bring-up/tear-down the connection pools.
if indexerAvail { // indexers count went from 0 -> 1
c.createConnPoolsLOCKED(c.hosts, false, true)
} else { // indexers count goes went 1 -> 0
c.closeConnPoolsLOCKED(c.hosts, eventTags[indexerAvailabilityChange],
false, true)
}
}
func (c *ftsClient) getGrpcClient() (
pb.SearchServiceClient, *connPool, *grpc.ClientConn) {
c.m.RLock()
defer c.m.RUnlock()
if len(c.hosts) == 0 {
logging.Errorf("n1fty: getGrpcClient, err: search host unavailable")
return nil, nil, nil
}
// randomly pick a search host
hostIdx := rand.Intn(len(c.hosts))
connPool, ok := c.connPool[c.hosts[hostIdx]]
if !ok {
logging.Errorf("n1fty: getGrpcClient, err: no connection pool exist for %v",
c.hosts[hostIdx])
return nil, nil, nil
}
conn, err := connPool.get()
if err != nil {
logging.Errorf("n1fty: getGrpcClient, err: failed to get a connection "+
"object from pool:%v, error:%v", c.hosts[hostIdx], err)
return nil, nil, nil
}
return pb.NewSearchServiceClient(conn), connPool, conn
}
// -----------------------------------------------------------------------------
// for concurrent access use under client.m lock.
type grpcOpts struct {
client *ftsClient
// common dial options for all the hosts
commonOpts []grpc.DialOption
// commonOpts + host specific options
hostOpts map[string][]grpc.DialOption
}
func newGrpcOpts(client *ftsClient) *grpcOpts {
return &grpcOpts{
hostOpts: make(map[string][]grpc.DialOption),
client: client,
}
}
func (g *grpcOpts) reset() {
g.commonOpts = nil
g.hostOpts = make(map[string][]grpc.DialOption)
}
func (g *grpcOpts) delete(hosts []string) {
if hosts == nil || len(hosts) == 0 {
return
}
for _, host := range hosts {
delete(g.hostOpts, host)
}
}
func (g *grpcOpts) get(host string) ([]grpc.DialOption, bool) {
opts, ok := g.hostOpts[host]
return opts, ok
}
// update dial options common to all the hosts.
// handles security config changes.
func (g *grpcOpts) updateCommonOpts() error {
secConfig := loadSecurityConfig()
connType := g.client.connType
if connType == __nilConnType {
return fmt.Errorf("nil connection type")
}
gRPCOpts := []grpc.DialOption{
grpc.WithBackoffMaxDelay(DefaultGrpcMaxBackOffDelay),
grpc.WithKeepaliveParams(keepalive.ClientParameters{
// send keepalive every N seconds to check the
// connection liveliness
Time: DefaultGrpcConnectionHeartBeatInterval,
// client waits for a duration of timeout
Timeout: DefaultGrpcConnectionIdleTimeout,
PermitWithoutStream: true,
}),
grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(DefaultGrpcMaxRecvMsgSize),
grpc.MaxCallSendMsgSize(DefaultGrpcMaxSendMsgSize),
),
}
if g.client.connType == sslConn {
certPool := x509.NewCertPool()
ok := certPool.AppendCertsFromPEM(secConfig.certInBytes)
if !ok {
return fmt.Errorf("client: failed to append ca certs")
}
var cred credentials.TransportCredentials
if secConfig.clientAuthType != nil &&
*secConfig.clientAuthType != tls.NoClientCert {
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{*secConfig.clientCertificate},
ClientAuth: *secConfig.clientAuthType,
RootCAs: certPool,
}
cred = credentials.NewTLS(tlsConfig)
} else {
cred = credentials.NewClientTLSFromCert(certPool, "")
}
gRPCOpts = append(gRPCOpts, grpc.WithTransportCredentials(cred))
} else { // non-ssl
gRPCOpts = append(gRPCOpts, grpc.WithInsecure())
}
g.commonOpts = gRPCOpts
return nil
}
// This function update(or create) grpc.DialOption(s) for given hosts
// and cache them
func (g *grpcOpts) updateHostOpts(hosts []string) error {
if len(hosts) == 0 {
return nil
}
if g.client.connType == __nilConnType {
return fmt.Errorf("nil connection type")
}
if len(g.commonOpts) == 0 {
err := g.updateCommonOpts()
if err != nil {
return fmt.Errorf("failed to update common grpc options, err: %v", err)
}
}
secConfig := loadSecurityConfig()
for _, host := range hosts {
g.hostOpts[host] = make([]grpc.DialOption, len(g.commonOpts))
copy(g.hostOpts[host], g.commonOpts)
g.hostOpts[host] = append(g.hostOpts[host],
grpc.WithPerRPCCredentials(&basicAuthCreds{
host: host,
requireTransportSecurity: secConfig.encryptionEnabled,
}))
}
return nil
}
// -----------------------------------------------------------------------------
// basicAuthCreds is an implementation of credentials.PerRPCCredentials
// that transforms the username and password into a base64 encoded value
// similar to HTTP Basic xxx
type basicAuthCreds struct {
host string // host:port
requireTransportSecurity bool
}
// GetRequestMetadata sets the value for "authorization" key
func (b *basicAuthCreds) GetRequestMetadata(context.Context, ...string) (
map[string]string, error) {
username, password, err := cbauth.GetHTTPServiceAuth(b.host)
if err != nil {
logging.Warnf("n1fty: GetRequestMetadata, err: %v", err)
return nil, err
}
return map[string]string{
"authorization": "Basic " + basicAuth(username, password),
}, nil
}
// RequireTransportSecurity indicates whether the credentials requires
// transport security.
func (b *basicAuthCreds) RequireTransportSecurity() bool {
return b.requireTransportSecurity
}
func basicAuth(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
}
// ------------------------------Utility Functions------------------------------
func extractHosts(nodeDefs *cbgt.NodeDefs) ([]string, []string) {
nonSslHosts := []string{}
sslHosts := []string{}
for _, v := range nodeDefs.NodeDefs {
var grpcFeatureSupport bool
extrasBindGRPC, err := v.GetFromParsedExtras("bindGRPC")
if err == nil && extrasBindGRPC != nil {
if bindGRPCstr, ok := extrasBindGRPC.(string); ok {
if bindGRPCstr != "" {
nonSslHosts = append(nonSslHosts, bindGRPCstr)
grpcFeatureSupport = true
}
}
}
extrasBindGRPCSSL, err := v.GetFromParsedExtras("bindGRPCSSL")
if err == nil && extrasBindGRPCSSL != nil {
if bindGRPCSSLstr, ok := extrasBindGRPCSSL.(string); ok {
if bindGRPCSSLstr != "" {
sslHosts = append(sslHosts, bindGRPCSSLstr)
grpcFeatureSupport = true
}
}
}
// if any node in the cluster doesn't support gRPC then fail right away
if !grpcFeatureSupport {
return nil, nil
}
}
return nonSslHosts, sslHosts
}
func indexersExist() bool {
numIndexers := mr.indexerCount()
return numIndexers != 0
}
func getTopologyAndConnType() ([]string, connectionType, error) {
nodeDefs, err := GetNodeDefs(srvConfig)
if err != nil {
logging.Errorf("n1fty: GetNodeDefs, err: %v", err)
return nil, __nilConnType, fmt.Errorf("GetNodeDefs failed [err: %v]", err)
}
if nodeDefs == nil || len(nodeDefs.NodeDefs) == 0 {
return nil, __nilConnType, fmt.Errorf("hosts (ssl/non-ssl) unavailable")
}
hosts, sslHosts := extractHosts(nodeDefs)
if len(hosts) == 0 && len(sslHosts) == 0 {
return nil, __nilConnType, ErrFeatureUnavailable
}
var newTopology []string
var connType connectionType
secConfig := loadSecurityConfig()
if secConfig.encryptionEnabled && len(sslHosts) > 0 {
newTopology = sslHosts
connType = sslConn
} else if !secConfig.disableNonSSLPorts && len(hosts) > 0 {
newTopology = hosts // non-ssl hosts
connType = nonSslConn
} else {
logging.Warnf("n1fty: client: hosts error, config: {encryptionEnabled:"+
" %v, disableNonSSLPorts: %v}, hosts: %v, sslHosts: %v",
secConfig.encryptionEnabled, secConfig.disableNonSSLPorts,
hosts, sslHosts)
return nil, __nilConnType, fmt.Errorf("hosts (ssl/non-ssl) unavailable")
}
return newTopology, connType, nil
}