Skip to content

Commit

Permalink
Throttled transactions return MySQL error code 1041 ER_OUT_OF_RESOURC…
Browse files Browse the repository at this point in the history
…ES (vitessio#12949)

This error code seems better suited to represent the fact that transactions are
being throttled by the server due to some form of resource contention than the
current code 1203 ER_TOO_MANY_USER_CONNECTIONS.

Signed-off-by: Eduardo J. Ortega U <[email protected]>
  • Loading branch information
ejortegau authored and timvaillancourt committed May 10, 2023
1 parent cfdbd1e commit 7690450
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
9 changes: 8 additions & 1 deletion go/mysql/sql_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"regexp"
"strconv"
"strings"

"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
Expand Down Expand Up @@ -135,7 +136,11 @@ func mapToSQLErrorFromErrorCode(err error, msg string) *SQLError {
ss = SSAccessDeniedError
case vtrpcpb.Code_RESOURCE_EXHAUSTED:
num = demuxResourceExhaustedErrors(err.Error())
ss = SSClientError
// 1041 ER_OUT_OF_RESOURCES has SQLSTATE HYOOO as per https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_out_of_resources,
// so don't override it here in that case.
if num != EROutOfResources {
ss = SSClientError
}
case vtrpcpb.Code_UNIMPLEMENTED:
num = ERNotSupportedYet
ss = SSClientError
Expand Down Expand Up @@ -222,6 +227,8 @@ func demuxResourceExhaustedErrors(msg string) int {
switch {
case isGRPCOverflowRE.Match([]byte(msg)):
return ERNetPacketTooLarge
case strings.Contains(msg, "Transaction throttled"):
return EROutOfResources
default:
return ERTooManyUserConnections
}
Expand Down
24 changes: 23 additions & 1 deletion go/mysql/sql_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package mysql

import (
"fmt"
"testing"

"vitess.io/vitess/go/vt/proto/vtrpc"
Expand All @@ -25,7 +26,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestDumuxResourceExhaustedErrors(t *testing.T) {
func TestDemuxResourceExhaustedErrors(t *testing.T) {
type testCase struct {
msg string
want int
Expand All @@ -42,6 +43,7 @@ func TestDumuxResourceExhaustedErrors(t *testing.T) {
// This should be explicitly handled by returning ERNetPacketTooLarge from the execturo directly
// and therefore shouldn't need to be teased out of another error.
{"in-memory row count exceeded allowed limit of 13", ERTooManyUserConnections},
{"rpc error: code = ResourceExhausted desc = Transaction throttled", EROutOfResources},
}

for _, c := range cases {
Expand Down Expand Up @@ -151,6 +153,26 @@ func TestNewSQLErrorFromError(t *testing.T) {
num: ERNoDb,
ss: SSNoDB,
},
{
err: fmt.Errorf("just some random text here"),
num: ERUnknownError,
ss: SSUnknownSQLState,
},
{
err: fmt.Errorf("task error: Column 'val' cannot be null (errno 1048) (sqlstate 23000) during query: insert into _edf4846d_ab65_11ed_abb1_0a43f95f28a3_20230213061619_vrepl(id,val,ts) values (1,2,'2023-02-13 04:46:16'), (2,3,'2023-02-13 04:46:16'), (3,null,'2023-02-13 04:46:16')"),
num: ERBadNullError,
ss: SSConstraintViolation,
},
{
err: vterrors.Wrapf(fmt.Errorf("Column 'val' cannot be null (errno 1048) (sqlstate 23000) during query: insert into _edf4846d_ab65_11ed_abb1_0a43f95f28a3_20230213061619_vrepl(id,val,ts) values (1,2,'2023-02-13 04:46:16'), (2,3,'2023-02-13 04:46:16'), (3,null,'2023-02-13 04:46:16')"), "task error: %d", 17),
num: ERBadNullError,
ss: SSConstraintViolation,
},
{
err: vterrors.Errorf(vtrpc.Code_RESOURCE_EXHAUSTED, "vttablet: rpc error: code = ResourceExhausted desc = Transaction throttled"),
num: EROutOfResources,
ss: SSUnknownSQLState,
},
}

for _, tc := range tCases {
Expand Down

0 comments on commit 7690450

Please sign in to comment.