Skip to content

Commit 28c2a0d

Browse files
authored
v8.5.5 pd,tidb: support affinity schedule (#22270) (#22315)
1 parent 7c03ee9 commit 28c2a0d

File tree

8 files changed

+215
-8
lines changed

8 files changed

+215
-8
lines changed

TOC.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@
881881
- [`SET ROLE`](/sql-statements/sql-statement-set-role.md)
882882
- [`SET TRANSACTION`](/sql-statements/sql-statement-set-transaction.md)
883883
- [`SET <variable>`](/sql-statements/sql-statement-set-variable.md)
884+
- [`SHOW AFFINITY`](/sql-statements/sql-statement-show-affinity.md)
884885
- [`SHOW ANALYZE STATUS`](/sql-statements/sql-statement-show-analyze-status.md)
885886
- [`SHOW [BACKUPS|RESTORES]`](/sql-statements/sql-statement-show-backups.md)
886887
- [`SHOW BINDINGS`](/sql-statements/sql-statement-show-bindings.md)
@@ -998,6 +999,7 @@
998999
- [Temporary Tables](/temporary-tables.md)
9991000
- [Cached Tables](/cached-tables.md)
10001001
- [FOREIGN KEY Constraints](/foreign-key.md)
1002+
- [Table-Level Data Affinity](/table-affinity.md)
10011003
- Character Set and Collation
10021004
- [Overview](/character-set-and-collation.md)
10031005
- [GBK](/character-set-gbk.md)

information-schema/information-schema-partitions.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ The output is as follows:
4545
| TABLESPACE_NAME | varchar(64) | YES | | NULL | |
4646
| TIDB_PARTITION_ID | bigint(21) | YES | | NULL | |
4747
| TIDB_PLACEMENT_POLICY_NAME | varchar(64) | YES | | NULL | |
48+
| TIDB_AFFINITY | varchar(128) | YES | | NULL | |
4849
+-------------------------------+--------------+------+------+---------+-------+
49-
27 rows in set (0.00 sec)
50+
28 rows in set (0.00 sec)
5051
```
5152

5253
```sql
@@ -85,6 +86,7 @@ SUBPARTITION_ORDINAL_POSITION: NULL
8586
TABLESPACE_NAME: NULL
8687
TIDB_PARTITION_ID: 89
8788
TIDB_PLACEMENT_POLICY_NAME: NULL
89+
TIDB_AFFINITY: NULL
8890
*************************** 2. row ***************************
8991
TABLE_CATALOG: def
9092
TABLE_SCHEMA: test
@@ -113,6 +115,7 @@ SUBPARTITION_ORDINAL_POSITION: NULL
113115
TABLESPACE_NAME: NULL
114116
TIDB_PARTITION_ID: 90
115117
TIDB_PLACEMENT_POLICY_NAME: NULL
118+
TIDB_AFFINITY: NULL
116119
2 rows in set (0.00 sec)
117120
```
118121

information-schema/information-schema-tables.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ DESC tables;
4141
| TABLE_COMMENT | varchar(2048) | YES | | NULL | |
4242
| TIDB_TABLE_ID | bigint(21) | YES | | NULL | |
4343
| TIDB_ROW_ID_SHARDING_INFO | varchar(255) | YES | | NULL | |
44+
| TIDB_PK_TYPE | varchar(64) | YES | | NULL | |
45+
| TIDB_PLACEMENT_POLICY_NAME | varchar(64) | YES | | NULL | |
46+
| TIDB_TABLE_MODE | varchar(16) | YES | | NULL | |
47+
| TIDB_AFFINITY | varchar(128) | YES | | NULL | |
4448
+---------------------------+---------------+------+------+----------+-------+
45-
23 rows in set (0.00 sec)
49+
27 rows in set (0.00 sec)
4650
```
4751

4852
{{< copyable "sql" >}}
@@ -72,10 +76,14 @@ SELECT * FROM tables WHERE table_schema='mysql' AND table_name='user'\G
7276
CHECK_TIME: NULL
7377
TABLE_COLLATION: utf8mb4_bin
7478
CHECKSUM: NULL
75-
CREATE_OPTIONS:
76-
TABLE_COMMENT:
79+
CREATE_OPTIONS:
80+
TABLE_COMMENT:
7781
TIDB_TABLE_ID: 5
7882
TIDB_ROW_ID_SHARDING_INFO: NULL
83+
TIDB_PK_TYPE: CLUSTERED
84+
TIDB_PLACEMENT_POLICY_NAME: NULL
85+
TIDB_TABLE_MODE: Normal
86+
TIDB_AFFINITY: NULL
7987
1 row in set (0.00 sec)
8088
```
8189

@@ -115,12 +123,16 @@ The description of columns in the `TABLES` table is as follows:
115123
* `CREATE_OPTIONS`: Creates options.
116124
* `TABLE_COMMENT`: The comments and notes of the table.
117125

118-
Most of the information in the table is the same as MySQL. Only two columns are newly defined by TiDB:
126+
Most of the information in the table is the same as MySQL. The following columns are newly defined by TiDB:
119127

120128
* `TIDB_TABLE_ID`: to indicate the internal ID of a table. This ID is unique in a TiDB cluster.
121129
* `TIDB_ROW_ID_SHARDING_INFO`: to indicate the sharding type of a table. The possible values are as follows:
122130
- `"NOT_SHARDED"`: the table is not sharded.
123131
- `"NOT_SHARDED(PK_IS_HANDLE)"`: the table that defines an integer Primary Key as its row id is not sharded.
124132
- `"PK_AUTO_RANDOM_BITS={bit_number}"`: the table that defines an integer Primary Key as its row id is sharded because the Primary Key is assigned with `AUTO_RANDOM` attribute.
125133
- `"SHARD_BITS={bit_number}"`: the table is sharded using `SHARD_ROW_ID_BITS={bit_number}`.
126-
- NULL: the table is a system table or view, and thus cannot be sharded.
134+
- `NULL`: the table is a system table or view, and thus cannot be sharded.
135+
* `TIDB_PK_TYPE`: the primary key type of the table. Possible values include `CLUSTERED` (clustered primary key) and `NONCLUSTERED` (non-clustered primary key).
136+
* `TIDB_PLACEMENT_POLICY_NAME`: the name of the placement policy applied to the table.
137+
* `TIDB_TABLE_MODE`: the mode of the table, for example, `Normal`, `Import`, or `Restore`.
138+
* `TIDB_AFFINITY`: the affinity level of the table. It is `table` for non-partitioned tables, `partition` for partitioned tables, and `NULL` when affinity is not enabled.

pd-configuration-file.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,13 @@ Configuration items related to scheduling
292292
+ Specifies the upper limit of the `Region Merge` key. When the Region key is greater than the specified value, the PD does not merge the Region with its adjacent Regions.
293293
+ Default value: `540000`. Before v8.4.0, the default value is `200000`. Starting from v8.4.0, the default value is `540000`.
294294
295+
### `max-affinity-merge-region-size` <span class="version-mark">New in v8.5.5</span>
296+
297+
+ Controls the threshold for automatically merging small adjacent Regions that belong to the same [affinity](/table-affinity.md) group. When a Region belongs to an affinity group and its size is smaller than this threshold, PD attempts to merge this Region with other small adjacent Regions in the same affinity group to reduce the number of Regions and maintain the affinity effect.
298+
+ Setting it to `0` disables the automatic merging of small adjacent Regions within an affinity group.
299+
+ Default value: `256`
300+
+ Unit: MiB
301+
295302
### `patrol-region-interval`
296303
297304
+ Controls the running frequency at which the checker inspects the health state of a Region. The smaller this value is, the faster the checker runs. Normally, you do not need to adjust this configuration.
@@ -372,6 +379,11 @@ Configuration items related to scheduling
372379
+ The number of the `Region Merge` scheduling tasks performed at the same time. Set this parameter to `0` to disable `Region Merge`.
373380
+ Default value: `8`
374381
382+
### `affinity-schedule-limit` <span class="version-mark">New in v8.5.5</span>
383+
384+
+ Controls the number of [affinity](/table-affinity.md) scheduling tasks that can be performed concurrently. Setting it to `0` disables affinity scheduling.
385+
+ Default value: `0`
386+
375387
### `high-space-ratio`
376388
377389
+ The threshold ratio below which the capacity of the store is sufficient. If the space occupancy ratio of the store is smaller than this threshold value, PD ignores the remaining space of the store when performing scheduling, and balances load mainly based on the Region size. This configuration takes effect only when `region-score-formula-version` is set to `v1`.

sql-statements/sql-statement-alter-table.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ AlterTableSpec ::=
5454
| TTLEnable EqOpt ( 'ON' | 'OFF' )
5555
| TTLJobInterval EqOpt stringLit
5656
)
57+
| 'AFFINITY' EqOpt stringLit
5758
| PlacementPolicyOption
5859
5960
PlacementPolicyOption ::=
@@ -181,6 +182,8 @@ The following major restrictions apply to `ALTER TABLE` in TiDB:
181182

182183
- Changes of some data types (for example, some TIME, Bit, Set, Enum, and JSON types) are not supported due to the compatibility issues of the `CAST` function's behavior between TiDB and MySQL.
183184

185+
- The `AFFINITY` option is a TiDB extension syntax. After `AFFINITY` is enabled for a table, you cannot modify the partition scheme of that table, such as adding, dropping, reorganizing, or swapping partitions. To modify the partition scheme, you must first remove `AFFINITY`.
186+
184187
- Spatial data types are not supported.
185188

186189
- `ALTER TABLE t CACHE | NOCACHE` is a TiDB extension to MySQL syntax. For details, see [Cached Tables](/cached-tables.md).

sql-statements/sql-statement-create-table.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ TableOption ::=
117117
| 'UNION' EqOpt '(' TableNameListOpt ')'
118118
| 'ENCRYPTION' EqOpt EncryptionOpt
119119
| 'TTL' EqOpt TimeColumnName '+' 'INTERVAL' Expression TimeUnit (TTLEnable EqOpt ( 'ON' | 'OFF' ))? (TTLJobInterval EqOpt stringLit)?
120+
| 'AFFINITY' EqOpt StringName
120121
| PlacementPolicyOption
121122
122123
OnCommitOpt ::=
@@ -168,21 +169,25 @@ The following *table_options* are supported. Other options such as `AVG_ROW_LENG
168169
|`AUTO_ID_CACHE`| To set the auto ID cache size in a TiDB instance. By default, TiDB automatically changes this size according to allocation speed of auto ID |`AUTO_ID_CACHE` = 200 |
169170
|`AUTO_RANDOM_BASE`| To set the initial incremental part value of auto_random. This option can be considered as a part of the internal interface. Users can ignore this parameter |`AUTO_RANDOM_BASE` = 0|
170171
| `CHARACTER SET` | To specify the [character set](/character-set-and-collation.md) for the table | `CHARACTER SET` = 'utf8mb4' |
172+
| `COLLATE` | To specify the character set collation for the table | `COLLATE` = 'utf8mb4_bin' |
171173
| `COMMENT` | The comment information | `COMMENT` = 'comment info' |
174+
| `AFFINITY` | To enable affinity scheduling for a table or partition. It can be set to `'table'` for non-partitioned tables and `'partition'` for partitioned tables. Setting it to `'none'` or leaving it empty disables affinity scheduling. | `AFFINITY` = 'table' |
172175

173176
<CustomContent platform="tidb">
174177

175178
> **Note:**
176179
>
177-
> The `split-table` configuration option is enabled by default. When it is enabled, a separate Region is created for each newly created table. For details, see [TiDB configuration file](/tidb-configuration-file.md).
180+
> - The `split-table` configuration option is enabled by default. When it is enabled, a separate Region is created for each newly created table. For details, see [TiDB configuration file](/tidb-configuration-file.md).
181+
> - Before using `AFFINITY`, note that modifying the partitioning scheme (such as adding, dropping, reorganizing, or swapping partitions) of a table with affinity enabled is not supported, and configuring `AFFINITY` on temporary tables or views is not supported.
178182
179183
</CustomContent>
180184

181185
<CustomContent platform="tidb-cloud">
182186

183187
> **Note:**
184188
>
185-
> TiDB creates a separate Region for each newly created table.
189+
> - TiDB creates a separate Region for each newly created table.
190+
> - Before using `AFFINITY`, note that modifying the partitioning scheme (such as adding, dropping, reorganizing, or swapping partitions) of a table with affinity enabled is not supported, and configuring `AFFINITY` on temporary tables or views is not supported.
186191
187192
</CustomContent>
188193

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
title: SHOW AFFINITY
3+
summary: An overview of the usage of SHOW AFFINITY for the TiDB database.
4+
---
5+
6+
# SHOW AFFINITY <span class="version-mark">New in v8.5.5</span>
7+
8+
The `SHOW AFFINITY` statement shows [affinity](/table-affinity.md) scheduling information for tables configured with the `AFFINITY` option, as well as the target replica distribution currently recorded by PD.
9+
10+
## Synopsis
11+
12+
```ebnf+diagram
13+
ShowAffinityStmt ::=
14+
"SHOW" "AFFINITY" ShowLikeOrWhereOpt
15+
```
16+
17+
`SHOW AFFINITY` supports filtering table names using `LIKE` or `WHERE` clauses.
18+
19+
## Examples
20+
21+
The following examples create two tables with affinity scheduling enabled and show how to view their scheduling information:
22+
23+
```sql
24+
CREATE TABLE t1 (a INT) AFFINITY = 'table';
25+
CREATE TABLE tp1 (a INT) AFFINITY = 'partition' PARTITION BY HASH(a) PARTITIONS 2;
26+
27+
SHOW AFFINITY;
28+
```
29+
30+
The example output is as follows:
31+
32+
```sql
33+
+---------+------------+----------------+-----------------+------------------+----------+--------------+----------------------+
34+
| Db_name | Table_name | Partition_name | Leader_store_id | Voter_store_ids | Status | Region_count | Affinity_region_count|
35+
+---------+------------+----------------+-----------------+------------------+----------+--------------+----------------------+
36+
| test | t1 | NULL | 1 | 1,2,3 | Stable | 8 | 8 |
37+
| test | tp1 | p0 | 4 | 4,5,6 | Preparing| 4 | 2 |
38+
| test | tp1 | p1 | 4 | 4,5,6 | Preparing| 3 | 2 |
39+
+---------+------------+----------------+-----------------+------------------+----------+--------------+----------------------+
40+
```
41+
42+
The meaning of each column is as follows:
43+
44+
- `Leader_store_id`, `Voter_store_ids`: the IDs of TiKV stores recorded by PD, indicating which stores host the target Leader and Voter replicas for the table or partitions. If the target replica locations for the affinity group are not determined, or if [`schedule.affinity-schedule-limit`](/pd-configuration-file.md#affinity-schedule-limit-new-in-v855) is set to `0`, the value is displayed as `NULL`.
45+
- `Status`: indicates the current status of affinity scheduling. Possible values are:
46+
- `Pending`: PD has not started affinity scheduling for the table or partition, such as when Leaders or Voters are not yet determined.
47+
- `Preparing`: PD is scheduling Regions to meet affinity requirements.
48+
- `Stable`: all Regions have reached the target distribution.
49+
- `Region_count`: the current number of Regions in the affinity group.
50+
- `Affinity_region_count`: the number of Regions that currently meet the affinity replica distribution requirements.
51+
- When `Affinity_region_count` is less than `Region_count`, it indicates that some Regions have not yet completed replica scheduling based on affinity.
52+
- When `Affinity_region_count` equals `Region_count`, it indicates that replica scheduling based on affinity is complete, meaning the distribution of all related Regions meets the affinity requirements. However, this does not indicate that related Region merge operations are complete.
53+
54+
## MySQL compatibility
55+
56+
This statement is a TiDB extension to MySQL syntax.
57+
58+
## See also
59+
60+
- [`CREATE TABLE`](/sql-statements/sql-statement-create-table.md)
61+
- [`ALTER TABLE`](/sql-statements/sql-statement-alter-table.md)

0 commit comments

Comments
 (0)