HBASE-13295 TestInfoServers hung

This commit is contained in:
zhangduo 2015-03-20 09:32:16 +08:00
parent 306c44156a
commit a78effcdc6

View File

@ -18,15 +18,16 @@
*/ */
package org.apache.hadoop.hbase; package org.apache.hadoop.hbase;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.io.BufferedInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.URL; import java.net.URL;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.MediumTests;
import org.apache.hadoop.hbase.testclassification.MiscTests; import org.apache.hadoop.hbase.testclassification.MiscTests;
import org.apache.hadoop.hbase.util.Bytes; 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. //We need to make sure that the server can be started as read only.
UTIL.getConfiguration().setBoolean("hbase.master.ui.readonly", true); UTIL.getConfiguration().setBoolean("hbase.master.ui.readonly", true);
UTIL.startMiniCluster(); UTIL.startMiniCluster();
if (!UTIL.getHBaseCluster().waitForActiveAndReadyMaster(30000)) {
throw new RuntimeException("Active master not ready");
}
} }
@AfterClass @AfterClass
@ -69,12 +73,10 @@ public class TestInfoServers {
// give the cluster time to start up // give the cluster time to start up
UTIL.getConnection().getTable(TableName.META_TABLE_NAME).close(); UTIL.getConnection().getTable(TableName.META_TABLE_NAME).close();
int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort(); int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort();
assertContainsContent(new URL("http://localhost:" + port + assertContainsContent(new URL("http://localhost:" + port + "/index.html"), "master-status");
"/index.html"), "master-status"); port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer()
port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer(). .getInfoServer().getPort();
getInfoServer().getPort(); assertContainsContent(new URL("http://localhost:" + port + "/index.html"), "rs-status");
assertContainsContent(new URL("http://localhost:" + port +
"/index.html"), "rs-status");
} }
/** /**
@ -86,15 +88,11 @@ public class TestInfoServers {
*/ */
@Test @Test
public void testInfoServersStatusPages() throws Exception { 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(); int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort();
assertContainsContent(new URL("http://localhost:" + port + assertContainsContent(new URL("http://localhost:" + port + "/master-status"), "meta");
"/master-status"), "meta"); port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer()
port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer(). .getInfoServer().getPort();
getInfoServer().getPort(); assertContainsContent(new URL("http://localhost:" + port + "/rs-status"), "meta");
assertContainsContent(new URL("http://localhost:" + port +
"/rs-status"), "meta");
} }
@Test @Test
@ -102,44 +100,34 @@ public class TestInfoServers {
TableName tableName = TableName.valueOf("testMasterServerReadOnly"); TableName tableName = TableName.valueOf("testMasterServerReadOnly");
byte[] cf = Bytes.toBytes("d"); byte[] cf = Bytes.toBytes("d");
UTIL.createTable(tableName, cf); UTIL.createTable(tableName, cf);
UTIL.getConnection().getTable(tableName).close(); UTIL.waitTableAvailable(tableName);
int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort(); 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( assertDoesNotContainContent(
new URL("http://localhost:" + port + "/table.jsp?name=" + tableName + "&action=split&key="), new URL("http://localhost:" + port + "/table.jsp?name=" + tableName), "Actions:");
"Table action request accepted");
assertDoesNotContainContent(
new URL("http://localhost:" + port + "/table.jsp?name=" + tableName),
"Actions:");
} }
private void assertContainsContent(final URL u, final String expected) private void assertContainsContent(final URL u, final String expected) throws IOException {
throws IOException {
LOG.info("Testing " + u.toString() + " has " + expected); LOG.info("Testing " + u.toString() + " has " + expected);
String content = getUrlContent(u); 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)); 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 { private String getUrlContent(URL u) throws IOException {
java.net.URLConnection c = u.openConnection(); java.net.URLConnection c = u.openConnection();
c.setConnectTimeout(2000);
c.setReadTimeout(2000);
c.connect(); c.connect();
StringBuilder sb = new StringBuilder(); try (InputStream in = c.getInputStream()) {
BufferedInputStream bis = new BufferedInputStream(c.getInputStream()); return IOUtils.toString(in);
byte [] bytes = new byte[1024];
for (int read = -1; (read = bis.read(bytes)) != -1;) {
sb.append(new String(bytes, 0, read));
} }
bis.close();
return sb.toString();
} }
} }