-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
bootstrap: use BatchCreateTableWithInfo to speed up #42432
Closed
Closed
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
099a974
bootstrap: use BatchCreateTableWithInfo to speed up
lance6716 c13cbc5
fix CI
lance6716 5efd987
fix CI
lance6716 0220324
try fix CI
lance6716 f4e7129
fix CI
lance6716 bc0c202
Merge branch 'master' of github.com:pingcap/tidb into batch-bootstrap
lance6716 4516948
fix missing MDL view
lance6716 de3dbc5
retain TableID
lance6716 021de88
fix CI
lance6716 935bcd2
Update executor/executor_test.go
hawkingrei 98b8e9e
address comment
lance6716 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,11 +40,13 @@ import ( | |
"github.com/pingcap/tidb/meta" | ||
"github.com/pingcap/tidb/owner" | ||
"github.com/pingcap/tidb/parser" | ||
"github.com/pingcap/tidb/parser/ast" | ||
"github.com/pingcap/tidb/parser/auth" | ||
"github.com/pingcap/tidb/parser/model" | ||
"github.com/pingcap/tidb/parser/mysql" | ||
"github.com/pingcap/tidb/parser/terror" | ||
"github.com/pingcap/tidb/planner/core" | ||
"github.com/pingcap/tidb/sessionctx" | ||
"github.com/pingcap/tidb/sessionctx/variable" | ||
"github.com/pingcap/tidb/table/tables" | ||
"github.com/pingcap/tidb/types" | ||
|
@@ -586,6 +588,31 @@ const ( | |
PRIMARY KEY (job_id), | ||
KEY (create_time), | ||
KEY (create_user));` | ||
// please make sure newly added CREATE TABLE consts are also added to below | ||
// allCreateTablesPart2 | ||
) | ||
|
||
var ( | ||
allCreateTablesPart1 = []string{ | ||
CreateUserTable, CreatePasswordHistory, CreateGlobalPrivTable, | ||
CreateDBPrivTable, CreateTablePrivTable, CreateColumnPrivTable, | ||
CreateGlobalVariablesTable, CreateTiDBTable, CreateHelpTopic, | ||
CreateStatsMetaTable, CreateStatsColsTable, CreateStatsBucketsTable, | ||
CreateGCDeleteRangeTable, CreateGCDeleteRangeDoneTable, CreateStatsFeedbackTable, | ||
CreateRoleEdgesTable, CreateDefaultRolesTable, CreateBindInfoTable, | ||
CreateStatsTopNTable, CreateExprPushdownBlacklist, CreateOptRuleBlacklist, | ||
CreateStatsExtended, CreateSchemaIndexUsageTable, CreateStatsFMSketchTable, | ||
CreateGlobalGrantsTable, CreateCapturePlanBaselinesBlacklist, | ||
CreateColumnStatsUsageTable, CreateTableCacheMetaTable, CreateAnalyzeOptionsTable, | ||
CreateStatsHistory, CreateStatsMetaHistory, CreateAnalyzeJobs, | ||
CreateAdvisoryLocks, | ||
} | ||
// we should create MDL view between allCreateTablesPart1 and allCreateTablesPart2 | ||
allCreateTablesPart2 = []string{ | ||
CreatePlanReplayerStatusTable, CreatePlanReplayerTaskTable, | ||
CreateStatsTableLocked, CreateTTLTableStatus, CreateTTLTask, | ||
CreateTTLJobHistory, CreateGlobalTask, CreateLoadDataJobs, | ||
} | ||
) | ||
|
||
// bootstrap initiates system DB for a store. | ||
|
@@ -613,7 +640,7 @@ func bootstrap(s Session) { | |
// To reduce conflict when multiple TiDB-server start at the same time. | ||
// Actually only one server need to do the bootstrap. So we chose DDL owner to do this. | ||
if dom.DDL().OwnerManager().IsOwner() { | ||
doDDLWorks(s) | ||
doDDLWorks(s, dom) | ||
doDMLWorks(s) | ||
runBootstrapSQLFile = true | ||
logutil.BgLogger().Info("bootstrap successful", | ||
|
@@ -1747,11 +1774,6 @@ func upgradeToVer57(s Session, ver int64) { | |
insertBuiltinBindInfoRow(s) | ||
} | ||
|
||
func initBindInfoTable(s Session) { | ||
mustExecute(s, CreateBindInfoTable) | ||
insertBuiltinBindInfoRow(s) | ||
} | ||
|
||
func insertBuiltinBindInfoRow(s Session) { | ||
mustExecute(s, `INSERT HIGH_PRIORITY INTO mysql.bind_info(original_sql, bind_sql, default_db, status, create_time, update_time, charset, collation, source) | ||
VALUES (%?, %?, "mysql", %?, "0000-00-00 00:00:00", "0000-00-00 00:00:00", "", "", %?)`, | ||
|
@@ -2460,94 +2482,74 @@ func getBootstrapVersion(s Session) (int64, error) { | |
} | ||
|
||
// doDDLWorks executes DDL statements in bootstrap stage. | ||
func doDDLWorks(s Session) { | ||
func doDDLWorks(s Session, dom *domain.Domain) { | ||
// Create a test database. | ||
mustExecute(s, "CREATE DATABASE IF NOT EXISTS test") | ||
// Create system db. | ||
mustExecute(s, "CREATE DATABASE IF NOT EXISTS %n", mysql.SystemDB) | ||
// Create user table. | ||
mustExecute(s, CreateUserTable) | ||
// Create password history. | ||
mustExecute(s, CreatePasswordHistory) | ||
// Create privilege tables. | ||
mustExecute(s, CreateGlobalPrivTable) | ||
mustExecute(s, CreateDBPrivTable) | ||
mustExecute(s, CreateTablePrivTable) | ||
mustExecute(s, CreateColumnPrivTable) | ||
// Create global system variable table. | ||
mustExecute(s, CreateGlobalVariablesTable) | ||
// Create TiDB table. | ||
mustExecute(s, CreateTiDBTable) | ||
// Create help table. | ||
mustExecute(s, CreateHelpTopic) | ||
// Create stats_meta table. | ||
mustExecute(s, CreateStatsMetaTable) | ||
// Create stats_columns table. | ||
mustExecute(s, CreateStatsColsTable) | ||
// Create stats_buckets table. | ||
mustExecute(s, CreateStatsBucketsTable) | ||
// Create gc_delete_range table. | ||
mustExecute(s, CreateGCDeleteRangeTable) | ||
// Create gc_delete_range_done table. | ||
mustExecute(s, CreateGCDeleteRangeDoneTable) | ||
// Create stats_feedback table. | ||
mustExecute(s, CreateStatsFeedbackTable) | ||
// Create role_edges table. | ||
mustExecute(s, CreateRoleEdgesTable) | ||
// Create default_roles table. | ||
mustExecute(s, CreateDefaultRolesTable) | ||
// Create bind_info table. | ||
initBindInfoTable(s) | ||
// Create stats_topn_store table. | ||
mustExecute(s, CreateStatsTopNTable) | ||
// Create expr_pushdown_blacklist table. | ||
mustExecute(s, CreateExprPushdownBlacklist) | ||
// Create opt_rule_blacklist table. | ||
mustExecute(s, CreateOptRuleBlacklist) | ||
// Create stats_extended table. | ||
mustExecute(s, CreateStatsExtended) | ||
// Create schema_index_usage. | ||
mustExecute(s, CreateSchemaIndexUsageTable) | ||
// Create stats_fm_sketch table. | ||
mustExecute(s, CreateStatsFMSketchTable) | ||
// Create global_grants | ||
mustExecute(s, CreateGlobalGrantsTable) | ||
// Create capture_plan_baselines_blacklist | ||
mustExecute(s, CreateCapturePlanBaselinesBlacklist) | ||
// Create column_stats_usage table | ||
mustExecute(s, CreateColumnStatsUsageTable) | ||
// Create table_cache_meta table. | ||
mustExecute(s, CreateTableCacheMetaTable) | ||
// Create analyze_options table. | ||
mustExecute(s, CreateAnalyzeOptionsTable) | ||
// Create stats_history table. | ||
mustExecute(s, CreateStatsHistory) | ||
// Create stats_meta_history table. | ||
mustExecute(s, CreateStatsMetaHistory) | ||
// Create analyze_jobs table. | ||
mustExecute(s, CreateAnalyzeJobs) | ||
// Create advisory_locks table. | ||
mustExecute(s, CreateAdvisoryLocks) | ||
// Create mdl view. | ||
p := parser.New() | ||
tableInfosPart1 := make([]*model.TableInfo, 0, len(allCreateTablesPart1)) | ||
for _, createTable := range allCreateTablesPart1 { | ||
stmt, err := p.ParseOneStmt(createTable, "", "") | ||
if err != nil { | ||
logutil.BgLogger().Fatal("ParseOneStmt error", zap.Error(err), zap.Stack("stack")) | ||
} | ||
tblInfo, err := ddl.BuildTableInfoWithContext(s, stmt.(*ast.CreateTableStmt)) | ||
if err != nil { | ||
logutil.BgLogger().Fatal("BuildTableInfoFromAST error", zap.Error(err), zap.Stack("stack")) | ||
} | ||
tableInfosPart1 = append(tableInfosPart1, tblInfo) | ||
} | ||
batchCreateTable(s, dom.DDL(), tableInfosPart1) | ||
mustExecute(s, CreateMDLView) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add comment |
||
// Create plan_replayer_status table | ||
mustExecute(s, CreatePlanReplayerStatusTable) | ||
// Create plan_replayer_task table | ||
mustExecute(s, CreatePlanReplayerTaskTable) | ||
// Create stats_meta_table_locked table | ||
mustExecute(s, CreateStatsTableLocked) | ||
// Create tidb_ttl_table_status table | ||
mustExecute(s, CreateTTLTableStatus) | ||
// Create tidb_ttl_task table | ||
mustExecute(s, CreateTTLTask) | ||
// Create tidb_ttl_job_history table | ||
mustExecute(s, CreateTTLJobHistory) | ||
// Create tidb_global_task table | ||
mustExecute(s, CreateGlobalTask) | ||
tableInfosPart2 := make([]*model.TableInfo, 0, len(allCreateTablesPart2)) | ||
for _, createTable := range allCreateTablesPart2 { | ||
stmt, err := p.ParseOneStmt(createTable, "", "") | ||
if err != nil { | ||
logutil.BgLogger().Fatal("ParseOneStmt error", zap.Error(err), zap.Stack("stack")) | ||
} | ||
tblInfo, err := ddl.BuildTableInfoWithContext(s, stmt.(*ast.CreateTableStmt)) | ||
if err != nil { | ||
logutil.BgLogger().Fatal("BuildTableInfoFromAST error", zap.Error(err), zap.Stack("stack")) | ||
} | ||
tableInfosPart2 = append(tableInfosPart2, tblInfo) | ||
} | ||
batchCreateTable(s, dom.DDL(), tableInfosPart2) | ||
|
||
err := dom.Reload() | ||
if err != nil { | ||
logutil.BgLogger().Fatal("Reload error", zap.Error(err), zap.Stack("stack")) | ||
} | ||
|
||
// init bind_info table. | ||
insertBuiltinBindInfoRow(s) | ||
// Create default resource group | ||
mustExecute(s, CreateDefaultResourceGroup) | ||
// Create load_data_jobs | ||
mustExecute(s, CreateLoadDataJobs) | ||
} | ||
|
||
func batchCreateTable(s Session, d ddl.DDL, tableInfos []*model.TableInfo) { | ||
s.SetValue(sessionctx.QueryString, "skip") | ||
|
||
err := d.BatchCreateTableWithInfo( | ||
s, | ||
model.NewCIStr(mysql.SystemDB), | ||
tableInfos, | ||
ddl.OnExistIgnore, | ||
ddl.SkipTableID(1), | ||
) | ||
if err == nil { | ||
return | ||
} | ||
if kv.ErrEntryTooLarge.Equal(err) { | ||
if len(tableInfos) == 1 { | ||
logutil.BgLogger().Fatal("too large TableInfo", zap.Any("tableInfo", tableInfos[0]), zap.Stack("stack")) | ||
} | ||
mid := len(tableInfos) / 2 | ||
batchCreateTable(s, d, tableInfos[:mid]) | ||
batchCreateTable(s, d, tableInfos[mid:]) | ||
} else { | ||
logutil.BgLogger().Fatal("batchCreateTable meet error", zap.Error(err), zap.Stack("stack")) | ||
} | ||
} | ||
|
||
// doBootstrapSQLFile executes SQL commands in a file as the last stage of bootstrap. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Due to BatchCreateTableWithInfo, we have less history DDL jobs