OneSizeFitsQuorum commented on code in PR #15022:
URL: https://github.com/apache/iotdb/pull/15022#discussion_r1983135969


##########
iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/load/balancer/region/IDestNodeSelector.java:
##########
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.confignode.manager.load.balancer.region;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TDataNodeConfiguration;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+
+import java.util.List;
+import java.util.Map;
+
+public interface IDestNodeSelector {
+
+  TDataNodeConfiguration selectDestDataNode(
+      Map<Integer, TDataNodeConfiguration> availableDataNodeMap,

Review Comment:
   add java doc for this



##########
iotdb-core/confignode/src/test/java/org/apache/iotdb/confignode/manager/load/balancer/region/DestNodeSelectorTest.java:
##########
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.confignode.manager.load.balancer.region;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupType;
+import org.apache.iotdb.common.rpc.thrift.TDataNodeConfiguration;
+import org.apache.iotdb.common.rpc.thrift.TDataNodeLocation;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+
+import org.junit.BeforeClass;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+public class DestNodeSelectorTest {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(DestNodeSelectorTest.class);
+
+  private static final IDestNodeSelector SELECTOR = new 
PartiteGraphPlacementDestNodeSelector();
+  private static final IRegionGroupAllocator ALLOCATOR =
+      new PartiteGraphPlacementRegionGroupAllocator();
+
+  private static final TDataNodeLocation REMOVE_DATANODE_LOCATION =
+      new TDataNodeLocation().setDataNodeId(5);
+  private static final int TEST_DATA_NODE_NUM = 5;
+  private static final int DATA_REGION_PER_DATA_NODE = 4;
+  private static final int DATA_REPLICATION_FACTOR = 2;
+
+  private static final Map<Integer, TDataNodeConfiguration> 
AVAILABLE_DATA_NODE_MAP =
+      new HashMap<>();
+  private static final Map<Integer, Double> FREE_SPACE_MAP = new HashMap<>();
+
+  @BeforeClass
+  public static void setUp() {
+    // Construct TEST_DATA_NODE_NUM DataNodes
+    for (int i = 1; i <= TEST_DATA_NODE_NUM; i++) {
+      AVAILABLE_DATA_NODE_MAP.put(
+          i, new TDataNodeConfiguration().setLocation(new 
TDataNodeLocation().setDataNodeId(i)));
+      FREE_SPACE_MAP.put(i, Math.random());
+    }
+  }
+
+  // manual test only
+  public void testSelectDestNode() {
+    final int dataRegionGroupNum =
+        DATA_REGION_PER_DATA_NODE * TEST_DATA_NODE_NUM / 
DATA_REPLICATION_FACTOR;
+
+    List<TRegionReplicaSet> allocateResult = new ArrayList<>();
+    for (int index = 0; index < dataRegionGroupNum; index++) {
+      allocateResult.add(
+          ALLOCATOR.generateOptimalRegionReplicasDistribution(
+              AVAILABLE_DATA_NODE_MAP,
+              FREE_SPACE_MAP,
+              allocateResult,
+              allocateResult,
+              DATA_REPLICATION_FACTOR,
+              new TConsensusGroupId(TConsensusGroupType.DataRegion, index)));
+    }
+    List<TRegionReplicaSet> migratedReplicas =
+        allocateResult.stream()
+            .filter(
+                replicaSet -> 
replicaSet.getDataNodeLocations().contains(REMOVE_DATANODE_LOCATION))
+            .collect(Collectors.toList());
+
+    AVAILABLE_DATA_NODE_MAP.remove(REMOVE_DATANODE_LOCATION.getDataNodeId());
+    FREE_SPACE_MAP.remove(REMOVE_DATANODE_LOCATION.getDataNodeId());
+
+    List<TRegionReplicaSet> remainReplicas = new ArrayList<>();
+
+    for (TRegionReplicaSet replicaSet : migratedReplicas) {
+      List<TDataNodeLocation> dataNodeLocations = 
replicaSet.getDataNodeLocations();
+      allocateResult.remove(replicaSet);
+      dataNodeLocations.remove(REMOVE_DATANODE_LOCATION);
+      replicaSet.setDataNodeLocations(dataNodeLocations);
+      allocateResult.add(replicaSet);
+      remainReplicas.add(replicaSet);
+    }
+
+    for (TRegionReplicaSet remainReplicaSet : remainReplicas) {
+      TDataNodeConfiguration selectedNode =
+          SELECTOR.selectDestDataNode(

Review Comment:
   Should you compare all nodes for load balance, divergence, etc.?
   
   Just like GreedyCopySetRegionGroupAllocatorTest?



##########
iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/load/balancer/region/PartiteGraphPlacementDestNodeSelector.java:
##########
@@ -0,0 +1,185 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.confignode.manager.load.balancer.region;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupType;
+import org.apache.iotdb.common.rpc.thrift.TDataNodeConfiguration;
+import org.apache.iotdb.common.rpc.thrift.TDataNodeLocation;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.confignode.conf.ConfigNodeDescriptor;
+import 
org.apache.iotdb.confignode.manager.load.balancer.region.GreedyRegionGroupAllocator.DataNodeEntry;
+
+import org.apache.tsfile.utils.Pair;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.stream.Collectors;
+
+public class PartiteGraphPlacementDestNodeSelector implements 
IDestNodeSelector {
+  private int replicationFactor;
+  private int regionPerDataNode;
+
+  private int dataNodeNum;
+  // The number of allocated Regions in each DataNode
+  private int[] regionCounter;
+  // The number of edges in current cluster
+  private int[][] combinationCounter;
+  private Map<Integer, Integer> fakeToRealIdMap;
+
+  private Set<Integer> remainDataNodesFakeId;
+
+  // Pair<combinationSum, RegionSum>
+  Pair<Integer, Integer> bestValue;
+
+  private Integer targetDataNode = -1;
+
+  @Override
+  public TDataNodeConfiguration selectDestDataNode(
+      Map<Integer, TDataNodeConfiguration> availableDataNodeMap,
+      Map<Integer, Double> freeDiskSpaceMap,
+      List<TRegionReplicaSet> allocatedRegionGroups,
+      List<TRegionReplicaSet> databaseAllocatedRegionGroups,
+      int replicationFactor,
+      TConsensusGroupId consensusGroupId,
+      TRegionReplicaSet remainReplicaSet) {
+    this.regionPerDataNode =
+        consensusGroupId.getType().equals(TConsensusGroupType.DataRegion)
+            ? 
ConfigNodeDescriptor.getInstance().getConf().getDataRegionPerDataNode()
+            : 
ConfigNodeDescriptor.getInstance().getConf().getSchemaRegionPerDataNode();
+    prepare(replicationFactor, availableDataNodeMap, allocatedRegionGroups, 
remainReplicaSet);
+
+    searchTargetDataNode(freeDiskSpaceMap);
+
+    if (targetDataNode == -1) {
+      Set<Integer> alternativeDataNodes =
+          fakeToRealIdMap.keySet().stream()
+              .filter(fakeId -> !remainDataNodesFakeId.contains(fakeId))
+              .collect(Collectors.toSet());
+      return 
availableDataNodeMap.get(alternativeDataNodes.stream().findAny().orElse(null));

Review Comment:
   What's the meaning of get(null) ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to