001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.net.BindException;
024
025import org.apache.hadoop.hbase.testclassification.MediumTests;
026
027import org.junit.ClassRule;
028import org.junit.Test;
029import org.junit.experimental.categories.Category;
030
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034@Category(MediumTests.class)
035public class TestClusterPortAssignment {
036  @ClassRule
037  public static final HBaseClassTestRule CLASS_RULE =
038      HBaseClassTestRule.forClass(TestClusterPortAssignment.class);
039
040  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
041  private static final Logger LOG = LoggerFactory.getLogger(TestClusterPortAssignment.class);
042
043  /**
044   * Check that we can start an HBase cluster specifying a custom set of
045   * RPC and infoserver ports.
046   */
047  @Test
048  public void testClusterPortAssignment() throws Exception {
049    boolean retry = false;
050    do {
051      int masterPort =  HBaseTestingUtility.randomFreePort();
052      int masterInfoPort =  HBaseTestingUtility.randomFreePort();
053      int rsPort =  HBaseTestingUtility.randomFreePort();
054      int rsInfoPort =  HBaseTestingUtility.randomFreePort();
055      TEST_UTIL.getConfiguration().setBoolean(LocalHBaseCluster.ASSIGN_RANDOM_PORTS, false);
056      TEST_UTIL.getConfiguration().setInt(HConstants.MASTER_PORT, masterPort);
057      TEST_UTIL.getConfiguration().setInt(HConstants.MASTER_INFO_PORT, masterInfoPort);
058      TEST_UTIL.getConfiguration().setInt(HConstants.REGIONSERVER_PORT, rsPort);
059      TEST_UTIL.getConfiguration().setInt(HConstants.REGIONSERVER_INFO_PORT, rsInfoPort);
060      try {
061        MiniHBaseCluster cluster = TEST_UTIL.startMiniCluster();
062        assertTrue("Cluster failed to come up", cluster.waitForActiveAndReadyMaster(30000));
063        retry = false;
064        assertEquals("Master RPC port is incorrect", masterPort,
065          cluster.getMaster().getRpcServer().getListenerAddress().getPort());
066        assertEquals("Master info port is incorrect", masterInfoPort,
067          cluster.getMaster().getInfoServer().getPort());
068        assertEquals("RS RPC port is incorrect", rsPort,
069          cluster.getRegionServer(0).getRpcServer().getListenerAddress().getPort());
070        assertEquals("RS info port is incorrect", rsInfoPort,
071          cluster.getRegionServer(0).getInfoServer().getPort());
072      } catch (BindException e) {
073        LOG.info("Failed to bind, need to retry", e);
074        retry = true;
075      } finally {
076        TEST_UTIL.shutdownMiniCluster();
077      }
078    } while (retry);
079  }
080}