@@ -542,33 +542,13 @@ func (s *KVStore) safeTSUpdater() {
542
542
}
543
543
}
544
544
545
- var (
546
- skipSafeTSUpdateCounter = metrics .TiKVSafeTSUpdateCounter .WithLabelValues ("skip" , "cluster" )
547
- successSafeTSUpdateCounter = metrics .TiKVSafeTSUpdateCounter .WithLabelValues ("success" , "cluster" )
548
- clusterMinSafeTSGap = metrics .TiKVMinSafeTSGapSeconds .WithLabelValues ("cluster" )
549
- )
550
-
551
545
func (s * KVStore ) updateSafeTS (ctx context.Context ) {
552
- stores := s .regionCache .GetStoresByType (tikvrpc .TiKV )
553
- // Try getting the cluster-level minimum resolved timestamp from PD first.
554
- clusterMinSafeTS , txnScopeMap , err := s .buildTxnScopeMap (ctx , stores )
555
- if clusterMinSafeTS != 0 && err == nil {
556
- preClusterMinSafeTS := s .GetMinSafeTS (oracle .GlobalTxnScope )
557
- if preClusterMinSafeTS > clusterMinSafeTS {
558
- skipSafeTSUpdateCounter .Inc ()
559
- preSafeTSTime := oracle .GetTimeFromTS (preClusterMinSafeTS )
560
- clusterMinSafeTSGap .Set (time .Since (preSafeTSTime ).Seconds ())
561
- return
562
- } else {
563
- s .minSafeTS .Store (oracle .GlobalTxnScope , clusterMinSafeTS )
564
- successSafeTSUpdateCounter .Inc ()
565
- safeTSTime := oracle .GetTimeFromTS (clusterMinSafeTS )
566
- clusterMinSafeTSGap .Set (time .Since (safeTSTime ).Seconds ())
567
- }
568
-
546
+ // Try to get the cluster-level minimum resolved timestamp from PD first.
547
+ if s .setClusterMinSafeTSByPD (ctx ) {
569
548
return
570
549
}
571
550
551
+ stores := s .regionCache .GetStoresByType (tikvrpc .TiKV )
572
552
tikvClient := s .GetTiKVClient ()
573
553
wg := & sync.WaitGroup {}
574
554
wg .Add (len (stores ))
@@ -580,7 +560,7 @@ func (s *KVStore) updateSafeTS(ctx context.Context) {
580
560
go func (ctx context.Context , wg * sync.WaitGroup , storeID uint64 , storeAddr string ) {
581
561
defer wg .Done ()
582
562
583
- resp , e := tikvClient .SendRequest (
563
+ resp , err := tikvClient .SendRequest (
584
564
ctx , storeAddr , tikvrpc .NewRequest (
585
565
tikvrpc .CmdStoreSafeTS , & kvrpcpb.StoreSafeTSRequest {
586
566
KeyRange : & kvrpcpb.KeyRange {
@@ -593,7 +573,7 @@ func (s *KVStore) updateSafeTS(ctx context.Context) {
593
573
), client .ReadTimeoutShort ,
594
574
)
595
575
storeIDStr := strconv .Itoa (int (storeID ))
596
- if e != nil {
576
+ if err != nil {
597
577
metrics .TiKVSafeTSUpdateCounter .WithLabelValues ("fail" , storeIDStr ).Inc ()
598
578
logutil .BgLogger ().Debug ("update safeTS failed" , zap .Error (err ), zap .Uint64 ("store-id" , storeID ))
599
579
return
@@ -614,36 +594,52 @@ func (s *KVStore) updateSafeTS(ctx context.Context) {
614
594
}(ctx , wg , storeID , storeAddr )
615
595
}
616
596
617
- for txnScope , storeIDs := range txnScopeMap {
618
- s .updateMinSafeTS (txnScope , storeIDs )
619
- }
620
- wg .Wait ()
621
- }
622
-
623
- // build txnScopeMap and judge whether it is needed to get safeTS from PD.
624
- // - if stores label are global, return get cluster min resolved ts from pd.
625
- // - if contains dc label store, return try to get it from TiKV.
626
- func (s * KVStore ) buildTxnScopeMap (ctx context.Context , stores []* Store ) (safeTS uint64 , txnScopeMap map [string ][]uint64 , err error ) {
627
- isGlobal := true
628
- txnScopeMap = make (map [string ][]uint64 )
597
+ txnScopeMap := make (map [string ][]uint64 )
629
598
for _ , store := range stores {
630
599
txnScopeMap [oracle .GlobalTxnScope ] = append (txnScopeMap [oracle .GlobalTxnScope ], store .StoreID ())
631
600
632
601
if label , ok := store .GetLabelValue (DCLabelKey ); ok {
633
602
txnScopeMap [label ] = append (txnScopeMap [label ], store .StoreID ())
634
- isGlobal = false
635
603
}
636
604
}
605
+ for txnScope , storeIDs := range txnScopeMap {
606
+ s .updateMinSafeTS (txnScope , storeIDs )
607
+ }
608
+ wg .Wait ()
609
+ }
610
+
611
+ var (
612
+ skipClusterSafeTSUpdateCounter = metrics .TiKVSafeTSUpdateCounter .WithLabelValues ("skip" , "cluster" )
613
+ successClusterSafeTSUpdateCounter = metrics .TiKVSafeTSUpdateCounter .WithLabelValues ("success" , "cluster" )
614
+ clusterMinSafeTSGap = metrics .TiKVMinSafeTSGapSeconds .WithLabelValues ("cluster" )
615
+ )
637
616
617
+ // setClusterMinSafeTSByPD try to get cluster-level's min resolved timestamp from PD when @@txn_scope is `global`.
618
+ func (s * KVStore ) setClusterMinSafeTSByPD (ctx context.Context ) bool {
619
+ isGlobal := config .GetTxnScopeFromConfig () == oracle .GlobalTxnScope
638
620
// Try to get the minimum resolved timestamp of the cluster from PD.
639
621
if s .pdHttpClient != nil && isGlobal {
640
- safeTS , err = s .pdHttpClient .GetClusterMinResolvedTS (ctx )
622
+ clusterMinSafeTS , err : = s .pdHttpClient .GetClusterMinResolvedTS (ctx )
641
623
if err != nil {
642
- logutil .BgLogger ().Debug ("get resolved TS from PD failed" , zap .Error (err ))
624
+ logutil .BgLogger ().Debug ("get cluster-level min resolved timestamp from PD failed" , zap .Error (err ))
625
+ } else if clusterMinSafeTS != 0 {
626
+ // Update metrics.
627
+ preClusterMinSafeTS := s .GetMinSafeTS (oracle .GlobalTxnScope )
628
+ if preClusterMinSafeTS > clusterMinSafeTS {
629
+ skipClusterSafeTSUpdateCounter .Inc ()
630
+ preSafeTSTime := oracle .GetTimeFromTS (preClusterMinSafeTS )
631
+ clusterMinSafeTSGap .Set (time .Since (preSafeTSTime ).Seconds ())
632
+ } else {
633
+ s .minSafeTS .Store (oracle .GlobalTxnScope , clusterMinSafeTS )
634
+ successClusterSafeTSUpdateCounter .Inc ()
635
+ safeTSTime := oracle .GetTimeFromTS (clusterMinSafeTS )
636
+ clusterMinSafeTSGap .Set (time .Since (safeTSTime ).Seconds ())
637
+ }
638
+ return true
643
639
}
644
640
}
645
641
646
- return safeTS , txnScopeMap , err
642
+ return false
647
643
}
648
644
649
645
// Variables defines the variables used by TiKV storage.
0 commit comments