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 */ 019 020package org.apache.reef.runtime.common.driver.evaluator.pojos; 021 022import org.apache.reef.annotations.audience.DriverSide; 023import org.apache.reef.annotations.audience.Private; 024import org.apache.reef.proto.ReefServiceProtos; 025 026/** 027 * DriverSide representation of ReefServiceProtos.State. 028 */ 029@DriverSide 030@Private 031public enum State { 032 033 INIT, 034 RUNNING, 035 SUSPEND, 036 DONE, 037 FAILED, 038 KILLED; 039 040 /** 041 * Get a driver-side state given the proto. It is a 1:1 mapping. 042 * @param protoState remote state from the proto. 043 * @return a corresponding (identical) driver-side state (always a 1:1 mapping). 044 */ 045 public static State fromProto(final ReefServiceProtos.State protoState) { 046 switch (protoState) { 047 case INIT: 048 return INIT; 049 case RUNNING: 050 return RUNNING; 051 case SUSPEND: 052 return SUSPEND; 053 case DONE: 054 return DONE; 055 case FAILED: 056 return FAILED; 057 case KILLED: 058 return KILLED; 059 default: 060 throw new IllegalStateException("Unknown state " + protoState + " in EvaluatorStatusProto"); 061 } 062 } 063 064 /** 065 * Checks if the ResourceManager can switch from the current state to the target state. 066 * See REEF-826 for the state transition matrix. 067 * @param toState state to switch to. 068 * @return true if the transition is legal; false otherwise. 069 */ 070 public final boolean isLegalTransition(final State toState) { 071 072 if (this == toState) { 073 return true; 074 } 075 076 switch (this) { 077 078 case INIT: 079 switch (toState) { 080 case RUNNING: 081 case SUSPEND: 082 case DONE: 083 case FAILED: 084 case KILLED: 085 return true; 086 default: 087 return false; 088 } 089 090 case RUNNING: 091 switch (toState) { 092 case SUSPEND: 093 case DONE: 094 case FAILED: 095 case KILLED: 096 return true; 097 default: 098 return false; 099 } 100 101 case SUSPEND: 102 switch (toState) { 103 case RUNNING: 104 case FAILED: 105 case KILLED: 106 return true; 107 default: 108 return false; 109 } 110 111 default: 112 return false; 113 } 114 } 115 116 /** 117 * Check if container is in RUNNING state. 118 * @return true if container is running. 119 */ 120 public final boolean isRunning() { 121 return this == RUNNING; 122 } 123 124 /** 125 * Check if container is available - that is, in one of the states INIT, RUNNING, or SUSPEND. 126 * @return true if container is available, false if it is closed or in the process of being shut down. 127 */ 128 public final boolean isAvailable() { 129 return this == INIT || this == RUNNING || this == SUSPEND; 130 } 131 132 /** 133 * Check if the container is stopped. That is, in one of the DONE, FAILED, or KILLED states. 134 * @return true if the container is completed, false if it is still available or suspended. 135 */ 136 public final boolean isCompleted() { 137 return this == DONE || this == FAILED || this == KILLED; 138 } 139 140 /** 141 * Check if the container is can be restarted. That is, in one of the INIT, RUNNING, or FAILED states. 142 * @return true if the container can be restarted. 143 */ 144 public final boolean isRestartable() { 145 return this == INIT || this == RUNNING || this == FAILED; 146 } 147}