Coverage Report - org.apache.giraph.comm.netty.handler.RequestDecoder
 
Classes in this File Line Coverage Branch Coverage Complexity
RequestDecoder
0%
0/32
0%
0/10
4
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 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  
  * Decodes encoded requests from the client.
 39  
  */
 40  
 public class RequestDecoder extends ChannelInboundHandlerAdapter {
 41  
   /** Class logger */
 42  0
   private static final Logger LOG =
 43  0
       Logger.getLogger(RequestDecoder.class);
 44  
   /** Time class to use */
 45  0
   private static final Time TIME = SystemTime.get();
 46  
   /** Configuration */
 47  
   private final ImmutableClassesGiraphConfiguration conf;
 48  
   /** In bound byte counter to output */
 49  
   private final InboundByteCounter byteCounter;
 50  
   /** Start nanoseconds for the decoding time */
 51  0
   private long startDecodingNanoseconds = -1;
 52  
   /**
 53  
    * Constructor.
 54  
    *
 55  
    * @param conf Configuration
 56  
    * @param byteCounter Keeps track of the decoded bytes
 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  
     // Output metrics every 1/2 minute
 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  
     // Decode the request
 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  
     // fire writableRequest object to upstream handlers
 100  0
     ctx.fireChannelRead(request);
 101  0
   }
 102  
 }