forked from vitessio/vitess
/
errors.go
290 lines (260 loc) · 11.7 KB
/
errors.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
// 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 services
import (
"errors"
"fmt"
"strings"
"golang.org/x/net/context"
"github.com/youtube/vitess/go/sqltypes"
"github.com/youtube/vitess/go/vt/callerid"
"github.com/youtube/vitess/go/vt/vterrors"
"github.com/youtube/vitess/go/vt/vtgate/vtgateservice"
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"
vtrpcpb "github.com/youtube/vitess/go/vt/proto/vtrpc"
)
// errorClient implements vtgateservice.VTGateService
// and returns specific errors. It is meant to test all possible error cases,
// and make sure all clients handle the errors correctly.
const (
// ErrorPrefix is the prefix to send with queries so they go through this service handler.
ErrorPrefix = "error://"
// PartialErrorPrefix is the prefix to send with queries so the RPC returns a partial error.
// A partial error is when we return an error as part of the RPC response instead of via
// the regular error channels. This occurs if an RPC partially succeeds, and therefore
// requires some kind of response, but still needs to return an error.
// VTGate Execute* calls do this: they always return a new session ID, but might also
// return an error in the response.
PartialErrorPrefix = "partialerror://"
)
type errorClient struct {
fallbackClient
}
func newErrorClient(fallback vtgateservice.VTGateService) *errorClient {
return &errorClient{
fallbackClient: newFallbackClient(fallback),
}
}
// requestToError returns an error for the given request, by looking at the
// request's prefix and requested error type. If the request doesn't match an
// error request, return nil.
func requestToError(request string) error {
if !strings.HasPrefix(request, ErrorPrefix) {
return nil
}
return trimmedRequestToError(strings.TrimPrefix(request, ErrorPrefix))
}
// requestToPartialError fills reply for a partial error if requested
// (that is, an error that may change the session).
// It returns true if a partial error was requested, false otherwise.
// This partial error should only be returned by Execute* calls.
func requestToPartialError(request string, session *vtgatepb.Session) error {
if !strings.HasPrefix(request, PartialErrorPrefix) {
return nil
}
request = strings.TrimPrefix(request, PartialErrorPrefix)
parts := strings.Split(request, "/")
if len(parts) > 1 && parts[1] == "close transaction" {
session.InTransaction = false
}
return trimmedRequestToError(parts[0])
}
// trimmedRequestToError returns an error for a trimmed request by looking at the
// requested error type. It assumes that prefix checking has already been done.
// If the received string doesn't match a known error, returns an unknown error.
func trimmedRequestToError(received string) error {
switch received {
case "bad input":
return vterrors.FromError(
vtrpcpb.ErrorCode_BAD_INPUT,
errors.New("vtgate test client forced error: bad input"),
)
case "deadline exceeded":
return vterrors.FromError(
vtrpcpb.ErrorCode_DEADLINE_EXCEEDED,
errors.New("vtgate test client forced error: deadline exceeded"),
)
case "integrity error":
return vterrors.FromError(
vtrpcpb.ErrorCode_INTEGRITY_ERROR,
errors.New("vtgate test client forced error: integrity error (errno 1062) (sqlstate 23000)"),
)
// request backlog and general throttling type errors
case "transient error":
return vterrors.FromError(
vtrpcpb.ErrorCode_TRANSIENT_ERROR,
errors.New("request_backlog: too many requests in flight: vtgate test client forced error: transient error"),
)
case "unauthenticated":
return vterrors.FromError(
vtrpcpb.ErrorCode_UNAUTHENTICATED,
errors.New("vtgate test client forced error: unauthenticated"),
)
case "unknown error":
return vterrors.FromError(
vtrpcpb.ErrorCode_UNKNOWN_ERROR,
errors.New("vtgate test client forced error: unknown error"),
)
default:
return vterrors.FromError(
vtrpcpb.ErrorCode_UNKNOWN_ERROR,
fmt.Errorf("vtgate test client error request unrecognized: %v", received),
)
}
}
func (c *errorClient) Execute(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspace string, tabletType topodatapb.TabletType, session *vtgatepb.Session, notInTransaction bool) (*sqltypes.Result, error) {
if err := requestToPartialError(sql, session); err != nil {
return nil, err
}
if err := requestToError(sql); err != nil {
return nil, err
}
return c.fallbackClient.Execute(ctx, sql, bindVariables, keyspace, tabletType, session, notInTransaction)
}
func (c *errorClient) ExecuteShards(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspace string, shards []string, tabletType topodatapb.TabletType, session *vtgatepb.Session, notInTransaction bool) (*sqltypes.Result, error) {
if err := requestToPartialError(sql, session); err != nil {
return nil, err
}
if err := requestToError(sql); err != nil {
return nil, err
}
return c.fallbackClient.ExecuteShards(ctx, sql, bindVariables, keyspace, shards, tabletType, session, notInTransaction)
}
func (c *errorClient) ExecuteKeyspaceIds(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspace string, keyspaceIds [][]byte, tabletType topodatapb.TabletType, session *vtgatepb.Session, notInTransaction bool) (*sqltypes.Result, error) {
if err := requestToPartialError(sql, session); err != nil {
return nil, err
}
if err := requestToError(sql); err != nil {
return nil, err
}
return c.fallbackClient.ExecuteKeyspaceIds(ctx, sql, bindVariables, keyspace, keyspaceIds, tabletType, session, notInTransaction)
}
func (c *errorClient) ExecuteKeyRanges(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspace string, keyRanges []*topodatapb.KeyRange, tabletType topodatapb.TabletType, session *vtgatepb.Session, notInTransaction bool) (*sqltypes.Result, error) {
if err := requestToPartialError(sql, session); err != nil {
return nil, err
}
if err := requestToError(sql); err != nil {
return nil, err
}
return c.fallbackClient.ExecuteKeyRanges(ctx, sql, bindVariables, keyspace, keyRanges, tabletType, session, notInTransaction)
}
func (c *errorClient) ExecuteEntityIds(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspace string, entityColumnName string, entityKeyspaceIDs []*vtgatepb.ExecuteEntityIdsRequest_EntityId, tabletType topodatapb.TabletType, session *vtgatepb.Session, notInTransaction bool) (*sqltypes.Result, error) {
if err := requestToPartialError(sql, session); err != nil {
return nil, err
}
if err := requestToError(sql); err != nil {
return nil, err
}
return c.fallbackClient.ExecuteEntityIds(ctx, sql, bindVariables, keyspace, entityColumnName, entityKeyspaceIDs, tabletType, session, notInTransaction)
}
func (c *errorClient) ExecuteBatchShards(ctx context.Context, queries []*vtgatepb.BoundShardQuery, tabletType topodatapb.TabletType, asTransaction bool, session *vtgatepb.Session) ([]sqltypes.Result, error) {
if len(queries) == 1 {
if err := requestToPartialError(queries[0].Query.Sql, session); err != nil {
return nil, err
}
if err := requestToError(queries[0].Query.Sql); err != nil {
return nil, err
}
}
return c.fallbackClient.ExecuteBatchShards(ctx, queries, tabletType, asTransaction, session)
}
func (c *errorClient) ExecuteBatchKeyspaceIds(ctx context.Context, queries []*vtgatepb.BoundKeyspaceIdQuery, tabletType topodatapb.TabletType, asTransaction bool, session *vtgatepb.Session) ([]sqltypes.Result, error) {
if len(queries) == 1 {
if err := requestToPartialError(queries[0].Query.Sql, session); err != nil {
return nil, err
}
if err := requestToError(queries[0].Query.Sql); err != nil {
return nil, err
}
}
return c.fallbackClient.ExecuteBatchKeyspaceIds(ctx, queries, tabletType, asTransaction, session)
}
func (c *errorClient) StreamExecute(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspace string, tabletType topodatapb.TabletType, sendReply func(*sqltypes.Result) error) error {
if err := requestToError(sql); err != nil {
return err
}
return c.fallbackClient.StreamExecute(ctx, sql, bindVariables, keyspace, tabletType, sendReply)
}
func (c *errorClient) StreamExecuteShards(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspace string, shards []string, tabletType topodatapb.TabletType, sendReply func(*sqltypes.Result) error) error {
if err := requestToError(sql); err != nil {
return err
}
return c.fallbackClient.StreamExecuteShards(ctx, sql, bindVariables, keyspace, shards, tabletType, sendReply)
}
func (c *errorClient) StreamExecuteKeyspaceIds(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspace string, keyspaceIds [][]byte, tabletType topodatapb.TabletType, sendReply func(*sqltypes.Result) error) error {
if err := requestToError(sql); err != nil {
return err
}
return c.fallbackClient.StreamExecuteKeyspaceIds(ctx, sql, bindVariables, keyspace, keyspaceIds, tabletType, sendReply)
}
func (c *errorClient) StreamExecuteKeyRanges(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspace string, keyRanges []*topodatapb.KeyRange, tabletType topodatapb.TabletType, sendReply func(*sqltypes.Result) error) error {
if err := requestToError(sql); err != nil {
return err
}
return c.fallbackClient.StreamExecuteKeyRanges(ctx, sql, bindVariables, keyspace, keyRanges, tabletType, sendReply)
}
func (c *errorClient) Begin(ctx context.Context) (*vtgatepb.Session, error) {
// The client sends the error request through the callerid, as there are no other parameters
cid := callerid.EffectiveCallerIDFromContext(ctx)
request := callerid.GetPrincipal(cid)
if err := requestToError(request); err != nil {
return nil, err
}
return c.fallbackClient.Begin(ctx)
}
func (c *errorClient) Commit(ctx context.Context, session *vtgatepb.Session) error {
// The client sends the error request through the callerid, as there are no other parameters
cid := callerid.EffectiveCallerIDFromContext(ctx)
request := callerid.GetPrincipal(cid)
if err := requestToError(request); err != nil {
return err
}
return c.fallbackClient.Commit(ctx, session)
}
func (c *errorClient) Rollback(ctx context.Context, session *vtgatepb.Session) error {
// The client sends the error request through the callerid, as there are no other parameters
cid := callerid.EffectiveCallerIDFromContext(ctx)
request := callerid.GetPrincipal(cid)
if err := requestToError(request); err != nil {
return err
}
return c.fallbackClient.Rollback(ctx, session)
}
func (c *errorClient) SplitQuery(ctx context.Context, keyspace string, sql string, bindVariables map[string]interface{}, splitColumn string, splitCount int64) ([]*vtgatepb.SplitQueryResponse_Part, error) {
if err := requestToError(sql); err != nil {
return nil, err
}
return c.fallbackClient.SplitQuery(ctx, sql, keyspace, bindVariables, splitColumn, splitCount)
}
// TODO(erez): Rename after migration to SplitQuery V2 is done.
func (c *errorClient) SplitQueryV2(
ctx context.Context,
keyspace string,
sql string,
bindVariables map[string]interface{},
splitColumns []string,
splitCount int64,
numRowsPerQueryPart int64,
algorithm querypb.SplitQueryRequest_Algorithm) ([]*vtgatepb.SplitQueryResponse_Part, error) {
if err := requestToError(sql); err != nil {
return nil, err
}
return c.fallbackClient.SplitQueryV2(
ctx,
sql,
keyspace,
bindVariables,
splitColumns,
splitCount,
numRowsPerQueryPart,
algorithm)
}
func (c *errorClient) GetSrvKeyspace(ctx context.Context, keyspace string) (*topodatapb.SrvKeyspace, error) {
if err := requestToError(keyspace); err != nil {
return nil, err
}
return c.fallbackClient.GetSrvKeyspace(ctx, keyspace)
}