Ejemplo n.º 1
0
  @SuppressWarnings("unchecked")
  private Future<?> workForBroker(
      WorkerComponent component,
      WorkerManagementClient wmc,
      String senderPublicKey,
      String brokerPubKey,
      boolean isWorkerIdle,
      boolean isWorkerOwner,
      boolean isWorkerAllocatedForPeer,
      boolean isWorkingState,
      boolean cleans,
      boolean isIncomingFile,
      boolean toDiffBroker,
      boolean isPreparingState,
      boolean isErrorState,
      boolean isLogged,
      boolean isAllocatedForBroker,
      String playpenDirWithError,
      List<TransferHandle> handles) {

    CommuneLogger oldLogger = component.getLogger();
    CommuneLogger newLogger = EasyMock.createMock(CommuneLogger.class);
    component.setLogger(newLogger);

    DeploymentID brokerID =
        new DeploymentID(new ContainerID("username", "server", "module", brokerPubKey), "broker");

    WorkerManagement workerManag = getWorkerManagement();
    ObjectDeployment wmOD = getWorkerManagementDeployment();

    ServiceID workerServiceID = createWorkerServiceID();

    FileTransferManager ftm = EasyMock.createMock(FileTransferManager.class);
    component.setFileTransferManager(ftm);

    ExecutorService newThreadPool = null;
    Future future = null;

    if (senderPublicKey != null) {
      newLogger.warn(
          "An unknown peer tried to command this Worker to work for a local consumer. This message was ignored."
              + " Unknown peer public key: ["
              + senderPublicKey
              + "].");
    } else {
      if (wmc != null) {

        if (!isLogged) {
          newLogger.warn("This Worker is not logged at peer.");
        } else {
          if (isErrorState) {
            newLogger.warn(
                "Peer commanded this Worker to work for a local consumer, but it's on error state.");
            wmc.statusChanged(WorkerStatus.ERROR);
          } else {
            if (isWorkerIdle && !isPreparingState) {
              newLogger.info(
                  "Peer commanded this Worker to work for a local consumer."
                      + " Local consumer public key: ["
                      + brokerPubKey
                      + "].");
              newLogger.debug("Status changed from IDLE to ALLOCATED_FOR_BROKER.");
              wmc.statusChangedAllocatedForBroker(workerServiceID, brokerPubKey);

            } else {

              if (!isWorkerOwner) {
                newLogger.info(
                    "Peer commanded this Worker to work for a local consumer."
                        + " Local consumer public key: ["
                        + brokerPubKey
                        + "].");

                if (cleans) {
                  newLogger.debug("Cleaning Worker playpen.");
                }

                if (playpenDirWithError != null) {
                  newLogger.error(
                      "Error while trying to clean the playpen directory ["
                          + playpenDirWithError
                          + "].");
                }

                if (handles != null) {
                  if (isIncomingFile) {
                    for (TransferHandle handle : handles) {
                      ftm.cancelIncomingTransfer((IncomingTransferHandle) handle);
                    }
                  } else {
                    for (TransferHandle handle : handles) {
                      ftm.cancelOutgoingTransfer((OutgoingTransferHandle) handle);
                    }
                  }
                  EasyMock.replay(ftm);
                }

                if (!isPreparingState) {

                  if (isWorkerAllocatedForPeer) {
                    newLogger.debug(
                        "Status changed from ALLOCATED_FOR_PEER to ALLOCATED_FOR_BROKER.");
                  }

                  if (isWorkingState && toDiffBroker) {

                    newLogger.debug(
                        "Worker begin allocation action, preparing to start the working.");

                    newThreadPool = EasyMock.createMock(ExecutorService.class);
                    component.setExecutorThreadPool(newThreadPool);

                    future = EasyMock.createMock(Future.class);

                    EasyMock.expect(
                            newThreadPool.submit(
                                BeginAllocationRunnableMatcher.eqMatcher(
                                    createBeginAllocationRunnable())))
                        .andReturn(future)
                        .once();
                    EasyMock.replay(newThreadPool);
                  } else {
                    wmc.statusChangedAllocatedForBroker(workerServiceID, brokerPubKey);
                  }
                }

              } else {
                newLogger.debug(
                    "This Worker was commanded to work for a local consumer,"
                        + " but it is in the OWNER status. This message was ignored.");
              }
            }
          }
        }
        EasyMock.replay(wmc);
      } else {
        newLogger.debug(
            "The master Peer tried to manage this Worker before setting itself as manager of this Worker."
                + " This message was ignored.");
      }
    }

    EasyMock.replay(newLogger);

    DeploymentID peerID =
        new DeploymentID(
            new ContainerID("peerUser", "peerServer", "peer", simulateAuthentication()), "broker");

    AcceptanceTestUtil.setExecutionContext(component, wmOD, peerID);

    workerManag.workForBroker(brokerID);

    EasyMock.verify(newLogger);

    if (handles != null) {
      EasyMock.verify(ftm);
    }

    if (future != null) {
      EasyMock.reset(future);
      EasyMock.expect(future.isDone()).andReturn(false).anyTimes();
      EasyMock.replay(future);
    }

    if (wmc != null) {
      EasyMock.verify(wmc);
      EasyMock.reset(wmc);
    }

    if (newThreadPool != null) {
      EasyMock.verify(newThreadPool);
      EasyMock.reset(newThreadPool);
    }

    component.setLogger(oldLogger);

    return future;
  }
Ejemplo n.º 2
0
  public void prepareAllocationCompleted(
      WorkerComponent component,
      WorkerManagementClient wmc,
      RemoteWorkerManagementClient rwmc,
      WorkerStatus oldStatus,
      boolean toIdle,
      String remotePeerPubKey,
      String brokerPubKey,
      boolean peerFailed,
      Future<?> future,
      boolean isPreparingAllocationState,
      WorkerStatus status) {

    CommuneLogger oldLogger = component.getLogger();
    CommuneLogger newLogger = EasyMock.createMock(CommuneLogger.class);
    component.setLogger(newLogger);

    if (!isPreparingAllocationState) {
      newLogger.warn(
          ExecutionControllerMessages.getNotInPrepareAllocationStateMessage(
              WorkerDAOFactory.getInstance().getWorkerStatusDAO().getStatus().toString()));
    }

    if (future != null) {
      EasyMock.reset(future);
      EasyMock.expect(future.isDone()).andReturn(true).anyTimes();
      EasyMock.replay(future);
    }

    WorkerStatus newStatus = null;

    if (wmc != null) {
      EasyMock.reset(wmc);

      if (!peerFailed) {

        if (toIdle) {
          wmc.statusChanged(WorkerStatus.IDLE);

        } else if (remotePeerPubKey != null) {
          newStatus = WorkerStatus.ALLOCATED_FOR_PEER;
          wmc.statusChangedAllocatedForPeer(
              createRemoteWorkerManagementServiceID(), remotePeerPubKey);

        } else if (brokerPubKey != null) {
          newStatus = WorkerStatus.ALLOCATED_FOR_BROKER;
          wmc.statusChangedAllocatedForBroker(createWorkerServiceID(), brokerPubKey);
        }
      }
      EasyMock.replay(wmc);
    }

    if (rwmc != null) {
      newStatus = WorkerStatus.ALLOCATED_FOR_BROKER;
      EasyMock.reset(rwmc);
      rwmc.statusChangedAllocatedForBroker(createWorkerServiceID());
      EasyMock.replay(rwmc);
    }

    if (oldStatus != null) {
      newLogger.debug("Status changed from " + oldStatus + " to " + newStatus + ".");
    }

    if (isPreparingAllocationState) {
      newLogger.debug("Allocation action was completed.");
    }

    EasyMock.replay(newLogger);

    getWorkerExecutionClient().readyForAllocation();

    if (wmc != null && isPreparingAllocationState) {
      EasyMock.verify(wmc);
      EasyMock.reset(wmc);
    }

    if (rwmc != null && isPreparingAllocationState) {
      EasyMock.verify(rwmc);
      EasyMock.reset(rwmc);
    }

    EasyMock.verify(newLogger);
    component.setLogger(oldLogger);
  }