您的位置:首页 > 其它

MyCat生产实践--一致性hash分片&扩容

2017-12-22 17:02 549 查看


1、 mycat一致性hash算法分片测试结果

配置el_user_user_info表使用一致性hash算法进行分片。 

schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="mycatdb" checkSQLschema="false" sqlMaxLimit="100">
<table name="el_user_user_info" dataNode="dn$1-2" rule="sharding-by-murmur-userid" />
</schema>
<dataNode name="dn$1-16" dataHost="localhost1" database="db$1-16" />
<dataHost name="localhost1" maxCon="500" minCon="100" balance="0"
writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
<heartbeat>select user()</heartbeat>

<writeHost host="hostM1" url="localhost:3306" user="root"
password="123456" >
</writeHost>

</dataHost>
</mycat:schema>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

bd75

rule.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/">

<tableRule name="sharding-by-murmur-userid">
<rule>
<columns>UserID</columns>
<algorithm>murmur-userid</algorithm>
</rule>
</tableRule>
<function name="murmur-userid" class="io.mycat.route.function.PartitionByMurmurHash">
<property name="seed">0</property><!-- 默认是0 -->
<property name="type">1</property><!-- 默认是0, 表示integer, 非0表示string-->
<property name="count">2</property><!-- 要分片的数据库节点数量,必须指定,否则没法分片 -->
<property name="virtualBucketTimes">160</property><!-- 一个实际的数据库节点被映射为这么多虚拟节点,默认是160倍,也就是虚拟节点数是物理节点数的160倍 -->
<!-- <property name="weightMapFile">weightMapFile</property> 节点的权重,没有指定权重的节点默认是1。以properties文件的格式填写,以从0开始到count-1的整数值也就是节点索引为key,以节点权重值为值。>所有权重值必须是正整数,否则以1代替 -->
<property name="bucketMapPath">/usr/local/mycat/logs/bucketMapPath-murmur-userid</property>
<!-- 用于测试时观察各物理节点与虚拟节点的分布情况,如果指定了这个属性,会把虚拟节点的murmur hash值与物理节点的映射按行输出到这个文件,没有默认值,如果不指定,就不会输出任何东西 -->
</function>
</mycat:rule>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

使用一致性hash算法(murmur),在MyCat中插入49911条数据,分为2个分片,数据量基本平衡。 




2、 mycat一致性hash算法扩容测试结果

先看扩容后的结果: 



有两种扩容方案:


方案一、

a) 停止数据服务,导出全部分片表数据,进行备份; 

b) 清空分片表; 

c) 在mycat中重新导入分片表,完成迁移。 

这种方案缺点很明显,需要导出导入的数据量大时,操作很费时,且容易出错。


方案二、

a) 修改MyCAT配置,使用扩容后的配置启动; 

b) 自己编写脚本,利用MyCAT的explain语法,分析出各个MySQL节点中分片表需要重新hash的数据,并记录ID(或数据)到文件中; 

b) 导出各节点中的需要重新hash的数据进行备份, 

c) 确认备份无误,清除原节点中的数据记录; 

c) 在mycat中重新导入备份的数据,完成迁移。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: