jerqi commented on code in PR #727:
URL: https://github.com/apache/incubator-uniffle/pull/727#discussion_r1140376209


##########
common/src/main/java/org/apache/uniffle/common/util/NettyUtils.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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.uniffle.common.util;
+
+import java.util.concurrent.ThreadFactory;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.PooledByteBufAllocator;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.epoll.EpollEventLoopGroup;
+import io.netty.channel.epoll.EpollSocketChannel;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.nio.NioSocketChannel;
+import io.netty.util.internal.PlatformDependent;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.uniffle.common.netty.IOMode;
+import org.apache.uniffle.common.netty.protocol.Message;
+
+public class NettyUtils {
+  private static final Logger logger = 
LoggerFactory.getLogger(NettyUtils.class);
+
+  /** Creates a Netty EventLoopGroup based on the IOMode. */
+  public static EventLoopGroup createEventLoop(IOMode mode, int numThreads, 
String threadPrefix) {
+    ThreadFactory threadFactory = 
ThreadUtils.getNettyThreadFactory(threadPrefix);
+
+    switch (mode) {
+      case NIO:
+        return new NioEventLoopGroup(numThreads, threadFactory);
+      case EPOLL:
+        return new EpollEventLoopGroup(numThreads, threadFactory);
+      default:
+        throw new IllegalArgumentException("Unknown io mode: " + mode);
+    }
+  }
+
+  /** Returns the correct (client) SocketChannel class based on IOMode. */
+  public static Class<? extends Channel> getClientChannelClass(IOMode mode) {
+    switch (mode) {
+      case NIO:
+        return NioSocketChannel.class;
+      case EPOLL:
+        return EpollSocketChannel.class;
+      default:
+        throw new IllegalArgumentException("Unknown io mode: " + mode);
+    }
+  }
+
+  public static PooledByteBufAllocator createPooledByteBufAllocator(
+      boolean allowDirectBufs, boolean allowCache, int numCores) {
+    if (numCores == 0) {
+      numCores = Runtime.getRuntime().availableProcessors();
+    }
+    return new PooledByteBufAllocator(
+        allowDirectBufs && PlatformDependent.directBufferPreferred(),
+        Math.min(PooledByteBufAllocator.defaultNumHeapArena(), numCores),
+        Math.min(PooledByteBufAllocator.defaultNumDirectArena(), 
allowDirectBufs ? numCores : 0),
+        PooledByteBufAllocator.defaultPageSize(),
+        PooledByteBufAllocator.defaultMaxOrder(),
+        allowCache ? PooledByteBufAllocator.defaultSmallCacheSize() : 0,
+        allowCache ? PooledByteBufAllocator.defaultNormalCacheSize() : 0,
+        allowCache && PooledByteBufAllocator.defaultUseCacheForAllThreads());
+  }
+
+  /** Returns the remote address on the channel or "&lt;unknown remote&gt;" if 
none exists. */
+  public static String getRemoteAddress(Channel channel) {
+    if (channel != null && channel.remoteAddress() != null) {
+      return channel.remoteAddress().toString();
+    }
+    return "<unknown remote>";
+  }
+
+  public static ChannelFuture writeResponseMsg(ChannelHandlerContext ctx, 
Message msg, boolean doWriteType) {
+    ByteBuf responseMsgBuf = ctx.alloc().buffer(1000);

Review Comment:
   > > How to ensure proper initial size?
   > 
   > This is not very sure. Some messages may be smaller than 64 bytes, and 
some messages may be larger than 64 bytes. Maybe we can use the encodeLength of 
RpcResponse as an initial value, because most Response are RpcResponse, 
Assuming that the retMessage of RpcResponse is empty.
   
   Could we refer to Spark?



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to