From a78effcdc619bd4a6037d722a80e8289e4a8b15b Mon Sep 17 00:00:00 2001 From: zhangduo Date: Fri, 20 Mar 2015 09:32:16 +0800 Subject: [PATCH] HBASE-13295 TestInfoServers hung --- .../apache/hadoop/hbase/TestInfoServers.java | 72 ++++++++----------- 1 file changed, 30 insertions(+), 42 deletions(-) diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/TestInfoServers.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/TestInfoServers.java index a72b151349a..8ac35565141 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/TestInfoServers.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/TestInfoServers.java @@ -18,15 +18,16 @@ */ package org.apache.hadoop.hbase; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import java.io.BufferedInputStream; import java.io.IOException; +import java.io.InputStream; import java.net.URL; +import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.testclassification.MediumTests; import org.apache.hadoop.hbase.testclassification.MiscTests; import org.apache.hadoop.hbase.util.Bytes; @@ -54,6 +55,9 @@ public class TestInfoServers { //We need to make sure that the server can be started as read only. UTIL.getConfiguration().setBoolean("hbase.master.ui.readonly", true); UTIL.startMiniCluster(); + if (!UTIL.getHBaseCluster().waitForActiveAndReadyMaster(30000)) { + throw new RuntimeException("Active master not ready"); + } } @AfterClass @@ -69,12 +73,10 @@ public class TestInfoServers { // give the cluster time to start up UTIL.getConnection().getTable(TableName.META_TABLE_NAME).close(); int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort(); - assertContainsContent(new URL("http://localhost:" + port + - "/index.html"), "master-status"); - port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer(). - getInfoServer().getPort(); - assertContainsContent(new URL("http://localhost:" + port + - "/index.html"), "rs-status"); + assertContainsContent(new URL("http://localhost:" + port + "/index.html"), "master-status"); + port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer() + .getInfoServer().getPort(); + assertContainsContent(new URL("http://localhost:" + port + "/index.html"), "rs-status"); } /** @@ -86,15 +88,11 @@ public class TestInfoServers { */ @Test public void testInfoServersStatusPages() throws Exception { - // give the cluster time to start up - UTIL.getConnection().getTable(TableName.META_TABLE_NAME).close(); int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort(); - assertContainsContent(new URL("http://localhost:" + port + - "/master-status"), "meta"); - port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer(). - getInfoServer().getPort(); - assertContainsContent(new URL("http://localhost:" + port + - "/rs-status"), "meta"); + assertContainsContent(new URL("http://localhost:" + port + "/master-status"), "meta"); + port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer() + .getInfoServer().getPort(); + assertContainsContent(new URL("http://localhost:" + port + "/rs-status"), "meta"); } @Test @@ -102,44 +100,34 @@ public class TestInfoServers { TableName tableName = TableName.valueOf("testMasterServerReadOnly"); byte[] cf = Bytes.toBytes("d"); UTIL.createTable(tableName, cf); - UTIL.getConnection().getTable(tableName).close(); + UTIL.waitTableAvailable(tableName); int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort(); + assertDoesNotContainContent(new URL("http://localhost:" + port + "/table.jsp?name=" + tableName + + "&action=split&key="), "Table action request accepted"); assertDoesNotContainContent( - new URL("http://localhost:" + port + "/table.jsp?name=" + tableName + "&action=split&key="), - "Table action request accepted"); - assertDoesNotContainContent( - new URL("http://localhost:" + port + "/table.jsp?name=" + tableName), - "Actions:"); + new URL("http://localhost:" + port + "/table.jsp?name=" + tableName), "Actions:"); } - private void assertContainsContent(final URL u, final String expected) - throws IOException { + private void assertContainsContent(final URL u, final String expected) throws IOException { LOG.info("Testing " + u.toString() + " has " + expected); String content = getUrlContent(u); - assertTrue("expected=" + expected + ", content=" + content, + assertTrue("expected=" + expected + ", content=" + content, content.contains(expected)); + } + + private void assertDoesNotContainContent(final URL u, final String expected) throws IOException { + LOG.info("Testing " + u.toString() + " does not have " + expected); + String content = getUrlContent(u); + assertFalse("Does Not Contain =" + expected + ", content=" + content, content.contains(expected)); } - - - private void assertDoesNotContainContent(final URL u, final String expected) - throws IOException { - LOG.info("Testing " + u.toString() + " has " + expected); - String content = getUrlContent(u); - assertTrue("Does Not Contain =" + expected + ", content=" + content, - !content.contains(expected)); - } - private String getUrlContent(URL u) throws IOException { java.net.URLConnection c = u.openConnection(); + c.setConnectTimeout(2000); + c.setReadTimeout(2000); c.connect(); - StringBuilder sb = new StringBuilder(); - BufferedInputStream bis = new BufferedInputStream(c.getInputStream()); - byte [] bytes = new byte[1024]; - for (int read = -1; (read = bis.read(bytes)) != -1;) { - sb.append(new String(bytes, 0, read)); + try (InputStream in = c.getInputStream()) { + return IOUtils.toString(in); } - bis.close(); - return sb.toString(); } }