This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
/
RequestDecoder.java
103 lines (94 loc) · 3.74 KB
/
RequestDecoder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.giraph.comm.netty.handler;
import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
import org.apache.giraph.comm.netty.ByteCounter;
import org.apache.giraph.comm.requests.RequestType;
import org.apache.giraph.comm.requests.WritableRequest;
import org.apache.giraph.utils.ReflectionUtils;
import org.apache.giraph.utils.SystemTime;
import org.apache.giraph.utils.Time;
import org.apache.giraph.utils.Times;
import org.apache.log4j.Logger;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBufferInputStream;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.oneone.OneToOneDecoder;
/**
* Decodes encoded requests from the client.
*/
public class RequestDecoder extends OneToOneDecoder {
/** Class logger */
private static final Logger LOG =
Logger.getLogger(RequestDecoder.class);
/** Time class to use */
private static final Time TIME = SystemTime.get();
/** Configuration */
private final ImmutableClassesGiraphConfiguration conf;
/** Byte counter to output */
private final ByteCounter byteCounter;
/** Start nanoseconds for the decoding time */
private long startDecodingNanoseconds = -1;
/**
* Constructor.
*
* @param conf Configuration
* @param byteCounter Keeps track of the decoded bytes
*/
public RequestDecoder(ImmutableClassesGiraphConfiguration conf,
ByteCounter byteCounter) {
this.conf = conf;
this.byteCounter = byteCounter;
}
@Override
protected Object decode(ChannelHandlerContext ctx,
Channel channel, Object msg) throws Exception {
if (!(msg instanceof ChannelBuffer)) {
throw new IllegalStateException("decode: Got illegal message " + msg);
}
// Output metrics every 1/2 minute
String metrics = byteCounter.getMetricsWindow(30000);
if (metrics != null) {
if (LOG.isInfoEnabled()) {
LOG.info("decode: Server window metrics " + metrics);
}
}
if (LOG.isDebugEnabled()) {
startDecodingNanoseconds = TIME.getNanoseconds();
}
// Decode the request
ChannelBuffer buffer = (ChannelBuffer) msg;
ChannelBufferInputStream inputStream = new ChannelBufferInputStream(buffer);
int enumValue = inputStream.readByte();
RequestType type = RequestType.values()[enumValue];
Class<? extends WritableRequest> writableRequestClass =
type.getRequestClass();
WritableRequest writableRequest =
ReflectionUtils.newInstance(writableRequestClass, conf);
writableRequest.readFields(inputStream);
if (LOG.isDebugEnabled()) {
LOG.debug("decode: Client " + writableRequest.getClientId() +
", requestId " + writableRequest.getRequestId() +
", " + writableRequest.getType() + ", with size " +
buffer.array().length + " took " +
Times.getNanosSince(TIME, startDecodingNanoseconds) + " ns");
}
return writableRequest;
}
}