-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-9835; Protect FileRecords.slice from concurrent write
#8451
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 all commits
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 |
|---|---|---|
|
|
@@ -36,6 +36,9 @@ | |
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.Random; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.Future; | ||
|
|
||
| import static java.util.Arrays.asList; | ||
| import static org.apache.kafka.common.utils.Utils.utf8; | ||
|
|
@@ -118,6 +121,36 @@ public void testIterationOverPartialAndTruncation() throws IOException { | |
| testPartialWrite(6, fileRecords); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSliceSizeLimitWithConcurrentWrite() throws Exception { | ||
| FileRecords log = FileRecords.open(tempFile()); | ||
| ExecutorService executor = Executors.newFixedThreadPool(2); | ||
| int maxSizeInBytes = 16384; | ||
|
|
||
| try { | ||
| Future<Object> readerCompletion = executor.submit(() -> { | ||
| while (log.sizeInBytes() < maxSizeInBytes) { | ||
| int currentSize = log.sizeInBytes(); | ||
| FileRecords slice = log.slice(0, currentSize); | ||
| assertEquals(currentSize, slice.sizeInBytes()); | ||
| } | ||
| return null; | ||
| }); | ||
|
|
||
| Future<Object> writerCompletion = executor.submit(() -> { | ||
| while (log.sizeInBytes() < maxSizeInBytes) { | ||
| append(log, values); | ||
| } | ||
| return null; | ||
| }); | ||
|
|
||
| writerCompletion.get(); | ||
| readerCompletion.get(); | ||
| } finally { | ||
| executor.shutdownNow(); | ||
|
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. How often does this fail?
Contributor
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. It fails consistently for me without the fix.
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. Nice |
||
| } | ||
| } | ||
|
|
||
| private void testPartialWrite(int size, FileRecords fileRecords) throws IOException { | ||
| ByteBuffer buffer = ByteBuffer.allocate(size); | ||
| for (int i = 0; i < size; i++) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was the boundary check wrong? You changed
>=to>.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The common case that we would see this is when the limit matches the file end exactly. So changing this to
>would also have fixed this problem for that case. I decided to remove it here though just because the equality check is redundant (ifend == start + currentSizeInBytes, then there's no need to update it).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense.