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

The following examples show how to use org.apache.reef.util.Optional#of() . 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: ContextRuntime.java    From reef with Apache License 2.0 6 votes vote down vote up
/**
 * Spawns a new context without services of its own.
 * <p>
 * The new context will have a serviceInjector that is created by forking the one in this object. The
 * contextConfiguration is used to fork the contextInjector from that new serviceInjector.
 *
 * @param contextConfiguration the new context's context (local) Configuration.
 * @return a child context.
 * @throws ContextClientCodeException If the context can't be instantiate due to user code / configuration issues.
 * @throws IllegalStateException      If this method is called when there is either a task or child context already
 *                                    present.
 */
ContextRuntime spawnChildContext(
    final Configuration contextConfiguration) throws ContextClientCodeException {

  synchronized (this.contextLifeCycle) {

    if (this.task.isPresent()) {
      throw new IllegalStateException(
          "Attempting to to spawn a child context while a Task with id '" +
              this.task.get().getId() + "' is running.");
    }

    if (this.childContext.isPresent()) {
      throw new IllegalStateException(
          "Attempting to spawn a child context on a context that is not the topmost active context");
    }

    final Injector childServiceInjector = this.serviceInjector.forkInjector();
    final ContextRuntime newChildContext =
        new ContextRuntime(childServiceInjector, contextConfiguration, Optional.of(this));

    this.childContext = Optional.of(newChildContext);
    return newChildContext;
  }
}
 
Example 2
Source File: AllocatedEvaluatorImpl.java    From reef with Apache License 2.0 6 votes vote down vote up
/**
 * Make configuration for evaluator.
 * @param contextConfiguration
 * @param serviceConfiguration
 * @param taskConfiguration
 * @return Configuration
 */
private Configuration makeEvaluatorConfiguration(final Configuration contextConfiguration,
                                                 final Optional<Configuration> serviceConfiguration,
                                                 final Optional<Configuration> taskConfiguration) {

  final String contextConfigurationString = this.configurationSerializer.toString(contextConfiguration);

  final Optional<String> taskConfigurationString;
  if (taskConfiguration.isPresent()) {
    taskConfigurationString = Optional.of(this.configurationSerializer.toString(taskConfiguration.get()));
  } else {
    taskConfigurationString = Optional.empty();
  }

  final Optional<Configuration> mergedServiceConfiguration = makeRootServiceConfiguration(serviceConfiguration);
  if (mergedServiceConfiguration.isPresent()) {
    final String serviceConfigurationString = this.configurationSerializer.toString(mergedServiceConfiguration.get());
    return makeEvaluatorConfiguration(contextConfigurationString, Optional.<String>empty(),
        Optional.of(serviceConfigurationString), taskConfigurationString);
  } else {
    return makeEvaluatorConfiguration(
        contextConfigurationString, Optional.<String>empty(), Optional.<String>empty(), taskConfigurationString);
  }
}
 
Example 3
Source File: EvaluatorContext.java    From reef with Apache License 2.0 6 votes vote down vote up
/**
 * @return a FailedContext for the case of an EvaluatorFailure.
 */
public synchronized FailedContext getFailedContextForEvaluatorFailure() {

  final String id = this.getId();
  final Optional<String> description = Optional.empty();
  final Optional<byte[]> data = Optional.empty();
  final Optional<Throwable> cause = Optional.empty();
  final String message = "Evaluator Failure";

  final Optional<ActiveContext> parentContext = getParentId().isPresent() ?
      Optional.<ActiveContext>of(this.contextRepresenters.getContext(getParentId().get())) :
      Optional.<ActiveContext>empty();

  final String evaluatorID = getEvaluatorId();

  return new FailedContextImpl(
      id, message, description, cause, data, parentContext, this.evaluatorDescriptor, evaluatorID);
}
 
Example 4
Source File: GRPCDriverClientService.java    From reef with Apache License 2.0 5 votes vote down vote up
private ActiveContextBridge toActiveContext(final ContextInfo contextInfo) {
  final AllocatedEvaluatorBridge eval = this.evaluatorBridgeMap.get(contextInfo.getEvaluatorId());
  return new ActiveContextBridge(
      this.driverServiceClient,
      contextInfo.getContextId(),
      StringUtils.isNotEmpty(contextInfo.getParentId()) ?
          Optional.of(contextInfo.getParentId()) : Optional.<String>empty(),
      eval.getId(),
      eval.getEvaluatorDescriptor());
}
 
Example 5
Source File: NoopTask.java    From reef with Apache License 2.0 5 votes vote down vote up
@Override
public void onNext(final DriverMessage driverMessage) {
  final byte[] msg = driverMessage.get().get();
  LOG.log(Level.INFO, "NoopTask.DriverMessageHandler.send() invoked: {0}", CODEC.decode(msg));
  synchronized (NoopTask.this) {
    NoopTask.this.message = Optional.of(TaskMessage.from(NoopTask.this.toString(), msg));
  }
}
 
Example 6
Source File: DefaultVortexMaster.java    From reef with Apache License 2.0 5 votes vote down vote up
/**
 * Add aggregate-able Tasklets to pendingTasklets.
 */
@Override
public <TInput, TOutput> VortexAggregateFuture<TInput, TOutput>
    enqueueTasklets(final VortexAggregateFunction<TOutput> aggregateFunction,
                    final VortexFunction<TInput, TOutput> vortexFunction,
                    final VortexAggregatePolicy policy,
                    final List<TInput> inputs,
                    final Optional<FutureCallback<AggregateResult<TInput, TOutput>>> callback) {
  final int aggregateFunctionId = aggregateIdCounter.getAndIncrement();
  aggregateFunctionRepository.put(aggregateFunctionId, aggregateFunction, policy);
  final List<Tasklet> tasklets = new ArrayList<>(inputs.size());
  final Map<Integer, TInput> taskletIdInputMap = new HashMap<>(inputs.size());

  for (final TInput input : inputs) {
    taskletIdInputMap.put(taskletIdCounter.getAndIncrement(), input);
  }

  final VortexAggregateFuture<TInput, TOutput> vortexAggregateFuture;
  if (callback.isPresent()) {
    vortexAggregateFuture = new VortexAggregateFuture<>(executor, taskletIdInputMap, callback.get());
  } else {
    vortexAggregateFuture = new VortexAggregateFuture<>(executor, taskletIdInputMap, null);
  }

  for (final Map.Entry<Integer, TInput> taskletIdInputEntry : taskletIdInputMap.entrySet()) {
    final Tasklet tasklet = new Tasklet<>(taskletIdInputEntry.getKey(), Optional.of(aggregateFunctionId),
        vortexFunction, taskletIdInputEntry.getValue(), vortexAggregateFuture);
    tasklets.add(tasklet);
    pendingTasklets.addLast(tasklet);
  }

  putDelegate(tasklets, vortexAggregateFuture);
  return vortexAggregateFuture;
}
 
Example 7
Source File: CreateContextAndTask.java    From reef with Apache License 2.0 5 votes vote down vote up
@Override
public Tuple<MockFailedContext, FailedTask> getFailureEvent() {
  return new Tuple<>(
      new MockFailedContext(this.context),
      new FailedTask(
          this.task.getId(),
          "mock",
          Optional.<String>empty(),
          Optional.<Throwable>empty(),
          Optional.<byte[]>empty(),
          Optional.of((ActiveContext)this.context)));
}
 
Example 8
Source File: TaskStatus.java    From reef with Apache License 2.0 5 votes vote down vote up
void setException(final Throwable throwable) {
  synchronized (this.heartBeatManager) {
    this.lastException = Optional.of(throwable);
    this.state = State.FAILED;
    this.check();
    this.heartbeat();
  }
}
 
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: SuspendTask.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 11
Source File: CloseTask.java    From reef with Apache License 2.0 5 votes vote down vote up
@Override
public FailedTask getFailureEvent() {
  return new FailedTask(
    task.getId(),
    "mock",
    Optional.<String>empty(),
    Optional.<Throwable>empty(),
    Optional.<byte[]>empty(),
    Optional.of(this.task.getActiveContext()));
}
 
Example 12
Source File: DriverMessaging.java    From reef with Apache License 2.0 5 votes vote down vote up
@Override
public void onNext(final RunningJob job) {
  LOG.log(Level.INFO, "The Job {0} is running", job.getId());
  synchronized (DriverMessaging.this) {
    DriverMessaging.this.status = LauncherStatus.RUNNING;
    DriverMessaging.this.theJob = Optional.of(job);
    DriverMessaging.this.lastMessage = "Hello, REEF!";
    DriverMessaging.this.theJob.get().send(DriverMessaging.this.lastMessage.getBytes(StandardCharsets.UTF_8));
  }
}
 
Example 13
Source File: LocalClasspathProvider.java    From reef with Apache License 2.0 5 votes vote down vote up
/**
 * @return the path to "JAVA_HOME", if that is set. Optional.empty(), else.
 */
private static Optional<Path> getJavaHome() {
  final Optional<String> javaHome = getEnv("JAVA_HOME");

  if (javaHome.isPresent()) {
    final File javaHomeFile = new File(javaHome.get());
    if (javaHomeFile.exists()) {
      return Optional.of(javaHomeFile.toPath());
    }
  }
  return Optional.empty();
}
 
Example 14
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 15
Source File: WatcherTestTask.java    From reef with Apache License 2.0 4 votes vote down vote up
@Override
public Optional<TaskMessage> getMessage() {
  return Optional.of(taskMessage);
}
 
Example 16
Source File: ContextRuntime.java    From reef with Apache License 2.0 4 votes vote down vote up
/**
 * Spawns a new context.
 * <p>
 * The new context will have a serviceInjector that is created by forking the one in this object with the given
 * serviceConfiguration. The contextConfiguration is used to fork the contextInjector from that new serviceInjector.
 *
 * @param contextConfiguration the new context's context (local) Configuration.
 * @param serviceConfiguration the new context's service Configuration.
 * @return a child context.
 * @throws ContextClientCodeException If the context can't be instantiate due to user code / configuration issues
 * @throws IllegalStateException      If this method is called when there is either a task or child context already
 *                                    present.
 */
ContextRuntime spawnChildContext(
    final Configuration contextConfiguration,
    final Configuration serviceConfiguration) throws ContextClientCodeException {

  synchronized (this.contextLifeCycle) {

    if (this.task.isPresent()) {
      throw new IllegalStateException(
          "Attempting to spawn a child context when a Task with id '" +
              this.task.get().getId() + "' is running.");
    }

    if (this.childContext.isPresent()) {
      throw new IllegalStateException(
          "Attempting to instantiate a child context on a context that is not the topmost active context");
    }

    try {

      final Injector childServiceInjector =
          this.serviceInjector.forkInjector(serviceConfiguration);

      final ContextRuntime newChildContext =
          new ContextRuntime(childServiceInjector, contextConfiguration, Optional.of(this));

      this.childContext = Optional.of(newChildContext);
      return newChildContext;

    } catch (final BindException e) {

      final Optional<String> parentID = this.getParentContext().isPresent() ?
          Optional.of(this.getParentContext().get().getIdentifier()) :
          Optional.<String>empty();

      throw new ContextClientCodeException(
          ContextClientCodeException.getIdentifier(contextConfiguration),
          parentID, "Unable to spawn context", e);
    }
  }
}
 
Example 17
Source File: ClosedContextBridge.java    From reef with Apache License 2.0 4 votes vote down vote up
@Override
public Optional<String> getParentId() {
  return Optional.of(parentContext.getId());
}
 
Example 18
Source File: CLRLaunchCommandBuilder.java    From reef with Apache License 2.0 4 votes vote down vote up
@Override
public CLRLaunchCommandBuilder setConfigurationFilePaths(final List<String> configurationFilePaths) {
  this.evaluatorConfigurationPaths = Optional.of(configurationFilePaths);
  return this;
}
 
Example 19
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 20
Source File: FailedContextBridge.java    From reef with Apache License 2.0 4 votes vote down vote up
@Override
public Optional<String> getDescription() {
  return Optional.of(message);
}