/
sandboxconn.go
410 lines (360 loc) · 13.5 KB
/
sandboxconn.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
/*
Copyright 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package sandboxconn provides a fake QueryService implementation for tests.
// It can return real results, and simulate error cases.
package sandboxconn
import (
"fmt"
"golang.org/x/net/context"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/sync2"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vttablet/queryservice"
querypb "vitess.io/vitess/go/vt/proto/query"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
)
// SandboxConn satisfies the QueryService interface
type SandboxConn struct {
tablet *topodatapb.Tablet
// These errors work for all functions.
MustFailCodes map[vtrpcpb.Code]int
// These errors are triggered only for specific functions.
// For now these are just for the 2PC functions.
MustFailPrepare int
MustFailCommitPrepared int
MustFailRollbackPrepared int
MustFailCreateTransaction int
MustFailStartCommit int
MustFailSetRollback int
MustFailConcludeTransaction int
// These Count vars report how often the corresponding
// functions were called.
ExecCount sync2.AtomicInt64
BeginCount sync2.AtomicInt64
CommitCount sync2.AtomicInt64
RollbackCount sync2.AtomicInt64
AsTransactionCount sync2.AtomicInt64
PrepareCount sync2.AtomicInt64
CommitPreparedCount sync2.AtomicInt64
RollbackPreparedCount sync2.AtomicInt64
CreateTransactionCount sync2.AtomicInt64
StartCommitCount sync2.AtomicInt64
SetRollbackCount sync2.AtomicInt64
ConcludeTransactionCount sync2.AtomicInt64
ReadTransactionCount sync2.AtomicInt64
// Queries stores the non-batch requests received.
Queries []*querypb.BoundQuery
// BatchQueries stores the batch requests received
// Each batch request is inlined as a slice of Queries.
BatchQueries [][]*querypb.BoundQuery
// Options stores the options received by all calls.
Options []*querypb.ExecuteOptions
// results specifies the results to be returned.
// They're consumed as results are returned. If there are
// no results left, SingleRowResult is returned.
results []*sqltypes.Result
// ReadTransactionResults is used for returning results for ReadTransaction.
ReadTransactionResults []*querypb.TransactionMetadata
MessageIDs []*querypb.Value
// transaction id generator
TransactionID sync2.AtomicInt64
}
var _ queryservice.QueryService = (*SandboxConn)(nil) // compile-time interface check
// NewSandboxConn returns a new SandboxConn targeted to the provided tablet.
func NewSandboxConn(t *topodatapb.Tablet) *SandboxConn {
return &SandboxConn{
tablet: t,
MustFailCodes: make(map[vtrpcpb.Code]int),
}
}
func (sbc *SandboxConn) getError() error {
for code, count := range sbc.MustFailCodes {
if count == 0 {
continue
}
sbc.MustFailCodes[code] = count - 1
return vterrors.New(code, fmt.Sprintf("%v error", code))
}
return nil
}
// SetResults sets what this con should return next time.
func (sbc *SandboxConn) SetResults(r []*sqltypes.Result) {
sbc.results = r
}
// Execute is part of the QueryService interface.
func (sbc *SandboxConn) Execute(ctx context.Context, target *querypb.Target, query string, bindVars map[string]*querypb.BindVariable, transactionID int64, options *querypb.ExecuteOptions) (*sqltypes.Result, error) {
sbc.ExecCount.Add(1)
bv := make(map[string]*querypb.BindVariable)
for k, v := range bindVars {
bv[k] = v
}
sbc.Queries = append(sbc.Queries, &querypb.BoundQuery{
Sql: query,
BindVariables: bv,
})
sbc.Options = append(sbc.Options, options)
if err := sbc.getError(); err != nil {
return nil, err
}
return sbc.getNextResult(), nil
}
// ExecuteBatch is part of the QueryService interface.
func (sbc *SandboxConn) ExecuteBatch(ctx context.Context, target *querypb.Target, queries []*querypb.BoundQuery, asTransaction bool, transactionID int64, options *querypb.ExecuteOptions) ([]sqltypes.Result, error) {
sbc.ExecCount.Add(1)
if asTransaction {
sbc.AsTransactionCount.Add(1)
}
if err := sbc.getError(); err != nil {
return nil, err
}
sbc.BatchQueries = append(sbc.BatchQueries, queries)
sbc.Options = append(sbc.Options, options)
result := make([]sqltypes.Result, 0, len(queries))
for range queries {
result = append(result, *(sbc.getNextResult()))
}
return result, nil
}
// StreamExecute is part of the QueryService interface.
func (sbc *SandboxConn) StreamExecute(ctx context.Context, target *querypb.Target, query string, bindVars map[string]*querypb.BindVariable, options *querypb.ExecuteOptions, callback func(*sqltypes.Result) error) error {
sbc.ExecCount.Add(1)
bv := make(map[string]*querypb.BindVariable)
for k, v := range bindVars {
bv[k] = v
}
sbc.Queries = append(sbc.Queries, &querypb.BoundQuery{
Sql: query,
BindVariables: bv,
})
sbc.Options = append(sbc.Options, options)
err := sbc.getError()
if err != nil {
return err
}
return callback(sbc.getNextResult())
}
// Begin is part of the QueryService interface.
func (sbc *SandboxConn) Begin(ctx context.Context, target *querypb.Target, options *querypb.ExecuteOptions) (int64, error) {
sbc.BeginCount.Add(1)
err := sbc.getError()
if err != nil {
return 0, err
}
return sbc.TransactionID.Add(1), nil
}
// Commit is part of the QueryService interface.
func (sbc *SandboxConn) Commit(ctx context.Context, target *querypb.Target, transactionID int64) error {
sbc.CommitCount.Add(1)
return sbc.getError()
}
// Rollback is part of the QueryService interface.
func (sbc *SandboxConn) Rollback(ctx context.Context, target *querypb.Target, transactionID int64) error {
sbc.RollbackCount.Add(1)
return sbc.getError()
}
// Prepare prepares the specified transaction.
func (sbc *SandboxConn) Prepare(ctx context.Context, target *querypb.Target, transactionID int64, dtid string) (err error) {
sbc.PrepareCount.Add(1)
if sbc.MustFailPrepare > 0 {
sbc.MustFailPrepare--
return vterrors.New(vtrpcpb.Code_FAILED_PRECONDITION, "error: err")
}
return sbc.getError()
}
// CommitPrepared commits the prepared transaction.
func (sbc *SandboxConn) CommitPrepared(ctx context.Context, target *querypb.Target, dtid string) (err error) {
sbc.CommitPreparedCount.Add(1)
if sbc.MustFailCommitPrepared > 0 {
sbc.MustFailCommitPrepared--
return vterrors.New(vtrpcpb.Code_FAILED_PRECONDITION, "error: err")
}
return sbc.getError()
}
// RollbackPrepared rolls back the prepared transaction.
func (sbc *SandboxConn) RollbackPrepared(ctx context.Context, target *querypb.Target, dtid string, originalID int64) (err error) {
sbc.RollbackPreparedCount.Add(1)
if sbc.MustFailRollbackPrepared > 0 {
sbc.MustFailRollbackPrepared--
return vterrors.New(vtrpcpb.Code_FAILED_PRECONDITION, "error: err")
}
return sbc.getError()
}
// CreateTransaction creates the metadata for a 2PC transaction.
func (sbc *SandboxConn) CreateTransaction(ctx context.Context, target *querypb.Target, dtid string, participants []*querypb.Target) (err error) {
sbc.CreateTransactionCount.Add(1)
if sbc.MustFailCreateTransaction > 0 {
sbc.MustFailCreateTransaction--
return vterrors.New(vtrpcpb.Code_FAILED_PRECONDITION, "error: err")
}
return sbc.getError()
}
// StartCommit atomically commits the transaction along with the
// decision to commit the associated 2pc transaction.
func (sbc *SandboxConn) StartCommit(ctx context.Context, target *querypb.Target, transactionID int64, dtid string) (err error) {
sbc.StartCommitCount.Add(1)
if sbc.MustFailStartCommit > 0 {
sbc.MustFailStartCommit--
return vterrors.New(vtrpcpb.Code_FAILED_PRECONDITION, "error: err")
}
return sbc.getError()
}
// SetRollback transitions the 2pc transaction to the Rollback state.
// If a transaction id is provided, that transaction is also rolled back.
func (sbc *SandboxConn) SetRollback(ctx context.Context, target *querypb.Target, dtid string, transactionID int64) (err error) {
sbc.SetRollbackCount.Add(1)
if sbc.MustFailSetRollback > 0 {
sbc.MustFailSetRollback--
return vterrors.New(vtrpcpb.Code_FAILED_PRECONDITION, "error: err")
}
return sbc.getError()
}
// ConcludeTransaction deletes the 2pc transaction metadata
// essentially resolving it.
func (sbc *SandboxConn) ConcludeTransaction(ctx context.Context, target *querypb.Target, dtid string) (err error) {
sbc.ConcludeTransactionCount.Add(1)
if sbc.MustFailConcludeTransaction > 0 {
sbc.MustFailConcludeTransaction--
return vterrors.New(vtrpcpb.Code_FAILED_PRECONDITION, "error: err")
}
return sbc.getError()
}
// ReadTransaction returns the metadata for the sepcified dtid.
func (sbc *SandboxConn) ReadTransaction(ctx context.Context, target *querypb.Target, dtid string) (metadata *querypb.TransactionMetadata, err error) {
sbc.ReadTransactionCount.Add(1)
if err := sbc.getError(); err != nil {
return nil, err
}
if len(sbc.ReadTransactionResults) >= 1 {
res := sbc.ReadTransactionResults[0]
sbc.ReadTransactionResults = sbc.ReadTransactionResults[1:]
return res, nil
}
return nil, nil
}
// BeginExecute is part of the QueryService interface.
func (sbc *SandboxConn) BeginExecute(ctx context.Context, target *querypb.Target, query string, bindVars map[string]*querypb.BindVariable, options *querypb.ExecuteOptions) (*sqltypes.Result, int64, error) {
transactionID, err := sbc.Begin(ctx, target, options)
if err != nil {
return nil, 0, err
}
result, err := sbc.Execute(ctx, target, query, bindVars, transactionID, options)
return result, transactionID, err
}
// BeginExecuteBatch is part of the QueryService interface.
func (sbc *SandboxConn) BeginExecuteBatch(ctx context.Context, target *querypb.Target, queries []*querypb.BoundQuery, asTransaction bool, options *querypb.ExecuteOptions) ([]sqltypes.Result, int64, error) {
transactionID, err := sbc.Begin(ctx, target, options)
if err != nil {
return nil, 0, err
}
results, err := sbc.ExecuteBatch(ctx, target, queries, asTransaction, transactionID, options)
return results, transactionID, err
}
// MessageStream is part of the QueryService interface.
func (sbc *SandboxConn) MessageStream(ctx context.Context, target *querypb.Target, name string, callback func(*sqltypes.Result) error) (err error) {
if err := sbc.getError(); err != nil {
return err
}
r := sbc.getNextResult()
if r == nil {
return nil
}
callback(r)
return nil
}
// MessageAck is part of the QueryService interface.
func (sbc *SandboxConn) MessageAck(ctx context.Context, target *querypb.Target, name string, ids []*querypb.Value) (count int64, err error) {
sbc.MessageIDs = ids
return int64(len(ids)), nil
}
// SandboxSQRowCount is the default number of fake splits returned.
var SandboxSQRowCount = int64(10)
// SplitQuery returns a single QuerySplit whose 'sql' field describes the received arguments.
func (sbc *SandboxConn) SplitQuery(
ctx context.Context,
target *querypb.Target,
query *querypb.BoundQuery,
splitColumns []string,
splitCount int64,
numRowsPerQueryPart int64,
algorithm querypb.SplitQueryRequest_Algorithm) ([]*querypb.QuerySplit, error) {
err := sbc.getError()
if err != nil {
return nil, err
}
splits := []*querypb.QuerySplit{
{
Query: &querypb.BoundQuery{
Sql: fmt.Sprintf(
"query:%v, splitColumns:%v, splitCount:%v,"+
" numRowsPerQueryPart:%v, algorithm:%v, shard:%v",
query, splitColumns, splitCount, numRowsPerQueryPart, algorithm, sbc.tablet.Shard),
},
},
}
return splits, nil
}
// StreamHealth is not implemented.
func (sbc *SandboxConn) StreamHealth(ctx context.Context, callback func(*querypb.StreamHealthResponse) error) error {
return fmt.Errorf("Not implemented in test")
}
// UpdateStream is part of the QueryService interface.
func (sbc *SandboxConn) UpdateStream(ctx context.Context, target *querypb.Target, position string, timestamp int64, callback func(*querypb.StreamEvent) error) error {
// FIXME(alainjobart) implement, use in vtgate tests.
return fmt.Errorf("Not implemented in test")
}
// HandlePanic is part of the QueryService interface.
func (sbc *SandboxConn) HandlePanic(err *error) {
}
// Close does not change ExecCount
func (sbc *SandboxConn) Close(ctx context.Context) error {
return nil
}
// Tablet is part of the QueryService interface.
func (sbc *SandboxConn) Tablet() *topodatapb.Tablet {
return sbc.tablet
}
func (sbc *SandboxConn) getNextResult() *sqltypes.Result {
if len(sbc.results) != 0 {
r := sbc.results[0]
sbc.results = sbc.results[1:]
return r
}
return SingleRowResult
}
// SingleRowResult is returned when there is no pre-stored result.
var SingleRowResult = &sqltypes.Result{
Fields: []*querypb.Field{
{Name: "id", Type: sqltypes.Int32},
{Name: "value", Type: sqltypes.VarChar},
},
RowsAffected: 1,
InsertID: 0,
Rows: [][]sqltypes.Value{{
sqltypes.NewInt32(1),
sqltypes.NewVarChar("foo"),
}},
}
// StreamRowResult is SingleRowResult with RowsAffected set to 0.
var StreamRowResult = &sqltypes.Result{
Fields: []*querypb.Field{
{Name: "id", Type: sqltypes.Int32},
{Name: "value", Type: sqltypes.VarChar},
},
RowsAffected: 0,
InsertID: 0,
Rows: [][]sqltypes.Value{{
sqltypes.NewInt32(1),
sqltypes.NewVarChar("foo"),
}},
}