github.com/KinWaiYuen/client-go/v2@v2.5.4/tikv/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 // NOTE: The code in this file is based on code from the 16 // TiDB project, licensed under the Apache License v 2.0 17 // 18 // https://github.com/pingcap/tidb/tree/cc5e161ac06827589c4966674597c137cc9e809c/store/tikv/test_probe.go 19 // 20 21 // Copyright 2021 PingCAP, Inc. 22 // 23 // Licensed under the Apache License, Version 2.0 (the "License"); 24 // you may not use this file except in compliance with the License. 25 // You may obtain a copy of the License at 26 // 27 // http://www.apache.org/licenses/LICENSE-2.0 28 // 29 // Unless required by applicable law or agreed to in writing, software 30 // distributed under the License is distributed on an "AS IS" BASIS, 31 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 32 // See the License for the specific language governing permissions and 33 // limitations under the License. 34 35 package tikv 36 37 import ( 38 "context" 39 40 "github.com/KinWaiYuen/client-go/v2/internal/retry" 41 "github.com/KinWaiYuen/client-go/v2/tikvrpc" 42 "github.com/KinWaiYuen/client-go/v2/txnkv/transaction" 43 "github.com/KinWaiYuen/client-go/v2/txnkv/txnlock" 44 "github.com/KinWaiYuen/client-go/v2/txnkv/txnsnapshot" 45 "github.com/pingcap/kvproto/pkg/metapb" 46 pd "github.com/tikv/pd/client" 47 ) 48 49 // StoreProbe wraps KVSTore and exposes internal states for testing purpose. 50 type StoreProbe struct { 51 *KVStore 52 } 53 54 // NewLockResolver creates a new LockResolver instance. 55 func (s StoreProbe) NewLockResolver() LockResolverProbe { 56 txnLockResolver := txnlock.LockResolverProbe{LockResolver: txnlock.NewLockResolver(s.KVStore)} 57 return LockResolverProbe{&txnLockResolver} 58 } 59 60 // Begin starts a transaction. 61 func (s StoreProbe) Begin() (transaction.TxnProbe, error) { 62 txn, err := s.KVStore.Begin() 63 return transaction.TxnProbe{KVTxn: txn}, err 64 } 65 66 // GetSnapshot returns a snapshot. 67 func (s StoreProbe) GetSnapshot(ts uint64) txnsnapshot.SnapshotProbe { 68 snap := s.KVStore.GetSnapshot(ts) 69 return txnsnapshot.SnapshotProbe{KVSnapshot: snap} 70 } 71 72 // SetRegionCachePDClient replaces pd client inside region cache. 73 func (s StoreProbe) SetRegionCachePDClient(client pd.Client) { 74 s.regionCache.SetPDClient(client) 75 } 76 77 // ClearTxnLatches clears store's txn latch scheduler. 78 func (s StoreProbe) ClearTxnLatches() { 79 if s.txnLatches != nil { 80 s.txnLatches.Close() 81 s.txnLatches = nil 82 } 83 } 84 85 // SendTxnHeartbeat renews a txn's ttl. 86 func (s StoreProbe) SendTxnHeartbeat(ctx context.Context, key []byte, startTS uint64, ttl uint64) (uint64, error) { 87 bo := retry.NewBackofferWithVars(ctx, transaction.PrewriteMaxBackoff, nil) 88 newTTL, _, err := transaction.SendTxnHeartBeat(bo, s.KVStore, key, startTS, ttl) 89 return newTTL, err 90 } 91 92 // LoadSafePoint from safepoint kv. 93 func (s StoreProbe) LoadSafePoint() (uint64, error) { 94 return loadSafePoint(s.GetSafePointKV()) 95 } 96 97 // SaveSafePoint saves safepoint to kv. 98 func (s StoreProbe) SaveSafePoint(v uint64) error { 99 return saveSafePoint(s.GetSafePointKV(), v) 100 } 101 102 // SetRegionCacheStore is used to set a store in region cache, for testing only 103 func (s StoreProbe) SetRegionCacheStore(id uint64, storeType tikvrpc.EndpointType, state uint64, labels []*metapb.StoreLabel) { 104 s.regionCache.SetRegionCacheStore(id, storeType, state, labels) 105 } 106 107 // SetSafeTS is used to set safeTS for the store with `storeID` 108 func (s StoreProbe) SetSafeTS(storeID, safeTS uint64) { 109 s.setSafeTS(storeID, safeTS) 110 } 111 112 // LockResolverProbe wraps a LockResolver and exposes internal stats for testing purpose. 113 type LockResolverProbe struct { 114 *txnlock.LockResolverProbe 115 } 116 117 // NewLockResolverProb create a LockResolverProbe from KVStore. 118 func NewLockResolverProb(r *txnlock.LockResolver) *LockResolverProbe { 119 resolver := txnlock.LockResolverProbe{LockResolver: r} 120 return &LockResolverProbe{&resolver} 121 } 122 123 // ResolveLock resolves single lock. 124 func (l LockResolverProbe) ResolveLock(ctx context.Context, lock *txnlock.Lock) error { 125 bo := retry.NewBackofferWithVars(ctx, transaction.ConfigProbe{}.GetPessimisticLockMaxBackoff(), nil) 126 return l.LockResolverProbe.ResolveLock(bo, lock) 127 } 128 129 // ResolvePessimisticLock resolves single pessimistic lock. 130 func (l LockResolverProbe) ResolvePessimisticLock(ctx context.Context, lock *txnlock.Lock) error { 131 bo := retry.NewBackofferWithVars(ctx, transaction.ConfigProbe{}.GetPessimisticLockMaxBackoff(), nil) 132 return l.LockResolverProbe.ResolvePessimisticLock(bo, lock) 133 } 134 135 // ConfigProbe exposes configurations and global variables for testing purpose. 136 type ConfigProbe struct{} 137 138 // GetTxnCommitBatchSize returns the batch size to commit txn. 139 func (c ConfigProbe) GetTxnCommitBatchSize() uint64 { 140 return transaction.ConfigProbe{}.GetTxnCommitBatchSize() 141 } 142 143 // GetBigTxnThreshold returns the txn size to be considered as big txn. 144 func (c ConfigProbe) GetBigTxnThreshold() int { 145 // bigTxnThreshold : transaction involves keys exceed this threshold can be treated as `big transaction`. 146 const bigTxnThreshold = 16 147 return bigTxnThreshold 148 } 149 150 // GetScanBatchSize returns the batch size to scan ranges. 151 func (c ConfigProbe) GetScanBatchSize() int { 152 return txnsnapshot.ConfigProbe{}.GetScanBatchSize() 153 } 154 155 // GetDefaultLockTTL returns the default lock TTL. 156 func (c ConfigProbe) GetDefaultLockTTL() uint64 { 157 return transaction.ConfigProbe{}.GetDefaultLockTTL() 158 } 159 160 // GetTTLFactor returns the factor to calculate txn TTL. 161 func (c ConfigProbe) GetTTLFactor() int { 162 return transaction.ConfigProbe{}.GetTTLFactor() 163 } 164 165 // GetGetMaxBackoff returns the max sleep for get command. 166 func (c ConfigProbe) GetGetMaxBackoff() int { 167 return txnsnapshot.ConfigProbe{}.GetGetMaxBackoff() 168 } 169 170 // LoadPreSplitDetectThreshold returns presplit detect threshold config. 171 func (c ConfigProbe) LoadPreSplitDetectThreshold() uint32 { 172 return transaction.ConfigProbe{}.LoadPreSplitDetectThreshold() 173 } 174 175 // StorePreSplitDetectThreshold updates presplit detect threshold config. 176 func (c ConfigProbe) StorePreSplitDetectThreshold(v uint32) { 177 transaction.ConfigProbe{}.StorePreSplitDetectThreshold(v) 178 } 179 180 // LoadPreSplitSizeThreshold returns presplit size threshold config. 181 func (c ConfigProbe) LoadPreSplitSizeThreshold() uint32 { 182 return transaction.ConfigProbe{}.LoadPreSplitSizeThreshold() 183 } 184 185 // StorePreSplitSizeThreshold updates presplit size threshold config. 186 func (c ConfigProbe) StorePreSplitSizeThreshold(v uint32) { 187 transaction.ConfigProbe{}.StorePreSplitSizeThreshold(v) 188 } 189 190 // SetOracleUpdateInterval sets the interval of updating cached ts. 191 func (c ConfigProbe) SetOracleUpdateInterval(v int) { 192 oracleUpdateInterval = v 193 }