code: private MatchedPair doMatch(
        SessionSchedulable schedulable, long now, long nodeWait, long rackWait) {
      schedulable.adjustLocalityRequirement(now, nodeWait, rackWait);
      for (LocalityLevel level : neededLocalityLevels) {
        if (level.isBetterThan(schedulable.getLastLocality())) {
          /**
           * This means that the last time we tried to schedule this session
           * we could not achieve the current LocalityLevel level.
           * Since this is the same iteration of the scheduler we do not need to
           * try this locality level.
           * The last locality level of the shcedulable is getting reset on every
           * iteration of the scheduler, so we will retry the better localities
           * in the next run of the scheduler.
           */
          continue;
        }
        if (needLocalityCheck(level, nodeWait, rackWait) &&
            !schedulable.isLocalityGoodEnough(level)) {
          break;
        }
        Session session = schedulable.getSession();
        synchronized (session) {
          if (session.isDeleted()) {
            return null;
          }
          int pendingRequestCount = session.getPendingRequestCountForType(type);
          MatchedPair matchedPair = null;
          if (nodeSnapshot == null ||
            pendingRequestCount < nodeSnapshot.getRunnableHostCount()) {
            matchedPair = matchNodeForSession(session, level);
          } else {
            matchedPair = matchSessionForNode(session, level);
          }
          if (matchedPair != null) {
            schedulable.setLocalityLevel(level);
            return matchedPair;
          }
        }
      }
      schedulable.startLocalityWait(now);
      if (LOG.isDebugEnabled()) {
        LOG.debug("Could not find a node for " +
          schedulable.getSession().getHandle());
      }
      return null;
    }
docs: Find a node to give resources to this schedulable session.
  
  @param schedulable Given a resource to this schedulable
  @param now Current time
  @param nodeWait Time to wait for node locality
  @param rackWait Time to wait for rack locality
  @return Pair of resource request and node or null if no node can be found.