forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
445 lines (401 loc) · 15.2 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
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
// Copyright 2015, 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 fakerpcvtgateconn provides a fake implementation of
// vtgateconn.Impl that doesn't do any RPC, but uses a local
// map to return results.
package fakerpcvtgateconn
import (
"errors"
"fmt"
"io"
"math/rand"
"reflect"
"sort"
"strings"
"time"
"github.com/youtube/vitess/go/sqltypes"
"github.com/youtube/vitess/go/vt/vtgate/vtgateconn"
"golang.org/x/net/context"
querypb "github.com/youtube/vitess/go/vt/proto/query"
topodatapb "github.com/youtube/vitess/go/vt/proto/topodata"
vtgatepb "github.com/youtube/vitess/go/vt/proto/vtgate"
)
// queryExecute contains all the fields we use to test Execute
type queryExecute struct {
SQL string
BindVariables map[string]interface{}
Keyspace string
TabletType topodatapb.TabletType
Session *vtgatepb.Session
NotInTransaction bool
}
// queryExecuteShards contains all the fields we use to test ExecuteShards
type queryExecuteShards struct {
SQL string
BindVariables map[string]interface{}
Keyspace string
Shards []string
TabletType topodatapb.TabletType
Session *vtgatepb.Session
NotInTransaction bool
}
type queryResponse struct {
execQuery *queryExecute
shardQuery *queryExecuteShards
reply *sqltypes.Result
err error
}
// querySplitQuery contains all the fields we use to test SplitQuery
type querySplitQuery struct {
Keyspace string
SQL string
BindVariables map[string]interface{}
SplitColumns []string
SplitCount int64
NumRowsPerQueryPart int64
Algorithm querypb.SplitQueryRequest_Algorithm
}
type splitQueryResponse struct {
splitQuery *querySplitQuery
reply []*vtgatepb.SplitQueryResponse_Part
err error
}
// FakeVTGateConn provides a fake implementation of vtgateconn.Impl
type FakeVTGateConn struct {
execMap map[string]*queryResponse
splitQueryMap map[string]*splitQueryResponse
}
// RegisterFakeVTGateConnDialer registers the proper dialer for this fake,
// and returns the underlying instance that will be returned by the dialer,
// and the protocol to use to get this fake.
func RegisterFakeVTGateConnDialer() (*FakeVTGateConn, string) {
protocol := "fake"
impl := &FakeVTGateConn{
execMap: make(map[string]*queryResponse),
splitQueryMap: make(map[string]*splitQueryResponse),
}
vtgateconn.RegisterDialer(protocol, func(ctx context.Context, address string, timeout time.Duration) (vtgateconn.Impl, error) {
return impl, nil
})
return impl, protocol
}
// AddQuery adds a query and expected result.
func (conn *FakeVTGateConn) AddQuery(
sql string,
bindVariables map[string]interface{},
tabletType topodatapb.TabletType,
session *vtgatepb.Session,
notInTransaction bool,
expectedResult *sqltypes.Result) {
conn.execMap[sql] = &queryResponse{
execQuery: &queryExecute{
SQL: sql,
BindVariables: bindVariables,
TabletType: tabletType,
Session: session,
NotInTransaction: notInTransaction,
},
reply: expectedResult,
}
}
// AddShardQuery adds a shard query and expected result.
func (conn *FakeVTGateConn) AddShardQuery(
sql string,
bindVariables map[string]interface{},
keyspace string,
shards []string,
tabletType topodatapb.TabletType,
session *vtgatepb.Session,
notInTransaction bool,
expectedResult *sqltypes.Result) {
conn.execMap[getShardQueryKey(sql, shards)] = &queryResponse{
shardQuery: &queryExecuteShards{
SQL: sql,
BindVariables: bindVariables,
Keyspace: keyspace,
Shards: shards,
TabletType: tabletType,
Session: session,
NotInTransaction: notInTransaction,
},
reply: expectedResult,
}
}
// AddSplitQuery adds a split query and expected result.
func (conn *FakeVTGateConn) AddSplitQuery(
keyspace string,
sql string,
bindVariables map[string]interface{},
splitColumns []string,
splitCount int64,
numRowsPerQueryPart int64,
algorithm querypb.SplitQueryRequest_Algorithm,
expectedResult []*vtgatepb.SplitQueryResponse_Part) {
reply := make([]*vtgatepb.SplitQueryResponse_Part, len(expectedResult))
copy(reply, expectedResult)
key := getSplitQueryKey(keyspace, sql, splitColumns, splitCount, numRowsPerQueryPart, algorithm)
conn.splitQueryMap[key] = &splitQueryResponse{
splitQuery: &querySplitQuery{
Keyspace: keyspace,
SQL: sql,
BindVariables: bindVariables,
SplitColumns: splitColumns,
SplitCount: splitCount,
NumRowsPerQueryPart: numRowsPerQueryPart,
Algorithm: algorithm,
},
reply: reply,
err: nil,
}
}
// Execute please see vtgateconn.Impl.Execute
func (conn *FakeVTGateConn) Execute(ctx context.Context, sql string, bindVars map[string]interface{}, keyspace string, tabletType topodatapb.TabletType, session interface{}, options *querypb.ExecuteOptions) (*sqltypes.Result, interface{}, error) {
var s *vtgatepb.Session
if session != nil {
s = session.(*vtgatepb.Session)
}
response, ok := conn.execMap[sql]
if !ok {
return nil, nil, fmt.Errorf("no match for: %s", sql)
}
query := &queryExecute{
SQL: sql,
BindVariables: bindVars,
Keyspace: keyspace,
TabletType: tabletType,
Session: s,
}
if !reflect.DeepEqual(query, response.execQuery) {
return nil, nil, fmt.Errorf(
"Execute: %+v, want %+v", query, response.execQuery)
}
var reply sqltypes.Result
reply = *response.reply
if s != nil {
s = newSession(true, "test_keyspace", []string{}, topodatapb.TabletType_MASTER)
}
return &reply, s, nil
}
// ExecuteShards please see vtgateconn.Impl.ExecuteShard
func (conn *FakeVTGateConn) ExecuteShards(ctx context.Context, sql string, keyspace string, shards []string, bindVars map[string]interface{}, tabletType topodatapb.TabletType, session interface{}, options *querypb.ExecuteOptions) (*sqltypes.Result, interface{}, error) {
var s *vtgatepb.Session
if session != nil {
s = session.(*vtgatepb.Session)
}
response, ok := conn.execMap[getShardQueryKey(sql, shards)]
if !ok {
return nil, nil, fmt.Errorf("no match for: %s", sql)
}
query := &queryExecuteShards{
SQL: sql,
BindVariables: bindVars,
TabletType: tabletType,
Keyspace: keyspace,
Shards: shards,
Session: s,
}
if !reflect.DeepEqual(query, response.shardQuery) {
return nil, nil, fmt.Errorf(
"ExecuteShards: %+v, want %+v", query, response.shardQuery)
}
var reply sqltypes.Result
reply = *response.reply
if s != nil {
s = newSession(true, keyspace, shards, tabletType)
}
return &reply, s, nil
}
// ExecuteKeyspaceIds please see vtgateconn.Impl.ExecuteKeyspaceIds
func (conn *FakeVTGateConn) ExecuteKeyspaceIds(ctx context.Context, query string, keyspace string, keyspaceIds [][]byte, bindVars map[string]interface{}, tabletType topodatapb.TabletType, session interface{}, options *querypb.ExecuteOptions) (*sqltypes.Result, interface{}, error) {
panic("not implemented")
}
// ExecuteKeyRanges please see vtgateconn.Impl.ExecuteKeyRanges
func (conn *FakeVTGateConn) ExecuteKeyRanges(ctx context.Context, query string, keyspace string, keyRanges []*topodatapb.KeyRange, bindVars map[string]interface{}, tabletType topodatapb.TabletType, session interface{}, options *querypb.ExecuteOptions) (*sqltypes.Result, interface{}, error) {
panic("not implemented")
}
// ExecuteEntityIds please see vtgateconn.Impl.ExecuteEntityIds
func (conn *FakeVTGateConn) ExecuteEntityIds(ctx context.Context, query string, keyspace string, entityColumnName string, entityKeyspaceIDs []*vtgatepb.ExecuteEntityIdsRequest_EntityId, bindVars map[string]interface{}, tabletType topodatapb.TabletType, session interface{}, options *querypb.ExecuteOptions) (*sqltypes.Result, interface{}, error) {
panic("not implemented")
}
// ExecuteBatch please see vtgateconn.Impl.ExecuteBatch
func (conn *FakeVTGateConn) ExecuteBatch(ctx context.Context, sqlList []string, bindVarsList []map[string]interface{}, keyspace string, tabletType topodatapb.TabletType, asTransaction bool, session interface{}, options *querypb.ExecuteOptions) ([]sqltypes.QueryResponse, interface{}, error) {
panic("not implemented")
}
// ExecuteBatchShards please see vtgateconn.Impl.ExecuteBatchShards
func (conn *FakeVTGateConn) ExecuteBatchShards(ctx context.Context, queries []*vtgatepb.BoundShardQuery, tabletType topodatapb.TabletType, asTransaction bool, session interface{}, options *querypb.ExecuteOptions) ([]sqltypes.Result, interface{}, error) {
panic("not implemented")
}
// ExecuteBatchKeyspaceIds please see vtgateconn.Impl.ExecuteBatchKeyspaceIds
func (conn *FakeVTGateConn) ExecuteBatchKeyspaceIds(ctx context.Context, queries []*vtgatepb.BoundKeyspaceIdQuery, tabletType topodatapb.TabletType, asTransaction bool, session interface{}, options *querypb.ExecuteOptions) ([]sqltypes.Result, interface{}, error) {
panic("not implemented")
}
type streamExecuteAdapter struct {
c chan *sqltypes.Result
}
func (a *streamExecuteAdapter) Recv() (*sqltypes.Result, error) {
r, ok := <-a.c
if !ok {
return nil, io.EOF
}
return r, nil
}
// StreamExecute please see vtgateconn.Impl.StreamExecute
func (conn *FakeVTGateConn) StreamExecute(ctx context.Context, sql string, bindVars map[string]interface{}, keyspace string, tabletType topodatapb.TabletType, options *querypb.ExecuteOptions) (sqltypes.ResultStream, error) {
response, ok := conn.execMap[sql]
if !ok {
return nil, fmt.Errorf("no match for: %s", sql)
}
query := &queryExecute{
SQL: sql,
BindVariables: bindVars,
Keyspace: keyspace,
TabletType: tabletType,
}
if !reflect.DeepEqual(query, response.execQuery) {
return nil, fmt.Errorf("StreamExecute: %+v, want %+v", sql, response.execQuery)
}
if response.err != nil {
return nil, response.err
}
var resultChan chan *sqltypes.Result
defer close(resultChan)
if response.reply != nil {
// create a result channel big enough to buffer all of
// the responses so we don't need to fork a go routine.
resultChan = make(chan *sqltypes.Result, len(response.reply.Rows)+1)
result := &sqltypes.Result{}
result.Fields = response.reply.Fields
resultChan <- result
for _, row := range response.reply.Rows {
result := &sqltypes.Result{}
result.Rows = [][]sqltypes.Value{row}
resultChan <- result
}
} else {
resultChan = make(chan *sqltypes.Result)
}
return &streamExecuteAdapter{resultChan}, nil
}
// StreamExecuteShards please see vtgateconn.Impl.StreamExecuteShards
func (conn *FakeVTGateConn) StreamExecuteShards(ctx context.Context, query string, keyspace string, shards []string, bindVars map[string]interface{}, tabletType topodatapb.TabletType, options *querypb.ExecuteOptions) (sqltypes.ResultStream, error) {
panic("not implemented")
}
// StreamExecuteKeyRanges please see vtgateconn.Impl.StreamExecuteKeyRanges
func (conn *FakeVTGateConn) StreamExecuteKeyRanges(ctx context.Context, query string, keyspace string, keyRanges []*topodatapb.KeyRange, bindVars map[string]interface{}, tabletType topodatapb.TabletType, options *querypb.ExecuteOptions) (sqltypes.ResultStream, error) {
panic("not implemented")
}
// StreamExecuteKeyspaceIds please see vtgateconn.Impl.StreamExecuteKeyspaceIds
func (conn *FakeVTGateConn) StreamExecuteKeyspaceIds(ctx context.Context, query string, keyspace string, keyspaceIds [][]byte, bindVars map[string]interface{}, tabletType topodatapb.TabletType, options *querypb.ExecuteOptions) (sqltypes.ResultStream, error) {
panic("not implemented")
}
// Begin please see vtgateconn.Impl.Begin
func (conn *FakeVTGateConn) Begin(ctx context.Context, singledb bool) (interface{}, error) {
return &vtgatepb.Session{
InTransaction: true,
SingleDb: singledb,
}, nil
}
// Commit please see vtgateconn.Impl.Commit
func (conn *FakeVTGateConn) Commit(ctx context.Context, session interface{}, twopc bool) error {
if session == nil {
return errors.New("commit: not in transaction")
}
return nil
}
// Rollback please see vtgateconn.Impl.Rollback
func (conn *FakeVTGateConn) Rollback(ctx context.Context, session interface{}) error {
return nil
}
// ResolveTransaction please see vtgateconn.Impl.ResolveTransaction
func (conn *FakeVTGateConn) ResolveTransaction(ctx context.Context, dtid string) error {
return nil
}
// MessageStream is part of the vtgate service API.
func (conn *FakeVTGateConn) MessageStream(ctx context.Context, keyspace string, shard string, keyRange *topodatapb.KeyRange, name string, callback func(*sqltypes.Result) error) error {
panic("not implemented")
}
// MessageAck is part of the vtgate service API.
func (conn *FakeVTGateConn) MessageAck(ctx context.Context, keyspace string, name string, ids []*querypb.Value) (int64, error) {
panic("not implemented")
}
// SplitQuery please see vtgateconn.Impl.SplitQuery
func (conn *FakeVTGateConn) SplitQuery(
ctx context.Context,
keyspace string,
query string,
bindVars map[string]interface{},
splitColumns []string,
splitCount int64,
numRowsPerQueryPart int64,
algorithm querypb.SplitQueryRequest_Algorithm) ([]*vtgatepb.SplitQueryResponse_Part, error) {
response, ok := conn.splitQueryMap[getSplitQueryKey(
keyspace, query, splitColumns, splitCount, numRowsPerQueryPart, algorithm)]
if !ok {
return nil, fmt.Errorf(
"no match for keyspace: %s,"+
" query: %v,"+
" splitColumns: %v,"+
" splitCount: %v"+
" numRowsPerQueryPart: %v"+
" algorithm: %v",
keyspace, query, splitColumns, splitCount, numRowsPerQueryPart, algorithm)
}
reply := make([]*vtgatepb.SplitQueryResponse_Part, len(response.reply))
copy(reply, response.reply)
return reply, nil
}
// GetSrvKeyspace please see vtgateconn.Impl.GetSrvKeyspace
func (conn *FakeVTGateConn) GetSrvKeyspace(ctx context.Context, keyspace string) (*topodatapb.SrvKeyspace, error) {
return nil, fmt.Errorf("NYI")
}
// UpdateStream please see vtgateconn.Impl.UpdateStream
func (conn *FakeVTGateConn) UpdateStream(ctx context.Context, keyspace string, shard string, keyRange *topodatapb.KeyRange, tabletType topodatapb.TabletType, timestamp int64, event *querypb.EventToken) (vtgateconn.UpdateStreamReader, error) {
return nil, fmt.Errorf("NYI")
}
// Close please see vtgateconn.Impl.Close
func (conn *FakeVTGateConn) Close() {
}
func getShardQueryKey(sql string, shards []string) string {
sort.Strings(shards)
return fmt.Sprintf("%s-%s", sql, strings.Join(shards, ":"))
}
func getSplitQueryKey(
keyspace string,
query string,
splitColumns []string,
splitCount int64,
numRowsPerQueryPart int64,
algorithm querypb.SplitQueryRequest_Algorithm) string {
return fmt.Sprintf(
"%v:%v:%v:%v:%v:%v",
keyspace,
query,
splitColumns,
splitCount,
numRowsPerQueryPart,
algorithm)
}
func newSession(
inTransaction bool,
keyspace string,
shards []string,
tabletType topodatapb.TabletType) *vtgatepb.Session {
shardSessions := make([]*vtgatepb.Session_ShardSession, len(shards))
for _, shard := range shards {
shardSessions = append(shardSessions, &vtgatepb.Session_ShardSession{
Target: &querypb.Target{
Keyspace: keyspace,
Shard: shard,
TabletType: tabletType,
},
TransactionId: rand.Int63(),
})
}
return &vtgatepb.Session{
InTransaction: inTransaction,
ShardSessions: shardSessions,
}
}
// Make sure FakeVTGateConn implements vtgateconn.Impl
var _ (vtgateconn.Impl) = (*FakeVTGateConn)(nil)