Replace mockCommitErr test flag with faipoint (#208)

Signed-off-by: disksing <i@disksing.com>
This commit is contained in:
disksing 2021-07-05 13:40:19 +08:00 committed by GitHub
parent 54f5ebdc7e
commit fcf3da2ad7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 65 deletions

View File

@ -341,8 +341,8 @@ func (s *KVStore) getTimestampWithRetry(bo *Backoffer, txnScope string) (uint64,
// Then mockGetTSErrorInRetry will return retryable error when first retry.
// Before PR #8743, we don't cleanup txn after meet error such as error like: PD server timeout
// This may cause duplicate data to be written.
if val, e := util.EvalFailpoint("mockGetTSErrorInRetry"); e == nil {
if val.(bool) && !IsMockCommitErrorEnable() {
if val, e := util.EvalFailpoint("mockGetTSErrorInRetry"); e == nil && val.(bool) {
if _, e := util.EvalFailpoint("mockCommitErrorOpt"); e != nil {
err = tikverr.NewErrPDServerTimeout("mock PD timeout")
}
}

View File

@ -33,11 +33,11 @@
package tikv
import (
"sync/atomic"
"github.com/google/uuid"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/tikv/client-go/v2/internal/locate"
"github.com/tikv/client-go/v2/util"
pd "github.com/tikv/pd/client"
)
@ -65,20 +65,18 @@ func NewTestTiKVStore(client Client, pdClient pd.Client, clientHijack func(Clien
return tikvStore, errors.Trace(err)
}
// mockCommitErrorEnable uses to enable `mockCommitError` and only mock error once.
var mockCommitErrorEnable = int64(0)
// MockCommitErrorEnable exports for gofail testing.
func MockCommitErrorEnable() {
atomic.StoreInt64(&mockCommitErrorEnable, 1)
failpoint.Enable(`tikvclient/mockCommitErrorOpt`, "return(true)")
}
// MockCommitErrorDisable exports for gofail testing.
func MockCommitErrorDisable() {
atomic.StoreInt64(&mockCommitErrorEnable, 0)
failpoint.Disable(`tikvclient/mockCommitErrorOpt`)
}
// IsMockCommitErrorEnable exports for gofail testing.
func IsMockCommitErrorEnable() bool {
return atomic.LoadInt64(&mockCommitErrorEnable) == 1
_, err := util.EvalFailpoint(`mockCommitErrorOpt`)
return err == nil
}

View File

@ -1,52 +0,0 @@
// Copyright 2021 TiKV Authors
//
// 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.
// NOTE: The code in this file is based on code from the
// TiDB project, licensed under the Apache License v 2.0
//
// https://github.com/pingcap/tidb/tree/cc5e161ac06827589c4966674597c137cc9e809c/store/tikv/tikv_test.go
//
// Copyright 2018 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 tikv
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestBasicFunc(t *testing.T) {
if IsMockCommitErrorEnable() {
defer MockCommitErrorEnable()
} else {
defer MockCommitErrorDisable()
}
MockCommitErrorEnable()
assert.True(t, IsMockCommitErrorEnable())
MockCommitErrorDisable()
assert.False(t, IsMockCommitErrorEnable())
}

View File

@ -47,6 +47,7 @@ import (
"github.com/dgryski/go-farm"
"github.com/opentracing/opentracing-go"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/kvrpcpb"
tikverr "github.com/tikv/client-go/v2/error"
"github.com/tikv/client-go/v2/internal/retry"
@ -331,9 +332,9 @@ func (txn *KVTxn) Commit(ctx context.Context) error {
}
defer txn.close()
if val, err := util.EvalFailpoint("mockCommitError"); err == nil {
if val.(bool) && IsMockCommitErrorEnable() {
MockCommitErrorDisable()
if val, err := util.EvalFailpoint("mockCommitError"); err == nil && val.(bool) {
if _, err := util.EvalFailpoint("mockCommitErrorOpt"); err == nil {
failpoint.Disable("tikvclient/mockCommitErrorOpt")
return errors.New("mock commit error")
}
}