forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 10
/
sandboxconn.go
300 lines (267 loc) · 8.59 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
// Copyright 2016, 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 sandboxconn provides a fake TabletConn implementation for tests.
// It can return real results, and simulate error cases.
package sandboxconn
import (
"fmt"
"io"
"github.com/youtube/vitess/go/sqltypes"
"github.com/youtube/vitess/go/sync2"
"github.com/youtube/vitess/go/vt/tabletserver/querytypes"
"github.com/youtube/vitess/go/vt/tabletserver/tabletconn"
"golang.org/x/net/context"
querypb "github.com/youtube/vitess/go/vt/proto/query"
topodatapb "github.com/youtube/vitess/go/vt/proto/topodata"
vtrpcpb "github.com/youtube/vitess/go/vt/proto/vtrpc"
)
// SandboxConn satisfies the TabletConn interface
type SandboxConn struct {
tablet *topodatapb.Tablet
MustFailRetry int
MustFailFatal int
MustFailServer int
MustFailConn int
MustFailTxPool int
MustFailNotTx 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
// Queries stores the non-batch requests received.
Queries []querytypes.BoundQuery
// BatchQueries stores the batch requests received
// Each batch request is inlined as a slice of Queries.
BatchQueries [][]querytypes.BoundQuery
// 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
// transaction id generator
TransactionID sync2.AtomicInt64
}
// NewSandboxConn returns a new SandboxConn targeted to the provided tablet.
func NewSandboxConn(t *topodatapb.Tablet) *SandboxConn {
return &SandboxConn{
tablet: t,
}
}
func (sbc *SandboxConn) getError() error {
if sbc.MustFailRetry > 0 {
sbc.MustFailRetry--
return &tabletconn.ServerError{
Err: "retry: err",
ServerCode: vtrpcpb.ErrorCode_QUERY_NOT_SERVED,
}
}
if sbc.MustFailFatal > 0 {
sbc.MustFailFatal--
return &tabletconn.ServerError{
Err: "fatal: err",
ServerCode: vtrpcpb.ErrorCode_INTERNAL_ERROR,
}
}
if sbc.MustFailServer > 0 {
sbc.MustFailServer--
return &tabletconn.ServerError{
Err: "error: err",
ServerCode: vtrpcpb.ErrorCode_BAD_INPUT,
}
}
if sbc.MustFailConn > 0 {
sbc.MustFailConn--
return tabletconn.OperationalError(fmt.Sprintf("error: conn"))
}
if sbc.MustFailTxPool > 0 {
sbc.MustFailTxPool--
return &tabletconn.ServerError{
Err: "tx_pool_full: err",
ServerCode: vtrpcpb.ErrorCode_RESOURCE_EXHAUSTED,
}
}
if sbc.MustFailNotTx > 0 {
sbc.MustFailNotTx--
return &tabletconn.ServerError{
Err: "not_in_tx: err",
ServerCode: vtrpcpb.ErrorCode_NOT_IN_TX,
}
}
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 TabletConn interface.
func (sbc *SandboxConn) Execute(ctx context.Context, query string, bindVars map[string]interface{}, transactionID int64) (*sqltypes.Result, error) {
sbc.ExecCount.Add(1)
bv := make(map[string]interface{})
for k, v := range bindVars {
bv[k] = v
}
sbc.Queries = append(sbc.Queries, querytypes.BoundQuery{
Sql: query,
BindVariables: bv,
})
if err := sbc.getError(); err != nil {
return nil, err
}
return sbc.getNextResult(), nil
}
// ExecuteBatch is part of the TabletConn interface.
func (sbc *SandboxConn) ExecuteBatch(ctx context.Context, queries []querytypes.BoundQuery, asTransaction bool, transactionID int64) ([]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)
result := make([]sqltypes.Result, 0, len(queries))
for range queries {
result = append(result, *(sbc.getNextResult()))
}
return result, nil
}
type streamExecuteAdapter struct {
result *sqltypes.Result
done bool
}
func (a *streamExecuteAdapter) Recv() (*sqltypes.Result, error) {
if a.done {
return nil, io.EOF
}
a.done = true
return a.result, nil
}
// StreamExecute is part of the TabletConn interface.
func (sbc *SandboxConn) StreamExecute(ctx context.Context, query string, bindVars map[string]interface{}) (sqltypes.ResultStream, error) {
sbc.ExecCount.Add(1)
bv := make(map[string]interface{})
for k, v := range bindVars {
bv[k] = v
}
sbc.Queries = append(sbc.Queries, querytypes.BoundQuery{
Sql: query,
BindVariables: bv,
})
err := sbc.getError()
if err != nil {
return nil, err
}
r := sbc.getNextResult()
return &streamExecuteAdapter{result: r}, nil
}
// Begin is part of the TabletConn interface.
func (sbc *SandboxConn) Begin(ctx context.Context) (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 TabletConn interface.
func (sbc *SandboxConn) Commit(ctx context.Context, transactionID int64) error {
sbc.CommitCount.Add(1)
return sbc.getError()
}
// Rollback is part of the TabletConn interface.
func (sbc *SandboxConn) Rollback(ctx context.Context, transactionID int64) error {
sbc.RollbackCount.Add(1)
return sbc.getError()
}
// BeginExecute is part of the TabletConn interface.
func (sbc *SandboxConn) BeginExecute(ctx context.Context, query string, bindVars map[string]interface{}) (*sqltypes.Result, int64, error) {
transactionID, err := sbc.Begin(ctx)
if err != nil {
return nil, 0, err
}
result, err := sbc.Execute(ctx, query, bindVars, transactionID)
return result, transactionID, err
}
// BeginExecuteBatch is part of the TabletConn interface.
func (sbc *SandboxConn) BeginExecuteBatch(ctx context.Context, queries []querytypes.BoundQuery, asTransaction bool) ([]sqltypes.Result, int64, error) {
transactionID, err := sbc.Begin(ctx)
if err != nil {
return nil, 0, err
}
results, err := sbc.ExecuteBatch(ctx, queries, asTransaction, transactionID)
return results, transactionID, err
}
// SandboxSQRowCount is the default number of fake splits returned.
var SandboxSQRowCount = int64(10)
// SplitQuery creates splits from the original query by appending the
// split index as a comment to the SQL. RowCount is always SandboxSQRowCount
func (sbc *SandboxConn) SplitQuery(ctx context.Context, query querytypes.BoundQuery, splitColumn string, splitCount int64) ([]querytypes.QuerySplit, error) {
splits := []querytypes.QuerySplit{}
for i := 0; i < int(splitCount); i++ {
split := querytypes.QuerySplit{
Sql: fmt.Sprintf("%s /*split %v */", query.Sql, i),
BindVariables: query.BindVariables,
RowCount: SandboxSQRowCount,
}
splits = append(splits, split)
}
return splits, nil
}
// SplitQueryV2 returns a single QuerySplit whose 'sql' field describes the received arguments.
// TODO(erez): Rename to SplitQuery after the migration to SplitQuery V2 is done.
func (sbc *SandboxConn) SplitQueryV2(
ctx context.Context,
query querytypes.BoundQuery,
splitColumns []string,
splitCount int64,
numRowsPerQueryPart int64,
algorithm querypb.SplitQueryRequest_Algorithm) ([]querytypes.QuerySplit, error) {
splits := []querytypes.QuerySplit{
{
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) (tabletconn.StreamHealthReader, error) {
return nil, fmt.Errorf("Not implemented in test")
}
// Close does not change ExecCount
func (sbc *SandboxConn) Close() {
}
// SetTarget is part of the TabletConn interface.
func (sbc *SandboxConn) SetTarget(keyspace, shard string, tabletType topodatapb.TabletType) error {
return nil
}
// Tablet is part of the TabletConn 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{
{"id", sqltypes.Int32},
{"value", sqltypes.VarChar},
},
RowsAffected: 1,
InsertID: 0,
Rows: [][]sqltypes.Value{{
sqltypes.MakeTrusted(sqltypes.Int32, []byte("1")),
sqltypes.MakeTrusted(sqltypes.VarChar, []byte("foo")),
}},
}