You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@reef.apache.org by we...@apache.org on 2015/11/02 18:01:25 UTC

incubator-reef git commit: [REEF-826] Add state transition check in ResourceManagerStatus.setState

Repository: incubator-reef
Updated Branches:
  refs/heads/master 08bc6c645 -> f5ee672c8


[REEF-826] Add state transition check in ResourceManagerStatus.setState

This patch:
 * Adds the isLegalStateTransition() method to ResourceManagerStatus
 * Performs checks in the method according to the state transition diagram discussed in REEF-826
 * Calls the method in setState()
 * Throws IllegalStateException if the state transition is prohibited

JIRA:
 [REEF-826](https://issues.apache.org/jira/browse/REEF-826)

Pull Request:
  This closes #596


Project: http://git-wip-us.apache.org/repos/asf/incubator-reef/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-reef/commit/f5ee672c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-reef/tree/f5ee672c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-reef/diff/f5ee672c

Branch: refs/heads/master
Commit: f5ee672c83a921cb3ec963a82d654bd2b9cf2e5d
Parents: 08bc6c6
Author: sergey.dudoladov@tu-berlin.de <se...@tu-berlin.de>
Authored: Fri Oct 23 13:55:14 2015 +0200
Committer: Markus Weimer <we...@apache.org>
Committed: Mon Nov 2 09:00:11 2015 -0800

----------------------------------------------------------------------
 .../resourcemanager/ResourceManagerStatus.java  | 80 +++++++++++++++++++-
 1 file changed, 77 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/f5ee672c/lang/java/reef-common/src/main/java/org/apache/reef/runtime/common/driver/resourcemanager/ResourceManagerStatus.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-common/src/main/java/org/apache/reef/runtime/common/driver/resourcemanager/ResourceManagerStatus.java b/lang/java/reef-common/src/main/java/org/apache/reef/runtime/common/driver/resourcemanager/ResourceManagerStatus.java
index 6cb9a5d..79448a2 100644
--- a/lang/java/reef-common/src/main/java/org/apache/reef/runtime/common/driver/resourcemanager/ResourceManagerStatus.java
+++ b/lang/java/reef-common/src/main/java/org/apache/reef/runtime/common/driver/resourcemanager/ResourceManagerStatus.java
@@ -144,10 +144,84 @@ public final class ResourceManagerStatus implements EventHandler<RuntimeStatusEv
     return ReefServiceProtos.State.RUNNING.equals(this.state);
   }
 
+  /**
+  *
+  * Checks if the ResourceManager can switch from the current state to the target state.
+  * See REEF-826 for the state transition matrix.
+  *
+  * @param from current state
+  * @param to   state to switch to
+  *
+  * @return true if the transition is legal; false otherwise
+  *
+  */
+  private synchronized boolean isLegalStateTransition(final ReefServiceProtos.State from,
+                                                      final ReefServiceProtos.State to) {
+
+    // handle diagonal elements of the transition matrix
+    if (from.equals(to)){
+      LOG.warning("Transition from " + from + " state to the same state. This shouldn't happen.");
+      return true;
+    }
+
+    // handle non-diagonal elements
+    switch (from) {
+
+    case INIT:
+      switch (to) {
+      case RUNNING:
+      case SUSPEND:
+      case DONE:
+      case FAILED:
+      case KILLED:
+        return true;
+      default:
+        return false;
+      }
+
+    case RUNNING:
+      switch (to) {
+      case SUSPEND:
+      case DONE:
+      case FAILED:
+      case KILLED:
+        return true;
+      default:
+        return false;
+      }
+
+    case SUSPEND:
+      switch (to) {
+      case RUNNING:
+      case FAILED:
+      case KILLED:
+        return true;
+      default:
+        return false;
+      }
+
+    case DONE:
+    case FAILED:
+    case KILLED:
+      return false;
+
+    default:
+      return false;
+
+    }
+
+  }
+
+  private synchronized void setState(final ReefServiceProtos.State newState) {
+
+    if (isLegalStateTransition(this.state, newState)) {
+      this.state = newState;
+    } else {
+      throw new IllegalStateException("Resource manager attempts illegal state transition from "
+                + this.state + " to "
+                + newState);
+    }
 
-  private synchronized void setState(final ReefServiceProtos.State state) {
-    // TODO[JIRA REEF-826]: Add state transition check
-    this.state = state;
   }