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
.junit
.Assert
.assertEquals
;
21 import static org
.junit
.Assert
.assertFalse
;
22 import static org
.junit
.Assert
.assertTrue
;
24 import java
.io
.IOException
;
25 import java
.util
.ArrayList
;
26 import java
.util
.Arrays
;
27 import java
.util
.Collections
;
28 import java
.util
.Comparator
;
29 import java
.util
.List
;
31 import org
.apache
.hadoop
.conf
.Configuration
;
32 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
33 import org
.apache
.hadoop
.hbase
.HBaseTestingUtil
;
34 import org
.apache
.hadoop
.hbase
.HConstants
;
35 import org
.apache
.hadoop
.hbase
.HRegionLocation
;
36 import org
.apache
.hadoop
.hbase
.ServerName
;
37 import org
.apache
.hadoop
.hbase
.StartTestingClusterOption
;
38 import org
.apache
.hadoop
.hbase
.TableName
;
39 import org
.apache
.hadoop
.hbase
.Waiter
;
40 import org
.apache
.hadoop
.hbase
.master
.HMaster
;
41 import org
.apache
.hadoop
.hbase
.testclassification
.ClientTests
;
42 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
43 import org
.junit
.AfterClass
;
44 import org
.junit
.BeforeClass
;
45 import org
.junit
.ClassRule
;
46 import org
.junit
.Test
;
47 import org
.junit
.experimental
.categories
.Category
;
49 import org
.apache
.hbase
.thirdparty
.com
.google
.common
.base
.Preconditions
;
51 @Category({ MediumTests
.class, ClientTests
.class })
52 public class TestMasterRegistry
{
55 public static final HBaseClassTestRule CLASS_RULE
=
56 HBaseClassTestRule
.forClass(TestMasterRegistry
.class);
57 private static final HBaseTestingUtil TEST_UTIL
= new HBaseTestingUtil();
60 public static void setUp() throws Exception
{
61 StartTestingClusterOption
.Builder builder
= StartTestingClusterOption
.builder();
62 builder
.numMasters(3).numRegionServers(3);
63 TEST_UTIL
.startMiniCluster(builder
.build());
64 HBaseTestingUtil
.setReplicas(TEST_UTIL
.getAdmin(), TableName
.META_TABLE_NAME
, 3);
68 public static void tearDown() throws Exception
{
69 TEST_UTIL
.shutdownMiniCluster();
73 * Generates a string of dummy master addresses in host:port format. Every other hostname won't
76 private static String
generateDummyMastersList(int size
) {
77 List
<String
> masters
= new ArrayList
<>();
78 for (int i
= 0; i
< size
; i
++) {
79 masters
.add(" localhost" + (i
% 2 == 0 ?
":" + (1000 + i
) : ""));
81 return String
.join(",", masters
);
85 * Makes sure the master registry parses the master end points in the configuration correctly.
88 public void testMasterAddressParsing() throws IOException
{
89 Configuration conf
= new Configuration(TEST_UTIL
.getConfiguration());
91 conf
.set(HConstants
.MASTER_ADDRS_KEY
, generateDummyMastersList(numMasters
));
92 try (MasterRegistry registry
= new MasterRegistry(conf
)) {
93 List
<ServerName
> parsedMasters
= new ArrayList
<>(registry
.getParsedServers());
94 // Half of them would be without a port, duplicates are removed.
95 assertEquals(numMasters
/ 2 + 1, parsedMasters
.size());
96 // Sort in the increasing order of port numbers.
97 Collections
.sort(parsedMasters
, Comparator
.comparingInt(ServerName
::getPort
));
98 for (int i
= 0; i
< parsedMasters
.size(); i
++) {
99 ServerName sn
= parsedMasters
.get(i
);
100 assertEquals("localhost", sn
.getHostname());
101 if (i
== parsedMasters
.size() - 1) {
102 // Last entry should be the one with default port.
103 assertEquals(HConstants
.DEFAULT_MASTER_PORT
, sn
.getPort());
105 assertEquals(1000 + (2 * i
), sn
.getPort());
112 public void testRegistryRPCs() throws Exception
{
113 Configuration conf
= new Configuration(TEST_UTIL
.getConfiguration());
114 HMaster activeMaster
= TEST_UTIL
.getHBaseCluster().getMaster();
115 final int size
= activeMaster
.getMetaLocations().size();
116 for (int numHedgedReqs
= 1; numHedgedReqs
<= size
; numHedgedReqs
++) {
117 conf
.setInt(MasterRegistry
.MASTER_REGISTRY_HEDGED_REQS_FANOUT_KEY
, numHedgedReqs
);
118 try (MasterRegistry registry
= new MasterRegistry(conf
)) {
119 // Add wait on all replicas being assigned before proceeding w/ test. Failed on occasion
120 // because not all replicas had made it up before test started.
121 RegionReplicaTestHelper
.waitUntilAllMetaReplicasAreReady(TEST_UTIL
, registry
);
122 assertEquals(registry
.getClusterId().get(), activeMaster
.getClusterId());
123 assertEquals(registry
.getActiveMaster().get(), activeMaster
.getServerName());
124 List
<HRegionLocation
> metaLocations
=
125 Arrays
.asList(registry
.getMetaRegionLocations().get().getRegionLocations());
126 List
<HRegionLocation
> actualMetaLocations
= activeMaster
.getMetaLocations();
127 Collections
.sort(metaLocations
);
128 Collections
.sort(actualMetaLocations
);
129 assertEquals(actualMetaLocations
, metaLocations
);
135 * Tests that the list of masters configured in the MasterRegistry is dynamically refreshed in the
139 public void testDynamicMasterConfigurationRefresh() throws Exception
{
140 Configuration conf
= new Configuration(TEST_UTIL
.getConfiguration());
141 String currentMasterAddrs
= Preconditions
.checkNotNull(conf
.get(HConstants
.MASTER_ADDRS_KEY
));
142 HMaster activeMaster
= TEST_UTIL
.getHBaseCluster().getMaster();
143 String clusterId
= activeMaster
.getClusterId();
144 // Add a non-working master
145 ServerName badServer
= ServerName
.valueOf("localhost", 1234, -1);
146 conf
.set(HConstants
.MASTER_ADDRS_KEY
, badServer
.toShortString() + "," + currentMasterAddrs
);
147 // Set the hedging fan out so that all masters are queried.
148 conf
.setInt(MasterRegistry
.MASTER_REGISTRY_HEDGED_REQS_FANOUT_KEY
, 4);
149 // Do not limit the number of refreshes during the test run.
150 conf
.setLong(MasterRegistry
.MASTER_REGISTRY_MIN_SECS_BETWEEN_REFRESHES
, 0);
151 try (MasterRegistry registry
= new MasterRegistry(conf
)) {
152 final Set
<ServerName
> masters
= registry
.getParsedServers();
153 assertTrue(masters
.contains(badServer
));
154 // Make a registry RPC, this should trigger a refresh since one of the hedged RPC fails.
155 assertEquals(registry
.getClusterId().get(), clusterId
);
156 // Wait for new set of masters to be populated.
157 TEST_UTIL
.waitFor(5000,
158 (Waiter
.Predicate
<Exception
>) () -> !registry
.getParsedServers().equals(masters
));
159 // new set of masters should not include the bad server
160 final Set
<ServerName
> newMasters
= registry
.getParsedServers();
161 // Bad one should be out.
162 assertEquals(3, newMasters
.size());
163 assertFalse(newMasters
.contains(badServer
));
164 // Kill the active master
165 activeMaster
.stopMaster();
166 TEST_UTIL
.waitFor(10000,
167 () -> TEST_UTIL
.getMiniHBaseCluster().getLiveMasterThreads().size() == 2);
168 TEST_UTIL
.getMiniHBaseCluster().waitForActiveAndReadyMaster(10000);
169 // Wait until the killed master de-registered. This should also trigger another refresh.
170 TEST_UTIL
.waitFor(10000, () -> registry
.getMasters().get().size() == 2);
171 TEST_UTIL
.waitFor(20000, () -> registry
.getParsedServers().size() == 2);
172 final Set
<ServerName
> newMasters2
= registry
.getParsedServers();
173 assertEquals(2, newMasters2
.size());
174 assertFalse(newMasters2
.contains(activeMaster
.getServerName()));
176 // Reset the state, add a killed master.
177 TEST_UTIL
.getMiniHBaseCluster().startMaster();