Skip to content

Commit 9d47468

Browse files
Enhance "remove region" sql to handle the case of DataNode not exist (apache#15728)
1 parent 369971c commit 9d47468

File tree

4 files changed

+44
-16
lines changed

4 files changed

+44
-16
lines changed

iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/ProcedureManager.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.iotdb.common.rpc.thrift.TConsensusGroupType;
2525
import org.apache.iotdb.common.rpc.thrift.TDataNodeConfiguration;
2626
import org.apache.iotdb.common.rpc.thrift.TDataNodeLocation;
27+
import org.apache.iotdb.common.rpc.thrift.TEndPoint;
2728
import org.apache.iotdb.common.rpc.thrift.TSStatus;
2829
import org.apache.iotdb.commons.cluster.NodeStatus;
2930
import org.apache.iotdb.commons.conf.CommonConfig;
@@ -161,6 +162,7 @@
161162
import org.slf4j.LoggerFactory;
162163

163164
import javax.annotation.Nonnull;
165+
import javax.annotation.Nullable;
164166

165167
import java.io.IOException;
166168
import java.nio.ByteBuffer;
@@ -837,15 +839,13 @@ private TSStatus checkExtendRegion(
837839
private TSStatus checkRemoveRegion(
838840
TRemoveRegionReq req,
839841
TConsensusGroupId regionId,
840-
TDataNodeLocation targetDataNode,
842+
@Nullable TDataNodeLocation targetDataNode,
841843
TDataNodeLocation coordinator) {
842844
String failMessage =
843845
regionOperationCommonCheck(
844846
regionId,
845847
targetDataNode,
846-
Arrays.asList(
847-
new Pair<>("Target DataNode", targetDataNode),
848-
new Pair<>("Coordinator", coordinator)),
848+
Arrays.asList(new Pair<>("Coordinator", coordinator)),
849849
req.getModel());
850850

851851
if (configManager
@@ -855,11 +855,12 @@ private TSStatus checkRemoveRegion(
855855
.getDataNodeLocationsSize()
856856
== 1) {
857857
failMessage = String.format("%s only has 1 replica, it cannot be removed", regionId);
858-
} else if (configManager
859-
.getPartitionManager()
860-
.getAllReplicaSets(targetDataNode.getDataNodeId())
861-
.stream()
862-
.noneMatch(replicaSet -> replicaSet.getRegionId().equals(regionId))) {
858+
} else if (targetDataNode != null
859+
&& configManager
860+
.getPartitionManager()
861+
.getAllReplicaSets(targetDataNode.getDataNodeId())
862+
.stream()
863+
.noneMatch(replicaSet -> replicaSet.getRegionId().equals(regionId))) {
863864
failMessage =
864865
String.format(
865866
"Target DataNode %s doesn't contain Region %s", req.getDataNodeId(), regionId);
@@ -1208,6 +1209,23 @@ public TSStatus removeRegion(TRemoveRegionReq req) {
12081209
return status;
12091210
}
12101211

1212+
// SPECIAL CASE
1213+
if (targetDataNode == null) {
1214+
// If targetDataNode is null, it means the target DataNode does not exist in the
1215+
// NodeManager.
1216+
// In this case, simply clean up the partition table once and do nothing else.
1217+
LOGGER.warn(
1218+
"Remove region: Target DataNode {} not found, will simply clean up the partition table of region {} and do nothing else.",
1219+
req.getDataNodeId(),
1220+
req.getRegionId());
1221+
this.executor
1222+
.getEnvironment()
1223+
.getRegionMaintainHandler()
1224+
.removeRegionLocation(
1225+
regionId, buildFakeDataNodeLocation(req.getDataNodeId(), "FakeIpForRemoveRegion"));
1226+
return new TSStatus(TSStatusCode.SUCCESS_STATUS.getStatusCode());
1227+
}
1228+
12111229
// submit procedure
12121230
RemoveRegionPeerProcedure procedure =
12131231
new RemoveRegionPeerProcedure(regionId, coordinator, targetDataNode);
@@ -1219,6 +1237,12 @@ public TSStatus removeRegion(TRemoveRegionReq req) {
12191237
}
12201238
}
12211239

1240+
private static TDataNodeLocation buildFakeDataNodeLocation(int dataNodeId, String message) {
1241+
TEndPoint fakeEndPoint = new TEndPoint(message, -1);
1242+
return new TDataNodeLocation(
1243+
dataNodeId, fakeEndPoint, fakeEndPoint, fakeEndPoint, fakeEndPoint, fakeEndPoint);
1244+
}
1245+
12221246
// endregion
12231247

12241248
/**

iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/partition/DatabasePartitionTable.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ void addRegionNewLocation(TConsensusGroupId regionId, TDataNodeLocation node) {
539539
regionGroup.addRegionLocation(node);
540540
}
541541

542-
void removeRegionLocation(TConsensusGroupId regionId, TDataNodeLocation node) {
542+
void removeRegionLocation(TConsensusGroupId regionId, int nodeId) {
543543
RegionGroup regionGroup = regionGroupMap.get(regionId);
544544
if (regionGroup == null) {
545545
LOGGER.warn(
@@ -548,16 +548,18 @@ void removeRegionLocation(TConsensusGroupId regionId, TDataNodeLocation node) {
548548
databaseName);
549549
return;
550550
}
551-
if (!regionGroup.getReplicaSet().getDataNodeLocations().contains(node)) {
551+
if (regionGroup.getReplicaSet().getDataNodeLocations().stream()
552+
.map(TDataNodeLocation::getDataNodeId)
553+
.noneMatch(id -> id == nodeId)) {
552554
LOGGER.info(
553555
"Node is not in region locations when removeRegionOldLocation in {}, "
554556
+ "no need to remove it, node: {}, region: {}",
555557
databaseName,
556-
node,
558+
nodeId,
557559
regionId);
558560
return;
559561
}
560-
regionGroup.removeRegionLocation(node);
562+
regionGroup.removeRegionLocation(nodeId);
561563
}
562564

563565
/**

iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/partition/PartitionInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ public TSStatus removeRegionLocation(RemoveRegionLocationPlan req) {
620620
.forEach(
621621
databasePartitionTable ->
622622
databasePartitionTable.removeRegionLocation(
623-
req.getRegionId(), req.getDeprecatedLocation()));
623+
req.getRegionId(), req.getDeprecatedLocation().getDataNodeId()));
624624
return new TSStatus(TSStatusCode.SUCCESS_STATUS.getStatusCode());
625625
}
626626

iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/partition/RegionGroup.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,10 @@ public synchronized void addRegionLocation(TDataNodeLocation node) {
9595
replicaSet.getDataNodeLocations().sort(TDataNodeLocation::compareTo);
9696
}
9797

98-
public synchronized void removeRegionLocation(TDataNodeLocation node) {
99-
replicaSet.getDataNodeLocations().remove(node);
98+
public synchronized void removeRegionLocation(int nodeId) {
99+
replicaSet
100+
.getDataNodeLocations()
101+
.removeIf(tDataNodeLocation -> nodeId == tDataNodeLocation.getDataNodeId());
100102
replicaSet.getDataNodeLocations().sort(TDataNodeLocation::compareTo);
101103
}
102104

0 commit comments

Comments
 (0)