Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ttl, variables: add system variables for ttl job schedule (#39727) #39869

Merged
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
4 changes: 0 additions & 4 deletions domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2385,10 +2385,6 @@ func (do *Domain) runTTLJobManager(ctx context.Context) {
ttlJobManager.Start()
do.ttlJobManager = ttlJobManager

// TODO: read the worker count from `do.sysVarCache` and resize the workers
ttlworker.ScanWorkersCount.Store(4)
ttlworker.DeleteWorkerCount.Store(4)

<-do.exit

ttlJobManager.Stop()
Expand Down
9 changes: 0 additions & 9 deletions executor/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta"
"github.com/pingcap/tidb/meta/autoid"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/parser/terror"
Expand Down Expand Up @@ -1525,8 +1524,6 @@ func TestRenameMultiTables(t *testing.T) {
}

func TestCreateTableWithTTL(t *testing.T) {
parser.TTLFeatureGate = true

store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
Expand All @@ -1546,8 +1543,6 @@ func TestCreateTableWithTTL(t *testing.T) {
}

func TestAlterTTLInfo(t *testing.T) {
parser.TTLFeatureGate = true

store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
Expand Down Expand Up @@ -1584,8 +1579,6 @@ func TestAlterTTLInfo(t *testing.T) {
}

func TestDisableTTLForTempTable(t *testing.T) {
parser.TTLFeatureGate = true

store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
Expand All @@ -1594,8 +1587,6 @@ func TestDisableTTLForTempTable(t *testing.T) {
}

func TestDisableTTLForFKParentTable(t *testing.T) {
parser.TTLFeatureGate = true

store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
Expand Down
1 change: 0 additions & 1 deletion executor/showtest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ go_test(
"//executor",
"//infoschema",
"//meta/autoid",
"//parser",
"//parser/auth",
"//parser/model",
"//parser/mysql",
Expand Down
3 changes: 0 additions & 3 deletions executor/showtest/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/executor"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/auth"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
Expand Down Expand Up @@ -2020,8 +2019,6 @@ func TestShowLimitReturnRow(t *testing.T) {
}

func TestShowTTLOption(t *testing.T) {
parser.TTLFeatureGate = true

store := testkit.CreateMockStore(t)

tk := testkit.NewTestKit(t, store)
Expand Down
1 change: 0 additions & 1 deletion parser/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ go_library(
"lexer.go",
"misc.go",
"parser.go",
"ttlfeaturegate.go",
"yy_parser.go",
],
importpath = "github.com/pingcap/tidb/parser",
Expand Down
3 changes: 0 additions & 3 deletions parser/ast/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package ast_test
import (
"testing"

"github.com/pingcap/tidb/parser"
. "github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/format"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -843,8 +842,6 @@ func TestFlashBackDatabaseRestore(t *testing.T) {
}

func TestTableOptionTTLRestore(t *testing.T) {
parser.TTLFeatureGate = true

sourceSQL1 := "create table t (created_at datetime) ttl = created_at + INTERVAL 1 YEAR"
sourceSQL2 := "alter table t ttl_enable = 'OFF'"
sourceSQL3 := "alter table t remove ttl"
Expand Down
12 changes: 0 additions & 12 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -11962,10 +11962,6 @@ yynewstate:
}
case 38:
{
if !TTLFeatureGate {
yylex.AppendError(ErrSyntax)
return 1
}
parser.yyVAL.item = &ast.AlterTableSpec{
Tp: ast.AlterTableRemoveTTL,
}
Expand Down Expand Up @@ -20080,10 +20076,6 @@ yynewstate:
}
case 2156:
{
if !TTLFeatureGate {
yylex.AppendError(ErrSyntax)
return 1
}
parser.yyVAL.item = &ast.TableOption{
Tp: ast.TableOptionTTL,
ColumnName: &ast.ColumnName{Name: model.NewCIStr(yyS[yypt-4].ident)},
Expand All @@ -20093,10 +20085,6 @@ yynewstate:
}
case 2157:
{
if !TTLFeatureGate {
yylex.AppendError(ErrSyntax)
return 1
}
onOrOff := strings.ToLower(yyS[yypt-0].ident)
if onOrOff == "on" {
parser.yyVAL.item = &ast.TableOption{Tp: ast.TableOptionTTLEnable, BoolValue: true}
Expand Down
12 changes: 0 additions & 12 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -1742,10 +1742,6 @@ AlterTableSpecSingleOpt:
}
| "REMOVE" "TTL"
{
if !TTLFeatureGate {
yylex.AppendError(ErrSyntax)
return 1
}
$$ = &ast.AlterTableSpec{
Tp: ast.AlterTableRemoveTTL,
}
Expand Down Expand Up @@ -11787,10 +11783,6 @@ TableOption:
}
| "TTL" EqOpt Identifier '+' "INTERVAL" Literal TimeUnit
{
if !TTLFeatureGate {
yylex.AppendError(ErrSyntax)
return 1
}
$$ = &ast.TableOption{
Tp: ast.TableOptionTTL,
ColumnName: &ast.ColumnName{Name: model.NewCIStr($3)},
Expand All @@ -11800,10 +11792,6 @@ TableOption:
}
| "TTL_ENABLE" EqOpt stringLit
{
if !TTLFeatureGate {
yylex.AppendError(ErrSyntax)
return 1
}
onOrOff := strings.ToLower($3)
if onOrOff == "on" {
$$ = &ast.TableOption{Tp: ast.TableOptionTTLEnable, BoolValue: true}
Expand Down
20 changes: 0 additions & 20 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7021,8 +7021,6 @@ func TestIntervalPartition(t *testing.T) {
}

func TestTTLTableOption(t *testing.T) {
parser.TTLFeatureGate = true

table := []testCase{
// create table with various temporal interval
{"create table t (created_at datetime) TTL = created_at + INTERVAL 3.1415 YEAR", true, "CREATE TABLE `t` (`created_at` DATETIME) TTL = `created_at` + INTERVAL 3.1415 YEAR"},
Expand Down Expand Up @@ -7050,21 +7048,3 @@ func TestTTLTableOption(t *testing.T) {

RunTest(t, table, false)
}

func TestTTLFeatureGate(t *testing.T) {
parser.TTLFeatureGate = false

table := []testCase{
{"create table t (created_at datetime) TTL = created_at + INTERVAL 3.1415 YEAR", false, ""},
{"create table t (created_at datetime) TTL_ENABLE = 'OFF'", false, ""},
{"create table t (created_at datetime) TTL created_at + INTERVAL 1 YEAR TTL_ENABLE 'OFF'", false, ""},
{"create table t (created_at datetime) /*T![ttl] ttl=created_at + INTERVAL 1 YEAR ttl_enable='ON'*/", false, ""},
{"alter table t TTL = created_at + INTERVAL 1 MONTH", false, ""},
{"alter table t TTL_ENABLE = 'ON'", false, ""},
{"alter table t TTL = created_at + INTERVAL 1 MONTH TTL_ENABLE 'OFF'", false, ""},
{"alter table t /*T![ttl] ttl=created_at + INTERVAL 1 YEAR ttl_enable='ON'*/", false, ""},
{"alter table t remove ttl", false, ""},
}

RunTest(t, table, false)
}
17 changes: 0 additions & 17 deletions parser/ttlfeaturegate.go

This file was deleted.

64 changes: 64 additions & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -2173,6 +2173,70 @@ var defaultSysVars = []*SysVar{
return nil
},
},
{
Scope: ScopeGlobal, Name: TiDBTTLJobRunInterval, Value: DefTiDBTTLJobRunInterval, Type: TypeDuration, MinValue: int64(10 * time.Minute), MaxValue: uint64(8760 * time.Hour), SetGlobal: func(ctx context.Context, vars *SessionVars, s string) error {
interval, err := time.ParseDuration(s)
if err != nil {
return err
}
TTLJobRunInterval.Store(interval)
return nil
}, GetGlobal: func(ctx context.Context, vars *SessionVars) (string, error) {
interval := TTLJobRunInterval.Load()

return interval.String(), nil
},
},
{
Scope: ScopeGlobal, Name: TiDBTTLJobScheduleWindowStartTime, Value: DefTiDBTTLJobScheduleWindowStartTime, Type: TypeTime, SetGlobal: func(ctx context.Context, vars *SessionVars, s string) error {
startTime, err := time.ParseInLocation(FullDayTimeFormat, s, time.UTC)
if err != nil {
return err
}
TTLJobScheduleWindowStartTime.Store(startTime)
return nil
}, GetGlobal: func(ctx context.Context, vars *SessionVars) (string, error) {
startTime := TTLJobScheduleWindowStartTime.Load()
return startTime.Format(FullDayTimeFormat), nil
},
},
{
Scope: ScopeGlobal, Name: TiDBTTLJobScheduleWindowEndTime, Value: DefTiDBTTLJobScheduleWindowEndTime, Type: TypeTime, SetGlobal: func(ctx context.Context, vars *SessionVars, s string) error {
endTime, err := time.ParseInLocation(FullDayTimeFormat, s, time.UTC)
if err != nil {
return err
}
TTLJobScheduleWindowEndTime.Store(endTime)
return nil
}, GetGlobal: func(ctx context.Context, vars *SessionVars) (string, error) {
endTime := TTLJobScheduleWindowEndTime.Load()
return endTime.Format(FullDayTimeFormat), nil
},
},
{
Scope: ScopeGlobal, Name: TiDBTTLScanWorkerCount, Value: strconv.Itoa(DefTiDBTTLScanWorkerCount), Type: TypeUnsigned, MinValue: 1, MaxValue: 256, SetGlobal: func(ctx context.Context, vars *SessionVars, s string) error {
val, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return err
}
TTLScanWorkerCount.Store(int32(val))
return nil
}, GetGlobal: func(ctx context.Context, vars *SessionVars) (string, error) {
return strconv.Itoa(int(TTLScanWorkerCount.Load())), nil
},
},
{
Scope: ScopeGlobal, Name: TiDBTTLDeleteWorkerCount, Value: strconv.Itoa(DefTiDBTTLDeleteWorkerCount), Type: TypeUnsigned, MinValue: 1, MaxValue: 256, SetGlobal: func(ctx context.Context, vars *SessionVars, s string) error {
val, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return err
}
TTLDeleteWorkerCount.Store(int32(val))
return nil
}, GetGlobal: func(ctx context.Context, vars *SessionVars) (string, error) {
return strconv.Itoa(int(TTLDeleteWorkerCount.Load())), nil
},
},
}

// FeedbackProbability points to the FeedbackProbability in statistics package.
Expand Down
41 changes: 41 additions & 0 deletions sessionctx/variable/sysvar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strconv"
"sync/atomic"
"testing"
"time"

"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/config"
Expand Down Expand Up @@ -1051,3 +1052,43 @@ func TestSetAggPushDownGlobally(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "ON", val)
}

func TestSetJobScheduleWindow(t *testing.T) {
vars := NewSessionVars(nil)
mock := NewMockGlobalAccessor4Tests()
mock.SessionVars = vars
vars.GlobalVarsAccessor = mock

// default value
val, err := mock.GetGlobalSysVar(TiDBTTLJobScheduleWindowStartTime)
require.NoError(t, err)
require.Equal(t, "00:00 +0000", val)

// set and get variable in UTC
vars.TimeZone = time.UTC
err = mock.SetGlobalSysVar(context.Background(), TiDBTTLJobScheduleWindowStartTime, "16:11")
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBTTLJobScheduleWindowStartTime)
require.NoError(t, err)
require.Equal(t, "16:11 +0000", val)

// set variable in UTC, get it in Asia/Shanghai
vars.TimeZone = time.UTC
err = mock.SetGlobalSysVar(context.Background(), TiDBTTLJobScheduleWindowStartTime, "16:11")
require.NoError(t, err)
vars.TimeZone, err = time.LoadLocation("Asia/Shanghai")
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBTTLJobScheduleWindowStartTime)
require.NoError(t, err)
require.Equal(t, "16:11 +0000", val)

// set variable in Asia/Shanghai, get it it UTC
vars.TimeZone, err = time.LoadLocation("Asia/Shanghai")
require.NoError(t, err)
err = mock.SetGlobalSysVar(context.Background(), TiDBTTLJobScheduleWindowStartTime, "16:11")
require.NoError(t, err)
vars.TimeZone = time.UTC
val, err = mock.GetGlobalSysVar(TiDBTTLJobScheduleWindowStartTime)
require.NoError(t, err)
require.Equal(t, "16:11 +0800", val)
}
Loading