Skip to content

HDDS-16327. Read the block file length once per WriteChunk instead of three fstat calls - #11150

Open
rich7420 wants to merge 3 commits into
apache:masterfrom
rich7420:HDDS-16327
Open

HDDS-16327. Read the block file length once per WriteChunk instead of three fstat calls#11150
rich7420 wants to merge 3 commits into
apache:masterfrom
rich7420:HDDS-16327

Conversation

@rich7420

@rich7420 rich7420 commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

FilePerBlockStrategy.writeChunk resolved the block file length via FileChannel.size() three times per WriteChunk
(twice on an overwrite), each an uncached fstat: the overwrite check, the offset validation, and the space
accounting. All three run before the write, and WriteChunk apply for a given block file is serialized, so the length is
stable across them.

This reads channel.size() once and reuses it for all three. The FileChannel-based ChunkUtils helpers
(validateChunkForOverwrite, isOverWriteRequested, and validateChunkSize) now take the already-read length instead
of re-stat-ing; the File-based overloads used by FilePerChunkStrategy are unchanged.

If the size read fails, the consolidated path preserves the previous effective result code. The old
UncheckedIOException was converted to CONTAINER_INTERNAL_ERROR by KeyValueHandler; the new path throws a
StorageContainerException with the same result code and retains the original cause. onFailure(volume) semantics are
unchanged.

What is the link to the Apache JIRA

https://issues.apache.org/jira/browse/HDDS-16327

How was this patch tested?

https://github.com/rich7420/ozone/actions/runs/33410588777

… three fstat calls

FilePerBlockStrategy.writeChunk resolved FileChannel.size() three times per
WriteChunk (twice on an overwrite). Read it once and reuse the length for the
overwrite check, the offset validation and the space accounting. The
FileChannel-based ChunkUtils.validateChunkForOverwrite / isOverWriteRequested /
validateChunkSize now take the already-read length instead of re-stat-ing.
Behavior unchanged.

Claude-Session: https://claude.ai/code/session_01FUpCUnmy6JzHPGvwMhyGzq
Copilot AI lite review requested due to automatic review settings August 29, 2026 03:09

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@rich7420
rich7420 marked this pull request as draft August 29, 2026 03:14
@rich7420

Copy link
Copy Markdown
Contributor Author

Quick JMH of writeChunk reading the length 3x (old) vs 1x (new) on a cached channel, page-cache hot, JDK 21 / macOS (5 forks x 5, n=25):

  chunk    old        new       saved
   4KB     1.56 us    0.92 us   0.64 us  (~41%)
   1MB     25.5 us    24.7 us   0.86 us  (~3%)

The saving is ~2 fstat (~0.6-0.9 us), roughly constant, so it is a meaningful fraction only for small cached writes (hsync / small keys / metadata) and is diluted by data movement at larger chunks. No fsync in this run, and macOS fstat is pricier than Linux, so the fraction is an upper bound. Standalone JMH, not part of the PR.

@rich7420
rich7420 marked this pull request as ready for review August 29, 2026 08:10

@chihsuan chihsuan left a comment

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.

Nice improvement overall! @rich7420

the single read now throws StorageContainerException(CHUNK_FILE_INCONSISTENCY), matching the other two size-read sites

Just want to confirm this is safe. Did you check how these two result codes are handled in ContainerStateMachine?

I also left a small suggestion to simplify the test.

@@ -236,11 +236,11 @@ void validateChunkForOverwrite() throws IOException {
try (FileChannel fileChannel =
FileChannel.open(tempFile, StandardOpenOption.READ)) {

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.

Could we simplify this test by passing the file length directly and removing the FileChannel setup? The helper still seems worth testing, but it no longer performs any file I/O.

@rich7420
rich7420 marked this pull request as draft August 31, 2026 15:51
@rich7420
rich7420 marked this pull request as ready for review September 1, 2026 06:54
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