public synchronized void adjustMaxRegionGroupNum() {
    // Get all DatabaseSchemas
    final Map<String, TDatabaseSchema> databaseSchemaMap =
        getMatchedDatabaseSchemasByName(getDatabaseNames(null), null);
    if (databaseSchemaMap.isEmpty()) {
      // Skip when there are no Databases
      return;
    }

    final int dataNodeNum = getNodeManager().getRegisteredDataNodeCount();
    final int totalCpuCoreNum = getNodeManager().getDataNodeCpuCoreCount();
    int databaseNum = databaseSchemaMap.size();

    for (final TDatabaseSchema databaseSchema : databaseSchemaMap.values()) {
      if (!isDatabaseExist(databaseSchema.getName())
          || databaseSchema.getName().equals(SchemaConstant.SYSTEM_DATABASE)) {
        // filter the pre deleted database and the system database
        databaseNum--;
      }
    }

    final AdjustMaxRegionGroupNumPlan adjustMaxRegionGroupNumPlan =
        new AdjustMaxRegionGroupNumPlan();
    for (final TDatabaseSchema databaseSchema : databaseSchemaMap.values()) {
      if (databaseSchema.getName().equals(SchemaConstant.SYSTEM_DATABASE)) {
        // filter the system database
        continue;
      }

      // Adjust maxSchemaRegionGroupNum for each Database.
      // All Databases share the DataNodes equally.
      // The allocated SchemaRegionGroups will not be shrunk.
      final int allocatedSchemaRegionGroupCount;
      try {
        allocatedSchemaRegionGroupCount =
            getPartitionManager()
                .getRegionGroupCount(databaseSchema.getName(), TConsensusGroupType.SchemaRegion);
      } catch (final DatabaseNotExistsException e) {
        // ignore the pre deleted database
        continue;
      }

      final int maxSchemaRegionGroupNum =
          calcMaxRegionGroupNum(
              databaseSchema.getMinSchemaRegionGroupNum(),
              SCHEMA_REGION_PER_DATA_NODE,
              dataNodeNum,
              databaseNum,
              databaseSchema.getSchemaReplicationFactor(),
              allocatedSchemaRegionGroupCount);
      LOGGER.info(
          "[AdjustRegionGroupNum] The maximum number of SchemaRegionGroups for Database: {} is adjusted to: {}",
          databaseSchema.getName(),
          maxSchemaRegionGroupNum);

      // Adjust maxDataRegionGroupNum for each Database.
      // All Databases share the DataNodes equally.
      // The allocated DataRegionGroups will not be shrunk.
      final int allocatedDataRegionGroupCount;
      try {
        allocatedDataRegionGroupCount =
            getPartitionManager()
                .getRegionGroupCount(databaseSchema.getName(), TConsensusGroupType.DataRegion);
      } catch (final DatabaseNotExistsException e) {
        // ignore the pre deleted database
        continue;
      }

      final int maxDataRegionGroupNum =
          calcMaxRegionGroupNum(
              databaseSchema.getMinDataRegionGroupNum(),
              DATA_REGION_PER_DATA_NODE == 0
                  ? CONF.getDataRegionPerDataNodeProportion()
                  : DATA_REGION_PER_DATA_NODE,
              DATA_REGION_PER_DATA_NODE == 0 ? totalCpuCoreNum : dataNodeNum,
              databaseNum,
              databaseSchema.getDataReplicationFactor(),
              allocatedDataRegionGroupCount);
      LOGGER.info(
          "[AdjustRegionGroupNum] The maximum number of DataRegionGroups for Database: {} is adjusted to: {}",
          databaseSchema.getName(),
          maxDataRegionGroupNum);

      adjustMaxRegionGroupNumPlan.putEntry(
          databaseSchema.getName(), new Pair<>(maxSchemaRegionGroupNum, maxDataRegionGroupNum));
    }
    try {
      getConsensusManager().write(adjustMaxRegionGroupNumPlan);
    } catch (final ConsensusException e) {
      LOGGER.warn(CONSENSUS_WRITE_ERROR, e);
    }
  }
Only leader use this interface. Adjust the maxSchemaRegionGroupNum and maxDataRegionGroupNum of
each Database based on existing cluster resources
iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/schema/ClusterSchemaManager.java