From 3fa4fb9d9119c6bbb7eda8131eef297395428a23 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Sat, 6 May 2023 13:16:56 +0800 Subject: [PATCH] log: let Write Conflict error message respect redact setting (#43486) (#43489) close pingcap/tidb#43485 --- kv/error.go | 27 +++++++++++++++++++++------ store/driver/txn/driver_test.go | 10 ++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/kv/error.go b/kv/error.go index 9a6878d57cebd..d8c344324fe80 100644 --- a/kv/error.go +++ b/kv/error.go @@ -32,8 +32,13 @@ var ( // ErrTxnRetryable is used when KV store occurs retryable error which SQL layer can safely retry the transaction. // When using TiKV as the storage node, the error is returned ONLY when lock not found (txnLockNotFound) in Commit, // subject to change it in the future. - ErrTxnRetryable = dbterror.ClassKV.NewStdErr(mysql.ErrTxnRetryable, - pmysql.Message(mysql.MySQLErrName[mysql.ErrTxnRetryable].Raw+TxnRetryableMark, []int{0})) + ErrTxnRetryable = dbterror.ClassKV.NewStdErr( + mysql.ErrTxnRetryable, + pmysql.Message( + mysql.MySQLErrName[mysql.ErrTxnRetryable].Raw+TxnRetryableMark, + mysql.MySQLErrName[mysql.ErrTxnRetryable].RedactArgPos, + ), + ) // ErrCannotSetNilValue is the error when sets an empty value. ErrCannotSetNilValue = dbterror.ClassKV.NewStd(mysql.ErrCannotSetNilValue) // ErrInvalidTxn is the error when commits or rollbacks in an invalid transaction. @@ -47,11 +52,21 @@ var ( // ErrNotImplemented returns when a function is not implemented yet. ErrNotImplemented = dbterror.ClassKV.NewStd(mysql.ErrNotImplemented) // ErrWriteConflict is the error when the commit meets an write conflict error. - ErrWriteConflict = dbterror.ClassKV.NewStdErr(mysql.ErrWriteConflict, - pmysql.Message(mysql.MySQLErrName[mysql.ErrWriteConflict].Raw+" "+TxnRetryableMark, []int{3})) + ErrWriteConflict = dbterror.ClassKV.NewStdErr( + mysql.ErrWriteConflict, + pmysql.Message( + mysql.MySQLErrName[mysql.ErrWriteConflict].Raw+" "+TxnRetryableMark, + mysql.MySQLErrName[mysql.ErrWriteConflict].RedactArgPos, + ), + ) // ErrWriteConflictInTiDB is the error when the commit meets an write conflict error when local latch is enabled. - ErrWriteConflictInTiDB = dbterror.ClassKV.NewStdErr(mysql.ErrWriteConflictInTiDB, - pmysql.Message(mysql.MySQLErrName[mysql.ErrWriteConflictInTiDB].Raw+" "+TxnRetryableMark, nil)) + ErrWriteConflictInTiDB = dbterror.ClassKV.NewStdErr( + mysql.ErrWriteConflictInTiDB, + pmysql.Message( + mysql.MySQLErrName[mysql.ErrWriteConflictInTiDB].Raw+" "+TxnRetryableMark, + mysql.MySQLErrName[mysql.ErrWriteConflictInTiDB].RedactArgPos, + ), + ) // ErrLockExpire is the error when the lock is expired. ErrLockExpire = dbterror.ClassTiKV.NewStd(mysql.ErrLockExpire) // ErrAssertionFailed is the error when an assertion fails. diff --git a/store/driver/txn/driver_test.go b/store/driver/txn/driver_test.go index 4ce8fb1f43693..26b2db3a1f699 100644 --- a/store/driver/txn/driver_test.go +++ b/store/driver/txn/driver_test.go @@ -16,6 +16,7 @@ package txn import ( "testing" + "github.com/pingcap/errors" "github.com/pingcap/kvproto/pkg/kvrpcpb" "github.com/pingcap/tidb/kv" "github.com/stretchr/testify/require" @@ -66,4 +67,13 @@ func TestWriteConflictPrettyFormat(t *testing.T) { "reason=Optimistic " + kv.TxnRetryableMark require.EqualError(t, newWriteConflictError(conflict), expectedStr) + + // test log redaction + original := errors.RedactLogEnabled.Load() + errors.RedactLogEnabled.Store(true) + defer func() { errors.RedactLogEnabled.Store(original) }() + expectedStr = "[kv:9007]Write conflict, " + + "txnStartTS=399402937522847774, conflictStartTS=399402937719455772, conflictCommitTS=399402937719455773, " + + "key=????, reason=Optimistic " + kv.TxnRetryableMark + require.EqualError(t, newWriteConflictError(conflict), expectedStr) }