001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.client; 019 020import static org.junit.Assert.assertFalse; 021import static org.junit.Assert.assertTrue; 022 023import java.io.IOException; 024import org.apache.hadoop.hbase.HBaseClassTestRule; 025import org.apache.hadoop.hbase.HBaseTestingUtility; 026import org.apache.hadoop.hbase.HColumnDescriptor; 027import org.apache.hadoop.hbase.HTableDescriptor; 028import org.apache.hadoop.hbase.ServerName; 029import org.apache.hadoop.hbase.TableName; 030import org.apache.hadoop.hbase.regionserver.HRegionServer; 031import org.apache.hadoop.hbase.regionserver.RSRpcServices; 032import org.apache.hadoop.hbase.testclassification.ClientTests; 033import org.apache.hadoop.hbase.testclassification.MediumTests; 034import org.apache.hadoop.hbase.util.Bytes; 035import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 036import org.junit.AfterClass; 037import org.junit.BeforeClass; 038import org.junit.ClassRule; 039import org.junit.Rule; 040import org.junit.Test; 041import org.junit.experimental.categories.Category; 042import org.junit.rules.TestName; 043 044import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.AdminService; 045import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ClientService; 046 047@Category({ MediumTests.class, ClientTests.class }) 048public class TestShortCircuitConnection { 049 050 @ClassRule 051 public static final HBaseClassTestRule CLASS_RULE = 052 HBaseClassTestRule.forClass(TestShortCircuitConnection.class); 053 054 private final static HBaseTestingUtility UTIL = new HBaseTestingUtility(); 055 056 @Rule 057 public TestName name = new TestName(); 058 059 @BeforeClass 060 public static void setUpBeforeClass() throws Exception { 061 UTIL.startMiniCluster(1); 062 } 063 064 @AfterClass 065 public static void tearDownAfterClass() throws Exception { 066 UTIL.shutdownMiniCluster(); 067 } 068 069 @Test 070 @SuppressWarnings("deprecation") 071 public void testShortCircuitConnection() throws IOException, InterruptedException { 072 final TableName tableName = TableName.valueOf(name.getMethodName()); 073 HTableDescriptor htd = UTIL.createTableDescriptor(tableName); 074 HColumnDescriptor hcd = new HColumnDescriptor(Bytes.toBytes("cf")); 075 htd.addFamily(hcd); 076 UTIL.createTable(htd, null); 077 HRegionServer regionServer = UTIL.getRSForFirstRegionInTable(tableName); 078 ClusterConnection connection = regionServer.getClusterConnection(); 079 Table tableIf = connection.getTable(tableName); 080 assertTrue(tableIf instanceof HTable); 081 HTable table = (HTable) tableIf; 082 assertTrue(table.getConnection() == connection); 083 AdminService.BlockingInterface admin = connection.getAdmin(regionServer.getServerName()); 084 ClientService.BlockingInterface client = connection.getClient(regionServer.getServerName()); 085 assertTrue(admin instanceof RSRpcServices); 086 assertTrue( 087 client instanceof ServerConnectionUtils.ShortCircuitingClusterConnection.ClientServiceBlockingInterfaceWrapper); 088 ServerName anotherSn = ServerName.valueOf(regionServer.getServerName().getAddress(), 089 EnvironmentEdgeManager.currentTime()); 090 admin = connection.getAdmin(anotherSn); 091 client = connection.getClient(anotherSn); 092 assertFalse(admin instanceof RSRpcServices); 093 assertFalse(client instanceof RSRpcServices); 094 assertTrue(connection.getAdmin().getConnection() == connection); 095 } 096}