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



##########
File path: 
generator/src/main/java/org/apache/kafka/message/MessageDataGenerator.java
##########
@@ -1529,37 +1516,37 @@ private void generateVariableLengthFieldSize(FieldSpec 
field,
                             if (tagged) {
                                 buffer.printf("int _stringPrefixSize = " +
                                     
"ByteUtils.sizeOfUnsignedVarint(_stringBytes.length + 1);%n");
-                                buffer.printf("_size += _stringBytes.length + 
_stringPrefixSize + " +
-                                    
"ByteUtils.sizeOfUnsignedVarint(_stringPrefixSize);%n");
+                                
buffer.printf("_size.addBytes(_stringBytes.length + _stringPrefixSize + " +
+                                    
"ByteUtils.sizeOfUnsignedVarint(_stringPrefixSize));%n");
                             } else {
-                                buffer.printf("_size += _stringBytes.length + 
" +
-                                    
"ByteUtils.sizeOfUnsignedVarint(_stringBytes.length + 1);%n");
+                                
buffer.printf("_size.addBytes(_stringBytes.length + " +
+                                    
"ByteUtils.sizeOfUnsignedVarint(_stringBytes.length + 1));%n");
                             }
                         }).
                         ifNotMember(__ -> {
                             if (tagged) {
                                 throw new RuntimeException("Tagged field " + 
field.name() +
                                     " should not be present in non-flexible 
versions.");
                             }
-                            buffer.printf("_size += _stringBytes.length + 
2;%n");
+                            buffer.printf("_size.addBytes(_stringBytes.length 
+ 2);%n");
                         }).
                         generate(buffer);
                 } else if (field.type().isArray()) {
-                    buffer.printf("int _arraySize = 0;%n");
+                    buffer.printf("int _sizeBeforeArray = 
_size.totalSize();%n");

Review comment:
       ```java
   if (tagged) {
     buffer.printf("int _sizeBeforeArray = _size.totalSize();%n");
   }
   ```

##########
File path: 
clients/src/main/java/org/apache/kafka/common/requests/AbstractResponse.java
##########
@@ -88,16 +80,11 @@ protected void updateErrorCounts(Map<Errors, Integer> 
errorCounts, Errors error)
 
     protected abstract Struct toStruct(short version);
 
-    public ByteBuffer serializeBody(short version) {
-        Struct dataStruct = toStruct(version);
-        ByteBuffer buffer = ByteBuffer.allocate(dataStruct.sizeOf());
-        dataStruct.writeTo(buffer);
-        buffer.flip();
-
-        return buffer;
-    }
-
-    public static AbstractResponse deserializeBody(ByteBuffer byteBuffer, 
RequestHeader header) {
+    /**
+     * Parse a response from the provided buffer. The buffer is expected to 
hold both
+     * the {@link ResponseHeader} as well as the response payload.
+     */
+    public static AbstractResponse parseResponse(ByteBuffer byteBuffer, 
RequestHeader header) {

Review comment:
       Is this method equal to 
https://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/clients/NetworkClient.java#L727
 ?

##########
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:
       Is it worth wrapping the byte array to a new ```ByteBufferSend``` to 
avoid array coping?




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