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