diff --git a/pom.xml b/pom.xml index 67f2ee014..1e6ebc86d 100644 --- a/pom.xml +++ b/pom.xml @@ -89,7 +89,7 @@ junit junit - 4.4 + 4.13.2 provided diff --git a/src/main/java/io/mycat/route/function/PartitionByRangeDateHash.java b/src/main/java/io/mycat/route/function/PartitionByRangeDateHash.java index 62f456918..f43be9872 100644 --- a/src/main/java/io/mycat/route/function/PartitionByRangeDateHash.java +++ b/src/main/java/io/mycat/route/function/PartitionByRangeDateHash.java @@ -9,6 +9,8 @@ import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.TimeZone; + /** * 先根据日期分组,再根据时间hash使得短期内数据分布的更均匀 * 优点可以避免扩容时的数据迁移,又可以一定程度上避免范围分片的热点问题 @@ -29,6 +31,7 @@ public class PartitionByRangeDateHash extends AbstractPartitionAlgorithm impleme private long partionTime; private static final long oneDay = 86400000; + private static final TimeZone UTC = TimeZone.getTimeZone("UTC"); private String groupPartionSize; private int intGroupPartionSize; @@ -40,13 +43,16 @@ public void init() { try { - beginDate = new SimpleDateFormat(dateFormat).parse(sBeginDate) - .getTime(); + SimpleDateFormat sdfInit = new SimpleDateFormat(dateFormat); + sdfInit.setTimeZone(UTC); + beginDate = sdfInit.parse(sBeginDate).getTime(); intGroupPartionSize = Integer.parseInt(groupPartionSize); formatter = new ThreadLocal() { @Override protected SimpleDateFormat initialValue() { - return new SimpleDateFormat(dateFormat); + SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); + sdf.setTimeZone(UTC); + return sdf; } }; if (intGroupPartionSize <= 0) @@ -80,8 +86,7 @@ public Integer calculateStart(String columnValue) { try { - long targetTime = new SimpleDateFormat(dateFormat).parse( - columnValue).getTime(); + long targetTime = formatter.get().parse(columnValue).getTime(); int targetPartition = (int) ((targetTime - beginDate) / partionTime); return targetPartition * intGroupPartionSize; @@ -96,8 +101,7 @@ public Integer calculateEnd(String columnValue) { try { - long targetTime = new SimpleDateFormat(dateFormat).parse( - columnValue).getTime(); + long targetTime = formatter.get().parse(columnValue).getTime(); int targetPartition = (int) ((targetTime - beginDate) / partionTime); return (targetPartition+1) * intGroupPartionSize - 1; diff --git a/src/test/java/io/mycat/route/function/PartitionByRangeDateHashTest.java b/src/test/java/io/mycat/route/function/PartitionByRangeDateHashTest.java index 30ee5cee6..8dbacd84c 100644 --- a/src/test/java/io/mycat/route/function/PartitionByRangeDateHashTest.java +++ b/src/test/java/io/mycat/route/function/PartitionByRangeDateHashTest.java @@ -39,16 +39,16 @@ public void test() throws ParseException { partition.init(); Integer calculate = partition.calculate("2014-01-01 00:00:00"); - Assert.assertEquals(true, 3 == calculate); + Assert.assertEquals(true, 1 == calculate); calculate = partition.calculate("2014-01-01 00:00:01"); - Assert.assertEquals(true, 1 == calculate); + Assert.assertEquals(true, 0 == calculate); calculate = partition.calculate("2014-01-04 00:00:00"); - Assert.assertEquals(true, 7 == calculate); + Assert.assertEquals(true, 6 == calculate); calculate = partition.calculate("2014-01-04 00:00:01"); - Assert.assertEquals(true, 11== calculate); + Assert.assertEquals(true, 10 == calculate); Date beginDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-01-01 00:00:00");