managed/src/main/java/com/yugabyte/yw/common/PlacementInfoUtil.java
/**
   * Given a set of nodes and number of desired masters, select masters from the nodes that
   * will be added to the universe.
   *
   * @param nodesMap   : a map of node name to NodeDetails
   * @param numMasters : the number of masters to choose
   */
Given a set of nodes and number of desired masters, select masters from the nodes that
will be added to the universe.

@param nodesMap   : a map of node name to NodeDetails
@param numMasters : the number of masters to choose
Given a set of nodes and number of desired masters select masters from the nodes that will be added to the universe . @param nodesMap : a map of node name to NodeDetails @param numMasters : the number of masters to choose
private static void selectMasters(Map<String, NodeDetails> nodesMap, int numMasters) {
    Map<String, TreeSet<String>> subnetsToNodenameMap = new HashMap<String, TreeSet<String>>();
    for (Entry<String, NodeDetails> entry : nodesMap.entrySet()) {
      NodeDetails node = entry.getValue();
      if (node.state != NodeState.Live && node.state != NodeState.ToBeAdded) {
        continue;
      }
      String subnet = node.cloudInfo.subnet_id;
      if (!subnetsToNodenameMap.containsKey(subnet)) {
        subnetsToNodenameMap.put(subnet, new TreeSet<String>());
      }
      TreeSet<String> nodeSet = subnetsToNodenameMap.get(subnet);
      nodeSet.add(entry.getKey());
    }
    LOG.info("Subnet map has {}, nodesMap has {}, need {} masters.",
            subnetsToNodenameMap.size(), nodesMap.size(), numMasters);
    int numMastersChosen = 0;
    if (subnetsToNodenameMap.size() >= maxMasterSubnets) {
      while (numMastersChosen < numMasters) {
        for (Entry<String, TreeSet<String>> entry : subnetsToNodenameMap.entrySet()) {
          TreeSet<String> value = entry.getValue();
          if (value.isEmpty()) {
            continue;
          }
          String nodeName = value.first();
          value.remove(nodeName);
          subnetsToNodenameMap.put(entry.getKey(), value);
          NodeDetails node = nodesMap.get(nodeName);
          node.isMaster = true;
          LOG.info("Chose node '{}' as a master from subnet {}.", nodeName, entry.getKey());
          numMastersChosen++;
          if (numMastersChosen == numMasters) {
            break;
          }
        }
      }
    } else {
      for (NodeDetails node : nodesMap.values()) {
        if (node.isMaster) {
          continue;
        }
        node.isMaster = true;
        LOG.info("Chose node {} as a master from subnet {}.",
                node.nodeName, node.cloudInfo.subnet_id);
        numMastersChosen++;
        if (numMastersChosen == numMasters) {
          break;
        }
      }
      if (numMastersChosen < numMasters) {
        throw new IllegalStateException("Could not pick " + numMasters + " masters, got only " +
                numMastersChosen + ". Nodes info. " + nodesMap);
      }
    }
  }
private static void selectMasters ( Map < String , NodeDetails > nodesMap , int numMasters ) { Map < String , TreeSet < String > > subnetsToNodenameMap = new HashMap < String , TreeSet < String > > ( ) ; for ( Entry < String , NodeDetails > entry : nodesMap . entrySet ( ) ) { NodeDetails node = entry . getValue ( ) ; if ( node . state != NodeState . Live && node . state != NodeState . ToBeAdded ) { continue ; } String subnet = node . cloudInfo . subnet_id ; if ( ! subnetsToNodenameMap . containsKey ( subnet ) ) { subnetsToNodenameMap . put ( subnet , new TreeSet < String > ( ) ) ; } TreeSet < String > nodeSet = subnetsToNodenameMap . get ( subnet ) ; nodeSet . add ( entry . getKey ( ) ) ; } LOG . info ( "Subnet map has {}, nodesMap has {}, need {} masters." , subnetsToNodenameMap . size ( ) , nodesMap . size ( ) , numMasters ) ; int numMastersChosen = 0 ; if ( subnetsToNodenameMap . size ( ) >= maxMasterSubnets ) { while ( numMastersChosen < numMasters ) { for ( Entry < String , TreeSet < String > > entry : subnetsToNodenameMap . entrySet ( ) ) { TreeSet < String > value = entry . getValue ( ) ; if ( value . isEmpty ( ) ) { continue ; } String nodeName = value . first ( ) ; value . remove ( nodeName ) ; subnetsToNodenameMap . put ( entry . getKey ( ) , value ) ; NodeDetails node = nodesMap . get ( nodeName ) ; node . isMaster = true ; LOG . info ( "Chose node '{}' as a master from subnet {}." , nodeName , entry . getKey ( ) ) ; numMastersChosen ++ ; if ( numMastersChosen == numMasters ) { break ; } } } } else { for ( NodeDetails node : nodesMap . values ( ) ) { if ( node . isMaster ) { continue ; } node . isMaster = true ; LOG . info ( "Chose node {} as a master from subnet {}." , node . nodeName , node . cloudInfo . subnet_id ) ; numMastersChosen ++ ; if ( numMastersChosen == numMasters ) { break ; } } if ( numMastersChosen < numMasters ) { throw new IllegalStateException ( "Could not pick " + numMasters + " masters, got only " + numMastersChosen + ". Nodes info. " + nodesMap ) ; } } }
Given a set of nodes and number of desired masters, select masters from the nodes that
 will be added to the universe.
Given a set of nodes and number of desired masters select masters from the nodes that will be added to the universe .
// Group the cluster nodes by subnets. // TODO: Live is checked as some tests use it as the starting state. Should be made ToBeAdded. // Add the node name into the node set. // Choose the masters such that we have one master per subnet if there are enough subnets. // Get one node from each subnet and removes it so that a different node is picked in next. // We do not have enough subnets. Simply pick enough masters.