You are viewing a plain text version of this content. The canonical link for it is here.
Posted to mapreduce-commits@hadoop.apache.org by st...@apache.org on 2012/03/15 11:39:17 UTC

svn commit: r1300894 - in /hadoop/common/trunk/hadoop-mapreduce-project: ./ hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/service/ hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/service/

Author: stevel
Date: Thu Mar 15 10:39:17 2012
New Revision: 1300894

URL: http://svn.apache.org/viewvc?rev=1300894&view=rev
Log:
MAPREDUCE-3970 ServiceOperations class

Modified:
    hadoop/common/trunk/hadoop-mapreduce-project/CHANGES.txt
    hadoop/common/trunk/hadoop-mapreduce-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/service/AbstractService.java
    hadoop/common/trunk/hadoop-mapreduce-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/service/ServiceAssert.java
    hadoop/common/trunk/hadoop-mapreduce-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/service/TestServiceLifecycle.java

Modified: hadoop/common/trunk/hadoop-mapreduce-project/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-mapreduce-project/CHANGES.txt?rev=1300894&r1=1300893&r2=1300894&view=diff
==============================================================================
--- hadoop/common/trunk/hadoop-mapreduce-project/CHANGES.txt (original)
+++ hadoop/common/trunk/hadoop-mapreduce-project/CHANGES.txt Thu Mar 15 10:39:17 2012
@@ -120,6 +120,9 @@ Release 0.23.3 - UNRELEASED
     MAPREDUCE-3773. Add queue metrics with buckets for job run times. (omalley
     via acmurthy)
 
+    MAPREDUCE-3970 Add ServiceOperations class to aid working with Services
+    (stevel)
+
   OPTIMIZATIONS
 
   BUG FIXES

Modified: hadoop/common/trunk/hadoop-mapreduce-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/service/AbstractService.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-mapreduce-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/service/AbstractService.java?rev=1300894&r1=1300893&r2=1300894&view=diff
==============================================================================
--- hadoop/common/trunk/hadoop-mapreduce-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/service/AbstractService.java (original)
+++ hadoop/common/trunk/hadoop-mapreduce-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/service/AbstractService.java Thu Mar 15 10:39:17 2012
@@ -145,10 +145,7 @@ public abstract class AbstractService im
    * the desired state
    */
   private void ensureCurrentState(STATE currentState) {
-    if (state != currentState) {
-      throw new IllegalStateException("For this operation, current State must " +
-        "be " + currentState + " instead of " + state);
-    }
+    ServiceOperations.ensureCurrentState(state, currentState);
   }
 
   /**

Modified: hadoop/common/trunk/hadoop-mapreduce-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/service/ServiceAssert.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-mapreduce-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/service/ServiceAssert.java?rev=1300894&r1=1300893&r2=1300894&view=diff
==============================================================================
--- hadoop/common/trunk/hadoop-mapreduce-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/service/ServiceAssert.java (original)
+++ hadoop/common/trunk/hadoop-mapreduce-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/service/ServiceAssert.java Thu Mar 15 10:39:17 2012
@@ -46,4 +46,34 @@ public class ServiceAssert extends Asser
     assertEquals("Service in wrong state: " + service, state,
                  service.getServiceState());
   }
+
+  /**
+   * Assert that the breakable service has entered a state exactly the number
+   * of time asserted.
+   * @param service service -if null an assertion is raised.
+   * @param state state to check.
+   * @param expected expected count.
+   */
+  public static void assertStateCount(BreakableService service,
+                        Service.STATE state,
+                        int expected) {
+    assertNotNull("Null service", service);
+    int actual = service.getCount(state);
+    if (expected != actual) {
+      fail("Expected entry count for state [" + state +"] of " + service
+               + " to be " + expected + " but was " + actual);
+    }
+  }
+
+  /**
+   * Assert that a service configuration contains a specific key; the value
+   * is ignored.
+   * @param service service to check
+   * @param key key to look for
+   */
+  public static void assertServiceConfigurationContains(Service service,
+                                                        String key) {
+    assertNotNull("No option "+ key + " in service configuration",
+                  service.getConfig().get(key));
+  }
 }

Modified: hadoop/common/trunk/hadoop-mapreduce-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/service/TestServiceLifecycle.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-mapreduce-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/service/TestServiceLifecycle.java?rev=1300894&r1=1300893&r2=1300894&view=diff
==============================================================================
--- hadoop/common/trunk/hadoop-mapreduce-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/service/TestServiceLifecycle.java (original)
+++ hadoop/common/trunk/hadoop-mapreduce-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/service/TestServiceLifecycle.java Thu Mar 15 10:39:17 2012
@@ -24,17 +24,11 @@ import org.junit.Test;
 
 public class TestServiceLifecycle extends ServiceAssert {
 
-  void assertStateCount(BreakableService service,
-                        Service.STATE state,
-                        int expected) {
-    int actual = service.getCount(state);
-    if (expected != actual) {
-      fail("Expected entry count for state [" + state +"] of " + service
-               + " to be " + expected + " but was " + actual);
-    }
-  }
-
-
+  /**
+   * Walk the {@link BreakableService} through it's lifecycle, 
+   * more to verify that service's counters work than anything else
+   * @throws Throwable if necessary
+   */
   @Test
   public void testWalkthrough() throws Throwable {
 
@@ -57,12 +51,14 @@ public class TestServiceLifecycle extend
 
   /**
    * call init twice
-   * @throws Throwable
+   * @throws Throwable if necessary
    */
   @Test
   public void testInitTwice() throws Throwable {
     BreakableService svc = new BreakableService();
-    svc.init(new Configuration());
+    Configuration conf = new Configuration();
+    conf.set("test.init","t");
+    svc.init(conf);
     try {
       svc.init(new Configuration());
       fail("Expected a failure, got " + svc);
@@ -70,11 +66,12 @@ public class TestServiceLifecycle extend
       //expected
     }
     assertStateCount(svc, Service.STATE.INITED, 2);
+    assertServiceConfigurationContains(svc, "test.init");
   }
 
   /**
-   * call start twice
-   * @throws Throwable
+   * Call start twice
+   * @throws Throwable if necessary
    */
   @Test
   public void testStartTwice() throws Throwable {
@@ -92,11 +89,11 @@ public class TestServiceLifecycle extend
 
 
   /**
-   * verify that when a service is stopped more than once, no exception
-   * is thrown, and the counter is incremented
-   * this is because the state change operations happen after the counter in
+   * Verify that when a service is stopped more than once, no exception
+   * is thrown, and the counter is incremented.
+   * This is because the state change operations happen after the counter in
    * the subclass is incremented, even though stop is meant to be a no-op
-   * @throws Throwable
+   * @throws Throwable if necessary
    */
   @Test
   public void testStopTwice() throws Throwable {
@@ -113,7 +110,7 @@ public class TestServiceLifecycle extend
   /**
    * Show that if the service failed during an init
    * operation, it stays in the created state, even after stopping it
-   * @throws Throwable
+   * @throws Throwable if necessary
    */
 
   @Test
@@ -139,7 +136,7 @@ public class TestServiceLifecycle extend
   /**
    * Show that if the service failed during an init
    * operation, it stays in the created state, even after stopping it
-   * @throws Throwable
+   * @throws Throwable if necessary
    */
 
   @Test
@@ -163,11 +160,10 @@ public class TestServiceLifecycle extend
   }
 
   /**
-   * verify that when a service is stopped more than once, no exception
-   * is thrown, and the counter is incremented
-   * this is because the state change operations happen after the counter in
-   * the subclass is incremented, even though stop is meant to be a no-op
-   * @throws Throwable
+   * verify that when a service fails during its stop operation,
+   * its state does not change, and the subclass invocation counter
+   * increments.
+   * @throws Throwable if necessary
    */
   @Test
   public void testFailingStop() throws Throwable {
@@ -181,6 +177,7 @@ public class TestServiceLifecycle extend
       //expected
     }
     assertStateCount(svc, Service.STATE.STOPPED, 1);
+    assertServiceStateStarted(svc);
     //now try again, and expect it to happen again
     try {
       svc.stop();
@@ -191,4 +188,31 @@ public class TestServiceLifecycle extend
     assertStateCount(svc, Service.STATE.STOPPED, 2);
   }
 
+  /**
+   * verify that when a service that is not started is stopped, its counter
+   * of stop calls is still incremented-and the service remains in its
+   * original state..
+   * @throws Throwable on a failure
+   */
+  @Test
+  public void testStopUnstarted() throws Throwable {
+    BreakableService svc = new BreakableService();
+    svc.stop();
+    assertServiceStateCreated(svc);
+    assertStateCount(svc, Service.STATE.STOPPED, 1);
+
+    //stop failed, now it can be initialised
+    svc.init(new Configuration());
+
+    //and try to stop again, with no state change but an increment
+    svc.stop();
+    assertServiceStateInited(svc);
+    assertStateCount(svc, Service.STATE.STOPPED, 2);
+
+    //once started, the service can be stopped reliably
+    svc.start();
+    ServiceOperations.stop(svc);
+    assertServiceStateStopped(svc);
+    assertStateCount(svc, Service.STATE.STOPPED, 3);
+  }
 }