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.conf.ImmutableClassesGiraphConfiguration; |
22 | |
import org.apache.giraph.comm.netty.InboundByteCounter; |
23 | |
import org.apache.giraph.comm.requests.RequestType; |
24 | |
import org.apache.giraph.comm.requests.WritableRequest; |
25 | |
import org.apache.giraph.time.SystemTime; |
26 | |
import org.apache.giraph.time.Time; |
27 | |
import org.apache.giraph.time.Times; |
28 | |
import org.apache.giraph.utils.ReflectionUtils; |
29 | |
import org.apache.giraph.utils.RequestUtils; |
30 | |
import org.apache.log4j.Logger; |
31 | |
|
32 | |
import io.netty.buffer.ByteBuf; |
33 | |
import io.netty.channel.ChannelHandlerContext; |
34 | |
import io.netty.channel.ChannelInboundHandlerAdapter; |
35 | |
import io.netty.util.ReferenceCountUtil; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public class RequestDecoder extends ChannelInboundHandlerAdapter { |
41 | |
|
42 | 0 | private static final Logger LOG = |
43 | 0 | Logger.getLogger(RequestDecoder.class); |
44 | |
|
45 | 0 | private static final Time TIME = SystemTime.get(); |
46 | |
|
47 | |
private final ImmutableClassesGiraphConfiguration conf; |
48 | |
|
49 | |
private final InboundByteCounter byteCounter; |
50 | |
|
51 | 0 | private long startDecodingNanoseconds = -1; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public RequestDecoder(ImmutableClassesGiraphConfiguration conf, |
59 | 0 | InboundByteCounter byteCounter) { |
60 | 0 | this.conf = conf; |
61 | 0 | this.byteCounter = byteCounter; |
62 | 0 | } |
63 | |
|
64 | |
@Override |
65 | |
public void channelRead(ChannelHandlerContext ctx, Object msg) |
66 | |
throws Exception { |
67 | 0 | if (!(msg instanceof ByteBuf)) { |
68 | 0 | throw new IllegalStateException("decode: Got illegal message " + msg); |
69 | |
} |
70 | |
|
71 | 0 | String metrics = byteCounter.getMetricsWindow(30000); |
72 | 0 | if (metrics != null) { |
73 | 0 | if (LOG.isInfoEnabled()) { |
74 | 0 | LOG.info("decode: Server window metrics " + metrics); |
75 | |
} |
76 | |
} |
77 | |
|
78 | 0 | if (LOG.isDebugEnabled()) { |
79 | 0 | startDecodingNanoseconds = TIME.getNanoseconds(); |
80 | |
} |
81 | |
|
82 | |
|
83 | 0 | ByteBuf buf = (ByteBuf) msg; |
84 | 0 | int enumValue = buf.readByte(); |
85 | 0 | RequestType type = RequestType.values()[enumValue]; |
86 | 0 | Class<? extends WritableRequest> requestClass = type.getRequestClass(); |
87 | 0 | WritableRequest request = |
88 | 0 | ReflectionUtils.newInstance(requestClass, conf); |
89 | 0 | request = RequestUtils.decodeWritableRequest(buf, request); |
90 | |
|
91 | 0 | if (LOG.isDebugEnabled()) { |
92 | 0 | LOG.debug("decode: Client " + request.getClientId() + |
93 | 0 | ", requestId " + request.getRequestId() + |
94 | 0 | ", " + request.getType() + ", with size " + |
95 | 0 | buf.writerIndex() + " took " + |
96 | 0 | Times.getNanosSince(TIME, startDecodingNanoseconds) + " ns"); |
97 | |
} |
98 | 0 | ReferenceCountUtil.release(buf); |
99 | |
|
100 | 0 | ctx.fireChannelRead(request); |
101 | 0 | } |
102 | |
} |