aurora-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wfar...@apache.org
Subject git commit: Add a state machine to react to job update status changes.
Date Wed, 03 Sep 2014 19:39:12 GMT
Repository: incubator-aurora
Updated Branches:
  refs/heads/master f6eeb0a0a -> 5445dea9b


Add a state machine to react to job update status changes.

Bugs closed: AURORA-613

Reviewed at https://reviews.apache.org/r/25300/


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

Branch: refs/heads/master
Commit: 5445dea9ba3c877060b4734fa6460c448e214cbe
Parents: f6eeb0a
Author: Bill Farner <wfarner@apache.org>
Authored: Wed Sep 3 12:35:43 2014 -0700
Committer: Bill Farner <wfarner@apache.org>
Committed: Wed Sep 3 12:35:43 2014 -0700

----------------------------------------------------------------------
 .../updater/JobUpdateStateMachine.java          | 100 +++++++++++++++++++
 .../updater/JobUpdateStateMachineTest.java      |  81 +++++++++++++++
 2 files changed, 181 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/5445dea9/src/main/java/org/apache/aurora/scheduler/updater/JobUpdateStateMachine.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/aurora/scheduler/updater/JobUpdateStateMachine.java
b/src/main/java/org/apache/aurora/scheduler/updater/JobUpdateStateMachine.java
new file mode 100644
index 0000000..037602d
--- /dev/null
+++ b/src/main/java/org/apache/aurora/scheduler/updater/JobUpdateStateMachine.java
@@ -0,0 +1,100 @@
+/**
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.aurora.scheduler.updater;
+
+import java.util.Map;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableMultimap;
+import com.google.common.collect.Multimap;
+
+import org.apache.aurora.gen.JobUpdateStatus;
+
+import static org.apache.aurora.gen.JobUpdateStatus.ABORTED;
+import static org.apache.aurora.gen.JobUpdateStatus.ERROR;
+import static org.apache.aurora.gen.JobUpdateStatus.ROLLED_BACK;
+import static org.apache.aurora.gen.JobUpdateStatus.ROLLED_FORWARD;
+import static org.apache.aurora.gen.JobUpdateStatus.ROLLING_BACK;
+import static org.apache.aurora.gen.JobUpdateStatus.ROLLING_FORWARD;
+import static org.apache.aurora.gen.JobUpdateStatus.ROLL_BACK_PAUSED;
+import static org.apache.aurora.gen.JobUpdateStatus.ROLL_FORWARD_PAUSED;
+import static org.apache.aurora.scheduler.updater.JobUpdateStateMachine.MonitorAction.ROLL_BACK;
+import static org.apache.aurora.scheduler.updater.JobUpdateStateMachine.MonitorAction.ROLL_FORWARD;
+
+/**
+ * A state machine that determines which high-level actions should be performed in response
to
+ * a requested change in the status of a job update.
+ */
+final class JobUpdateStateMachine {
+
+  private JobUpdateStateMachine() {
+    // Utility class.
+  }
+
+  private static final Multimap<JobUpdateStatus, JobUpdateStatus> ALLOWED_TRANSITIONS
=
+      ImmutableMultimap.<JobUpdateStatus, JobUpdateStatus>builder()
+          .putAll(ROLLING_FORWARD,
+              ROLLING_BACK,
+              ROLL_FORWARD_PAUSED,
+              ROLLED_FORWARD,
+              ABORTED,
+              ERROR)
+          .putAll(ROLLING_BACK, ROLL_BACK_PAUSED, ROLLED_BACK, ABORTED, ERROR)
+          .putAll(ROLL_FORWARD_PAUSED, ROLLING_FORWARD, ABORTED, ERROR)
+          .putAll(ROLL_BACK_PAUSED, ROLLING_BACK, ABORTED, ERROR)
+          .build();
+
+  private static final Map<JobUpdateStatus, MonitorAction> ACTIONS =
+      ImmutableMap.<JobUpdateStatus, MonitorAction>builder()
+          .put(ROLLING_FORWARD, ROLL_FORWARD)
+          .put(ROLLING_BACK, ROLL_BACK)
+          .build();
+
+  /**
+   * Determines the action to take in response to a status change on a job update.
+   *
+   * @param from Starting state.
+   * @param to Desired target state.
+   * @return The action to perform when moving from {@code from} to {@code to}.
+   * @throws IllegalStateException if the requested transition is not allowed.
+   */
+  static MonitorAction transition(JobUpdateStatus from, JobUpdateStatus to) {
+    if (!ALLOWED_TRANSITIONS.containsEntry(from, to)) {
+      throw new IllegalStateException("Cannot transition update from " + from + " to " +
to);
+    }
+
+    return Optional.fromNullable(ACTIONS.get(to)).or(MonitorAction.STOP_WATCHING);
+  }
+
+  /**
+   * An action to take in response to a state transition.
+   */
+  enum MonitorAction {
+    /**
+     * The update has moved to an inactive state and its tasks should no longer be monitored.
+     */
+    STOP_WATCHING,
+
+    /**
+     * Initiate an update of the job.
+     */
+    ROLL_FORWARD,
+
+    /**
+     * Initiate a revert of the job update to the previous configuration.
+     */
+    ROLL_BACK
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/5445dea9/src/test/java/org/apache/aurora/scheduler/updater/JobUpdateStateMachineTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/aurora/scheduler/updater/JobUpdateStateMachineTest.java
b/src/test/java/org/apache/aurora/scheduler/updater/JobUpdateStateMachineTest.java
new file mode 100644
index 0000000..6eed641
--- /dev/null
+++ b/src/test/java/org/apache/aurora/scheduler/updater/JobUpdateStateMachineTest.java
@@ -0,0 +1,81 @@
+/**
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.aurora.scheduler.updater;
+
+import java.util.Map;
+
+import com.google.common.collect.ImmutableMap;
+import com.twitter.common.collections.Pair;
+
+import org.apache.aurora.gen.JobUpdateStatus;
+import org.junit.Test;
+
+import static org.apache.aurora.gen.JobUpdateStatus.ABORTED;
+import static org.apache.aurora.gen.JobUpdateStatus.ERROR;
+import static org.apache.aurora.gen.JobUpdateStatus.ROLLED_BACK;
+import static org.apache.aurora.gen.JobUpdateStatus.ROLLED_FORWARD;
+import static org.apache.aurora.gen.JobUpdateStatus.ROLLING_BACK;
+import static org.apache.aurora.gen.JobUpdateStatus.ROLLING_FORWARD;
+import static org.apache.aurora.gen.JobUpdateStatus.ROLL_BACK_PAUSED;
+import static org.apache.aurora.gen.JobUpdateStatus.ROLL_FORWARD_PAUSED;
+import static org.apache.aurora.scheduler.updater.JobUpdateStateMachine.MonitorAction;
+import static org.apache.aurora.scheduler.updater.JobUpdateStateMachine.MonitorAction.ROLL_BACK;
+import static org.apache.aurora.scheduler.updater.JobUpdateStateMachine.MonitorAction.ROLL_FORWARD;
+import static org.apache.aurora.scheduler.updater.JobUpdateStateMachine.MonitorAction.STOP_WATCHING;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+public class JobUpdateStateMachineTest {
+
+  private static final Map<Pair<JobUpdateStatus, JobUpdateStatus>, MonitorAction>
EXPECTED =
+      ImmutableMap.<Pair<JobUpdateStatus, JobUpdateStatus>, MonitorAction>builder()
+          .put(Pair.of(ROLLING_FORWARD, ROLLING_BACK), ROLL_BACK)
+          .put(Pair.of(ROLLING_FORWARD, ROLL_FORWARD_PAUSED), STOP_WATCHING)
+          .put(Pair.of(ROLLING_FORWARD, ROLLED_FORWARD), STOP_WATCHING)
+          .put(Pair.of(ROLLING_FORWARD, ABORTED), STOP_WATCHING)
+          .put(Pair.of(ROLLING_FORWARD, ERROR), STOP_WATCHING)
+          .put(Pair.of(ROLLING_BACK, ROLL_BACK_PAUSED), STOP_WATCHING)
+          .put(Pair.of(ROLLING_BACK, ROLLED_BACK), STOP_WATCHING)
+          .put(Pair.of(ROLLING_BACK, ABORTED), STOP_WATCHING)
+          .put(Pair.of(ROLLING_BACK, ERROR), STOP_WATCHING)
+          .put(Pair.of(ROLL_FORWARD_PAUSED, ROLLING_FORWARD), ROLL_FORWARD)
+          .put(Pair.of(ROLL_FORWARD_PAUSED, ABORTED), STOP_WATCHING)
+          .put(Pair.of(ROLL_FORWARD_PAUSED, ERROR), STOP_WATCHING)
+          .put(Pair.of(ROLL_BACK_PAUSED, ROLLING_BACK), ROLL_BACK)
+          .put(Pair.of(ROLL_BACK_PAUSED, ABORTED), STOP_WATCHING)
+          .put(Pair.of(ROLL_BACK_PAUSED, ERROR), STOP_WATCHING)
+          .build();
+
+  @Test
+  public void testTransition() {
+    for (JobUpdateStatus from : JobUpdateStatus.values()) {
+      for (JobUpdateStatus to : JobUpdateStatus.values()) {
+        Pair<JobUpdateStatus, JobUpdateStatus> key = Pair.of(from, to);
+        MonitorAction expected = EXPECTED.get(key);
+        try {
+          MonitorAction actual = JobUpdateStateMachine.transition(from, to);
+          if (expected == null) {
+            fail("Transition " + key + " should have been disallowed, but got result " +
actual);
+          }
+          assertEquals("Failed transition " + key, expected, actual);
+        } catch (IllegalStateException e) {
+          if (expected != null) {
+            fail("Expected " + expected + " for transition " + key
+                + " but the transition was disallowed: " + e);
+          }
+        }
+      }
+    }
+  }
+}


Mime
View raw message