View Javadoc

1   /*
2    *
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
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
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.
18   */
19  package org.apache.hadoop.hbase.master;
20  
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.HBaseTestingUtility;
31  import org.apache.hadoop.hbase.HRegionLocation;
32  import org.apache.hadoop.hbase.ServerName;
33  import org.apache.hadoop.hbase.ipc.HBaseRpcController;
34  import org.apache.hadoop.hbase.ipc.RpcClient;
35  import org.apache.hadoop.hbase.ipc.RpcClientFactory;
36  import org.apache.hadoop.hbase.ipc.RpcControllerFactory;
37  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
38  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
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.Test;
46  import org.junit.experimental.categories.Category;
47  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.ClientMetaService;
48  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.GetClusterIdRequest;
49  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.GetClusterIdResponse;
50  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.GetMetaRegionLocationsRequest;
51  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.GetMetaRegionLocationsResponse;
52  
53  @Category({MediumTests.class, MasterTests.class})
54  public class TestClientMetaServiceRPCs {
55  
56    // Total number of masters (active + stand by) for the purpose of this test.
57    private static final int MASTER_COUNT = 3;
58    private static final int RS_COUNT = 3;
59    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
60    private static Configuration conf;
61    private static int rpcTimeout;
62    private static RpcClient rpcClient;
63  
64    @BeforeClass
65    public static void setUp() throws Exception {
66      // Start the mini cluster with stand-by masters.
67      TEST_UTIL.startMiniCluster(MASTER_COUNT, RS_COUNT);
68      conf = TEST_UTIL.getConfiguration();
69      rpcTimeout = (int) Math.min(Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(
70          conf.getLong(HBASE_RPC_TIMEOUT_KEY, DEFAULT_HBASE_RPC_TIMEOUT)));
71      rpcClient = RpcClientFactory.createClient(conf,
72          TEST_UTIL.getMiniHBaseCluster().getMaster().getClusterId());
73    }
74  
75    @AfterClass
76    public static void tearDown() throws Exception {
77      if (rpcClient != null) {
78        rpcClient.close();
79      }
80      TEST_UTIL.shutdownMiniCluster();
81    }
82  
83    private static ClientMetaService.BlockingInterface getMasterStub(ServerName server)
84        throws IOException {
85      return ClientMetaService.newBlockingStub(
86          rpcClient.createBlockingRpcChannel(server, User.getCurrent(), rpcTimeout));
87    }
88  
89    private static HBaseRpcController getRpcController() {
90      return RpcControllerFactory.instantiate(conf).newController();
91    }
92  
93    /**
94     * Verifies the cluster ID from all running masters.
95     */
96    @Test public void TestClusterID() throws Exception {
97      HBaseRpcController rpcController = getRpcController();
98      String clusterID = TEST_UTIL.getMiniHBaseCluster().getMaster().getClusterId();
99      int rpcCount = 0;
100     for (JVMClusterUtil.MasterThread masterThread:
101         TEST_UTIL.getMiniHBaseCluster().getMasterThreads()) {
102       ClientMetaService.BlockingInterface stub =
103           getMasterStub(masterThread.getMaster().getServerName());
104       GetClusterIdResponse resp =
105           stub.getClusterId(rpcController, GetClusterIdRequest.getDefaultInstance());
106       assertEquals(clusterID, resp.getClusterId());
107       rpcCount++;
108     }
109     assertEquals(MASTER_COUNT, rpcCount);
110   }
111 
112   /**
113    * Verifies that the meta region locations RPC returns consistent results across all masters.
114    */
115   @Test public void TestMetaLocations() throws Exception {
116     HBaseRpcController rpcController = getRpcController();
117     List<HRegionLocation> metaLocations = TEST_UTIL.getMiniHBaseCluster().getMaster()
118         .getMetaRegionLocationCache().getMetaRegionLocations();
119     Collections.sort(metaLocations);
120     int rpcCount = 0;
121     for (JVMClusterUtil.MasterThread masterThread:
122       TEST_UTIL.getMiniHBaseCluster().getMasterThreads()) {
123       ClientMetaService.BlockingInterface stub =
124           getMasterStub(masterThread.getMaster().getServerName());
125       GetMetaRegionLocationsResponse resp = stub.getMetaRegionLocations(
126           rpcController, GetMetaRegionLocationsRequest.getDefaultInstance());
127       List<HRegionLocation> result = new ArrayList<>();
128       for (HBaseProtos.RegionLocation location: resp.getMetaLocationsList()) {
129         result.add(ProtobufUtil.toRegionLocation(location));
130       }
131       Collections.sort(result);
132       assertEquals(metaLocations, result);
133       rpcCount++;
134     }
135     assertEquals(MASTER_COUNT, rpcCount);
136   }
137 }