bool reservation_holder::is_orphaned(

  const char  *rsv_id,
  std::string &job_id)

  {
  bool              orphaned = false;
  job              *pjob;
  std::map<std::string, alps_reservation>::iterator it;

  pthread_mutex_lock(&this->rh_mutex);
  it = this->reservations.find(rsv_id);

  if (it != this->reservations.end())
    {
    job_id = job_mapper.get_name(it->second.internal_job_id);

    if ((pjob = svr_find_job_by_id(it->second.internal_job_id)) != NULL)
      {
      if (pjob->ji_qs.ji_state == JOB_STATE_COMPLETE)
        orphaned = true;

      unlock_ji_mutex(pjob, __func__, "1", LOGLEVEL);
      }
    else
      {
      orphaned = true;
      }
    }
  else
    {
    job_id = "unknown";
    orphaned = true;
    }

  if (orphaned == true)
    {
    if (this->orphaned_reservations.find(rsv_id) != this->orphaned_reservations.end())
      {
      // Only send one release reservation request at a time.
      orphaned = false;
      }
    else
      {
      // Record this so we only send one at a time
      this->orphaned_reservations.insert(rsv_id);
      }
    }

  pthread_mutex_unlock(&this->rh_mutex);

  return(orphaned);
  } /* END is_orphaned() */
/*
 * record_reservation()
 *
 * @pre-cond: pnode and rsv_id must be valid pointers
 * @post-cond: the reservation will be recorded in pbs_server's tracking mechanism
 * and on the job which has the node reserved, or -1 is returned and the reservation
 * is not recorded.
 * @param - pnode the node which is reporting the reservation
 * @param - rsv_id the id of the reservation being reported
 * @return - PBSE_NONE if the reservation was successfully recorded, -1 otherwise
 */
int record_reservation(

  struct pbsnode *pnode,
  const char     *rsv_id)

  {
  job            *pjob;
  bool            found_job = false;

  for (unsigned int i = 0; i < pnode->nd_job_usages.size(); i++)
    {
    /* cray only allows one job per node, so any valid job will be the job that is 
     * reserving this node. */
    const job_usage_info &jui = pnode->nd_job_usages[i];
    int                   internal_job_id = jui.internal_job_id;

    pnode->unlock_node(__func__, NULL, LOGLEVEL);

    if ((pjob = svr_find_job_by_id(internal_job_id)) != NULL)
      {
      mutex_mgr job_mutex(pjob->ji_mutex, true);
      pjob->ji_wattr[JOB_ATR_reservation_id].at_val.at_str = strdup(rsv_id);
      pjob->ji_wattr[JOB_ATR_reservation_id].at_flags = ATR_VFLAG_SET;

      /* add environment variable BATCH_PARTITION_ID */
      char buf[1024];
      snprintf(buf, sizeof(buf), "BATCH_PARTITION_ID=%s", rsv_id);
      pbs_attribute  tempattr;
      clear_attr(&tempattr, &job_attr_def[JOB_ATR_variables]);
      job_attr_def[JOB_ATR_variables].at_decode(&tempattr,
        NULL, NULL, buf, 0);

      job_attr_def[JOB_ATR_variables].at_set(
        &pjob->ji_wattr[JOB_ATR_variables], &tempattr, INCR);

      job_attr_def[JOB_ATR_variables].at_free(&tempattr);

      alps_reservations.track_alps_reservation(pjob);
      found_job = true;

      job_mutex.unlock(); 
      pnode->lock_node(__func__, NULL, LOGLEVEL);
      break;
      }
    else
      pnode->lock_node(__func__, NULL, LOGLEVEL);
    }

  if (found_job == false)
    return(-1);

  return(PBSE_NONE);
  } /* END record_reservation() */
bool is_orphaned(

  char *rsv_id,
  char *job_id)

  {
  bool              orphaned = false;
  job              *pjob;
  alps_reservation *ar = NULL;

  alps_reservations.lock();
  ar = alps_reservations.find(rsv_id);
  alps_reservations.unlock();

  if (ar != NULL)
    {
    if (job_id != NULL)
      snprintf(job_id, PBS_MAXSVRJOBID, "%s", job_mapper.get_name(ar->internal_job_id));

    if ((pjob = svr_find_job_by_id(ar->internal_job_id)) != NULL)
      {
      if (pjob->ji_qs.ji_state == JOB_STATE_COMPLETE)
        orphaned = true;

      unlock_ji_mutex(pjob, __func__, "1", LOGLEVEL);
      }
    else
      orphaned = true;
    }
  else
    {
    if (job_id != NULL)
      snprintf(job_id, PBS_MAXSVRJOBID, "unknown");
    orphaned = true;
    }

  return(orphaned);
  } /* END is_orphaned() */