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.client;
20  
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.io.IOException;
25  
26  import org.apache.hadoop.hbase.HBaseTestingUtility;
27  import org.apache.hadoop.hbase.HColumnDescriptor;
28  import org.apache.hadoop.hbase.HTableDescriptor;
29  import org.apache.hadoop.hbase.ServerName;
30  import org.apache.hadoop.hbase.TableName;
31  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.AdminService;
32  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ClientService;
33  import org.apache.hadoop.hbase.regionserver.HRegionServer;
34  import org.apache.hadoop.hbase.regionserver.RSRpcServices;
35  import org.apache.hadoop.hbase.testclassification.MediumTests;
36  import org.apache.hadoop.hbase.util.Bytes;
37  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
38  import org.junit.AfterClass;
39  import org.junit.BeforeClass;
40  import org.junit.Test;
41  import org.junit.experimental.categories.Category;
42  
43  @Category(MediumTests.class)
44  public class TestShortCircuitConnection {
45  
46    private final static HBaseTestingUtility UTIL = new HBaseTestingUtility();
47  
48    @BeforeClass
49    public static void setUpBeforeClass() throws Exception {
50      UTIL.startMiniCluster(1);
51    }
52  
53    @AfterClass
54    public static void tearDownAfterClass() throws Exception {
55      UTIL.shutdownMiniCluster();
56    }
57  
58    @Test
59    @SuppressWarnings("deprecation")
60    public void testShortCircuitConnection() throws IOException, InterruptedException {
61      String tnAsString = "testShortCircuitConnection";
62      TableName tn = TableName.valueOf(tnAsString);
63      HTableDescriptor htd = UTIL.createTableDescriptor(tnAsString);
64      HColumnDescriptor hcd = new HColumnDescriptor(Bytes.toBytes("cf"));
65      htd.addFamily(hcd);
66      UTIL.createTable(htd, null);
67      HRegionServer regionServer = UTIL.getRSForFirstRegionInTable(tn);
68      ClusterConnection connection = regionServer.getConnection();
69      HTableInterface tableIf = connection.getTable(tn);
70      assertTrue(tableIf instanceof HTable);
71      HTable table = (HTable) tableIf;
72      assertTrue(table.getConnection() == connection);
73      AdminService.BlockingInterface admin = connection.getAdmin(regionServer.getServerName());
74      ClientService.BlockingInterface client = connection.getClient(regionServer.getServerName());
75      assertTrue(admin instanceof RSRpcServices);
76      assertTrue(client instanceof RSRpcServices);
77      ServerName anotherSn = ServerName.valueOf(regionServer.getServerName().getHostAndPort(),
78        EnvironmentEdgeManager.currentTime());
79      admin = connection.getAdmin(anotherSn);
80      client = connection.getClient(anotherSn);
81      assertFalse(admin instanceof RSRpcServices);
82      assertFalse(client instanceof RSRpcServices);
83      assertTrue(connection.getAdmin().getConnection() == connection);
84    }
85  }