1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.ipc;
19
20 import java.io.IOException;
21 import java.net.Socket;
22 import java.net.SocketAddress;
23 import java.util.List;
24
25 import com.google.common.collect.Lists;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.conf.Configuration;
29 import org.apache.hadoop.hbase.HBaseTestingUtility;
30 import org.apache.hadoop.hbase.HConstants;
31 import org.apache.hadoop.hbase.TableName;
32 import org.apache.hadoop.hbase.client.Connection;
33 import org.apache.hadoop.hbase.client.ConnectionFactory;
34 import org.apache.hadoop.hbase.client.Get;
35 import org.apache.hadoop.hbase.client.RetriesExhaustedException;
36 import org.apache.hadoop.hbase.client.Table;
37 import org.apache.hadoop.hbase.codec.Codec;
38 import org.apache.hadoop.hbase.testclassification.SmallTests;
39 import org.apache.hadoop.io.compress.CompressionCodec;
40 import org.junit.AfterClass;
41 import org.junit.BeforeClass;
42 import org.junit.Rule;
43 import org.junit.Test;
44 import org.junit.experimental.categories.Category;
45 import org.junit.rules.ExpectedException;
46
47 import static org.apache.hadoop.hbase.HBaseTestingUtility.fam1;
48 import static org.junit.Assert.*;
49
50 @Category(SmallTests.class)
51 public class TestRpcClientLeaks {
52
53 public static class MyRpcClientImpl extends RpcClientImpl {
54 public static List<Socket> savedSockets = Lists.newArrayList();
55 @Rule public ExpectedException thrown = ExpectedException.none();
56
57 public MyRpcClientImpl(Configuration conf, String clusterId) {
58 super(conf, clusterId);
59 }
60
61 public MyRpcClientImpl(Configuration conf, String clusterId, SocketAddress address) {
62 super(conf, clusterId, address);
63 }
64
65 @Override
66 protected Connection createConnection(ConnectionId remoteId, Codec codec,
67 CompressionCodec compressor) throws IOException {
68 return new Connection(remoteId, codec, compressor) {
69 @Override
70 protected synchronized void setupConnection() throws IOException {
71 super.setupConnection();
72 synchronized (savedSockets) {
73 savedSockets.add(socket);
74 }
75 throw new IOException("Sample exception for " +
76 "verifying socket closure in case of exceptions.");
77 }
78 };
79 }
80 }
81
82 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
83
84 @BeforeClass
85 public static void setup() throws Exception {
86 UTIL.startMiniCluster();
87 }
88
89 @AfterClass
90 public static void teardown() throws Exception {
91 UTIL.shutdownMiniCluster();
92 }
93
94 public static final Log LOG = LogFactory.getLog(TestRpcClientLeaks.class);
95
96 @Test(expected=RetriesExhaustedException.class)
97 public void testSocketClosed() throws IOException, InterruptedException {
98 String tableName = "testSocketClosed";
99 TableName name = TableName.valueOf(tableName);
100 UTIL.createTable(name, fam1).close();
101
102 Configuration conf = new Configuration(UTIL.getConfiguration());
103 conf.set(RpcClientFactory.CUSTOM_RPC_CLIENT_IMPL_CONF_KEY,
104 MyRpcClientImpl.class.getName());
105 conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);
106 Connection connection = ConnectionFactory.createConnection(conf);
107 Table table = connection.getTable(TableName.valueOf(tableName));
108 table.get(new Get("asd".getBytes()));
109 connection.close();
110 for (Socket socket : MyRpcClientImpl.savedSockets) {
111 assertTrue("Socket + " + socket + " is not closed", socket.isClosed());
112 }
113 }
114 }
115