Skip to content

Commit 346ee72

Browse files
authored
[remove datanode] Fix ArrayIndexOutOfBoundsException in computeInitialDbLoad (apache#15718)
1 parent 9d47468 commit 346ee72

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/load/balancer/region/GreedyCopySetRegionGroupAllocator.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public Map<TConsensusGroupId, TDataNodeConfiguration> removeNodeReplicaSelect(
155155
// 1. prepare: compute regionCounter, databaseRegionCounter, and combinationCounter
156156

157157
prepare(availableDataNodeMap, allocatedRegionGroups, Collections.emptyList());
158-
computeInitialDbLoad(databaseAllocatedRegionGroupMap);
158+
computeInitialDbLoad(availableDataNodeMap, databaseAllocatedRegionGroupMap);
159159

160160
// 2. Build allowed candidate set for each region that needs to be migrated.
161161
// For each region in remainReplicasMap, the candidate destination nodes are all nodes in
@@ -432,9 +432,11 @@ private int[] getCurrentMetrics(
432432
/**
433433
* Compute the initial load for each database on each data node.
434434
*
435+
* @param availableDataNodeMap currently available DataNodes, ensure size() >= replicationFactor
435436
* @param databaseAllocatedRegionGroupMap Mapping of each database to its list of replica sets.
436437
*/
437438
private void computeInitialDbLoad(
439+
Map<Integer, TDataNodeConfiguration> availableDataNodeMap,
438440
Map<String, List<TRegionReplicaSet>> databaseAllocatedRegionGroupMap) {
439441
initialDbLoad = new HashMap<>();
440442

@@ -446,7 +448,9 @@ private void computeInitialDbLoad(
446448
for (TRegionReplicaSet replicaSet : replicaSets) {
447449
for (TDataNodeLocation location : replicaSet.getDataNodeLocations()) {
448450
int nodeId = location.getDataNodeId();
449-
load[nodeId]++;
451+
if (availableDataNodeMap.containsKey(nodeId)) {
452+
load[nodeId]++;
453+
}
450454
}
451455
}
452456
initialDbLoad.put(database, load);

iotdb-core/confignode/src/test/java/org/apache/iotdb/confignode/manager/load/balancer/region/GreedyCopySetRemoveNodeReplicaSelectTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,20 @@ public void testSelectDestNode() {
8181
DATA_REGION_PER_DATA_NODE * TEST_DATA_NODE_NUM / DATA_REPLICATION_FACTOR;
8282

8383
List<TRegionReplicaSet> allocateResult = new ArrayList<>();
84+
List<TRegionReplicaSet> databaseAllocateResult = new ArrayList<>();
8485
for (int index = 0; index < dataRegionGroupNum; index++) {
85-
allocateResult.add(
86+
TRegionReplicaSet replicaSet =
8687
GCR_ALLOCATOR.generateOptimalRegionReplicasDistribution(
8788
AVAILABLE_DATA_NODE_MAP,
8889
FREE_SPACE_MAP,
8990
allocateResult,
9091
allocateResult,
9192
DATA_REPLICATION_FACTOR,
92-
new TConsensusGroupId(TConsensusGroupType.DataRegion, index)));
93+
new TConsensusGroupId(TConsensusGroupType.DataRegion, index));
94+
TRegionReplicaSet replicaSetCopy = new TRegionReplicaSet(replicaSet);
95+
96+
allocateResult.add(replicaSet);
97+
databaseAllocateResult.add(replicaSetCopy);
9398
}
9499

95100
List<TRegionReplicaSet> migratedReplicas =
@@ -158,7 +163,7 @@ public void testSelectDestNode() {
158163
}
159164
Map<TConsensusGroupId, TRegionReplicaSet> remainReplicasMap = new HashMap<>();
160165
Map<String, List<TRegionReplicaSet>> databaseAllocatedRegionGroupMap = new HashMap<>();
161-
databaseAllocatedRegionGroupMap.put("database", allocateResult);
166+
databaseAllocatedRegionGroupMap.put("database", databaseAllocateResult);
162167

163168
for (TRegionReplicaSet remainReplicaSet : remainReplicas) {
164169
remainReplicasMap.put(remainReplicaSet.getRegionId(), remainReplicaSet);

0 commit comments

Comments
 (0)