You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by GitBox <gi...@apache.org> on 2022/11/04 07:22:29 UTC

[GitHub] [iotdb] wangchao316 commented on a diff in pull request #7774: [IOTDB-4768] Balancing cluster RegionGroup leader distribution by MinimumCostFlow algorithm

wangchao316 commented on code in PR #7774:
URL: https://github.com/apache/iotdb/pull/7774#discussion_r1013692458


##########
confignode/src/main/java/org/apache/iotdb/confignode/manager/load/balancer/RouteBalancer.java:
##########
@@ -241,7 +256,81 @@ public void stopRouteBalancingService() {
   }
 
   private void balancingRegionLeader() {
-    // TODO: IOTDB-4768
+    balancingRegionLeader(TConsensusGroupType.SchemaRegion);
+    balancingRegionLeader(TConsensusGroupType.DataRegion);
+  }
+
+  private void balancingRegionLeader(TConsensusGroupType regionGroupType) {
+    // Collect latest data to generate leaderBalancer
+    MCFLeaderBalancer leaderBalancer =
+        new MCFLeaderBalancer(
+            getPartitionManager().getAllReplicaSetsMap(regionGroupType),
+            regionRouteMap.getRegionLeaderMap(),
+            getNodeManager()
+                .filterDataNodeThroughStatus(
+                    NodeStatus.Unknown, NodeStatus.ReadOnly, NodeStatus.Removing)
+                .stream()
+                .map(TDataNodeConfiguration::getLocation)
+                .map(TDataNodeLocation::getDataNodeId)
+                .collect(Collectors.toSet()));
+
+    // Calculate the optimal leader distribution
+    Map<TConsensusGroupId, Integer> leaderDistribution =
+        leaderBalancer.generateOptimalLeaderDistribution();
+
+    // Transfer leader to the optimal distribution
+    AtomicInteger requestId = new AtomicInteger(0);
+    AsyncClientHandler<TRegionLeaderChangeReq, TSStatus> clientHandler =
+        new AsyncClientHandler<>(DataNodeRequestType.CHANGE_REGION_LEADER);
+    leaderDistribution.forEach(
+        (regionGroupId, newLeaderId) -> {
+          if (newLeaderId != regionRouteMap.getLeader(regionGroupId)) {
+            String consensusProtocolClass;
+            switch (regionGroupId.getType()) {
+              case SchemaRegion:
+                consensusProtocolClass = SCHEMA_REGION_CONSENSUS_PROTOCOL_CLASS;
+                break;
+              case DataRegion:
+              default:
+                consensusProtocolClass = DATA_REGION_CONSENSUS_PROTOCOL_CLASS;
+                break;
+            }
+            LOGGER.info(
+                "[LeaderBalancer] Try to change the leader of Region: {} to DataNode: {} ",
+                regionGroupId,
+                newLeaderId);
+            changeRegionLeader(

Review Comment:
   I have a question .   change region leader during removing datanode or adding datanode, Whether Scaling Is Affected ?



##########
confignode/src/main/java/org/apache/iotdb/confignode/manager/load/balancer/RouteBalancer.java:
##########
@@ -219,8 +234,8 @@ public void startRouteBalancingService() {
                 leaderBalancingExecutor,
                 this::balancingRegionLeader,

Review Comment:
   Is there adding a config, can close balance?



-- 
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-unsubscribe@iotdb.apache.org

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