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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org
.apache
.hadoop
.hbase
.ipc
;
20 import org
.apache
.hbase
.thirdparty
.io
.netty
.buffer
.ByteBuf
;
21 import org
.apache
.hbase
.thirdparty
.io
.netty
.channel
.ChannelHandlerContext
;
22 import org
.apache
.hbase
.thirdparty
.io
.netty
.channel
.ChannelInboundHandlerAdapter
;
23 import org
.apache
.hbase
.thirdparty
.io
.netty
.channel
.group
.ChannelGroup
;
25 import org
.apache
.yetus
.audience
.InterfaceAudience
;
28 * Decoder for rpc request.
31 @InterfaceAudience.Private
32 class NettyRpcServerRequestDecoder
extends ChannelInboundHandlerAdapter
{
34 private final ChannelGroup allChannels
;
36 private final MetricsHBaseServer metrics
;
38 public NettyRpcServerRequestDecoder(ChannelGroup allChannels
, MetricsHBaseServer metrics
) {
39 this.allChannels
= allChannels
;
40 this.metrics
= metrics
;
43 private NettyServerRpcConnection connection
;
45 void setConnection(NettyServerRpcConnection connection
) {
46 this.connection
= connection
;
50 public void channelActive(ChannelHandlerContext ctx
) throws Exception
{
51 allChannels
.add(ctx
.channel());
52 NettyRpcServer
.LOG
.trace("Connection {}; # active connections={}",
53 ctx
.channel().remoteAddress(), (allChannels
.size() - 1));
54 super.channelActive(ctx
);
58 public void channelRead(ChannelHandlerContext ctx
, Object msg
) throws Exception
{
59 ByteBuf input
= (ByteBuf
) msg
;
60 // 4 bytes length field
61 metrics
.receivedBytes(input
.readableBytes() + 4);
62 connection
.process(input
);
66 public void channelInactive(ChannelHandlerContext ctx
) throws Exception
{
67 allChannels
.remove(ctx
.channel());
68 NettyRpcServer
.LOG
.trace("Disconnection {}; # active connections={}",
69 ctx
.channel().remoteAddress(), (allChannels
.size() - 1));
70 super.channelInactive(ctx
);
74 public void exceptionCaught(ChannelHandlerContext ctx
, Throwable e
) {
75 allChannels
.remove(ctx
.channel());
76 NettyRpcServer
.LOG
.trace("Connection {}; caught unexpected downstream exception.",
77 ctx
.channel().remoteAddress(), e
);
78 ctx
.channel().close();