forked from pingcap/tiflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
etcd/client(ticdc): add retry operation for etcd transaction api (pin…
…gcap#4248) (pingcap#4474) close pingcap#4248
- Loading branch information
1 parent
19b9177
commit ed41f5c
Showing
9 changed files
with
306 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright 2021 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package errorutil | ||
|
||
import ( | ||
dmysql "github.com/go-sql-driver/mysql" | ||
"github.com/pingcap/errors" | ||
tddl "github.com/pingcap/tidb/ddl" | ||
"github.com/pingcap/tidb/infoschema" | ||
"github.com/pingcap/tidb/parser/mysql" | ||
v3rpc "go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes" | ||
) | ||
|
||
// IsIgnorableMySQLDDLError is used to check what error can be ignored | ||
// we can get error code from: | ||
// infoschema's error definition: https://github.com/pingcap/tidb/blob/master/infoschema/infoschema.go | ||
// DDL's error definition: https://github.com/pingcap/tidb/blob/master/ddl/ddl.go | ||
// tidb/mysql error code definition: https://github.com/pingcap/tidb/blob/master/mysql/errcode.go | ||
func IsIgnorableMySQLDDLError(err error) bool { | ||
err = errors.Cause(err) | ||
mysqlErr, ok := err.(*dmysql.MySQLError) | ||
if !ok { | ||
return false | ||
} | ||
|
||
errCode := errors.ErrCode(mysqlErr.Number) | ||
switch errCode { | ||
case infoschema.ErrDatabaseExists.Code(), infoschema.ErrDatabaseDropExists.Code(), | ||
infoschema.ErrTableExists.Code(), infoschema.ErrTableDropExists.Code(), | ||
infoschema.ErrColumnExists.Code(), infoschema.ErrIndexExists.Code(), | ||
infoschema.ErrKeyNotExists.Code(), tddl.ErrCantDropFieldOrKey.Code(), | ||
mysql.ErrDupKeyName, mysql.ErrSameNamePartition, | ||
mysql.ErrDropPartitionNonExistent, mysql.ErrMultiplePriKey: | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
// IsRetryableEtcdError is used to check what error can be ignored for an etcd Txn | ||
func IsRetryableEtcdError(err error) bool { | ||
etcdErr := errors.Cause(err) | ||
|
||
switch etcdErr { | ||
// Etcd ResourceExhausted errors, may recover after some time | ||
case v3rpc.ErrNoSpace, v3rpc.ErrTooManyRequests: | ||
return true | ||
// Etcd Unavailable errors, may be available after some time | ||
// https://github.com/etcd-io/etcd/pull/9934/files#diff-6d8785d0c9eaf96bc3e2b29c36493c04R162-R167 | ||
// ErrStopped: | ||
// one of the etcd nodes stopped from failure injection | ||
// ErrNotCapable: | ||
// capability check has not been done (in the beginning) | ||
case v3rpc.ErrNoLeader, v3rpc.ErrLeaderChanged, v3rpc.ErrNotCapable, v3rpc.ErrStopped, v3rpc.ErrTimeout, | ||
v3rpc.ErrTimeoutDueToLeaderFail, v3rpc.ErrGRPCTimeoutDueToConnectionLost, v3rpc.ErrUnhealthy: | ||
return true | ||
default: | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2021 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package errorutil | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/go-sql-driver/mysql" | ||
"github.com/pingcap/tidb/infoschema" | ||
tmysql "github.com/pingcap/tidb/parser/mysql" | ||
"github.com/stretchr/testify/require" | ||
v3rpc "go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes" | ||
) | ||
|
||
func newMysqlErr(number uint16, message string) *mysql.MySQLError { | ||
return &mysql.MySQLError{ | ||
Number: number, | ||
Message: message, | ||
} | ||
} | ||
|
||
func TestIgnoreMysqlDDLError(t *testing.T) { | ||
cases := []struct { | ||
err error | ||
ret bool | ||
}{ | ||
{errors.New("raw error"), false}, | ||
{newMysqlErr(tmysql.ErrDupKeyName, "Error: Duplicate key name 'some_key'"), true}, | ||
{newMysqlErr(uint16(infoschema.ErrDatabaseExists.Code()), "Can't create database"), true}, | ||
{newMysqlErr(uint16(infoschema.ErrAccessDenied.Code()), "Access denied for user"), false}, | ||
} | ||
|
||
for _, item := range cases { | ||
require.Equal(t, item.ret, IsIgnorableMySQLDDLError(item.err)) | ||
} | ||
} | ||
|
||
func TestIsRetryableEtcdError(t *testing.T) { | ||
cases := []struct { | ||
err error | ||
ret bool | ||
}{ | ||
{nil, false}, | ||
{v3rpc.ErrCorrupt, false}, | ||
|
||
{v3rpc.ErrGRPCTimeoutDueToConnectionLost, true}, | ||
{v3rpc.ErrTimeoutDueToLeaderFail, true}, | ||
{v3rpc.ErrNoSpace, true}, | ||
} | ||
|
||
for _, item := range cases { | ||
require.Equal(t, item.ret, IsRetryableEtcdError(item.err)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.