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 implements EventHandler<RemoteMessage<ReefServiceProtos.RuntimeErrorProto>> {
039  private static final Logger LOG = Logger.getLogger(EvaluatorResourceManagerErrorHandler.class.toString());
040  private final Evaluators evaluators;
041
042
043  @Inject
044  EvaluatorResourceManagerErrorHandler(final Evaluators evaluators) {
045    this.evaluators = evaluators;
046    LOG.log(Level.FINE, "Instantiated 'EvaluatorResourceManagerErrorHandler'");
047  }
048
049  @Override
050  public void onNext(final RemoteMessage<ReefServiceProtos.RuntimeErrorProto> runtimeErrorProtoRemoteMessage) {
051    final ReefServiceProtos.RuntimeErrorProto runtimeErrorProto = runtimeErrorProtoRemoteMessage.getMessage();
052    final FailedRuntime error = new FailedRuntime(runtimeErrorProto);
053    final String evaluatorId = error.getId();
054    LOG.log(Level.WARNING, "Runtime error: " + error);
055
056    final EvaluatorException evaluatorException = error.getReason().isPresent() ?
057        new EvaluatorException(evaluatorId, error.getReason().get()) :
058        new EvaluatorException(evaluatorId, "Runtime error");
059
060    final Optional<EvaluatorManager> evaluatorManager = this.evaluators.get(evaluatorId);
061    if (evaluatorManager.isPresent()) {
062      evaluatorManager.get().onEvaluatorException(evaluatorException);
063    } else {
064      LOG.log(Level.WARNING, "Unknown evaluator runtime error: " + error);
065    }
066  }
067}