You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ratis.apache.org by GitBox <gi...@apache.org> on 2020/07/30 07:28:20 UTC
[GitHub] [incubator-ratis] szetszwo commented on a change in pull request #155: RATIS-1009 - Zero-copy semantics in Netty.
szetszwo commented on a change in pull request #155:
URL: https://github.com/apache/incubator-ratis/pull/155#discussion_r462500379
##########
File path: ratis-experiments/src/main/java/org/apache/ratis/experiments/nettyzerocopy/encoders/RequestEncoder.java
##########
@@ -0,0 +1,46 @@
+/**
+ * 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.ratis.experiments.nettyzerocopy.encoders;
+
+import org.apache.ratis.experiments.nettyzerocopy.objects.RequestData;
+import org.apache.ratis.thirdparty.io.netty.buffer.Unpooled;
+import org.apache.ratis.thirdparty.io.netty.channel.ChannelHandlerContext;
+import org.apache.ratis.thirdparty.io.netty.handler.codec.MessageToMessageEncoder;
+
+import java.nio.ByteBuffer;
+import java.util.List;
+
+
+/**
+ * Encoder class for {@link RequestData}
+ * Writes ID, Length of the buffer and buffer to the outbound message.
+ */
+public class RequestEncoder
+ extends MessageToMessageEncoder<RequestData> {
+
+ @Override
+ protected void encode(ChannelHandlerContext channelHandlerContext,
+ RequestData requestData, List<Object> list) throws Exception {
+ ByteBuffer bb = ByteBuffer.allocateDirect(8);
Review comment:
This is okay for experiment code. In the production code, we should use a buffer pool instead of ByteBuffer.allocateDirect.
##########
File path: ratis-experiments/src/main/java/org/apache/ratis/experiments/nettyzerocopy/client/NettyClient.java
##########
@@ -0,0 +1,150 @@
+/**
+ * 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.ratis.experiments.nettyzerocopy.client;
+
+import org.apache.ratis.experiments.nettyzerocopy.objects.RequestData;
+import org.apache.ratis.experiments.nettyzerocopy.encoders.RequestEncoder;
+import org.apache.ratis.experiments.nettyzerocopy.objects.ResponseData;
+import org.apache.ratis.experiments.nettyzerocopy.decoders.ResponseDecoder;
+import org.apache.ratis.thirdparty.io.netty.bootstrap.Bootstrap;
+import org.apache.ratis.thirdparty.io.netty.channel.*;
+import org.apache.ratis.thirdparty.io.netty.channel.nio.NioEventLoopGroup;
+import org.apache.ratis.thirdparty.io.netty.channel.socket.SocketChannel;
+import org.apache.ratis.thirdparty.io.netty.channel.socket.nio.NioSocketChannel;
+
+import java.nio.ByteBuffer;
+
+/**
+ * A client program the utilizes Netty async-I/O to
+ * stream 10,000 messages using {@link RequestEncoder}
+ * with zero-copy semantics.
+ */
+public class NettyClient {
+ private final int times = 10000;
+ private Channel channel;
+ private final EventLoopGroup workerGroup = new NioEventLoopGroup();
+ private long startTime;
+
+ public int getTimes() {
+ return times;
+ }
+
+ public EventLoopGroup getWorkerGroup() {
+ return workerGroup;
+ }
+
+ public Channel getChannel() {
+ return channel;
+ }
+
+ public long getStartTime() {
+ return startTime;
+ }
+
+ public void timeClient(){
+ long endTime = System.nanoTime();
+ System.out.printf("Time taken by Client to send %d messages is %f seconds\n",
+ times, (double)(endTime - startTime)/(1000*1000*1000));
Review comment:
FYI, we may use (endTime - startTime)/1_000-000_000.0
##########
File path: ratis-experiments/src/main/java/org/apache/ratis/experiments/nettyzerocopy/client/NettyClient.java
##########
@@ -0,0 +1,150 @@
+/**
+ * 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.ratis.experiments.nettyzerocopy.client;
+
+import org.apache.ratis.experiments.nettyzerocopy.objects.RequestData;
+import org.apache.ratis.experiments.nettyzerocopy.encoders.RequestEncoder;
+import org.apache.ratis.experiments.nettyzerocopy.objects.ResponseData;
+import org.apache.ratis.experiments.nettyzerocopy.decoders.ResponseDecoder;
+import org.apache.ratis.thirdparty.io.netty.bootstrap.Bootstrap;
+import org.apache.ratis.thirdparty.io.netty.channel.*;
+import org.apache.ratis.thirdparty.io.netty.channel.nio.NioEventLoopGroup;
+import org.apache.ratis.thirdparty.io.netty.channel.socket.SocketChannel;
+import org.apache.ratis.thirdparty.io.netty.channel.socket.nio.NioSocketChannel;
+
+import java.nio.ByteBuffer;
+
+/**
+ * A client program the utilizes Netty async-I/O to
+ * stream 10,000 messages using {@link RequestEncoder}
+ * with zero-copy semantics.
+ */
+public class NettyClient {
+ private final int times = 10000;
+ private Channel channel;
+ private final EventLoopGroup workerGroup = new NioEventLoopGroup();
+ private long startTime;
+
+ public int getTimes() {
+ return times;
+ }
+
+ public EventLoopGroup getWorkerGroup() {
+ return workerGroup;
+ }
+
+ public Channel getChannel() {
+ return channel;
+ }
+
+ public long getStartTime() {
+ return startTime;
+ }
+
+ public void timeClient(){
+ long endTime = System.nanoTime();
+ System.out.printf("Time taken by Client to send %d messages is %f seconds\n",
+ times, (double)(endTime - startTime)/(1000*1000*1000));
+ }
+
+ /**
+ * Closes client worker, invoked on program completion
+ * @throws InterruptedException
+ */
+ private void closeClient() throws InterruptedException {
+ try {
+ channel.closeFuture().sync();
+ } finally {
+ workerGroup.shutdownGracefully();
+ }
+ }
+
+ /**
+ * Client handler for server messages.
+ * @return ChannelInboundHandler
+ */
+ private ChannelInboundHandler getClientHandler(){
+ return new ChannelInboundHandlerAdapter(){
+ @Override
+ public void channelRead(ChannelHandlerContext ctx, Object msg) throws InterruptedException {
+ final ResponseData reply = (ResponseData)msg;
+ if(reply.getId() == times){
+ System.out.println("Received all messages, closing client.");
+ timeClient();
+ closeClient();
+ }
+ }
+ };
+ }
+
+ private ChannelInitializer<SocketChannel> getInitializer(){
+ return new ChannelInitializer<SocketChannel>(){
+ @Override
+ public void initChannel(SocketChannel ch)
+ throws Exception {
+ ChannelPipeline p = ch.pipeline();
+ p.addLast(new RequestEncoder());
+ p.addLast(new ResponseDecoder());
+ p.addLast(getClientHandler());
+ }
+ };
+ }
+
+ private void transferMessage(Object msg){
+ channel.writeAndFlush(msg);
Review comment:
We should not flush for every message.
##########
File path: ratis-experiments/src/main/java/org/apache/ratis/experiments/nettyzerocopy/client/NettyClient.java
##########
@@ -0,0 +1,150 @@
+/**
+ * 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.ratis.experiments.nettyzerocopy.client;
+
+import org.apache.ratis.experiments.nettyzerocopy.objects.RequestData;
+import org.apache.ratis.experiments.nettyzerocopy.encoders.RequestEncoder;
+import org.apache.ratis.experiments.nettyzerocopy.objects.ResponseData;
+import org.apache.ratis.experiments.nettyzerocopy.decoders.ResponseDecoder;
+import org.apache.ratis.thirdparty.io.netty.bootstrap.Bootstrap;
+import org.apache.ratis.thirdparty.io.netty.channel.*;
+import org.apache.ratis.thirdparty.io.netty.channel.nio.NioEventLoopGroup;
+import org.apache.ratis.thirdparty.io.netty.channel.socket.SocketChannel;
+import org.apache.ratis.thirdparty.io.netty.channel.socket.nio.NioSocketChannel;
+
+import java.nio.ByteBuffer;
+
+/**
+ * A client program the utilizes Netty async-I/O to
+ * stream 10,000 messages using {@link RequestEncoder}
+ * with zero-copy semantics.
+ */
+public class NettyClient {
+ private final int times = 10000;
+ private Channel channel;
+ private final EventLoopGroup workerGroup = new NioEventLoopGroup();
+ private long startTime;
+
+ public int getTimes() {
+ return times;
+ }
+
+ public EventLoopGroup getWorkerGroup() {
+ return workerGroup;
+ }
+
+ public Channel getChannel() {
+ return channel;
+ }
+
+ public long getStartTime() {
+ return startTime;
+ }
+
+ public void timeClient(){
+ long endTime = System.nanoTime();
+ System.out.printf("Time taken by Client to send %d messages is %f seconds\n",
+ times, (double)(endTime - startTime)/(1000*1000*1000));
Review comment:
FYI, we may use (endTime - startTime)/1_000_000_000.0
----------------------------------------------------------------
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:
users@infra.apache.org