You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by vg...@apache.org on 2011/12/21 23:58:58 UTC

svn commit: r1221928 - in /incubator/ambari/trunk/controller/src: main/java/org/apache/ambari/controller/Util.java main/java/org/apache/ambari/resource/statemachine/ClusterImpl.java test/java/org/apache/ambari/resource/statemachine/TestClusterImpl.java

Author: vgogate
Date: Wed Dec 21 22:58:58 2011
New Revision: 1221928

URL: http://svn.apache.org/viewvc?rev=1221928&view=rev
Log:
AMBARI-170. Update the cluster state after state machine transitions it to final ACTIVE/INACTIVE state

Modified:
    incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/Util.java
    incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/ClusterImpl.java
    incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestClusterImpl.java

Modified: incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/Util.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/Util.java?rev=1221928&r1=1221927&r2=1221928&view=diff
==============================================================================
--- incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/Util.java (original)
+++ incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/Util.java Wed Dec 21 22:58:58 2011
@@ -20,18 +20,24 @@ package org.apache.ambari.controller;
 import java.util.Date;
 import java.util.GregorianCalendar;
 
+import javax.xml.datatype.DatatypeConfigurationException;
 import javax.xml.datatype.DatatypeFactory;
 import javax.xml.datatype.XMLGregorianCalendar;
 
 public class Util {
     
-    public static XMLGregorianCalendar getXMLGregorianCalendar (Date date) throws Exception {
+    public static XMLGregorianCalendar getXMLGregorianCalendar (Date date)  {
         if (date == null) {
             return null;
         }
         GregorianCalendar cal = new GregorianCalendar();
         cal.setTime(date);
-        return DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);   
+        try {
+            return DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);  
+        } catch (NullPointerException ne) {
+        } catch (DatatypeConfigurationException de) {
+        }
+        return null;
     }
     
     public static String getInstallAndConfigureCommand() {

Modified: incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/ClusterImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/ClusterImpl.java?rev=1221928&r1=1221927&r2=1221928&view=diff
==============================================================================
--- incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/ClusterImpl.java (original)
+++ incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/ClusterImpl.java Wed Dec 21 22:58:58 2011
@@ -29,6 +29,8 @@ import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReadWriteLock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
+import javax.xml.datatype.DatatypeConfigurationException;
+
 import org.apache.ambari.common.rest.entities.ClusterState;
 import org.apache.ambari.common.state.MultipleArcTransition;
 import org.apache.ambari.common.state.SingleArcTransition;
@@ -255,19 +257,19 @@ public class ClusterImpl implements Clus
   }
   
   private void updateClusterState (String x) {
+      
       try {
-          ClusterState cs = this.cls.getClusterState();
-          cs.setLastUpdateTime(Util.getXMLGregorianCalendar(new Date()));
-          cs.setState(x);
-          this.cls.updateClusterState(cs);
-        } catch (Exception e) {
-            /*
-             * TODO: Handle the exception correctly. Should we bring down the controller? 
-             */
-            System.out.println ("Unbale to update/persist the cluster state change. Shutting down the controller!");
-            e.printStackTrace();
-            System.exit(-1);   
-        }
+        ClusterState cs = this.cls.getClusterState();
+        cs.setLastUpdateTime(Util.getXMLGregorianCalendar(new Date()));
+        cs.setState(x);
+        this.cls.updateClusterState(cs);
+      } catch (IOException e) {
+        /*
+         * TODO: Should we bring down the controller? 
+         */
+        System.out.println ("Unbale to update/persist the cluster state change. Shutting down the controller!");
+        e.printStackTrace();
+        System.exit(-1);  
+      }
   }
-
 }

Modified: incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestClusterImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestClusterImpl.java?rev=1221928&r1=1221927&r2=1221928&view=diff
==============================================================================
--- incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestClusterImpl.java (original)
+++ incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestClusterImpl.java Wed Dec 21 22:58:58 2011
@@ -3,7 +3,9 @@ package org.apache.ambari.resource.state
 import static org.mockito.Matchers.anyInt;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.doAnswer;
 import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
 
 import java.io.IOException;
 import java.util.ArrayList;
@@ -11,6 +13,9 @@ import java.util.ArrayList;
 import org.apache.ambari.common.rest.entities.ClusterDefinition;
 import org.apache.ambari.common.rest.entities.ClusterState;
 import org.apache.ambari.controller.Cluster;
+import org.mockito.Mockito;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
@@ -34,14 +39,16 @@ public class TestClusterImpl {
       ClusterStateFSM.ACTIVE
   };
   
+  ClusterState clsState;
+  Cluster cluster;
   
   @BeforeMethod
   public void setup() throws IOException{
     Guice.createInjector(new TestModule());
     ClusterDefinition clusterDef = mock(ClusterDefinition.class);
     when(clusterDef.getEnabledServices()).thenReturn(new ArrayList<String>());
-    Cluster cluster = mock(Cluster.class);
-    ClusterState clsState = mock(ClusterState.class);
+    cluster = mock(Cluster.class);
+    clsState = new ClusterState();
     when(cluster.getClusterDefinition(anyInt())).thenReturn(clusterDef);
     when(cluster.getClusterState()).thenReturn(clsState);
     clusterImpl = new ClusterImpl(cluster, 1);
@@ -53,6 +60,13 @@ public class TestClusterImpl {
    */
   @Test
   public void testInactiveToActive() throws Exception{
+    doAnswer(new Answer<Void>(){
+        public Void answer(InvocationOnMock invocation) throws Throwable {
+            ClusterState cs = (ClusterState)invocation.getArguments()[0];
+            assertTrue(cs.getState().equals(ClusterState.CLUSTER_STATE_ACTIVE));
+            return null;
+        }     
+    }).when(cluster).updateClusterState(clsState);
     verifyTransitions(ClusterStateFSM.INACTIVE, startEvents, startStates);
   }
 
@@ -63,6 +77,13 @@ public class TestClusterImpl {
    */
   @Test
   public void testFailToActive() throws Exception{
+    doAnswer(new Answer<Void>(){
+        public Void answer(InvocationOnMock invocation) throws Throwable {
+            ClusterState cs = (ClusterState)invocation.getArguments()[0];
+            assertTrue(cs.getState().equals(ClusterState.CLUSTER_STATE_ACTIVE));
+            return null;
+        }     
+    }).when(cluster).updateClusterState(clsState);
     verifyTransitions(ClusterStateFSM.FAIL, startEvents, startStates);
   }
   
@@ -83,6 +104,13 @@ public class TestClusterImpl {
    */
   @Test
   public void testActivetoInactive() throws Exception{
+    doAnswer(new Answer<Void>(){
+        public Void answer(InvocationOnMock invocation) throws Throwable {
+            ClusterState cs = (ClusterState)invocation.getArguments()[0];
+            assertTrue(cs.getState().equals(ClusterState.CLUSTER_STATE_INACTIVE));
+            return null;
+        }     
+    }).when(cluster).updateClusterState(clsState);
     verifyTransitions(ClusterStateFSM.ACTIVE, stopEvents, stopStates);
   }
   
@@ -93,6 +121,13 @@ public class TestClusterImpl {
    */
   @Test
   public void testFailtoInactive() throws Exception{
+    doAnswer(new Answer<Void>(){
+        public Void answer(InvocationOnMock invocation) throws Throwable {
+            ClusterState cs = (ClusterState)invocation.getArguments()[0];
+            assertTrue(cs.getState().equals(ClusterState.CLUSTER_STATE_INACTIVE));
+            return null;
+        }     
+    }).when(cluster).updateClusterState(clsState);
     verifyTransitions(ClusterStateFSM.FAIL, stopEvents, stopStates);
   }