rule.xml 配置如下
<function name="partition-by-month-history" class="io.mycat.route.function.PartitionByMonthAndHistory">
<property name="dateFormat">yyyy-MM-dd</property>
<property name="sBeginDate">2019-01-01</property>
<property name="sEndDate">2019-12-31</property>
</function>
首先看先init:
public void init() {
try {
beginDate = Calendar.getInstance();
beginDate.setTime(new SimpleDateFormat(dateFormat)
.parse(sBeginDate));
formatter = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(dateFormat);
}
};
if(sEndDate!=null&&!sEndDate.equals("")) {
endDate = Calendar.getInstance();
endDate.setTime(new SimpleDateFormat(dateFormat).parse(sEndDate));
nPartition = ((endDate.get(Calendar.YEAR) - beginDate.get(Calendar.YEAR)) * 12
+ endDate.get(Calendar.MONTH) - beginDate.get(Calendar.MONTH)) + 1;
if (nPartition <= 0) {
throw new java.lang.IllegalArgumentException("Incorrect time range for month partitioning!");
}
} else {
nPartition = -1;
}
} catch (ParseException e) {
throw new java.lang.IllegalArgumentException(e);
}
}
通过init方法可以看出,rule.xml的配置 sBeginDate 及 sEndDate有以下规则:
- sBeginDate 需要早于 sEndDate,否则计算出的 nPartition <= 0,会抛出IllegalArgumentException;
- 如果sBeginDate 和 sEndDate 同时存在,且 sBeginDate < sEndDate, 则nPartition等于 beginDate 到 endDate 跨越的月份数;
- sBeginDate 必须配置,否则在Mycat启动时抛出异常:“io.mycat.config.util.ConfigException: java.lang.NullPointerException”;
- sEndDate 可以不指定,不指定的话 nPartition 为 -1;
那个该算法是如果计算分片的呢,先看下涉及到的代码:
private int reCalculatePartition(int targetPartition) {
/**
* If target date is previous of start time of partition setting, shift
* the delta range between target and start date to be positive value
*/
if (targetPartition < 0) {
targetPartition = nPartition - (-targetPartition) % nPartition;
}
if (targetPartition >= nPartition) {
targetPartition = targetPartition % nPartition;
}
LOGGER.debug("partition is:" + targetPartition);
return targetPartition;
}
@Override
public Integer calculate(String columnValue) {
try {
int targetPartition;
Calendar curTime = Calendar.getInstance();
curTime.setTime(formatter.get().parse(columnValue));
targetPartition = ((curTime.get(Calendar.YEAR) - beginDate.get(Calendar.YEAR))
* 12 + curTime.get(Calendar.MONTH)
- beginDate.get(Calendar.MONTH));
/**
* For circulatory partition, calculated value of target partition needs to be
* rotated to fit the partition range
*/
if (nPartition > 0) {
targetPartition = reCalculatePartition(targetPartition);
}
return targetPartition;
} catch (ParseException e) {
throw new IllegalArgumentException(new StringBuilder().append("columnValue:").append(columnValue).append(" Please check if the format satisfied.").toString(),e);
}
}
根据calculate方法,逻辑过程大致如下:
- 计算columnValue的日期值与 sBeginDate 间隔的月份数targetPartition;
- 如果init得到的 nPartition > 0,则调用reCalculatePartition 方法计算分片索引,reCalculatePartition简单的说就是:
·当columnValue < beginDate,targetPartition < 0,返回:nPartition - (-targetPartition) % nPartition;
·当columnValue 介于 beginDate 和 endDate 之间,直接返回 targetPartition;
·当columnValue > endDate,取模,返回:targetPartition % nPartition; - 如果init 得到的nPartition <= 0,则直接返回targetPartition作为分片索引;
根据 init 和 calculate 方法可以看出,rule.xml配置sBeginDate和sEndDate时需要注意:
- sBeginDate 和 sEndDate 跨越的月份数应该是分配的dataNode个数,否则插入插入数据可能抛出异常“SQLNonTransientException: Can't find a valid data node for specified node index”。;
- 如果sEndDate没有配置,则 columnValue 和 sBeginDate 跨越的月份不能超过dataNode个数,否则会抛出抛出异常“SQLNonTransientException: Can't find a valid data node for specified node index”。;