-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-10694: Implement zero copy for FetchSnapshot #9819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
3f604b4
7f58dd4
0ccd53d
d2025a2
2e76f4b
6b28107
79c1209
2ead903
a3b29e7
d75d65b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
|
|
||
| public class SliceRange { | ||
|
dengziming marked this conversation as resolved.
Outdated
|
||
|
|
||
| public final int start; | ||
| public final int end; | ||
|
|
||
| public SliceRange(int start, int end) { | ||
| this.start = start; | ||
| this.end = end; | ||
| } | ||
|
|
||
| public int size() { | ||
| return end - start; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| /** | ||
| * Represents a record set which can be transfer to a channel | ||
|
dengziming marked this conversation as resolved.
Outdated
|
||
| * @see Records | ||
| * @see UnalignedRecords | ||
| */ | ||
| public interface TransferableRecords extends BaseRecords { | ||
|
|
||
| /** | ||
| * Attempts to write the contents of this buffer to a channel. | ||
| * @param channel The channel to write to | ||
| * @param position The position in the buffer to write from | ||
| * @param length The number of bytes to write | ||
| * @return The number of bytes actually written | ||
| * @throws IOException For any IO errors | ||
| */ | ||
| long writeTo(TransferableChannel channel, long position, int length) throws IOException; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have test coverage for this?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add UnalignedFileRecordsTest
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm. The semantic between the in-memory implementation vs the file channel implementation is slightly different. The in-memory version throws an exception if there are not enough bytes to send I suspect that we want to throw in both cases else the calling code won't know why
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jsancio , I think you are right, I also find that the implementation of |
||
| long position = this.position + previouslyWritten; | ||
| long count = Math.min(remaining, sizeInBytes() - previouslyWritten); | ||
| return destChannel.transferFrom(channel, position, count); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * 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 org.apache.kafka.common.utils.Utils; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.ByteBuffer; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Represents a memory record set which is not necessarily offset-aligned | ||
| */ | ||
| public class UnalignedMemoryRecords implements UnalignedRecords { | ||
|
|
||
| private final ByteBuffer buffer; | ||
|
|
||
| public UnalignedMemoryRecords(ByteBuffer buffer) { | ||
| this.buffer = Objects.requireNonNull(buffer); | ||
| } | ||
|
|
||
| public ByteBuffer buffer() { | ||
| return buffer.duplicate(); | ||
| } | ||
|
|
||
| @Override | ||
| public int sizeInBytes() { | ||
| return buffer.remaining(); | ||
| } | ||
|
|
||
| public static UnalignedMemoryRecords readableRecords(ByteBuffer buffer) { | ||
| Objects.requireNonNull(buffer, "buffer should not be null"); | ||
| return new UnalignedMemoryRecords(buffer); | ||
| } | ||
|
|
||
| @Override | ||
| public long writeTo(TransferableChannel channel, long position, int length) throws IOException { | ||
| return Utils.tryWriteTo(channel, position, length, buffer); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| /** | ||
| * Represents a record set which is not necessarily offset-aligned, and is | ||
| * only used when fetching raft snapshot | ||
| */ | ||
| public interface UnalignedRecords extends TransferableRecords { | ||
|
|
||
| @Override | ||
| default RecordsSend<? extends BaseRecords> toSend() { | ||
| return new DefaultRecordsSend<>(this, sizeInBytes()); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.