SteNicholas commented on a change in pull request #679: HBASE-23113 IPC Netty 
Optimization
URL: https://github.com/apache/hbase/pull/679#discussion_r331377750
 
 

 ##########
 File path: 
hbase-server/src/main/java/org/apache/hadoop/hbase/ipc/AbstractBatchDecoder.java
 ##########
 @@ -0,0 +1,442 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.hadoop.hbase.ipc;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.hbase.thirdparty.io.netty.buffer.ByteBuf;
+import org.apache.hbase.thirdparty.io.netty.buffer.ByteBufAllocator;
+import org.apache.hbase.thirdparty.io.netty.buffer.CompositeByteBuf;
+import org.apache.hbase.thirdparty.io.netty.buffer.Unpooled;
+import org.apache.hbase.thirdparty.io.netty.channel.ChannelHandlerContext;
+import 
org.apache.hbase.thirdparty.io.netty.channel.ChannelInboundHandlerAdapter;
+import org.apache.hbase.thirdparty.io.netty.handler.codec.ByteToMessageDecoder;
+import org.apache.hbase.thirdparty.io.netty.handler.codec.DecoderException;
+import org.apache.hbase.thirdparty.io.netty.util.internal.RecyclableArrayList;
+import org.apache.hbase.thirdparty.io.netty.util.internal.StringUtil;
+
+/**
+ * This class mainly hack the {@link 
org.apache.hbase.thirdparty.io.netty.handler.codec.ByteToMessageDecoder} to 
provide batch submission capability.
+ * This can be used the same way as ByteToMessageDecoder except the case your 
following inbound handler may get a decoded msg,
+ * which actually is an array list, then you can submit the list of msgs to an 
executor to process. For example
+ * <pre>
+ *   if (msg instanceof List) {
+ *       processorManager.getDefaultExecutor().execute(new Runnable() {
+ *           public void run() {
+ *               // batch submit to an executor
+ *               for (Object m : (List<?>) msg) {
+ *                   RpcCommandHandler.this.process(ctx, m);
+ *               }
+ *           }
+ *       });
+ *   } else {
+ *       process(ctx, msg);
+ *   }
+ * </pre>
+ * You can check the method {@link 
AbstractBatchDecoder#channelRead(ChannelHandlerContext, Object)} ()}
+ *   to know the detail modification.
+ */
+public abstract class AbstractBatchDecoder extends 
ChannelInboundHandlerAdapter {
+    /**
+     * Cumulate {@link ByteBuf}s by merge them into one {@link ByteBuf}'s, 
using memory copies.
+     */
+    public static final Cumulator MERGE_CUMULATOR     = new Cumulator() {
+                                                          @Override
+                                                          public ByteBuf 
cumulate(ByteBufAllocator alloc,
+                                                                               
   ByteBuf cumulation,
+                                                                               
   ByteBuf in) {
+                                                              ByteBuf buffer;
+                                                              if 
(cumulation.writerIndex() > cumulation
+                                                                  
.maxCapacity()
+                                                                               
              - in.readableBytes()
+                                                                  || 
cumulation.refCnt() > 1) {
+                                                                  // Expand 
cumulation (by replace it) when either there is not more room in the buffer
+                                                                  // or if the 
refCnt is greater then 1 which may happen when the user use slice().retain() or
+                                                                  // 
duplicate().retain().
+                                                                  //
+                                                                  // See:
+                                                                  // - 
https://github.com/netty/netty/issues/2327
+                                                                  // - 
https://github.com/netty/netty/issues/1764
+                                                                  buffer = 
expandCumulation(alloc,
+                                                                      
cumulation,
+                                                                      
in.readableBytes());
+                                                              } else {
+                                                                  buffer = 
cumulation;
+                                                              }
+                                                              
buffer.writeBytes(in);
+                                                              in.release();
+                                                              return buffer;
+                                                          }
+                                                      };
+
+    /**
+     * Cumulate {@link ByteBuf}s by add them to a {@link CompositeByteBuf} and 
so do no memory copy whenever possible.
+     * Be aware that {@link CompositeByteBuf} use a more complex indexing 
implementation so depending on your use-case
+     * and the decoder implementation this may be slower then just use the 
{@link #MERGE_CUMULATOR}.
+     */
+    public static final Cumulator COMPOSITE_CUMULATOR = new Cumulator() {
+                                                          @Override
 
 Review comment:
   @saintstack I reformat AbstractBatchDecoder class file.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to