0
3
2
0
专栏/.../

TiDB数据库placement-rules使用指南

 cchouqiang  发表于  2024-12-10

placement-rules介绍

TiDB的placement-rules是一种用于控制数据在TiKV集群中放置位置的规则系统。它允许用户通过SQL接口、配置文件的方式,来配置数据的副本数量、存放位置、主机类型等属性,以满足不同的业务需求和优化数据的高可用性,还可以实现读写分离。

TiDB的placement-rules用于指导 PD 针对不同类型的数据生成对应的调度。通过组合不同的调度规则,用户可以精细地控制任何一段连续数据的副本数量、存放位置、主机类型、是否参与 Raft 投票、是否可以担任 Raft leader 等属性。

Placement Rules 特性在4.0版本开始引入的一套副本规则系统,Placement Rules 特性在 TiDB v5.0 及以上的版本中默认开启。

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来查看初始化规则

tiup ctl:v7.5.4 pd -u 10.2.103.125:2379 config placement-rules show

2、设置5副本

tiup ctl:v7.5.4 pd -u 10.2.103.125:2379 config placement-rules save --in="rule_new_1.json"

rule_new_1.json如下:

注意:
如果需要删除某条规则,只需要将规则的 count 置为 0 即可,对应 GroupID+ID 相同的规则会被删除。
由默认变为指定的规则,必须把"id"为"default"规则的count设置为0,否则不能实现预期。
设置完新规则后,一定要进行config placement-rules show验证。
[
  {
    "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"]
    }
]

3、指定leader在zone1上

tiup ctl:v7.5.4 pd -u 10.2.103.125:2379 config placement-rules save --in="rule_new_2.json"

rule_new_2.json如下:

[
  {
    "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"]
    }
]

此时,leader都集中在125节点上。

注意:

此规则可以配合tidb_replica_read参数,实现读写分离,读在follower节点,写在leader节点。

set global tidb_replica_read = 'follower';
--设置读访问follower节点

通过placement-rules in sql来设置规则

1、在没设置placement-rules时,tpcc.oorder表有2个region,leader分别在“h-125”、“h-78”。

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;

2、创建placement-rules,指定表tpcc.oorder的leader在“h-54”上。

创建policy_role_1规则,leader在“h-54”上,follower在“h-125”、“h-78”

CREATE PLACEMENT POLICY policy_role_1 LEADER_CONSTRAINTS="[+host=h-54]" FOLLOWER_CONSTRAINTS='{"+host=h-78": 1, "+host=h-125": 1,}';

MySQL [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;

通过设置规则后,leader从“h-125”转移到“h-54”

3、创建placement-rules,指定表tpcc.oorder为五副本

创建policy_role_2规则,leader在“h-78”上,其余副本在另外几个tikv节点上

CREATE 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';

4、查看已有的placement-rules in sql

--查看placement-rules
SHOW PLACEMENT;

--查看集群labels
SHOW PLACEMENT LABELS;

--删除表的placement-rules
alter table tpcc.oorder placement policy=default;

总结

1、利用好placement-rules特性,可以更好的帮助我们实现数据库高可用。

2、placement-rules易操作、灵活性高。

0
3
2
0

版权声明:本文为 TiDB 社区用户原创文章,遵循 CC BY-NC-SA 4.0 版权协议,转载请附上原文出处链接和本声明。

评论
暂无评论