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
23 import java.net.BindException;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.hbase.testclassification.MediumTests;
29
30 import org.junit.Test;
31 import org.junit.experimental.categories.Category;
32
33 @Category(MediumTests.class)
34 public class TestClusterPortAssignment {
35 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
36 private static final Log LOG = LogFactory.getLog(TestClusterPortAssignment.class);
37
38
39
40
41
42 @Test
43 public void testClusterPortAssignment() throws Exception {
44 boolean retry = false;
45 do {
46 int masterPort = HBaseTestingUtility.randomFreePort();
47 int masterInfoPort = HBaseTestingUtility.randomFreePort();
48 int rsPort = HBaseTestingUtility.randomFreePort();
49 int rsInfoPort = HBaseTestingUtility.randomFreePort();
50 TEST_UTIL.getConfiguration().setBoolean(LocalHBaseCluster.ASSIGN_RANDOM_PORTS, false);
51 TEST_UTIL.getConfiguration().setInt(HConstants.MASTER_PORT, masterPort);
52 TEST_UTIL.getConfiguration().setInt(HConstants.MASTER_INFO_PORT, masterInfoPort);
53 TEST_UTIL.getConfiguration().setInt(HConstants.REGIONSERVER_PORT, rsPort);
54 TEST_UTIL.getConfiguration().setInt(HConstants.REGIONSERVER_INFO_PORT, rsInfoPort);
55 try {
56 MiniHBaseCluster cluster = TEST_UTIL.startMiniCluster();
57 assertTrue("Cluster failed to come up", cluster.waitForActiveAndReadyMaster(30000));
58 retry = false;
59 assertEquals("Master RPC port is incorrect", masterPort,
60 cluster.getMaster().getRpcServer().getListenerAddress().getPort());
61 assertEquals("Master info port is incorrect", masterInfoPort,
62 cluster.getMaster().getInfoServer().getPort());
63 assertEquals("RS RPC port is incorrect", rsPort,
64 cluster.getRegionServer(0).getRpcServer().getListenerAddress().getPort());
65 assertEquals("RS info port is incorrect", rsInfoPort,
66 cluster.getRegionServer(0).getInfoServer().getPort());
67 } catch (BindException e) {
68 LOG.info("Failed to bind, need to retry", e);
69 retry = true;
70 } finally {
71 TEST_UTIL.shutdownMiniCluster();
72 }
73 } while (retry);
74 }
75 }