From 67b86884013f7545d762df598cbff3c4bda0e07a Mon Sep 17 00:00:00 2001 From: H-Jason <953986800@qq.com> Date: Thu, 6 Jun 2024 00:31:45 +0800 Subject: [PATCH] fix dead lock in multi DB when using batchUpdate(#31038) --- .../prepare/AbstractExecutionPrepareEngine.java | 4 ++-- .../jdbc/commons/TestShardingService.java | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/AbstractExecutionPrepareEngine.java b/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/AbstractExecutionPrepareEngine.java index eea1fec1043b5..7519f521863b1 100644 --- a/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/AbstractExecutionPrepareEngine.java +++ b/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/AbstractExecutionPrepareEngine.java @@ -30,11 +30,11 @@ import java.sql.SQLException; import java.util.Collection; import java.util.Collections; -import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.TreeMap; /** * Abstract execution prepare engine. @@ -80,7 +80,7 @@ private List> group(final List sqlUnits) { protected abstract List> group(String dataSourceName, int connectionOffset, List> executionUnitGroups, ConnectionMode connectionMode) throws SQLException; private Map> aggregateExecutionUnitGroups(final Collection executionUnits) { - Map> result = new LinkedHashMap<>(executionUnits.size(), 1F); + Map> result = new TreeMap<>(); for (ExecutionUnit each : executionUnits) { result.computeIfAbsent(each.getDataSourceName(), unused -> new LinkedList<>()).add(each); } diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/TestShardingService.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/TestShardingService.java index 8e577b480b3f1..02f6906ce883b 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/TestShardingService.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/TestShardingService.java @@ -63,16 +63,16 @@ public void processSuccess() throws SQLException { final Collection orderIds = insertData(); Collection orders = orderRepository.selectAll(); assertThat(orders.stream().map(Order::getOrderType).collect(Collectors.toList()), - equalTo(Arrays.asList(1, 1, 1, 1, 1, 0, 0, 0, 0, 0))); + equalTo(Arrays.asList(0, 0, 0, 0, 0, 1, 1, 1, 1, 1))); assertThat(orders.stream().map(Order::getUserId).collect(Collectors.toList()), - equalTo(new ArrayList<>(Arrays.asList(1, 3, 5, 7, 9, 2, 4, 6, 8, 10)))); + equalTo(new ArrayList<>(Arrays.asList(2, 4, 6, 8, 10, 1, 3, 5, 7, 9)))); assertThat(orders.stream().map(Order::getAddressId).collect(Collectors.toList()), - equalTo(new ArrayList<>(Arrays.asList(1L, 3L, 5L, 7L, 9L, 2L, 4L, 6L, 8L, 10L)))); + equalTo(new ArrayList<>(Arrays.asList(2L, 4L, 6L, 8L, 10L, 1L, 3L, 5L, 7L, 9L)))); assertThat(orders.stream().map(Order::getStatus).collect(Collectors.toList()), equalTo(IntStream.range(1, 11).mapToObj(i -> "INSERT_TEST").collect(Collectors.toList()))); Collection orderItems = orderItemRepository.selectAll(); assertThat(orderItems.stream().map(OrderItem::getUserId).collect(Collectors.toList()), - equalTo(new ArrayList<>(Arrays.asList(1, 3, 5, 7, 9, 2, 4, 6, 8, 10)))); + equalTo(new ArrayList<>(Arrays.asList(2, 4, 6, 8, 10, 1, 3, 5, 7, 9)))); assertThat(orderItems.stream().map(OrderItem::getPhone).collect(Collectors.toList()), equalTo(IntStream.range(1, 11).mapToObj(i -> "13800000001").collect(Collectors.toList()))); assertThat(orderItems.stream().map(OrderItem::getStatus).collect(Collectors.toList()), @@ -98,16 +98,16 @@ public void processSuccessInClickHouse() throws SQLException { assertThat(orderIds, notNullValue()); Collection orders = orderRepository.selectAll(); assertThat(orders.stream().map(Order::getOrderType).collect(Collectors.toList()), - equalTo(Arrays.asList(1, 1, 1, 1, 1, 0, 0, 0, 0, 0))); + equalTo(Arrays.asList(0, 0, 0, 0, 0, 1, 1, 1, 1, 1))); assertThat(orders.stream().map(Order::getUserId).collect(Collectors.toList()), - equalTo(new ArrayList<>(Arrays.asList(1, 3, 5, 7, 9, 2, 4, 6, 8, 10)))); + equalTo(new ArrayList<>(Arrays.asList(2, 4, 6, 8, 10, 1, 3, 5, 7, 9)))); assertThat(orders.stream().map(Order::getAddressId).collect(Collectors.toList()), - equalTo(new ArrayList<>(Arrays.asList(1L, 3L, 5L, 7L, 9L, 2L, 4L, 6L, 8L, 10L)))); + equalTo(new ArrayList<>(Arrays.asList(2L, 4L, 6L, 8L, 10L, 1L, 3L, 5L, 7L, 9L)))); assertThat(orders.stream().map(Order::getStatus).collect(Collectors.toList()), equalTo(IntStream.range(1, 11).mapToObj(i -> "INSERT_TEST").collect(Collectors.toList()))); Collection orderItems = orderItemRepository.selectAll(); assertThat(orderItems.stream().map(OrderItem::getUserId).collect(Collectors.toList()), - equalTo(new ArrayList<>(Arrays.asList(1, 3, 5, 7, 9, 2, 4, 6, 8, 10)))); + equalTo(new ArrayList<>(Arrays.asList(2, 4, 6, 8, 10, 1, 3, 5, 7, 9)))); assertThat(orderItems.stream().map(OrderItem::getPhone).collect(Collectors.toList()), equalTo(IntStream.range(1, 11).mapToObj(i -> "13800000001").collect(Collectors.toList()))); assertThat(orderItems.stream().map(OrderItem::getStatus).collect(Collectors.toList()),