forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
360 lines (322 loc) · 10.5 KB
/
conn.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
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package grpctabletconn
import (
"fmt"
"io"
"sync"
"time"
mproto "github.com/youtube/vitess/go/mysql/proto"
"github.com/youtube/vitess/go/netutil"
"github.com/youtube/vitess/go/vt/callerid"
tproto "github.com/youtube/vitess/go/vt/tabletserver/proto"
"github.com/youtube/vitess/go/vt/tabletserver/tabletconn"
"golang.org/x/net/context"
"google.golang.org/grpc"
pb "github.com/youtube/vitess/go/vt/proto/query"
pbs "github.com/youtube/vitess/go/vt/proto/queryservice"
pbt "github.com/youtube/vitess/go/vt/proto/topodata"
)
const protocolName = "grpc"
func init() {
tabletconn.RegisterDialer(protocolName, DialTablet)
}
// gRPCQueryClient implements a gRPC implementation for TabletConn
type gRPCQueryClient struct {
// endPoint is set at construction time, and never changed
endPoint *pbt.EndPoint
// mu protects the next fields
mu sync.RWMutex
cc *grpc.ClientConn
c pbs.QueryClient
sessionID int64
target *pb.Target
}
// DialTablet creates and initializes gRPCQueryClient.
func DialTablet(ctx context.Context, endPoint *pbt.EndPoint, keyspace, shard string, tabletType pbt.TabletType, timeout time.Duration) (tabletconn.TabletConn, error) {
// create the RPC client
addr := netutil.JoinHostPort(endPoint.Host, endPoint.PortMap["grpc"])
cc, err := grpc.Dial(addr, grpc.WithInsecure(), grpc.WithBlock(), grpc.WithTimeout(timeout))
if err != nil {
return nil, err
}
c := pbs.NewQueryClient(cc)
result := &gRPCQueryClient{
endPoint: endPoint,
cc: cc,
c: c,
}
if tabletType == pbt.TabletType_UNKNOWN {
// we use session
gsir, err := c.GetSessionId(ctx, &pb.GetSessionIdRequest{
Keyspace: keyspace,
Shard: shard,
})
if err != nil {
cc.Close()
return nil, err
}
result.sessionID = gsir.SessionId
} else {
// we use target
result.target = &pb.Target{
Keyspace: keyspace,
Shard: shard,
TabletType: tabletType,
}
}
return result, nil
}
// Execute sends the query to VTTablet.
func (conn *gRPCQueryClient) Execute(ctx context.Context, query string, bindVars map[string]interface{}, transactionID int64) (*mproto.QueryResult, error) {
conn.mu.RLock()
defer conn.mu.RUnlock()
if conn.cc == nil {
return nil, tabletconn.ConnClosed
}
req := &pb.ExecuteRequest{
Target: conn.target,
EffectiveCallerId: callerid.EffectiveCallerIDFromContext(ctx),
ImmediateCallerId: callerid.ImmediateCallerIDFromContext(ctx),
Query: tproto.BoundQueryToProto3(query, bindVars),
TransactionId: transactionID,
SessionId: conn.sessionID,
}
er, err := conn.c.Execute(ctx, req)
if err != nil {
return nil, tabletconn.TabletErrorFromGRPC(err)
}
return mproto.Proto3ToQueryResult(er.Result), nil
}
// Execute2 is the same with Execute in gRPC, since Execute is already CallerID enabled
func (conn *gRPCQueryClient) Execute2(ctx context.Context, query string, bindVars map[string]interface{}, transactionID int64) (*mproto.QueryResult, error) {
return conn.Execute(ctx, query, bindVars, transactionID)
}
// ExecuteBatch sends a batch query to VTTablet.
func (conn *gRPCQueryClient) ExecuteBatch(ctx context.Context, queries []tproto.BoundQuery, asTransaction bool, transactionID int64) (*tproto.QueryResultList, error) {
conn.mu.RLock()
defer conn.mu.RUnlock()
if conn.cc == nil {
return nil, tabletconn.ConnClosed
}
req := &pb.ExecuteBatchRequest{
Target: conn.target,
EffectiveCallerId: callerid.EffectiveCallerIDFromContext(ctx),
ImmediateCallerId: callerid.ImmediateCallerIDFromContext(ctx),
Queries: make([]*pb.BoundQuery, len(queries)),
AsTransaction: asTransaction,
TransactionId: transactionID,
SessionId: conn.sessionID,
}
for i, q := range queries {
req.Queries[i] = tproto.BoundQueryToProto3(q.Sql, q.BindVariables)
}
ebr, err := conn.c.ExecuteBatch(ctx, req)
if err != nil {
return nil, tabletconn.TabletErrorFromGRPC(err)
}
return tproto.Proto3ToQueryResultList(ebr.Results), nil
}
// ExecuteBatch2 is the same with ExecuteBatch in gRPC, which is already CallerID enabled
func (conn *gRPCQueryClient) ExecuteBatch2(ctx context.Context, queries []tproto.BoundQuery, asTransaction bool, transactionID int64) (*tproto.QueryResultList, error) {
return conn.ExecuteBatch(ctx, queries, asTransaction, transactionID)
}
// StreamExecute starts a streaming query to VTTablet.
func (conn *gRPCQueryClient) StreamExecute(ctx context.Context, query string, bindVars map[string]interface{}, transactionID int64) (<-chan *mproto.QueryResult, tabletconn.ErrFunc, error) {
conn.mu.RLock()
defer conn.mu.RUnlock()
if conn.cc == nil {
return nil, nil, tabletconn.ConnClosed
}
req := &pb.StreamExecuteRequest{
Target: conn.target,
EffectiveCallerId: callerid.EffectiveCallerIDFromContext(ctx),
ImmediateCallerId: callerid.ImmediateCallerIDFromContext(ctx),
Query: tproto.BoundQueryToProto3(query, bindVars),
SessionId: conn.sessionID,
}
stream, err := conn.c.StreamExecute(ctx, req)
if err != nil {
return nil, nil, tabletconn.TabletErrorFromGRPC(err)
}
sr := make(chan *mproto.QueryResult, 10)
var finalError error
go func() {
for {
ser, err := stream.Recv()
if err != nil {
if err != io.EOF {
finalError = tabletconn.TabletErrorFromGRPC(err)
}
close(sr)
return
}
sr <- mproto.Proto3ToQueryResult(ser.Result)
}
}()
return sr, func() error {
return finalError
}, nil
}
// StreamExecute2 is the same as StreamExecute for gRPC
func (conn *gRPCQueryClient) StreamExecute2(ctx context.Context, query string, bindVars map[string]interface{}, transactionID int64) (<-chan *mproto.QueryResult, tabletconn.ErrFunc, error) {
return conn.StreamExecute(ctx, query, bindVars, transactionID)
}
// Begin starts a transaction.
func (conn *gRPCQueryClient) Begin(ctx context.Context) (transactionID int64, err error) {
conn.mu.RLock()
defer conn.mu.RUnlock()
if conn.cc == nil {
return 0, tabletconn.ConnClosed
}
req := &pb.BeginRequest{
Target: conn.target,
EffectiveCallerId: callerid.EffectiveCallerIDFromContext(ctx),
ImmediateCallerId: callerid.ImmediateCallerIDFromContext(ctx),
SessionId: conn.sessionID,
}
br, err := conn.c.Begin(ctx, req)
if err != nil {
return 0, tabletconn.TabletErrorFromGRPC(err)
}
return br.TransactionId, nil
}
// Begin2 is the same as Begin for gRPC
func (conn *gRPCQueryClient) Begin2(ctx context.Context) (transactionID int64, err error) {
return conn.Begin(ctx)
}
// Commit commits the ongoing transaction.
func (conn *gRPCQueryClient) Commit(ctx context.Context, transactionID int64) error {
conn.mu.RLock()
defer conn.mu.RUnlock()
if conn.cc == nil {
return tabletconn.ConnClosed
}
req := &pb.CommitRequest{
Target: conn.target,
EffectiveCallerId: callerid.EffectiveCallerIDFromContext(ctx),
ImmediateCallerId: callerid.ImmediateCallerIDFromContext(ctx),
TransactionId: transactionID,
SessionId: conn.sessionID,
}
_, err := conn.c.Commit(ctx, req)
if err != nil {
return tabletconn.TabletErrorFromGRPC(err)
}
return nil
}
// Commit2 is the same as Commit for gRPC
func (conn *gRPCQueryClient) Commit2(ctx context.Context, transactionID int64) error {
return conn.Commit(ctx, transactionID)
}
// Rollback rolls back the ongoing transaction.
func (conn *gRPCQueryClient) Rollback(ctx context.Context, transactionID int64) error {
conn.mu.RLock()
defer conn.mu.RUnlock()
if conn.cc == nil {
return tabletconn.ConnClosed
}
req := &pb.RollbackRequest{
Target: conn.target,
EffectiveCallerId: callerid.EffectiveCallerIDFromContext(ctx),
ImmediateCallerId: callerid.ImmediateCallerIDFromContext(ctx),
TransactionId: transactionID,
SessionId: conn.sessionID,
}
_, err := conn.c.Rollback(ctx, req)
if err != nil {
return tabletconn.TabletErrorFromGRPC(err)
}
return nil
}
// Rollback2 is the same as Rollback for gRPC
func (conn *gRPCQueryClient) Rollback2(ctx context.Context, transactionID int64) error {
return conn.Rollback(ctx, transactionID)
}
// SplitQuery is the stub for TabletServer.SplitQuery RPC
func (conn *gRPCQueryClient) SplitQuery(ctx context.Context, query tproto.BoundQuery, splitColumn string, splitCount int) (queries []tproto.QuerySplit, err error) {
conn.mu.RLock()
defer conn.mu.RUnlock()
if conn.cc == nil {
err = tabletconn.ConnClosed
return
}
req := &pb.SplitQueryRequest{
Target: conn.target,
EffectiveCallerId: callerid.EffectiveCallerIDFromContext(ctx),
ImmediateCallerId: callerid.ImmediateCallerIDFromContext(ctx),
Query: tproto.BoundQueryToProto3(query.Sql, query.BindVariables),
SplitColumn: splitColumn,
SplitCount: int64(splitCount),
SessionId: conn.sessionID,
}
sqr, err := conn.c.SplitQuery(ctx, req)
if err != nil {
return nil, tabletconn.TabletErrorFromGRPC(err)
}
return tproto.Proto3ToQuerySplits(sqr.Queries), nil
}
// StreamHealth is the stub for TabletServer.StreamHealth RPC
func (conn *gRPCQueryClient) StreamHealth(ctx context.Context) (<-chan *pb.StreamHealthResponse, tabletconn.ErrFunc, error) {
conn.mu.RLock()
defer conn.mu.RUnlock()
if conn.cc == nil {
return nil, nil, tabletconn.ConnClosed
}
result := make(chan *pb.StreamHealthResponse, 10)
stream, err := conn.c.StreamHealth(ctx, &pb.StreamHealthRequest{})
if err != nil {
return nil, nil, err
}
var finalErr error
go func() {
for {
hsr, err := stream.Recv()
if err != nil {
if err != io.EOF {
finalErr = err
}
close(result)
return
}
result <- hsr
}
}()
return result, func() error {
return finalErr
}, nil
}
// Close closes underlying bsonrpc.
func (conn *gRPCQueryClient) Close() {
conn.mu.Lock()
defer conn.mu.Unlock()
if conn.cc == nil {
return
}
conn.sessionID = 0
cc := conn.cc
conn.cc = nil
cc.Close()
}
// SetTarget can be called to change the target used for subsequent calls.
func (conn *gRPCQueryClient) SetTarget(keyspace, shard string, tabletType pbt.TabletType) error {
conn.mu.Lock()
defer conn.mu.Unlock()
if conn.target == nil {
return fmt.Errorf("cannot set target on sessionId based conn")
}
if tabletType == pbt.TabletType_UNKNOWN {
return fmt.Errorf("cannot set tablet type to UNKNOWN")
}
conn.target = &pb.Target{
Keyspace: keyspace,
Shard: shard,
TabletType: tabletType,
}
return nil
}
// EndPoint returns the rpc end point.
func (conn *gRPCQueryClient) EndPoint() *pbt.EndPoint {
return conn.endPoint
}