-
Notifications
You must be signed in to change notification settings - Fork 28
/
grpc_util.go
358 lines (308 loc) · 9.94 KB
/
grpc_util.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
// 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 cbft
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"fmt"
"math"
"math/rand"
"sync"
"sync/atomic"
"time"
pb "github.com/couchbase/cbft/protobuf"
"github.com/couchbase/cbgt"
log "github.com/couchbase/clog"
"github.com/couchbase/cbauth"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
)
// RPCClientConn represent the gRPC client connection cache.
var RPCClientConn map[string][]*grpc.ClientConn
var rpcConnMutex sync.Mutex
// rpc.ClientConn pool size/static connections per remote host
var connPoolSize = 5
var defaultRPCClientCacheSize = 10
// 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
// The number of concurrent streams/requests on a client connection can only
// be controlled by adjusting the server value. Set a very large value for the
// server config so that we have no fixed limit on the number of concurrent
// streams/requests on either the client or server.
var DefaultGrpcMaxConcurrentStreams = uint32(math.MaxInt32)
func init() {
RPCClientConn = make(map[string][]*grpc.ClientConn, defaultRPCClientCacheSize)
rand.Seed(time.Now().UnixNano())
}
// resetGrpcClients is used to reset the existing clients so that
// further getRpcClient calls will get newer clients with the fresh
// and the latest configs.
func resetGrpcClients() error {
rpcConnMutex.Lock()
RPCClientConn = make(map[string][]*grpc.ClientConn, defaultRPCClientCacheSize)
rpcConnMutex.Unlock()
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 {
username string
password string
requireTransportSecurity bool
}
// GetRequestMetadata sets the value for "authorization" key
func (b *basicAuthCreds) GetRequestMetadata(context.Context,
...string) (map[string]string, error) {
return map[string]string{
"authorization": "Basic " + basicAuth(b.username, b.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))
}
func getRpcClient(nodeUUID, hostPort string, certInBytes []byte,
clientCert tls.Certificate, clientAuth tls.ClientAuthType) (
pb.SearchServiceClient, error) {
var hostPool []*grpc.ClientConn
var initialised bool
key := nodeUUID + "-" + hostPort
index := rand.Intn(connPoolSize)
rpcConnMutex.Lock()
if hostPool, initialised = RPCClientConn[key]; !initialised {
opts, err := getGrpcOpts(hostPort, certInBytes, clientCert, clientAuth)
if err != nil {
rpcConnMutex.Unlock()
log.Errorf("grpc_client: getGrpcOpts, host port: %s, err: %v",
hostPort, err)
return nil, err
}
for i := 0; i < connPoolSize; i++ {
conn, err := grpc.Dial(hostPort, opts...)
if err != nil {
rpcConnMutex.Unlock()
log.Errorf("grpc_client: grpc.Dial, err: %v", err)
return nil, err
}
log.Printf("grpc_client: grpc ClientConn Created %d for host: %s", i, key)
RPCClientConn[key] = append(RPCClientConn[key], conn)
}
hostPool = RPCClientConn[key]
}
rpcConnMutex.Unlock()
// TODO connection mgmt
// when to perform explicit conn.Close()?
cli := pb.NewSearchServiceClient(hostPool[index])
if len(certInBytes) == 0 {
atomic.AddUint64(&totRemoteGrpc, 1)
} else {
atomic.AddUint64(&totRemoteGrpcSsl, 1)
}
return cli, nil
}
func getGrpcOpts(hostPort string, certInBytes []byte, clientCert tls.Certificate,
clientAuth tls.ClientAuthType) ([]grpc.DialOption, error) {
cbUser, cbPasswd, err := cbauth.GetHTTPServiceAuth(hostPort)
if err != nil {
return nil, fmt.Errorf("grpc_util: cbauth err: %v", err)
}
bac := &basicAuthCreds{
username: cbUser,
password: cbPasswd,
}
if len(certInBytes) != 0 {
bac.requireTransportSecurity = true
}
opts := []grpc.DialOption{
grpc.WithBackoffMaxDelay(DefaultGrpcMaxBackOffDelay),
grpc.WithKeepaliveParams(keepalive.ClientParameters{
// send keepalive every 60 seconds to check the
// connection livliness
Time: DefaultGrpcConnectionHeartBeatInterval,
// timeout value for an inactive connection
Timeout: DefaultGrpcConnectionIdleTimeout,
}),
grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(DefaultGrpcMaxRecvMsgSize),
grpc.MaxCallSendMsgSize(DefaultGrpcMaxSendMsgSize),
),
grpc.WithPerRPCCredentials(bac),
}
if len(certInBytes) != 0 {
// create a certificate pool from the CA
certPool := x509.NewCertPool()
// append the certificates from the CA
ok := certPool.AppendCertsFromPEM(certInBytes)
if !ok {
return nil, fmt.Errorf("grpc_util: failed to append ca certs")
}
var creds credentials.TransportCredentials
if clientAuth != tls.NoClientCert {
creds = credentials.NewTLS(&tls.Config{
RootCAs: certPool,
Certificates: []tls.Certificate{clientCert},
ClientAuth: clientAuth,
})
} else {
creds = credentials.NewClientTLSFromCert(certPool, "")
}
opts = append(opts, grpc.WithTransportCredentials(creds))
} else {
opts = append(opts, grpc.WithInsecure())
}
return opts, nil
}
// GRPCPathStats represents the stats for a gRPC path spec.
type GRPCPathStats struct {
m sync.Mutex
focusStats map[string]*RPCFocusStats
}
// FocusStats returns the RPCFocusStats for a given focus value like
// an indexName
func (s *GRPCPathStats) FocusStats(focusVal string) *RPCFocusStats {
s.m.Lock()
if s.focusStats == nil {
s.focusStats = map[string]*RPCFocusStats{}
}
rv, exists := s.focusStats[focusVal]
if !exists {
rv = &RPCFocusStats{}
s.focusStats[focusVal] = rv
}
s.m.Unlock()
return rv
}
func (s *GRPCPathStats) ResetFocusStats(focusVal string) {
s.m.Lock()
if s.focusStats != nil {
if _, exists := s.focusStats[focusVal]; exists {
s.focusStats[focusVal] = &RPCFocusStats{}
}
}
s.m.Unlock()
}
// FocusValues returns the focus value strings
func (s *GRPCPathStats) FocusValues() (rv []string) {
s.m.Lock()
for focusVal := range s.focusStats {
rv = append(rv, focusVal)
}
s.m.Unlock()
return rv
}
// GrpcIndexQueryPath is keyed by path spec strings.
var GrpcPathStats = GRPCPathStats{
focusStats: map[string]*RPCFocusStats{},
}
// -------------------------------------------------------
// RPCFocusStats represents stats for a targeted or "focused" gRPC
// endpoint.
type RPCFocusStats struct {
TotGrpcRequest uint64
TotGrpcRequestTimeNS uint64
TotGrpcRequestErr uint64 `json:"TotGrpcRequestErr,omitempty"`
TotGrpcRequestSlow uint64 `json:"TotGrpcRequestSlow,omitempty"`
TotGrpcRequestTimeout uint64 `json:"TotGrpcRequestTimeout,omitempty"`
TotGrpcResponseBytes uint64 `json:"TotGrpcResponseBytes,omitempty"`
TotGrpcInternalRequest uint64
TotGrpcInternalRequestTimeNS uint64
}
func updateRpcFocusStats(startTime time.Time, mgr *cbgt.Manager,
req *pb.SearchRequest, ctx context.Context, err error) {
focusStats := GrpcPathStats.FocusStats(req.IndexName)
if focusStats != nil {
// check whether its a client request and track only in the coordinating node
if _, er := extractMetaHeader(ctx, rpcClusterActionKey); er != nil {
// co-ordinating node
atomic.AddUint64(&focusStats.TotGrpcRequest, 1)
atomic.AddUint64(&focusStats.TotGrpcRequestTimeNS,
uint64(time.Now().Sub(startTime)))
slowQueryLogTimeoutV := mgr.GetOption("slowQueryLogTimeout")
if slowQueryLogTimeoutV != "" {
var slowQueryLogTimeout time.Duration
slowQueryLogTimeout, err = time.ParseDuration(slowQueryLogTimeoutV)
if err == nil {
d := time.Since(startTime)
if d > slowQueryLogTimeout {
log.Warnf("grpc_util: slow-query index: %s,"+
" query: %s, duration: %v, err: %v",
req.IndexName, string(req.Contents), d, err)
atomic.AddUint64(&focusStats.TotGrpcRequestSlow, 1)
}
}
}
if err != nil {
atomic.AddUint64(&focusStats.TotGrpcRequestErr, 1)
if err == context.DeadlineExceeded {
atomic.AddUint64(&focusStats.TotGrpcRequestTimeout, 1)
}
}
} else {
// not a co-ordinating node
atomic.AddUint64(&focusStats.TotGrpcInternalRequest, 1)
atomic.AddUint64(&focusStats.TotGrpcInternalRequestTimeNS,
uint64(time.Now().Sub(startTime)))
}
}
}
// httpStatusCodes map the canonical gRPC codes to HTTP status codes
// as per https://github.com/grpc/grpc/blob/master/doc/statuscodes.md
func httpStatusCodes(code codes.Code) int {
switch code {
case codes.OK:
return 200
case codes.Canceled:
return 499
case codes.Unknown:
return 500
case codes.InvalidArgument:
return 400
case codes.DeadlineExceeded:
return 504
case codes.NotFound:
return 404
case codes.AlreadyExists:
return 409
case codes.PermissionDenied:
return 403
case codes.ResourceExhausted:
return 429
case codes.FailedPrecondition:
return 412 // custom override for fts
case codes.Aborted:
return 409
case codes.OutOfRange:
return 400
case codes.Unimplemented:
return 501
case codes.Internal:
return 500
case codes.Unavailable:
return 503
case codes.DataLoss:
return 500
case codes.Unauthenticated:
return 401
default:
return 500
}
}