@@ -547,24 +547,14 @@ func (s *KVStore) updateSafeTS(ctx context.Context) {
547
547
tikvClient := s .GetTiKVClient ()
548
548
wg := & sync.WaitGroup {}
549
549
wg .Add (len (stores ))
550
+ safeTS , txnScopeMap , err := s .buildTxnScopeMap (ctx )
550
551
for _ , store := range stores {
551
552
storeID := store .StoreID ()
552
553
storeAddr := store .GetAddr ()
553
554
go func (ctx context.Context , wg * sync.WaitGroup , storeID uint64 , storeAddr string ) {
554
555
defer wg .Done ()
555
556
556
- var (
557
- safeTS uint64
558
- err error
559
- )
560
557
storeIDStr := strconv .Itoa (int (storeID ))
561
- // Try to get the minimum resolved timestamp of the store from PD.
562
- if s .pdHttpClient != nil {
563
- safeTS , err = s .pdHttpClient .GetStoreMinResolvedTS (ctx , storeID )
564
- if err != nil {
565
- logutil .BgLogger ().Debug ("get resolved TS from PD failed" , zap .Error (err ), zap .Uint64 ("store-id" , storeID ))
566
- }
567
- }
568
558
// If getting the minimum resolved timestamp from PD failed or returned 0, try to get it from TiKV.
569
559
if safeTS == 0 || err != nil {
570
560
resp , err := tikvClient .SendRequest (
@@ -601,18 +591,37 @@ func (s *KVStore) updateSafeTS(ctx context.Context) {
601
591
}(ctx , wg , storeID , storeAddr )
602
592
}
603
593
604
- txnScopeMap := make (map [string ][]uint64 )
594
+ for txnScope , storeIDs := range txnScopeMap {
595
+ s .updateMinSafeTS (txnScope , storeIDs )
596
+ }
597
+ wg .Wait ()
598
+ }
599
+
600
+ // build txnScopeMap and judge whether it is needed to get safeTS from PD.
601
+ // - if stores label are global, return get cluster min resolved ts from pd.
602
+ // - 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 ) {
604
+ isGlobal := true
605
+ txnScopeMap = make (map [string ][]uint64 )
606
+ stores := s .regionCache .GetStoresByType (tikvrpc .TiKV )
605
607
for _ , store := range stores {
606
608
txnScopeMap [oracle .GlobalTxnScope ] = append (txnScopeMap [oracle .GlobalTxnScope ], store .StoreID ())
607
609
608
610
if label , ok := store .GetLabelValue (DCLabelKey ); ok {
609
611
txnScopeMap [label ] = append (txnScopeMap [label ], store .StoreID ())
612
+ isGlobal = false
610
613
}
611
614
}
612
- for txnScope , storeIDs := range txnScopeMap {
613
- s .updateMinSafeTS (txnScope , storeIDs )
615
+
616
+ // Try to get the minimum resolved timestamp of the cluster from PD.
617
+ if s .pdHttpClient != nil && isGlobal {
618
+ safeTS , err = s .pdHttpClient .GetClusterMinResolvedTS (ctx )
619
+ if err != nil {
620
+ logutil .BgLogger ().Debug ("get resolved TS from PD failed" , zap .Error (err ))
621
+ }
614
622
}
615
- wg .Wait ()
623
+
624
+ return safeTS , txnScopeMap , err
616
625
}
617
626
618
627
// Variables defines the variables used by TiKV storage.
0 commit comments