-
Notifications
You must be signed in to change notification settings - Fork 6.2k
bootstrap: add cluster_id to the mysql.tidb table (#59511)
#65564
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
Changes from 4 commits
7450065
8bd98c2
8cad105
f88160a
84ae464
f9e9b4b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -854,6 +854,8 @@ const ( | |
| tidbDefOOMAction = "default_oom_action" | ||
| // The variable name in mysql.tidb table and it records the current DDLTableVersion | ||
| tidbDDLTableVersion = "ddl_table_version" | ||
| // The variable name in mysql.tidb table and it records the cluster id of this cluster | ||
| tidbClusterID = "cluster_id" | ||
| // Const for TiDB server version 2. | ||
| version2 = 2 | ||
| version3 = 3 | ||
|
|
@@ -1244,16 +1246,20 @@ const ( | |
| // Add max_node_count column to tidb_global_task and tidb_global_task_history. | ||
| version224 = 224 | ||
|
|
||
| // version 225 | ||
| // insert `cluster_id` into the `mysql.tidb` table. | ||
| version225 = 225 | ||
|
|
||
| // ... | ||
| // [version225, version238] is the version range reserved for patches of 8.5.x | ||
| // [version226, version238] is the version range reserved for patches of 8.5.x | ||
| // ... | ||
|
|
||
| // next version should start with 239 | ||
| ) | ||
|
|
||
| // 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 = version224 | ||
| var currentBootstrapVersion int64 = version225 | ||
|
|
||
| // 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 | ||
|
|
@@ -1433,6 +1439,7 @@ var ( | |
| upgradeToVer222, | ||
| upgradeToVer223, | ||
| upgradeToVer224, | ||
| upgradeToVer225, | ||
| } | ||
| ) | ||
|
|
||
|
|
@@ -3317,6 +3324,30 @@ func upgradeToVer224(s sessiontypes.Session, ver int64) { | |
| doReentrantDDL(s, "ALTER TABLE mysql.tidb_global_task_history ADD COLUMN max_node_count INT DEFAULT 0 AFTER `modify_params`;", infoschema.ErrColumnExists) | ||
| } | ||
|
|
||
| // writeClusterID writes cluster id into mysql.tidb | ||
| func writeClusterID(s sessiontypes.Session) { | ||
| ctx, cancel := context.WithTimeout(context.Background(), time.Duration(internalSQLTimeout)*time.Second) | ||
| defer cancel() | ||
|
|
||
| clusterID := s.GetDomain().(*domain.Domain).GetPDClient().GetClusterID(ctx) | ||
|
|
||
| mustExecute(s, `INSERT HIGH_PRIORITY INTO %n.%n VALUES (%?, %?, "TiDB Cluster ID.") ON DUPLICATE KEY UPDATE VARIABLE_VALUE= %?`, | ||
| mysql.SystemDB, | ||
| mysql.TiDBTable, | ||
| tidbClusterID, | ||
| clusterID, | ||
| clusterID, | ||
| ) | ||
|
Comment on lines
+3351
to
+3364
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. Guard the PD client before fetching
🛠️ Suggested fix // writeClusterID writes cluster id into mysql.tidb
func writeClusterID(s sessiontypes.Session) {
+ pdClient := domain.GetDomain(s).GetPDClient()
+ if pdClient == nil {
+ logutil.BgLogger().Warn("skip writing cluster_id: PD client unavailable during bootstrap/upgrade")
+ return
+ }
+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(internalSQLTimeout)*time.Second)
defer cancel()
- clusterID := s.GetDomain().(*domain.Domain).GetPDClient().GetClusterID(ctx)
+ clusterID := pdClient.GetClusterID(ctx)
mustExecute(s, `INSERT HIGH_PRIORITY INTO %n.%n VALUES (%?, %?, "TiDB Cluster ID.") ON DUPLICATE KEY UPDATE VARIABLE_VALUE= %?`,
mysql.SystemDB,
mysql.TiDBTable,As per coding guidelines, "Keep error handling actionable and contextual; avoid silently swallowing errors in Go code". 🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| func upgradeToVer225(s sessiontypes.Session, ver int64) { | ||
| if ver >= version225 { | ||
| return | ||
| } | ||
|
|
||
| writeClusterID(s) | ||
| } | ||
|
|
||
| // 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) | ||
|
|
@@ -3568,6 +3599,8 @@ func doDMLWorks(s sessiontypes.Session) { | |
|
|
||
| writeDDLTableVersion(s) | ||
|
|
||
| writeClusterID(s) | ||
|
|
||
| ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnBootstrap) | ||
| _, err := s.ExecuteInternal(ctx, "COMMIT") | ||
| if err != nil { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.