如果不能正常显示,请查看原文 , 或返回

Mycat的rule.xml及分片规则

rule.xml 里面就定义了我们对表进行拆分所涉及到的规则定义。我们可以灵活的对表使用不同的分片算法,或者对表使用相同的算法但具体的参数不同。这个文件里面主要有 tableRule 和 function 这两个标签。在具体使用过程中可以按照需求添加 tableRule 和 function。

xml version="1.0" encoding="UTF-8"?>

DOCTYPE mycat:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/">
        <tableRule name="rule1">
                <rule>
                        <columns>idcolumns>
                        <algorithm>func1algorithm>
                rule>
        tableRule>

        <tableRule name="rule2">
                <rule>
                        <columns>user_idcolumns>
                        <algorithm>func1algorithm>
                rule>
        tableRule>

        <tableRule name="sharding-by-intfile">
                <rule>
                        <columns>sharding_idcolumns>
                        <algorithm>hash-intalgorithm>
                rule>
        tableRule>
        <tableRule name="auto-sharding-long">
                <rule>
                        <columns>idcolumns>
                        <algorithm>rang-longalgorithm>
                rule>
        tableRule>
        <tableRule name="mod-long">
                <rule>
                        <columns>idcolumns>
                        <algorithm>mod-longalgorithm>
                rule>
        tableRule>
        <tableRule name="sharding-by-murmur">
                <rule>
                        <columns>idcolumns>
                        <algorithm>murmuralgorithm>
                rule>
        tableRule>
        <tableRule name="crc32slot">
                <rule>
                        <columns>idcolumns>
                        <algorithm>crc32slotalgorithm>
                rule>
        tableRule>
        <tableRule name="sharding-by-month">
                <rule>
                        <columns>create_timecolumns>
                        <algorithm>partbymonthalgorithm>
                rule>
        tableRule>
        <tableRule name="latest-month-calldate">
                <rule>
                        <columns>calldatecolumns>
                        <algorithm>latestMonthalgorithm>
                rule>
        tableRule>

        <tableRule name="auto-sharding-rang-mod">
                <rule>
                        <columns>idcolumns>
                        <algorithm>rang-modalgorithm>
                rule>
        tableRule>

        <tableRule name="jch">
                <rule>
                        <columns>idcolumns>
                        <algorithm>jump-consistent-hashalgorithm>
                rule>
        tableRule>

        <function name="murmur"
                class="io.mycat.route.function.PartitionByMurmurHash">
                <property name="seed">0property>
                <property name="count">2property>
                <property name="virtualBucketTimes">160property>
                
                
        function>

        <function name="crc32slot"
                          class="io.mycat.route.function.PartitionByCRC32PreSlot">
        function>
        <function name="hash-int"
                class="io.mycat.route.function.PartitionByFileMap">
                <property name="mapFile">partition-hash-int.txtproperty>
        function>
        <function name="rang-long"
                class="io.mycat.route.function.AutoPartitionByLong">
                <property name="mapFile">autopartition-long.txtproperty>
        function>
        <function name="mod-long" class="io.mycat.route.function.PartitionByMod">
                
                <property name="count">3property>
        function>

        <function name="func1" class="io.mycat.route.function.PartitionByLong">
                <property name="partitionCount">8property>
                <property name="partitionLength">128property>
        function>
        <function name="latestMonth"
                class="io.mycat.route.function.LatestMonthPartion">
                <property name="splitOneDay">24property>
        function>
        <function name="partbymonth"
                class="io.mycat.route.function.PartitionByMonth">
                <property name="dateFormat">yyyy-MM-ddproperty>
                <property name="sBeginDate">2015-01-01property>
        function>

        <function name="rang-mod" class="io.mycat.route.function.PartitionByRangeMod">
                <property name="mapFile">partition-range-mod.txtproperty>
        function>

        <function name="jump-consistent-hash" class="io.mycat.route.function.PartitionByJumpConsistentHash">
                <property name="totalBuckets">3property>
        function>
mycat:rule>
View Code

一、rule.xml

1. 标签

定义表规则:

<tableRule name="rule1">
    <rule>
        <columns>idcolumns>
        <algorithm>func1algorithm>
    rule>
tableRule>
  • name:属性指定唯一的名字,用于标识不同的表规则。内嵌的 rule 标签则指定对物理表中的哪一列进行拆分和使用什么路由算法。
  • columns:内指定要拆分的列名字。
  • algorithm:使用 function 标签中的 name 属性。连接表规则和具体路由算法。当然,多个表规则可以连接到同一个路由算法上。table 标签内使用。让逻辑表使用这个规则进行分片。

2. 标签

<function name="hash-int" class="io.mycat.route.function.PartitionByFileMap">
    <property name="mapFile">partition-hash-int.txtproperty>
function>
  • name:指定算法的名字。
  • class :制定路由算法具体的类名字。
  • property: 为具体算法需要用到的一些属性。

二、分片规则

1. 全局表

如果你的业务中有些数据类似于数据字典,比如配置文件的配置,常用业务的配置或者数据量不大很少变动的表,这些表往往不是特别大,而且大部分的业务场景都会用到,那么这种表适合于 Mycat 全局表,无须对数据进行切分,只要在所有的分片上保存一份数据即可。
Mycat 在 Join 操作中,业务表与全局表进行 Join 聚合会优先选择相同分片内的全局表 join,避免跨库 Join,在进行数据插入操作时,mycat 将把数据分发到全局表对应的所有分片执行,在进行数据读取时候将会随机获取一个节点读取数据。

全局表的配置如下:

<table name="t_area" primaryKey="id" type="global" dataNode="dn1,dn2" />

总结:即静态参数或一些公共的数据定义为全局表,在每个分片都存一份,但是需要保证每个分片的数据是一致的。

2. E-R分片表

有一类业务,例如订单(order)跟订单明细(order_detail),明细表会依赖于订单,也就是说会存在表的主从关系,这类似业务的切分可以抽象出合适的切分规则,比如根据用户 ID 切分,其他相关的表都依赖于用户 ID,再或者根据订单 ID 切分,总之部分业务总会可以抽象出父子关系的表。这类表适用于 ER 分片表,子表的记录与所关联的父表记录存放在同一个数据分片上,避免数据 Join 跨库操作。

以 order 与 order_detail 为例,schema.xml 中定义如下的分片配置,order,order_detail 根据 order_id进行数据切分,保证相同 order_id 的数据分到同一个分片上,在进行数据插入操作时,Mycat 会获取 order 所在的分片,然后将 order_detail 也插入到 order 所在的分片。

<table name="order" dataNode="dn$1-32" rule="mod-long">
    <childTable name="order_detail" primaryKey="id" joinKey="order_id" parentKey="order_id" />
table>

总结:将有主从关系的表都保存到同一个分片上,避免跨片join。

3. 多对多关联

有一类业务场景是 “主表 A+关系表+主表 B”,举例来说就是商户会员+订单+商户,对应这类业务,如何切分?

从会员的角度,如果需要查询会员购买的订单,那按照会员进行切分即可,但是如果要查询商户当天售出的订单,那又需要按照商户做切分,可是如果既要按照会员又要按照商户切分,几乎是无法实现,这类业务如何选择切分规则非常难。目前还暂时无法很好支持这种模式下的 3 个表之间的关联。目前总的原则是需要从业务角度来看,关系表更偏向哪个表,即“A 的关系”还是“B 的关系”,来决定关系表跟从个方向存储,未来 Mycat版本中将考虑将中间表进行双向复制,以实现从 A-关系表 以及 B-关系表的双向关联。

总结:针对“主表 A+关系表+主表 B”这种拥有中间表的模式,目前没有特别好的解决方法,只能根据关系表更偏向A表还是B表,让关系表跟从那个表存储。

4. 主键分片VS非主键分片

当你没有任何字段可以作为分片字段的时候,主键分片就是唯一选择,其优点是按照主键的查询最快,当采用自动增长的序列号作为主键时,还能比较均匀的将数据分片在不同的节点上。

  • 尽可能的比较均匀分布数据到各个节点上;
  • 该业务字段是最频繁的或者最重要的查询条件。

常见的除了主键之外的其他可能分片字段有“订单创建时间”、“店铺类别”或“所在省”等。当你找到某个合适的业务字段作为分片字段以后,不必纠结于“牺牲了按主键查询记录的性能”,因为在这种情况下,MyCAT 提供了“主键到分片”的内存缓存机制,热点数据按照主键查询,丝毫不损失性能。

<table name="t_user" primaryKey="user_id" dataNode="dn$1-32" rule="mod-long">
    <childTable name="t_user_detail" primaryKey="id" joinKey="user_id" parentKey="user_id" />
table>

对于非主键分片的 table,填写属性 primaryKey,此时 MyCAT 会将你根据主键查询的 SQL 语句的第一次执行结果进行分析,确定该 Table 的某个主键在什么分片上,并进行主键到分片 ID 的缓存。第二次或后续查询mycat 会优先从缓存中查询是否有 id–>node 即主键到分片的映射,如果有直接查询,通过此种方法提高了非主键分片的查询性能。

总结:分片规则的分片字段并不一定是主键字段,也可以是那些具有特殊意义的字段,如订单创建时间、省份、地市等。

三、常用的分片规则

1. 分片枚举

通过在配置文件中配置可能的枚举 id,自己配置分片,本规则适用于特定的场景,比如有些业务需要按照省份或地市来做保存,而全国省份区县固定的,这类业务使用本条规则。

rule.xml配置如下:

<tableRule name="sharding-by-intfile">
    <rule>
        <columns>region_idcolumns>
        <algorithm>hash-intalgorithm>
    rule>
tableRule>

<function name="hash-int" class="io.mycat.route.function.PartitionByFileMap">
    <property name="mapFile">partition-hash-int.txtproperty>
    <property name="type">1property>
    <property name="defaultNode">0property>
function>        

partition-hash-int.txt 配置:

#这里的0,1,2等是data node index
850=0
851=1
852=2
853=3
854=4
855=5
DEFAULT_NODE=0

上面columns 标识将要分片的表字段,algorithm 分片函数,其中分片函数配置中,mapFile标识配置文件名称,type默认值为0,0表示Integer,非零表示String,
所有的节点配置都是从0开始,及0代表节点1。

defaultNode 默认节点:小于0表示不设置默认节点,大于等于0表示设置默认节点,节点为指定的值。
默认节点的作用:枚举分片时,如果碰到不识别的枚举值,就让它路由到默认节点,如果不配置默认节点(defaultNode值小于0表示不配置默认节点),碰到不识别的枚举值就会报错, 类似这样的错误:can't find datanode for sharding column:column_name val:ffffffff

总结:对一些具有固定值的,如地市,进行枚举划分,哪个枚举字段存入哪个分片。

2. 固定分片hash算法

本条规则类似于十进制的求模运算,区别在于是二进制的操作,是取 id 的二进制低 10 位,即 id 二进制 &1111111111。
此算法的优点在于如果按照 10 进制取模运算,在连续插入 1-10 时候 1-10 会被分到 1-10 个分片,增大了插入的事务控制难度,而此算法根据二进制则可能会分到连续的分片,减少插入事务事务控制难度。

<tableRule name="rule1">
<rule>
<columns>user_idcolumns>
<algorithm>func1algorithm>
     rule>
tableRule>
<function name="func1" class="io.mycat.route.function.PartitionByLong">
<property name="partitionCount">2,1property>
<property name="partitionLength">256,512property>
function>

上面 columns 标识将要分片的表字段,algorithm 分片函数,partitionCount 分片个数列表,partitionLength 分片范围列表,分区长度:默认为最大 2^n=1024 ,即最大支持 1024 分区。
约束:
  count,length 两个数组的长度必须是一致的。
  1024 = sum((count[i]*length[i])). count 和 length 两个向量的点积恒等于 1024。

用法例子:
  本例的分区策略:希望将数据水平分成 3 份,前两份各占 25%,第三份占 50%。(故本例非均匀分区)

// |<———————1024———————————>|
// |<—-256—>|<—-256—>|<———-512————->|
// | partition0 | partition1 | partition2 |
// | 共 2 份,故 count[0]=2 | 共 1 份,故 count[1]=1 |
int[] count = new int[] { 2, 1 };
int[] length = new int[] { 256, 512 };
PartitionUtil pu = new PartitionUtil(count, length);

// 下面代码演示分别以 offerId 字段或 memberId 字段根据上述分区策略拆分的分配结果
int DEFAULT_STR_HEAD_LEN = 8; // cobar 默认会配置为此值
long offerId = 12345;
String memberId = "qiushuo";
// 若根据 offerId 分配,partNo1 将等于 0,即按照上述分区策略,offerId 为 12345 时将会被分配到 partition0 中
int partNo1 = pu.partition(offerId);
// 若根据 memberId 分配,partNo2 将等于 2,即按照上述分区策略,memberId 为 qiushuo 时将会被分到 partition2 中
int partNo2 = pu.partition(memberId, 0, DEFAULT_STR_HEAD_LEN);

如果需要平均分配设置:平均分为 4 分片,partitionCount*partitionLength=1024


    4
    256

总结:取 分片字段 的二进制 &1111111111,进行hash路由。

3. 范围固定

此分片适用于,提前规划好分片字段某个范围属于哪个分片

<tableRule name="auto-sharding-long">
    <rule>
        <columns>user_idcolumns>
        <algorithm>rang-longalgorithm>
    rule>
tableRule>
<function name="rang-long" class="io.mycat.route.function.AutoPartitionByLong">
    <property name="mapFile">autopartition-long.txtproperty>
    <property name="defaultNode">0property>
function>

上面 columns 标识将要分片的表字段,algorithm 分片函数,rang-long 函数中 mapFile 代表配置文件路径,defaultNode 表示超过范围后的默认节点。
所有的节点配置都是从 0 开始,及 0 代表节点 1,此配置非常简单,即预先制定可能的 id 范围到某个分片。

#这里的0,1,2等是data node index
0-500M=0
500M-1000M=1
1000M-1500M=20-10000000=0
10000001-20000000=1

总结:预先划定那个范围存入哪个分片节点。

4. 取模

为对分片字段求模运算。

<tableRule name="mod-long">
    <rule>
        <columns>user_idcolumns>
        <algorithm>mod-longalgorithm>
    rule>
tableRule>
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
    
    <property name="count">3property>
function>

上面 columns 标识将要分片的表字段,algorithm 分片函数。
此种配置非常明确即根据 id 进行十进制求模运算,相比固定分片 hash,此种在批量插入时可能存在批量插入单事务插入多数据分片,增大事务一致性难度。

缺点:存在批量插入,数据路由到多个分片,若失败,会导致事务不一致。

5. 按日期(天)分片

<tableRule name="sharding-by-date">
    <rule>
        <columns>create_timecolumns>
        <algorithm>sharding-by-datealgorithm>
    rule>
tableRule>
<function name="sharding-by-date" class="io.mycat.route.function.PartitionByDate">
    <property name="dateFormat">yyyy-MM-ddproperty>
    <property name="sBeginDate">2018-01-31property>
    <property name="sEndDate">2018-12-31property>
    <property name="sPartionDay">2property>
function>

说明:

  • columns :标识将要分片的表字段
  • algorithm :分片函数
  • dateFormat :日期格式
  • sBeginDate :开始日期
  • sEndDate:结束日期,如果配置了结束日期,则代码数据到达了这个日期的分片后,会重复从开始分片插入
  • sPartionDay :分区天数,即默认从开始日期算起,分隔 10 天一个分区

注意:配置规则的表的 dataNode 的分片,必须和分片规则数量一致,例如 2018-01-31 到 2018-12-31 ,每2天一个分片,一共需要168个分片。

6. 取模范围约束

此种规则是取模运算与范围约束的结合,主要为了后续数据迁移做准备,即可以自主决定取模后数据的节点分布。

<tableRule name="sharding-by-pattern">
    <rule>
        <columns>user_idcolumns>
        <algorithm>sharding-by-patternalgorithm>
rule>
tableRule>
<function name="sharding-by-pattern" class="io.mycat.route.function.PartitionByPattern"
    ="patternValue">256property>
    <property name="defaultNode">2property>
    <property name="mapFile">partition-pattern.txtproperty>
function>

partition-pattern.txt

# id partition range start-end ,data node index
###### first host configuration
1-32=0
33-64=1
65-96=2
97-128=3

######## second host configuration
129-160=4
161-192=5
193-224=6
225-256=7
0-0=8

上面 columns 标识将要分片的表字段,algorithm 分片函数,patternValue 即求模基数,defaoultNode默认节点,如果配置了默认,则不会按照求模运算。mapFile 配置文件路径。

配置文件中,1-32 即代表 id%256 后分布的范围,如果在 1-32 则在分区 1,其他类推,如果 id 非数据,则会分配在 defaoultNode 默认节点。

7. 截取数字做 hash 求模范围约束

类似于取模范围约束,此规则支持数据符号字母取模。

<tableRule name="sharding-by-prefixpattern">
    <rule>
        <columns>user_idcolumns>
        <algorithm>sharding-by-prefixpatternalgorithm>
    rule>
tableRule>
<function name="sharding-by-pattern"    class="io.mycat.route.function.PartitionByPrefixPattern">
    <property name="patternValue">256property>
    <property name="prefixLength">5property>
    <property name="mapFile">partition-pattern.txtproperty>
function>

partition-pattern.txt

# range start-end ,data node index
# ASCII
# 8-57=0-9 阿拉伯数字
# 64、65-90=@、A-Z
# 97-122=a-z

###### first host configuration
1-4=0
5-8=1
9-12=2
13-16=3
###### second host configuration
17-20=4
21-24=5
25-28=6
29-32=7
0-0=7

上面 columns 标识将要分片的表字段,algorithm 分片函数,patternValue 即求模基数,prefixLength ASCII 截取的位数。

mapFile 配置文件路径。配置文件中,1-32 即代表 id%256 后分布的范围,如果在 1-32 则在分区 1,其他类推

此种方式类似方式 6, 只不过采取的是将列中获取前 prefixLength 位列所有 ASCII 码的和进行求模
sum%patternValue , 获取的值,在范围内的分片数,

String idVal="gf89f9a";
Assert.assertEquals(true, 0==autoPartition.calculate(idVal));
idVal="8df99a";
Assert.assertEquals(true, 4==autoPartition.calculate(idVal));
idVal="8dhdf99a";
Assert.assertEquals(true, 3==autoPartition.calculate(idVal));

8. 应用指定

在运行阶段有应用自主决定路由到那个分片。

<tableRule name="sharding-by-substring">
    <rule>
    <columns>user_idcolumns>
    <algorithm>sharding-by-substringalgorithm>
rule>
tableRule>
    <function name="sharding-by-substring" class="io.mycat.route.function.PartitionDirectBySubString">
    <property name="startIndex">0property>
    <property name="size">2property>
    <property name="partitionCount">8property>
    <property name="defaultPartition">0property>
function>

上面 columns 标识将要分片的表字段,algorithm 分片函数。
此方法为直接根据字符子串(必须是数字)计算分区号(由应用传递参数,显式指定分区号)。
例如 id=05-100000002
在此配置中代表根据 id 中从 startIndex=0,开始,截取 siz=2 位数字即 05,05 就是获取的分区,如果没传
默认分配到 defaultPartition。

9. 截取数字 hash 解析

截取字符串中的 int 数值 hash 分片。

<tableRule name="sharding-by-stringhash">
    <rule>
        <columns>user_idcolumns>
        <algorithm>sharding-by-stringhashalgorithm>
    rule>
tableRule>
<function name="sharding-by-stringhash" class="io.mycat.route.function.PartitionByString">
    <property name="partitionLength">512property>
    <property name="partitionCount">2property>
    <property name="hashSlice">0:2property>
function>

这种分片规则一般不会用。

10. 一致性 hash

一致性 hash 运算有效解决了分布式数据的扩容问题。

<tableRule name="sharding-by-murmur">
    <rule>
        <columns>user_idcolumns>
        <algorithm>murmuralgorithm>
    rule>
tableRule>
<function name="murmur" class="io.mycat.route.function.PartitionByMurmurHash">
    <property name="seed">0property>
    <property name="count">2property>
    <property name="virtualBucketTimes">160property>
    
    
function>    

11. 按单月小时拆分

单月内按照小时拆分,最小粒度是小时,可以一天最多 24 个分片,最少 1 个分片,一个月完后下月从头开始循环。
每个月月尾,需要手工清理数据。

<tableRule name="sharding-by-hour">
    <rule>
        <columns>create_timecolumns>
        <algorithm>sharding-by-houralgorithm>
    rule>
tableRule>
<function name="sharding-by-hour" class="io.mycat.route.function.LatestMonthPartion">
    <property name="splitOneDay">24property>
function>    

columns: 拆分字段,字符串类型(yyyymmddHH)
splitOneDay : 一天切分的分片数

代码示例:

LatestMonthPartion partion = new LatestMonthPartion();
partion.setSplitOneDay(24);
Integer val = partion.calculate("2015020100");
assertTrue(val == 0);
val = partion.calculate("2015020216");
assertTrue(val == 40);
val = partion.calculate("2015022823");
assertTrue(val == 27 * 24 + 23);
Integer[] span = partion.calculateRange("2015020100", "2015022823");
assertTrue(span.length == 27 * 24 + 23 + 1);
assertTrue(span[0] == 0 && span[span.length - 1] == 27 * 24 + 23);
span = partion.calculateRange("2015020100", "2015020123");
assertTrue(span.length == 24);
assertTrue(span[0] == 0 && span[span.length - 1] == 23);

12. 范围求模切片

先进行范围分片计算出分片组,组内再求模。
优点可以避免扩容时的数据迁移,又可以一定程度上避免范围分片的热点问题。
综合了范围分片和求模分片的优点,分片组内使用求模可以保证组内数据比较均匀,分片组之间是范围分片可以兼顾范围查询。
最好事先规划好分片的数量,数据扩容时按分片组扩容,则原有分片组的数据不需要迁移。由于分片组内数据比较均匀,所以分片组内可以避免热点数据问题。

<tableRule name="auto-sharding-rang-mod">
    <rule>
        <columns>idcolumns>
        <algorithm>rang-modalgorithm>
    rule>
tableRule>
<function name="rang-mod" class="io.mycat.route.function.PartitionByRangeMod">
    <property name="mapFile">partition-range-mod.txtproperty>
    <property name="defaultNode">21property>
function>

上面 columns 标识将要分片的表字段,algorithm 分片函数,rang-mod 函数中 mapFile 代表配置文件路径,defaultNode 超过范围后的默认节点顺序号,节点从 0 开始。

partition-range-mod.txt

range start-end ,data node group size
以下配置一个范围代表一个分片组,=号后面的数字代表该分片组所拥有的分片的数量。
0-200M=5 //代表有 5 个分片节点
200M1-400M=1
400M1-600M=4
600M1-800M=4
800M1-1000M=6

13. 冷热数据分片

根据日期查询日志数据 冷热数据分布 ,最近 n 个月的到实时交易库查询,超过 n 个月的按照 m 天分片。

<tableRule name="sharding-by-date">
    <rule>
        <columns>create_timecolumns>
        <algorithm>sharding-by-hotdatealgorithm>
    rule>
tableRule>
<function name="sharding-by-hotdate" class="io.mycat.route.function.PartitionByHotDate">
    <property name="dateFormat">yyyy-MM-ddproperty>
    <property name="sLastDay">10property>
    <property name="sPartionDay">30property>
function>

14. 自然月分片

按月份列分区 ,每个自然月一个分片,格式 between 操作解析的范例。

<tableRule name="sharding-by-month">
    <rule>
        <columns>create_timecolumns>
        <algorithm>sharding-by-monthalgorithm>
    rule>
tableRule>
<function name="sharding-by-month" class="io.mycat.route.function.PartitionByMonth">
    <property name="dateFormat">yyyy-MM-ddproperty>
    <property name="sBeginDate">2014-01-01property>
function>

说明:
columns: 分片字段,字符串类型
dateFormat : 日期字符串格式
sBeginDate : 开始日期

PartitionByMonth partition = new PartitionByMonth();
partition.setDateFormat("yyyy-MM-dd");
partition.setsBeginDate("2014-01-01");
partition.init();
Assert.assertEquals(true, 0 == partition.calculate("2014-01-01")); 

 

返回