View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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     * Check that we can start an HBase cluster specifying a custom set of
41     * RPC and infoserver ports.
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  }