Skip to content
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/executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -1429,7 +1429,7 @@ func constructResultOfShowCreateTable(ctx sessionctx.Context, dbName *pmodel.CIS
restoreCtx.WriteKeyWord("TTL_JOB_INTERVAL")
restoreCtx.WritePlain("=")
if len(tableInfo.TTLInfo.JobInterval) == 0 {
restoreCtx.WriteString(model.DefaultJobInterval.String())
restoreCtx.WriteString(model.DefaultJobIntervalStr)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not use model.DefaultJobInterval.String() (return 1h0m0s) as the string interval value because the unit s is not supported in duration.PartionDuration @YangKeao

} else {
restoreCtx.WriteString(tableInfo.TTLInfo.JobInterval)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/meta/model/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ go_test(
deps = [
"//pkg/parser/ast",
"//pkg/parser/charset",
"//pkg/parser/duration",
"//pkg/parser/model",
"//pkg/parser/mysql",
"//pkg/parser/terror",
Expand Down
3 changes: 3 additions & 0 deletions pkg/meta/model/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,9 @@ func (s WindowRepeatType) String() string {
// DefaultJobInterval sets the default interval between TTL jobs
const DefaultJobInterval = time.Hour

// DefaultJobIntervalStr is the string representation of DefaultJobInterval
const DefaultJobIntervalStr = "1h"

// TTLInfo records the TTL config
type TTLInfo struct {
ColumnName model.CIStr `json:"column"`
Expand Down
8 changes: 8 additions & 0 deletions pkg/meta/model/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"time"

"github.com/pingcap/tidb/pkg/parser/charset"
"github.com/pingcap/tidb/pkg/parser/duration"
"github.com/pingcap/tidb/pkg/parser/model"
"github.com/pingcap/tidb/pkg/parser/mysql"
"github.com/pingcap/tidb/pkg/parser/types"
Expand Down Expand Up @@ -235,3 +236,10 @@ func TestClearReorgIntermediateInfo(t *testing.T) {
require.Equal(t, true, ptInfo.DDLColumns == nil)
require.Equal(t, int64(0), ptInfo.NewTableID)
}

func TestTTLDefaultJobInterval(t *testing.T) {
// test const `DefaultJobIntervalStr` and `DefaultJobInterval` are consistent.
d, err := duration.ParseDuration(DefaultJobIntervalStr)
require.NoError(t, err)
require.Equal(t, DefaultJobInterval, d)
}
21 changes: 17 additions & 4 deletions pkg/ttl/ttlworker/timer_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,11 @@ func (g *TTLTimersSyncer) shouldSyncTimer(timer *timerapi.TimerRecord, schema pm

tags := getTimerTags(schema, tblInfo, partition)
ttlInfo := tblInfo.TTLInfo
policyType, policyExpr := getTTLSchedulePolicy(ttlInfo)
return !slices.Equal(timer.Tags, tags) ||
timer.Enable != ttlInfo.Enable ||
timer.SchedPolicyExpr != ttlInfo.JobInterval
timer.SchedPolicyType != policyType ||
timer.SchedPolicyExpr != policyExpr
}

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

policyType, policyExpr := getTTLSchedulePolicy(ttlInfo)
timer, err = g.cli.CreateTimer(ctx, timerapi.TimerSpec{
Key: key,
Tags: tags,
Data: data,
SchedPolicyType: timerapi.SchedEventInterval,
SchedPolicyExpr: ttlInfo.JobInterval,
SchedPolicyType: policyType,
SchedPolicyExpr: policyExpr,
HookClass: timerHookClass,
Watermark: watermark,
Enable: ttlInfo.Enable,
Expand All @@ -329,7 +332,7 @@ func (g *TTLTimersSyncer) syncOneTimer(ctx context.Context, se session.Session,

err = g.cli.UpdateTimer(ctx, timer.ID,
timerapi.WithSetTags(tags),
timerapi.WithSetSchedExpr(timerapi.SchedEventInterval, tblInfo.TTLInfo.JobInterval),
timerapi.WithSetSchedExpr(getTTLSchedulePolicy(tblInfo.TTLInfo)),
timerapi.WithSetEnable(tblInfo.TTLInfo.Enable),
)

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

return cache.RowToTableStatus(se, rows[0])
}

// getTTLSchedulePolicy returns the timer's schedule policy and expression for a TTL job
func getTTLSchedulePolicy(info *model.TTLInfo) (timerapi.SchedPolicyType, string) {
interval := info.JobInterval
if interval == "" {
// This only happens when the table is created from 6.5 in which the `tidb_job_interval` is not introduced yet.
interval = model.DefaultJobIntervalStr
}
return timerapi.SchedEventInterval, interval
}
21 changes: 21 additions & 0 deletions pkg/ttl/ttlworker/timer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/google/uuid"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/pkg/meta/model"
"github.com/pingcap/tidb/pkg/sessionctx/variable"
timerapi "github.com/pingcap/tidb/pkg/timer/api"
"github.com/pingcap/tidb/pkg/util/logutil"
Expand Down Expand Up @@ -567,3 +568,23 @@ func TestTTLTimerRuntime(t *testing.T) {
r.Pause()
require.Nil(t, r.rt)
}

func TestGetTTLSchedulePolicy(t *testing.T) {
// normal case
tp, expr := getTTLSchedulePolicy(&model.TTLInfo{
JobInterval: "12h",
})
require.Equal(t, timerapi.SchedEventInterval, tp)
require.Equal(t, "12h", expr)
_, err := timerapi.CreateSchedEventPolicy(tp, expr)
require.NoError(t, err)

// empty job interval
tp, expr = getTTLSchedulePolicy(&model.TTLInfo{
JobInterval: "",
})
require.Equal(t, timerapi.SchedEventInterval, tp)
require.Equal(t, model.DefaultJobIntervalStr, expr)
_, err = timerapi.CreateSchedEventPolicy(tp, expr)
require.NoError(t, err)
}