3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 package org
.apache
.hadoop
.hbase
.master
;
21 import static org
.apache
.hadoop
.hbase
.HConstants
.DEFAULT_HBASE_RPC_TIMEOUT
;
22 import static org
.apache
.hadoop
.hbase
.HConstants
.HBASE_RPC_TIMEOUT_KEY
;
23 import static org
.junit
.Assert
.assertEquals
;
24 import java
.io
.IOException
;
25 import java
.util
.ArrayList
;
26 import java
.util
.Collections
;
27 import java
.util
.List
;
28 import java
.util
.concurrent
.TimeUnit
;
29 import org
.apache
.hadoop
.conf
.Configuration
;
30 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
31 import org
.apache
.hadoop
.hbase
.HBaseTestingUtility
;
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
.ipc
.HBaseRpcController
;
36 import org
.apache
.hadoop
.hbase
.ipc
.RpcClient
;
37 import org
.apache
.hadoop
.hbase
.ipc
.RpcClientFactory
;
38 import org
.apache
.hadoop
.hbase
.ipc
.RpcControllerFactory
;
39 import org
.apache
.hadoop
.hbase
.security
.User
;
40 import org
.apache
.hadoop
.hbase
.testclassification
.MasterTests
;
41 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
42 import org
.apache
.hadoop
.hbase
.util
.JVMClusterUtil
;
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
;
48 import org
.apache
.hadoop
.hbase
.shaded
.protobuf
.ProtobufUtil
;
49 import org
.apache
.hadoop
.hbase
.shaded
.protobuf
.generated
.MasterProtos
.ClientMetaService
;
50 import org
.apache
.hadoop
.hbase
.shaded
.protobuf
.generated
.MasterProtos
.GetActiveMasterRequest
;
51 import org
.apache
.hadoop
.hbase
.shaded
.protobuf
.generated
.MasterProtos
.GetActiveMasterResponse
;
52 import org
.apache
.hadoop
.hbase
.shaded
.protobuf
.generated
.MasterProtos
.GetClusterIdRequest
;
53 import org
.apache
.hadoop
.hbase
.shaded
.protobuf
.generated
.MasterProtos
.GetClusterIdResponse
;
54 import org
.apache
.hadoop
.hbase
.shaded
.protobuf
.generated
.MasterProtos
.GetMetaRegionLocationsRequest
;
55 import org
.apache
.hadoop
.hbase
.shaded
.protobuf
.generated
.MasterProtos
.GetMetaRegionLocationsResponse
;
57 @Category({MediumTests
.class, MasterTests
.class})
58 public class TestClientMetaServiceRPCs
{
61 public static final HBaseClassTestRule CLASS_RULE
=
62 HBaseClassTestRule
.forClass(TestClientMetaServiceRPCs
.class);
64 // Total number of masters (active + stand by) for the purpose of this test.
65 private static final int MASTER_COUNT
= 3;
66 private static final HBaseTestingUtility TEST_UTIL
= new HBaseTestingUtility();
67 private static Configuration conf
;
68 private static int rpcTimeout
;
69 private static RpcClient rpcClient
;
72 public static void setUp() throws Exception
{
73 // Start the mini cluster with stand-by masters.
74 StartMiniClusterOption
.Builder builder
= StartMiniClusterOption
.builder();
75 builder
.numMasters(MASTER_COUNT
).numRegionServers(3);
76 TEST_UTIL
.startMiniCluster(builder
.build());
77 conf
= TEST_UTIL
.getConfiguration();
78 rpcTimeout
= (int) Math
.min(Integer
.MAX_VALUE
, TimeUnit
.MILLISECONDS
.toNanos(
79 conf
.getLong(HBASE_RPC_TIMEOUT_KEY
, DEFAULT_HBASE_RPC_TIMEOUT
)));
80 rpcClient
= RpcClientFactory
.createClient(conf
,
81 TEST_UTIL
.getMiniHBaseCluster().getMaster().getClusterId());
85 public static void tearDown() throws Exception
{
86 if (rpcClient
!= null) {
89 TEST_UTIL
.shutdownMiniCluster();
92 private static ClientMetaService
.BlockingInterface
getMasterStub(ServerName server
)
94 return ClientMetaService
.newBlockingStub(
95 rpcClient
.createBlockingRpcChannel(server
, User
.getCurrent(), rpcTimeout
));
98 private static HBaseRpcController
getRpcController() {
99 return RpcControllerFactory
.instantiate(conf
).newController();
103 * Verifies the cluster ID from all running masters.
105 @Test public void TestClusterID() throws Exception
{
106 HBaseRpcController rpcController
= getRpcController();
107 String clusterID
= TEST_UTIL
.getMiniHBaseCluster().getMaster().getClusterId();
109 for (JVMClusterUtil
.MasterThread masterThread
:
110 TEST_UTIL
.getMiniHBaseCluster().getMasterThreads()) {
111 ClientMetaService
.BlockingInterface stub
=
112 getMasterStub(masterThread
.getMaster().getServerName());
113 GetClusterIdResponse resp
=
114 stub
.getClusterId(rpcController
, GetClusterIdRequest
.getDefaultInstance());
115 assertEquals(clusterID
, resp
.getClusterId());
118 assertEquals(MASTER_COUNT
, rpcCount
);
122 * Verifies the active master ServerName as seen by all masters.
124 @Test public void TestActiveMaster() throws Exception
{
125 HBaseRpcController rpcController
= getRpcController();
126 ServerName activeMaster
= TEST_UTIL
.getMiniHBaseCluster().getMaster().getServerName();
128 for (JVMClusterUtil
.MasterThread masterThread
:
129 TEST_UTIL
.getMiniHBaseCluster().getMasterThreads()) {
130 ClientMetaService
.BlockingInterface stub
=
131 getMasterStub(masterThread
.getMaster().getServerName());
132 GetActiveMasterResponse resp
=
133 stub
.getActiveMaster(rpcController
, GetActiveMasterRequest
.getDefaultInstance());
134 assertEquals(activeMaster
, ProtobufUtil
.toServerName(resp
.getServerName()));
137 assertEquals(MASTER_COUNT
, rpcCount
);
141 * Verifies that the meta region locations RPC returns consistent results across all masters.
143 @Test public void TestMetaLocations() throws Exception
{
144 HBaseRpcController rpcController
= getRpcController();
145 List
<HRegionLocation
> metaLocations
= TEST_UTIL
.getMiniHBaseCluster().getMaster()
146 .getMetaRegionLocationCache().getMetaRegionLocations().get();
147 Collections
.sort(metaLocations
);
149 for (JVMClusterUtil
.MasterThread masterThread
:
150 TEST_UTIL
.getMiniHBaseCluster().getMasterThreads()) {
151 ClientMetaService
.BlockingInterface stub
=
152 getMasterStub(masterThread
.getMaster().getServerName());
153 GetMetaRegionLocationsResponse resp
= stub
.getMetaRegionLocations(
154 rpcController
, GetMetaRegionLocationsRequest
.getDefaultInstance());
155 List
<HRegionLocation
> result
= new ArrayList
<>();
156 resp
.getMetaLocationsList().forEach(
157 location
-> result
.add(ProtobufUtil
.toRegionLocation(location
)));
158 Collections
.sort(result
);
159 assertEquals(metaLocations
, result
);
162 assertEquals(MASTER_COUNT
, rpcCount
);