code: public static <T extends DatanodeID> List<T> bestNode(T[] nodes, boolean doRandom) 
      throws IOException {
      TreeSet<T> deadNodes = new TreeSet<T>();
      T chosenNode = null;
      int failures = 0;
      Socket s = null;
      int index = -1;
      if (nodes == null || nodes.length == 0) {
        throw new IOException("No nodes contain this block");
      }
      while (s == null) {
        if (chosenNode == null) {
          do {
            if (doRandom) {
              index = rand.nextInt(nodes.length);
            } else {
              index++;
            }
            chosenNode = nodes[index];
          } while (deadNodes.contains(chosenNode));
        }
        chosenNode = nodes[index];
  
        //just ping to check whether the node is alive
        InetSocketAddress targetAddr = NetUtils.createSocketAddr(
            chosenNode.getHost() + ":" + chosenNode.getInfoPort());
          
        try {
          s = new Socket();
          s.connect(targetAddr, HdfsConstants.READ_TIMEOUT);
          s.setSoTimeout(HdfsConstants.READ_TIMEOUT);
        } catch (IOException e) {
          HttpServer.LOG.warn("Failed to connect to "+chosenNode.name, e);
          deadNodes.add(chosenNode);
          s.close();
          s = null;
          failures++;
        } finally {
          if (s!=null) {
            s.close();
          }
        }
        if (failures == nodes.length)
          throw new IOException("Could not reach the block containing the data. Please try again");
          
      }
      List<T> candidates;
      if (doRandom) {
        candidates = new ArrayList<T>(1);
        candidates.add(chosenNode);
      } else {
        candidates = new ArrayList<T>(nodes.length - index);
        for (int i=index; i<nodes.length - index; i++) {
          candidates.add(nodes[i]);
        }
      }
      return candidates;
    }
docs: Choose a list datanodes from the specified list. The best one is
  the first one in the list.
  
  If doRamdom is true, then
  a random datanode is selected. Otherwise, a node that appears earlier
  in the list has more probability of being selected