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.ipc; 019 020import static org.apache.hadoop.hbase.HBaseTestingUtil.fam1; 021import static org.junit.jupiter.api.Assertions.assertTrue; 022import static org.junit.jupiter.api.Assertions.fail; 023 024import java.io.IOException; 025import java.net.Socket; 026import java.net.SocketAddress; 027import java.util.Map; 028import java.util.concurrent.BlockingQueue; 029import java.util.concurrent.LinkedBlockingQueue; 030import org.apache.hadoop.conf.Configuration; 031import org.apache.hadoop.hbase.HBaseTestingUtil; 032import org.apache.hadoop.hbase.HConstants; 033import org.apache.hadoop.hbase.TableName; 034import org.apache.hadoop.hbase.client.Connection; 035import org.apache.hadoop.hbase.client.ConnectionFactory; 036import org.apache.hadoop.hbase.client.Get; 037import org.apache.hadoop.hbase.client.MetricsConnection; 038import org.apache.hadoop.hbase.client.RetriesExhaustedException; 039import org.apache.hadoop.hbase.client.Table; 040import org.apache.hadoop.hbase.testclassification.MediumTests; 041import org.apache.hadoop.hbase.util.Bytes; 042import org.junit.jupiter.api.AfterAll; 043import org.junit.jupiter.api.BeforeAll; 044import org.junit.jupiter.api.BeforeEach; 045import org.junit.jupiter.api.Tag; 046import org.junit.jupiter.api.Test; 047import org.junit.jupiter.api.TestInfo; 048import org.slf4j.Logger; 049import org.slf4j.LoggerFactory; 050 051@Tag(MediumTests.TAG) 052public class TestRpcClientLeaks { 053 054 private static BlockingQueue<Socket> SAVED_SOCKETS = new LinkedBlockingQueue<>(); 055 056 public static class MyRpcClientImpl extends BlockingRpcClient { 057 058 // Exceptions thrown only when this is set to false. 059 private static boolean throwException = false; 060 061 public MyRpcClientImpl(Configuration conf) { 062 super(conf); 063 } 064 065 public MyRpcClientImpl(Configuration conf, String clusterId, SocketAddress address, 066 MetricsConnection metrics, Map<String, byte[]> connectionAttributes) { 067 super(conf, clusterId, address, metrics, connectionAttributes); 068 } 069 070 @Override 071 protected BlockingRpcConnection createConnection(ConnectionId remoteId) throws IOException { 072 return new BlockingRpcConnection(this, remoteId) { 073 @Override 074 protected synchronized void setupConnection() throws IOException { 075 super.setupConnection(); 076 if (throwException) { 077 SAVED_SOCKETS.add(socket); 078 throw new IOException( 079 "Sample exception for verifying socket closure in case of exceptions."); 080 } 081 } 082 }; 083 } 084 085 public static void enableThrowExceptions() { 086 throwException = true; 087 } 088 } 089 090 private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); 091 private String testMethodName; 092 093 @BeforeAll 094 public static void setup() throws Exception { 095 UTIL.startMiniCluster(); 096 } 097 098 @AfterAll 099 public static void teardown() throws Exception { 100 UTIL.shutdownMiniCluster(); 101 } 102 103 public static final Logger LOG = LoggerFactory.getLogger(TestRpcClientLeaks.class); 104 105 @BeforeEach 106 public void setUpTest(TestInfo testInfo) { 107 testMethodName = testInfo.getTestMethod().get().getName(); 108 } 109 110 @Test 111 public void testSocketClosed() throws IOException, InterruptedException { 112 TableName tableName = TableName.valueOf(testMethodName); 113 UTIL.createTable(tableName, fam1).close(); 114 115 Configuration conf = new Configuration(UTIL.getConfiguration()); 116 conf.set(RpcClientFactory.CUSTOM_RPC_CLIENT_IMPL_CONF_KEY, MyRpcClientImpl.class.getName()); 117 conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2); 118 try (Connection connection = ConnectionFactory.createConnection(conf); 119 Table table = connection.getTable(TableName.valueOf(testMethodName))) { 120 MyRpcClientImpl.enableThrowExceptions(); 121 table.get(new Get(Bytes.toBytes("asd"))); 122 fail("Should fail because the injected error"); 123 } catch (RetriesExhaustedException e) { 124 // expected 125 } 126 for (Socket socket : SAVED_SOCKETS) { 127 assertTrue(socket.isClosed(), "Socket " + socket + " is not closed"); 128 } 129 } 130}