From c3ae17fdc9dbf4f16364aa792fb457dd3d1fe3d3 Mon Sep 17 00:00:00 2001 From: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com> Date: Mon, 18 Nov 2024 15:04:05 +0200 Subject: [PATCH] Handle MySQL handler error codes Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com> --- go/mysql/sqlerror/constants.go | 21 +++++++++++++++++++ go/mysql/sqlerror/sql_error.go | 12 +++++++++++ go/mysql/sqlerror/sql_error_test.go | 21 +++++++++++++++++++ .../tabletmanager/vreplication/utils.go | 10 +++++++++ .../tabletmanager/vreplication/utils_test.go | 10 +++++++++ 5 files changed, 74 insertions(+) diff --git a/go/mysql/sqlerror/constants.go b/go/mysql/sqlerror/constants.go index da2351bf00d..674651d6fca 100644 --- a/go/mysql/sqlerror/constants.go +++ b/go/mysql/sqlerror/constants.go @@ -203,6 +203,7 @@ const ( ERBlobKeyWithoutLength = ErrorCode(1170) ERPrimaryCantHaveNull = ErrorCode(1171) ERTooManyRows = ErrorCode(1172) + ERErrorDuringCommit = ErrorCode(1180) ERLockOrActiveTransaction = ErrorCode(1192) ERUnknownSystemVariable = ErrorCode(1193) ERSetConstantsOnly = ErrorCode(1204) @@ -301,6 +302,26 @@ const ( ERServerIsntAvailable = ErrorCode(3168) ) +// HandlerErrorCode is for errors thrown by the handler, and which are then embedded in other errors. +type HandlerErrorCode uint16 + +func (e HandlerErrorCode) ToString() string { + return strconv.FormatUint(uint64(e), 10) +} + +const ( + HaErrCrashed = HandlerErrorCode(126) + HaErrTooBigRow = HandlerErrorCode(139) + HaErrFoundDuppUnique = HandlerErrorCode(141) + HaErrLockWaitTimeout = HandlerErrorCode(146) + HaErrLockTableFull = HandlerErrorCode(147) + HaErrLockDeadlock = HandlerErrorCode(149) + HaErrTooManyConcurrentTrxs = HandlerErrorCode(177) + HaErrNotInLockPartitions = HandlerErrorCode(178) + HaErrQueryInterrupted = HandlerErrorCode(196) + HaErrDiskFullNowait = HandlerErrorCode(204) +) + // Sql states for errors. // Originally found in include/mysql/sql_state.h const ( diff --git a/go/mysql/sqlerror/sql_error.go b/go/mysql/sqlerror/sql_error.go index f2a5fb46388..4600f0927cc 100644 --- a/go/mysql/sqlerror/sql_error.go +++ b/go/mysql/sqlerror/sql_error.go @@ -53,6 +53,18 @@ func NewSQLError(number ErrorCode, sqlState string, msg string) *SQLError { } } +var handlerErrExtract = regexp.MustCompile(`Got error ([0-9]*) [-] .* (from storage engine|during COMMIT|during ROLLBACK)`) + +func (se *SQLError) HaErrorCode() HandlerErrorCode { + match := handlerErrExtract.FindStringSubmatch(se.Message) + if len(match) >= 1 { + if code, err := strconv.ParseUint(match[1], 10, 16); err == nil { + return HandlerErrorCode(code) + } + } + return 0 +} + // Error implements the error interface func (se *SQLError) Error() string { var buf strings.Builder diff --git a/go/mysql/sqlerror/sql_error_test.go b/go/mysql/sqlerror/sql_error_test.go index b38cec26388..9e73138d60f 100644 --- a/go/mysql/sqlerror/sql_error_test.go +++ b/go/mysql/sqlerror/sql_error_test.go @@ -57,6 +57,7 @@ func TestNewSQLErrorFromError(t *testing.T) { var tCases = []struct { err error num ErrorCode + ha HandlerErrorCode ss string }{ { @@ -179,6 +180,24 @@ func TestNewSQLErrorFromError(t *testing.T) { num: ERDupEntry, ss: SSConstraintViolation, }, + { + err: fmt.Errorf("ERROR HY000: Got error 204 - 'No more room in disk' during COMMIT"), + num: ERUnknownError, + ss: SSUnknownSQLState, + ha: HaErrDiskFullNowait, + }, + { + err: fmt.Errorf("COMMIT failed w/ error: Got error 204 - 'No more room in disk' during COMMIT (errno 1180) (sqlstate HY000) during query: commit"), + num: ERErrorDuringCommit, + ss: SSUnknownSQLState, + ha: HaErrDiskFullNowait, + }, + { + err: fmt.Errorf("COMMIT failed w/ error: Got error 149 - 'Lock deadlock; Retry transaction' during COMMIT (errno 1180) (sqlstate HY000) during query: commit"), + num: ERErrorDuringCommit, + ss: SSUnknownSQLState, + ha: HaErrLockDeadlock, + }, } for _, tc := range tCases { @@ -187,6 +206,8 @@ func TestNewSQLErrorFromError(t *testing.T) { require.ErrorAs(t, NewSQLErrorFromError(tc.err), &err) assert.Equal(t, tc.num, err.Number()) assert.Equal(t, tc.ss, err.SQLState()) + ha := err.HaErrorCode() + assert.Equal(t, tc.ha, ha) }) } } diff --git a/go/vt/vttablet/tabletmanager/vreplication/utils.go b/go/vt/vttablet/tabletmanager/vreplication/utils.go index bb1c469cc93..66e0ca23406 100644 --- a/go/vt/vttablet/tabletmanager/vreplication/utils.go +++ b/go/vt/vttablet/tabletmanager/vreplication/utils.go @@ -232,6 +232,16 @@ func isUnrecoverableError(err error) bool { sqlerror.ERWrongValueCountOnRow: log.Errorf("Got unrecoverable error: %v", sqlErr) return true + case sqlerror.ERErrorDuringCommit: + switch sqlErr.HaErrorCode() { + case + sqlerror.HaErrCrashed, + sqlerror.HaErrTooBigRow, + sqlerror.HaErrFoundDuppUnique, + sqlerror.HaErrDiskFullNowait: + log.Errorf("Got unrecoverable error: %v", sqlErr) + return true + } } return false } diff --git a/go/vt/vttablet/tabletmanager/vreplication/utils_test.go b/go/vt/vttablet/tabletmanager/vreplication/utils_test.go index 69a57c34341..15093e299fc 100644 --- a/go/vt/vttablet/tabletmanager/vreplication/utils_test.go +++ b/go/vt/vttablet/tabletmanager/vreplication/utils_test.go @@ -152,6 +152,16 @@ func TestIsUnrecoverableError(t *testing.T) { err: sqlerror.NewSQLError(sqlerror.ERDataOutOfRange, "data out of range", "test"), expected: true, }, + { + name: "SQL error with HaErrDiskFullNowait error", + err: sqlerror.NewSQLError(sqlerror.ERErrorDuringCommit, "unknown", "ERROR HY000: Got error 204 - 'No more room in disk' during COMMIT"), + expected: true, + }, + { + name: "SQL error with HaErrLockDeadlock error", + err: sqlerror.NewSQLError(sqlerror.ERErrorDuringCommit, "unknown", "ERROR HY000: Got error 149 - 'Lock deadlock; Retry transaction' during COMMIT"), + expected: false, + }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) {