forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tablet_error.go
108 lines (96 loc) · 2.18 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
// 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"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/mysql"
"github.com/youtube/vitess/go/tb"
)
const (
FAIL = iota
RETRY
FATAL
TX_POOL_FULL
NOT_IN_TX
)
type TabletError struct {
ErrorType int
Message string
SqlError int
}
// This is how go-mysql exports its error number
type hasNumber interface {
Number() int
}
func NewTabletError(errorType int, format string, args ...interface{}) *TabletError {
return &TabletError{errorType, fmt.Sprintf(format, args...), 0}
}
func NewTabletErrorSql(errorType int, err error) *TabletError {
te := NewTabletError(errorType, "%s", err)
if sqlErr, ok := err.(hasNumber); ok {
te.SqlError = sqlErr.Number()
}
return te
}
func (te *TabletError) Error() string {
format := "error: %s"
switch te.ErrorType {
case RETRY:
format = "retry: %s"
case FATAL:
format = "fatal: %s"
case TX_POOL_FULL:
format = "tx_pool_full: %s"
case NOT_IN_TX:
format = "not_in_tx: %s"
}
return fmt.Sprintf(format, te.Message)
}
func (te *TabletError) RecordStats() {
switch te.ErrorType {
case RETRY:
errorStats.Add("Retry", 1)
case FATAL:
errorStats.Add("Fatal", 1)
case TX_POOL_FULL:
errorStats.Add("TxPoolFull", 1)
case NOT_IN_TX:
errorStats.Add("NotInTx", 1)
default:
switch te.SqlError {
case mysql.DUP_ENTRY:
errorStats.Add("DupKey", 1)
case mysql.LOCK_WAIT_TIMEOUT, mysql.LOCK_DEADLOCK:
errorStats.Add("Deadlock", 1)
default:
errorStats.Add("Fail", 1)
}
}
}
func handleError(err *error, logStats *sqlQueryStats) {
if logStats != nil {
logStats.Send()
}
if x := recover(); x != nil {
terr, ok := x.(*TabletError)
if !ok {
log.Errorf("Uncaught panic:\n%v\n%s", x, tb.Stack(4))
*err = NewTabletError(FAIL, "%v: uncaught panic", x)
errorStats.Add("Panic", 1)
return
}
*err = terr
terr.RecordStats()
if terr.ErrorType == RETRY { // Retry errors are too spammy
return
}
log.Errorf("%s", terr.Message)
}
}
func logError() {
if x := recover(); x != nil {
log.Errorf("%s", x.(*TabletError).Message)
}
}