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 org.apache.hbase.thirdparty.io.netty.buffer.ByteBuf; 021import org.apache.hbase.thirdparty.io.netty.channel.ChannelHandlerContext; 022import org.apache.hbase.thirdparty.io.netty.channel.ChannelInboundHandlerAdapter; 023import org.apache.hbase.thirdparty.io.netty.channel.group.ChannelGroup; 024 025import org.apache.yetus.audience.InterfaceAudience; 026 027/** 028 * Decoder for rpc request. 029 * @since 2.0.0 030 */ 031@InterfaceAudience.Private 032class NettyRpcServerRequestDecoder extends ChannelInboundHandlerAdapter { 033 034 private final ChannelGroup allChannels; 035 036 private final MetricsHBaseServer metrics; 037 038 public NettyRpcServerRequestDecoder(ChannelGroup allChannels, MetricsHBaseServer metrics) { 039 this.allChannels = allChannels; 040 this.metrics = metrics; 041 } 042 043 private NettyServerRpcConnection connection; 044 045 void setConnection(NettyServerRpcConnection connection) { 046 this.connection = connection; 047 } 048 049 @Override 050 public void channelActive(ChannelHandlerContext ctx) throws Exception { 051 allChannels.add(ctx.channel()); 052 NettyRpcServer.LOG.trace("Connection {}; # active connections={}", 053 ctx.channel().remoteAddress(), (allChannels.size() - 1)); 054 super.channelActive(ctx); 055 } 056 057 @Override 058 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 059 ByteBuf input = (ByteBuf) msg; 060 // 4 bytes length field 061 metrics.receivedBytes(input.readableBytes() + 4); 062 connection.process(input); 063 } 064 065 @Override 066 public void channelInactive(ChannelHandlerContext ctx) throws Exception { 067 allChannels.remove(ctx.channel()); 068 NettyRpcServer.LOG.trace("Disconnection {}; # active connections={}", 069 ctx.channel().remoteAddress(), (allChannels.size() - 1)); 070 super.channelInactive(ctx); 071 } 072 073 @Override 074 public void exceptionCaught(ChannelHandlerContext ctx, Throwable e) { 075 allChannels.remove(ctx.channel()); 076 NettyRpcServer.LOG.trace("Connection {}; caught unexpected downstream exception.", 077 ctx.channel().remoteAddress(), e.getCause()); 078 ctx.channel().close(); 079 } 080}