MyCat 分片算法 PartitionByMonthAndHistory

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方法,逻辑过程大致如下:

  1. 计算columnValue的日期值与 sBeginDate 间隔的月份数targetPartition;
  2. 如果init得到的 nPartition > 0,则调用reCalculatePartition 方法计算分片索引,reCalculatePartition简单的说就是:
    ·当columnValue < beginDate,targetPartition < 0,返回:nPartition - (-targetPartition) % nPartition;
    ·当columnValue 介于 beginDate 和 endDate 之间,直接返回 targetPartition;
    ·当columnValue > endDate,取模,返回:targetPartition % nPartition;
  3. 如果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”。;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wyr2018

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值