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


##########
iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/load/balancer/region/GreedyCopySetRegionGroupAllocator.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * 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.TDataNodeLocation;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/** Allocate Region through Greedy and CopySet Algorithm. */
+public class GreedyCopySetRegionGroupAllocator implements 
IRegionGroupAllocator {
+
+  int replicationFactor;
+  // RegionGroup allocation BitSet
+  private List<BitSet> allocatedBitSets;
+  // Map<DataNodeId, RegionGroup count>
+  private Map<Integer, AtomicInteger> regionCounter;
+  // Available DataNodeIds
+  private Integer[] dataNodeIds;
+
+  // First Key: the sum of Regions at the DataNodes in the allocation result 
is minimal
+  int optimalRegionSum;
+  // Second Key: the sum of intersected Regions with other allocated 
RegionGroups is minimal
+  int optimalIntersectionSum;
+  List<Integer[]> optimalReplicaSets;
+
+  public GreedyCopySetRegionGroupAllocator() {
+    // Empty constructor
+  }
+
+  @Override
+  public TRegionReplicaSet generateOptimalRegionReplicasDistribution(
+      Map<Integer, TDataNodeConfiguration> availableDataNodeMap,
+      Map<Integer, Double> freeDiskSpaceMap,
+      List<TRegionReplicaSet> allocatedRegionGroups,
+      int replicationFactor,
+      TConsensusGroupId consensusGroupId) {
+
+    prepare(replicationFactor, availableDataNodeMap, allocatedRegionGroups);
+    dfs(-1, 0, new Integer[replicationFactor], 0, 0);
+
+    // Randomly pick one optimal plan as result
+    Collections.shuffle(optimalReplicaSets);
+    Integer[] optimalReplicaSet = optimalReplicaSets.get(0);
+    TRegionReplicaSet result = new TRegionReplicaSet();
+    result.setRegionId(consensusGroupId);
+    for (int i = 0; i < replicationFactor; i++) {
+      
result.addToDataNodeLocations(availableDataNodeMap.get(optimalReplicaSet[i]).getLocation());
+    }
+    return result;
+  }
+
+  /**
+   * Prepare some statistics before dfs.
+   *
+   * @param replicationFactor replication factor in the cluster
+   * @param availableDataNodeMap currently available DataNodes, ensure size() 
>= replicationFactor
+   * @param allocatedRegionGroups already allocated RegionGroups in the cluster
+   */
+  private void prepare(
+      int replicationFactor,
+      Map<Integer, TDataNodeConfiguration> availableDataNodeMap,
+      List<TRegionReplicaSet> allocatedRegionGroups) {
+
+    this.replicationFactor = replicationFactor;
+    int maxDataNodeId = 
availableDataNodeMap.keySet().stream().max(Integer::compareTo).orElse(0);
+    for (TRegionReplicaSet regionReplicaSet : allocatedRegionGroups) {
+      for (TDataNodeLocation dataNodeLocation : 
regionReplicaSet.getDataNodeLocations()) {
+        // Store the maximum DataNodeId in this algorithm loop
+        maxDataNodeId = Math.max(maxDataNodeId, 
dataNodeLocation.getDataNodeId());
+      }
+    }
+
+    // Convert the allocatedRegionGroups into allocatedBitSets,
+    // where a true in BitSet corresponding to a DataNodeId in the RegionGroup
+    allocatedBitSets = new ArrayList<>();

Review Comment:
   allocatedRegionGroups.size()



##########
iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/load/balancer/region/GreedyCopySetRegionGroupAllocator.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * 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.TDataNodeLocation;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/** Allocate Region through Greedy and CopySet Algorithm. */
+public class GreedyCopySetRegionGroupAllocator implements 
IRegionGroupAllocator {
+
+  int replicationFactor;
+  // RegionGroup allocation BitSet
+  private List<BitSet> allocatedBitSets;

Review Comment:
   Is it necessary for us to clear these elements after each computation to 
allow GC instead of waiting until the next function call to clean them up



##########
iotdb-core/confignode/src/test/java/org/apache/iotdb/confignode/manager/load/balancer/region/GreedyCopySetRegionGroupAllocatorTest.java:
##########
@@ -0,0 +1,209 @@
+/*
+ * 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.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.BitSet;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+import java.util.concurrent.atomic.AtomicInteger;
+
+public class GreedyCopySetRegionGroupAllocatorTest {
+
+  private static final Logger LOGGER =
+      LoggerFactory.getLogger(GreedyCopySetRegionGroupAllocatorTest.class);
+
+  private static final GreedyRegionGroupAllocator GREEDY_ALLOCATOR =
+      new GreedyRegionGroupAllocator();
+  private static final GreedyCopySetRegionGroupAllocator 
GREEDY_COPY_SET_ALLOCATOR =
+      new GreedyCopySetRegionGroupAllocator();
+
+  private static final int TEST_DATA_NODE_NUM = 21;
+  private static final int DATA_REGION_PER_DATA_NODE =
+      (int) 
ConfigNodeDescriptor.getInstance().getConf().getDataRegionPerDataNode();
+  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 21 DataNodes
+    Random random = new Random();
+    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, random.nextDouble());
+    }
+  }
+
+  @Test
+  public void test2Factor() {
+    testRegionDistributionAndScatterWidth(2);
+  }
+
+  @Test
+  public void test3Factor() {
+    testRegionDistributionAndScatterWidth(3);
+  }
+
+  private void testRegionDistributionAndScatterWidth(int replicationFactor) {
+    final int dataRegionGroupNum =
+        DATA_REGION_PER_DATA_NODE * TEST_DATA_NODE_NUM / replicationFactor;
+
+    /* Allocate DataRegionGroups */
+    List<TRegionReplicaSet> greedyResult = new ArrayList<>();
+    List<TRegionReplicaSet> greedyCopySetResult = new ArrayList<>();
+    for (int index = 0; index < dataRegionGroupNum; index++) {
+      greedyResult.add(
+          GREEDY_ALLOCATOR.generateOptimalRegionReplicasDistribution(
+              AVAILABLE_DATA_NODE_MAP,
+              FREE_SPACE_MAP,
+              greedyResult,
+              replicationFactor,
+              new TConsensusGroupId(TConsensusGroupType.DataRegion, index)));
+      greedyCopySetResult.add(
+          GREEDY_COPY_SET_ALLOCATOR.generateOptimalRegionReplicasDistribution(
+              AVAILABLE_DATA_NODE_MAP,
+              FREE_SPACE_MAP,
+              greedyCopySetResult,
+              replicationFactor,
+              new TConsensusGroupId(TConsensusGroupType.DataRegion, index)));
+    }
+
+    /* Statistics result */
+    // Map<DataNodeId, RegionGroup Count> for greedy algorithm
+    Map<Integer, AtomicInteger> greedyRegionCounter = new HashMap<>();

Review Comment:
   use Integer



##########
iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/load/balancer/region/GreedyCopySetRegionGroupAllocator.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * 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.TDataNodeLocation;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/** Allocate Region through Greedy and CopySet Algorithm. */
+public class GreedyCopySetRegionGroupAllocator implements 
IRegionGroupAllocator {
+
+  int replicationFactor;
+  // RegionGroup allocation BitSet
+  private List<BitSet> allocatedBitSets;
+  // Map<DataNodeId, RegionGroup count>
+  private Map<Integer, AtomicInteger> regionCounter;

Review Comment:
   use Integer



##########
iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/load/balancer/region/GreedyCopySetRegionGroupAllocator.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * 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.TDataNodeLocation;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/** Allocate Region through Greedy and CopySet Algorithm. */
+public class GreedyCopySetRegionGroupAllocator implements 
IRegionGroupAllocator {
+
+  int replicationFactor;
+  // RegionGroup allocation BitSet
+  private List<BitSet> allocatedBitSets;
+  // Map<DataNodeId, RegionGroup count>
+  private Map<Integer, AtomicInteger> regionCounter;
+  // Available DataNodeIds
+  private Integer[] dataNodeIds;
+
+  // First Key: the sum of Regions at the DataNodes in the allocation result 
is minimal
+  int optimalRegionSum;
+  // Second Key: the sum of intersected Regions with other allocated 
RegionGroups is minimal
+  int optimalIntersectionSum;
+  List<Integer[]> optimalReplicaSets;

Review Comment:
   can we use List<int[]> ?



##########
iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/load/balancer/RegionBalancer.java:
##########
@@ -147,6 +151,7 @@ private LoadManager getLoadManager() {
 
   public enum RegionGroupAllocatePolicy {
     COPY_SET,

Review Comment:
   Maybe we can also remove this incomplete copyset implementation, which will 
never be used



##########
iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/load/balancer/region/GreedyCopySetRegionGroupAllocator.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * 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.TDataNodeLocation;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/** Allocate Region through Greedy and CopySet Algorithm. */
+public class GreedyCopySetRegionGroupAllocator implements 
IRegionGroupAllocator {
+
+  int replicationFactor;
+  // RegionGroup allocation BitSet
+  private List<BitSet> allocatedBitSets;
+  // Map<DataNodeId, RegionGroup count>
+  private Map<Integer, AtomicInteger> regionCounter;
+  // Available DataNodeIds
+  private Integer[] dataNodeIds;
+
+  // First Key: the sum of Regions at the DataNodes in the allocation result 
is minimal
+  int optimalRegionSum;
+  // Second Key: the sum of intersected Regions with other allocated 
RegionGroups is minimal
+  int optimalIntersectionSum;
+  List<Integer[]> optimalReplicaSets;
+
+  public GreedyCopySetRegionGroupAllocator() {
+    // Empty constructor
+  }
+
+  @Override
+  public TRegionReplicaSet generateOptimalRegionReplicasDistribution(
+      Map<Integer, TDataNodeConfiguration> availableDataNodeMap,
+      Map<Integer, Double> freeDiskSpaceMap,
+      List<TRegionReplicaSet> allocatedRegionGroups,
+      int replicationFactor,
+      TConsensusGroupId consensusGroupId) {
+
+    prepare(replicationFactor, availableDataNodeMap, allocatedRegionGroups);
+    dfs(-1, 0, new Integer[replicationFactor], 0, 0);
+
+    // Randomly pick one optimal plan as result
+    Collections.shuffle(optimalReplicaSets);
+    Integer[] optimalReplicaSet = optimalReplicaSets.get(0);
+    TRegionReplicaSet result = new TRegionReplicaSet();
+    result.setRegionId(consensusGroupId);
+    for (int i = 0; i < replicationFactor; i++) {
+      
result.addToDataNodeLocations(availableDataNodeMap.get(optimalReplicaSet[i]).getLocation());
+    }
+    return result;
+  }
+
+  /**
+   * Prepare some statistics before dfs.
+   *
+   * @param replicationFactor replication factor in the cluster
+   * @param availableDataNodeMap currently available DataNodes, ensure size() 
>= replicationFactor
+   * @param allocatedRegionGroups already allocated RegionGroups in the cluster
+   */
+  private void prepare(
+      int replicationFactor,
+      Map<Integer, TDataNodeConfiguration> availableDataNodeMap,
+      List<TRegionReplicaSet> allocatedRegionGroups) {
+
+    this.replicationFactor = replicationFactor;
+    int maxDataNodeId = 
availableDataNodeMap.keySet().stream().max(Integer::compareTo).orElse(0);
+    for (TRegionReplicaSet regionReplicaSet : allocatedRegionGroups) {
+      for (TDataNodeLocation dataNodeLocation : 
regionReplicaSet.getDataNodeLocations()) {
+        // Store the maximum DataNodeId in this algorithm loop
+        maxDataNodeId = Math.max(maxDataNodeId, 
dataNodeLocation.getDataNodeId());
+      }
+    }
+
+    // Convert the allocatedRegionGroups into allocatedBitSets,
+    // where a true in BitSet corresponding to a DataNodeId in the RegionGroup
+    allocatedBitSets = new ArrayList<>();
+    for (TRegionReplicaSet regionReplicaSet : allocatedRegionGroups) {
+      BitSet bitSet = new BitSet(maxDataNodeId + 1);
+      for (TDataNodeLocation dataNodeLocation : 
regionReplicaSet.getDataNodeLocations()) {
+        bitSet.set(dataNodeLocation.getDataNodeId());
+      }
+      allocatedBitSets.add(bitSet);
+    }
+
+    // Count the number of Regions in each DataNode
+    regionCounter = new HashMap<>();

Review Comment:
   maxDataNodeId



-- 
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: reviews-unsubscr...@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to