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.HBaseTestingUtility.fam1;
021import static org.junit.Assert.assertTrue;
022import static org.junit.Assert.fail;
023
024import java.io.IOException;
025import java.net.Socket;
026import java.net.SocketAddress;
027import java.util.concurrent.BlockingQueue;
028import java.util.concurrent.LinkedBlockingQueue;
029import org.apache.hadoop.conf.Configuration;
030import org.apache.hadoop.hbase.HBaseClassTestRule;
031import org.apache.hadoop.hbase.HBaseTestingUtility;
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.AfterClass;
043import org.junit.BeforeClass;
044import org.junit.ClassRule;
045import org.junit.Rule;
046import org.junit.Test;
047import org.junit.experimental.categories.Category;
048import org.junit.rules.TestName;
049import org.slf4j.Logger;
050import org.slf4j.LoggerFactory;
051
052@Category(MediumTests.class)
053public class TestRpcClientLeaks {
054
055  @ClassRule
056  public static final HBaseClassTestRule CLASS_RULE =
057    HBaseClassTestRule.forClass(TestRpcClientLeaks.class);
058
059  @Rule
060  public TestName name = new TestName();
061
062  private static BlockingQueue<Socket> SAVED_SOCKETS = new LinkedBlockingQueue<>();
063
064  public static class MyRpcClientImpl extends BlockingRpcClient {
065
066    public MyRpcClientImpl(Configuration conf) {
067      super(conf);
068    }
069
070    public MyRpcClientImpl(Configuration conf, String clusterId, SocketAddress address,
071        MetricsConnection metrics) {
072      super(conf, clusterId, address, metrics);
073    }
074
075    @Override
076    protected BlockingRpcConnection createConnection(ConnectionId remoteId) throws IOException {
077      return new BlockingRpcConnection(this, remoteId) {
078        @Override
079        protected synchronized void setupConnection() throws IOException {
080          super.setupConnection();
081          SAVED_SOCKETS.add(socket);
082          throw new IOException(
083            "Sample exception for verifying socket closure in case of exceptions.");
084        }
085      };
086    }
087  }
088
089  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
090
091  @BeforeClass
092  public static void setup() throws Exception {
093    UTIL.startMiniCluster();
094  }
095
096  @AfterClass
097  public static void teardown() throws Exception {
098    UTIL.shutdownMiniCluster();
099  }
100
101  public static final Logger LOG = LoggerFactory.getLogger(TestRpcClientLeaks.class);
102
103  @Test
104  public void testSocketClosed() throws IOException, InterruptedException {
105    TableName tableName = TableName.valueOf(name.getMethodName());
106    UTIL.createTable(tableName, fam1).close();
107
108    Configuration conf = new Configuration(UTIL.getConfiguration());
109    conf.set(RpcClientFactory.CUSTOM_RPC_CLIENT_IMPL_CONF_KEY, MyRpcClientImpl.class.getName());
110    conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);
111    try (Connection connection = ConnectionFactory.createConnection(conf);
112      Table table = connection.getTable(TableName.valueOf(name.getMethodName()))) {
113      table.get(new Get(Bytes.toBytes("asd")));
114      fail("Should fail because the injected error");
115    } catch (RetriesExhaustedException e) {
116      // expected
117    }
118    for (Socket socket : SAVED_SOCKETS) {
119      assertTrue("Socket " + socket + " is not closed", socket.isClosed());
120    }
121  }
122}