diff --git a/integration-test/src/test/java/org/apache/iotdb/confignode/it/regionmigration/pass/commit/IoTDBRegionReconstructForIoTV1IT.java b/integration-test/src/test/java/org/apache/iotdb/confignode/it/regionmigration/pass/commit/IoTDBRegionReconstructForIoTV1IT.java index 8db81eed1af6..08a4db981382 100644 --- a/integration-test/src/test/java/org/apache/iotdb/confignode/it/regionmigration/pass/commit/IoTDBRegionReconstructForIoTV1IT.java +++ b/integration-test/src/test/java/org/apache/iotdb/confignode/it/regionmigration/pass/commit/IoTDBRegionReconstructForIoTV1IT.java @@ -103,12 +103,13 @@ public void normal1C3DTest() throws Exception { session.open(); // delete one DataNode's data dir, stop another DataNode - FileUtils.deleteDirectory( + File dataDirToBeReconstructed = new File( EnvFactory.getEnv() .dataNodeIdToWrapper(dataNodeToBeReconstructed) .get() - .getDataPath())); + .getDataPath()); + FileUtils.deleteDirectory(dataDirToBeReconstructed); EnvFactory.getEnv().dataNodeIdToWrapper(dataNodeToBeClosed).get().stopForcibly(); // now, the query should throw exception @@ -121,11 +122,13 @@ public void normal1C3DTest() throws Exception { EnvFactory.getAbstractEnv().checkNodeInStatus(dataNodeToBeClosed, NodeStatus.Running); session.executeNonQueryStatement( String.format(RECONSTRUCT_FORMAT, selectedRegion, dataNodeToBeReconstructed)); - Thread.sleep(5000); Awaitility.await() .pollInterval(1, TimeUnit.SECONDS) .atMost(1, TimeUnit.MINUTES) - .until(() -> getRegionStatusWithoutRunning(session).isEmpty()); + .until( + () -> + getRegionStatusWithoutRunning(session).isEmpty() + && dataDirToBeReconstructed.exists()); EnvFactory.getEnv().dataNodeIdToWrapper(dataNodeToBeClosed).get().stopForcibly(); // now, the query should work fine diff --git a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/env/RegionMaintainHandler.java b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/env/RegionMaintainHandler.java index 92415ec72c9b..2ec8a85acc17 100644 --- a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/env/RegionMaintainHandler.java +++ b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/env/RegionMaintainHandler.java @@ -577,12 +577,19 @@ public Optional filterDataNodeWithOtherRegionReplica( configManager.getNodeManager().filterDataNodeThroughStatus(allowingStatus).stream() .map(TDataNodeConfiguration::getLocation) .collect(Collectors.toList()); + final int leaderId = configManager.getLoadManager().getRegionLeaderMap().get(regionId); Collections.shuffle(aliveDataNodes); + Optional bestChoice = Optional.empty(); for (TDataNodeLocation aliveDataNode : aliveDataNodes) { if (regionLocations.contains(aliveDataNode) && !excludeLocations.contains(aliveDataNode)) { - return Optional.of(aliveDataNode); + if (leaderId == aliveDataNode.getDataNodeId()) { + bestChoice = Optional.of(aliveDataNode); + break; + } else if (!bestChoice.isPresent()) { + bestChoice = Optional.of(aliveDataNode); + } } } - return Optional.empty(); + return bestChoice; } }