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 io.netty.channel.Channel;
21 import io.netty.channel.EventLoopGroup;
22 import io.netty.channel.nio.NioEventLoopGroup;
23 import io.netty.channel.socket.nio.NioSocketChannel;
24 import io.netty.util.concurrent.DefaultThreadFactory;
25
26 import java.io.IOException;
27 import java.net.SocketAddress;
28
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.hbase.HBaseInterfaceAudience;
31 import org.apache.hadoop.hbase.HConstants;
32 import org.apache.hadoop.hbase.classification.InterfaceAudience;
33 import org.apache.hadoop.hbase.client.MetricsConnection;
34 import org.apache.hadoop.hbase.util.Pair;
35
36
37
38
39 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
40 public class NettyRpcClient extends AbstractRpcClient<NettyRpcConnection> {
41
42 final EventLoopGroup group;
43
44 final Class<? extends Channel> channelClass;
45
46 private final boolean shutdownGroupWhenClose;
47
48 public NettyRpcClient(Configuration configuration, String clusterId, SocketAddress localAddress,
49 MetricsConnection metrics) {
50 super(configuration, clusterId, localAddress, metrics);
51 Pair<EventLoopGroup, Class<? extends Channel>> groupAndChannelClass = NettyRpcClientConfigHelper
52 .getEventLoopConfig(conf);
53 if (groupAndChannelClass == null) {
54
55 this.group = new NioEventLoopGroup(0,
56 new DefaultThreadFactory("IPC-NioEventLoopGroup", true, Thread.MAX_PRIORITY));
57 this.channelClass = NioSocketChannel.class;
58 this.shutdownGroupWhenClose = true;
59 } else {
60 this.group = groupAndChannelClass.getFirst();
61 this.channelClass = groupAndChannelClass.getSecond();
62 this.shutdownGroupWhenClose = false;
63 }
64 }
65
66
67 NettyRpcClient(Configuration configuration) {
68 this(configuration, HConstants.CLUSTER_ID_DEFAULT, null, null);
69 }
70
71 @Override
72 protected NettyRpcConnection createConnection(ConnectionId remoteId) throws IOException {
73 return new NettyRpcConnection(this, remoteId);
74 }
75
76 @Override
77 protected void closeInternal() {
78 if (shutdownGroupWhenClose) {
79 group.shutdownGracefully();
80 }
81 }
82 }