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  
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     * Check that we can start an HBase cluster specifying a custom set of
40     * RPC and infoserver ports.
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  }