001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.runtime.common.driver.evaluator;
020
021import org.apache.reef.annotations.audience.Private;
022import org.apache.reef.client.FailedRuntime;
023import org.apache.reef.exception.EvaluatorException;
024import org.apache.reef.proto.ReefServiceProtos;
025import org.apache.reef.util.Optional;
026import org.apache.reef.wake.EventHandler;
027import org.apache.reef.wake.remote.RemoteMessage;
028
029import javax.inject.Inject;
030import java.util.logging.Level;
031import java.util.logging.Logger;
032
033/**
034 * The error handler receives all resourcemanager errors from all evaluators in the system.
035 * Its primary function is to dispatch these to the appropriate EvaluatorManager.
036 */
037@Private
038public final class EvaluatorResourceManagerErrorHandler
039    implements EventHandler<RemoteMessage<ReefServiceProtos.RuntimeErrorProto>> {
040  private static final Logger LOG = Logger.getLogger(EvaluatorResourceManagerErrorHandler.class.toString());
041  private final Evaluators evaluators;
042
043
044  @Inject
045  EvaluatorResourceManagerErrorHandler(final Evaluators evaluators) {
046    this.evaluators = evaluators;
047    LOG.log(Level.FINE, "Instantiated 'EvaluatorResourceManagerErrorHandler'");
048  }
049
050  @Override
051  public void onNext(final RemoteMessage<ReefServiceProtos.RuntimeErrorProto> runtimeErrorProtoRemoteMessage) {
052    final ReefServiceProtos.RuntimeErrorProto runtimeErrorProto = runtimeErrorProtoRemoteMessage.getMessage();
053    final FailedRuntime error = new FailedRuntime(runtimeErrorProto);
054    final String evaluatorId = error.getId();
055    LOG.log(Level.WARNING, "Runtime error: " + error);
056
057    final EvaluatorException evaluatorException = error.getReason().isPresent() ?
058        new EvaluatorException(evaluatorId, error.getReason().get()) :
059        new EvaluatorException(evaluatorId, "Runtime error");
060
061    final Optional<EvaluatorManager> evaluatorManager = this.evaluators.get(evaluatorId);
062    if (evaluatorManager.isPresent()) {
063      evaluatorManager.get().onEvaluatorException(evaluatorException);
064    } else {
065      if (this.evaluators.wasClosed(evaluatorId)) {
066        LOG.log(Level.WARNING, "Evaluator [" + evaluatorId + "] has raised exception after it was closed.");
067      } else {
068        LOG.log(Level.WARNING, "Unknown evaluator runtime error: " + error);
069      }
070    }
071  }
072}