code: protected boolean chooseRemoteRack(int rackIdx, int firstRack, int rackWindow,
        int machineIdx, int windowSize, HashMap<Node, Node> excludedNodes,
        long blocksize, int maxReplicasPerRack, List<DatanodeDescriptor> results,
        boolean reverse) throws NotEnoughReplicasException {
      // randomly choose one node from remote racks
  
      readLock();
      try {
        HashSet<Integer> excludedRacks = new HashSet<Integer>();
        excludedRacks.add(rackIdx);
        int n = racks.size();
        int currRackSize = racksMap.get(racks.get(rackIdx)).rackNodes.size();
        while (excludedRacks.size() < rackWindow) {
  
          int newRack = randomIntInWindow(firstRack, rackWindow, n, excludedRacks);
          if (newRack < 0)
            break;
  
          excludedRacks.add(newRack);
  
          int newRackSize = racksMap.get(racks.get(newRack)).rackNodes.size();
          int firstMachine = machineIdx * newRackSize / currRackSize;
  
          int newWindowSize = windowSize;
          if (reverse) {
            firstMachine = ((int) Math.ceil((double) machineIdx * newRackSize
                / currRackSize))
                % newRackSize;
  
            newWindowSize = Math.max(1, windowSize * newRackSize / currRackSize);
          }
  
          if (newWindowSize <= 0) {
            continue;
          }
  
          if (chooseMachine(racks.get(newRack), firstMachine, newWindowSize,
              excludedNodes, blocksize, maxReplicasPerRack, results)) {
            return true;
          }
        }
        return false;
      } finally {
        readUnlock();
      }
    }
docs: Picks up a remote machine within defined window
  @param rackIdx rack the request is coming from and that should be avoided
  @param firstRack rack that starts window
  @param rackWindow rack window size
  @param machineIdx index of first replica within its rack
  @param windowSize size of the machine window
  @param excludedNodes list of black listed nodes.
  @param blocksize size of a block
  @param maxReplicasPerRack maximum number of replicas per rack
  @param results List of results
  @param reverse adjustment when looking forward or backward.
  @return
  @throws NotEnoughReplicasException