code: private DatanodeInfo chooseDatanode(DatanodeInfo[] locationsToAvoid,
                            DatanodeInfo[] live)
    throws IOException {
      LOG.info("Choosing a datanode from " + live.length +
          " live nodes while avoiding " + locationsToAvoid.length);
      Random rand = new Random();
      DatanodeInfo chosen = null;
      int maxAttempts = 1000;
      for (int i = 0; i < maxAttempts && chosen == null; i++) {
        int idx = rand.nextInt(live.length);
        chosen = live[idx];
        for (DatanodeInfo avoid: locationsToAvoid) {
          if (chosen.equals(avoid)) {
            LOG.info("Avoiding " + avoid.name);
            chosen = null;
            break;
          }
        }
      }
      if (chosen == null) {
        throw new IOException("Could not choose datanode");
      }
      LOG.info("Choosing datanode " + chosen.name);
      return chosen;
    }
docs: Choose a datanode (hostname:portnumber). The datanode is chosen at
  random from the live datanodes.
  @param locationsToAvoid locations to avoid.
  @return A chosen datanode.
  @throws IOException