Skip to content

Commit

Permalink
session: add indexes for mysql.analyze_jobs
Browse files Browse the repository at this point in the history
Signed-off-by: Rustin170506 <[email protected]>
  • Loading branch information
Rustin170506 committed Dec 10, 2024
1 parent 93295c2 commit 2b64e04
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
15 changes: 14 additions & 1 deletion pkg/session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -1203,11 +1203,15 @@ const (
// version 239
// add modify_params to tidb_global_task and tidb_global_task_history.
version239 = 239

// version 240
// Add indexes to mysql.analyze_jobs to speed up the query.
version240 = 240
)

// 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 = version239
var currentBootstrapVersion int64 = version240

// 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 @@ -1382,6 +1386,7 @@ var (
upgradeToVer217,
upgradeToVer218,
upgradeToVer239,
upgradeToVer240,
}
)

Expand Down Expand Up @@ -3287,6 +3292,14 @@ func upgradeToVer239(s sessiontypes.Session, ver int64) {
doReentrantDDL(s, "ALTER TABLE mysql.tidb_global_task_history ADD COLUMN modify_params json AFTER `error`;", infoschema.ErrColumnExists)
}

func upgradeToVer240(s sessiontypes.Session, ver int64) {
if ver >= version240 {
return
}
doReentrantDDL(s, "ALTER TABLE mysql.analyze_jobs ADD INDEX idx_schema_table_state (table_schema, table_name, state)", dbterror.ErrDupKeyName)
doReentrantDDL(s, "ALTER TABLE mysql.analyze_jobs ADD INDEX idx_schema_table_partition_state (table_schema, table_name, partition_name, state)", dbterror.ErrDupKeyName)
}

// initGlobalVariableIfNotExists initialize a global variable with specific val if it does not exist.
func initGlobalVariableIfNotExists(s sessiontypes.Session, name string, val any) {
ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnBootstrap)
Expand Down
45 changes: 45 additions & 0 deletions pkg/session/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2524,3 +2524,48 @@ func TestIndexJoinMultiPatternByUpgrade650To840(t *testing.T) {
require.Equal(t, 1, row.Len())
require.Equal(t, int64(0), row.GetInt64(0))
}

func TestTiDBUpgradeToVer240(t *testing.T) {
ctx := context.Background()
store, dom := CreateStoreAndBootstrap(t)
defer func() { require.NoError(t, store.Close()) }()

ver239 := version239
seV239 := CreateSessionAndSetID(t, store)
txn, err := store.Begin()
require.NoError(t, err)
m := meta.NewMutator(txn)
err = m.FinishBootstrap(int64(ver239))
require.NoError(t, err)
revertVersionAndVariables(t, seV239, ver239)
err = txn.Commit(ctx)
require.NoError(t, err)
store.SetOption(StoreBootstrappedKey, nil)

// Check index not exist.
res := MustExecToRecodeSet(t, seV239, "show create table mysql.analyze_jobs")
chk := res.NewChunk(nil)
err = res.Next(ctx, chk)
require.NoError(t, err)
require.Equal(t, 1, chk.NumRows())
require.NotContains(t, string(chk.GetRow(0).GetBytes(1)), "idx_schema_table_state")
require.NotContains(t, string(chk.GetRow(0).GetBytes(1)), "idx_schema_table_partition_state")

dom.Close()
domCurVer, err := BootstrapSession(store)
require.NoError(t, err)
defer domCurVer.Close()
seCurVer := CreateSessionAndSetID(t, store)
ver, err := getBootstrapVersion(seCurVer)
require.NoError(t, err)
require.Equal(t, currentBootstrapVersion, ver)

// Check index exist.
res = MustExecToRecodeSet(t, seCurVer, "show create table mysql.analyze_jobs")
chk = res.NewChunk(nil)
err = res.Next(ctx, chk)
require.NoError(t, err)
require.Equal(t, 1, chk.NumRows())
require.Contains(t, string(chk.GetRow(0).GetBytes(1)), "idx_schema_table_state")
require.Contains(t, string(chk.GetRow(0).GetBytes(1)), "idx_schema_table_partition_state")
}

0 comments on commit 2b64e04

Please sign in to comment.