1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23
24 import java.net.BindException;
25
26 import org.apache.commons.lang.exception.ExceptionUtils;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.hbase.testclassification.MediumTests;
30
31 import org.junit.Test;
32 import org.junit.experimental.categories.Category;
33
34 @Category(MediumTests.class)
35 public class TestClusterPortAssignment {
36 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
37 private static final Log LOG = LogFactory.getLog(TestClusterPortAssignment.class);
38
39
40
41
42
43 @Test(timeout = 300000)
44 public void testClusterPortAssignment() throws Exception {
45 boolean retry = false;
46 do {
47 int masterPort = HBaseTestingUtility.randomFreePort();
48 int masterInfoPort = HBaseTestingUtility.randomFreePort();
49 int rsPort = HBaseTestingUtility.randomFreePort();
50 int rsInfoPort = HBaseTestingUtility.randomFreePort();
51 TEST_UTIL.getConfiguration().setBoolean(LocalHBaseCluster.ASSIGN_RANDOM_PORTS, false);
52 TEST_UTIL.getConfiguration().setBoolean(HConstants.REGIONSERVER_INFO_PORT_AUTO, false);
53 TEST_UTIL.getConfiguration().setBoolean("fs.hdfs.impl.disable.cache", true);
54 TEST_UTIL.getConfiguration().setInt(HConstants.MASTER_PORT, masterPort);
55 TEST_UTIL.getConfiguration().setInt(HConstants.MASTER_INFO_PORT, masterInfoPort);
56 TEST_UTIL.getConfiguration().setInt(HConstants.REGIONSERVER_PORT, rsPort);
57 TEST_UTIL.getConfiguration().setInt(HConstants.REGIONSERVER_INFO_PORT, rsInfoPort);
58 LOG.info(
59 "Ports: " + masterPort + ", " + masterInfoPort + ", " + rsPort + ", "
60 + rsInfoPort);
61 try {
62 MiniHBaseCluster cluster = TEST_UTIL.startMiniCluster();
63 assertTrue("Cluster failed to come up", cluster.waitForActiveAndReadyMaster(30000));
64 retry = false;
65 assertEquals("Master RPC port is incorrect", masterPort,
66 cluster.getMaster().getRpcServer().getListenerAddress().getPort());
67 assertEquals("Master info port is incorrect", masterInfoPort,
68 cluster.getMaster().getInfoServer().getPort());
69 assertEquals("RS RPC port is incorrect", rsPort,
70 cluster.getRegionServer(0).getRpcServer().getListenerAddress().getPort());
71 assertEquals("RS info port is incorrect", rsInfoPort,
72 cluster.getRegionServer(0).getInfoServer().getPort());
73 } catch (Exception e) {
74 Throwable rootCause = ExceptionUtils.getRootCause(e);
75 if (rootCause instanceof BindException) {
76 LOG.info("Failed bind, need to retry", e);
77 retry = true;
78 } else {
79 LOG.error("Failed to start mini cluster", e);
80 retry = false;
81 fail("Failed to start mini cluster with assigned ports.");
82 }
83 } finally {
84 TEST_UTIL.shutdownMiniCluster();
85 }
86 } while (retry);
87 }
88 }