Skip to content

Fix directIO reader hang when pread returns short reads#4841

Open
void-ptr974 wants to merge 2 commits into
apache:masterfrom
void-ptr974:fix-directreader-short-read
Open

Fix directIO reader hang when pread returns short reads#4841
void-ptr974 wants to merge 2 commits into
apache:masterfrom
void-ptr974:fix-directreader-short-read

Conversation

@void-ptr974

@void-ptr974 void-ptr974 commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

DirectReader performs aligned O_DIRECT reads into a reusable native buffer. Short reads must preserve alignment, and failed partial reads must not leave modified buffer contents associated with stale cache metadata.

This change:

  • advances aligned short reads from the next aligned offset;
  • fails with IOException when a short read cannot make aligned progress;
  • invalidates cached block metadata before loading a new block;
  • copies only the bytes available in the loaded block.

Tests:

  • mvn -pl bookkeeper-server -DskipTests=false -Dtest=TestDirectReader test
  • mvn -pl bookkeeper-server -DskipTests checkstyle:check
  • git diff --check

@void-ptr974 void-ptr974 changed the title Fix directIO reads stuck after short reads Fix directIO reader hang after short reads Jul 18, 2026
@void-ptr974 void-ptr974 changed the title Fix directIO reader hang after short reads Fix directIO reader hang when pread returns short reads Jul 18, 2026
@StevenLuMT
StevenLuMT requested a review from Copilot July 18, 2026 15:16

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a potential hang in DirectReader when using O_DIRECT reads and pread returns a short read: the reader now advances by the aligned portion actually read (and fails fast if no aligned progress is possible), preventing repeated reads at the same offset. It also tightens buffer-copying to only return bytes that are actually available within the currently loaded block, and adds a regression test for aligned short-read progress.

Changes:

  • Fix aligned progress calculation in DirectReader.readBlock() for short pread results and fail fast on zero aligned progress.
  • Ensure readBytesIntoBuf() copies only the bytes available in the current cached block.
  • Add a unit test that simulates aligned short reads and verifies the reader advances to the next aligned offset.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/storage/directentrylogger/DirectReader.java Fixes short-read alignment progress and restricts copying to bytes available in the current block.
bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/directentrylogger/TestDirectReader.java Adds a regression test to ensure aligned short reads advance the read offset and complete.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +233 to +235
.kv("requestedBytes", blockSize)
.kv("offset", blockStart)
.kv("expectedBytes", Math.min(blockSize, bytesAvailable))
.kv("size", size).toString());
}
return nativeBuffer.readByteBuf(buf, offsetInBuffer, size);
return nativeBuffer.readByteBuf(buf, offsetInBuffer, sizeInBuffer);
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