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

remove direct placement #7400

Merged
merged 13 commits into from
Mar 9, 2022
49 changes: 39 additions & 10 deletions placement-rules-in-sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,52 @@ The detailed user scenarios are as follows:
- Schedule the leaders of hotspot data to high-performance TiKV instances
- Separate cold data to lower-cost storage mediums to improve cost efficiency

## Specify placement options
## Specify placement rules

To use Placement Rules in SQL, you need to specify one or more placement options in a SQL statement. To specify the Placement options, you can either use _direct placement_ or use a _placement policy_.
To specify placement rules, first create a placement policy:

In the following example, both tables `t1` and `t2` have the same rules. `t1` is specified rules using a direct placement while `t2` is specified rules using a placement policy.
```sql
CREATE PLACEMENT POLICY myplacementpolicy PRIMARY_REGION="us-east-1" REGIONS="us-east-1,us-west-1";
```

Then attach the policy to a table or partition using either `CREATE TABLE` or `ALTER TABLE`:

```sql
CREATE TABLE t1 (a INT) PRIMARY_REGION="us-east-1" REGIONS="us-east-1,us-west-1";
CREATE PLACEMENT POLICY eastandwest PRIMARY_REGION="us-east-1" REGIONS="us-east-1,us-west-1";
CREATE TABLE t2 (a INT) PLACEMENT POLICY=eastandwest;
CREATE TABLE t1 (a INT) PLACEMENT POLICY myplacementpolicy;
CREATE TABLE t2 (a INT);
ALTER TABLE t2 PLACEMENT POLICY myplacementpolicy;
```

It is recommended to use placement policies for simpler rule management. When you change a placement policy (via [`ALTER PLACEMENT POLICY`](/sql-statements/sql-statement-alter-placement-policy.md)), the change automatically propagates to all database objects.
A placement policy is not associated with any database schema and has the global scope. Therefore, assigning a placement policy does not require any additional privileges over the `CREATE TABLE` privilege.

If you use direct placement options, you have to alter rules for each object (for example, tables and partitions).
## View current placement rules

If a table has placement rules attached, you can view the placement rules in the output of `SHOW CREATE TABLE`. To view the definition of the policy available, execute `SHOW CREATE PLACEMENT POLICY`:

```sql
tidb> SHOW CREATE TABLE t1\G
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`a` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![placement] PLACEMENT POLICY=`myplacementpolicy` */
1 row in set (0.00 sec)

tidb> SHOW CREATE PLACEMENT POLICY myplacementpolicy\G
*************************** 1. row ***************************
Policy: myplacementpolicy
Create Policy: CREATE PLACEMENT POLICY myplacementpolicy PRIMARY_REGION="us-east-1" REGIONS="us-east-1,us-west-1"
1 row in set (0.00 sec)
```

The `information_schema.tables` and `information_schema.partitions` tables also include a column for `tidb_placement_policy_name`, which shows all objects with placement rules attached:

```sql
SELECT * FROM information_schema.tables WHERE tidb_placement_policy_name IS NOT NULL;
SELECT * FROM information_schema.partitions WHERE tidb_placement_policy_name IS NOT NULL;
```

`PLACEMENT POLICY` is not associated with any database schema and has the global scope. Therefore, assigning a placement policy does not require any additional privileges over the `CREATE TABLE` privilege.
Rules that are attached to objects are applied _asynchronously_. To view the current scheduling progress of placement, use [`SHOW PLACEMENT`](/sql-statements/sql-statement-show-placement.md).

## Option reference

Expand Down Expand Up @@ -173,7 +202,7 @@ The following known limitations exist in the experimental release of Placement R

* Dumpling does not support dumping placement policies. See [issue #29371](https://github.com/pingcap/tidb/issues/29371).
* TiDB tools, including Backup & Restore (BR), TiCDC, TiDB Lightning, and TiDB Data Migration (DM), do not yet support placement rules.
* Temporary tables do not support placement options (either via direct placement or placement policies).
* Temporary tables do not support placement options.
* Syntactic sugar rules are permitted for setting `PRIMARY_REGION` and `REGIONS`. In the future, we plan to add varieties for `PRIMARY_RACK`, `PRIMARY_ZONE`, and `PRIMARY_HOST`. See [issue #18030](https://github.com/pingcap/tidb/issues/18030).
* TiFlash learners are not configurable through Placement Rules syntax.
* Placement rules only ensure that data at rest resides on the correct TiKV store. The rules do not guarantee that data in transit (via either user queries or internal operations) only occurs in a specific region.
15 changes: 11 additions & 4 deletions sql-statements/sql-statement-alter-placement-policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ summary: The usage of ALTER PLACEMENT POLICY in TiDB.

`ALTER PLACEMENT POLICY` is used to modify existing placement policies that have previously been created. All the tables and partitions which use the placement policy will automatically be updated.

`ALTER PLACEMENT POLICY` _replaces_ the previous policy with the new definition. It does not _merge_ the old policy with the new one. In the following example, `FOLLOWERS=4` is lost when the `ALTER PLACEMENT POLICY` is executed:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`ALTER PLACEMENT POLICY` _replaces_ the previous policy with the new definition. It does not _merge_ the old policy with the new one. In the following example, `FOLLOWERS=4` is lost when the `ALTER PLACEMENT POLICY` is executed:
`ALTER PLACEMENT POLICY` _replaces_ the previous policy with the new definition. It does not _merge_ the old policy with the new one. In the following example, `FOLLOWERS=4` is overwritten by the `ALTER PLACEMENT POLICY` statement:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"lost" is more clear here, because it was not specified (overwritten) in the ALTER statement.


```sql
CREATE PLACEMENT POLICY p1 FOLLOWERS=4;
ALTER PLACEMENT POLICY p1 PRIMARY_REGION="us-east-1" REGIONS="us-east-1,us-west-1";
```

## Synopsis

```ebnf+diagram
Expand All @@ -23,11 +30,11 @@ PolicyName ::=
Identifier

PlacementOptionList ::=
DirectPlacementOption
| PlacementOptionList DirectPlacementOption
| PlacementOptionList ',' DirectPlacementOption
PlacementOption
| PlacementOptionList PlacementOption
| PlacementOptionList ',' PlacementOption

DirectPlacementOption ::=
PlacementOption ::=
"PRIMARY_REGION" EqOpt stringLit
| "REGIONS" EqOpt stringLit
| "FOLLOWERS" EqOpt LengthNum
Expand Down
8 changes: 4 additions & 4 deletions sql-statements/sql-statement-create-placement-policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ PolicyName ::=
Identifier

PlacementOptionList ::=
DirectPlacementOption
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do this:

PlacementOptionList ::=
     PlacementSugarSyntaxOptionList
   | PlacementNoSugarSyntaxOptionList

PlacementSugarSyntaxOptionList ::= "PRIMARY_REGION" EqOpt stringLit "REGIONS" EqOpt stringLit ...

User can easily learn how to write a right sql

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

| PlacementOptionList DirectPlacementOption
| PlacementOptionList ',' DirectPlacementOption
PlacementOption
| PlacementOptionList PlacementOption
| PlacementOptionList ',' PlacementOption

DirectPlacementOption ::=
PlacementOption ::=
"PRIMARY_REGION" EqOpt stringLit
| "REGIONS" EqOpt stringLit
| "FOLLOWERS" EqOpt LengthNum
Expand Down
29 changes: 8 additions & 21 deletions sql-statements/sql-statement-show-placement-for.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ summary: The usage of SHOW PLACEMENT FOR in TiDB.
>
> If you understand the risks, you can enable this experiment feature by executing `SET GLOBAL tidb_enable_alter_placement = 1;`.

`SHOW PLACEMENT FOR` summarizes all placement options from direct placement and placement policies, and presents them in the canonical form for a specific table, database schema, or partition.
`SHOW PLACEMENT FOR` summarizes all placement options, and presents them in the canonical form for a specific table, database schema, or partition.
Copy link
Contributor

@TomShawn TomShawn Mar 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

placement options, policies, or rules? The [docs-cn PR] uses 'placement policy' but translated as 'placement rules'. I am a bit confused. See https://github.com/pingcap/docs-cn/pull/8230/files#diff-439cd260163b3aaafae1cb9ac228dc2343f5ee9c23a9211fdb2c6168ea5c08beR12.
@xhebox @morgo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • A placement OPTION is something like "FOLLOWERS=4".
  • A placement POLICY is a way of grouping options together into a named object which can be assigned to tables.
  • Placement RULES previously included both direct placement options and placement policies, but we've since removed direct placement so this term has lost much of its meaning. So it is now usually possible to use RULES and POLICY interchangably, but I think we should keep the terms for now because it is similar to using the terms "RBAC" and "permissions" interchangebly. "Policy" is like the RBAC of Placement.


The statement returns a result set in which the `Scheduling_State` field indicates the current progress that the Placement Driver (PD) has made in scheduling the placement:

* `PENDING`: The PD has not yet started scheduling the placement. This might indicate that that the placement rules are semantically correct, but can not currently be satisfied by the cluster. For example, if `FOLLOWERS=4` but there are only 3 TiKV stores which are candidates for followers.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* `PENDING`: The PD has not yet started scheduling the placement. This might indicate that that the placement rules are semantically correct, but can not currently be satisfied by the cluster. For example, if `FOLLOWERS=4` but there are only 3 TiKV stores which are candidates for followers.
* `PENDING`: The PD has not yet started scheduling the placement. This might indicate that the placement rules are semantically correct, but cannot currently be satisfied by the cluster. For example, if `FOLLOWERS=4` but there are only 3 TiKV stores that are candidates for followers.

* `INPROGRESS`: The PD is currently scheduling the placement.
* `SCHEDULED`: The PD has successfully scheduled the placement.

## Synopsis

Expand All @@ -34,14 +40,11 @@ CREATE PLACEMENT POLICY p1 PRIMARY_REGION="us-east-1" REGIONS="us-east-1,us-west
use test;
ALTER DATABASE test PLACEMENT POLICY=p1;
CREATE TABLE t1 (a INT);
CREATE TABLE t2 (a INT) PRIMARY_REGION="us-east-1" REGIONS="us-east-1,us-west-1" FOLLOWERS=4;
SHOW PLACEMENT FOR DATABASE test;
SHOW PLACEMENT FOR TABLE t1;
SHOW CREATE TABLE t1\G
SHOW PLACEMENT FOR TABLE t2;
CREATE TABLE t3 (a INT) PARTITION BY RANGE (a) (PARTITION p1 VALUES LESS THAN (10), PARTITION p2 VALUES LESS THAN (20) FOLLOWERS=4);
CREATE TABLE t3 (a INT) PARTITION BY RANGE (a) (PARTITION p1 VALUES LESS THAN (10), PARTITION p2 VALUES LESS THAN (20));
SHOW PLACEMENT FOR TABLE t3 PARTITION p1;
SHOW PLACEMENT FOR TABLE t3 PARTITION p2;
```

```
Expand Down Expand Up @@ -74,28 +77,12 @@ Create Table: CREATE TABLE `t1` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![placement] PLACEMENT POLICY=`p1` */
1 row in set (0.00 sec)

+---------------+----------------------------------------------------------------------+------------------+
| Target | Placement | Scheduling_State |
+---------------+----------------------------------------------------------------------+------------------+
| TABLE test.t2 | PRIMARY_REGION="us-east-1" REGIONS="us-east-1,us-west-1" FOLLOWERS=4 | INPROGRESS |
+---------------+----------------------------------------------------------------------+------------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.14 sec)

+----------------------------+-----------------------------------------------------------------------+------------------+
| Target | Placement | Scheduling_State |
+----------------------------+-----------------------------------------------------------------------+------------------+
| TABLE test.t3 PARTITION p1 | PRIMARY_REGION="us-east-1" REGIONS="us-east-1,,us-west-1" FOLLOWERS=4 | INPROGRESS |
+----------------------------+-----------------------------------------------------------------------+------------------+
1 row in set (0.00 sec)

+----------------------------+-------------+------------------+
| Target | Placement | Scheduling_State |
+----------------------------+-------------+------------------+
| TABLE test.t3 PARTITION p2 | FOLLOWERS=4 | INPROGRESS |
+----------------------------+-------------+------------------+
1 row in set (0.00 sec)
```

## MySQL compatibility
Expand Down
12 changes: 7 additions & 5 deletions sql-statements/sql-statement-show-placement.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ summary: The usage of SHOW PLACEMENT in TiDB.
>
> If you understand the risks, you can enable this experiment feature by executing `SET GLOBAL tidb_enable_alter_placement = 1;`.

`SHOW PLACEMENT` summarizes all placement options from direct placement and placement policies, and presents them in canonical form.
`SHOW PLACEMENT` summarizes all placement options from placement policies, and presents them in canonical form.

The statement returns a result set in which the `Scheduling_State` field indicates the current progress that the Placement Driver (PD) has made in scheduling the placement:

* `PENDING`: The PD has not yet started scheduling the placement. This might indicate that that the placement rules are semantically correct, but can not currently be satisfied by the cluster. For example, if `FOLLOWERS=4` but there are only 3 TiKV stores which are candidates for followers.
* `INPROGRESS`: The PD is currently scheduling the placement.
* `SCHEDULED`: The PD has successfully scheduled the placement.

## Synopsis

Expand All @@ -27,7 +33,6 @@ ShowStmt ::=
```sql
CREATE PLACEMENT POLICY p1 PRIMARY_REGION="us-east-1" REGIONS="us-east-1,us-west-1" FOLLOWERS=4;
CREATE TABLE t1 (a INT) PLACEMENT POLICY=p1;
CREATE TABLE t2 (a INT) PRIMARY_REGION="us-east-1" REGIONS="us-east-1,us-west-1" FOLLOWERS=4;
SHOW PLACEMENT;
```

Expand All @@ -36,15 +41,12 @@ Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

+---------------+----------------------------------------------------------------------+------------------+
| Target | Placement | Scheduling_State |
+---------------+----------------------------------------------------------------------+------------------+
| POLICY p1 | PRIMARY_REGION="us-east-1" REGIONS="us-east-1,us-west-1" FOLLOWERS=4 | NULL |
| DATABASE test | PRIMARY_REGION="us-east-1" REGIONS="us-east-1,us-west-1" FOLLOWERS=4 | INPROGRESS |
| TABLE test.t1 | PRIMARY_REGION="us-east-1" REGIONS="us-east-1,us-west-1" FOLLOWERS=4 | INPROGRESS |
| TABLE test.t2 | PRIMARY_REGION="us-east-1" REGIONS="us-east-1,us-west-1" FOLLOWERS=4 | INPROGRESS |
+---------------+----------------------------------------------------------------------+------------------+
4 rows in set (0.00 sec)
```
Expand Down