View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Netty client for the requests and responses.
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        // Use our own EventLoopGroup.
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    /** Used in test only. */
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  }