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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org
.apache
.hadoop
.hbase
.client
;
20 import static org
.apache
.hadoop
.hbase
.HConstants
.META_REPLICAS_NUM
;
21 import static org
.junit
.Assert
.assertEquals
;
22 import java
.net
.UnknownHostException
;
23 import java
.util
.ArrayList
;
24 import java
.util
.Arrays
;
25 import java
.util
.Collections
;
26 import java
.util
.Comparator
;
27 import java
.util
.List
;
28 import org
.apache
.hadoop
.conf
.Configuration
;
29 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
30 import org
.apache
.hadoop
.hbase
.HBaseTestingUtility
;
31 import org
.apache
.hadoop
.hbase
.HConstants
;
32 import org
.apache
.hadoop
.hbase
.HRegionLocation
;
33 import org
.apache
.hadoop
.hbase
.ServerName
;
34 import org
.apache
.hadoop
.hbase
.StartMiniClusterOption
;
35 import org
.apache
.hadoop
.hbase
.master
.HMaster
;
36 import org
.apache
.hadoop
.hbase
.testclassification
.ClientTests
;
37 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
38 import org
.junit
.AfterClass
;
39 import org
.junit
.BeforeClass
;
40 import org
.junit
.ClassRule
;
41 import org
.junit
.Test
;
42 import org
.junit
.experimental
.categories
.Category
;
44 @Category({ MediumTests
.class, ClientTests
.class })
45 public class TestMasterRegistry
{
48 public static final HBaseClassTestRule CLASS_RULE
=
49 HBaseClassTestRule
.forClass(TestMasterRegistry
.class);
50 private static final HBaseTestingUtility TEST_UTIL
= new HBaseTestingUtility();
53 public static void setUp() throws Exception
{
54 TEST_UTIL
.getConfiguration().setInt(META_REPLICAS_NUM
, 3);
55 StartMiniClusterOption
.Builder builder
= StartMiniClusterOption
.builder();
56 builder
.numMasters(3).numRegionServers(3);
57 TEST_UTIL
.startMiniCluster(builder
.build());
61 public static void tearDown() throws Exception
{
62 TEST_UTIL
.shutdownMiniCluster();
66 * Generates a string of dummy master addresses in host:port format. Every other hostname won't
69 private static String
generateDummyMastersList(int size
) {
70 List
<String
> masters
= new ArrayList
<>();
71 for (int i
= 0; i
< size
; i
++) {
72 masters
.add(" localhost" + (i
% 2 == 0 ?
":" + (1000 + i
) : ""));
74 return String
.join(",", masters
);
78 * Makes sure the master registry parses the master end points in the configuration correctly.
80 @Test public void testMasterAddressParsing() throws UnknownHostException
{
81 Configuration conf
= new Configuration(TEST_UTIL
.getConfiguration());
83 conf
.set(HConstants
.MASTER_ADDRS_KEY
, generateDummyMastersList(numMasters
));
84 try (MasterRegistry registry
= new MasterRegistry(conf
)) {
85 List
<ServerName
> parsedMasters
= new ArrayList
<>(registry
.getParsedMasterServers());
86 // Half of them would be without a port, duplicates are removed.
87 assertEquals(numMasters
/2 + 1, parsedMasters
.size());
88 // Sort in the increasing order of port numbers.
89 Collections
.sort(parsedMasters
, Comparator
.comparingInt(ServerName
::getPort
));
90 for (int i
= 0; i
< parsedMasters
.size(); i
++) {
91 ServerName sn
= parsedMasters
.get(i
);
92 assertEquals("localhost", sn
.getHostname());
93 if (i
== parsedMasters
.size() - 1) {
94 // Last entry should be the one with default port.
95 assertEquals(HConstants
.DEFAULT_MASTER_PORT
, sn
.getPort());
97 assertEquals(1000 + (2 * i
), sn
.getPort());
103 @Test public void testRegistryRPCs() throws Exception
{
104 Configuration conf
= new Configuration(TEST_UTIL
.getConfiguration());
105 HMaster activeMaster
= TEST_UTIL
.getHBaseCluster().getMaster();
106 for (int numHedgedReqs
= 1; numHedgedReqs
<=3; numHedgedReqs
++) {
107 if (numHedgedReqs
== 1) {
108 conf
.setBoolean(HConstants
.MASTER_REGISTRY_ENABLE_HEDGED_READS_KEY
, false);
110 conf
.setBoolean(HConstants
.MASTER_REGISTRY_ENABLE_HEDGED_READS_KEY
, true);
112 conf
.setInt(HConstants
.HBASE_RPCS_HEDGED_REQS_FANOUT_KEY
, numHedgedReqs
);
113 try (MasterRegistry registry
= new MasterRegistry(conf
)) {
114 assertEquals(registry
.getClusterId().get(), activeMaster
.getClusterId());
115 assertEquals(registry
.getActiveMaster().get(), activeMaster
.getServerName());
116 List
<HRegionLocation
> metaLocations
=
117 Arrays
.asList(registry
.getMetaRegionLocations().get().getRegionLocations());
118 List
<HRegionLocation
> actualMetaLocations
= activeMaster
.getMetaRegionLocationCache()
119 .getMetaRegionLocations().get();
120 Collections
.sort(metaLocations
);
121 Collections
.sort(actualMetaLocations
);
122 assertEquals(actualMetaLocations
, metaLocations
);