Java Code Examples for org.apache.reef.util.Optional#empty()

The following examples show how to use org.apache.reef.util.Optional#empty() . 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: DefaultVortexMaster.java    From reef with Apache License 2.0 6 votes vote down vote up
/**
 * Add a new tasklet to pendingTasklets.
 */
@Override
public <TInput, TOutput> VortexFuture<TOutput>
    enqueueTasklet(final VortexFunction<TInput, TOutput> function, final TInput input,
                   final Optional<FutureCallback<TOutput>> callback) {
  // TODO[REEF-500]: Simple duplicate Vortex Tasklet launch.
  final VortexFuture<TOutput> vortexFuture;
  final int id = taskletIdCounter.getAndIncrement();
  if (callback.isPresent()) {
    vortexFuture = new VortexFuture<>(executor, this, id, callback.get());
  } else {
    vortexFuture = new VortexFuture<>(executor, this, id);
  }

  final Tasklet tasklet = new Tasklet<>(id, Optional.<Integer>empty(), function, input, vortexFuture);
  putDelegate(Collections.singletonList(tasklet), vortexFuture);
  this.pendingTasklets.addLast(tasklet);

  return vortexFuture;
}
 
Example 2
Source File: MockRuntimeDriver.java    From reef with Apache License 2.0 6 votes vote down vote up
@Override
public void fail(final AllocatedEvaluator evaluator) {
  if (this.clock.get().isClosed()) {
    throw new IllegalStateException("clock is closed");
  }
  if (this.allocatedEvaluatorMap.remove(evaluator.getId()) == null) {
    throw new IllegalStateException("unknown evaluator " + evaluator);
  }
  FailedTask failedTask = null;
  if (this.runningTasks.containsKey(evaluator.getId())) {
    final RunningTask task = this.runningTasks.remove(evaluator.getId());
    failedTask = new FailedTask(
        task.getId(),
        "mock",
        Optional.<String>empty(),
        Optional.<Throwable>empty(),
        Optional.<byte[]>empty(),
        Optional.<ActiveContext>of(task.getActiveContext()));
  }
  final List<FailedContext> failedContexts = new ArrayList<>();
  for (final MockActiveContext context : this.allocatedContextsMap.get(evaluator.getId())) {
    failedContexts.add(new MockFailedContext(context));
  }
  post(this.failedEvaluatorHandlers, new MockFailedEvaluator(
      evaluator.getId(), failedContexts, Optional.ofNullable(failedTask)));
}
 
Example 3
Source File: ContextRepresenters.java    From reef with Apache License 2.0 6 votes vote down vote up
/**
 * Create and add a new context representer.
 *
 * @param contextStatus             the message to create the context from
 * @param notifyClientOnNewActiveContext whether or not to fire an event to the user.
 */
private synchronized void onNewContext(final ContextStatusPOJO contextStatus,
                                       final boolean notifyClientOnNewActiveContext) {
  final String contextID = contextStatus.getContextId();
  LOG.log(Level.FINE, "Adding new context {0}.", contextID);

  final Optional<String> parentID = contextStatus.hasParentId() ?
      Optional.of(contextStatus.getParentId()) : Optional.<String>empty();
  final EvaluatorContext context = contextFactory.newContext(contextID, parentID);
  this.addContext(context);
  if (notifyClientOnNewActiveContext) {
    if (driverRestartManager.getEvaluatorRestartState(context.getEvaluatorId())
        == EvaluatorRestartState.REREGISTERED) {
      // if restart, advance restart state and all the restart context active handlers.
      driverRestartManager.setEvaluatorProcessed(context.getEvaluatorId());
      this.messageDispatcher.onDriverRestartContextActive(context);
    }

    this.messageDispatcher.onContextActive(context);
  }
}
 
Example 4
Source File: MockAllocatedEvaluator.java    From reef with Apache License 2.0 5 votes vote down vote up
@Override
public void submitContext(final Configuration contextConfiguration) {
  if (this.rootContext != null) {
    throw new IllegalStateException("Root context already created");
  }
  final String rootContextID = MockUtils.getValue(contextConfiguration, ContextIdentifier.class);
  this.rootContext = new MockActiveContext(
      this.mockRuntimeDriver,
      this,
      Optional.<MockActiveContext>empty(),
      rootContextID);
  this.mockRuntimeDriver.add(new CreateContext(this.rootContext));
}
 
Example 5
Source File: ContainerManager.java    From reef with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the rack where to allocate the container, selected from the list of
 * preferred rack names. If the list is empty, and there's space in the default
 * rack, then the default rack is returned. The relax locality semantic is
 * enabled if the list of rack names contains '/*', otherwise relax locality
 * is considered disabled.
 *
 * @param rackNames the list of preferred racks.
 * @return the rack name where to allocate the container.
 */
private Optional<String> getPreferredRack(final List<String> rackNames) {

  for (final String rackName : getRackNamesOrDefault(rackNames)) {

    // if it does not end with the any modifier, then we should do an exact match
    if (!rackName.endsWith(Constants.ANY_RACK)) {
      if (freeNodesPerRack.containsKey(rackName) && freeNodesPerRack.get(rackName).size() > 0) {
        return Optional.of(rackName);
      }
    } else {

      // if ends with the any modifier, we do a prefix match
      for (final String possibleRackName : this.availableRacks) {

        // remove the any modifier
        final String newRackName = rackName.substring(0, rackName.length() - 1);

        if (possibleRackName.startsWith(newRackName) &&
            this.freeNodesPerRack.get(possibleRackName).size() > 0) {
          return Optional.of(possibleRackName);
        }
      }
    }
  }

  return Optional.empty();
}
 
Example 6
Source File: DefaultExceptionCodec.java    From reef with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<Throwable> fromBytes(final byte[] bytes) {
  try {
    if (bytes != null && bytes.length > 0) {
      return Optional.of((Throwable) SerializationUtils.deserialize(bytes));
    }
  } catch (final SerializationException | IllegalArgumentException e) {
    LOG.log(Level.WARNING, "Unable to deserialize a Throwable.", e);
  }
  return Optional.empty();
}
 
Example 7
Source File: CreateTask.java    From reef with Apache License 2.0 5 votes vote down vote up
@Override
public FailedTask getFailureEvent() {
  return new FailedTask(
      this.task.getId(),
      "mock",
      Optional.<String>empty(),
      Optional.<Throwable>empty(),
      Optional.<byte[]>empty(),
      Optional.of(this.task.getActiveContext()));
}
 
Example 8
Source File: ContextRuntime.java    From reef with Apache License 2.0 5 votes vote down vote up
/**
 * Called by the child context when it has been closed.
 */
private void resetChildContext() {
  synchronized (this.contextLifeCycle) {
    if (this.childContext.isPresent()) {
      this.childContext = Optional.empty();
    } else {
      throw new IllegalStateException("no child context set");
    }
  }
}
 
Example 9
Source File: FirstFitSchedulingPolicy.java    From reef with Apache License 2.0 5 votes vote down vote up
/**
 * Checking from nextIndex, choose the first worker that fits to schedule the tasklet onto.
 * @param tasklet to schedule
 * @return the next worker that has enough resources for the tasklet
 */
@Override
public Optional<String> trySchedule(final Tasklet tasklet) {
  for (int i = 0; i < idList.size(); i++) {
    final int index = (nextIndex + i) % idList.size();
    final String workerId = idList.get(index);
    
    if (idLoadMap.get(workerId) < workerCapacity) {
      nextIndex = (index + 1) % idList.size();
      return Optional.of(workerId);
    }
  }
  return Optional.empty();
}
 
Example 10
Source File: RootContextLauncher.java    From reef with Apache License 2.0 5 votes vote down vote up
@Inject
RootContextLauncher(@Parameter(RootContextConfiguration.class) final String rootContextConfiguration,
                    final Injector injector, final ConfigurationSerializer configurationSerializer)
    throws IOException, BindException {
  this.injector = injector;
  this.configurationSerializer = configurationSerializer;
  this.rootContextConfiguration = this.configurationSerializer.fromString(rootContextConfiguration);
  this.rootServiceConfiguration = Optional.empty();
  this.initialTaskConfiguration = Optional.empty();
}
 
Example 11
Source File: WrappedValue.java    From reef with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a representation of a cached entry.
 * The value is written after computing valueFetcher and the time
 * it was written is recorded using currentTime.
 *
 * @param valueFetcher method used to fetch the value
 * @param currentTime  class that returns the current time
 */
WrappedValue(final Callable<V> valueFetcher,
                    final CurrentTime currentTime) {
  this.valueFetcher = valueFetcher;
  this.currentTime = currentTime;

  this.value = Optional.empty();
  this.writeTime = Optional.empty();
}
 
Example 12
Source File: EvaluatorManager.java    From reef with Apache License 2.0 4 votes vote down vote up
/**
 * EvaluatorException will trigger is FailedEvaluator and state transition to FAILED.
 *
 * @param exception on the EvaluatorRuntime
 */
public void onEvaluatorException(final EvaluatorException exception) {
  synchronized (this.evaluatorDescriptor) {

    if (this.stateManager.isCompleted()) {
      LOG.log(Level.FINE,
          "Ignoring an exception received for Evaluator {0} which is already in state {1}.",
          new Object[] {this.getId(), this.stateManager});
      return;
    }

    LOG.log(Level.WARNING, "Failed evaluator: " + getId(), exception);

    try {

      final List<FailedContext> failedContextList = this.contextRepresenters.getFailedContextsForEvaluatorFailure();

      final Optional<FailedTask> failedTaskOptional;
      if (this.task.isPresent()) {

        final String taskId = this.task.get().getId();
        final Optional<ActiveContext> evaluatorContext = Optional.empty();
        final Optional<byte[]> bytes = Optional.empty();
        final Optional<Throwable> taskException = Optional.<Throwable>of(new Exception("Evaluator crash"));
        final String message = "Evaluator crash";
        final Optional<String> description = Optional.empty();
        final FailedTask failedTask =
            new FailedTask(taskId, message, description, taskException, bytes, evaluatorContext);

        failedTaskOptional = Optional.of(failedTask);

      } else {
        failedTaskOptional = Optional.empty();
      }

      final FailedEvaluator failedEvaluator = new FailedEvaluatorImpl(
          exception, failedContextList, failedTaskOptional, this.evaluatorId);

      if (driverRestartManager.getEvaluatorRestartState(evaluatorId).isFailedOrExpired()) {
        this.messageDispatcher.onDriverRestartEvaluatorFailed(failedEvaluator);
      } else {
        this.messageDispatcher.onEvaluatorFailed(failedEvaluator);
      }
    } catch (final Exception e) {
      LOG.log(Level.SEVERE, "Exception while handling FailedEvaluator", e);
    } finally {
      this.stateManager.setFailed();
      this.close();
    }
  }
}
 
Example 13
Source File: AggregateResult.java    From reef with Apache License 2.0 4 votes vote down vote up
AggregateResult(final TOutput aggregatedOutput,
                final List<TInput> inputList) {
  this(Optional.of(aggregatedOutput), Optional.<Exception>empty(), inputList);
}
 
Example 14
Source File: FailedContextBridge.java    From reef with Apache License 2.0 4 votes vote down vote up
@Override
public Optional<String> getParentId() {
  return this.parentContext.isPresent() ?
      Optional.of(this.parentContext.get().getId()) : Optional.<String>empty();
}
 
Example 15
Source File: MockActiveContext.java    From reef with Apache License 2.0 4 votes vote down vote up
@Override
public Optional<String> getParentId() {
  return this.parentContext.isPresent() ?
      Optional.of(this.parentContext.get().getId()) :
      Optional.<String>empty();
}
 
Example 16
Source File: MockFailedContext.java    From reef with Apache License 2.0 4 votes vote down vote up
@Override
public Optional<String> getDescription() {
  return Optional.empty();
}
 
Example 17
Source File: AggregateResult.java    From reef with Apache License 2.0 4 votes vote down vote up
AggregateResult(final Exception exception,
                final List<TInput> inputList) {
  this(Optional.<TOutput>empty(), Optional.of(exception), inputList);
}
 
Example 18
Source File: DefaultContextMessageSource.java    From reef with Apache License 2.0 4 votes vote down vote up
@Override
public Optional<ContextMessage> getMessage() {
  return Optional.empty();
}
 
Example 19
Source File: ContextRuntime.java    From reef with Apache License 2.0 2 votes vote down vote up
/**
 * Create a new ContextRuntime for the root context.
 *
 * @param serviceInjector      the serviceInjector to be used.
 * @param contextConfiguration the Configuration for this context.
 * @throws ContextClientCodeException if the context cannot be instantiated.
 */
ContextRuntime(final Injector serviceInjector,
               final Configuration contextConfiguration) throws ContextClientCodeException {
  this(serviceInjector, contextConfiguration, Optional.<ContextRuntime>empty());
  LOG.log(Level.FINEST, "Instantiating root context");
}
 
Example 20
Source File: FailedRuntime.java    From reef with Apache License 2.0 2 votes vote down vote up
/**
 * Create a new Failure object out of protobuf data.
 *
 * @param error Error message as a protocol buffers object.
 */
public FailedRuntime(final RuntimeErrorProto error) {
  super(error.getIdentifier(), error.getMessage(), Optional.<String>empty(), Optional.of(getThrowable(error)),
      Optional.<byte[]>empty());
}