Skip to content

CASSANALYTICS-147: BufferingInputStream fails to read last chunk#193

Open
lukasz-antoniak wants to merge 3 commits intoapache:trunkfrom
lukasz-antoniak:CASSANALYTICS-147
Open

CASSANALYTICS-147: BufferingInputStream fails to read last chunk#193
lukasz-antoniak wants to merge 3 commits intoapache:trunkfrom
lukasz-antoniak:CASSANALYTICS-147

Conversation

@lukasz-antoniak
Copy link
Copy Markdown
Member

@lukasz-antoniak lukasz-antoniak marked this pull request as ready for review April 2, 2026 11:44

int bytesToRead = chunkSize * numChunks;
long skipAhead = size - bytesToRead + 1;
long skipAhead = size - bytesToRead;
Copy link
Copy Markdown
Member Author

@lukasz-antoniak lukasz-antoniak Apr 2, 2026

Choose a reason for hiding this comment

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

I am not sure how effective the change in BufferingInputStream would affect skip() used during BIG index reading. All integration tests pass though, and I think hereby unit test is just a simulation.


// Deliver data in chunks until request is fulfilled
while (position < actualEnd)
while (position <= actualEnd) // range boundaries are inclusive
Copy link
Copy Markdown
Member Author

@lukasz-antoniak lukasz-antoniak Apr 2, 2026

Choose a reason for hiding this comment

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

According to below JavaDoc, ranges should be considered inclusive.

/**
 * Asynchronously request bytes for the SSTable file component in the range start-end, and pass on to the StreamConsumer when available.
 * The start-end range is inclusive.
 *
 * @param start    the start of the bytes range
 * @param end      the end of the bytes range
 * @param consumer the StreamConsumer to return the bytes to when the request is complete
 */
void request(long start, long end, StreamConsumer consumer);

@arjunashok
Copy link
Copy Markdown
Contributor

Seems like FileSystemSource.request() has a workaround (lines 94–99) that was compensating for the old off-by-one in BufferingInputStream.requestMore().
Comment in line 97: "Start-end range is inclusive but on the final request end == length so we need to exclude".

With this fix, end is now at most source.size() - 1, and length equals source.size(), so length <= end is always false.
As a result -

  1. the code always takes the increment = 1 path, which happens to produce the correct end - start + 1, so no data corruption, but it's confusing dead code.

  2. More importantly, close is never true, so the autoClose path in the finally block never triggers.
    For sequential reads via FileSystemSSTable (non-BTI format, where autoClose = true), the RandomAccessFile handle is never closed after the last chunk, which is a file descriptor leak.

}

@Test
public void testUnalignedEndReading() throws IOException
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.

Minor: We might want to assert on returnedBuffers.size() == 2 to catch regressions where extra or missing requests are issued

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants