TiDB 数据库 placement-rules 使用指南
作者: cchouqiang 原文来源:https://tidb.net/blog/26a9c334
placement-rules 介绍
placement-rules 作用
1、placement-rule 允许用户精细控制任何一段连续数据的副本数量、Raft 角色、放置位置等属性;
2、它由多条规则(Rule)组成,每条规则可以指定不同的副本数量、Raft 角色、放置位置等属性,以及这条规则生效的 key range;
3、利用 placement-rules 可以实现两地三中心的高可用性。
4、把重要的数据存储为 5 副本,把不太重要的数据存储为 3 副本。
如上图所示,通过 placement-rules 可以实现表的精细化控制。meta 原数据表设置为 5 个 voter 副本;table-01 设置为 3 个 voter 副本,2 个 learner 副本;table-02 设置为 3 个 voter 副本,2 个 follower 副本;table-03 设置为 1 个 voter 副本,2 个 follower 副本,1 个 learner 副本。
placement-rules 使用方法
设置 placement-rules 规则常用的两种方式为:
1、通过配置文件方式来设置规则;
2、通过 sql 来设置规则(placement-rules in sql)。
通过配置文件来设置规则
1、查看初始化 placement-rules 规则
通过 pd-ctl 来查看初始化规则
2、设置 5 副本
rule_new_1.json 如下:
> 注意: > 如果需要删除某条规则,只需要将规则的 count 置为 0 即可,对应 GroupID+ID 相同的规则会被删除。 > 由默认变为指定的规则,必须把"id"为"default"规则的count设置为0,否则不能实现预期。 > 设置完新规则后,一定要进行config placement-rules show验证。 > ```
Bash[ { “group_id”: “pd”, “id”: “default”, “start_key”: “”, “end_key”: “”, “role”: “voter”, “is_witness”: false, “count”: 0, “location_labels”: [ “zone”, “host” ] }, { “group_id”: “pd”, “id”: “zone1”, “start_key”: “”, “end_key”: “”, “role”: “voter”, “count”: 1, “label_constraints”: [ {“key”: “host”, “op”: “in”, “values”: [“h-125”]} ], “location_labels”: [“zone”, “rack”, “host”] }, { “group_id”: “pd”, “id”: “zone2”, “start_key”: “”, “end_key”: “”, “role”: “voter”, “count”: 1, “label_constraints”: [ {“key”: “host”, “op”: “in”, “values”: [“h-162”]} ], “location_labels”: [“zone”, “rack”, “host”] }, { “group_id”: “pd”, “id”: “zone3”, “start_key”: “”, “end_key”: “”, “role”: “voter”, “count”: 1, “label_constraints”: [ {“key”: “host”, “op”: “in”, “values”: [“h-163”]} ], “location_labels”: [“zone”, “rack”, “host”] }, { “group_id”: “pd”, “id”: “zone4”, “start_key”: “”, “end_key”: “”, “role”: “voter”, “count”: 1, “label_constraints”: [ {“key”: “host”, “op”: “in”, “values”: [“h-54”]} ], “location_labels”: [“zone”, “rack”, “host”] }, { “group_id”: “pd”, “id”: “zone5”, “start_key”: “”, “end_key”: “”, “role”: “voter”, “count”: 1, “label_constraints”: [ {“key”: “host”, “op”: “in”, “values”: [“h-78”]} ], “location_labels”: [“zone”, “rack”, “host”] }]
![](https://tidb-blog.oss-cn-beijing.aliyuncs.com/media/unnamed-1733830127624.png) ![](https://tidb-blog.oss-cn-beijing.aliyuncs.com/media/unnamed-1733830068816.png) ![](https://tidb-blog.oss-cn-beijing.aliyuncs.com/media/unnamed-1733830068876.png) ![](https://tidb-blog.oss-cn-beijing.aliyuncs.com/media/unnamed-1733830068869.png) 3、指定leader在zone1上
Bashtiup ctl:v7.5.4 pd -u 10.2.103.125:2379 config placement-rules save –in=“rule_new_2.json”
rule\_new\_2.json如下:
Bash[ { “group_id”: “pd”, “id”: “default”, “start_key”: “”, “end_key”: “”, “role”: “voter”, “is_witness”: false, “count”: 0, “location_labels”: [ “zone”, “host” ] }, { “group_id”: “pd”, “id”: “zone1”, “start_key”: “”, “end_key”: “”, “role”: “voter”, “count”: 1, “label_constraints”: [ {“key”: “host”, “op”: “in”, “values”: [“h-125”]} ], “location_labels”: [“zone”, “rack”, “host”] }, { “group_id”: “pd”, “id”: “zone2”, “start_key”: “”, “end_key”: “”, “role”: “follower”, “count”: 1, “label_constraints”: [ {“key”: “host”, “op”: “in”, “values”: [“h-162”]} ], “location_labels”: [“zone”, “rack”, “host”] }, { “group_id”: “pd”, “id”: “zone3”, “start_key”: “”, “end_key”: “”, “role”: “follower”, “count”: 1, “label_constraints”: [ {“key”: “host”, “op”: “in”, “values”: [“h-163”]} ], “location_labels”: [“zone”, “rack”, “host”] }, { “group_id”: “pd”, “id”: “zone4”, “start_key”: “”, “end_key”: “”, “role”: “follower”, “count”: 1, “label_constraints”: [ {“key”: “host”, “op”: “in”, “values”: [“h-54”]} ], “location_labels”: [“zone”, “rack”, “host”] }, { “group_id”: “pd”, “id”: “zone5”, “start_key”: “”, “end_key”: “”, “role”: “follower”, “count”: 1, “label_constraints”: [ {“key”: “host”, “op”: “in”, “values”: [“h-78”]} ], “location_labels”: [“zone”, “rack”, “host”] }]
![](https://tidb-blog.oss-cn-beijing.aliyuncs.com/media/unnamed-1733830213501.png) ![](https://tidb-blog.oss-cn-beijing.aliyuncs.com/media/unnamed-1733830227201.png) 此时,leader都集中在125节点上。 注意: 此规则可以配合tidb\_replica\_read参数,实现读写分离,读在follower节点,写在leader节点。
Bashset global tidb_replica_read = ‘follower’;– 设置读访问 follower 节点
## 通过placement-rules in sql来设置规则 1、在没设置placement-rules时,tpcc.oorder表有2个region,leader分别在“h-125”、“h-78”。
sqlselect distinct t2.db_name,t2.table_name,t2.region_id,t3.peer_id,t3.is_leader,t1.address,replace(replace(t1.label,‘, “value”’, “),‘“key”: ‘,“) from INFORMATION_SCHEMA.tikv_store_status t1,INFORMATION_SCHEMA.tikv_region_status t2,INFORMATION_SCHEMA.tikv_region_peers t3 where t2.db_name=‘tpcc’ and t2.region_id=t3.region_id and t3.store_id=t1.store_id and t2.table_name=‘oorder’ order by 1,2,3,4;
![](https://tidb-blog.oss-cn-beijing.aliyuncs.com/media/unnamed-1733829676983.png) 2、创建placement-rules,指定表tpcc.oorder的leader在“h-54”上。 创建policy\_role\_1规则,leader在“h-54”上,follower在“h-125”、“h-78”
SQLCREATE PLACEMENT POLICY policy_role_1 LEADER_CONSTRAINTS=”[+host=h-54]” FOLLOWER_CONSTRAINTS=‘{“+host=h-78”: 1, “+host=h-125”: 1,}‘;
![](https://tidb-blog.oss-cn-beijing.aliyuncs.com/media/unnamed-1733829676949.png)
SQLMySQL [tpcc]> alter table oorder placement policy=‘policy_role_1’;
MySQL [tpcc]> select distinct t2.db_name,t2.table_name,t2.region_id,t3.peer_id,t3.is_leader,t1.address,replace(replace(t1.label,‘, “value”’, “),‘“key”: ‘,“)
from INFORMATION_SCHEMA.tikv_store_status t1,INFORMATION_SCHEMA.tikv_region_status t2,INFORMATION_SCHEMA.tikv_region_peers t3
where t2.db_name=‘tpcc’ and t2.region_id=t3.region_id and t3.store_id=t1.store_id and t2.table_name=‘oorder’ order by 1,2,3,4;
SQLCREATE PLACEMENT POLICY policy_role_2 LEADER_CONSTRAINTS=”[+host=h-78]” FOLLOWER_CONSTRAINTS=‘{“+host=h-125”: 1, “+host=h-162”: 1, “+host=h-163”: 1, “+host=h-54”: 1,}‘;
alter table oorder placement policy=‘policy_role_2’;
SQL– 查看 placement-rulesSHOW PLACEMENT;
– 查看集群 labelsSHOW PLACEMENT LABELS;
– 删除表的 placement-rulesalter table tpcc.oorder placement policy=default;```
总结
1、利用好 placement-rules 特性,可以更好的帮助我们实现数据库高可用。
2、placement-rules 易操作、灵活性高。
版权声明: 本文为 InfoQ 作者【TiDB 社区干货传送门】的原创文章。
原文链接:【http://xie.infoq.cn/article/36e02643c2b321165d676fe63】。文章转载请联系作者。
评论