分片枚舉
通過在配置文件中配置可能的枚舉id,自己配置分片,本規則適用於特定的場景,比如有些業務需要按照省份或區縣來做保存,
而全國省份區縣固定的,這類業務使用本條規則,配置如下:
<tableRule name="sharding-by-intfile"> <rule> <!--標識將要分片的表字段--> <columns>user_id</columns> <!--分片函數--> <algorithm>hash-int</algorithm> </rule> </tableRule> <function name="hash-int" class="org.opencloudb.route.function.PartitionByFileMap"> <!--標識配置文件名稱--> <property name="mapFile">partition-hash-int.txt</property> <!--type默認值為0,0表示Integer,非零表示String--> <property name="type">0</property> <!-- 默認節點:小於0表示不設置默認節點,大於等於0表示設置默認節點, 所有的節點配置都是從0開始,即0代表節點1 作用: 枚舉分片時,如果碰到不識別的枚舉值,就讓它路由到默認節點 如果不配置默認節點(defaultNode值小於0表示不配置默認節點),碰到不識別的枚舉值就會報錯, can’t find datanode for sharding column:column_name val:ffffffff --> <property name="defaultNode">0</property> </function>
partition-hash-int.txt 配置:
10000=0
10010=1
DEFAULT_NODE=1
固定分片hash算法
本條規則類似於十進制的求模運算,區別在於是二進制的操作,是取id的二進制低10位,即id二進制&1111111111。
此算法的優點在於如果按照10進制取模運算,在連續插入1-10時候1-10會被分到1-10個分片,增大了插入的事務控制難度,而
此算法根據二進制則可能會分到連續的分片,減少插入事務事務控制難度。
<tableRule name="rule1"> <rule> <columns>user_id</columns> <algorithm>func1</algorithm> </rule> </tableRule> <function name="func1" class="org.opencloudb.route.function.PartitionByLong"> <!--分片個數列表--> <property name="partitionCount">2,1</property> <!--分片范圍列表--> <property name="partitionLength">256,512</property> </function>
分區長度:默認為最大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);
<function name="func1" class="org.opencloudb.route.function.PartitionByLong"> <property name="partitionCount">4</property> <property name="partitionLength">256</property> </function>
范圍約定
此分片適用於,提前規划好分片字段某個范圍屬於哪個分片,
start <= range <= end.
range start-end ,data node index
K=1000,M=10000.
<tableRule name="auto-sharding-long"> <rule> <columns>user_id</columns> <algorithm>rang-long</algorithm> </rule> </tableRule> <function name="rang-long" class="org.opencloudb.route.function.AutoPartitionByLong"> <!--配置文件路徑--> <property name="mapFile">autopartition-long.txt</property> <!-- 超過范圍后的默認節點。 所有的節點配置都是從0開始,及0代表節點1,即預先制定可能的id范圍到某個分片 0-500M=0 500M-1000M=1 1000M-1500M=2 或 0-10000000=0 10000001-20000000=1 --> <property name="defaultNode">0</property> </function>
求模
此規則為對分片字段求摸運算。
<tableRule name="mod-long"> <rule> <columns>user_id</columns> <algorithm>mod-long</algorithm> </rule> </tableRule> <function name="mod-long" class="org.opencloudb.route.function.PartitionByMod"> <!-- how many data nodes --> <property name="count">3</property> </function>
此種配置非常明確即根據id進行十進制求模預算,相比固定分片hash,此種在批量插入時可能存在批量插入單事務插入多數據分
片,增大事務一致性難度。
按日期(天)分片
此規則為按天分片。
<tableRule name="sharding-by-date"> <rule> <columns>create_time</columns> <algorithm>sharding-by-date</algorithm> </rule> </tableRule> <function name="sharding-by-date" class="org.opencloudb.route.function.PartitionByDate"> <!--日期格式--> <property name="dateFormat">yyyy-MM-dd</property> <!--開始日期--> <property name="sBeginDate">2014-01-01</property> <!--分區天數,即默認從開始日期算起,分隔10天一個分區--> <property name="sPartionDay">10</property> </function>
配置說明:
Assert.assertEquals(true, 0 == partition.calculate(“2014-01-01”)); Assert.assertEquals(true, 0 == partition.calculate(“2014-01-10”)); Assert.assertEquals(true, 1 == partition.calculate(“2014-01-11”)); Assert.assertEquals(true, 12 == partition.calculate(“2014-05-01”));
取模范圍約束
此種規則是取模運算與范圍約束的結合,主要為了后續數據遷移做准備,即可以自主決定取模后數據的節點分布。
<tableRule name="sharding-by-pattern"> <rule> <columns>user_id</columns> <algorithm>sharding-by-pattern</algorithm> </rule> </tableRule> <function name="sharding-by-pattern" class="org.opencloudb.route.function.PartitionByPattern"> <property name="patternValue">256</property> <property name="defaultNode">2</property> <property name="mapFile">partition-pattern.txt</property> </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=7
patternValue 即求模基數,
defaoultNode 默認節點,如果配置了默認,則不會按照求模運算
mapFile 配置文件路徑
配置文件中,1-32 即代表id%256后分布的范圍,如果在1-32則在分區1,其他類推,如果id非數據,則會分配在defaoultNode
默認節點
String idVal = “0”; Assert.assertEquals(true, 7 == autoPartition.calculate(idVal)); idVal = “45a”; Assert.assertEquals(true, 2 == autoPartition.calculate(idVal));
ASCII碼求模范圍約束
此種規則類似於取模范圍約束,此規則支持數據符號字母取模。
<tableRule name="sharding-by-prefixpattern"> <rule> <columns>user_id</columns> <algorithm>sharding-by-prefixpattern</algorithm> </rule> </tableRule> <function name="sharding-by-pattern" class="org.opencloudb.route.function.PartitionByPrefixPattern"> <property name="patternValue">256</property> <property name="prefixLength">5</property> <property name="mapFile">partition-pattern.txt</property> </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
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));
應用指定
此規則是在運行階段有應用自主決定路由到那個分片。
<tableRule name="sharding-by-substring"> <rule> <columns>user_id</columns> <algorithm>sharding-by-substring</algorithm> </rule> </tableRule> <function name="sharding-by-substring" class="org.opencloudb.route.function.PartitionDirectBySubString"> <property name="startIndex">0</property> <!-- zero-based --> <property name="size">2</property> <property name="partitionCount">8</property> <property name="defaultPartition">0</property> </function>
此方法為直接根據字符子串(必須是數字)計算分區號(由應用傳遞參數,顯式指定分區號)。
例如id=05-100000002
在此配置中代表根據id中從startIndex=0,開始,截取siz=2位數字即05,05就是獲取的分區,如果沒傳默認分配到
defaultPartition
字符串hash解析
此規則是截取字符串中的int數值hash分片。
<tableRule name="sharding-by-stringhash"> <rule> <columns>user_id</columns> <algorithm>sharding-by-stringhash</algorithm> </rule> </tableRule> <function name="sharding-by-stringhash" class="org.opencloudb.route.function.PartitionByString"> <property name=length>512</property> <!-- zero-based --> <property name="count">2</property> <property name="hashSlice">0:2</property> </function>
函數中length代表字符串hash求模基數,count分區數,hashSlice hash預算位
即根據子字符串中int值 hash運算
hashSlice : 0 means str.length(), -1 means str.length()-1
/**
* “2” -> (0,2)
* “1:2” -> (1,2)
* “1:” -> (1,0)
* “-1:” -> (-1,0)
* “:-1” -> (0,-1)
* “:” -> (0,0)
*/
例子:
String idVal=null; rule.setPartitionLength("512"); rule.setPartitionCount("2"); rule.init(); rule.setHashSlice("0:2"); // idVal = "0"; // Assert.assertEquals(true, 0 == rule.calculate(idVal)); // idVal = "45a"; // Assert.assertEquals(true, 1 == rule.calculate(idVal)); //last 4 rule = new PartitionByString(); rule.setPartitionLength("512"); rule.setPartitionCount("2"); rule.init(); //last 4 characters rule.setHashSlice("-4:0"); idVal = "aaaabbb0000"; Assert.assertEquals(true, 0 == rule.calculate(idVal)); idVal = "aaaabbb2359"; Assert.assertEquals(true, 0 == rule.calculate(idVal));
一致性hash
一致性hash預算有效解決了分布式數據的擴容問題。
<tableRule name="sharding-by-murmur"> <rule> <columns>user_id</columns> <algorithm>murmur</algorithm> </rule> </tableRule> <function name="murmur" class="org.opencloudb.route.function.PartitionByMurmurHash"> <property name="seed">0</property><!-- 默認是0--> <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">/etc/mycat/bucketMapPath</property> 用於測試時觀察各物理節點與虛擬節點的分布情況,如果指定了這個屬性,會把虛擬節點的murmur hash值與物理節點的映射按行輸出到這個文件,沒有默認值,如果不指定,就不會輸出任何東西 --> </function>
按單月小時拆分
此規則是單月內按照小時拆分,最小粒度是小時,可以一天最多24個分片,最少1個分片,一個月完后下月從頭開始循環。
每個月月尾,需要手工清理數據。
<tableRule name="sharding-by-hour"> <rule> <columns>create_time</columns> <algorithm>sharding-by-hour</algorithm> </rule> </tableRule> <function name="sharding-by-hour" class="org.opencloudb.route.function.LatestMonthPartion"> <property name="splitOneDay">24</property> </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);
自然月分片
按月份列分區,每個自然月一個分片,格式 between操作解析的范例。
<tableRule name="sharding-by-month"> <rule> <columns>create_time</columns> <algorithm>sharding-by-month</algorithm> </rule> </tableRule> <function name="sharding-by-month" class="org.opencloudb.route.function.PartitionByMonth"> <property name="dateFormat">yyyy-MM-dd</property> <property name="sBeginDate">2014-01-01</property> </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")); Assert.assertEquals(true, 0 == partition.calculate("2014-01-10")); Assert.assertEquals(true, 0 == partition.calculate("2014-01-31")); Assert.assertEquals(true, 1 == partition.calculate("2014-02-01")); Assert.assertEquals(true, 1 == partition.calculate("2014-02-28")); Assert.assertEquals(true, 2 == partition.calculate("2014-03-1")); Assert.assertEquals(true, 11 == partition.calculate("2014-12-31")); Assert.assertEquals(true, 12 == partition.calculate("2015-01-31")); Assert.assertEquals(true, 23 == partition.calculate("2015-12-31"));