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.resourcemanager;
020
021import org.apache.reef.annotations.audience.Private;
022import org.apache.reef.runtime.common.driver.evaluator.EvaluatorManager;
023import org.apache.reef.runtime.common.driver.evaluator.EvaluatorManagerFactory;
024import org.apache.reef.runtime.common.driver.evaluator.Evaluators;
025import org.apache.reef.util.Optional;
026import org.apache.reef.wake.EventHandler;
027
028import javax.inject.Inject;
029
030/**
031 * A ResourceStatusProto message comes from the ResourceManager layer to indicate what it thinks
032 * about the current state of a given resource. Ideally, we should think the same thing.
033 */
034@Private
035public final class ResourceStatusHandler implements EventHandler<ResourceStatusEvent> {
036
037  private final Evaluators evaluators;
038  private final EvaluatorManagerFactory evaluatorManagerFactory;
039
040  @Inject
041  ResourceStatusHandler(final Evaluators evaluators, final EvaluatorManagerFactory evaluatorManagerFactory) {
042    this.evaluators = evaluators;
043    this.evaluatorManagerFactory = evaluatorManagerFactory;
044  }
045
046  /**
047   * This resource status message comes from the ResourceManager layer; telling me what it thinks
048   * about the state of the resource executing an Evaluator; This method simply passes the message
049   * off to the referenced EvaluatorManager
050   *
051   * @param resourceStatusEvent resource status message from the ResourceManager
052   */
053  @Override
054  public void onNext(final ResourceStatusEvent resourceStatusEvent) {
055    final Optional<EvaluatorManager> evaluatorManager = this.evaluators.get(resourceStatusEvent.getIdentifier());
056    if (evaluatorManager.isPresent()) {
057      evaluatorManager.get().onResourceStatusMessage(resourceStatusEvent);
058    } else {
059      if (resourceStatusEvent.getIsFromPreviousDriver().get()) {
060        EvaluatorManager previousEvaluatorManager = this.evaluatorManagerFactory.createForEvaluatorFailedDuringDriverRestart(resourceStatusEvent);
061        previousEvaluatorManager.onResourceStatusMessage(resourceStatusEvent);
062      } else {
063        throw new RuntimeException(
064            "Unknown resource status from evaluator " + resourceStatusEvent.getIdentifier() +
065                " with state " + resourceStatusEvent.getState()
066        );
067      }
068    }
069  }
070}