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.driver.restart; 020 021import org.apache.reef.annotations.Unstable; 022import org.apache.reef.annotations.audience.DriverSide; 023import org.apache.reef.annotations.audience.Private; 024import org.apache.reef.util.BuilderUtils; 025 026import java.util.Collections; 027import java.util.HashMap; 028import java.util.Map; 029import java.util.Set; 030 031/** 032 * Represents holds the set of Evaluator information needed to recover EvaluatorManagers 033 * on the restarted Driver. 034 */ 035@Private 036@DriverSide 037@Unstable 038public final class RestartEvaluators { 039 private final Map<String, EvaluatorRestartInfo> restartEvaluatorsMap; 040 041 private RestartEvaluators(final Map<String, EvaluatorRestartInfo> restartEvaluatorsMap){ 042 this.restartEvaluatorsMap = BuilderUtils.notNull(restartEvaluatorsMap); 043 } 044 045 /** 046 * @return true if Evaluator with evaluatorId can be an Evaluator from 047 * previous application attempts. 048 */ 049 boolean contains(final String evaluatorId) { 050 return restartEvaluatorsMap.containsKey(evaluatorId); 051 } 052 053 /** 054 * @return The {@link EvaluatorRestartInfo} of an Evaluator from 055 * previous application attempts. 056 */ 057 EvaluatorRestartInfo get(final String evaluatorId) { 058 return restartEvaluatorsMap.get(evaluatorId); 059 } 060 061 /** 062 * @return The set of Evaluator IDs of Evaluators from previous 063 * application attempts. 064 */ 065 Set<String> getEvaluatorIds() { 066 return restartEvaluatorsMap.keySet(); 067 } 068 069 /** 070 * @return a new Builder to build an instance of {@link RestartEvaluators}. 071 */ 072 public static Builder newBuilder() { 073 return new Builder(); 074 } 075 076 /** 077 * Builder to build {@link RestartEvaluators}. 078 */ 079 public static final class Builder implements org.apache.reef.util.Builder<RestartEvaluators>{ 080 private final Map<String, EvaluatorRestartInfo> restartInfoMap = new HashMap<>(); 081 082 private Builder(){ 083 } 084 085 public boolean addRestartEvaluator(final EvaluatorRestartInfo evaluatorRestartInfo) { 086 if (evaluatorRestartInfo == null) { 087 return false; 088 } 089 090 final String evaluatorId = evaluatorRestartInfo.getResourceRecoverEvent().getIdentifier(); 091 if (evaluatorId == null || restartInfoMap.containsKey(evaluatorId)) { 092 return false; 093 } 094 095 restartInfoMap.put(evaluatorId, evaluatorRestartInfo); 096 return true; 097 } 098 099 @Override 100 public RestartEvaluators build() { 101 return new RestartEvaluators(Collections.unmodifiableMap(restartInfoMap)); 102 } 103 } 104}