@@ -30,6 +30,11 @@ import (
30
30
"github.com/pingcap/failpoint"
31
31
"github.com/pingcap/tidb/pkg/domain"
32
32
"github.com/pingcap/tidb/pkg/kv"
33
+ << << << < HEAD
34
+ == == == =
35
+ "github.com/pingcap/tidb/pkg/meta/model"
36
+ metrics2 "github.com/pingcap/tidb/pkg/metrics"
37
+ >> >> >> > 75154399927 (ttl : only gc in leader to save performance (#59358 ))
33
38
"github.com/pingcap/tidb/pkg/parser/ast"
34
39
"github.com/pingcap/tidb/pkg/parser/model"
35
40
dbsession "github.com/pingcap/tidb/pkg/session"
@@ -44,6 +49,11 @@ import (
44
49
"github.com/pingcap/tidb/pkg/ttl/session"
45
50
"github.com/pingcap/tidb/pkg/ttl/ttlworker"
46
51
"github.com/pingcap/tidb/pkg/util/logutil"
52
+ << << << < HEAD
53
+ == == == =
54
+ "github.com/pingcap/tidb/pkg/util/skip"
55
+ "github.com/prometheus/client_golang/prometheus"
56
+ >> >> >> > 75154399927 (ttl : only gc in leader to save performance (#59358 ))
47
57
dto "github.com/prometheus/client_model/go"
48
58
"github.com/stretchr/testify/require"
49
59
"go.uber.org/atomic"
@@ -807,11 +817,20 @@ func TestGCScanTasks(t *testing.T) {
807
817
addScanTaskRecord (3 , 2 , 1 )
808
818
addScanTaskRecord (3 , 2 , 2 )
809
819
820
+ isLeader := false
810
821
m := ttlworker .NewJobManager ("manager-1" , nil , store , nil , func () bool {
811
- return true
822
+ return isLeader
812
823
})
813
824
se := session .NewSession (tk .Session (), tk .Session (), func (_ session.Session ) {})
825
+ << << << < HEAD
814
826
m .DoGC (context .TODO (), se )
827
+ == == == =
828
+ // only leader can do GC
829
+ m .DoGC (context .TODO (), se , se .Now ())
830
+ tk .MustQuery ("select count(1) from mysql.tidb_ttl_task" ).Check (testkit .Rows ("6" ))
831
+ isLeader = true
832
+ m .DoGC (context .TODO (), se , se .Now ())
833
+ >> >> >> > 75154399927 (ttl : only gc in leader to save performance (#59358 ))
815
834
tk .MustQuery ("select job_id, scan_id from mysql.tidb_ttl_task order by job_id, scan_id asc" ).Check (testkit .Rows ("1 1" , "1 2" ))
816
835
}
817
836
@@ -826,11 +845,20 @@ func TestGCTableStatus(t *testing.T) {
826
845
// insert table status without corresponding table
827
846
tk .MustExec ("INSERT INTO mysql.tidb_ttl_table_status (table_id,parent_table_id) VALUES (?, ?)" , 2024 , 2024 )
828
847
848
+ isLeader := false
829
849
m := ttlworker .NewJobManager ("manager-1" , nil , store , nil , func () bool {
830
- return true
850
+ return isLeader
831
851
})
832
852
se := session .NewSession (tk .Session (), tk .Session (), func (_ session.Session ) {})
853
+ << << << < HEAD
833
854
m .DoGC (context .TODO (), se )
855
+ == == == =
856
+ // only leader can do GC
857
+ m .DoGC (context .TODO (), se , se .Now ())
858
+ tk .MustQuery ("select count(1) from mysql.tidb_ttl_table_status" ).Check (testkit .Rows ("1" ))
859
+ isLeader = true
860
+ m .DoGC (context .TODO (), se , se .Now ())
861
+ >> >> >> > 75154399927 (ttl : only gc in leader to save performance (#59358 ))
834
862
tk .MustQuery ("select * from mysql.tidb_ttl_table_status" ).Check (nil )
835
863
836
864
// insert a running table status without corresponding table
@@ -887,11 +915,20 @@ func TestGCTTLHistory(t *testing.T) {
887
915
addHistory (6 , 91 )
888
916
addHistory (7 , 100 )
889
917
918
+ isLeader := false
890
919
m := ttlworker .NewJobManager ("manager-1" , nil , store , nil , func () bool {
891
- return true
920
+ return isLeader
892
921
})
893
922
se := session .NewSession (tk .Session (), tk .Session (), func (_ session.Session ) {})
923
+ << << << < HEAD
894
924
m .DoGC (context .TODO (), se )
925
+ == == == =
926
+ m .DoGC (context .TODO (), se , se .Now ())
927
+ // only leader can go GC
928
+ tk .MustQuery ("select count(1) from mysql.tidb_ttl_job_history" ).Check (testkit .Rows ("7" ))
929
+ isLeader = true
930
+ m .DoGC (context .TODO (), se , se .Now ())
931
+ >> >> >> > 75154399927 (ttl : only gc in leader to save performance (#59358 ))
895
932
tk .MustQuery ("select job_id from mysql.tidb_ttl_job_history order by job_id asc" ).Check (testkit .Rows ("1" , "2" , "3" , "4" , "5" ))
896
933
}
897
934
@@ -1057,6 +1094,53 @@ func TestDelayMetrics(t *testing.T) {
1057
1094
checkRecord (records , "t3" , now .Add (- 3 * time .Hour ))
1058
1095
checkRecord (records , "t4" , now .Add (- 3 * time .Hour ))
1059
1096
checkRecord (records , "t5" , emptyTime )
1097
+
1098
+ metrics .ClearDelayMetrics ()
1099
+ getMetricCnt := func () int {
1100
+ ch := make (chan prometheus.Metric )
1101
+ go func () {
1102
+ metrics2 .TTLWatermarkDelay .Collect (ch )
1103
+ close (ch )
1104
+ }()
1105
+
1106
+ cnt := 0
1107
+ for range ch {
1108
+ cnt ++
1109
+ }
1110
+ return cnt
1111
+ }
1112
+
1113
+ isLeader := false
1114
+ m := ttlworker .NewJobManager ("test-ttl-job-manager" , nil , store , nil , func () bool {
1115
+ return isLeader
1116
+ })
1117
+ // If the manager is not leader, the metrics will be empty.
1118
+ m .ReportMetrics (se )
1119
+ require .Zero (t , getMetricCnt ())
1120
+ // leader will collect metrics
1121
+ isLeader = true
1122
+ m .SetLastReportDelayMetricsTime (time .Now ().Add (- 11 * time .Minute ))
1123
+ m .ReportMetrics (se )
1124
+ require .Equal (t , len (metrics .WaterMarkScheduleDelayNames ), getMetricCnt ())
1125
+ require .InDelta (t , time .Now ().Unix (), m .GetLastReportDelayMetricsTime ().Unix (), 5 )
1126
+ // will not collect metrics in 10 minutes
1127
+ lastReportTime := time .Now ().Add (- 9 * time .Minute )
1128
+ m .SetLastReportDelayMetricsTime (lastReportTime )
1129
+ m .ReportMetrics (se )
1130
+ require .Equal (t , len (metrics .WaterMarkScheduleDelayNames ), getMetricCnt ())
1131
+ require .Equal (t , lastReportTime .Unix (), m .GetLastReportDelayMetricsTime ().Unix (), 5 )
1132
+ // when back to non-leader, the metrics will be empty and last report time will not be updated.
1133
+ isLeader = false
1134
+ lastReportTime = time .Now ().Add (- 11 * time .Minute )
1135
+ m .SetLastReportDelayMetricsTime (lastReportTime )
1136
+ m .ReportMetrics (se )
1137
+ require .Zero (t , getMetricCnt ())
1138
+ require .Equal (t , lastReportTime .Unix (), m .GetLastReportDelayMetricsTime ().Unix ())
1139
+ // when back to leader again, the metrics will be collected.
1140
+ isLeader = true
1141
+ m .ReportMetrics (se )
1142
+ require .Equal (t , len (metrics .WaterMarkScheduleDelayNames ), getMetricCnt ())
1143
+ require .InDelta (t , time .Now ().Unix (), m .GetLastReportDelayMetricsTime ().Unix (), 5 )
1060
1144
}
1061
1145
1062
1146
func TestManagerJobAdapterCanSubmitJob (t * testing.T ) {
@@ -1444,6 +1528,7 @@ func TestDisableTTLAfterLoseHeartbeat(t *testing.T) {
1444
1528
1445
1529
ctx := context .Background ()
1446
1530
m1 := ttlworker .NewJobManager ("test-ttl-job-manager-1" , nil , store , nil , nil )
1531
+ << << << < HEAD
1447
1532
require .NoError (t , m1 .InfoSchemaCache ().Update (se ))
1448
1533
require .NoError (t , m1 .TableStatusCache ().Update (ctx , se ))
1449
1534
@@ -1464,6 +1549,11 @@ func TestDisableTTLAfterLoseHeartbeat(t *testing.T) {
1464
1549
require .NoError (t , m2 .InfoSchemaCache ().Update (se ))
1465
1550
require .NoError (t , m2 .TableStatusCache ().Update (ctx , se ))
1466
1551
m2 .RescheduleJobs (se , now )
1552
+ == == == =
1553
+ m2 := ttlworker .NewJobManager ("test-ttl-job-manager-2" , nil , store , nil , func () bool {
1554
+ return true
1555
+ })
1556
+ >> >> >> > 75154399927 (ttl : only gc in leader to save performance (#59358 ))
1467
1557
1468
1558
// the job should have been cancelled
1469
1559
tk .MustQuery ("select current_job_status from mysql.tidb_ttl_table_status" ).Check (testkit .Rows ("<nil>" ))
0 commit comments