Skip to content

Commit 2512cb8

Browse files
authored
ttl: fix a wrong ttl's job schedule for TTL table upgraded from 6.5 (#56540) (#56593)
close #56539
1 parent 15a52d8 commit 2512cb8

File tree

7 files changed

+53
-7
lines changed

7 files changed

+53
-7
lines changed

pkg/executor/show.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1364,7 +1364,7 @@ func constructResultOfShowCreateTable(ctx sessionctx.Context, dbName *model.CISt
13641364
restoreCtx.WriteKeyWord("TTL_JOB_INTERVAL")
13651365
restoreCtx.WritePlain("=")
13661366
if len(tableInfo.TTLInfo.JobInterval) == 0 {
1367-
restoreCtx.WriteString(model.DefaultJobInterval.String())
1367+
restoreCtx.WriteString(model.DefaultJobIntervalStr)
13681368
} else {
13691369
restoreCtx.WriteString(tableInfo.TTLInfo.JobInterval)
13701370
}

pkg/parser/model/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ go_test(
3030
],
3131
embed = [":model"],
3232
flaky = True,
33-
shard_count = 21,
33+
shard_count = 22,
3434
deps = [
3535
"//pkg/parser/charset",
36+
"//pkg/parser/duration",
3637
"//pkg/parser/mysql",
3738
"//pkg/parser/terror",
3839
"//pkg/parser/types",

pkg/parser/model/model.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,6 +1828,9 @@ func (p *PolicyInfo) Clone() *PolicyInfo {
18281828
// DefaultJobInterval sets the default interval between TTL jobs
18291829
const DefaultJobInterval = time.Hour
18301830

1831+
// DefaultJobIntervalStr is the string representation of DefaultJobInterval
1832+
const DefaultJobIntervalStr = "1h"
1833+
18311834
// TTLInfo records the TTL config
18321835
type TTLInfo struct {
18331836
ColumnName CIStr `json:"column"`

pkg/parser/model/model_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"time"
2121

2222
"github.com/pingcap/tidb/pkg/parser/charset"
23+
"github.com/pingcap/tidb/pkg/parser/duration"
2324
"github.com/pingcap/tidb/pkg/parser/mysql"
2425
"github.com/pingcap/tidb/pkg/parser/types"
2526
"github.com/stretchr/testify/require"
@@ -831,3 +832,10 @@ func TestClearReorgIntermediateInfo(t *testing.T) {
831832
require.Equal(t, true, ptInfo.DDLColumns == nil)
832833
require.Equal(t, int64(0), ptInfo.NewTableID)
833834
}
835+
836+
func TestTTLDefaultJobInterval(t *testing.T) {
837+
// test const `DefaultJobIntervalStr` and `DefaultJobInterval` are consistent.
838+
d, err := duration.ParseDuration(DefaultJobIntervalStr)
839+
require.NoError(t, err)
840+
require.Equal(t, DefaultJobInterval, d)
841+
}

pkg/ttl/ttlworker/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ go_test(
6969
embed = [":ttlworker"],
7070
flaky = True,
7171
race = "on",
72-
shard_count = 49,
72+
shard_count = 50,
7373
deps = [
7474
"//pkg/domain",
7575
"//pkg/infoschema",

pkg/ttl/ttlworker/timer_sync.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,11 @@ func (g *TTLTimersSyncer) shouldSyncTimer(timer *timerapi.TimerRecord, schema mo
253253

254254
tags := getTimerTags(schema, tblInfo, partition)
255255
ttlInfo := tblInfo.TTLInfo
256+
policyType, policyExpr := getTTLSchedulePolicy(ttlInfo)
256257
return !slices.Equal(timer.Tags, tags) ||
257258
timer.Enable != ttlInfo.Enable ||
258-
timer.SchedPolicyExpr != ttlInfo.JobInterval
259+
timer.SchedPolicyType != policyType ||
260+
timer.SchedPolicyExpr != policyExpr
259261
}
260262

261263
func (g *TTLTimersSyncer) syncOneTimer(ctx context.Context, se session.Session, schema model.CIStr, tblInfo *model.TableInfo, partition *model.PartitionDefinition, skipCache bool) (*timerapi.TimerRecord, error) {
@@ -306,12 +308,13 @@ func (g *TTLTimersSyncer) syncOneTimer(ctx context.Context, se session.Session,
306308
return nil, err
307309
}
308310

311+
policyType, policyExpr := getTTLSchedulePolicy(ttlInfo)
309312
timer, err = g.cli.CreateTimer(ctx, timerapi.TimerSpec{
310313
Key: key,
311314
Tags: tags,
312315
Data: data,
313-
SchedPolicyType: timerapi.SchedEventInterval,
314-
SchedPolicyExpr: ttlInfo.JobInterval,
316+
SchedPolicyType: policyType,
317+
SchedPolicyExpr: policyExpr,
315318
HookClass: timerHookClass,
316319
Watermark: watermark,
317320
Enable: ttlInfo.Enable,
@@ -330,7 +333,7 @@ func (g *TTLTimersSyncer) syncOneTimer(ctx context.Context, se session.Session,
330333

331334
err = g.cli.UpdateTimer(ctx, timer.ID,
332335
timerapi.WithSetTags(tags),
333-
timerapi.WithSetSchedExpr(timerapi.SchedEventInterval, tblInfo.TTLInfo.JobInterval),
336+
timerapi.WithSetSchedExpr(getTTLSchedulePolicy(tblInfo.TTLInfo)),
334337
timerapi.WithSetEnable(tblInfo.TTLInfo.Enable),
335338
)
336339

@@ -396,3 +399,13 @@ func getTTLTableStatus(ctx context.Context, se session.Session, tblInfo *model.T
396399

397400
return cache.RowToTableStatus(se, rows[0])
398401
}
402+
403+
// getTTLSchedulePolicy returns the timer's schedule policy and expression for a TTL job
404+
func getTTLSchedulePolicy(info *model.TTLInfo) (timerapi.SchedPolicyType, string) {
405+
interval := info.JobInterval
406+
if interval == "" {
407+
// This only happens when the table is created from 6.5 in which the `tidb_job_interval` is not introduced yet.
408+
interval = model.DefaultJobIntervalStr
409+
}
410+
return timerapi.SchedEventInterval, interval
411+
}

pkg/ttl/ttlworker/timer_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/google/uuid"
2525
"github.com/pingcap/errors"
26+
"github.com/pingcap/tidb/pkg/parser/model"
2627
"github.com/pingcap/tidb/pkg/sessionctx/variable"
2728
timerapi "github.com/pingcap/tidb/pkg/timer/api"
2829
"github.com/pingcap/tidb/pkg/util/logutil"
@@ -555,3 +556,23 @@ func TestTTLTimerRuntime(t *testing.T) {
555556
r.Pause()
556557
require.Nil(t, r.rt)
557558
}
559+
560+
func TestGetTTLSchedulePolicy(t *testing.T) {
561+
// normal case
562+
tp, expr := getTTLSchedulePolicy(&model.TTLInfo{
563+
JobInterval: "12h",
564+
})
565+
require.Equal(t, timerapi.SchedEventInterval, tp)
566+
require.Equal(t, "12h", expr)
567+
_, err := timerapi.CreateSchedEventPolicy(tp, expr)
568+
require.NoError(t, err)
569+
570+
// empty job interval
571+
tp, expr = getTTLSchedulePolicy(&model.TTLInfo{
572+
JobInterval: "",
573+
})
574+
require.Equal(t, timerapi.SchedEventInterval, tp)
575+
require.Equal(t, model.DefaultJobIntervalStr, expr)
576+
_, err = timerapi.CreateSchedEventPolicy(tp, expr)
577+
require.NoError(t, err)
578+
}

0 commit comments

Comments
 (0)