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;
024
025/**
026 * The state that the evaluator is in in the driver restart process.
027 */
028@Private
029@DriverSide
030@Unstable
031public enum EvaluatorRestartState {
032
033  /**
034   * The evaluator is not a restarted instance. Not expecting.
035   */
036  NOT_EXPECTED,
037
038  /**
039   * Have not yet heard back from an evaluator, but we are expecting it to report back.
040   */
041  EXPECTED,
042
043  /**
044   * Received the evaluator heartbeat, but have not yet processed it.
045   */
046  REPORTED,
047
048  /**
049   * The evaluator has had its recovery heartbeat processed.
050   */
051  REREGISTERED,
052
053  /**
054   * The evaluator has had its context/running task processed.
055   */
056  PROCESSED,
057
058  /**
059   * The evaluator has only contacted the driver after the expiration period.
060   */
061  EXPIRED,
062
063  /**
064   * The evaluator has failed on driver restart.
065   */
066  FAILED;
067
068  /**
069   * Check if the transition of {@link EvaluatorRestartState} from one state to another is legal.
070   * @param fromState start state.
071   * @param toState destination state.
072   * @return true if the transition of {@link EvaluatorRestartState} is legal.
073   * @deprecated TODO[JIRA REEF-1560] Use non-static method instead. Remove after version 0.16
074   */
075  @Deprecated
076  public static boolean isLegalTransition(
077      final EvaluatorRestartState fromState, final EvaluatorRestartState toState) {
078    return fromState.isLegalTransition(toState);
079  }
080
081  /**
082   * Check if the transition of {@link EvaluatorRestartState} from current state to the given one is legal.
083   * @param toState destination state.
084   * @return true if the transition is legal, false otherwise.
085   */
086  public final boolean isLegalTransition(final EvaluatorRestartState toState) {
087
088    switch(this) {
089    case EXPECTED:
090      switch(toState) {
091      case EXPIRED:
092      case REPORTED:
093        return true;
094      default:
095        return false;
096      }
097
098    case REPORTED:
099      switch(toState) {
100      case REREGISTERED:
101        return true;
102      default:
103        return false;
104      }
105
106    case REREGISTERED:
107      switch(toState) {
108      case PROCESSED:
109        return true;
110      default:
111        return false;
112      }
113
114    default:
115      return false;
116    }
117  }
118
119  /**
120   * @return true if the evaluator has heartbeated back to the driver.
121   */
122  public boolean hasReported() {
123    switch(this) {
124    case REPORTED:
125    case REREGISTERED:
126    case PROCESSED:
127      return true;
128    default:
129      return false;
130    }
131  }
132
133  /**
134   * @return true if the evaluator has failed on driver restart or is not expected to report back to the driver.
135   */
136  public boolean isFailedOrNotExpected() {
137    switch(this) {
138    case FAILED:
139    case NOT_EXPECTED:
140      return true;
141    default:
142      return false;
143    }
144  }
145
146  /**
147   * @return true if the evaluator has failed on driver restart or has been expired.
148   */
149  public boolean isFailedOrExpired() {
150    switch(this) {
151    case FAILED:
152    case EXPIRED:
153      return true;
154    default:
155      return false;
156    }
157  }
158
159  /**
160   * @return true if the evaluator has had its recovery heartbeat processed.
161   */
162  public boolean isReregistered() {
163    return this == REREGISTERED;
164  }
165}