Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ttl: only gc in leader to save performance (#59358) #59471

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/metrics/grafana/tidb.json
Original file line number Diff line number Diff line change
Expand Up @@ -20236,7 +20236,7 @@
"targets": [
{
"exemplar": true,
"expr": "avg(tidb_server_ttl_watermark_delay{k8s_cluster=\"$k8s_cluster\",tidb_cluster=\"$tidb_cluster\", type=\"schedule\"}) by (type, name)",
"expr": "max(tidb_server_ttl_watermark_delay{k8s_cluster=\"$k8s_cluster\",tidb_cluster=\"$tidb_cluster\", type=\"schedule\"}) by (type, name)",
"interval": "",
"legendFormat": "{{ name }}",
"queryType": "randomWalk",
Expand Down
5 changes: 5 additions & 0 deletions pkg/ttl/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,8 @@ func UpdateDelayMetrics(records map[int64]*DelayMetricsRecord) {
metrics.TTLWatermarkDelay.With(prometheus.Labels{metrics.LblType: "schedule", metrics.LblName: delay}).Set(v)
}
}

// ClearDelayMetrics clears the metrics of TTL delay
func ClearDelayMetrics() {
metrics.TTLWatermarkDelay.Reset()
}
1 change: 1 addition & 0 deletions pkg/ttl/ttlworker/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ go_test(
"@com_github_ngaut_pools//:pools",
"@com_github_pingcap_errors//:errors",
"@com_github_pingcap_failpoint//:failpoint",
"@com_github_prometheus_client_golang//prometheus",
"@com_github_prometheus_client_model//go",
"@com_github_stretchr_testify//assert",
"@com_github_stretchr_testify//mock",
Expand Down
13 changes: 13 additions & 0 deletions pkg/ttl/ttlworker/job_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,15 @@ func (m *JobManager) reportMetrics(se session.Session) {
metrics.RunningJobsCnt.Set(runningJobs)
metrics.CancellingJobsCnt.Set(cancellingJobs)

if !m.isLeader() {
// only the leader can do collect delay metrics to reduce the performance overhead
metrics.ClearDelayMetrics()
return
}

if time.Since(m.lastReportDelayMetricsTime) > 10*time.Minute {
m.lastReportDelayMetricsTime = time.Now()
logutil.Logger(m.ctx).Info("TTL leader to collect delay metrics")
records, err := GetDelayMetricRecords(m.ctx, se, time.Now())
if err != nil {
logutil.Logger(m.ctx).Info("failed to get TTL delay metrics", zap.Error(err))
Expand Down Expand Up @@ -998,6 +1005,12 @@ func summarizeTaskResult(tasks []*cache.TTLTask) (*TTLSummary, error) {

// DoGC deletes some old TTL job histories and redundant scan tasks
func (m *JobManager) DoGC(ctx context.Context, se session.Session) {
if !m.isLeader() {
// only the leader can do the GC to reduce the performance impact
return
}

logutil.Logger(m.ctx).Info("TTL leader to DoGC")
// Remove the table not exist in info schema cache.
// Delete the table status before deleting the tasks. Therefore the related tasks
if err := m.updateInfoSchemaCache(se); err == nil {
Expand Down
70 changes: 67 additions & 3 deletions pkg/ttl/ttlworker/job_manager_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/pkg/domain"
"github.com/pingcap/tidb/pkg/kv"
metrics2 "github.com/pingcap/tidb/pkg/metrics"
"github.com/pingcap/tidb/pkg/parser/ast"
"github.com/pingcap/tidb/pkg/parser/model"
dbsession "github.com/pingcap/tidb/pkg/session"
Expand All @@ -44,6 +45,7 @@ import (
"github.com/pingcap/tidb/pkg/ttl/session"
"github.com/pingcap/tidb/pkg/ttl/ttlworker"
"github.com/pingcap/tidb/pkg/util/logutil"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
Expand Down Expand Up @@ -807,10 +809,15 @@ func TestGCScanTasks(t *testing.T) {
addScanTaskRecord(3, 2, 1)
addScanTaskRecord(3, 2, 2)

isLeader := false
m := ttlworker.NewJobManager("manager-1", nil, store, nil, func() bool {
return true
return isLeader
})
se := session.NewSession(tk.Session(), tk.Session(), func(_ session.Session) {})
// only leader can do GC
m.DoGC(context.TODO(), se)
tk.MustQuery("select count(1) from mysql.tidb_ttl_task").Check(testkit.Rows("6"))
isLeader = true
m.DoGC(context.TODO(), se)
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"))
}
Expand All @@ -826,10 +833,15 @@ func TestGCTableStatus(t *testing.T) {
// insert table status without corresponding table
tk.MustExec("INSERT INTO mysql.tidb_ttl_table_status (table_id,parent_table_id) VALUES (?, ?)", 2024, 2024)

isLeader := false
m := ttlworker.NewJobManager("manager-1", nil, store, nil, func() bool {
return true
return isLeader
})
se := session.NewSession(tk.Session(), tk.Session(), func(_ session.Session) {})
// only leader can do GC
m.DoGC(context.TODO(), se)
tk.MustQuery("select count(1) from mysql.tidb_ttl_table_status").Check(testkit.Rows("1"))
isLeader = true
m.DoGC(context.TODO(), se)
tk.MustQuery("select * from mysql.tidb_ttl_table_status").Check(nil)

Expand Down Expand Up @@ -887,11 +899,16 @@ func TestGCTTLHistory(t *testing.T) {
addHistory(6, 91)
addHistory(7, 100)

isLeader := false
m := ttlworker.NewJobManager("manager-1", nil, store, nil, func() bool {
return true
return isLeader
})
se := session.NewSession(tk.Session(), tk.Session(), func(_ session.Session) {})
m.DoGC(context.TODO(), se)
// only leader can go GC
tk.MustQuery("select count(1) from mysql.tidb_ttl_job_history").Check(testkit.Rows("7"))
isLeader = true
m.DoGC(context.TODO(), se)
tk.MustQuery("select job_id from mysql.tidb_ttl_job_history order by job_id asc").Check(testkit.Rows("1", "2", "3", "4", "5"))
}

Expand Down Expand Up @@ -1057,6 +1074,53 @@ func TestDelayMetrics(t *testing.T) {
checkRecord(records, "t3", now.Add(-3*time.Hour))
checkRecord(records, "t4", now.Add(-3*time.Hour))
checkRecord(records, "t5", emptyTime)

metrics.ClearDelayMetrics()
getMetricCnt := func() int {
ch := make(chan prometheus.Metric)
go func() {
metrics2.TTLWatermarkDelay.Collect(ch)
close(ch)
}()

cnt := 0
for range ch {
cnt++
}
return cnt
}

isLeader := false
m := ttlworker.NewJobManager("test-ttl-job-manager", nil, store, nil, func() bool {
return isLeader
})
// If the manager is not leader, the metrics will be empty.
m.ReportMetrics(se)
require.Zero(t, getMetricCnt())
// leader will collect metrics
isLeader = true
m.SetLastReportDelayMetricsTime(time.Now().Add(-11 * time.Minute))
m.ReportMetrics(se)
require.Equal(t, len(metrics.WaterMarkScheduleDelayNames), getMetricCnt())
require.InDelta(t, time.Now().Unix(), m.GetLastReportDelayMetricsTime().Unix(), 5)
// will not collect metrics in 10 minutes
lastReportTime := time.Now().Add(-9 * time.Minute)
m.SetLastReportDelayMetricsTime(lastReportTime)
m.ReportMetrics(se)
require.Equal(t, len(metrics.WaterMarkScheduleDelayNames), getMetricCnt())
require.Equal(t, lastReportTime.Unix(), m.GetLastReportDelayMetricsTime().Unix(), 5)
// when back to non-leader, the metrics will be empty and last report time will not be updated.
isLeader = false
lastReportTime = time.Now().Add(-11 * time.Minute)
m.SetLastReportDelayMetricsTime(lastReportTime)
m.ReportMetrics(se)
require.Zero(t, getMetricCnt())
require.Equal(t, lastReportTime.Unix(), m.GetLastReportDelayMetricsTime().Unix())
// when back to leader again, the metrics will be collected.
isLeader = true
m.ReportMetrics(se)
require.Equal(t, len(metrics.WaterMarkScheduleDelayNames), getMetricCnt())
require.InDelta(t, time.Now().Unix(), m.GetLastReportDelayMetricsTime().Unix(), 5)
}

func TestManagerJobAdapterCanSubmitJob(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions pkg/ttl/ttlworker/job_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@ func (m *JobManager) UpdateHeartBeat(ctx context.Context, se session.Session, no
return m.updateHeartBeat(ctx, se, now)
}

// SetLastReportDelayMetricsTime sets the lastReportDelayMetricsTime for test
func (m *JobManager) SetLastReportDelayMetricsTime(t time.Time) {
m.lastReportDelayMetricsTime = t
}

// GetLastReportDelayMetricsTime returns the lastReportDelayMetricsTime for test
func (m *JobManager) GetLastReportDelayMetricsTime() time.Time {
return m.lastReportDelayMetricsTime
}

// ReportMetrics is an exported version of reportMetrics
func (m *JobManager) ReportMetrics(se session.Session) {
m.reportMetrics(se)
Expand Down