-
Notifications
You must be signed in to change notification settings - Fork 1.2k
pd,tidb: support affinity schedule #21207
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
Open
lhy1024
wants to merge
28
commits into
pingcap:master
Choose a base branch
from
lhy1024:affinity
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
44f8f26
pd,tidb:: support affinity schedule
lhy1024 a26c81f
address comments
lhy1024 aedfcf3
address comments
lhy1024 da72e6b
revise descriptions in table-affinity.md
qiancai 8ad61fa
Update sql-statements/sql-statement-show-affinity.md
lhy1024 aaa4115
Update pd-configuration-file.md
lhy1024 716c904
Update pd-configuration-file.md
lhy1024 fd96fef
Update sql-statements/sql-statement-show-affinity.md
lhy1024 ab2f15d
Update sql-statements/sql-statement-show-affinity.md
lhy1024 448f0d5
Update table-affinity.md
lhy1024 a89d382
Update information-schema/information-schema-partitions.md
lhy1024 39d1ab3
Update information-schema/information-schema-tables.md
lhy1024 00bf417
Update information-schema/information-schema-partitions.md
lhy1024 4442fcd
Update information-schema/information-schema-tables.md
lhy1024 e92537d
Update information-schema/information-schema-tables.md
lhy1024 6102ec9
Update pd-configuration-file.md
lhy1024 0ef11f2
Update sql-statements/sql-statement-show-affinity.md
lhy1024 e59108f
Update pd-configuration-file.md
lhy1024 3dc15f4
apply suggestions from lhy1024
qiancai c07d0f8
Update table-affinity.md
lhy1024 d0621e5
Update table-affinity.md
lhy1024 f2005df
address comments
lhy1024 c912fcd
Apply suggestions from code review
qiancai d564660
Update sql-statements/sql-statement-show-affinity.md
qiancai c8a6adb
Apply suggestions from code review
qiancai cae10c9
Apply suggestions from code review
qiancai 8261428
update ADMIN SPLIT
qiancai 02f2000
format udpates
qiancai 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| --- | ||
| title: SHOW AFFINITY | ||
| summary: 介绍 TiDB 数据库中 SHOW AFFINITY 的使用概况。 | ||
| --- | ||
|
|
||
| # SHOW AFFINITY <span class="version-mark">从 v8.5.5 和 v9.0.0 版本开始引入</span> | ||
|
|
||
| `SHOW AFFINITY` 语句用于查看配置了 `AFFINITY` 选项的表的[亲和性](/table-affinity.md)调度信息,以及 PD 当前记录的目标副本分布。 | ||
|
|
||
| ## 语法图 | ||
|
|
||
| ```ebnf+diagram | ||
| ShowAffinityStmt ::= | ||
| "SHOW" "AFFINITY" ShowLikeOrWhereOpt | ||
| ``` | ||
|
|
||
| `SHOW AFFINITY` 支持使用 `LIKE` 或 `WHERE` 子句过滤表名。 | ||
|
|
||
| ## 示例 | ||
|
|
||
| 以下示例创建两个启用了亲和性调度的表,并查看其调度信息: | ||
|
|
||
| ```sql | ||
| CREATE TABLE t1 (a INT) AFFINITY = 'table'; | ||
| CREATE TABLE tp1 (a INT) AFFINITY = 'partition' PARTITION BY HASH(a) PARTITIONS 2; | ||
|
|
||
| SHOW AFFINITY; | ||
| ``` | ||
|
|
||
| 输出结果示例如下: | ||
|
|
||
| ```sql | ||
| +---------+------------+----------------+-----------------+------------------+----------+--------------+----------------------+ | ||
| | Db_name | Table_name | Partition_name | Leader_store_id | Voter_store_ids | Status | Region_count | Affinity_region_count| | ||
| +---------+------------+----------------+-----------------+------------------+----------+--------------+----------------------+ | ||
| | test | t1 | NULL | 1 | 1,2,3 | Stable | 8 | 8 | | ||
| | test | tp1 | p0 | 4 | 4,5,6 | Preparing| 4 | 2 | | ||
| | test | tp1 | p1 | 4 | 4,5,6 | Preparing| 3 | 2 | | ||
| +---------+------------+----------------+-----------------+------------------+----------+--------------+----------------------+ | ||
| ``` | ||
|
|
||
| 各列含义如下: | ||
|
|
||
| - `Leader_store_id`、`Voter_store_ids`:PD 为该表或分区记录的目标 Leader 副本和 Voter 副本所在的 TiKV Store ID。如果亲和性分组尚未确定目标副本位置或 [`schedule.affinity-schedule-limit`](/pd-configuration-file.md#affinity-schedule-limit-从-v855-和-v900-版本开始引入) 为 `0`,则显示为 `NULL`。 | ||
| - `Status`:表示当前亲和性调度的状态。可能的取值如下: | ||
| - `Pending`:PD 尚未对该表或分区进行亲和性调度(比如未确定 Leader 或 Voter 时)。 | ||
| - `Preparing`:PD 正在调度 Region 以满足亲和性要求。 | ||
| - `Stable`:所有 Region 已达到目标分布。 | ||
| - `Region_count`:当前在该亲和性组中的 Region 数量。 | ||
| - `Affinity_region_count`:当前已满足亲和性副本分布要求的 Region 数量。 | ||
| - 当 `Affinity_region_count` 小于 `Region_count` 时,表示仍有部分 Region 尚未完成基于亲和性的副本调度。 | ||
| - 当 `Affinity_region_count` 等于 `Region_count` 时,表示基于亲和性的 Region 副本迁移调度已完成,也就意味着所有 Region 的分布已经满足亲和性要求,但并不代表相关 Region 的合并操作已完成。 | ||
|
|
||
| ## MySQL 兼容性 | ||
|
|
||
| 该语句是 TiDB 对 MySQL 语法的扩展。 | ||
|
|
||
| ## 另请参阅 | ||
|
|
||
| - [`CREATE TABLE`](/sql-statements/sql-statement-create-table.md) | ||
| - [`ALTER TABLE`](/sql-statements/sql-statement-alter-table.md) | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| --- | ||
| title: 表级数据亲和性 | ||
| summary: 通过为表或分区配置亲和性约束,控制 Region 副本的分布并查看调度状态。 | ||
| --- | ||
|
|
||
| # 表级数据亲和性 <span class="version-mark">从 v8.5.5 和 v9.0.0 开始引入</span> | ||
|
|
||
| > **警告:** | ||
| > | ||
| > 该功能为实验特性,不建议在生产环境中使用。该功能可能会在未事先通知的情况下发生变化或删除。如果发现 bug,请在 GitHub 上提 [issue](https://github.com/pingcap/tidb/issues) 反馈。 | ||
|
|
||
| 表级数据亲和性是 PD 在表级别上的数据分布调度机制,用于控制同一个表或分区中 Region 的 Leader 和 Voter 副本在 TiKV 集群中的分布。 | ||
|
|
||
| 开启 PD 数据亲和性调度,并将表的 `AFFINITY` 选项设置为 `table` 或 `partition` 后,PD 会将同一张表或同一个分区的 Region 归入同一个亲和性分组,并在调度过程中优先将这些 Region 的 Leader 和 Voter 副本放置到相同的少数 TiKV 节点上,从而减少查询过程中跨节点访问带来的网络延迟,提升查询性能。 | ||
|
|
||
lhy1024 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ## 使用限制 | ||
|
|
||
lhy1024 marked this conversation as resolved.
Show resolved
Hide resolved
qiancai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 使用表级数据亲和性前,请注意以下限制: | ||
|
|
||
| - 在 [PD 微服务模式](/pd-microservices.md)下,该功能不会生效。 | ||
| - [临时表](/temporary-tables.md)和[视图](/views.md)不支持配置数据亲和性。 | ||
| - 为[分区表](/partitioned-table.md)配置数据亲和性后,**不支持修改该表的分区方案**,包括新增、删除、重组或交换分区。如需调整分区配置,请先移除该表的亲和性设置。 | ||
| - **数据量较大时需提前评估磁盘容量**:开启数据亲和性后,PD 会优先将表或分区的 Region 调度到相同的少数 TiKV 节点上。对于数据量较大的表或分区,可能导致这些节点的磁盘使用率显著升高。建议提前评估磁盘容量并做好监控。 | ||
| - 数据亲和性调度仅影响 Leader 和 Voter 副本的分布。如果表有 Learner 副本(如 TiFlash),Learner 副本的分布不受亲和性配置影响。 | ||
|
|
||
| ## 前提条件 | ||
|
|
||
| PD 亲和性调度特性默认关闭。在设置表或分区的亲和性前,请开启并配置该特性。 | ||
|
|
||
| 1. 将 PD 配置项 [`schedule.affinity-schedule-limit`](/pd-configuration-file.md#affinity-schedule-limit-从-v855-和-v900-版本开始引入) 设置为大于 `0` 的值,以开启 PD 的亲和性调度。 | ||
|
|
||
| 例如,执行以下命令将该配置项设置为 `4`,表示允许 PD 最多同时执行 4 个亲和性调度任务: | ||
|
|
||
| ```bash | ||
| pd-ctl config set schedule.affinity-schedule-limit 4 | ||
| ``` | ||
|
|
||
| 2. (可选)根据需要设置 PD 配置项 [`schedule.max-affinity-merge-region-size`](/pd-configuration-file.md#max-affinity-merge-region-size-从-v855-和-v900-版本开始引入)(默认值为 `256`,单位为 MiB),用于控制属于同一亲和性分组中相邻的小 Region 自动合并的阈值。设置为 `0` 表示关闭亲和性分组中相邻的小 Region 的自动合并。 | ||
|
|
||
| ## 使用方法 | ||
|
|
||
| 本节介绍如何配置表或分区的亲和性,以及如何查看亲和性调度状态。 | ||
|
|
||
| ### 配置表或分区的亲和性 | ||
|
|
||
| 你可以通过 `CREATE TABLE` 或 `ALTER TABLE` 语句中的 `AFFINITY` 选项配置表或分区的亲和性。 | ||
|
|
||
| | 亲和性等级 | 适用范围 | 效果 | | ||
| |---|---|---| | ||
| | `AFFINITY='table'` | 非分区表 | 开启该表的亲和性,PD 会为此表的所有 Region 创建一个亲和性分组。 | | ||
| | `AFFINITY='partition'` | 分区表 | 开启该表中每个分区的亲和性,PD 会为此表的**每个分区**对应的 Region 分别创建独立的亲和性分组。例如,当表包含 4 个分区时,PD 将为该表创建 4 个相互独立的亲和性分组。 | | ||
| | `AFFINITY=''` 或 `AFFINITY='none'` | 已设置 `AFFINITY='table'` 或 `AFFINITY='partition'` 的表 | 关闭该表或分区的亲和性。设置后,PD 会删除对应表或分区的亲和性分组,表或分区的 Region 将不再受到亲和性调度约束。TiKV 上 Region 的自动分裂将在最长 10 分钟内恢复为默认状态。 | | ||
Oreoxmt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| **示例**: | ||
|
|
||
| 创建非分区表时开启该表的亲和性: | ||
|
|
||
| ```sql | ||
| CREATE TABLE t1 (a INT) AFFINITY = 'table'; | ||
| ``` | ||
|
|
||
| 创建分区表时开启该表中每个分区的亲和性: | ||
|
|
||
| ```sql | ||
| CREATE TABLE tp1 (a INT) | ||
| AFFINITY = 'partition' | ||
| PARTITION BY HASH(a) PARTITIONS 4; | ||
| ``` | ||
|
|
||
| 为现有非分区表开启亲和性: | ||
|
|
||
| ```sql | ||
| CREATE TABLE t2 (a INT); | ||
| ALTER TABLE t2 AFFINITY = 'table'; | ||
| ``` | ||
|
|
||
| 关闭表的亲和性: | ||
|
|
||
| ```sql | ||
| ALTER TABLE t1 AFFINITY = ''; | ||
| ``` | ||
|
|
||
| ### 查看亲和性 | ||
|
|
||
| 可以通过以下方式查看表或分区的亲和性信息: | ||
|
|
||
| - 执行 [`SHOW AFFINITY`](/sql-statements/sql-statement-show-affinity.md) 语句,在 `Status` 列查看已开启亲和性的表或分区及其调度状态。`Status` 列的值含义如下: | ||
|
|
||
| - `Pending`:PD 尚未对该表或分区进行亲和性调度,比如未确定 Leader 或 Voter 时。 | ||
| - `Preparing`:PD 正在调度 Region 以满足亲和性要求。 | ||
| - `Stable`:所有 Region 已达到目标分布。 | ||
|
|
||
| - 查询 [`INFORMATION_SCHEMA.TABLES`](/information-schema/information-schema-tables.md) 表的 `TIDB_AFFINITY` 列查看表的亲和性等级。 | ||
| - 查询 [`INFORMATION_SCHEMA.PARTITIONS`](/information-schema/information-schema-partitions.md) 表的 `TIDB_AFFINITY` 列查看分区的亲和性等级。 | ||
|
|
||
| ## 注意事项 | ||
|
|
||
| - **Region 的自动分裂**:当 Region 属于某个亲和性分组且亲和性生效时,Region 默认不会自动分裂,以避免产生过多 Region 影响亲和性效果。只有当 Region 大小超过 [`schedule.max-affinity-merge-region-size`](/pd-configuration-file.md#max-affinity-merge-region-size-从-v855-和-v900-版本开始引入) 值的四倍时,才会触发自动分裂。需要注意的是,非 TiKV 或 PD 自动触发的 Region 分裂(例如手动执行的 [`SPLIT TABLE`](/sql-statements/sql-statement-split-region.md))不受此限制。 | ||
|
|
||
| - **降级与过期机制**:如果亲和性分组中目标 Leader 或 Voter 所在的 TiKV 节点处于不可用状态(例如节点宕机或磁盘空间不足)、Leader 被驱逐,或与现有放置规则发生冲突时,PD 会将该亲和性分组标记为降级状态。在降级期间,对应表或分区的亲和性调度将暂停。 | ||
|
|
||
| - 若相关节点在 10 分钟内恢复正常,PD 会继续按照原有亲和性设置进行调度。 | ||
| - 若超过 10 分钟仍未恢复,该亲和性分组将被标记为过期。此时 PD 会先恢复常规调度行为([`SHOW AFFINITY`](/sql-statements/sql-statement-show-affinity.md) 的状态会回到 `Pending`),然后自动更新亲和性分组中的 Leader 和 Voter,以重新启用亲和性调度。 | ||
|
|
||
| ## 相关语句与配置 | ||
|
|
||
| - [`CREATE TABLE`](/sql-statements/sql-statement-create-table.md) 和 [`ALTER TABLE`](/sql-statements/sql-statement-alter-table.md) 的 `AFFINITY` 选项 | ||
| - [`SHOW AFFINITY`](/sql-statements/sql-statement-show-affinity.md) | ||
| - PD 配置项:[`schedule.affinity-schedule-limit`](/pd-configuration-file.md#affinity-schedule-limit-从-v855-和-v900-版本开始引入) 和 [`schedule.max-affinity-merge-region-size`](/pd-configuration-file.md#max-affinity-merge-region-size-从-v855-和-v900-版本开始引入) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.