未验证 提交 de385f34 编写于 作者: J Juan Pan(Trista) 提交者: GitHub

Handle the out of bounds case for DatetimeShardingAlgorithm (#5996)

上级 7aeded68
......@@ -99,9 +99,9 @@ public final class DatetimeShardingAlgorithm implements StandardShardingAlgorith
}
private int doSharding(final long shardingValue) {
DecimalFormat df = new DecimalFormat("0.00");
String position = df.format((float) shardingValue / getPartitionValue());
return Math.max(0, (int) Math.ceil(Float.parseFloat(position)));
DecimalFormat decimalFormat = new DecimalFormat("0.00");
String position = decimalFormat.format((float) shardingValue / getPartitionValue());
return Math.min(Math.max(0, (int) Math.ceil(Float.parseFloat(position))), autoTablesAmount - 1);
}
private int getFirstPartition(final Range<Comparable<?>> valueRange) {
......
......@@ -71,6 +71,16 @@ public final class DatetimeShardingAlgorithmTest {
assertTrue(actual.contains("t_order_0"));
assertTrue(actual.contains("t_order_1"));
}
@Test
public void assertPreciseDoShardingBeyondTheLastOne() {
List<String> availableTargetNames = Lists.newArrayList("t_order_0", "t_order_1", "t_order_2", "t_order_3", "t_order_4", "t_order_5");
List<RouteValue> shardingValues = Lists.newArrayList(new ListRouteValue<>("create_time", "t_order",
Lists.newArrayList("2021-01-01 00:00:02")));
Collection<String> actual = shardingStrategy.doSharding(availableTargetNames, shardingValues, new ConfigurationProperties(new Properties()));
assertThat(actual.size(), is(1));
assertTrue(actual.contains("t_order_5"));
}
@Test
public void assertRangeDoShardingWithAllRange() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册