[GitHub] [kafka] jsancio commented on a change in pull request #9819: KAFKA-10694: Implement zero copy for FetchSnapshot
jsancio commented on a change in pull request #9819: URL: https://github.com/apache/kafka/pull/9819#discussion_r564124971 ## 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: Isn't this `position()` and `limit()` modification the same as `ByteBuffer.slice`? Having said that I find it strange that we want to set the absolute position and limit. Shouldn't this be set relative to the current position? For example think about `sourceBuffer.position() > position`. ## 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 { Review comment: How about removing the IO from this method with a signature like: ```java public static ByteBuffer relativeSlice(ByteBuffer buffer, int position, int length); ``` The caller of this method can then `long written = destChannel.write(Utils.relativeSlice(buffer, position, length))`. ## 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();
[GitHub] [kafka] jsancio commented on a change in pull request #9819: KAFKA-10694: Implement zero copy for FetchSnapshot
jsancio commented on a change in pull request #9819: URL: https://github.com/apache/kafka/pull/9819#discussion_r564124971 ## 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: Isn't this `position()` and `limit()` modification the same as `ByteBuffer.slice`? Having said that I find it strange that we want to set the absolute position and limit. Shouldn't this be set relative to the current position? For example think about `sourceBuffer.position() > position`. ## 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 { Review comment: How about removing the IO from this method with a signature like: ```java public static ByteBuffer relativeSlice(ByteBuffer buffer, int position, int length); ``` The caller of this method can then `long written = destChannel.write(Utils.relativeSlice(buffer, position, length))`. ## 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();
[GitHub] [kafka] jsancio commented on a change in pull request #9819: KAFKA-10694: Implement zero copy for FetchSnapshot
jsancio commented on a change in pull request #9819: URL: https://github.com/apache/kafka/pull/9819#discussion_r561427699 ## 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: > I change the signature to keep consistent with FileRawSnapshotReader Okay. I think this is something that I struggled with when creating the original APIs. I am okay with "inconsistent" APIs since `RawSnapshot{Reader,Writer}` are internal interfaces to the raft client and are not exposed to the state machine (controller). I think this "inconsistency" will go away when we implement the long term solution. 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
[GitHub] [kafka] jsancio commented on a change in pull request #9819: KAFKA-10694: Implement zero copy for FetchSnapshot
jsancio commented on a change in pull request #9819: URL: https://github.com/apache/kafka/pull/9819#discussion_r561427699 ## 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: > I change the signature to keep consistent with FileRawSnapshotReader Okay. I think this is something that I struggled with when creating the original APIs. I am okay with "inconsistent" APIs since `RawSnapshot{Reader,Writer}` are internal interfaces to the raft client and are not exposed to the state machine (controller). I think this "inconsistency" will go away when we implement the long term solution. 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
[GitHub] [kafka] jsancio commented on a change in pull request #9819: KAFKA-10694: Implement zero copy for FetchSnapshot
jsancio commented on a change in pull request #9819: URL: https://github.com/apache/kafka/pull/9819#discussion_r560420440 ## File path: raft/src/main/java/org/apache/kafka/raft/KafkaRaftClient.java ## @@ -1220,34 +1216,35 @@ private FetchSnapshotResponseData handleFetchSnapshotRequest( if (!snapshotOpt.isPresent()) { return FetchSnapshotResponse.singleton( log.topicPartition(), -responsePartitionSnapshot -> { -return addQuorumLeader(responsePartitionSnapshot) -.setErrorCode(Errors.SNAPSHOT_NOT_FOUND.code()); -} +responsePartitionSnapshot -> addQuorumLeader(responsePartitionSnapshot) +.setErrorCode(Errors.SNAPSHOT_NOT_FOUND.code()) ); } try (RawSnapshotReader snapshot = snapshotOpt.get()) { if (partitionSnapshot.position() < 0 || partitionSnapshot.position() >= snapshot.sizeInBytes()) { return FetchSnapshotResponse.singleton( log.topicPartition(), -responsePartitionSnapshot -> { -return addQuorumLeader(responsePartitionSnapshot) -.setErrorCode(Errors.POSITION_OUT_OF_RANGE.code()); -} +responsePartitionSnapshot -> addQuorumLeader(responsePartitionSnapshot) +.setErrorCode(Errors.POSITION_OUT_OF_RANGE.code()) ); } int maxSnapshotSize; +int maxSnapshotPosition; try { maxSnapshotSize = Math.toIntExact(snapshot.sizeInBytes()); } catch (ArithmeticException e) { maxSnapshotSize = Integer.MAX_VALUE; } -ByteBuffer buffer = ByteBuffer.allocate(Math.min(data.maxBytes(), maxSnapshotSize)); -snapshot.read(buffer, partitionSnapshot.position()); -buffer.flip(); +try { +maxSnapshotPosition = Math.toIntExact(partitionSnapshot.position()); +} catch (ArithmeticException e) { +maxSnapshotPosition = Integer.MAX_VALUE; Review comment: Resetting the position to `Integer.MAX_VALUE` doesn't seem safe. I think the best we can do is let the exception bubble up. Related to this, shouldn't the position be a long in `RawSnapshotReader::read`? ## File path: clients/src/main/java/org/apache/kafka/common/record/BaseRegion.java ## @@ -0,0 +1,62 @@ +/* + * 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.ByteBuffer; +import java.nio.channels.FileChannel; + +/** + * Base interface for accessing raft snapshot which could be file region or an in-memory buffers. + */ +public interface BaseRegion { Review comment: I don't think this interface is used or needed. Are you planning to remove it? ## 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: What do you think about keeping this signature as `void append(ByterBuff
[GitHub] [kafka] jsancio commented on a change in pull request #9819: KAFKA-10694: Implement zero copy for FetchSnapshot
jsancio commented on a change in pull request #9819: URL: https://github.com/apache/kafka/pull/9819#discussion_r552869364 ## File path: clients/src/main/java/org/apache/kafka/common/record/FileRegion.java ## @@ -0,0 +1,82 @@ +/* + * 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.KafkaException; +import org.apache.kafka.common.network.Send; +import org.apache.kafka.common.network.TransferableChannel; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; + +/** + * Represent a file with a start offset and length, we use it to send large snapshot file + */ +public class FileRegion implements BaseRegion { Review comment: I agree that it is very similar to how `FileRecords` works with the difference that the file region described doesn't have to be a valid record batch. It would be interesting to see if we can unify these abstractions. 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