Skip to content

Commit 924784a

Browse files
authored
ttl: make the default value of ttl_job_interval to 24h (#57758)
close #57757
1 parent 2b03447 commit 924784a

File tree

10 files changed

+45
-35
lines changed

10 files changed

+45
-35
lines changed

pkg/ddl/tests/serial/serial_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -500,29 +500,29 @@ func TestRecoverTableWithTTL(t *testing.T) {
500500
tk.MustExec("create table t_recover1 (t timestamp) TTL=`t`+INTERVAL 1 DAY")
501501
tk.MustExec("drop table t_recover1")
502502
tk.MustExec("recover table t_recover1")
503-
tk.MustQuery("show create table t_recover1").Check(testkit.Rows("t_recover1 CREATE TABLE `t_recover1` (\n `t` timestamp NULL DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`t` + INTERVAL 1 DAY */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='1h' */"))
503+
tk.MustQuery("show create table t_recover1").Check(testkit.Rows("t_recover1 CREATE TABLE `t_recover1` (\n `t` timestamp NULL DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`t` + INTERVAL 1 DAY */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='24h' */"))
504504

505505
// recover table with job id
506506
tk.MustExec("create table t_recover2 (t timestamp) TTL=`t`+INTERVAL 1 DAY")
507507
tk.MustExec("drop table t_recover2")
508508
jobID := getDDLJobID("t_recover2", "drop table")
509509
tk.MustExec(fmt.Sprintf("recover table BY JOB %d", jobID))
510-
tk.MustQuery("show create table t_recover2").Check(testkit.Rows("t_recover2 CREATE TABLE `t_recover2` (\n `t` timestamp NULL DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`t` + INTERVAL 1 DAY */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='1h' */"))
510+
tk.MustQuery("show create table t_recover2").Check(testkit.Rows("t_recover2 CREATE TABLE `t_recover2` (\n `t` timestamp NULL DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`t` + INTERVAL 1 DAY */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='24h' */"))
511511

512512
// flashback table
513513
tk.MustExec("create table t_recover3 (t timestamp) TTL=`t`+INTERVAL 1 DAY")
514514
tk.MustExec("drop table t_recover3")
515515
tk.MustExec("flashback table t_recover3")
516-
tk.MustQuery("show create table t_recover3").Check(testkit.Rows("t_recover3 CREATE TABLE `t_recover3` (\n `t` timestamp NULL DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`t` + INTERVAL 1 DAY */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='1h' */"))
516+
tk.MustQuery("show create table t_recover3").Check(testkit.Rows("t_recover3 CREATE TABLE `t_recover3` (\n `t` timestamp NULL DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`t` + INTERVAL 1 DAY */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='24h' */"))
517517

518518
// flashback database
519519
tk.MustExec("create database if not exists test_recover2")
520520
tk.MustExec("create table test_recover2.t1 (t timestamp) TTL=`t`+INTERVAL 1 DAY")
521521
tk.MustExec("create table test_recover2.t2 (t timestamp) TTL=`t`+INTERVAL 1 DAY")
522522
tk.MustExec("drop database test_recover2")
523523
tk.MustExec("flashback database test_recover2")
524-
tk.MustQuery("show create table test_recover2.t1").Check(testkit.Rows("t1 CREATE TABLE `t1` (\n `t` timestamp NULL DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`t` + INTERVAL 1 DAY */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='1h' */"))
525-
tk.MustQuery("show create table test_recover2.t2").Check(testkit.Rows("t2 CREATE TABLE `t2` (\n `t` timestamp NULL DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`t` + INTERVAL 1 DAY */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='1h' */"))
524+
tk.MustQuery("show create table test_recover2.t1").Check(testkit.Rows("t1 CREATE TABLE `t1` (\n `t` timestamp NULL DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`t` + INTERVAL 1 DAY */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='24h' */"))
525+
tk.MustQuery("show create table test_recover2.t2").Check(testkit.Rows("t2 CREATE TABLE `t2` (\n `t` timestamp NULL DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`t` + INTERVAL 1 DAY */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='24h' */"))
526526
}
527527

528528
func TestRecoverTableByJobID(t *testing.T) {

pkg/ddl/ttl.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ import (
3030
"github.com/pingcap/tidb/pkg/util/dbterror"
3131
)
3232

33-
// DefaultTTLJobInterval is the default value for ttl job interval.
34-
const DefaultTTLJobInterval = "1h"
35-
3633
func onTTLInfoRemove(jobCtx *jobContext, job *model.Job) (ver int64, err error) {
3734
tblInfo, err := GetTableInfoAndCancelFaultJob(jobCtx.metaMut, job, job.SchemaID)
3835
if err != nil {
@@ -195,7 +192,7 @@ func getTTLInfoInOptions(options []*ast.TableOption) (ttlInfo *model.TTLInfo, tt
195192
IntervalExprStr: intervalExpr,
196193
IntervalTimeUnit: int(op.TimeUnitValue.Unit),
197194
Enable: true,
198-
JobInterval: DefaultTTLJobInterval,
195+
JobInterval: model.DefaultTTLJobInterval,
199196
}
200197
case ast.TableOptionTTLEnable:
201198
ttlEnable = &op.BoolValue

pkg/ddl/ttl_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
func Test_getTTLInfoInOptions(t *testing.T) {
2727
falseValue := false
2828
trueValue := true
29-
twentyFourHours := "24h"
29+
twentyFiveHours := "25h"
3030

3131
cases := []struct {
3232
options []*ast.TableOption
@@ -56,7 +56,7 @@ func Test_getTTLInfoInOptions(t *testing.T) {
5656
IntervalExprStr: "5",
5757
IntervalTimeUnit: int(ast.TimeUnitYear),
5858
Enable: true,
59-
JobInterval: DefaultTTLJobInterval,
59+
JobInterval: model.DefaultTTLJobInterval,
6060
},
6161
nil,
6262
nil,
@@ -80,7 +80,7 @@ func Test_getTTLInfoInOptions(t *testing.T) {
8080
IntervalExprStr: "5",
8181
IntervalTimeUnit: int(ast.TimeUnitYear),
8282
Enable: false,
83-
JobInterval: DefaultTTLJobInterval,
83+
JobInterval: model.DefaultTTLJobInterval,
8484
},
8585
&falseValue,
8686
nil,
@@ -108,7 +108,7 @@ func Test_getTTLInfoInOptions(t *testing.T) {
108108
IntervalExprStr: "5",
109109
IntervalTimeUnit: int(ast.TimeUnitYear),
110110
Enable: true,
111-
JobInterval: DefaultTTLJobInterval,
111+
JobInterval: model.DefaultTTLJobInterval,
112112
},
113113
&trueValue,
114114
nil,
@@ -124,18 +124,18 @@ func Test_getTTLInfoInOptions(t *testing.T) {
124124
},
125125
{
126126
Tp: ast.TableOptionTTLJobInterval,
127-
StrValue: "24h",
127+
StrValue: "25h",
128128
},
129129
},
130130
&model.TTLInfo{
131131
ColumnName: pmodel.NewCIStr("test_column"),
132132
IntervalExprStr: "5",
133133
IntervalTimeUnit: int(ast.TimeUnitYear),
134134
Enable: true,
135-
JobInterval: "24h",
135+
JobInterval: "25h",
136136
},
137137
nil,
138-
&twentyFourHours,
138+
&twentyFiveHours,
139139
nil,
140140
},
141141
}

pkg/executor/show.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,10 @@ 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.DefaultJobIntervalStr)
1432+
// This only happens when the table is created from 6.5 in which the `tidb_job_interval` is not introduced yet.
1433+
// We use `OldDefaultTTLJobInterval` as the return value to ensure a consistent behavior for the
1434+
// upgrades: v6.5 -> v8.5(or previous version) -> newer version than v8.5.
1435+
restoreCtx.WriteString(model.OldDefaultTTLJobInterval)
14331436
} else {
14341437
restoreCtx.WriteString(tableInfo.TTLInfo.JobInterval)
14351438
}

pkg/meta/model/table.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,11 +1320,12 @@ func (s WindowRepeatType) String() string {
13201320
}
13211321
}
13221322

1323-
// DefaultJobInterval sets the default interval between TTL jobs
1324-
const DefaultJobInterval = time.Hour
1323+
// DefaultTTLJobInterval is the default interval of TTL jobs.
1324+
const DefaultTTLJobInterval = "24h"
13251325

1326-
// DefaultJobIntervalStr is the string representation of DefaultJobInterval
1327-
const DefaultJobIntervalStr = "1h"
1326+
// OldDefaultTTLJobInterval is the default interval of TTL jobs in v8.5 and the previous versions.
1327+
// It is used by some codes to keep compatible with the previous versions.
1328+
const OldDefaultTTLJobInterval = "1h"
13281329

13291330
// TTLInfo records the TTL config
13301331
type TTLInfo struct {
@@ -1351,7 +1352,10 @@ func (t *TTLInfo) Clone() *TTLInfo {
13511352
// and could avoid bugs blocking users from upgrading or bootstrapping the cluster.
13521353
func (t *TTLInfo) GetJobInterval() (time.Duration, error) {
13531354
if len(t.JobInterval) == 0 {
1354-
return DefaultJobInterval, nil
1355+
// This only happens when the table is created from 6.5 in which the `tidb_job_interval` is not introduced yet.
1356+
// We use `OldDefaultTTLJobInterval` as the return value to ensure a consistent behavior for the
1357+
// upgrades: v6.5 -> v8.5(or previous version) -> newer version than v8.5.
1358+
return duration.ParseDuration(OldDefaultTTLJobInterval)
13551359
}
13561360

13571361
return duration.ParseDuration(t.JobInterval)

pkg/meta/model/table_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,12 @@ func TestClearReorgIntermediateInfo(t *testing.T) {
238238
}
239239

240240
func TestTTLDefaultJobInterval(t *testing.T) {
241-
// test const `DefaultJobIntervalStr` and `DefaultJobInterval` are consistent.
242-
d, err := duration.ParseDuration(DefaultJobIntervalStr)
241+
// test default value of `DefaultTTLJobInterval` is valid.
242+
d, err := duration.ParseDuration(DefaultTTLJobInterval)
243243
require.NoError(t, err)
244-
require.Equal(t, DefaultJobInterval, d)
244+
require.Equal(t, 24*time.Hour, d)
245+
// test default value of `OldDefaultTTLJobInterval` is valid.
246+
d, err = duration.ParseDuration(OldDefaultTTLJobInterval)
247+
require.NoError(t, err)
248+
require.Equal(t, time.Hour, d)
245249
}

pkg/ttl/ttlworker/timer_sync.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,9 @@ func getTTLSchedulePolicy(info *model.TTLInfo) (timerapi.SchedPolicyType, string
414414
interval := info.JobInterval
415415
if interval == "" {
416416
// This only happens when the table is created from 6.5 in which the `tidb_job_interval` is not introduced yet.
417-
interval = model.DefaultJobIntervalStr
417+
// We use `OldDefaultTTLJobInterval` as the return value to ensure a consistent behavior for the
418+
// upgrades: v6.5 -> v8.5(or previous version) -> newer version than v8.5.
419+
interval = model.OldDefaultTTLJobInterval
418420
}
419421
return timerapi.SchedEventInterval, interval
420422
}

pkg/ttl/ttlworker/timer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ func TestGetTTLSchedulePolicy(t *testing.T) {
584584
JobInterval: "",
585585
})
586586
require.Equal(t, timerapi.SchedEventInterval, tp)
587-
require.Equal(t, model.DefaultJobIntervalStr, expr)
587+
require.Equal(t, model.OldDefaultTTLJobInterval, expr)
588588
_, err = timerapi.CreateSchedEventPolicy(tp, expr)
589589
require.NoError(t, err)
590590
}

tests/integrationtest/r/executor/ddl.result

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ SHOW CREATE TABLE t;
179179
Table Create Table
180180
t CREATE TABLE `t` (
181181
`created_at` datetime DEFAULT NULL
182-
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`created_at` + INTERVAL 5 DAY */ /*T![ttl] TTL_ENABLE='ON' */ /*T![ttl] TTL_JOB_INTERVAL='1h' */
182+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`created_at` + INTERVAL 5 DAY */ /*T![ttl] TTL_ENABLE='ON' */ /*T![ttl] TTL_JOB_INTERVAL='24h' */
183183
DROP TABLE t;
184184
CREATE TABLE t (id int) TTL = `id` + INTERVAL 5 DAY;
185185
Error 8148 (HY000): Field 'id' is of a not supported type for TTL config, expect DATETIME, DATE or TIMESTAMP
@@ -199,7 +199,7 @@ SHOW CREATE TABLE t;
199199
Table Create Table
200200
t CREATE TABLE `t` (
201201
`created_at` datetime DEFAULT NULL
202-
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`created_at` + INTERVAL 3 DAY */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='1h' */
202+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`created_at` + INTERVAL 3 DAY */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='24h' */
203203
DROP TABLE t;
204204
drop table if exists t;
205205
CREATE TABLE t (created_at datetime, updated_at datetime, wrong_type int) TTL = `created_at` + INTERVAL 5 DAY;
@@ -210,15 +210,15 @@ t CREATE TABLE `t` (
210210
`created_at` datetime DEFAULT NULL,
211211
`updated_at` datetime DEFAULT NULL,
212212
`wrong_type` int DEFAULT NULL
213-
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`updated_at` + INTERVAL 2 YEAR */ /*T![ttl] TTL_ENABLE='ON' */ /*T![ttl] TTL_JOB_INTERVAL='1h' */
213+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`updated_at` + INTERVAL 2 YEAR */ /*T![ttl] TTL_ENABLE='ON' */ /*T![ttl] TTL_JOB_INTERVAL='24h' */
214214
ALTER TABLE t TTL_ENABLE = 'OFF';
215215
SHOW CREATE TABLE t;
216216
Table Create Table
217217
t CREATE TABLE `t` (
218218
`created_at` datetime DEFAULT NULL,
219219
`updated_at` datetime DEFAULT NULL,
220220
`wrong_type` int DEFAULT NULL
221-
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`updated_at` + INTERVAL 2 YEAR */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='1h' */
221+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`updated_at` + INTERVAL 2 YEAR */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='24h' */
222222
ALTER TABLE t TTL_JOB_INTERVAL = '1d';
223223
SHOW CREATE TABLE t;
224224
Table Create Table

tests/integrationtest/r/executor/show.result

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,28 +116,28 @@ show create table t;
116116
Table Create Table
117117
t CREATE TABLE `t` (
118118
`created_at` datetime DEFAULT NULL
119-
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`created_at` + INTERVAL 100 YEAR */ /*T![ttl] TTL_ENABLE='ON' */ /*T![ttl] TTL_JOB_INTERVAL='1h' */
119+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`created_at` + INTERVAL 100 YEAR */ /*T![ttl] TTL_ENABLE='ON' */ /*T![ttl] TTL_JOB_INTERVAL='24h' */
120120
drop table if exists t;
121121
create table t(created_at datetime) ttl = `created_at` + INTERVAL 100 YEAR ttl_enable = 'OFF';
122122
show create table t;
123123
Table Create Table
124124
t CREATE TABLE `t` (
125125
`created_at` datetime DEFAULT NULL
126-
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`created_at` + INTERVAL 100 YEAR */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='1h' */
126+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`created_at` + INTERVAL 100 YEAR */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='24h' */
127127
drop table if exists t;
128128
create table t (created_at datetime) TTL = created_at + INTERVAL 3.14159 HOUR_MINUTE;
129129
show create table t;
130130
Table Create Table
131131
t CREATE TABLE `t` (
132132
`created_at` datetime DEFAULT NULL
133-
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`created_at` + INTERVAL 3.14159 HOUR_MINUTE */ /*T![ttl] TTL_ENABLE='ON' */ /*T![ttl] TTL_JOB_INTERVAL='1h' */
133+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`created_at` + INTERVAL 3.14159 HOUR_MINUTE */ /*T![ttl] TTL_ENABLE='ON' */ /*T![ttl] TTL_JOB_INTERVAL='24h' */
134134
drop table if exists t;
135135
create table t (created_at datetime) TTL = created_at + INTERVAL "15:20" HOUR_MINUTE;
136136
show create table t;
137137
Table Create Table
138138
t CREATE TABLE `t` (
139139
`created_at` datetime DEFAULT NULL
140-
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`created_at` + INTERVAL _utf8mb4'15:20' HOUR_MINUTE */ /*T![ttl] TTL_ENABLE='ON' */ /*T![ttl] TTL_JOB_INTERVAL='1h' */
140+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`created_at` + INTERVAL _utf8mb4'15:20' HOUR_MINUTE */ /*T![ttl] TTL_ENABLE='ON' */ /*T![ttl] TTL_JOB_INTERVAL='24h' */
141141
drop table if exists t;
142142
create table t (created_at datetime) TTL = created_at + INTERVAL 100 YEAR TTL_JOB_INTERVAL = '1d';
143143
show create table t;

0 commit comments

Comments
 (0)