private final String generateConfig(ServerType serverType, int rpcPort, String daemonDir) {
    StringBuilder sb = new StringBuilder();
    Map<String, Object> confMap = new TreeMap<>();
    confMap.put(HConstants.CLUSTER_DISTRIBUTED, true);

    if (serverType == ServerType.MASTER) {
      confMap.put(HConstants.MASTER_PORT, rpcPort);

      int masterInfoPort = HBaseTestingUtil.randomFreePort();
      reportWebUIPort("master", masterInfoPort);
      confMap.put(HConstants.MASTER_INFO_PORT, masterInfoPort);
    } else if (serverType == ServerType.RS) {
      confMap.put(HConstants.REGIONSERVER_PORT, rpcPort);

      int rsInfoPort = HBaseTestingUtil.randomFreePort();
      reportWebUIPort("region server", rsInfoPort);
      confMap.put(HConstants.REGIONSERVER_INFO_PORT, rsInfoPort);
    } else {
      confMap.put(HConstants.ZOOKEEPER_DATA_DIR, daemonDir);
    }

    confMap.put(HConstants.ZOOKEEPER_CLIENT_PORT, zkClientPort);
    confMap.put(HConstants.HREGION_MAX_FILESIZE, MAX_FILE_SIZE_OVERRIDE);

    if (dfsCluster != null) {
      String fsURL = "hdfs://" + HConstants.LOCALHOST + ":" + dfsCluster.getNameNodePort();
      confMap.put("fs.defaultFS", fsURL);
      confMap.put("hbase.rootdir", fsURL + "/hbase_test");
    }

    sb.append("<configuration>\n");
    for (Map.Entry<String, Object> entry : confMap.entrySet()) {
      sb.append("  <property>\n");
      sb.append("    <name>" + entry.getKey() + "</name>\n");
      sb.append("    <value>" + entry.getValue() + "</value>\n");
      sb.append("  </property>\n");
    }
    sb.append("</configuration>\n");
    return sb.toString();
  }
Generates a list of random port numbers in the sorted order. A sorted order makes sense if we
ever want to refer to these servers by their index in the returned array, e.g. server #0, #1,
etc.
hbase-diagnostics/src/test/java/org/apache/hadoop/hbase/util/ProcessBasedLocalHBaseCluster.java