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

session: use mDDLTableVersion key to control backfill tables #40984

Merged
merged 5 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
25 changes: 24 additions & 1 deletion meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,12 @@ func (m *Meta) SetMDLTables() error {
return errors.Trace(err)
}

// SetBackfillTables write a key into storage.
func (m *Meta) SetBackfillTables() error {
err := m.txn.Set(mDDLTableVersion, []byte("3"))
return errors.Trace(err)
}

// CreateMySQLDatabaseIfNotExists creates mysql schema and return its DB ID.
func (m *Meta) CreateMySQLDatabaseIfNotExists() (int64, error) {
id, err := m.GetSystemDBID()
Expand Down Expand Up @@ -681,7 +687,24 @@ func (m *Meta) CheckMDLTableExists() (bool, error) {
if err != nil {
return false, errors.Trace(err)
}
return bytes.Equal(v, []byte("2")), nil
ver, err := strconv.ParseUint(string(v), 10, 64)
if err != nil {
return false, errors.Trace(err)
}
return ver >= 2, nil
}

// CheckBackfillTableExists check if the tables related to concurrent DDL exists.
func (m *Meta) CheckBackfillTableExists() (bool, error) {
Defined2014 marked this conversation as resolved.
Show resolved Hide resolved
Defined2014 marked this conversation as resolved.
Show resolved Hide resolved
v, err := m.txn.Get(mDDLTableVersion)
if err != nil {
return false, errors.Trace(err)
}
ver, err := strconv.ParseUint(string(v), 10, 64)
if err != nil {
return false, errors.Trace(err)
}
return ver >= 3, nil
}

// SetMetadataLock sets the metadata lock.
Expand Down
41 changes: 24 additions & 17 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -3108,36 +3108,20 @@ func InitDDLJobTables(store kv.Storage) error {
return kv.RunInNewTxn(kv.WithInternalSourceType(context.Background(), kv.InternalTxnDDL), store, true, func(ctx context.Context, txn kv.Transaction) error {
t := meta.NewMeta(txn)
exists, err := t.CheckDDLTableExists()
if err != nil {
if err != nil || exists {
return errors.Trace(err)
}
dbID, err := t.CreateMySQLDatabaseIfNotExists()
if err != nil {
return err
}
if exists {
return initBackfillJobTables(store, t, dbID)
}

if err = createAndSplitTables(store, t, dbID, DDLJobTables); err != nil {
return err
}
if err = initBackfillJobTables(store, t, dbID); err != nil {
return err
}
return t.SetDDLTables()
})
}

// initBackfillJobTables is to create tidb_ddl_backfill and tidb_ddl_backfill_history.
func initBackfillJobTables(store kv.Storage, t *meta.Meta, dbID int64) error {
tblExist, err := t.CheckTableExists(dbID, BackfillTables[0].id)
if err != nil || tblExist {
return errors.Trace(err)
}
return createAndSplitTables(store, t, dbID, BackfillTables)
}

func createAndSplitTables(store kv.Storage, t *meta.Meta, dbID int64, tables []tableBasicInfo) error {
tableIDs := make([]int64, 0, len(tables))
for _, tbl := range tables {
Expand Down Expand Up @@ -3199,6 +3183,25 @@ func InitMDLTable(store kv.Storage) error {
})
}

// InitBackfillTable is to create tidb_ddl_backfill and tidb_ddl_backfill_history, which is used for distreorg.
func InitBackfillTable(store kv.Storage) error {
return kv.RunInNewTxn(kv.WithInternalSourceType(context.Background(), kv.InternalTxnDDL), store, true, func(ctx context.Context, txn kv.Transaction) error {
t := meta.NewMeta(txn)
exists, err := t.CheckBackfillTableExists()
if err != nil || exists {
return errors.Trace(err)
}
dbID, err := t.CreateMySQLDatabaseIfNotExists()
if err != nil {
return err
}
if err = createAndSplitTables(store, t, dbID, BackfillTables); err != nil {
return err
}
return t.SetBackfillTables()
})
}

// InitMDLVariableForBootstrap initializes the metadata lock variable.
func InitMDLVariableForBootstrap(store kv.Storage) error {
err := kv.RunInNewTxn(kv.WithInternalSourceType(context.Background(), kv.InternalTxnDDL), store, true, func(ctx context.Context, txn kv.Transaction) error {
Expand Down Expand Up @@ -3280,6 +3283,10 @@ func BootstrapSession(store kv.Storage) (*domain.Domain, error) {
if err != nil {
return nil, err
}
err = InitBackfillTable(store)
if err != nil {
return nil, err
}
ver := getStoreBootstrapVersion(store)
if ver == notBootstrapped {
runInBootstrapSession(store, bootstrap)
Expand Down
12 changes: 9 additions & 3 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ func TestInitMetaTable(t *testing.T) {
tk.MustExec(sql.SQL)
}

for _, sql := range session.BackfillTables {
tk.MustExec(sql.SQL)
}

tbls := map[string]struct{}{
"tidb_ddl_job": {},
"tidb_ddl_reorg": {},
"tidb_ddl_history": {},
"tidb_ddl_job": {},
"tidb_ddl_reorg": {},
"tidb_ddl_history": {},
"tidb_ddl_backfill": {},
"tidb_ddl_backfill_history": {},
}

for tbl := range tbls {
Expand Down