public synchronized void unreserveResource(SchedulerApplicationAttempt application) {

    // adding NP checks as this can now be called for preemption
    if (reservedContainer != null
        && reservedContainer.getContainer() != null
        && reservedContainer.getContainer().getId() != null
        && reservedContainer.getContainer().getId().getApplicationAttemptId() != null) {

      // Cannot unreserve for wrong application...
      ApplicationAttemptId reservedApplication =
          reservedContainer.getContainer().getId().getApplicationAttemptId();
      if (!reservedApplication.equals(application.getApplicationAttemptId())) {
        throw new IllegalStateException(
            "Trying to unreserve "
                + " for application "
                + application.getApplicationAttemptId()
                + " when currently reserved "
                + " for application "
                + reservedApplication.getApplicationId()
                + " on node "
                + this);
      }
    }
    reservedContainer = null;
  }
  /**
   * Reserve a spot for {@code container} on this {@code node}. If the container is {@code
   * alreadyReserved} on the node, simply update relevant bookeeping. This dispatches ro relevant
   * handlers in {@link FSSchedulerNode}..
   */
  private void reserve(
      Priority priority, FSSchedulerNode node, Container container, boolean alreadyReserved) {
    LOG.info("Making reservation: node=" + node.getNodeName() + " app_id=" + getApplicationId());

    if (!alreadyReserved) {
      getMetrics().reserveResource(getUser(), container.getResource());
      RMContainer rmContainer = super.reserve(node, priority, null, container);
      node.reserveResource(this, priority, rmContainer);
    } else {
      RMContainer rmContainer = node.getReservedContainer();
      super.reserve(node, priority, rmContainer, container);
      node.reserveResource(this, priority, rmContainer);
    }
  }
  public synchronized void reserveResource(
      SchedulerApplicationAttempt application, Priority priority, RMContainer reservedContainer) {
    // Check if it's already reserved
    if (this.reservedContainer != null) {
      // Sanity check
      if (!reservedContainer.getContainer().getNodeId().equals(getNodeID())) {
        throw new IllegalStateException(
            "Trying to reserve"
                + " container "
                + reservedContainer
                + " on node "
                + reservedContainer.getReservedNode()
                + " when currently"
                + " reserved resource "
                + this.reservedContainer
                + " on node "
                + this.reservedContainer.getReservedNode());
      }

      // Cannot reserve more than one application attempt on a given node!
      // Reservation is still against attempt.
      if (!this.reservedContainer
          .getContainer()
          .getId()
          .getApplicationAttemptId()
          .equals(reservedContainer.getContainer().getId().getApplicationAttemptId())) {
        throw new IllegalStateException(
            "Trying to reserve"
                + " container "
                + reservedContainer
                + " for application "
                + application.getApplicationAttemptId()
                + " when currently"
                + " reserved container "
                + this.reservedContainer
                + " on node "
                + this);
      }

      if (LOG.isDebugEnabled()) {
        LOG.debug(
            "Updated reserved container "
                + reservedContainer.getContainer().getId()
                + " on node "
                + this
                + " for application attempt "
                + application.getApplicationAttemptId());
      }
    } else {
      if (LOG.isDebugEnabled()) {
        LOG.debug(
            "Reserved container "
                + reservedContainer.getContainer().getId()
                + " on node "
                + this
                + " for application attempt "
                + application.getApplicationAttemptId());
      }
    }
    this.reservedContainer = reservedContainer;
  }