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

this.rangeStart = position;
// adjusting bytes read and written so that isFinished() returns correct output and we know
// when we reached the end of the stream
this.bytesRead = position == 0 ? 0 : (position + 1);
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.

Is this calculation still correct after this fix? For example, if position is 50, does it mean BufferingInputStream should start reading from the offset 50? If yes, don't we need to set both bytesRead and bytesWritten to 50 in that case? Whereas this code will set both of them to 51. Is this expected?


// Configure source to provide sequential data
testSource.setRequestHandler(call -> {
int dataSize = (int) (call.end - call.start);
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.

If both start and end are inclusive, don't we need to modify dataSize = end - start + 1 ? Perhaps we can catch this off by a byte by adding content/length verification below.

@@ -157,7 +157,7 @@
// Backward seek path has a bug: requests buffer.remaining() + 1 bytes
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.

Existing bug requests 1 byte extra. Looks like after this change, CdcRandomAccessReader's rebuffer requests 2 bytes extra for backward seek case. Assume offset is 0, buffer remaining is 50, then we will request from 0 to 51, which will be 2 bytes extra. Don't we need to fix this code in CdcRandomAccessReader ?

                    // attempting to read bytes previously read
                    // in practice we read the Commit Logs sequentially
                    // but we still need to respect random access reader API, it will just require blocking
                    int requestLen = buffer.remaining() + 1;
                    long end = offset + requestLen;
                    BlockingStreamConsumer streamConsumer = new BlockingStreamConsumer();
                    source.request(offset, end, streamConsumer);

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.

Looks like this code was always off by 2, this PR didn't change this.

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.

3 participants