Skip to content

Commit

Permalink
ttl: fix a wrong ttl's job schedule for TTL table upgraded from 6.5 (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Oct 15, 2024
1 parent 5901eea commit 5db81ef
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,7 @@ func constructResultOfShowCreateTable(ctx sessionctx.Context, dbName *model.CISt
restoreCtx.WriteKeyWord("TTL_JOB_INTERVAL")
restoreCtx.WritePlain("=")
if len(tableInfo.TTLInfo.JobInterval) == 0 {
restoreCtx.WriteString(model.DefaultJobInterval.String())
restoreCtx.WriteString(model.DefaultJobIntervalStr)
} else {
restoreCtx.WriteString(tableInfo.TTLInfo.JobInterval)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/parser/model/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ go_test(
],
embed = [":model"],
flaky = True,
shard_count = 22,
shard_count = 23,
deps = [
"//pkg/parser/charset",
"//pkg/parser/duration",
"//pkg/parser/mysql",
"//pkg/parser/terror",
"//pkg/parser/types",
Expand Down
3 changes: 3 additions & 0 deletions pkg/parser/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -1845,6 +1845,9 @@ func (p *PolicyInfo) Clone() *PolicyInfo {
// 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 CIStr `json:"column"`
Expand Down
8 changes: 8 additions & 0 deletions pkg/parser/model/model_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/mysql"
"github.com/pingcap/tidb/pkg/parser/types"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -831,3 +832,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 @@ -253,9 +253,11 @@ func (g *TTLTimersSyncer) shouldSyncTimer(timer *timerapi.TimerRecord, schema mo

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 model.CIStr, tblInfo *model.TableInfo, partition *model.PartitionDefinition, skipCache bool) (*timerapi.TimerRecord, error) {
Expand Down Expand Up @@ -306,12 +308,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 @@ -330,7 +333,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 @@ -396,3 +399,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/parser/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)
}

0 comments on commit 5db81ef

Please sign in to comment.