Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,20 @@ public void readInto(ByteBuffer buffer, int position) throws IOException {
* @return A sliced wrapper on this message set limited based on the given position and size
*/
public FileRecords slice(int position, int size) throws IOException {
// Cache current size in case concurrent write changes it
int currentSizeInBytes = sizeInBytes();

if (position < 0)
throw new IllegalArgumentException("Invalid position: " + position + " in read from " + this);
if (position > sizeInBytes() - start)
if (position > currentSizeInBytes - start)
throw new IllegalArgumentException("Slice from position " + position + " exceeds end position of " + this);
if (size < 0)
throw new IllegalArgumentException("Invalid size: " + size + " in read from " + this);

int end = this.start + position + size;
// handle integer overflow or if end is beyond the end of the file
if (end < 0 || end >= start + sizeInBytes())
end = start + sizeInBytes();
if (end < 0 || end > start + currentSizeInBytes)

Copy link
Copy Markdown
Member

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

@hachikuji hachikuji Apr 8, 2020

Copy link
Copy Markdown
Contributor Author

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 (if end == start + currentSizeInBytes, then there's no need to update it).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

end = start + currentSizeInBytes;
return new FileRecords(file, channel, this.start + position, end, true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How often does this fail?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It fails consistently for me without the fix.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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++)
Expand Down