dengziming commented on a change in pull request #9819:
URL: https://github.com/apache/kafka/pull/9819#discussion_r563617104



##########
File path: clients/src/main/resources/common/message/FetchSnapshotResponse.json
##########
@@ -51,8 +51,8 @@
           "about": "The total size of the snapshot." },
         { "name": "Position", "type": "int64", "versions": "0+",
           "about": "The starting byte position within the snapshot included in 
the Bytes field." },
-        { "name": "Bytes", "type": "bytes", "versions": "0+", "zeroCopy": true,
-          "about": "Snapshot data." }
+        { "name": "Bytes", "type": "records", "versions": "0+",

Review comment:
       I have a question here, should we bump the version when changing the 
name?

##########
File path: 
clients/src/main/java/org/apache/kafka/common/protocol/SendBuilder.java
##########
@@ -137,6 +138,9 @@ public void writeRecords(BaseRecords records) {
         if (records instanceof MemoryRecords) {
             flushPendingBuffer();
             addBuffer(((MemoryRecords) records).buffer());
+        } else if (records instanceof UnalignedMemoryRecords) {

Review comment:
       Add a testZeroCopyUnalignedRecords

##########
File path: 
clients/src/main/java/org/apache/kafka/common/record/UnalignedFileRecords.java
##########
@@ -0,0 +1,50 @@
+/*
+ * 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.record;
+
+import org.apache.kafka.common.network.TransferableChannel;
+
+import java.io.IOException;
+import java.nio.channels.FileChannel;
+
+/**
+ * Represents a file record set which is not necessarily offset-aligned
+ */
+public class UnalignedFileRecords implements UnalignedRecords {
+
+    private final FileChannel channel;
+    private final long position;
+    private final int size;
+
+    public UnalignedFileRecords(FileChannel channel, long position, int size) {
+        this.channel = channel;
+        this.position = position;
+        this.size = size;
+    }
+
+    @Override
+    public int sizeInBytes() {
+        return size;
+    }
+
+    @Override
+    public long writeTo(TransferableChannel destChannel, long 
previouslyWritten, int remaining) throws IOException {

Review comment:
       Add UnalignedFileRecordsTest

##########
File path: 
raft/src/main/java/org/apache/kafka/snapshot/FileRawSnapshotWriter.java
##########
@@ -53,13 +56,20 @@ public long sizeInBytes() throws IOException {
     }
 
     @Override
-    public void append(ByteBuffer buffer) throws IOException {
+    public void append(BaseRecords records) throws IOException {
         if (frozen) {
             throw new IllegalStateException(
-                String.format("Append is not supported. Snapshot is already 
frozen: id = %s; temp path = %s", snapshotId, tempSnapshotPath)
+                    String.format("Append is not supported. Snapshot is 
already frozen: id = %s; temp path = %s", snapshotId, tempSnapshotPath)
             );
         }
-
+        ByteBuffer buffer;
+        if (records instanceof MemoryRecords) {
+            buffer = ((MemoryRecords) records).buffer();
+        } else {
+            buffer = ByteBuffer.allocate(records.sizeInBytes());
+            ((FileRecords) records).channel().read(buffer);
+            buffer.flip();
+        }

Review comment:
       Keep 2 methods here, one for MemoryRecords and one for 
UnalignedMemoryRecords.

##########
File path: 
clients/src/test/java/org/apache/kafka/common/record/UnalignedFileRecordsTest.java
##########
@@ -0,0 +1,73 @@
+/*
+ * 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.record;
+
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Iterator;
+
+import static org.apache.kafka.test.TestUtils.tempFile;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class UnalignedFileRecordsTest {
+
+    private byte[][] values = new byte[][] {
+            "foo".getBytes(),
+            "bar".getBytes()
+    };
+    private FileRecords fileRecords;
+
+    @BeforeEach
+    public void setup() throws IOException {
+        this.fileRecords = createFileRecords(values);
+    }
+
+    @Test
+    public void testWriteTo() throws IOException {
+
+        org.apache.kafka.common.requests.ByteBufferChannel channel = new 
org.apache.kafka.common.requests.ByteBufferChannel(fileRecords.sizeInBytes());

Review comment:
       ImportControl doesn't allow us to import ByteBufferChannel here, this is 
similar to `NonOverflowingByteBufferChannel` in your pr #4574

##########
File path: 
clients/src/main/java/org/apache/kafka/common/record/UnalignedFileRecords.java
##########
@@ -0,0 +1,50 @@
+/*
+ * 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.record;
+
+import org.apache.kafka.common.network.TransferableChannel;
+
+import java.io.IOException;
+import java.nio.channels.FileChannel;
+
+/**
+ * Represents a file record set which is not necessarily offset-aligned
+ */
+public class UnalignedFileRecords implements UnalignedRecords {
+
+    private final FileChannel channel;
+    private final long position;
+    private final int size;
+
+    public UnalignedFileRecords(FileChannel channel, long position, int size) {
+        this.channel = channel;
+        this.position = position;
+        this.size = size;
+    }
+
+    @Override
+    public int sizeInBytes() {
+        return size;
+    }
+
+    @Override
+    public long writeTo(TransferableChannel destChannel, long 
previouslyWritten, int remaining) throws IOException {

Review comment:
       @jsancio , I think you are right, I also find that the implementation of 
`MemoryRecords` and `FileRecords` is also different, I will spend some time 
checking this.

##########
File path: 
clients/src/test/java/org/apache/kafka/common/record/UnalignedFileRecordsTest.java
##########
@@ -0,0 +1,73 @@
+/*
+ * 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.record;
+
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Iterator;
+
+import static org.apache.kafka.test.TestUtils.tempFile;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class UnalignedFileRecordsTest {
+
+    private byte[][] values = new byte[][] {
+            "foo".getBytes(),
+            "bar".getBytes()
+    };
+    private FileRecords fileRecords;
+
+    @BeforeEach
+    public void setup() throws IOException {
+        this.fileRecords = createFileRecords(values);
+    }
+
+    @Test
+    public void testWriteTo() throws IOException {
+
+        org.apache.kafka.common.requests.ByteBufferChannel channel = new 
org.apache.kafka.common.requests.ByteBufferChannel(fileRecords.sizeInBytes());
+        int size = fileRecords.sizeInBytes();
+
+        UnalignedFileRecords records1 = fileRecords.sliceUnaligned(0, size / 
2);
+        UnalignedFileRecords records2 = fileRecords.sliceUnaligned(size / 2, 
size - size / 2);

Review comment:
       Size may be an odd number or even number.

##########
File path: clients/src/main/java/org/apache/kafka/common/utils/Utils.java
##########
@@ -1098,6 +1099,29 @@ public static void writeFully(FileChannel channel, 
ByteBuffer sourceBuffer) thro
             channel.write(sourceBuffer);
     }
 
+    /**
+     * Trying to write data in source buffer to a {@link TransferableChannel}, 
we may need to call this method multiple
+     * times since this method doesn't ensure data in source buffer can be 
fully written to dest channel.
+     *
+     * @param destChannel The dest channel
+     * @param position From which the source buffer will be written
+     * @param length The max size of bytes can be written
+     * @param sourceBuffer The source buffer
+     *
+     * @return The length of the actual written data
+     * @throws IOException If an I/O error occurs
+     */
+    public static long tryWriteTo(TransferableChannel destChannel,
+                                  int position,
+                                  int length,
+                                  ByteBuffer sourceBuffer) throws IOException {
+
+        ByteBuffer dup = sourceBuffer.duplicate();
+        dup.position(position);
+        dup.limit(position + length);

Review comment:
       There are many occurrences of `position`+`limit` in Kafka project, I 
think this is because the developers of Kafka like to handle the ByteBuffer by 
ourselves.




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