HBASE-26921 Rewrite the counting cells part in TestMultiVersions (#4316)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / master / TestClientMetaServiceRPCs.java
blobb2e204cc1e3182685e7de818d6a594108316624a
1 /*
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;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.concurrent.TimeUnit;
30 import org.apache.hadoop.conf.Configuration;
31 import org.apache.hadoop.hbase.HBaseClassTestRule;
32 import org.apache.hadoop.hbase.HBaseTestingUtil;
33 import org.apache.hadoop.hbase.HRegionLocation;
34 import org.apache.hadoop.hbase.ServerName;
35 import org.apache.hadoop.hbase.StartTestingClusterOption;
36 import org.apache.hadoop.hbase.ipc.HBaseRpcController;
37 import org.apache.hadoop.hbase.ipc.RpcClient;
38 import org.apache.hadoop.hbase.ipc.RpcClientFactory;
39 import org.apache.hadoop.hbase.ipc.RpcControllerFactory;
40 import org.apache.hadoop.hbase.security.User;
41 import org.apache.hadoop.hbase.testclassification.MasterTests;
42 import org.apache.hadoop.hbase.testclassification.MediumTests;
43 import org.apache.hadoop.hbase.util.JVMClusterUtil;
44 import org.junit.AfterClass;
45 import org.junit.BeforeClass;
46 import org.junit.ClassRule;
47 import org.junit.Test;
48 import org.junit.experimental.categories.Category;
50 import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
51 import org.apache.hadoop.hbase.shaded.protobuf.generated.RegistryProtos.ClientMetaService;
52 import org.apache.hadoop.hbase.shaded.protobuf.generated.RegistryProtos.GetActiveMasterRequest;
53 import org.apache.hadoop.hbase.shaded.protobuf.generated.RegistryProtos.GetActiveMasterResponse;
54 import org.apache.hadoop.hbase.shaded.protobuf.generated.RegistryProtos.GetClusterIdRequest;
55 import org.apache.hadoop.hbase.shaded.protobuf.generated.RegistryProtos.GetClusterIdResponse;
56 import org.apache.hadoop.hbase.shaded.protobuf.generated.RegistryProtos.GetMetaRegionLocationsRequest;
57 import org.apache.hadoop.hbase.shaded.protobuf.generated.RegistryProtos.GetMetaRegionLocationsResponse;
59 @Category({MediumTests.class, MasterTests.class})
60 public class TestClientMetaServiceRPCs {
62 @ClassRule
63 public static final HBaseClassTestRule CLASS_RULE =
64 HBaseClassTestRule.forClass(TestClientMetaServiceRPCs.class);
66 // Total number of masters (active + stand by) for the purpose of this test.
67 private static final int MASTER_COUNT = 3;
68 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
69 private static Configuration conf;
70 private static int rpcTimeout;
71 private static RpcClient rpcClient;
73 @BeforeClass
74 public static void setUp() throws Exception {
75 // Start the mini cluster with stand-by masters.
76 StartTestingClusterOption.Builder builder = StartTestingClusterOption.builder();
77 builder.numMasters(MASTER_COUNT).numRegionServers(3);
78 TEST_UTIL.startMiniCluster(builder.build());
79 conf = TEST_UTIL.getConfiguration();
80 rpcTimeout = (int) Math.min(Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(
81 conf.getLong(HBASE_RPC_TIMEOUT_KEY, DEFAULT_HBASE_RPC_TIMEOUT)));
82 rpcClient = RpcClientFactory.createClient(conf,
83 TEST_UTIL.getMiniHBaseCluster().getMaster().getClusterId());
86 @AfterClass
87 public static void tearDown() throws Exception {
88 if (rpcClient != null) {
89 rpcClient.close();
91 TEST_UTIL.shutdownMiniCluster();
94 private static ClientMetaService.BlockingInterface getMasterStub(ServerName server)
95 throws IOException {
96 return ClientMetaService.newBlockingStub(
97 rpcClient.createBlockingRpcChannel(server, User.getCurrent(), rpcTimeout));
100 private static HBaseRpcController getRpcController() {
101 return RpcControllerFactory.instantiate(conf).newController();
105 * Verifies the cluster ID from all running masters.
107 @Test public void TestClusterID() throws Exception {
108 HBaseRpcController rpcController = getRpcController();
109 String clusterID = TEST_UTIL.getMiniHBaseCluster().getMaster().getClusterId();
110 int rpcCount = 0;
111 for (JVMClusterUtil.MasterThread masterThread:
112 TEST_UTIL.getMiniHBaseCluster().getMasterThreads()) {
113 ClientMetaService.BlockingInterface stub =
114 getMasterStub(masterThread.getMaster().getServerName());
115 GetClusterIdResponse resp =
116 stub.getClusterId(rpcController, GetClusterIdRequest.getDefaultInstance());
117 assertEquals(clusterID, resp.getClusterId());
118 rpcCount++;
120 assertEquals(MASTER_COUNT, rpcCount);
124 * Verifies the active master ServerName as seen by all masters.
126 @Test public void TestActiveMaster() throws Exception {
127 HBaseRpcController rpcController = getRpcController();
128 ServerName activeMaster = TEST_UTIL.getMiniHBaseCluster().getMaster().getServerName();
129 int rpcCount = 0;
130 for (JVMClusterUtil.MasterThread masterThread:
131 TEST_UTIL.getMiniHBaseCluster().getMasterThreads()) {
132 ClientMetaService.BlockingInterface stub =
133 getMasterStub(masterThread.getMaster().getServerName());
134 GetActiveMasterResponse resp =
135 stub.getActiveMaster(rpcController, GetActiveMasterRequest.getDefaultInstance());
136 assertEquals(activeMaster, ProtobufUtil.toServerName(resp.getServerName()));
137 rpcCount++;
139 assertEquals(MASTER_COUNT, rpcCount);
143 * Verifies that the meta region locations RPC returns consistent results across all masters.
145 @Test public void TestMetaLocations() throws Exception {
146 HBaseRpcController rpcController = getRpcController();
147 List<HRegionLocation> metaLocations =
148 TEST_UTIL.getMiniHBaseCluster().getMaster().getMetaLocations();
149 Collections.sort(metaLocations);
150 int rpcCount = 0;
151 for (JVMClusterUtil.MasterThread masterThread:
152 TEST_UTIL.getMiniHBaseCluster().getMasterThreads()) {
153 ClientMetaService.BlockingInterface stub =
154 getMasterStub(masterThread.getMaster().getServerName());
155 GetMetaRegionLocationsResponse resp = stub.getMetaRegionLocations(
156 rpcController, GetMetaRegionLocationsRequest.getDefaultInstance());
157 List<HRegionLocation> result = new ArrayList<>();
158 resp.getMetaLocationsList().forEach(
159 location -> result.add(ProtobufUtil.toRegionLocation(location)));
160 Collections.sort(result);
161 assertEquals(metaLocations, result);
162 rpcCount++;
164 assertEquals(MASTER_COUNT, rpcCount);