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


##########
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:
   Fixed



-- 
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