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 @@ -278,7 +278,7 @@ public long writeTo(TransferableChannel destChannel, long offset, int length) th
file.getAbsolutePath(), oldSize, newSize));

long position = start + offset;
int count = Math.min(length, oldSize);
long count = Math.min(length, oldSize - offset);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This code is called from SocketServer through RecordsSend. The expectation is that the underlying file channel has enough bytes to return for the requested length. Normally this should be the case. If for some reason this is not true, by bounding the transfer amount by the remaining bytes in the file channel will cause RecordsSend.writeTo() to be called in a loop until the request times out. This may hide a real problem.

Perhaps what we could do is to throw an IllegalStateException if the file channel doesn't have enough bytes left. This will cause the socket channel to be closed and the Selector to log a warning.

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.

Perhaps what we could do is to throw an IllegalStateException if the file channel doesn't have enough bytes left. This will cause the socket channel to be closed and the Selector to log a warning.

Please update docs for related writeTo methods if we want to apply this behavior. The current code use return value to calculate the length for next write. The return value gets redundant since the number of transferred data is always equal to expected length (it throws exception when there is no enough bytes left).

return destChannel.transferFrom(channel, position, count);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.header.Header;
import org.apache.kafka.common.header.internals.RecordHeader;
import org.apache.kafka.common.network.TransferableChannel;
import org.apache.kafka.common.utils.MockTime;
import org.apache.kafka.common.utils.Time;
import org.apache.kafka.test.TestUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.io.File;
import java.io.IOException;
Expand All @@ -51,7 +53,9 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
Expand Down Expand Up @@ -512,6 +516,25 @@ public void testConversion() throws IOException {
doTestConversion(CompressionType.GZIP, RecordBatch.MAGIC_VALUE_V2);
}

@Test
public void testBytesLengthOfWriteTo() throws IOException {

int size = fileRecords.sizeInBytes();
long firstWritten = size / 3;

TransferableChannel channel = Mockito.mock(TransferableChannel.class);

// Firstly we wrote some of the data
fileRecords.writeTo(channel, 0, (int) firstWritten);
verify(channel).transferFrom(any(), anyLong(), eq(firstWritten));

// Ensure (length > size - firstWritten)
int secondWrittenLength = size - (int) firstWritten + 1;
fileRecords.writeTo(channel, firstWritten, secondWrittenLength);
// But we still only write (size - firstWritten), which is not fulfilled in the old version
verify(channel).transferFrom(any(), anyLong(), eq(size - firstWritten));
}

private void doTestConversion(CompressionType compressionType, byte toMagic) throws IOException {
List<Long> offsets = asList(0L, 2L, 3L, 9L, 11L, 15L, 16L, 17L, 22L, 24L);

Expand Down