分布式 | DBLE 分区算法系列总结

作者:管长龙

我们已经通过之前的六篇文章,介绍了 DBLE 和 MyCat 常见的六种分片算法,那么我们来做个总结吧!


  • DBLE 分片算法MyCat分片算法DBLEMyCat
    hash固定分片 hash 取模分片数最大支持 2880count 和 length 两个向量的点乘积恒等于 1024
    stringhash截取数字 hash 解析分片数最大支持 2880count 和 length 两个向量的点乘积恒等于 1024
    enum分片枚举
    numberrange范围约定写具体的数值可用 K 或 M 等数量替换符
    patternrange取模范围约束
    date按日期(天)分片

第七种 DBLE 分片算法 — jumpStringHash

除了以上六种常见的分片算法之外,DBLE 还独有一种分片算法:跳跃字符串算法

具体配置如下:

#rule.xml
<function name="jumphash"
          class="jumpStringHash">
        <property name="partitionCount">2</property>
        <property name="hashSlice">0:2</property>
</function>

partitionCont:分片数量

hashSlice:分片截取长度

该算法来自于 Google 的一篇文章《A Fast, Minimal Memory, Consistent Hash Algorithm》其核心思想是通过概率分布的方法将一个 hash 值在每个节点分布的概率变成 1/n,并且可以通过更简便的方法可以计算得出,而且分布也更加均匀。

注意事项:分片字段值为NULL时,数据恒落在0号节点之上;当真实存在于mysql的字段值为not null的时候,报错 “Sharding column can’t be null when the table in MySQL column is not null”

至此我们 DBLE 中支持的七种分片算法就介绍完了,相信读者朋友对 DBLE 的使用增加的熟悉,

系列文章的最后

完整分区实例

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dble:rule SYSTEM "rule.dtd">
<dble:rule xmlns:dble="http://dble.cloud/" version="9.9.9.9">
    <tableRule name="sharding-by-enum">
        <rule>
            <columns>id</columns>
            <algorithm>enum</algorithm>
        </rule>
    </tableRule>

    <tableRule name="sharding-by-range">
        <rule>
            <columns>id</columns>
            <algorithm>rangeLong</algorithm>
        </rule>
    </tableRule>

    <tableRule name="sharding-by-hash">
        <rule>
            <columns>id</columns>
            <algorithm>hashLong</algorithm>
        </rule>
    </tableRule>

    <tableRule name="sharding-by-hash2">
        <rule>
            <columns>id</columns>
            <algorithm>hashLong2</algorithm>
        </rule>
    </tableRule>

    <tableRule name="sharding-by-hash3">
        <rule>
            <columns>id</columns>
            <algorithm>hashLong3</algorithm>
        </rule>
    </tableRule>

    <tableRule name="sharding-by-mod">
        <rule>
            <columns>id</columns>
            <algorithm>hashmod</algorithm>
        </rule>
    </tableRule>

    <tableRule name="sharding-by-hash-str">
        <rule>
            <columns>id</columns>
            <algorithm>hashString</algorithm>
        </rule>
    </tableRule>

    <tableRule name="sharding-by-date">
        <rule>
            <columns>calldate</columns>
            <algorithm>partbydate</algorithm>
        </rule>
    </tableRule>

    <tableRule name="sharding-by-pattern">
        <rule>
            <columns>id</columns>
            <algorithm>pattern</algorithm>
        </rule>
    </tableRule>


    <!-- enum partition -->
    <function name="enum"
              class="Enum">
        <property name="mapFile">partition-hash-int.txt</property>
        <property name="defaultNode">0</property><!--the default is -1,means unexpected value will report error-->
        <property name="type">0</property><!--0 means key is a number, 1 means key is a string-->
    </function>

    <!-- number range partition -->
    <function name="rangeLong" class="NumberRange">
        <property name="mapFile">autopartition-long.txt</property>
        <property name="defaultNode">0</property><!--he default is -1,means unexpected value will report error-->
    </function>

    <!-- Hash partition,when partitionLength=1, it is a mod partition-->
    <!--MAX(sum(count*length[i]) must not more then 2880-->
    <function name="hashLong" class="Hash">
        <property name="partitionCount">8</property>
        <property name="partitionLength">128</property>
        <!-- <property name="partitionCount">2,3</property>
        <property name="partitionLength">4,5</property>-->
    </function>

    <!-- Hash partition,when partitionLength=1, it is a mod partition-->
    <!--MAX(sum(count*length[i]) must not more then 2880-->
    <function name="hashLong2" class="Hash">
        <property name="partitionCount">2</property>
        <property name="partitionLength">512</property>
        <!-- <property name="partitionCount">2,3</property>
        <property name="partitionLength">4,5</property>-->
    </function>

    <!-- Hash partition,when partitionLength=1, it is a mod partition-->
    <!--MAX(sum(count*length[i]) must not more then 2880-->
    <function name="hashLong3" class="Hash">
        <property name="partitionCount">2,1</property>
        <property name="partitionLength">256,512</property>
        <!-- <property name="partitionCount">2,3</property>
        <property name="partitionLength">4,5</property>-->
    </function>

    <!-- eg:  mod 4 -->
    <function name="hashmod" class="Hash">
        <property name="partitionCount">4</property>
        <property name="partitionLength">1</property>
    </function>

    <!-- Hash partition for string-->
    <function name="hashString" class="StringHash">
        <property name="partitionCount">8</property>
        <property name="partitionLength">128</property>
        <property name="hashSlice">0:2</property>
        <!--<property name="hashSlice">-4:0</property> -->
    </function>

    <!-- date partition -->
    <!-- 4 case:
    1.set sEndDate and defaultNode: input <sBeginDate ,router to defaultNode; input>sEndDate ,mod the period
    2.set sEndDate, but no defaultNode:input <sBeginDate report error; input>sEndDate ,mod the period
    3.set defaultNode without sEndDate: input <sBeginDate router to defaultNode;input>sBeginDate + (node size)*sPartionDay-1 will report error(expected is defaultNode,but can't control now)
    4.sEndDate and defaultNode are all not set: input <sBeginDate report error;input>sBeginDate + (node size)*sPartionDay-1 will report error
     -->
    <function name="partbydate" class="Date">
        <property name="dateFormat">yyyy-MM-dd</property>
        <property name="sBeginDate">2015-01-01</property>
        <property name="sEndDate">2015-01-31
        </property> <!--if not set sEndDate,then in fact ,the sEndDate = sBeginDate+ (node size)*sPartionDay-1 -->
        <property name="sPartionDay">10</property>
        <property name="defaultNode">0</property><!--the default is -1-->
    </function>

    <!-- pattern partition -->
    <!--mapFile must contains all value of 0~patternValue-1,key and value must be Continuous increase-->
    <function name="pattern"
              class="PatternRange">
        <property name="mapFile">partition-pattern.txt</property>
        <property name="patternValue">1024</property>
        <property name="defaultNode">0</property><!--contains string which is not number,router to default node-->
    </function>
</dble:rule>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值