@Override
  public AllocateCallbackResponse allocateCallback(AllocateCallback allocateCallback) {
    CallbackRequestType request = allocateCallback.getAllocateCallback();
    this.logger.debug(
        "Received "
            + this.getClass().getSimpleName()
            + "#allocateCallback with params: rigname="
            + request.getName()
            + ", success="
            + request.getSuccess()
            + '.');

    AllocateCallbackResponse response = new AllocateCallbackResponse();
    ProviderResponse status = new ProviderResponse();
    response.setAllocateCallbackResponse(status);

    /* Load session from rig. */
    RigDao dao = new RigDao();
    Rig rig = dao.findByName(request.getName());
    Session ses = null;
    if (rig == null) {
      /* If the rig wasn't found, something is seriously wrong. */
      this.logger.error(
          "Received allocate callback for rig '" + request.getName() + "' that doesn't exist.");
      status.setSuccessful(false);
    } else if ((ses = rig.getSession()) == null) {
      this.logger.warn(
          "Received allocate callback for session that doesn't exist. Rig who sent callback "
              + "response was '"
              + request.getName()
              + "'.");
      status.setSuccessful(false);

      /* Make sure the rig is no marked as in session. */
      rig.setInSession(false);
      rig.setLastUpdateTimestamp(new Date());
    } else if (request.getSuccess()) {
      /* If the response from allocate is successful, put the session to ready. */
      ses.setReady(true);
      status.setSuccessful(true);
      rig.setLastUpdateTimestamp(new Date());

      RigProviderActivator.notifySessionEvent(SessionEvent.READY, ses, dao.getSession());
    } else {
      ErrorType err = request.getError();
      this.logger.error(
          "Received allocate response for "
              + ses.getUserNamespace()
              + ':'
              + ses.getUserName()
              + ", allocation not successful. Error reason is '"
              + err.getReason()
              + "'.");

      /* Allocation failed so end the session and take the rig offline depending on error. */
      ses.setActive(false);
      ses.setReady(false);
      ses.setRemovalReason("Allocation failure with reason: " + err.getReason());
      ses.setRemovalTime(new Date());

      RigProviderActivator.notifySessionEvent(SessionEvent.FINISHED, ses, dao.getSession());

      if (err.getCode() == 4) // Error code 4 is an existing session exists
      {
        this.logger.error(
            "Allocation failure reason was caused by an existing session, so not putting rig offline "
                + "because a session already has it.");
      } else {
        rig.setInSession(false);
        rig.setOnline(false);
        rig.setOfflineReason("Allocation failed with reason: " + err.getReason());
        rig.setSession(null);

        /* Log the rig going offline. */
        RigLogDao logDao = new RigLogDao(dao.getSession());
        logDao.addOfflineLog(rig, "Allocation failed with reason: " + err.getReason() + "");

        RigProviderActivator.notifyRigEvent(RigStateChangeEvent.OFFLINE, rig, dao.getSession());
      }

      rig.setLastUpdateTimestamp(new Date());

      /* Whilst allocation was not successful, the process was clean. */
      status.setSuccessful(true);
    }

    dao.flush();
    dao.closeSession();
    return response;
  }
Ejemplo n.º 2
0
  /**
   * Test method for {@link
   * au.edu.uts.eng.remotelabs.schedserver.queuer.QueueRun#attemptAssignment(long)}.
   */
  @SuppressWarnings("unchecked")
  @Test
  public void testAttemptAssignment() throws Exception {
    org.hibernate.Session db = DataAccessActivator.getNewSession();

    Date before = new Date(System.currentTimeMillis() - 10000);
    Date after = new Date(System.currentTimeMillis() + 10000);
    Date now = new Date();

    db.beginTransaction();
    User user1 = new User("qperm1", "testns", "USER");
    db.persist(user1);

    UserClass uc1 = new UserClass();
    uc1.setName("uc1");
    uc1.setActive(true);
    db.persist(uc1);

    UserAssociation ass1 =
        new UserAssociation(new UserAssociationId(user1.getId(), uc1.getId()), uc1, user1);
    db.persist(ass1);

    RigType rt = new RigType();
    rt.setName("QR_Test_Rig_Type");
    db.persist(rt);

    RigCapabilities caps = new RigCapabilities("qr,test,rig,type");
    db.persist(caps);

    Rig r = new Rig();
    r.setName("QR_Rig_Test_Rig1");
    r.setRigType(rt);
    r.setRigCapabilities(caps);
    r.setLastUpdateTimestamp(before);
    r.setActive(true);
    r.setOnline(false);
    r.setInSession(true);
    db.persist(r);

    ResourcePermission p1 = new ResourcePermission();
    p1.setType("RIG");
    p1.setUserClass(uc1);
    p1.setStartTime(before);
    p1.setExpiryTime(after);
    p1.setRig(r);
    db.persist(p1);

    Session ses1 = new Session();
    ses1.setActive(true);
    ses1.setReady(true);
    ses1.setActivityLastUpdated(now);
    ses1.setExtensions((short) 5);
    ses1.setPriority((short) 5);
    ses1.setRequestTime(now);
    ses1.setRequestedResourceId(r.getId());
    ses1.setRequestedResourceName(r.getName());
    ses1.setResourceType("RIG");
    ses1.setResourcePermission(p1);
    ses1.setUser(user1);
    ses1.setUserName(user1.getName());
    ses1.setUserNamespace(user1.getNamespace());
    db.persist(ses1);

    db.getTransaction().commit();

    Queue.getInstance().addEntry(ses1, db);

    r.setOnline(true);
    r.setInSession(false);
    db.beginTransaction();
    db.flush();
    db.getTransaction().commit();

    QueueRun.attemptAssignment(r.getId());

    db.refresh(ses1);
    db.refresh(r);

    db.beginTransaction();
    db.delete(ses1);
    db.delete(p1);
    db.delete(r);
    db.delete(rt);
    db.delete(caps);
    db.delete(ass1);
    db.delete(uc1);
    db.delete(user1);
    db.getTransaction().commit();

    Field f = Queue.class.getDeclaredField("rigQueues");
    f.setAccessible(true);
    Map<Long, InnerQueue> que = (Map<Long, InnerQueue>) f.get(Queue.getInstance());
    assertNotNull(que);
    assertEquals(0, que.size());

    assertTrue(r.isInSession());
    assertNotNull(ses1.getAssignmentTime());
    assertEquals(r.getId(), ses1.getRig().getId());
    assertEquals(r.getName(), ses1.getAssignedRigName());
  }