1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.giraph.comm.netty.handler; |
20 | |
|
21 | |
import org.apache.giraph.comm.netty.NettyClient; |
22 | |
import org.apache.hadoop.conf.Configuration; |
23 | |
import org.apache.log4j.Logger; |
24 | |
|
25 | |
import io.netty.buffer.ByteBuf; |
26 | |
import io.netty.channel.ChannelHandlerContext; |
27 | |
import io.netty.channel.ChannelInboundHandlerAdapter; |
28 | |
import io.netty.util.ReferenceCountUtil; |
29 | |
|
30 | |
import static org.apache.giraph.conf.GiraphConstants.NETTY_SIMULATE_FIRST_RESPONSE_FAILED; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
public class ResponseClientHandler extends ChannelInboundHandlerAdapter { |
36 | |
|
37 | 0 | private static final Logger LOG = |
38 | 0 | Logger.getLogger(ResponseClientHandler.class); |
39 | |
|
40 | 0 | private static volatile boolean ALREADY_DROPPED_FIRST_RESPONSE = false; |
41 | |
|
42 | |
private final boolean dropFirstResponse; |
43 | |
|
44 | |
private final NettyClient nettyClient; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | 0 | public ResponseClientHandler(NettyClient nettyClient, Configuration conf) { |
52 | 0 | this.nettyClient = nettyClient; |
53 | 0 | dropFirstResponse = NETTY_SIMULATE_FIRST_RESPONSE_FAILED.get(conf); |
54 | 0 | } |
55 | |
|
56 | |
@Override |
57 | |
public void channelRead(ChannelHandlerContext ctx, Object msg) |
58 | |
throws Exception { |
59 | 0 | if (!(msg instanceof ByteBuf)) { |
60 | 0 | throw new IllegalStateException("channelRead: Got a " + |
61 | |
"non-ByteBuf message " + msg); |
62 | |
} |
63 | |
|
64 | 0 | ByteBuf buf = (ByteBuf) msg; |
65 | 0 | int senderId = -1; |
66 | 0 | long requestId = -1; |
67 | 0 | int response = -1; |
68 | |
try { |
69 | 0 | senderId = buf.readInt(); |
70 | 0 | requestId = buf.readLong(); |
71 | 0 | response = buf.readInt(); |
72 | 0 | } catch (IndexOutOfBoundsException e) { |
73 | 0 | throw new IllegalStateException( |
74 | |
"channelRead: Got IndexOutOfBoundsException ", e); |
75 | 0 | } |
76 | 0 | ReferenceCountUtil.release(buf); |
77 | |
|
78 | 0 | boolean shouldDrop = false; |
79 | |
|
80 | 0 | if (dropFirstResponse && !ALREADY_DROPPED_FIRST_RESPONSE) { |
81 | 0 | LOG.info("channelRead: Simulating dropped response " + response + |
82 | |
" for request " + requestId); |
83 | 0 | setAlreadyDroppedFirstResponse(); |
84 | 0 | shouldDrop = true; |
85 | |
} |
86 | |
|
87 | 0 | nettyClient.messageReceived(senderId, requestId, response, shouldDrop); |
88 | 0 | } |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
private static void setAlreadyDroppedFirstResponse() { |
94 | 0 | ALREADY_DROPPED_FIRST_RESPONSE = true; |
95 | 0 | } |
96 | |
|
97 | |
@Override |
98 | |
public void channelInactive(ChannelHandlerContext ctx) throws Exception { |
99 | 0 | if (LOG.isDebugEnabled()) { |
100 | 0 | LOG.debug("channelClosed: Closed the channel on " + |
101 | 0 | ctx.channel().remoteAddress()); |
102 | |
} |
103 | 0 | ctx.fireChannelInactive(); |
104 | 0 | } |
105 | |
|
106 | |
@Override |
107 | |
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) |
108 | |
throws Exception { |
109 | 0 | LOG.warn("exceptionCaught: Channel channelId=" + |
110 | 0 | ctx.channel().hashCode() + " failed with remote address " + |
111 | 0 | ctx.channel().remoteAddress(), cause); |
112 | 0 | } |
113 | |
} |
114 | |
|