public static SCMHANodeDetails loadSCMHAConfig()

in hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMHANodeDetails.java [148:286]


  public static SCMHANodeDetails loadSCMHAConfig(OzoneConfiguration conf,
                                                 SCMStorageConfig storageConfig)
      throws IOException {
    InetSocketAddress localRpcAddress = null;
    String localScmServiceId = null;
    String localScmNodeId = null;
    int localRatisPort = 0;
    int localGrpcPort = 0;

    Collection<String> scmServiceIds;

    localScmServiceId = conf.getTrimmed(
        ScmConfigKeys.OZONE_SCM_DEFAULT_SERVICE_ID);

    LOG.info("ServiceID for StorageContainerManager is {}", localScmServiceId);
    if (localScmServiceId == null) {
      // There is no internal scm service id is being set, fall back to ozone
      // .scm.service.ids.
      LOG.info("{} is not defined, falling back to {} to find serviceID for "
              + "StorageContainerManager if it is HA enabled cluster",
          OZONE_SCM_DEFAULT_SERVICE_ID, OZONE_SCM_SERVICE_IDS_KEY);
      scmServiceIds = conf.getTrimmedStringCollection(
          OZONE_SCM_SERVICE_IDS_KEY);
    } else {
      LOG.info("ServiceID for StorageContainerManager is {}",
          localScmServiceId);
      scmServiceIds = Collections.singleton(localScmServiceId);
    }

    localScmNodeId = conf.get(ScmConfigKeys.OZONE_SCM_NODE_ID_KEY);
    int found = 0;
    boolean isSCMddressSet = false;

    for (String serviceId : scmServiceIds) {
      Collection<String> scmNodeIds = HddsUtils.getSCMNodeIds(conf, serviceId);

      // TODO: need to fall back to ozone.scm.names in case scm node ids are
      // not defined.
      if (scmNodeIds.isEmpty()) {
        throw new IllegalArgumentException(
            String.format("Configuration does not have any value set for %s " +
                "for the service %s. List of SCM Node ID's should be " +
                "specified for an SCM service",
                ScmConfigKeys.OZONE_SCM_NODES_KEY, serviceId));
      }
      // TODO: load Ratis peers configuration
      boolean isPeer;
      List<SCMNodeDetails> peerNodesList = new ArrayList<>();
      for (String nodeId : scmNodeIds) {
        if (localScmNodeId != null && !localScmNodeId.equals(nodeId)) {
          isPeer = true;
        } else {
          isPeer = false;
        }

        String rpcAddrKey = ConfUtils.addKeySuffixes(
            OZONE_SCM_ADDRESS_KEY, serviceId, nodeId);
        String rpcAddrStr = conf.get(rpcAddrKey);
        if (rpcAddrStr == null || rpcAddrStr.isEmpty()) {
          throwConfException("Configuration does not have any value set for " +
              "%s. SCM RPC Address should be set for all nodes in a SCM " +
              "service.", rpcAddrKey);
        }
        isSCMddressSet = true;

        String ratisPortKey = ConfUtils.addKeySuffixes(OZONE_SCM_RATIS_PORT_KEY,
            serviceId, nodeId);
        int ratisPort = conf.getInt(ratisPortKey,
            conf.getInt(OZONE_SCM_RATIS_PORT_KEY,
                OZONE_SCM_RATIS_PORT_DEFAULT));

        String grpcPortKey = ConfUtils
            .addKeySuffixes(ScmConfigKeys.OZONE_SCM_GRPC_PORT_KEY, serviceId,
                nodeId);
        int grpcPort = conf.getInt(grpcPortKey,
            conf.getInt(OZONE_SCM_GRPC_PORT_KEY, OZONE_SCM_GRPC_PORT_DEFAULT));

        InetSocketAddress addr = null;
        try {
          addr = NetUtils.createSocketAddr(rpcAddrStr, ratisPort);
        } catch (Exception e) {
          LOG.error("Couldn't create socket address for SCM {} : {}", nodeId,
              rpcAddrStr, e);
          throw e;
        }

        boolean flexibleFqdnResolutionEnabled = conf.getBoolean(
                OZONE_FLEXIBLE_FQDN_RESOLUTION_ENABLED,
                OZONE_FLEXIBLE_FQDN_RESOLUTION_ENABLED_DEFAULT);
        if (OzoneNetUtils.isUnresolved(flexibleFqdnResolutionEnabled, addr)) {
          LOG.error("Address for SCM {} : {} couldn't be resolved. Proceeding "
                  + "with unresolved host to create Ratis ring.", nodeId,
              rpcAddrStr);
        }

        if (!isPeer
                && OzoneNetUtils
                .isAddressLocal(flexibleFqdnResolutionEnabled, addr)) {
          localRpcAddress = addr;
          localScmServiceId = serviceId;
          localScmNodeId = nodeId;
          localRatisPort = ratisPort;
          localGrpcPort = grpcPort;
          found++;
        } else {
          peerNodesList.add(getHASCMNodeDetails(conf, serviceId,
              nodeId, addr, ratisPort, grpcPort));
        }
      }

      if (found == 1) {
        LOG.info("Found matching SCM address with SCMServiceId: {}, " +
                "SCMNodeId: {}, RPC Address: {} and Ratis port: {}",
            localScmServiceId, localScmNodeId,
            NetUtils.getHostPortString(localRpcAddress), localRatisPort);

        // Set SCM node specific config keys.
        ConfUtils.setNodeSpecificConfigs(nodeSpecificConfigKeys, conf,
            localScmServiceId, localScmNodeId, LOG);

        return new SCMHANodeDetails(
            getHASCMNodeDetails(conf, localScmServiceId, localScmNodeId,
                localRpcAddress, localRatisPort, localGrpcPort), peerNodesList);

      } else if (found > 1) {
        throwConfException("Configuration has multiple %s addresses that " +
                "match local node's address. Please configure the system " +
                "with %s and %s", OZONE_SCM_ADDRESS_KEY,
            OZONE_SCM_SERVICE_IDS_KEY, OZONE_SCM_ADDRESS_KEY);
      }
    }

    if (!isSCMddressSet) {
      // If HA config is not set, fall back to default configuration
      return loadDefaultConfig(conf);
    } else {
      return null;
    }
  }