Skip to content

Commit

Permalink
Add best practices doc for readonly nodes (#12431)
Browse files Browse the repository at this point in the history
  • Loading branch information
Oreoxmt committed Feb 16, 2023
1 parent 5fea45a commit 8816c2d
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 1 deletion.
1 change: 1 addition & 0 deletions TOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@
- [Three-node Hybrid Deployment](/best-practices/three-nodes-hybrid-deployment.md)
- [Local Read Under Three Data Centers Deployment](/best-practices/three-dc-local-read.md)
- [Use UUIDs](/best-practices/uuid.md)
- [Read-Only Storage Nodes](/best-practices/readonly-nodes.md)
- [Use Placement Rules](/configure-placement-rules.md)
- [Use Load Base Split](/configure-load-base-split.md)
- [Use Store Limit](/configure-store-limit.md)
Expand Down
131 changes: 131 additions & 0 deletions best-practices/readonly-nodes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
---
title: Best Practices for Read-Only Storage Nodes
summary: Learn how to configure read-only storage nodes to physically isolate important online services.
---

# Best Practices for Read-Only Storage Nodes

This document introduces how to configure read-only storage nodes and how to direct backup, analysis, testing, and other traffic to these nodes. In this way, loads with high tolerance for delay can be physically isolated from important online services.

## Procedures

### 1. Specify some TiKV nodes as read-only

To specify some TiKV nodes as read-only, you can mark these nodes with a special label (use `$` as the prefix of the label key). Unless you explicitly specify these nodes to store some data using Placement Rules, PD does not schedule any data to these nodes.

You can configure a read-only node by running the `tiup cluster edit-config` command:

```
tikv_servers:
- host: ...
...
labels:
$mode: readonly
```

### 2. Use Placement Rules to store data on read-only nodes as learners

1. Run the `pd-ctl config placement-rules` command to export the default Placement Rules:

```shell
pd-ctl config placement-rules rule-bundle load --out="rules.json"
```

If you have not configured Placement Rules before, the output is as follows:

```json
[
{
"group_id": "pd",
"group_index": 0,
"group_override": false,
"rules": [
{
"group_id": "pd",
"id": "default",
"start_key": "",
"end_key": "",
"role": "voter",
"count": 3
}
]
}
]
```

2. Store all data on the read-only nodes as a learner. The following example is based on the default configuration:

```json
[
{
"group_id": "pd",
"group_index": 0,
"group_override": false,
"rules": [
{
"group_id": "pd",
"id": "default",
"start_key": "",
"end_key": "",
"role": "voter",
"count": 3
},
{
"group_id": "pd",
"id": "readonly",
"start_key": "",
"end_key": "",
"role": "learner",
"count": 1,
"label_constraints": [
{
"key": "$mode",
"op": "in",
"values": [
"readonly"
]
}
],
"version": 1
}
]
}
]
```

3. Use the `pd-ctl config placement-rules` command to write the preceding configurations to PD:

```shell
pd-ctl config placement-rules rule-bundle save --in="rules.json"
```

> **Note:**
>
> - If you perform the preceding operations on a cluster with a large dataset, the entire cluster might need some time to completely replicate data to read-only nodes. During this period, the read-only nodes might not be able to provide services.
> - Because of the special implementation of backup, the learner number of each label cannot exceed 1. Otherwise, it will generate duplicate data during backup.
### 3. Use Follower Read to read data from read-only nodes

#### 3.1 Use Follower Read in TiDB

To read data from read-only nodes when using TiDB, you can set the system variable [`tidb_replica_read`](/system-variables.md#tidb_replica_read-new-in-v40) to `learner`:

```sql
set tidb_replica_read=learner;
```

#### 3.2 Use Follower Read in TiSpark

To read data from read-only nodes when using TiSpark, you can set the configuration item `spark.tispark.replica_read` to `learner` in the Spark configuration file:

```
spark.tispark.replica_read learner
```

#### 3.3 Use Follower Read when backing up cluster data

To read data from read-only nodes when backing up cluster data, you can specify the `--replica-read-label` option in the br command line. Note that when running the following command in shell, you need to use single quotes to wrap the label to prevent `$` from being parsed.

```shell
br backup full ... --replica-read-label '$mode:readonly'
```
2 changes: 2 additions & 0 deletions follower-read.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ This variable is used to set the expected data read mode.
- If the estimated result of a read request is greater than or equal to the value of [`tidb_adaptive_closest_read_threshold`](/system-variables.md#tidb_adaptive_closest_read_threshold-new-in-v630), TiDB prefers to select a replica in the same availability zone for read operations. To avoid unbalanced distribution of read traffic across availability zones, TiDB dynamically detects the distribution of availability zones for all online TiDB and TiKV nodes. In each availability zone, the number of TiDB nodes whose `closest-adaptive` configuration takes effect is limited, which is always the same as the number of TiDB nodes in the availability zone with the fewest TiDB nodes, and the other TiDB nodes automatically read from the leader replica. For example, if TiDB nodes are distributed across 3 availability zones (A, B, and C), where A and B each contains 3 TiDB nodes and C contains only 2 TiDB nodes, the number of TiDB nodes whose `closest-adaptive` configuration takes effect in each availability zone is 2, and the other TiDB node in each of the A and B availability zones automatically selects the leader replica for read operations.
- If the estimated result of a read request is less than the value of [`tidb_adaptive_closest_read_threshold`](/system-variables.md#tidb_adaptive_closest_read_threshold-new-in-v630), TiDB can only select the leader replica for read operations.

- When the value of `tidb_replica_read` is set to `learner`, TiDB reads data from the learner replica. If there is no learner replica in the Region, TiDB returns an error.

<CustomContent platform="tidb">

> **Note:**
Expand Down
2 changes: 1 addition & 1 deletion system-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -3534,7 +3534,7 @@ SHOW WARNINGS;
- Persists to cluster: Yes
- Type: Enumeration
- Default value: `leader`
- Possible values: `leader`, `follower`, `leader-and-follower`, `prefer-leader`, `closest-replicas`, `closest-adaptive`
- Possible values: `leader`, `follower`, `leader-and-follower`, `prefer-leader`, `closest-replicas`, `closest-adaptive`, and `learner`. The `learner` value is introduced in v6.6.0.
- This variable is used to control where TiDB reads data.
- For more details about usage and implementation, see [Follower read](/follower-read.md).

Expand Down
2 changes: 2 additions & 0 deletions tispark-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ The configurations in the following table can be put together with `spark-defaul
| `spark.tispark.tikv.conn_recycle_time` | `60s` | The interval for cleaning expired connections with TiKV. It takes effect only when certificate reloading is enabled. The default value is `60s` (60 seconds). |
| `spark.tispark.host_mapping` | | The route map used to configure the mapping between public IP addresses and intranet IP addresses. When the TiDB cluster is running on the intranet, you can map a set of intranet IP addresses to public IP addresses for an outside Spark cluster to access. The format is `{Intranet IP1}:{Public IP1};{Intranet IP2}:{Public IP2}`, for example, `192.168.0.2:8.8.8.8;192.168.0.3:9.9.9.9`. |
| `spark.tispark.new_collation_enable` | | When [new collation](https://docs.pingcap.com/tidb/stable/character-set-and-collation#new-framework-for-collations) is enabled on TiDB, this configuration can be set to `true`. If `new collation` is not enabled on TiDB, this configuration can be set to `false`. If this item is not configured, TiSpark configures `new collation` automatically based on the TiDB version. The configuration rule is as follows: If the TiDB version is greater than or equal to v6.0.0, it is `true`; otherwise, it is `false`. |
| `spark.tispark.replica_read` | `leader` | The type of the replica to read. Value options are `leader`, `follower`, and `learner`. Multiple types can be specified at the same time and TiSpark selects the type according to the order. |
| `spark.tispark.replica_read.label` | | The label of the target TiKV node. The format is `label_x=value_x,label_y=value_y`, and the items are connected by logical conjunction. |

### TLS configurations

Expand Down

0 comments on commit 8816c2d

Please sign in to comment.