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.conf.Configuration;
026import org.apache.hadoop.hbase.testclassification.MediumTests;
027
028import org.junit.ClassRule;
029import org.junit.Test;
030import org.junit.experimental.categories.Category;
031
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035@Category(MediumTests.class)
036public class TestClusterPortAssignment {
037  @ClassRule
038  public static final HBaseClassTestRule CLASS_RULE =
039      HBaseClassTestRule.forClass(TestClusterPortAssignment.class);
040
041  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
042  private static final Logger LOG = LoggerFactory.getLogger(TestClusterPortAssignment.class);
043
044  /**
045   * Check that we can start an HBase cluster specifying a custom set of
046   * RPC and infoserver ports.
047   */
048  @Test
049  public void testClusterPortAssignment() throws Exception {
050    boolean retry = false;
051    do {
052      int masterPort =  HBaseTestingUtility.randomFreePort();
053      int masterInfoPort =  HBaseTestingUtility.randomFreePort();
054      int rsPort =  HBaseTestingUtility.randomFreePort();
055      int rsInfoPort =  HBaseTestingUtility.randomFreePort();
056      TEST_UTIL.getConfiguration().setBoolean(LocalHBaseCluster.ASSIGN_RANDOM_PORTS, false);
057      TEST_UTIL.getConfiguration().setInt(HConstants.MASTER_PORT, masterPort);
058      TEST_UTIL.getConfiguration().setInt(HConstants.MASTER_INFO_PORT, masterInfoPort);
059      TEST_UTIL.getConfiguration().setInt(HConstants.REGIONSERVER_PORT, rsPort);
060      TEST_UTIL.getConfiguration().setInt(HConstants.REGIONSERVER_INFO_PORT, rsInfoPort);
061      try {
062        MiniHBaseCluster cluster = TEST_UTIL.startMiniCluster();
063        assertTrue("Cluster failed to come up", cluster.waitForActiveAndReadyMaster(30000));
064        retry = false;
065        assertEquals("Master RPC port is incorrect", masterPort,
066          cluster.getMaster().getRpcServer().getListenerAddress().getPort());
067        assertEquals("Master info port is incorrect", masterInfoPort,
068          cluster.getMaster().getInfoServer().getPort());
069        assertEquals("RS RPC port is incorrect", rsPort,
070          cluster.getRegionServer(0).getRpcServer().getListenerAddress().getPort());
071        assertEquals("RS info port is incorrect", rsInfoPort,
072          cluster.getRegionServer(0).getInfoServer().getPort());
073      } catch (BindException e) {
074        LOG.info("Failed to bind, need to retry", e);
075        retry = true;
076      } finally {
077        TEST_UTIL.shutdownMiniCluster();
078      }
079    } while (retry);
080  }
081}