Skip to content

Commit e9c8612

Browse files
committed
ttl: fix a wrong ttl's job schedule for TTL table upgraded from 6.5
1 parent 1e24d39 commit e9c8612

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed

pkg/executor/show.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,7 @@ func constructResultOfShowCreateTable(ctx sessionctx.Context, dbName *pmodel.CIS
14291429
restoreCtx.WriteKeyWord("TTL_JOB_INTERVAL")
14301430
restoreCtx.WritePlain("=")
14311431
if len(tableInfo.TTLInfo.JobInterval) == 0 {
1432-
restoreCtx.WriteString(model.DefaultJobInterval.String())
1432+
restoreCtx.WriteString(model.DefaultJobIntervalStr)
14331433
} else {
14341434
restoreCtx.WriteString(tableInfo.TTLInfo.JobInterval)
14351435
}

pkg/meta/model/table.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,9 @@ func (s WindowRepeatType) String() string {
12451245
// DefaultJobInterval sets the default interval between TTL jobs
12461246
const DefaultJobInterval = time.Hour
12471247

1248+
// DefaultJobIntervalStr is the string representation of DefaultJobInterval
1249+
const DefaultJobIntervalStr = "1h"
1250+
12481251
// TTLInfo records the TTL config
12491252
type TTLInfo struct {
12501253
ColumnName model.CIStr `json:"column"`

pkg/meta/model/table_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/model"
2425
"github.com/pingcap/tidb/pkg/parser/mysql"
2526
"github.com/pingcap/tidb/pkg/parser/types"
@@ -235,3 +236,10 @@ func TestClearReorgIntermediateInfo(t *testing.T) {
235236
require.Equal(t, true, ptInfo.DDLColumns == nil)
236237
require.Equal(t, int64(0), ptInfo.NewTableID)
237238
}
239+
240+
func TestTTLDefaultJobInterval(t *testing.T) {
241+
// test const `DefaultJobIntervalStr` and `DefaultJobInterval` are consistent.
242+
d, err := duration.ParseDuration(DefaultJobIntervalStr)
243+
require.NoError(t, err)
244+
require.Equal(t, DefaultJobInterval, d)
245+
}

pkg/ttl/ttlworker/timer_sync.go

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

253253
tags := getTimerTags(schema, tblInfo, partition)
254254
ttlInfo := tblInfo.TTLInfo
255+
policyType, policyExpr := getTTLSchedulePolicy(ttlInfo)
255256
return !slices.Equal(timer.Tags, tags) ||
256257
timer.Enable != ttlInfo.Enable ||
257-
timer.SchedPolicyExpr != ttlInfo.JobInterval
258+
timer.SchedPolicyType != policyType ||
259+
timer.SchedPolicyExpr != policyExpr
258260
}
259261

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

310+
policyType, policyExpr := getTTLSchedulePolicy(ttlInfo)
308311
timer, err = g.cli.CreateTimer(ctx, timerapi.TimerSpec{
309312
Key: key,
310313
Tags: tags,
311314
Data: data,
312-
SchedPolicyType: timerapi.SchedEventInterval,
313-
SchedPolicyExpr: ttlInfo.JobInterval,
315+
SchedPolicyType: policyType,
316+
SchedPolicyExpr: policyExpr,
314317
HookClass: timerHookClass,
315318
Watermark: watermark,
316319
Enable: ttlInfo.Enable,
@@ -329,7 +332,7 @@ func (g *TTLTimersSyncer) syncOneTimer(ctx context.Context, se session.Session,
329332

330333
err = g.cli.UpdateTimer(ctx, timer.ID,
331334
timerapi.WithSetTags(tags),
332-
timerapi.WithSetSchedExpr(timerapi.SchedEventInterval, tblInfo.TTLInfo.JobInterval),
335+
timerapi.WithSetSchedExpr(getTTLSchedulePolicy(tblInfo.TTLInfo)),
333336
timerapi.WithSetEnable(tblInfo.TTLInfo.Enable),
334337
)
335338

@@ -395,3 +398,13 @@ func getTTLTableStatus(ctx context.Context, se session.Session, tblInfo *model.T
395398

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

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/meta/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"
@@ -567,3 +568,23 @@ func TestTTLTimerRuntime(t *testing.T) {
567568
r.Pause()
568569
require.Nil(t, r.rt)
569570
}
571+
572+
func TestGetTTLSchedulePolicy(t *testing.T) {
573+
// normal case
574+
tp, expr := getTTLSchedulePolicy(&model.TTLInfo{
575+
JobInterval: "12h",
576+
})
577+
require.Equal(t, timerapi.SchedEventInterval, tp)
578+
require.Equal(t, "12h", expr)
579+
_, err := timerapi.CreateSchedEventPolicy(tp, expr)
580+
require.NoError(t, err)
581+
582+
// empty job interval
583+
tp, expr = getTTLSchedulePolicy(&model.TTLInfo{
584+
JobInterval: "",
585+
})
586+
require.Equal(t, timerapi.SchedEventInterval, tp)
587+
require.Equal(t, model.DefaultJobIntervalStr, expr)
588+
_, err = timerapi.CreateSchedEventPolicy(tp, expr)
589+
require.NoError(t, err)
590+
}

0 commit comments

Comments
 (0)