You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by ji...@apache.org on 2012/10/22 22:57:19 UTC

svn commit: r1401079 - in /incubator/ambari/branches/AMBARI-666: ./ ambari-server/src/main/java/org/apache/ambari/server/agent/ ambari-server/src/test/java/org/apache/ambari/server/agent/

Author: jitendra
Date: Mon Oct 22 20:57:18 2012
New Revision: 1401079

URL: http://svn.apache.org/viewvc?rev=1401079&view=rev
Log:
AMBARI-894. TestHeartBeatMonitor fails intermittently.

Modified:
    incubator/ambari/branches/AMBARI-666/AMBARI-666-CHANGES.txt
    incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/agent/ActionQueue.java
    incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/agent/HeartbeatMonitor.java
    incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatMonitor.java

Modified: incubator/ambari/branches/AMBARI-666/AMBARI-666-CHANGES.txt
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/AMBARI-666-CHANGES.txt?rev=1401079&r1=1401078&r2=1401079&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/AMBARI-666-CHANGES.txt (original)
+++ incubator/ambari/branches/AMBARI-666/AMBARI-666-CHANGES.txt Mon Oct 22 20:57:18 2012
@@ -12,6 +12,8 @@ AMBARI-666 branch (unreleased changes)
 
   NEW FEATURES
 
+  AMBARI-894. TestHeartBeatMonitor fails intermittently. (jitendra)
+
   AMBARI-892. Add puppet executor at the agent to be able to run various
   commands from the server. (mahadev)
 

Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/agent/ActionQueue.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/agent/ActionQueue.java?rev=1401079&r1=1401078&r2=1401079&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/agent/ActionQueue.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/agent/ActionQueue.java Mon Oct 22 20:57:18 2012
@@ -76,6 +76,10 @@ public class ActionQueue {
       return q.remove();
     }
   }
+  
+  public synchronized int size(String hostname) {
+    return getQueue(hostname).size();
+  }
 
   public List<AgentCommand> dequeueAll(String hostname) {
     LOG.info("Dequeue all elements for hostname: "+hostname);

Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/agent/HeartbeatMonitor.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/agent/HeartbeatMonitor.java?rev=1401079&r1=1401078&r2=1401079&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/agent/HeartbeatMonitor.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/agent/HeartbeatMonitor.java Mon Oct 22 20:57:18 2012
@@ -78,6 +78,7 @@ public class HeartbeatMonitor implements
   //notify action manager for node failure.
   private void doWork() throws InvalidStateTransitonException {
     List<Host> allHosts = fsm.getHosts();
+    LOG.info("Number of hosts to monitor: "+allHosts.size());
     long now = System.currentTimeMillis();
     for (Host hostObj : allHosts) {
       String host = hostObj.getHostName();
@@ -89,6 +90,7 @@ public class HeartbeatMonitor implements
       } catch (AmbariException e) {
         LOG.warn("Exception in getting host object; Is it fatal?", e);
       }
+      LOG.info("host : "+ hostObj.getHostName() +", last heartbeat = "+lastHeartbeat);
       if (lastHeartbeat + 5*threadWakeupInterval < now) {
         LOG.warn("Hearbeat lost from host "+host);
         //Heartbeat is expired

Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatMonitor.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatMonitor.java?rev=1401079&r1=1401078&r2=1401079&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatMonitor.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatMonitor.java Mon Oct 22 20:57:18 2012
@@ -17,44 +17,69 @@
  */
 package org.apache.ambari.server.agent;
 
-import static org.junit.Assert.*;
-import static org.mockito.Mockito.*;
-
-import java.util.ArrayList;
-import java.util.List;
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
 
+import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.actionmanager.ActionManager;
+import org.apache.ambari.server.orm.GuiceJpaInitializer;
+import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Host;
-import org.apache.ambari.server.state.HostEvent;
 import org.apache.ambari.server.state.HostState;
+import org.apache.ambari.server.state.cluster.ClustersImpl;
+import org.apache.ambari.server.state.fsm.InvalidStateTransitonException;
+import org.junit.After;
+import org.junit.Before;
 import org.junit.Test;
 
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.persist.PersistService;
+
 public class TestHeartbeatMonitor {
 
+  private static Injector injector;
+
+  @Before
+  public void setup() {
+    injector = Guice.createInjector(new InMemoryDefaultTestModule());
+    injector.getInstance(GuiceJpaInitializer.class);
+    //injector.getInstance(OrmTestHelper.class).createDefaultData();
+  }
+
+  @After
+  public void teardown() {
+    injector.getInstance(PersistService.class).stop();
+  }
+  
   @Test
-  public void testHeartbeatExpiry() throws Exception {
-    Clusters fsm = mock(Clusters.class);
+  public void testHeartbeatLoss() throws AmbariException, InterruptedException,
+      InvalidStateTransitonException {
+    Clusters fsm = new ClustersImpl(injector);
+    String hostname = "host1";
+    fsm.addHost(hostname);
     ActionQueue aq = new ActionQueue();
     ActionManager am = mock(ActionManager.class);
-    HostState hs = HostState.WAITING_FOR_HOST_STATUS_UPDATES;
-    List<Host> allHosts = new ArrayList<Host>();
-    Host hostObj = mock(Host.class);
-    allHosts.add(hostObj);
-    when(fsm.getHosts()).thenReturn(allHosts);
-    when(fsm.getHost("host1")).thenReturn(hostObj);
-    when(hostObj.getState()).thenReturn(hs);
-    when(hostObj.getHostName()).thenReturn("host1");
-    aq.enqueue("host1", new ExecutionCommand());
-    HeartbeatMonitor hm = new HeartbeatMonitor(fsm, aq, am, 100);
+    HeartbeatMonitor hm = new HeartbeatMonitor(fsm, aq, am, 10);
+    HeartBeatHandler handler = new HeartBeatHandler(fsm, aq, am);
+    Register reg = new Register();
+    reg.setHostname(hostname);
+    reg.setResponseId(12);
+    reg.setTimestamp(System.currentTimeMillis() - 300);
+    reg.setHardwareProfile(new HostInfo());
+    handler.handleRegistration(reg);
+    HeartBeat hb = new HeartBeat();
+    hb.setHostname(hostname);
+    hb.setNodeStatus(new HostStatus(HostStatus.Status.HEALTHY, "cool"));
+    hb.setTimestamp(System.currentTimeMillis());
+    hb.setResponseId(12);
+    handler.handleHeartBeat(hb);
     hm.start();
-    Thread.sleep(120);
-    //Heartbeat must have expired for host1, action queue must be flushed
-    assertEquals(0, aq.dequeueAll("host1").size());
-    verify(am, times(1)).handleLostHost("host1");
-    verify(hostObj, times(1)).handleEvent(any(HostEvent.class));
-    verify(hostObj, times(1)).setState(HostState.INIT);
-    hm.shutdown();
+    aq.enqueue(hostname, new ExecutionCommand());
+    //Heartbeat will expire and action queue will be flushed
+    while (aq.size(hostname) != 0) {
+      Thread.sleep(1);
+    }
+    assertEquals(fsm.getHost(hostname).getState(), HostState.HEARTBEAT_LOST);
   }
-
 }