forked from dolthub/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tablet_error.go
190 lines (173 loc) · 5.47 KB
/
tablet_error.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
// 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 tabletserver
import (
"fmt"
"regexp"
"strconv"
"strings"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/mysql"
"github.com/youtube/vitess/go/sqldb"
"github.com/youtube/vitess/go/tb"
vtrpcpb "github.com/youtube/vitess/go/vt/proto/vtrpc"
)
const (
maxErrLen = 5000
)
// ErrConnPoolClosed is returned / panicked when the connection pool is closed.
var ErrConnPoolClosed = NewTabletError(
// connection pool being closed is not the query's fault, it can be retried on a
// different VtTablet.
vtrpcpb.ErrorCode_INTERNAL_ERROR,
"connection pool is closed")
// TabletError is the error type we use in this library.
// It implements vterrors.VtError interface.
type TabletError struct {
Message string
SQLError int
SQLState string
// ErrorCode will be used to transmit the error across RPC boundaries
ErrorCode vtrpcpb.ErrorCode
}
// NewTabletError returns a TabletError of the given type
func NewTabletError(errCode vtrpcpb.ErrorCode, format string, args ...interface{}) *TabletError {
return &TabletError{
Message: printable(fmt.Sprintf(format, args...)),
ErrorCode: errCode,
}
}
// NewTabletErrorSQL returns a TabletError based on the error
func NewTabletErrorSQL(errCode vtrpcpb.ErrorCode, err error) *TabletError {
var errnum int
errstr := err.Error()
sqlState := sqldb.SQLStateGeneral
if sqlErr, ok := err.(*sqldb.SQLError); ok {
errnum = sqlErr.Number()
sqlState = sqlErr.SQLState()
switch errnum {
case mysql.ErrOptionPreventsStatement:
// Override error type if MySQL is in read-only mode. It's probably because
// there was a remaster and there are old clients still connected.
if strings.Contains(errstr, "read-only") {
errCode = vtrpcpb.ErrorCode_QUERY_NOT_SERVED
}
case mysql.ErrDupEntry:
errCode = vtrpcpb.ErrorCode_INTEGRITY_ERROR
case mysql.ErrDataTooLong, mysql.ErrDataOutOfRange:
errCode = vtrpcpb.ErrorCode_BAD_INPUT
default:
}
}
return &TabletError{
Message: printable(errstr),
SQLError: errnum,
SQLState: sqlState,
ErrorCode: errCode,
}
}
// PrefixTabletError attempts to add a string prefix to a TabletError,
// while preserving its ErrorCode. If the given error is not a
// TabletError, a new TabletError is returned with the desired ErrorCode.
func PrefixTabletError(errCode vtrpcpb.ErrorCode, err error, prefix string) error {
if terr, ok := err.(*TabletError); ok {
return NewTabletError(terr.ErrorCode, "%s%s", prefix, terr.Message)
}
return NewTabletError(errCode, "%s%s", prefix, err)
}
func printable(in string) string {
if len(in) > maxErrLen {
in = in[:maxErrLen]
}
in = fmt.Sprintf("%q", in)
return in[1 : len(in)-1]
}
var errExtract = regexp.MustCompile(`.*\(errno ([0-9]*)\).*`)
// IsConnErr returns true if the error is a connection error. If
// the error is of type TabletError or hasNumber, it checks the error
// code. Otherwise, it parses the string looking for (errno xxxx)
// and uses the extracted value to determine if it's a conn error.
func IsConnErr(err error) bool {
var sqlError int
switch err := err.(type) {
case *TabletError:
sqlError = err.SQLError
case *sqldb.SQLError:
sqlError = err.Number()
default:
match := errExtract.FindStringSubmatch(err.Error())
if len(match) < 2 {
return false
}
var convErr error
sqlError, convErr = strconv.Atoi(match[1])
if convErr != nil {
return false
}
}
// ErrServerLost means that someone sniped the query.
if sqlError == mysql.ErrServerLost {
return false
}
return sqlError >= 2000 && sqlError <= 2018
}
func (te *TabletError) Error() string {
return te.Prefix() + te.Message
}
// VtErrorCode returns the underlying Vitess error code
func (te *TabletError) VtErrorCode() vtrpcpb.ErrorCode {
return te.ErrorCode
}
// Prefix returns the prefix for the error, like error, fatal, etc.
func (te *TabletError) Prefix() string {
prefix := "error: "
switch te.ErrorCode {
case vtrpcpb.ErrorCode_QUERY_NOT_SERVED:
prefix = "retry: "
case vtrpcpb.ErrorCode_INTERNAL_ERROR:
prefix = "fatal: "
case vtrpcpb.ErrorCode_RESOURCE_EXHAUSTED:
prefix = "tx_pool_full: "
case vtrpcpb.ErrorCode_NOT_IN_TX:
prefix = "not_in_tx: "
}
// Special case for killed queries.
if te.SQLError == mysql.ErrServerLost {
prefix = prefix + "the query was killed either because it timed out or was canceled: "
}
return prefix
}
// RecordStats will record the error in the proper stat bucket
func (te *TabletError) RecordStats(queryServiceStats *QueryServiceStats) {
switch te.ErrorCode {
case vtrpcpb.ErrorCode_QUERY_NOT_SERVED:
queryServiceStats.InfoErrors.Add("Retry", 1)
case vtrpcpb.ErrorCode_INTERNAL_ERROR:
queryServiceStats.InfoErrors.Add("Fatal", 1)
case vtrpcpb.ErrorCode_RESOURCE_EXHAUSTED:
queryServiceStats.ErrorStats.Add("TxPoolFull", 1)
case vtrpcpb.ErrorCode_NOT_IN_TX:
queryServiceStats.ErrorStats.Add("NotInTx", 1)
default:
switch te.SQLError {
case mysql.ErrDupEntry:
queryServiceStats.InfoErrors.Add("DupKey", 1)
case mysql.ErrLockWaitTimeout, mysql.ErrLockDeadlock:
queryServiceStats.ErrorStats.Add("Deadlock", 1)
default:
queryServiceStats.ErrorStats.Add("Fail", 1)
}
}
}
func logError(queryServiceStats *QueryServiceStats) {
if x := recover(); x != nil {
terr, ok := x.(*TabletError)
if !ok {
log.Errorf("Uncaught panic:\n%v\n%s", x, tb.Stack(4))
queryServiceStats.InternalErrors.Add("Panic", 1)
return
}
log.Errorf("%v", terr)
}
}