Skip to content

Commit

Permalink
timer: add sys table mysql.tidb_timers to store all timers in tidb (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lcwangchao committed Jul 13, 2023
1 parent 756ba49 commit f9e5621
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 9 deletions.
2 changes: 1 addition & 1 deletion executor/infoschema_cluster_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func TestTableStorageStats(t *testing.T) {
"test 2",
))
rows := tk.MustQuery("select TABLE_NAME from information_schema.TABLE_STORAGE_STATS where TABLE_SCHEMA = 'mysql';").Rows()
result := 51
result := 52
require.Len(t, rows, result)

// More tests about the privileges.
Expand Down
10 changes: 5 additions & 5 deletions server/testdata/optimizer_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"Version": 440930000000000000
},
"TableName": "t",
"TblInfoID": 94,
"TblInfoID": 96,
"Uninitialized": true,
"UsePartitionStats": false
}
Expand Down Expand Up @@ -139,7 +139,7 @@
"Version": 440930000000000000
},
"TableName": "t",
"TblInfoID": 94,
"TblInfoID": 96,
"Uninitialized": true,
"UsePartitionStats": false
}
Expand Down Expand Up @@ -379,7 +379,7 @@
"Version": 440930000000000000
},
"TableName": "t",
"TblInfoID": 94,
"TblInfoID": 96,
"Uninitialized": true,
"UsePartitionStats": false
}
Expand Down Expand Up @@ -550,7 +550,7 @@
"Version": 440930000000000000
},
"TableName": "t",
"TblInfoID": 94,
"TblInfoID": 96,
"Uninitialized": true,
"UsePartitionStats": false
}
Expand Down Expand Up @@ -618,7 +618,7 @@
"Version": 440930000000000000
},
"TableName": "t",
"TblInfoID": 94,
"TblInfoID": 96,
"Uninitialized": true,
"UsePartitionStats": false
}
Expand Down
3 changes: 2 additions & 1 deletion session/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ go_library(
"//tablecodec",
"//telemetry",
"//testkit/testenv",
"//timer/tablestore",
"//ttl/ttlworker",
"//types",
"//types/parser_driver",
Expand Down Expand Up @@ -129,7 +130,7 @@ go_test(
embed = [":session"],
flaky = True,
race = "on",
shard_count = 49,
shard_count = 50,
deps = [
"//autoid_service",
"//bindinfo",
Expand Down
17 changes: 16 additions & 1 deletion session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/table/tables"
timertable "github.com/pingcap/tidb/timer/tablestore"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/dbterror"
Expand Down Expand Up @@ -651,6 +652,9 @@ const (
KEY (status));`
)

// CreateTimers is a table to store all timers for tidb
var CreateTimers = timertable.CreateTimerTableSQL("mysql", "tidb_timers")

// bootstrap initiates system DB for a store.
func bootstrap(s Session) {
startTime := time.Now()
Expand Down Expand Up @@ -942,11 +946,12 @@ const (
version167 = 167
version168 = 168
version169 = 169
version170 = 170
)

// currentBootstrapVersion is defined as a variable, so we can modify its value for testing.
// please make sure this is the largest version
var currentBootstrapVersion int64 = version169
var currentBootstrapVersion int64 = version170

// DDL owner key's expired time is ManagerSessionTTL seconds, we should wait the time and give more time to have a chance to finish it.
var internalSQLTimeout = owner.ManagerSessionTTL + 15
Expand Down Expand Up @@ -1084,6 +1089,7 @@ var (
upgradeToVer167,
upgradeToVer168,
upgradeToVer169,
upgradeToVer170,
}
)

Expand Down Expand Up @@ -2731,6 +2737,13 @@ func upgradeToVer169(s Session, ver int64) {
mustExecute(s, CreateRunawayTable)
}

func upgradeToVer170(s Session, ver int64) {
if ver >= version170 {
return
}
mustExecute(s, CreateTimers)
}

func writeOOMAction(s Session) {
comment := "oom-action is `log` by default in v3.0.x, `cancel` by default in v4.0.11+"
mustExecute(s, `INSERT HIGH_PRIORITY INTO %n.%n VALUES (%?, %?, %?) ON DUPLICATE KEY UPDATE VARIABLE_VALUE= %?`,
Expand Down Expand Up @@ -2851,6 +2864,8 @@ func doDDLWorks(s Session) {
mustExecute(s, CreateRunawayQuarantineWatchTable)
// create runaway_queries
mustExecute(s, CreateRunawayTable)
// create tidb_timers
mustExecute(s, CreateTimers)
}

// doBootstrapSQLFile executes SQL commands in a file as the last stage of bootstrap.
Expand Down
29 changes: 29 additions & 0 deletions session/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2208,3 +2208,32 @@ func TestTiDBUpgradeToVer145(t *testing.T) {
require.Less(t, int64(ver144), ver)
dom.Close()
}

func TestTiDBUpgradeToVer170(t *testing.T) {
store, _ := CreateStoreAndBootstrap(t)
defer func() {
require.NoError(t, store.Close())
}()
ver169 := version169
seV169 := CreateSessionAndSetID(t, store)
txn, err := store.Begin()
require.NoError(t, err)
m := meta.NewMeta(txn)
err = m.FinishBootstrap(int64(ver169))
require.NoError(t, err)
mustExec(t, seV169, fmt.Sprintf("update mysql.tidb set variable_value=%d where variable_name='tidb_server_version'", ver169))
err = txn.Commit(context.Background())
require.NoError(t, err)

unsetStoreBootstrapped(store.UUID())
ver, err := getBootstrapVersion(seV169)
require.NoError(t, err)
require.Equal(t, int64(ver169), ver)

dom, err := BootstrapSession(store)
require.NoError(t, err)
ver, err = getBootstrapVersion(seV169)
require.NoError(t, err)
require.Less(t, int64(ver169), ver)
dom.Close()
}
1 change: 0 additions & 1 deletion timer/tablestore/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func CreateTimerTableSQL(dbName, tableName string) string {
VERSION BIGINT(64) UNSIGNED NOT NULL,
PRIMARY KEY (ID),
UNIQUE KEY timer_key(NAMESPACE, TIMER_KEY),
KEY tags((CAST(TIMER_EXT->'$.tags' AS CHAR(256) ARRAY))),
KEY hook_class(HOOK_CLASS)
)`, indentString(dbName, tableName))
}
Expand Down

0 comments on commit f9e5621

Please sign in to comment.