Skip to content

Update follower read and learner read descriptions #20609

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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions follower-read.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ set [session | global] tidb_replica_read = '<目标值>';
该变量用于设置期待的数据读取方式。

- 当设置为默认值 `leader` 或者空字符串时,TiDB 会维持原有行为方式,将所有的读取操作都发送给 leader 副本处理。
- 当设置为 `follower` 时,TiDB 会选择 Region 的 follower 副本完成所有的数据读取操作。
- 当设置为 `follower` 时,TiDB 会选择 Region 的 follower 副本完成所有的数据读取操作。当 Region 存在 learner 副本时,TiDB 也会考虑从 learner 副本读取数据,此时 follower 副本和 learner 副本具有相同优先级。若当前 Region 无可用 follower 副本和 learner 副本,TiDB 会回退到从 leader 副本读取数据。
- 当设置为 `leader-and-follower` 时,TiDB 可以选择任意副本来执行读取操作,此时读请求会在 leader 和 follower 之间负载均衡。
- 当设置为 `prefer-leader` 时,TiDB 会优先选择 leader 副本执行读取操作。当 leader 副本的处理速度明显变慢时,例如由于磁盘或网络性能抖动,TiDB 将选择其他可用的 follower 副本来执行读取操作。
- 当设置为 `closest-replicas` 时,TiDB 会优先选择分布在同一可用区的副本执行读取操作,对应的副本可以是 leader 或 follower。如果同一可用区内没有副本分布,则会从 leader 执行读取。
Expand All @@ -42,11 +42,16 @@ set [session | global] tidb_replica_read = '<目标值>';
- 当一个读请求的预估返回结果大于或等于变量 [`tidb_adaptive_closest_read_threshold`](/system-variables.md#tidb_adaptive_closest_read_threshold-从-v630-版本开始引入) 的值时,TiDB 会优先选择分布在同一可用区的副本执行读取操作。此时,为了避免读流量在各个可用区分布不均衡,TiDB 会动态检测当前在线的所有 TiDB 和 TiKV 的可用区数量分布,在每个可用区中 `closest-adaptive` 配置实际生效的 TiDB 节点数总是与包含 TiDB 节点最少的可用区中的 TiDB 节点数相同,并将其他多出的 TiDB 节点自动切换为读取 leader 副本。例如,如果 TiDB 分布在 3 个可用区,其中 A 和 B 两个可用区各包含 3 个 TiDB 节点,C 可用区只包含 2 个 TiDB 节点,那么每个可用区中 `closest-adaptive` 实际生效的 TiDB 节点数为 2,A 和 B 可用区中各有 1 个节点自动被切换为读取 leader 副本。
- 当一个读请求的预估返回结果小于变量 [`tidb_adaptive_closest_read_threshold`](/system-variables.md#tidb_adaptive_closest_read_threshold-从-v630-版本开始引入) 的值时,TiDB 会选择 leader 副本执行读取操作。

- 当设置为 `learner` 时,TiDB 会选择 learner 副本执行读取操作。在读取时,如果当前 Region 没有 learner 副本,TiDB 会报错
- 当设置为 `learner` 时,TiDB 会选择 learner 副本执行读取操作。在读取时,如果当前 Region 没有 learner 副本或 learner 副本不可用,TiDB 会回退到从可用 leader 或 follower 副本读取数据

> **注意:**
>
> 当设置为 `closest-replicas` 或 `closest-adaptive` 时,你需要配置集群以确保副本按照指定的设置分布在各个可用区。请参考[通过拓扑 label 进行副本调度](/schedule-replicas-by-topology-labels.md)为 PD 配置 `location-labels` 并为 TiDB 和 TiKV 设置正确的 `labels`。TiDB 依赖 `zone` 标签匹配位于同一可用区的 TiKV,因此请**务必**在 PD 的 `location-labels` 配置中包含 `zone` 并确保每个 TiDB 和 TiKV 节点的 `labels` 配置中包含 `zone`。如果是使用 TiDB Operator 部署的集群,请参考[数据的高可用](https://docs.pingcap.com/zh/tidb-in-kubernetes/v1.4/configure-a-tidb-cluster#%E6%95%B0%E6%8D%AE%E7%9A%84%E9%AB%98%E5%8F%AF%E7%94%A8)进行配置。
>
> 对于 v7.5 及之前的 TiDB 版本:
>
> - 当设置为 `follower` 且无可用 follower 副本及 learner 副本时,TiDB 会报错。
> - 当设置为 `learner` 且无可用 learner 副本时,TiDB 会报错。

## 实现机制

Expand Down