private void flushOldLeaderIfIoTV2() {
    if (!IS_ENABLE_AUTO_LEADER_BALANCE_FOR_DATA_REGION
        || !Objects.equals(
            DATA_REGION_CONSENSUS_PROTOCOL_CLASS, ConsensusFactory.IOT_CONSENSUS_V2)) {
      return;
    }

    BiConsumer<Integer, List<String>> consumer =
        (oldLeaderId, regionGroupIds) -> {
          TDataNodeConfiguration configuration =
              getNodeManager().getRegisteredDataNode(oldLeaderId);
          Map<Integer, TDataNodeLocation> oldLeaderDataNodeLocation = new HashMap<>();
          oldLeaderDataNodeLocation.put(
              configuration.getLocation().dataNodeId, configuration.getLocation());

          TFlushReq flushReq = new TFlushReq();
          flushReq.setRegionIds(regionGroupIds);
          // Do our best to flush. If flush failed, never retry
          TSStatus result = configManager.flushOnSpecificDN(flushReq, oldLeaderDataNodeLocation);
          if (result.getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
            LOGGER.info(
                "[IoTConsensusV2 Leader Changed] Successfully flush old leader {} for region {}",
                oldLeaderId,
                regionGroupIds);
          } else {
            LOGGER.info(
                "[IoTConsensusV2 Leader Changed] Failed to flush old leader {} for region {}",
                oldLeaderId,
                regionGroupIds);
          }
        };
    lastBalancedOldLeaderId2RegionMap.forEach(consumer);
    // after flush, clear map for next balance
    lastBalancedOldLeaderId2RegionMap.clear();
  }
Balance cluster RegionGroup leader distribution through configured algorithm.
iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/load/balancer/RouteBalancer.java