@Override
  public Resource getHeadroom() {
    final FSQueue queue = (FSQueue) this.queue;
    SchedulingPolicy policy = queue.getPolicy();

    Resource queueFairShare = queue.getFairShare();
    Resource queueUsage = queue.getResourceUsage();
    Resource clusterResource = this.scheduler.getClusterResource();
    Resource clusterUsage = this.scheduler.getRootQueueMetrics().getAllocatedResources();
    Resource clusterAvailableResource = Resources.subtract(clusterResource, clusterUsage);
    Resource headroom = policy.getHeadroom(queueFairShare, queueUsage, clusterAvailableResource);
    if (LOG.isDebugEnabled()) {
      LOG.debug(
          "Headroom calculation for "
              + this.getName()
              + ":"
              + "Min("
              + "(queueFairShare="
              + queueFairShare
              + " - queueUsage="
              + queueUsage
              + "),"
              + " clusterAvailableResource="
              + clusterAvailableResource
              + "(clusterResource="
              + clusterResource
              + " - clusterUsage="
              + clusterUsage
              + ")"
              + "Headroom="
              + headroom);
    }
    return headroom;
  }
 /**
  * Traverses the queue hierarchy under the given queue to gather all lists of non-runnable
  * applications.
  */
 private void gatherPossiblyRunnableAppLists(FSQueue queue, List<List<FSAppAttempt>> appLists) {
   if (queue.getNumRunnableApps()
       < scheduler.getAllocationConfiguration().getQueueMaxApps(queue.getName())) {
     if (queue instanceof FSLeafQueue) {
       appLists.add(((FSLeafQueue) queue).getCopyOfNonRunnableAppSchedulables());
     } else {
       for (FSQueue child : queue.getChildQueues()) {
         gatherPossiblyRunnableAppLists(child, appLists);
       }
     }
   }
 }
  /** Checks whether making the application runnable would exceed any maxRunningApps limits. */
  public boolean canAppBeRunnable(FSQueue queue, String user) {
    AllocationConfiguration allocConf = scheduler.getAllocationConfiguration();
    Integer userNumRunnable = usersNumRunnableApps.get(user);
    if (userNumRunnable == null) {
      userNumRunnable = 0;
    }
    if (userNumRunnable >= allocConf.getUserMaxApps(user)) {
      return false;
    }
    // Check queue and all parent queues
    while (queue != null) {
      int queueMaxApps = allocConf.getQueueMaxApps(queue.getName());
      if (queue.getNumRunnableApps() >= queueMaxApps) {
        return false;
      }
      queue = queue.getParent();
    }

    return true;
  }