jojochuang commented on PR #10350:
URL: https://github.com/apache/ozone/pull/10350#issuecomment-5273884814

   ## `computeChecksum(ChunkBuffer)` assumes read-ready underlying buffers
   
   The new `computeChecksumDirect()` / `ChecksumCache` walk calls 
`data.asByteBufferList()` and, for each underlying `ByteBuffer`, checksums 
bytes in `[src.position(), src.limit())` using a local index — it does **not** 
call `ChunkBuffer.findCurrent()` and does **not** stop after `data.remaining()` 
bytes.
   
   That is fine **when callers have prepared the list for reading**, so that:
   
   1. `data.remaining()` equals the sum of `(limit − position)` over all 
underlying buffers, and
   2. those ranges are exactly the logical byte stream the checksum should 
cover.
   
   ### Why this matters
   
   `ChunkBuffer.remaining()` is a **global** notion (e.g. `limit − position()` 
on `IncrementalChunkBuffer`, or the same on a wrapped single buffer). The 
checksum walk uses **per-buffer** `position`/`limit`. Correctness requires both 
views to describe the same bytes.
   
   When that holds (typical read paths):
   
   - Fully consumed segments have `position == limit` → the walk skips them (0 
bytes).
   - Unread segments expose valid data from `position` up to `limit`.
   - Window straddling is handled by feeding slices incrementally; CRC/digest 
associativity makes that equivalent to the old `iterate()` linearization.
   
   When it does **not** hold — e.g. an `IncrementalChunkBuffer` still in 
**write** layout where a full segment has `position` at the write head — 
`[position, limit)` can point past the written data while `remaining()` still 
reports the full logical length. The walk would feed the wrong bytes (or the 
wrong total), without necessarily throwing. (The old `iterate()` path on 
`IncrementalChunkBuffer` also returned raw `asByteBufferList()`, so this 
contract predates this PR; the direct walk just makes the assumption more 
explicit.)
   
   ### Current production callers satisfy this
   
   | Path | Preparation |
   |------|-------------|
   | `BlockOutputStream.createChunkInfo` | `lastChunkBuffer.flip()` before 
`computeChecksum(..., true)` |
   | `ChunkUtils.readData` | `Arrays.stream(buffers).forEach(ByteBuffer::flip)` 
after read |
   | `KeyValueHandler.validateChunkChecksumData` (`ChunkBuffer`) | 
`b.duplicate(b.position(), b.limit())` |
   | Protobuf / single contiguous buffers | Already read-ready (`position=0`, 
`limit=len`) |
   
   ### Suggestion
   
   Consider an `@implNote` on `computeChecksum(ChunkBuffer)` (and/or a debug 
assert that walked bytes `== data.remaining()`) so future callers do not pass a 
partially written multi-buffer without `flip()`/`duplicate()` first.
   
   Tests (`TestChecksumMultiBuffer`, growing/positioned multi-buffer cache 
tests) validate the read-ready case well; they do not exercise live 
write-layout incremental buffers, which production avoids anyway.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to