hachikuji commented on a change in pull request #9563:
URL: https://github.com/apache/kafka/pull/9563#discussion_r520849832



##########
File path: 
clients/src/main/java/org/apache/kafka/common/protocol/SendBuilder.java
##########
@@ -0,0 +1,194 @@
+/*
+ * 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.kafka.common.protocol;
+
+import org.apache.kafka.common.network.ByteBufferSend;
+import org.apache.kafka.common.network.Send;
+import org.apache.kafka.common.record.BaseRecords;
+import org.apache.kafka.common.record.MultiRecordsSend;
+import org.apache.kafka.common.requests.RequestHeader;
+import org.apache.kafka.common.requests.ResponseHeader;
+import org.apache.kafka.common.utils.ByteUtils;
+
+import java.nio.ByteBuffer;
+import java.util.ArrayDeque;
+import java.util.Queue;
+
+/**
+ * This class provides a way to build {@link Send} objects for network 
transmission
+ * from generated {@link org.apache.kafka.common.protocol.ApiMessage} types 
without
+ * allocating new space for "zero-copy" fields (see {@link 
#writeByteBuffer(ByteBuffer)}
+ * and {@link #writeRecords(BaseRecords)}).
+ *
+ * See {@link org.apache.kafka.common.requests.EnvelopeRequest#toSend(String, 
RequestHeader)}
+ * for example usage.
+ */
+public class SendBuilder implements Writable {
+    private final Queue<Send> sends = new ArrayDeque<>();
+    private final ByteBuffer buffer;
+    private final String destinationId;
+
+    SendBuilder(String destinationId, int size) {
+        this.destinationId = destinationId;
+        this.buffer = ByteBuffer.allocate(size);
+        this.buffer.mark();
+    }
+
+    private void flushCurrentBuffer() {
+        int latestPosition = buffer.position();
+        buffer.reset();
+
+        if (latestPosition > buffer.position()) {
+            buffer.limit(latestPosition);
+            addByteBufferSend(buffer.slice());
+            buffer.position(latestPosition);
+            buffer.limit(buffer.capacity());
+            buffer.mark();
+        }
+    }
+
+    private void addByteBufferSend(ByteBuffer buffer) {
+        sends.add(new ByteBufferSend(destinationId, buffer));
+    }
+
+    @Override
+    public void writeByte(byte val) {
+        buffer.put(val);
+    }
+
+    @Override
+    public void writeShort(short val) {
+        buffer.putShort(val);
+    }
+
+    @Override
+    public void writeInt(int val) {
+        buffer.putInt(val);
+    }
+
+    @Override
+    public void writeLong(long val) {
+        buffer.putLong(val);
+    }
+
+    @Override
+    public void writeDouble(double val) {
+        buffer.putDouble(val);
+    }
+
+    @Override
+    public void writeByteArray(byte[] arr) {

Review comment:
       I did that in an earlier iteration, but I wasn't sure if the overhead of 
the `ByteBuffer` and `ByteBufferSend` made it a net win in the end. I also 
thought about adding a heuristic, such as looking for a minimum size. In the 
end, it seemed simpler to rely on "zeroCopy" to let us know when we are likely 
to get a benefit. 




----------------------------------------------------------------
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:
us...@infra.apache.org


Reply via email to