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.buffer.ByteBuf;
21 import io.netty.buffer.ByteBufInputStream;
22 import io.netty.channel.ChannelHandlerContext;
23 import io.netty.channel.ChannelInboundHandlerAdapter;
24
25 import java.io.IOException;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.hbase.CellScanner;
30 import org.apache.hadoop.hbase.classification.InterfaceAudience;
31 import org.apache.hadoop.hbase.protobuf.generated.RPCProtos;
32 import org.apache.hadoop.ipc.RemoteException;
33
34 import com.google.protobuf.Message;
35
36
37
38
39 @InterfaceAudience.Private
40 public class AsyncServerResponseHandler extends ChannelInboundHandlerAdapter {
41 public static final Log LOG = LogFactory.getLog(AsyncServerResponseHandler.class.getName());
42
43 private final AsyncRpcChannel channel;
44
45
46
47
48
49
50 public AsyncServerResponseHandler(AsyncRpcChannel channel) {
51 this.channel = channel;
52 }
53
54 @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
55 ByteBuf inBuffer = (ByteBuf) msg;
56 ByteBufInputStream in = new ByteBufInputStream(inBuffer);
57 int totalSize = inBuffer.readableBytes();
58 try {
59
60 RPCProtos.ResponseHeader responseHeader = RPCProtos.ResponseHeader.parseDelimitedFrom(in);
61 int id = responseHeader.getCallId();
62 AsyncCall call = channel.removePendingCall(id);
63 if (call == null) {
64
65
66
67
68
69 int readSoFar = IPCUtil.getTotalSizeWhenWrittenDelimited(responseHeader);
70 int whatIsLeftToRead = totalSize - readSoFar;
71
72
73
74
75 inBuffer.skipBytes(whatIsLeftToRead);
76 return;
77 }
78
79 if (responseHeader.hasException()) {
80 RPCProtos.ExceptionResponse exceptionResponse = responseHeader.getException();
81 RemoteException re = createRemoteException(exceptionResponse);
82 if (exceptionResponse.getExceptionClassName().
83 equals(FatalConnectionException.class.getName())) {
84 channel.close(re);
85 } else {
86 call.setFailed(re);
87 }
88 } else {
89 Message value = null;
90
91 if (call.responseDefaultType != null) {
92 Message.Builder builder = call.responseDefaultType.newBuilderForType();
93 builder.mergeDelimitedFrom(in);
94 value = builder.build();
95 }
96 CellScanner cellBlockScanner = null;
97 if (responseHeader.hasCellBlockMeta()) {
98 int size = responseHeader.getCellBlockMeta().getLength();
99 byte[] cellBlock = new byte[size];
100 inBuffer.readBytes(cellBlock, 0, cellBlock.length);
101 cellBlockScanner = channel.client.createCellScanner(cellBlock);
102 }
103 call.setSuccess(value, cellBlockScanner);
104 }
105 } catch (IOException e) {
106
107 channel.close(e);
108 } finally {
109 inBuffer.release();
110 }
111 }
112
113
114
115
116
117 private RemoteException createRemoteException(final RPCProtos.ExceptionResponse e) {
118 String innerExceptionClassName = e.getExceptionClassName();
119 boolean doNotRetry = e.getDoNotRetry();
120 return e.hasHostname() ?
121
122 new RemoteWithExtrasException(innerExceptionClassName, e.getStackTrace(), e.getHostname(),
123 e.getPort(), doNotRetry) :
124 new RemoteWithExtrasException(innerExceptionClassName, e.getStackTrace(), doNotRetry);
125 }
126 }