github.com/KinWaiYuen/client-go/v2@v2.5.4/txnkv/txnlock/test_probe.go (about) 1 // Copyright 2021 TiKV Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package txnlock 16 17 import ( 18 "github.com/KinWaiYuen/client-go/v2/internal/locate" 19 "github.com/KinWaiYuen/client-go/v2/internal/retry" 20 "github.com/pingcap/errors" 21 "github.com/pingcap/kvproto/pkg/kvrpcpb" 22 ) 23 24 // LockProbe exposes some lock utilities for testing purpose. 25 type LockProbe struct{} 26 27 // NewLockStatus returns a txn state that has been locked. 28 func (l LockProbe) NewLockStatus(keys [][]byte, useAsyncCommit bool, minCommitTS uint64) TxnStatus { 29 return TxnStatus{ 30 primaryLock: &kvrpcpb.LockInfo{ 31 Secondaries: keys, 32 UseAsyncCommit: useAsyncCommit, 33 MinCommitTs: minCommitTS, 34 }, 35 } 36 } 37 38 // GetPrimaryKeyFromTxnStatus returns the primary key of the transaction. 39 func (l LockProbe) GetPrimaryKeyFromTxnStatus(s TxnStatus) []byte { 40 return s.primaryLock.Key 41 } 42 43 // LockResolverProbe wraps a LockResolver and exposes internal stats for testing purpose. 44 type LockResolverProbe struct { 45 *LockResolver 46 } 47 48 // ResolveLockAsync tries to resolve a lock using the txn states. 49 func (l LockResolverProbe) ResolveLockAsync(bo *retry.Backoffer, lock *Lock, status TxnStatus) error { 50 return l.resolveLockAsync(bo, lock, status) 51 } 52 53 // ResolveLock resolves single lock. 54 func (l LockResolverProbe) ResolveLock(bo *retry.Backoffer, lock *Lock) error { 55 return l.resolveLock(bo, lock, TxnStatus{}, false, make(map[locate.RegionVerID]struct{})) 56 } 57 58 // ResolvePessimisticLock resolves single pessimistic lock. 59 func (l LockResolverProbe) ResolvePessimisticLock(bo *retry.Backoffer, lock *Lock) error { 60 return l.resolvePessimisticLock(bo, lock, make(map[locate.RegionVerID]struct{})) 61 } 62 63 // GetTxnStatus sends the CheckTxnStatus request to the TiKV server. 64 func (l LockResolverProbe) GetTxnStatus(bo *retry.Backoffer, txnID uint64, primary []byte, 65 callerStartTS, currentTS uint64, rollbackIfNotExist bool, forceSyncCommit bool, lockInfo *Lock) (TxnStatus, error) { 66 return l.getTxnStatus(bo, txnID, primary, callerStartTS, currentTS, rollbackIfNotExist, forceSyncCommit, lockInfo) 67 } 68 69 // GetTxnStatusFromLock queries tikv for a txn's status. 70 func (l LockResolverProbe) GetTxnStatusFromLock(bo *retry.Backoffer, lock *Lock, callerStartTS uint64, forceSyncCommit bool) (TxnStatus, error) { 71 return l.getTxnStatusFromLock(bo, lock, callerStartTS, forceSyncCommit) 72 } 73 74 // GetSecondariesFromTxnStatus returns the secondary locks from txn status. 75 func (l LockResolverProbe) GetSecondariesFromTxnStatus(status TxnStatus) [][]byte { 76 return status.primaryLock.GetSecondaries() 77 } 78 79 // SetMeetLockCallback is called whenever it meets locks. 80 func (l LockResolverProbe) SetMeetLockCallback(f func([]*Lock)) { 81 l.testingKnobs.meetLock = f 82 } 83 84 // CheckAllSecondaries checks the secondary locks of an async commit transaction to find out the final 85 // status of the transaction. 86 func (l LockResolverProbe) CheckAllSecondaries(bo *retry.Backoffer, lock *Lock, status *TxnStatus) error { 87 _, err := l.checkAllSecondaries(bo, lock, status) 88 return err 89 } 90 91 // IsErrorNotFound checks if an error is caused by txnNotFoundErr. 92 func (l LockResolverProbe) IsErrorNotFound(err error) bool { 93 _, ok := errors.Cause(err).(txnNotFoundErr) 94 return ok 95 } 96 97 // IsNonAsyncCommitLock checks if an error is nonAsyncCommitLock error. 98 func (l LockResolverProbe) IsNonAsyncCommitLock(err error) bool { 99 _, ok := errors.Cause(err).(*nonAsyncCommitLock) 100 return ok 101 }