You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by sw...@apache.org on 2013/06/14 02:47:37 UTC

svn commit: r1492917 - in /incubator/ambari/trunk/ambari-server/src: main/java/org/apache/ambari/server/state/svccomphost/HBaseMasterPortScanner.java test/java/org/apache/ambari/server/state/svccomphost/HBaseMasterPortScannerTest.java

Author: swagle
Date: Fri Jun 14 00:47:36 2013
New Revision: 1492917

URL: http://svn.apache.org/r1492917
Log:
AMBARI-2363. Intermittent test failure with HBase port Scanner test. (Dmitry Lysnichenko via swagle)

Modified:
    incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/HBaseMasterPortScanner.java
    incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/HBaseMasterPortScannerTest.java

Modified: incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/HBaseMasterPortScanner.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/HBaseMasterPortScanner.java?rev=1492917&r1=1492916&r2=1492917&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/HBaseMasterPortScanner.java (original)
+++ incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/HBaseMasterPortScanner.java Fri Jun 14 00:47:36 2013
@@ -54,13 +54,13 @@ public class HBaseMasterPortScanner impl
   private final int port = 60010;
   private int maxAttempts = 3;
   private int attempts = 0;
-  private int countAttempts = 0;
+  protected int countAttempts = 0;
   private Map<ServiceComponentHost,Boolean> componentHostMap;
   private Cluster currentCluster;
   private Timer scheduleTimer;
   private RescanSchedulerTask rescanSchedulerTask;
   @Inject
-  private Clusters clusters;
+  protected Clusters clusters;
 
   /**
    * 
@@ -96,13 +96,6 @@ public class HBaseMasterPortScanner impl
     return testScanTimeoutMsc;
   }
 
-  /**
-   * 
-   * @return count attempts (need unitests)
-   */
-  public int getCountAttempts() {
-    return countAttempts;
-  }
 
   /**
    * 
@@ -120,10 +113,8 @@ public class HBaseMasterPortScanner impl
    */
   private boolean activeAwakeRequest = false;
 
-  public HBaseMasterPortScanner(int scanTimeoutMsc) {
-    this.defaultScanTimeoutMsc = scanTimeoutMsc;
-    this.scanTimeoutMsc = scanTimeoutMsc;
-    this.start();
+  public HBaseMasterPortScanner(Timer timer) {
+    scheduleTimer = timer;
   }
 
   public HBaseMasterPortScanner() {
@@ -139,9 +130,6 @@ public class HBaseMasterPortScanner impl
     }
   }
 
-  public void stop() {
-    schedulerThread.interrupt();
-  }
 
   /**
    * Should be called from another thread when we want HBase Master scanner to
@@ -221,50 +209,7 @@ public class HBaseMasterPortScanner impl
   @Override
   public void run() {
     while (true) {
-      if(rescanSchedulerTask != null){
-        rescanSchedulerTask.cancel();
-        scheduleTimer.purge();
-      }          
-      activeAwakeRequest = false;
-      if (componentHostMap != null) {
-        for (Map.Entry<ServiceComponentHost, Boolean> entry : componentHostMap.entrySet()) {
-          entry.setValue(scan(entry.getKey().getHostName()));
-          if (schedulerThread.isInterrupted()) {
-            scanTimeoutMsc = defaultScanTimeoutMsc;
-            return;
-          }
-          if (activeAwakeRequest) {
-            scanTimeoutMsc = defaultScanTimeoutMsc;
-            attempts = 0;
-            break;
-          }
-        }
-        attempts++;
-        countAttempts = attempts;
-        LOG.info("Attempt to scan of HBASE_MASTER port : "+ attempts);
-        if(validateScanResults(componentHostMap)){
-          //If results valid set it to ServiceComponentHost
-          setScanResults(componentHostMap);
-          scanTimeoutMsc = defaultScanTimeoutMsc;
-          attempts = 0;
-        } else {
-          if(attempts <= maxAttempts){
-            //Increase timeout
-            scanTimeoutMsc += defaultScanTimeoutMsc;
-            testScanTimeoutMsc = scanTimeoutMsc;
-            LOG.info("Increase timeout for scan HBASE_MASTER port to : "+ scanTimeoutMsc);
-            activeAwakeRequest = true;
-          } else {
-            LOG.info("No valid data about HBASE_MASTER, ports will rescanned after "+rescanTimeoutMsc/1000 + " seconds");
-            scanTimeoutMsc = defaultScanTimeoutMsc;
-            attempts = 0;
-            //Create task for latter scan
-            rescanSchedulerTask = new RescanSchedulerTask(currentCluster);
-            scheduleTimer.schedule(rescanSchedulerTask, rescanTimeoutMsc);
-          }
-        }        
-        
-      }
+      execute();
       if (activeAwakeRequest) {
         activeAwakeRequest = false;
         continue;
@@ -279,6 +224,50 @@ public class HBaseMasterPortScanner impl
     }
   }
 
+  protected void execute() {
+    if (rescanSchedulerTask != null) {
+      rescanSchedulerTask.cancel();
+      scheduleTimer.purge();
+    }
+    activeAwakeRequest = false;
+    if (componentHostMap != null) {
+      for (Map.Entry<ServiceComponentHost, Boolean> entry : componentHostMap.entrySet()) {
+        entry.setValue(scan(entry.getKey().getHostName()));
+        if (activeAwakeRequest) {
+          scanTimeoutMsc = defaultScanTimeoutMsc;
+          attempts = 0;
+          break;
+        }
+      }
+      attempts++;
+      countAttempts = attempts;
+      LOG.info("Attempt to scan of HBASE_MASTER port : " + attempts);
+      if (validateScanResults(componentHostMap)) {
+        //If results valid set it to ServiceComponentHost
+        setScanResults(componentHostMap);
+        scanTimeoutMsc = defaultScanTimeoutMsc;
+        attempts = 0;
+      } else {
+        if (attempts <= maxAttempts) {
+          //Increase timeout
+          scanTimeoutMsc += defaultScanTimeoutMsc;
+          testScanTimeoutMsc = scanTimeoutMsc;
+          LOG.info("Increase timeout for scan HBASE_MASTER port to : " + scanTimeoutMsc);
+          activeAwakeRequest = true;
+        } else {
+          LOG.info("No valid data about HBASE_MASTER, ports will rescanned after " + rescanTimeoutMsc / 1000 + " seconds");
+          scanTimeoutMsc = defaultScanTimeoutMsc;
+          attempts = 0;
+          //Create task for latter scan
+          rescanSchedulerTask = new RescanSchedulerTask(currentCluster);
+          scheduleTimer.schedule(rescanSchedulerTask, rescanTimeoutMsc);
+        }
+      }
+
+    }
+
+  }
+
   private void setScanResults(Map<ServiceComponentHost, Boolean> scanResuls){
     for (Map.Entry<ServiceComponentHost, Boolean> entry : scanResuls.entrySet()) {
       entry.getKey().setHAState((entry.getValue()) ? "active" : "passive");
@@ -302,7 +291,7 @@ public class HBaseMasterPortScanner impl
     return res;  
   }
   
-  private boolean scan(String hostname) {
+  protected boolean scan(String hostname) {
     try {
       Socket socket = new Socket();
       socket.connect(new InetSocketAddress(hostname, port), scanTimeoutMsc);
@@ -334,5 +323,5 @@ public class HBaseMasterPortScanner impl
     }
     
   }
-   
+
 }

Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/HBaseMasterPortScannerTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/HBaseMasterPortScannerTest.java?rev=1492917&r1=1492916&r2=1492917&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/HBaseMasterPortScannerTest.java (original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/HBaseMasterPortScannerTest.java Fri Jun 14 00:47:36 2013
@@ -16,14 +16,16 @@
  */
 package org.apache.ambari.server.state.svccomphost;
 
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 import com.google.inject.Guice;
 import com.google.inject.Injector;
-import java.io.IOException;
-import java.net.ServerSocket;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
+import java.util.Timer;
 import org.apache.ambari.server.AmbariException;
 import static org.apache.ambari.server.agent.DummyHeartbeatConstants.DATANODE;
 import static org.apache.ambari.server.agent.DummyHeartbeatConstants.DummyCluster;
@@ -41,7 +43,6 @@ import org.apache.ambari.server.state.Se
 import org.apache.ambari.server.state.ServiceComponentHost;
 import org.apache.ambari.server.state.ServiceFactory;
 import org.apache.ambari.server.state.StackId;
-import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -57,9 +58,8 @@ public class HBaseMasterPortScannerTest 
 
   private static final Logger log = LoggerFactory.getLogger(HBaseMasterPortScannerTest.class);
   private static List<String> hostnames;
-  private static ServerSocket serverSocket;
   private static Injector injector;
-  private static HBaseMasterPortScanner scaner;
+  private static HBaseMasterPortScannerMock scaner;
   private static ServiceFactory serviceFactory;
   private static AmbariMetaInfo metaInfo;
   private static Clusters clusters;
@@ -67,40 +67,12 @@ public class HBaseMasterPortScannerTest 
   private static Host host;
   private static ServiceComponentHost serviceComponentHost;
   private static int scanTimeOut = 100; 
-  private static int reScanTimeOut = 3000;
+  private static int reScanTimeOut = 1000;
   private static int maxAttempts = 2;
+  private static Timer timerMock = mock(Timer.class);
 
   public HBaseMasterPortScannerTest() {
   }
-
-  private static void setUpPortState(boolean open) {
-    if (open) {
-      if (serverSocket == null || serverSocket.isClosed()) {
-        try {
-          serverSocket = new ServerSocket(60010);
-        } catch (IOException e) {
-          try {
-            serverSocket.close();
-          } catch (IOException ex) {
-            log.debug("Could not close on port: 60010");
-            log.error(ex.getMessage());
-          }
-          log.error("Could not listen on port: 60010");
-        }
-      }
-    } else {
-      if (serverSocket != null && !serverSocket.isClosed()) {
-        try {
-          serverSocket.close();
-          serverSocket = null;
-        } catch (IOException ex) {
-          log.debug("Could not close on port: 60010");
-          log.error(ex.getMessage());
-        }
-      }
-    }
-  }
-  
   
   @BeforeClass
   public static void setUpClass() throws Exception {
@@ -111,8 +83,8 @@ public class HBaseMasterPortScannerTest 
     hostnames.add("host1");
     hostnames.add("host2");
     hostnames.add("host3");
-    scaner = injector.getInstance(HBaseMasterPortScanner.class);
     clusters = injector.getInstance(Clusters.class);
+    scaner = new HBaseMasterPortScannerMock(clusters);
     metaInfo = injector.getInstance(AmbariMetaInfo.class);
     serviceFactory = injector.getInstance(ServiceFactory.class);
     metaInfo.init();
@@ -151,17 +123,9 @@ public class HBaseMasterPortScannerTest 
         serviceComponentHost = service.getServiceComponent(HBASE_MASTER).getServiceComponentHost(hostname);
       }
     }
+    when(timerMock.purge()).thenReturn(0);
   }
 
-  @AfterClass
-  public static void tearDownUpClass() {
-    try {
-      if(serverSocket!=null) serverSocket.close();
-    } catch (IOException ex) {
-      log.debug("Could not close on port: 60010");
-      log.error(ex.getMessage());
-    }
-  }
 
   @Before
   public void setUp() throws AmbariException, Exception {
@@ -173,13 +137,12 @@ public class HBaseMasterPortScannerTest 
    */
   @Test
   public void testUpdateHBaseMaster_Cluster() throws InterruptedException {
-    setUpPortState(true);
     scaner.setDefaultScanTimeoutMsc(scanTimeOut);
     scaner.setMaxAttempts(maxAttempts);
     scaner.setRescanTimeoutMsc(reScanTimeOut);
     log.debug("updateHBaseMaster - pass Cluster");
     scaner.updateHBaseMaster(cluster);
-    Thread.sleep(1000);
+    scaner.execute();
     assertEquals("active", serviceComponentHost.convertToResponse().getHa_status());
   }
 
@@ -188,13 +151,12 @@ public class HBaseMasterPortScannerTest 
    */
   @Test
   public void testUpdateHBaseMaster_Host() throws InterruptedException {
-    setUpPortState(true);
     scaner.setDefaultScanTimeoutMsc(scanTimeOut);
     scaner.setMaxAttempts(maxAttempts);
     scaner.setRescanTimeoutMsc(reScanTimeOut);
     log.debug("updateHBaseMaster - pass Host");
     scaner.updateHBaseMaster(host);
-    Thread.sleep(1000);
+    scaner.execute();
     assertEquals("active", serviceComponentHost.convertToResponse().getHa_status());
   }
 
@@ -203,58 +165,78 @@ public class HBaseMasterPortScannerTest 
    */
   @Test
   public void testUpdateHBaseMaster_ServiceComponentHost() throws InterruptedException {
-    setUpPortState(true);
     scaner.setDefaultScanTimeoutMsc(scanTimeOut);
     scaner.setMaxAttempts(maxAttempts);
     scaner.setRescanTimeoutMsc(reScanTimeOut);    
     log.debug("updateHBaseMaster - pass ServiceComponentHost");
     scaner.updateHBaseMaster(serviceComponentHost);
-    Thread.sleep(1000);
+    scaner.execute();
     assertEquals("active", serviceComponentHost.convertToResponse().getHa_status());
   }
 
-  /**
-   * Test of multiple call of updateHBaseMaster method.
-   */
-  @Test
-  public void testMultipleCall() throws InterruptedException {
-    setUpPortState(true);
-    scaner.setDefaultScanTimeoutMsc(scanTimeOut);
-    scaner.setMaxAttempts(maxAttempts);
-    scaner.setRescanTimeoutMsc(reScanTimeOut);    
-    log.debug("updateHBaseMaster - pass ServiceComponentHost");
-    //Test if some call of updateHBaseMaster in short time
-    scaner.updateHBaseMaster(cluster);
-    scaner.updateHBaseMaster(host);
-    scaner.updateHBaseMaster(serviceComponentHost);
-    Thread.sleep(1000);
-    assertEquals("active", serviceComponentHost.convertToResponse().getHa_status());
-  }  
-  
+
   /**
    * Test case of if port is closed or not enough scan timeout.
    */
   @Test
   public void testOfBrokenMasterScenario() throws InterruptedException {
-    setUpPortState(false);
+    scaner.setLiveHBaseHost("");
     scaner.setDefaultScanTimeoutMsc(scanTimeOut);
     scaner.setMaxAttempts(maxAttempts);
     scaner.setRescanTimeoutMsc(reScanTimeOut);
     log.debug("testOfBrokenMasterScenario start");
     scaner.updateHBaseMaster(cluster);
-    Thread.sleep(2000);
+    scaner.execute(3);
     //Should not be active masters
     assertEquals("passive", serviceComponentHost.convertToResponse().getHa_status());
     serviceComponentHost.setHAState("passive");
     //Scanner should try to scan maxAttempts times
     assertEquals(maxAttempts, scaner.getCountAttempts()-1);
-    //Timeout for scan shoul be scanTimeOut * scaner.getCountAttempts()
+    //Timeout for scan should be scanTimeOut * scaner.getCountAttempts()
     assertEquals(scanTimeOut * scaner.getCountAttempts(), scaner.getTestScanTimeoutMsc());
     //Task for latter scan shoul be created
     assertNotNull(scaner.getRescanSchedulerTask());
-    setUpPortState(true);
-    Thread.sleep(3500);
+    scaner.setLiveHBaseHost("127.0.0.1");
+    scaner.execute(3);
     //Test active masters after latter rescan
     assertEquals("active", serviceComponentHost.convertToResponse().getHa_status());
-  }  
+  }
+  
+
+  public static class HBaseMasterPortScannerMock extends HBaseMasterPortScanner {
+
+    private String liveHBaseHost = "127.0.0.1";
+
+    public void setLiveHBaseHost(String liveHBaseHost) {
+      this.liveHBaseHost = liveHBaseHost;
+    }
+    
+    @Override
+    protected boolean scan(String hostname) {
+      return (hostname.equals(liveHBaseHost)) ? true : false;
+    }
+
+    public int getCountAttempts() {
+      return countAttempts;
+    }
+
+        
+    public HBaseMasterPortScannerMock(Clusters c) {
+      super(timerMock);
+      clusters = c;
+    }
+
+    @Override
+    public void execute() {
+      super.execute();
+    }
+    
+    public void execute(int count) {
+      for (int i = 0; i < count; i++) {
+        execute();
+      }
+    }    
+    
+  }
+  
 }
\ No newline at end of file