Skip to content

Commit 2332614

Browse files
committed
reduce compare
Signed-off-by: husharp <[email protected]>
1 parent 6a19c69 commit 2332614

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

tikv/kv.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -547,17 +547,18 @@ func (s *KVStore) updateSafeTS(ctx context.Context) {
547547
tikvClient := s.GetTiKVClient()
548548
wg := &sync.WaitGroup{}
549549
wg.Add(len(stores))
550-
safeTS, txnScopeMap, err := s.buildTxnScopeMap(ctx)
550+
clusterTS, txnScopeMap, err := s.buildTxnScopeMap(ctx, stores)
551551
for _, store := range stores {
552552
storeID := store.StoreID()
553553
storeAddr := store.GetAddr()
554554
go func(ctx context.Context, wg *sync.WaitGroup, storeID uint64, storeAddr string) {
555555
defer wg.Done()
556556

557557
storeIDStr := strconv.Itoa(int(storeID))
558+
safeTS := clusterTS
558559
// If getting the minimum resolved timestamp from PD failed or returned 0, try to get it from TiKV.
559560
if safeTS == 0 || err != nil {
560-
resp, err := tikvClient.SendRequest(
561+
resp, e := tikvClient.SendRequest(
561562
ctx, storeAddr, tikvrpc.NewRequest(
562563
tikvrpc.CmdStoreSafeTS, &kvrpcpb.StoreSafeTSRequest{
563564
KeyRange: &kvrpcpb.KeyRange{
@@ -569,7 +570,7 @@ func (s *KVStore) updateSafeTS(ctx context.Context) {
569570
},
570571
), client.ReadTimeoutShort,
571572
)
572-
if err != nil {
573+
if e != nil {
573574
metrics.TiKVSafeTSUpdateCounter.WithLabelValues("fail", storeIDStr).Inc()
574575
logutil.BgLogger().Debug("update safeTS failed", zap.Error(err), zap.Uint64("store-id", storeID))
575576
return
@@ -591,19 +592,22 @@ func (s *KVStore) updateSafeTS(ctx context.Context) {
591592
}(ctx, wg, storeID, storeAddr)
592593
}
593594

594-
for txnScope, storeIDs := range txnScopeMap {
595-
s.updateMinSafeTS(txnScope, storeIDs)
595+
if clusterTS != 0 && err == nil {
596+
s.minSafeTS.Store(oracle.GlobalTxnScope, clusterTS)
597+
} else {
598+
for txnScope, storeIDs := range txnScopeMap {
599+
s.updateMinSafeTS(txnScope, storeIDs)
600+
}
596601
}
597602
wg.Wait()
598603
}
599604

600605
// build txnScopeMap and judge whether it is needed to get safeTS from PD.
601606
// - if stores label are global, return get cluster min resolved ts from pd.
602607
// - if contains dc label store, return try to get it from TiKV.
603-
func (s *KVStore) buildTxnScopeMap(ctx context.Context) (safeTS uint64, txnScopeMap map[string][]uint64, err error) {
608+
func (s *KVStore) buildTxnScopeMap(ctx context.Context, stores []*Store) (safeTS uint64, txnScopeMap map[string][]uint64, err error) {
604609
isGlobal := true
605610
txnScopeMap = make(map[string][]uint64)
606-
stores := s.regionCache.GetStoresByType(tikvrpc.TiKV)
607611
for _, store := range stores {
608612
txnScopeMap[oracle.GlobalTxnScope] = append(txnScopeMap[oracle.GlobalTxnScope], store.StoreID())
609613

0 commit comments

Comments
 (0)