Skip to content

ttl: Add TTL options in DDL #57

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

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
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
133 changes: 123 additions & 10 deletions ddl/ddl_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ func (c *testCase) generateDDLOps() error {
if err := c.generateSetTilfahReplica(defaultTime); err != nil {
return errors.Trace(err)
}
if err := c.generateModifyTableTTL(defaultTime); err != nil {
return errors.Trace(err)
}
return nil
}

Expand Down Expand Up @@ -121,7 +124,7 @@ const (
ddlSetTiflashReplica

ddlMultiSchemaChange

ddlModifyTableTTL
ddlKindNil
)

Expand Down Expand Up @@ -768,17 +771,21 @@ func (c *testCase) prepareAddTable(cfg interface{}, taskCh chan *ddlJobTask) err
}

charset, collate := c.pickupRandomCharsetAndCollate()
ttlExpr, ttlEnable, ttlSchedule := c.pickupRandomTTLOptions(tableColumns, true)

tableInfo := ddlTestTable{
name: uuid.NewV4().String()[:8],
columns: tableColumns,
indexes: make([]*ddlTestIndex, 0),
numberOfRows: 0,
deleted: 0,
comment: uuid.NewV4().String()[:8],
charset: charset,
collate: collate,
lock: new(sync.RWMutex),
name: uuid.NewV4().String()[:8],
columns: tableColumns,
indexes: make([]*ddlTestIndex, 0),
numberOfRows: 0,
deleted: 0,
comment: uuid.NewV4().String()[:8],
charset: charset,
collate: collate,
lock: new(sync.RWMutex),
ttlExpr: ttlExpr,
ttlEnable: ttlEnable,
ttlJobSchedule: ttlSchedule,
}

sql := fmt.Sprintf("CREATE TABLE `%s` (", tableInfo.name)
Expand All @@ -804,6 +811,17 @@ func (c *testCase) prepareAddTable(cfg interface{}, taskCh chan *ddlJobTask) err

sql += fmt.Sprintf("COMMENT '%s' CHARACTER SET '%s' COLLATE '%s'",
tableInfo.comment, charset, collate)
if ttlExpr != "" {
sql += " " + tableInfo.ttlExpr
}

if ttlEnable != "" {
sql += " " + ttlEnable
}

if ttlSchedule != "" {
sql += " " + ttlSchedule
}

if rand.Intn(3) == 0 && partitionColumnName != "" {
sql += fmt.Sprintf(" partition by hash(`%s`) partitions %d ", partitionColumnName, rand.Intn(10)+1)
Expand Down Expand Up @@ -1660,6 +1678,11 @@ func (c *testCase) prepareModifyColumn2(ctx interface{}, taskCh chan *ddlJobTask
sql += fmt.Sprintf(" AFTER `%s`", insertAfterColumn.name)
}
}

if checkIsTTLColumn(table, origColumn) && !checkColumnSupportTTL(modifiedColumn) {
return nil
}

if modifiedColumn.name == origColumn.name {
modifiedColumn.name = ""
}
Expand Down Expand Up @@ -1745,6 +1768,11 @@ func (c *testCase) prepareModifyColumn(ctx interface{}, taskCh chan *ddlJobTask)
sql += fmt.Sprintf(" AFTER `%s`", insertAfterColumn.name)
}
}

if checkIsTTLColumn(table, origColumn) && !checkColumnSupportTTL(modifiedColumn) {
return nil
}

if modifiedColumn.name == origColumn.name {
modifiedColumn.name = ""
}
Expand Down Expand Up @@ -1855,6 +1883,10 @@ func (c *testCase) prepareDropColumn(ctx interface{}, taskCh chan *ddlJobTask) e

columnToDrop := getColumnFromArrayList(table.columns, columnToDropIndex)

if checkIsTTLColumn(table, columnToDrop) {
return nil
}

if !checkAddDropColumn(ctx, columnToDrop) {
return nil
}
Expand Down Expand Up @@ -2016,6 +2048,87 @@ func (c *testCase) generateSetTilfahReplica(repeat int) error {
return nil
}

type ddlModifyTableTTLArg struct {
ttlExpr string
ttlEnable string
ttlJobSchedule string
}

func (c *testCase) generateModifyTableTTL(repeat int) error {
for i := 0; i < repeat; i++ {
c.ddlOps = append(c.ddlOps, ddlTestOpExecutor{c.prepareModifyTableTTL, nil, ddlModifyTableTTL})
}
return nil
}

func (c *testCase) prepareModifyTableTTL(_ interface{}, taskCh chan *ddlJobTask) error {
table := c.pickupRandomTable()
if table == nil {
return nil
}

opt := ""
var ttlExpr, ttlEnable, ttlSchedule string
if rand.Intn(5) == 0 {
opt = " REMOVE TTL"
} else {
ttlExpr, ttlEnable, ttlSchedule = c.pickupRandomTTLOptions(table.columns, table.ttlExpr == "")
if ttlExpr != "" {
opt += " " + ttlExpr
}
if ttlEnable != "" {
opt += " " + ttlEnable
}
if ttlSchedule != "" {
opt += " " + ttlSchedule
}
}

if opt == "" {
return nil
}

sql := fmt.Sprintf("ALTER TABLE `%s` %s", table.name, opt)
task := &ddlJobTask{
k: ddlModifyTableTTL,
sql: sql,
tblInfo: table,
arg: ddlJobArg(&ddlModifyTableTTLArg{
ttlExpr,
ttlEnable,
ttlSchedule,
}),
}
taskCh <- task
return nil
}

func (c *testCase) modifyTTLOptionJob(task *ddlJobTask) error {
table := task.tblInfo
table.lock.Lock()
defer table.lock.Unlock()
if c.isTableDeleted(table) {
return fmt.Errorf("table %s is not exists", table.name)
}
arg := (*ddlModifyTableTTLArg)(task.arg)
table.ttlExpr = arg.ttlExpr
table.ttlEnable = arg.ttlEnable
table.ttlJobSchedule = arg.ttlJobSchedule
return nil
}

func checkColumnSupportTTL(col *ddlTestColumn) bool {
switch col.k {
case KindDATE, KindDATETIME, KindTIMESTAMP:
return true
}
return false
}

func checkIsTTLColumn(tbl *ddlTestTable, col *ddlTestColumn) bool {
return strings.Contains(tbl.ttlExpr, fmt.Sprintf("`%s`", col.name))
}

type ddlSetTiflashReplicaArg struct {
cnt int
}
Expand Down
82 changes: 69 additions & 13 deletions ddl/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,59 @@ func (c *testCase) pickupRandomTable() *ddlTestTable {
return c.tables[name]
}

func (c *testCase) pickupRandomTTLOptions(columns *arraylist.List, forceTTLOption bool) (ttlExpr string, ttlEnable string, ttlJobSchedule string) {
timeColumns := make([]*ddlTestColumn, 0, columns.Size())
for ite := columns.Iterator(); ite.Next(); {
col := ite.Value().(*ddlTestColumn)
if checkColumnSupportTTL(col) {
timeColumns = append(timeColumns, col)
}

if col.isPrimaryKey {
ft := strings.ToLower(col.fieldType)
if strings.Contains(ft, "float") || strings.Contains(ft, "double") {
// TTL do not support double/float pk
return
}
}
}

ttlColIndex := rand.Intn(len(timeColumns) + 1)
if ttlColIndex >= len(timeColumns) {
return
}

if forceTTLOption || rand.Intn(2) > 0 {
ttlCol := timeColumns[ttlColIndex]
expireIntervals := []string{"1", "1.2"}
expireIntervalUnits := []string{"MINUTE", "HOUR", "DAY", "MONTH", "YEAR"}

ttlExpr = fmt.Sprintf("TTL=`%s` + INTERVAL %s %s",
ttlCol.name,
expireIntervals[rand.Intn(len(expireIntervals))],
expireIntervalUnits[rand.Intn(len(expireIntervalUnits))],
)
}

if rand.Intn(2) > 0 {
ttlEnable = fmt.Sprintf(" TTL_ENABLE='%s'", []string{"ON", "OFF"}[rand.Intn(2)])
}

if rand.Intn(2) > 0 {
intervals := []string{"1h20m", "1d10h30m", "1d20m"}
for _, v := range []string{"1"} {
for _, u := range []string{"m", "h", "d"} {
intervals = append(intervals, v+u)
}
}

interval := intervals[rand.Intn(len(intervals))]
ttlJobSchedule = fmt.Sprintf("TTL_JOB_INTERVAL='%s'", interval)
}

return
}

func (c *testCase) pickupRandomCharsetAndCollate() (string, string) {
// When a table created by a binary charset and collate, it would
// convert char type column to binary type which would cause the
Expand Down Expand Up @@ -163,19 +216,22 @@ func (c *testCase) isColumnDeleted(column *ddlTestColumn, table *ddlTestTable) b
}

type ddlTestTable struct {
deleted int32
name string
id string // table_id , get from admin show ddl jobs
columns *arraylist.List
indexes []*ddlTestIndex
numberOfRows int
shardRowId int64 // shard_row_id_bits
autoIncID int64
comment string // table comment
charset string
collate string
lock *sync.RWMutex
replicaCnt int
deleted int32
name string
id string // table_id , get from admin show ddl jobs
columns *arraylist.List
indexes []*ddlTestIndex
numberOfRows int
shardRowId int64 // shard_row_id_bits
autoIncID int64
comment string // table comment
charset string
collate string
lock *sync.RWMutex
replicaCnt int
ttlExpr string
ttlEnable string
ttlJobSchedule string
}

func (table *ddlTestTable) isDeleted() bool {
Expand Down