Java Code Examples for org.apache.reef.driver.context.ActiveContext#close()

The following examples show how to use org.apache.reef.driver.context.ActiveContext#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EvaluatorReuseTestDriver.java    From reef with Apache License 2.0 6 votes vote down vote up
private void startTask(final ActiveContext context) {
  if (counter < numberOfIterations) {
    try {
      this.lastMessage = "ECHO-" + counter;
      client.sendMessageToClient(("Submitting iteration " + counter).getBytes(StandardCharsets.UTF_8));
      final String memento = DatatypeConverter.printBase64Binary(this.lastMessage.getBytes(StandardCharsets.UTF_8));
      context.submitTask(TaskConfiguration.CONF
          .set(TaskConfiguration.IDENTIFIER, this.lastMessage)
          .set(TaskConfiguration.TASK, EchoTask.class)
          .set(TaskConfiguration.MEMENTO, memento)
          .build());
      counter += 1;
    } catch (final BindException e) {
      context.close();
      throw new RuntimeException(e);
    }
  } else {
    client.sendMessageToClient("Done. Closing the Context".getBytes(StandardCharsets.UTF_8));
    context.close();
  }
}
 
Example 2
Source File: TaskResubmitDriver.java    From reef with Apache License 2.0 6 votes vote down vote up
@Override
public void onNext(final FailedTask failedTask) {

  LOG.log(Level.INFO, "FailedTask: {0}", failedTask);

  final Throwable ex = failedTask.getReason().get();
  if (!TestUtils.hasCause(ex, SimulatedTaskFailure.class)) {
    final String msg = "Expected SimulatedTaskFailure from " + failedTask.getId();
    LOG.log(Level.SEVERE, msg, ex);
    throw new TaskSideFailure(msg, ex);
  }

  final ActiveContext activeContext = failedTask.getActiveContext().get();
  if (++TaskResubmitDriver.this.failuresSeen <= 1) { // resubmit the task
    activeContext.submitTask(getTaskConfiguration());
  } else { // Close the context
    activeContext.close();
  }
}
 
Example 3
Source File: HeronMasterDriver.java    From incubator-heron with Apache License 2.0 6 votes vote down vote up
@Override
public void onNext(ActiveContext context) {
  if (isTopologyKilled.get()) {
    LOG.log(Level.WARNING, "Topology has been killed, close new context: {0}", context.getId());
    context.close();
    return;
  }

  int workerId = Integer.valueOf(context.getId());
  Optional<HeronWorker> worker = multiKeyWorkerMap.lookupByWorkerId(workerId);
  if (!worker.isPresent()) {
    context.close();
    return;
  }

  worker.get().context = context;
  submitHeronExecutorTask(workerId);
}
 
Example 4
Source File: JobDriver.java    From reef with Apache License 2.0 6 votes vote down vote up
/**
 * Submit a Task to a single Evaluator.
 */
private void submit(final ActiveContext context) {
  try {
    LOG.log(Level.INFO, "Send task to context: {0}", new Object[]{context});
    if (JobDriver.this.handlerManager.getActiveContextHandler() == 0) {
      throw new RuntimeException("Active Context Handler not initialized by CLR.");
    }
    final ActiveContextBridge activeContextBridge = activeContextBridgeFactory.getActiveContextBridge(context);
    NativeInterop.clrSystemActiveContextHandlerOnNext(JobDriver.this.handlerManager.getActiveContextHandler(),
        activeContextBridge, JobDriver.this.interopLogger);
  } catch (final Exception ex) {
    LOG.log(Level.SEVERE, "Fail to submit task to active context");
    context.close();
    throw new RuntimeException(ex);
  }
}
 
Example 5
Source File: HttpShellJobDriver.java    From reef with Apache License 2.0 6 votes vote down vote up
/**
 * Submit a Task that execute the command to a single Evaluator.
 * This method is called from <code>submitTask(cmd)</code>.
 */
private void submit(final ActiveContext context, final String command) {
  try {
    LOG.log(Level.INFO, "Send command {0} to context: {1}", new Object[]{command, context});
    final JavaConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder();
    cb.addConfiguration(
        TaskConfiguration.CONF
            .set(TaskConfiguration.IDENTIFIER, context.getId() + "_task")
            .set(TaskConfiguration.TASK, ShellTask.class)
            .build()
    );
    cb.bindNamedParameter(Command.class, command);
    context.submitTask(cb.build());
  } catch (final BindException ex) {
    LOG.log(Level.SEVERE, "Bad Task configuration for context: " + context.getId(), ex);
    context.close();
    throw new RuntimeException(ex);
  }
}
 
Example 6
Source File: SchedulerDriver.java    From reef with Apache License 2.0 6 votes vote down vote up
@Override
public void onNext(final ActiveContext context) {
  synchronized (SchedulerDriver.this) {
    LOG.log(Level.INFO, "Context available : {0}", context.getId());

    if (scheduler.hasPendingTasks()) {
      state = State.RUNNING;
      scheduler.submitTask(context);
    } else if (nActiveEval > 1) {
      nActiveEval--;
      context.close();
    } else {
      state = State.READY;
      waitForCommands(context);
    }
  }
}
 
Example 7
Source File: JobDriver.java    From reef with Apache License 2.0 5 votes vote down vote up
@Override
public void onNext(final StopTime time) {
  LOG.log(Level.INFO, " StopTime: {0}", new Object[]{time});
  try (LoggingScope ls = loggingScopeFactory.driverStop(time.getTimestamp())) {
    for (final ActiveContext context : contexts.values()) {
      context.close();
    }
  }
}
 
Example 8
Source File: HttpShellJobDriver.java    From reef with Apache License 2.0 5 votes vote down vote up
@Override
public void onNext(final StopTime time) {
  synchronized (HttpShellJobDriver.this) {
    LOG.log(Level.INFO, "{0} StopTime: {1}", new Object[]{state, time});
    for (final ActiveContext context : contexts.values()) {
      context.close();
    }
  }
}
 
Example 9
Source File: HttpShellJobDriver.java    From reef with Apache License 2.0 5 votes vote down vote up
@Override
public void onNext(final Void aVoid) throws RuntimeException {
  LOG.log(Level.INFO, "Received a close message from the client. " +
      "You can put code here to properly close drivers and evaluators.");
  for (final ActiveContext c : contexts.values()) {
    c.close();
  }
}
 
Example 10
Source File: SchedulerDriver.java    From reef with Apache License 2.0 5 votes vote down vote up
/**
 * Always close the complete evaluators and
 * allocate a new evaluator if necessary.
 */
private synchronized void reallocateEvaluator(final ActiveContext context) {
  nActiveEval--;
  context.close();

  if (scheduler.hasPendingTasks()) {
    requestEvaluator(1);
  } else if (nActiveEval <= 0) {
    state = State.WAIT_EVALUATORS;
    requestEvaluator(1);
  }
}
 
Example 11
Source File: SchedulerDriver.java    From reef with Apache License 2.0 5 votes vote down vote up
/**
 * Retain the complete evaluators submitting another task
 * until there is no need to reuse them.
 */
private synchronized void retainEvaluator(final ActiveContext context) {
  if (scheduler.hasPendingTasks()) {
    scheduler.submitTask(context);
  } else if (nActiveEval > 1) {
    nActiveEval--;
    context.close();
  } else {
    state = State.READY;
    waitForCommands(context);
  }
}
 
Example 12
Source File: BGDDriver.java    From reef with Apache License 2.0 5 votes vote down vote up
private boolean jobRunning(final ActiveContext activeContext) {
  synchronized (runningTasks) {
    if (!jobComplete.get()) {
      return true;
    } else {
      LOG.log(Level.INFO, "Job complete. Not submitting any task. Closing context {0}", activeContext);
      activeContext.close();
      return false;
    }
  }
}
 
Example 13
Source File: BroadcastDriver.java    From reef with Apache License 2.0 5 votes vote down vote up
@Override
public void onNext(final ClosedContext closedContext) {
  LOG.log(Level.FINE, "Got closed context: {0}", closedContext.getId());
  final ActiveContext parentContext = closedContext.getParentContext();
  if (parentContext != null) {
    LOG.log(Level.FINE, "Closing parent context: {0}", parentContext.getId());
    parentContext.close();
  }
}
 
Example 14
Source File: JobDriver.java    From reef with Apache License 2.0 5 votes vote down vote up
@Override
public void onNext(final CompletedTask task) {

  final ActiveContext context = task.getActiveContext();
  LOG.log(Level.INFO, "TIME: Completed Task {0} on Evaluator {1}",
      new Object[]{task.getId(), context.getEvaluatorId()});

  final boolean runTask;
  final int nTask;
  synchronized (JobDriver.this) {
    runTask = numTasksStarted < numTasks;
    if (runTask) {
      ++numTasksStarted;
    }
    nTask = numTasksStarted;
  }

  if (runTask) {
    final String taskId = String.format("Task_%08d", nTask);
    LOG.log(Level.INFO, "TIME: Submit Task {0} to Evaluator {1}",
        new Object[]{taskId, context.getEvaluatorId()});
    context.submitTask(getTaskConfiguration(taskId));
  } else {
    LOG.log(Level.INFO, "TIME: Close Evaluator {0}", context.getEvaluatorId());
    context.close();
  }
}
 
Example 15
Source File: JobDriver.java    From reef with Apache License 2.0 5 votes vote down vote up
@Override
public void onNext(final ActiveContext context) {

  LOG.log(Level.INFO, "TIME: Active Context {0}", context.getId());

  if (isPiggyback) {
    return; // Task already submitted
  }

  final boolean runTask;
  final int nTask;

  synchronized (JobDriver.this) {
    runTask = numTasksStarted < numTasks;
    if (runTask) {
      ++numTasksStarted;
    }
    nTask = numTasksStarted;
  }

  if (runTask) {
    final String taskId = String.format("StartTask_%08d", nTask);
    LOG.log(Level.INFO, "TIME: Submit Task {0} to Evaluator {1}",
        new Object[]{taskId, context.getEvaluatorId()});
    context.submitTask(getTaskConfiguration(taskId));
  } else {
    context.close();
  }
}
 
Example 16
Source File: DefaultTaskCompletionHandler.java    From reef with Apache License 2.0 5 votes vote down vote up
@Override
public void onNext(final CompletedTask completedTask) {
  final ActiveContext context = completedTask.getActiveContext();
  LOG.log(Level.INFO, "Received CompletedTask: {0} :: CLOSING context: {1}",
      new Object[]{completedTask, context});
  context.close();
}
 
Example 17
Source File: LineCounter.java    From reef with Apache License 2.0 4 votes vote down vote up
@Override
public void onNext(final ActiveContext activeContext) {

  final String contextId = activeContext.getId();
  LOG.log(Level.FINER, "Context active: {0}", contextId);

  if (dataLoadingService.isDataLoadedContext(activeContext)) {

    final String lcContextId = "LineCountCtxt-" + ctrlCtxIds.getAndIncrement();
    LOG.log(Level.FINEST, "Submit LineCount context {0} to: {1}",
        new Object[]{lcContextId, contextId});

    final Configuration poisonedConfiguration = PoisonedConfiguration.CONTEXT_CONF
        .set(PoisonedConfiguration.CRASH_PROBABILITY, "0.4")
        .set(PoisonedConfiguration.CRASH_TIMEOUT, "1")
        .build();

    activeContext.submitContext(Tang.Factory.getTang()
        .newConfigurationBuilder(poisonedConfiguration,
            ContextConfiguration.CONF.set(ContextConfiguration.IDENTIFIER, lcContextId).build())
        .build());

  } else if (activeContext.getId().startsWith("LineCountCtxt")) {

    final String taskId = "LineCountTask-" + ctrlCtxIds.getAndIncrement();
    LOG.log(Level.FINEST, "Submit LineCount task {0} to: {1}", new Object[]{taskId, contextId});

    try {
      activeContext.submitTask(TaskConfiguration.CONF
          .set(TaskConfiguration.IDENTIFIER, taskId)
          .set(TaskConfiguration.TASK, LineCountingTask.class)
          .build());
    } catch (final BindException ex) {
      LOG.log(Level.SEVERE, "Configuration error in " + contextId, ex);
      throw new RuntimeException("Configuration error in " + contextId, ex);
    }
  } else {
    LOG.log(Level.FINEST, "Line count Compute Task {0} -- Closing", contextId);
    activeContext.close();
  }
}
 
Example 18
Source File: DefaultDriverRestartContextActiveHandler.java    From reef with Apache License 2.0 4 votes vote down vote up
@Override
public void onNext(final ActiveContext activeContext) {
  LOG.log(Level.INFO, "Received ActiveContext running on previous Evaluator during driver restart: {0} :: CLOSING",
      activeContext);
  activeContext.close();
}
 
Example 19
Source File: GRPCDriverService.java    From reef with Apache License 2.0 4 votes vote down vote up
@Override
public void activeContextOp(
    final ActiveContextRequest request,
    final StreamObserver<Void> responseObserver) {
  LOG.log(Level.INFO, "Active context operation {0}", request.getOperationCase());
  synchronized (GRPCDriverService.this) {
    LOG.log(Level.INFO, "i'm in");
    final String contextId = request.getContextId();
    final ActiveContext context = GRPCDriverService.this.activeContextMap.get(contextId);
    if (context == null) {
      LOG.log(Level.SEVERE, "Context does not exist with id {0}", contextId);
      responseObserver.onError(Status.INTERNAL
          .withDescription("Context does not exist with id " + contextId)
          .asRuntimeException());
      return;
    }
    switch (request.getOperationCase()) {
    case CLOSE_CONTEXT:
      if (request.getCloseContext()) {
        try (ObserverCleanup cleanup = ObserverCleanup.of(responseObserver)) {
          LOG.log(Level.INFO, "closing context {0}", context.getId());
          context.close();
        }
      } else {
        LOG.log(Level.SEVERE, "Close context operation not set to true");
        responseObserver.onError(Status.INTERNAL
            .withDescription("Close context operation not set to true")
            .asRuntimeException());
      }
      break;
    case MESSAGE:
      if (request.getMessage() != null) {
        try (ObserverCleanup cleanup = ObserverCleanup.of(responseObserver)) {
          LOG.log(Level.INFO, "send message to context {0}", context.getId());
          context.sendMessage(request.getMessage().toByteArray());
        }
      } else {
        responseObserver.onError(Status.INTERNAL
            .withDescription("Empty message on operation send message").asRuntimeException());
      }
      break;
    case NEW_CONTEXT_REQUEST:
      try (ObserverCleanup cleanup = ObserverCleanup.of(responseObserver)) {
        LOG.log(Level.INFO, "submitting child context to context {0}", context.getId());
        ((EvaluatorContext) context).submitContext(request.getNewContextRequest());
      }
      break;
    case NEW_TASK_REQUEST:
      try (ObserverCleanup cleanup = ObserverCleanup.of(responseObserver)) {
        LOG.log(Level.INFO, "submitting task to context {0}", context.getId());
        ((EvaluatorContext) context).submitTask(request.getNewTaskRequest());
      }
      break;
    default:
      throw new RuntimeException("Unknown operation " + request.getOperationCase());
    }
  }
}
 
Example 20
Source File: DefaultContextActiveHandler.java    From reef with Apache License 2.0 4 votes vote down vote up
@Override
public void onNext(final ActiveContext activeContext) {
  LOG.log(Level.INFO, "Received ActiveContext: {0} :: CLOSING", activeContext);
  activeContext.close();
}