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

The following examples show how to use org.apache.reef.util.Optional#isPresent() . 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: JobLauncher.java    From nemo with Apache License 2.0 6 votes vote down vote up
/**
 * Launch application using the application DAG.
 * @param dag the application DAG.
 */
// When modifying the signature of this method, see CompilerTestUtil#compileDAG and make corresponding changes
public static void launchDAG(final DAG dag) {
  try {
    if (jobAndDriverConf == null || deployModeConf == null) {
      throw new RuntimeException("Configuration for launching driver is not ready");
    }
    final String serializedDAG = Base64.getEncoder().encodeToString(SerializationUtils.serialize(dag));
    final Configuration dagConf = TANG.newConfigurationBuilder()
        .bindNamedParameter(JobConf.SerializedDAG.class, serializedDAG)
        .build();
    // Launch and wait indefinitely for the job to finish
    final LauncherStatus launcherStatus =  DriverLauncher.getLauncher(deployModeConf)
        .run(Configurations.merge(jobAndDriverConf, dagConf));
    final Optional<Throwable> possibleError = launcherStatus.getError();
    if (possibleError.isPresent()) {
      throw new RuntimeException(possibleError.get());
    } else {
      LOG.info("Job successfully completed");
    }
  } catch (final InjectionException e) {
    throw new RuntimeException(e);
  }
}
 
Example 2
Source File: JobDriver.java    From reef with Apache License 2.0 6 votes vote down vote up
@Override
public void onNext(final FailedContext context) {
  LOG.log(Level.SEVERE, "FailedContext", context);
  try (LoggingScope ls = loggingScopeFactory.evaluatorFailed(context.getId())) {
    if (JobDriver.this.handlerManager.getFailedContextHandler() != 0) {
      final FailedContextBridge failedContextBridge = new FailedContextBridge(context, activeContextBridgeFactory);
      // if CLR implements the failed context handler, handle it in CLR
      LOG.log(Level.INFO, "Handling the event of failed context in CLR bridge.");
      NativeInterop.clrSystemFailedContextHandlerOnNext(JobDriver.this.handlerManager.getFailedContextHandler(),
          failedContextBridge);
    }
    JobDriver.this.contexts.remove(context.getId());
    final Optional<byte[]> err = context.getData();
    if (err.isPresent()) {
      JobDriver.this.jobMessageObserver.sendMessageToClient(err.get());
    }
  }
}
 
Example 3
Source File: AllocatedEvaluatorImpl.java    From reef with Apache License 2.0 6 votes vote down vote up
/**
 * Merges the Configurations provided by the evaluatorConfigurationProviders into the given
 * serviceConfiguration, if any.
 */
private Optional<Configuration> makeRootServiceConfiguration(final Optional<Configuration> serviceConfiguration) {
  final EvaluatorType evaluatorType = this.evaluatorManager.getEvaluatorDescriptor().getProcess().getType();
  if (EvaluatorType.CLR == evaluatorType) {
    LOG.log(Level.FINE, "Not using the ConfigurationProviders as we are configuring a {0} Evaluator.", evaluatorType);
    return serviceConfiguration;
  }

  if (!serviceConfiguration.isPresent() && this.evaluatorConfigurationProviders.isEmpty()) {
    // No configurations to merge.
    LOG.info("No service configuration given and no ConfigurationProviders set.");
    return Optional.empty();
  } else {
    final ConfigurationBuilder configurationBuilder = getConfigurationBuilder(serviceConfiguration);
    for (final ConfigurationProvider configurationProvider : this.evaluatorConfigurationProviders) {
      configurationBuilder.addConfiguration(configurationProvider.getConfiguration());
    }
    return Optional.of(configurationBuilder.build());
  }
}
 
Example 4
Source File: HeartBeatManager.java    From reef with Apache License 2.0 6 votes vote down vote up
private EvaluatorRuntimeProtocol.EvaluatorHeartbeatProto getEvaluatorHeartbeatProto(
    final ReefServiceProtos.EvaluatorStatusProto evaluatorStatusProto,
    final Iterable<ReefServiceProtos.ContextStatusProto> contextStatusProtos,
    final Optional<ReefServiceProtos.TaskStatusProto> taskStatusProto) {

  final EvaluatorRuntimeProtocol.EvaluatorHeartbeatProto.Builder builder =
      EvaluatorRuntimeProtocol.EvaluatorHeartbeatProto.newBuilder()
          .setTimestamp(System.currentTimeMillis())
          .setEvaluatorStatus(evaluatorStatusProto);

  for (final ReefServiceProtos.ContextStatusProto contextStatusProto : contextStatusProtos) {
    builder.addContextStatus(contextStatusProto);
  }

  if (taskStatusProto.isPresent()) {
    builder.setTaskStatus(taskStatusProto.get());
  }

  return builder.build();
}
 
Example 5
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 6
Source File: EvaluatorResourceManagerErrorHandler.java    From reef with Apache License 2.0 6 votes vote down vote up
@Override
public void onNext(final RemoteMessage<ReefServiceProtos.RuntimeErrorProto> runtimeErrorProtoRemoteMessage) {
  final ReefServiceProtos.RuntimeErrorProto runtimeErrorProto = runtimeErrorProtoRemoteMessage.getMessage();
  final FailedRuntime error = new FailedRuntime(runtimeErrorProto);
  final String evaluatorId = error.getId();
  LOG.log(Level.WARNING, "Runtime error: " + error);

  final EvaluatorException evaluatorException = error.getReason().isPresent() ?
      new EvaluatorException(evaluatorId, error.getReason().get()) :
      new EvaluatorException(evaluatorId, "Runtime error");

  final Optional<EvaluatorManager> evaluatorManager = this.evaluators.get(evaluatorId);
  if (evaluatorManager.isPresent()) {
    evaluatorManager.get().onEvaluatorException(evaluatorException);
  } else {
    if (this.evaluators.wasClosed(evaluatorId)) {
      LOG.log(Level.WARNING, "Evaluator [" + evaluatorId + "] has raised exception after it was closed.");
    } else {
      LOG.log(Level.WARNING, "Unknown evaluator runtime error: " + error);
    }
  }
}
 
Example 7
Source File: DriverStatusManager.java    From reef with Apache License 2.0 5 votes vote down vote up
/**
 * Sends the final message to the Driver. This is used by DriverRuntimeStopHandler.onNext().
 * @param exception Exception that caused the job to end (can be absent).
 * @deprecated TODO[JIRA REEF-1548] Do not use DriverStatusManager as a proxy to the job client.
 * After release 0.16, make this method private and use it inside onRuntimeStop() method instead.
 */
@Deprecated
public synchronized void sendJobEndingMessageToClient(final Optional<Throwable> exception) {

  if (!this.isClosing()) {
    LOG.log(Level.SEVERE, "Sending message in a state different that SHUTTING_DOWN or FAILING. " +
        "This is likely a illegal call to clock.close() at play. Current state: {0}", this.driverStatus);
  }

  if (this.driverTerminationHasBeenCommunicatedToClient) {
    LOG.log(Level.SEVERE, ".sendJobEndingMessageToClient() called twice. Ignoring the second call");
    return;
  }

  // Log the shutdown situation
  if (this.shutdownCause.isPresent()) {
    LOG.log(Level.WARNING, "Sending message about an unclean driver shutdown.", this.shutdownCause.get());
  }

  if (exception.isPresent()) {
    LOG.log(Level.WARNING, "There was an exception during clock.close().", exception.get());
  }

  if (this.shutdownCause.isPresent() && exception.isPresent()) {
    LOG.log(Level.WARNING, "The driver is shutdown because of an exception (see above) and there was " +
        "an exception during clock.close(). Only the first exception will be sent to the client");
  }

  // Send the earlier exception, if there was one. Otherwise, send the exception passed.
  this.clientConnection.send(getJobEndingMessage(
      this.shutdownCause.isPresent() ? this.shutdownCause : exception));

  this.driverTerminationHasBeenCommunicatedToClient = true;
}
 
Example 8
Source File: JobLauncher.java    From incubator-nemo with Apache License 2.0 5 votes vote down vote up
public static void shutdown() {
  // Trigger driver shutdown afterwards
  driverRPCServer.send(ControlMessage.ClientToDriverMessage.newBuilder()
    .setType(ControlMessage.ClientToDriverMessageType.DriverShutdown).build());
  // Wait for driver to naturally finish
  synchronized (driverLauncher) {
    while (!driverLauncher.getStatus().isDone()) {
      try {
        LOG.info("Wait for the driver to finish");
        driverLauncher.wait();
      } catch (final InterruptedException e) {
        LOG.warn(INTERRUPTED, e);
        // clean up state...
        Thread.currentThread().interrupt();
      }
    }
    LOG.info("Driver terminated");
  }

  // Close everything that's left
  driverRPCServer.shutdown();
  driverLauncher.close();
  isSetUp = false;
  final Optional<Throwable> possibleError = driverLauncher.getStatus().getError();
  if (possibleError.isPresent()) {
    throw new RuntimeException(possibleError.get());
  } else if (jobDoneLatch.getCount() > 0) {
    LOG.info("Job cancelled");
  } else {
    LOG.info("Job successfully completed");
  }
}
 
Example 9
Source File: ResourceStatusHandler.java    From reef with Apache License 2.0 5 votes vote down vote up
/**
 * This resource status message comes from the ResourceManager layer, telling me what it thinks
 * about the state of the resource executing an Evaluator. This method simply passes the message
 * off to the referenced EvaluatorManager
 *
 * @param resourceStatusEvent resource status message from the ResourceManager
 */
@Override
public void onNext(final ResourceStatusEvent resourceStatusEvent) {

  final String id = resourceStatusEvent.getIdentifier();
  final Optional<EvaluatorManager> evaluatorManager = this.evaluators.get(id);

  LOG.log(Level.FINEST, "Evaluator {0} status: {1}",
      new Object[] {evaluatorManager, resourceStatusEvent.getState()});

  if (evaluatorManager.isPresent()) {
    final EvaluatorManager evaluatorManagerImpl = evaluatorManager.get();
    evaluatorManagerImpl.onResourceStatusMessage(resourceStatusEvent);
    if (evaluatorManagerImpl.isClosed()) {
      this.evaluators.removeClosedEvaluator(evaluatorManagerImpl);
    }

  } else {

    if (this.evaluators.wasClosed(id)) {
      LOG.log(Level.WARNING,
          "Unexpected resource status from closed evaluator {0} with state {1}",
          new Object[] {id, resourceStatusEvent.getState()});
    }

    if (driverRestartManager.get().getEvaluatorRestartState(id).isFailedOrExpired()) {

      final EvaluatorManager previousEvaluatorManager = this.evaluatorManagerFactory
          .getNewEvaluatorManagerForEvaluatorFailedDuringDriverRestart(resourceStatusEvent);

      previousEvaluatorManager.onResourceStatusMessage(resourceStatusEvent);

    } else {
      throw new RuntimeException(
          "Unknown resource status from evaluator " + id + " with state " + resourceStatusEvent.getState());
    }
  }
}
 
Example 10
Source File: CacheImpl.java    From reef with Apache License 2.0 5 votes vote down vote up
private void expireEntriesAtTime(final long now) {
  for (final Entry<K, WrappedValue<V>> entry : internalMap.entrySet()) {
    if (entry.getValue() != null) {
      final Optional<Long> writeTime = entry.getValue().getWriteTime();
      if (writeTime.isPresent() && writeTime.get() + timeoutMillis < now) {
        invalidate(entry.getKey());
      }
    }
  }
}
 
Example 11
Source File: DefaultVortexMaster.java    From reef with Apache License 2.0 5 votes vote down vote up
/**
 * Remove the worker from runningWorkers and add back the lost tasklets to pendingTasklets.
 */
@Override
public void workerPreempted(final String id) {
  final Optional<Collection<Tasklet>> preemptedTasklets = runningWorkers.removeWorker(id);
  if (preemptedTasklets.isPresent()) {
    for (final Tasklet tasklet : preemptedTasklets.get()) {
      pendingTasklets.addFirst(tasklet);
    }
  }
}
 
Example 12
Source File: RemoteNodeManager.java    From reef with Apache License 2.0 5 votes vote down vote up
void onResourceRequest(final ResourceRequestEvent resourceRequestEvent) {
  final Optional<String> node = selectNode(resourceRequestEvent);
  final String nodeId;

  if (node.isPresent()) {
    nodeId = node.get();
  } else {
    // Allocate new container
    nodeId = this.getNode() + ":" + String.valueOf(sshPortNum);
  }

  final String processID = nodeId + "-" + String.valueOf(System.currentTimeMillis());
  final File processFolder = new File(this.rootFolder, processID);

  final SshProcessContainer sshProcessContainer = new SshProcessContainer(errorHandlerRID, nodeId, processID,
      processFolder, resourceRequestEvent.getMemorySize().get(), resourceRequestEvent.getVirtualCores().get(),
      null, this.fileNames, this.nodeFolder, this.processObserver, this.containerThreads);

  this.containers.put(processID, sshProcessContainer);

  final ResourceAllocationEvent alloc = ResourceEventImpl.newAllocationBuilder()
      .setIdentifier(processID)
      .setNodeId(nodeId)
      .setResourceMemory(resourceRequestEvent.getMemorySize().get())
      .setVirtualCores(resourceRequestEvent.getVirtualCores().get())
      .setRuntimeName("STANDALONE")
      .build();
  reefEventHandlers.onResourceAllocation(alloc);

  // set the status as RUNNING.
  updateRuntimeStatus();
}
 
Example 13
Source File: ClientService.java    From reef with Apache License 2.0 5 votes vote down vote up
@Override
public void onNext(final FailedJob job) {
  final Optional<Throwable> ex = job.getReason();
  LOG.log(Level.SEVERE, "Received an error for job " + job.getId(), ex);
  theJob = null;
  setStatusAndNotify(LauncherStatus.failed(ex));
  if (clientStub != null) {
    final Optional<Throwable> error = exceptionCodec.fromBytes(job.getData().get());
    final ExceptionInfo exception;
    if (error.isPresent()) {
      LOG.log(Level.SEVERE, "Error for " + job.getId(), ex);
      exception = ExceptionInfo.newBuilder()
          .setName(job.toString())
          .setMessage(job.getMessage())
          .setData(getRootCause(error.get()))
          .build();
    } else {
      LOG.log(Level.SEVERE, "Unserialized error for job {0}: {1}",
          new Object[] {job.getId(), job.getMessage()});
      exception = ExceptionInfo.newBuilder()
          .setName(job.toString())
          .setMessage(job.getMessage())
          .build();
    }
    clientStub.jobFailedHandler(ClientProtocol.JobFailedEvent.newBuilder()
        .setJobId(job.getId())
        .setException(exception)
        .build());
  }
}
 
Example 14
Source File: DriverStatusManager.java    From reef with Apache License 2.0 5 votes vote down vote up
/**
 * @param exception the exception that ended the Driver, if any.
 * @return message to be sent to the client at the end of the job.
 */
private ReefServiceProtos.JobStatusProto getJobEndingMessage(final Optional<Throwable> exception) {
  if (exception.isPresent()) {
    return ReefServiceProtos.JobStatusProto.newBuilder()
        .setIdentifier(this.jobIdentifier)
        .setState(ReefServiceProtos.State.FAILED)
        .setException(ByteString.copyFrom(this.exceptionCodec.toBytes(exception.get())))
        .build();
  } else {
    return ReefServiceProtos.JobStatusProto.newBuilder()
        .setIdentifier(this.jobIdentifier)
        .setState(ReefServiceProtos.State.DONE)
        .build();
  }
}
 
Example 15
Source File: YarnJobSubmissionHandler.java    From reef with Apache License 2.0 5 votes vote down vote up
@Override
public void onNext(final JobSubmissionEvent jobSubmissionEvent) {

  final String id = jobSubmissionEvent.getIdentifier();
  LOG.log(Level.FINEST, "Submitting{0} job: {1}",
      new Object[] {this.isUnmanaged ? " UNMANAGED AM" : "", jobSubmissionEvent});

  try (YarnSubmissionHelper submissionHelper = new YarnSubmissionHelper(this.yarnConfiguration,
      this.fileNames, this.classpath, this.yarnProxyUser, this.tokenProvider, this.isUnmanaged)) {

    LOG.log(Level.FINE, "Assembling submission JAR for the Driver.");
    final Optional<String> userBoundJobSubmissionDirectory =
        getUserBoundJobSubmissionDirectory(jobSubmissionEvent.getConfiguration());
    final JobFolder jobFolderOnDfs = userBoundJobSubmissionDirectory.isPresent()
        ? this.uploader.createJobFolder(userBoundJobSubmissionDirectory.get())
        : this.uploader.createJobFolder(submissionHelper.getApplicationId());
    final Configuration driverConfiguration = makeDriverConfiguration(jobSubmissionEvent, jobFolderOnDfs.getPath());
    final File jobSubmissionFile = this.jobJarMaker.createJobSubmissionJAR(jobSubmissionEvent, driverConfiguration);
    final LocalResource driverJarOnDfs =
        jobFolderOnDfs.uploadAsLocalResource(jobSubmissionFile, LocalResourceType.ARCHIVE);

    submissionHelper
        .addLocalResource(this.fileNames.getREEFFolderName(), driverJarOnDfs)
        .setApplicationName(id)
        .setDriverResources(jobSubmissionEvent.getDriverMemory().get(), jobSubmissionEvent.getDriverCPUCores().get())
        .setPriority(getPriority(jobSubmissionEvent))
        .setQueue(getQueue(jobSubmissionEvent))
        .setPreserveEvaluators(getPreserveEvaluators(jobSubmissionEvent))
        .setMaxApplicationAttempts(getMaxApplicationSubmissions(jobSubmissionEvent))
        .submit();

    this.applicationId = submissionHelper.getStringApplicationId();
    LOG.log(Level.FINEST, "Submitted{0} job with ID {1} :: {2}", new String[] {
        this.isUnmanaged ? " UNMANAGED AM" : "", id, this.applicationId});

  } catch (final YarnException | IOException e) {
    throw new RuntimeException("Unable to submit Driver to YARN: " + id, e);
  }
}
 
Example 16
Source File: ExpectedTaskFailureHandler.java    From reef with Apache License 2.0 5 votes vote down vote up
/**
 * Checks whether the FailedTask was caused by a ExpectedTaskException.
 *
 * @param failedTask
 * @throws org.apache.reef.tests.library.exceptions.DriverSideFailure if the FailedTask wasn't triggered by a
 *                                                                    ExpectedTaskException
 */

@Override
public void onNext(final FailedTask failedTask) {
  final Optional<Throwable> reasonOptional = failedTask.getReason();
  if (!reasonOptional.isPresent()) {
    throw new DriverSideFailure("Received a FailedTask, but it did not contain an exception.");
  } else if (!(Exceptions.getUltimateCause(reasonOptional.get()) instanceof ExpectedTaskException)) {
    throw new DriverSideFailure("Received a FailedTask, but the ExpectedTaskException isn't the ultimate cause.",
        reasonOptional.get());
  }
  failedTask.getActiveContext().get().close();
}
 
Example 17
Source File: NamingTest.java    From reef with Apache License 2.0 5 votes vote down vote up
private static NameLookupClient getNewNameLookupClient(final String serverAddr,
                                                       final int serverPort,
                                                       final long timeout,
                                                       final int retryCount,
                                                       final int retryTimeout,
                                                       final Optional<LocalAddressProvider> localAddressProvider,
                                                       final Optional<IdentifierFactory> factory)
    throws InjectionException {


  final Configuration injectorConf = Tang.Factory.getTang().newConfigurationBuilder()
      .bindNamedParameter(NameResolverNameServerAddr.class, serverAddr)
      .bindNamedParameter(NameResolverNameServerPort.class, Integer.toString(serverPort))
      .bindNamedParameter(NameResolverCacheTimeout.class, Long.toString(timeout))
      .bindNamedParameter(NameResolverRetryCount.class, Integer.toString(retryCount))
      .bindNamedParameter(NameResolverRetryTimeout.class, Integer.toString(retryTimeout))
      .build();

  final Injector injector = Tang.Factory.getTang().newInjector(injectorConf);
  if (localAddressProvider.isPresent()) {
    injector.bindVolatileInstance(LocalAddressProvider.class, localAddressProvider.get());
  }

  if (factory.isPresent()) {
    injector.bindVolatileInstance(IdentifierFactory.class, factory.get());
  }

  return injector.getInstance(NameLookupClient.class);
}
 
Example 18
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 19
Source File: RunningWorkers.java    From reef with Apache License 2.0 4 votes vote down vote up
/**
 * Concurrency: Called by single scheduler thread.
 * Parameter: Same tasklet can be launched multiple times.
 */
void launchTasklet(final Tasklet tasklet) {
  lock.lock();
  try {
    if (!terminated) {
      Optional<String> workerId;
      while(true) {
        workerId = schedulingPolicy.trySchedule(tasklet);
        if (!workerId.isPresent()) {
          try {
            noWorkerOrResource.await();
          } catch (InterruptedException e) {
            throw new RuntimeException(e);
          }
        } else {
          break;
        }
      }

      // TODO[JIRA REEF-500]: Will need to support duplicate tasklets.
      if (taskletsToCancel.contains(tasklet.getId())) {
        tasklet.cancelled();
        taskletsToCancel.remove(tasklet.getId());
        LOG.log(Level.FINE, "Cancelled tasklet {0}.", tasklet.getId());
        return;
      }

      final Optional<Integer> taskletAggFunctionId =  tasklet.getAggregateFunctionId();
      final VortexWorkerManager vortexWorkerManager = runningWorkers.get(workerId.get());

      if (taskletAggFunctionId.isPresent() &&
          !workerHasAggregateFunction(vortexWorkerManager.getId(), taskletAggFunctionId.get())) {

        // This assumes that all aggregate tasklets share the same user function.
        vortexWorkerManager.sendAggregateFunction(
            taskletAggFunctionId.get(),
            aggregateFunctionRepository.getAggregateFunction(taskletAggFunctionId.get()),
            tasklet.getUserFunction(),
            aggregateFunctionRepository.getPolicy(taskletAggFunctionId.get()));
        workerAggregateFunctionMap.get(vortexWorkerManager.getId()).add(taskletAggFunctionId.get());
      }

      vortexWorkerManager.launchTasklet(tasklet);
      schedulingPolicy.taskletLaunched(vortexWorkerManager, tasklet);
    }
  } finally {
    lock.unlock();
  }
}
 
Example 20
Source File: EvaluatorHeartbeatHandler.java    From reef with Apache License 2.0 4 votes vote down vote up
@Override
public void onNext(final RemoteMessage<EvaluatorRuntimeProtocol.EvaluatorHeartbeatProto> evaluatorHeartbeatMessage) {
  final EvaluatorRuntimeProtocol.EvaluatorHeartbeatProto heartbeat = evaluatorHeartbeatMessage.getMessage();
  final ReefServiceProtos.EvaluatorStatusProto status = heartbeat.getEvaluatorStatus();
  final String evaluatorId = status.getEvaluatorId();

  LOG.log(Level.FINEST, "TIME: Begin Heartbeat {0}", evaluatorId);
  LOG.log(Level.FINEST, "Heartbeat from Evaluator {0} with state {1} timestamp {2} from remoteId {3}",
      new Object[]{evaluatorId, status.getState(), heartbeat.getTimestamp(),
          evaluatorHeartbeatMessage.getIdentifier()});

  try {
    final Optional<EvaluatorManager> evaluatorManager = this.evaluators.get(evaluatorId);
    if (evaluatorManager.isPresent()) {
      evaluatorManager.get().onEvaluatorHeartbeatMessage(evaluatorHeartbeatMessage);
      return;
    }

    if (this.evaluators.wasClosed(evaluatorId)) {
      LOG.log(Level.WARNING, "Evaluator [" + evaluatorId + "] has reported back to the driver after it was closed.");
      return;
    }

    if (driverRestartManager.isRestarting() &&
        driverRestartManager.getEvaluatorRestartState(evaluatorId) == EvaluatorRestartState.EXPECTED) {

      if (this.driverRestartManager.onRecoverEvaluator(evaluatorId)) {
        LOG.log(Level.FINE, "Evaluator [" + evaluatorId + "] has reported back to the driver after restart.");

        evaluators.put(recoverEvaluatorManager(evaluatorId, evaluatorHeartbeatMessage));
      } else {
        LOG.log(Level.FINE, "Evaluator [" + evaluatorId + "] has already been recovered.");
      }
      return;
    }

    if (driverRestartManager.getEvaluatorRestartState(evaluatorId) == EvaluatorRestartState.EXPIRED) {
      LOG.log(Level.FINE, "Expired evaluator " + evaluatorId + " has reported back to the driver after restart.");

      // Create the evaluator manager, analyze its heartbeat, but don't add it to the set of Evaluators.
      // Immediately close it.
      recoverEvaluatorManager(evaluatorId, evaluatorHeartbeatMessage).close();
      return;
    }

    final StringBuilder message = new StringBuilder("Contact from unknown Evaluator with identifier '");
    message.append(evaluatorId);
    if (heartbeat.hasEvaluatorStatus()) {
      message.append("' with state '");
      message.append(status.getState());
    }
    message.append('\'');
    throw new RuntimeException(message.toString());
  } finally {
    LOG.log(Level.FINEST, "TIME: End Heartbeat {0}", evaluatorId);
  }
}