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