1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }