yandrey321 opened a new pull request, #12050:
URL: https://github.com/apache/ozone/pull/12050

   ## What changes were proposed in this pull request?
   
   **Problem.** `KeyOutputStream.close()` sometimes failed with 
`IllegalStateException: refCnt: expected == 0 but computed
   == 1` from `CodecBuffer.release()`, after the key was already committed. On 
a 12-datanode secure cluster it failed 8
   TestDFSIO jobs over 7 runs (Ozone 2.2.0 and 2.3.0). Every failure was on a 
RATIS/THREE bucket, while TestDFSIO wrote
   its 250–1000 control files from a thread pool.
   
   The cause is the check itself. `release()` called `buf.release()` and, when 
that dropped the last reference, read
   `buf.refCnt()` to assert it was 0. But once the last reference is gone, 
Netty's pooled allocator recycles the
   `PooledByteBuf` object, and another thread can allocate the same object 
right away, which sets its refCnt back to 1.
   The check then read the new owner's count. It's not a double release: a 
standalone program that allocates direct
   `CodecBuffer`s on some threads and releases each exactly once on others 
reproduces the exact error (11 times in 74
   million releases, 8 thread pairs, 20 s).
   
   A failing close also leaked the rest of the stream's buffers: 
`BufferPool.clearBufferPool()` stopped at the first
   exception, so the remaining direct buffers were never released, and the pool 
was never emptied.
   
   **Implementation idea.**
   - `CodecBuffer.release()` reads `refCnt()` before `buf.release()`, while 
this buffer still owns the object, and
     asserts that it was 1 when the release frees the buffer. Nothing reads the 
`ByteBuf` after the last release.
   - `BufferPool.clearBufferPool()` closes every buffer even if some fail, 
always empties the pool, and throws the first
     failure with the later ones suppressed. The loop is shared as 
`BufferUtils.closeAll`.
   - The same applies to the rest of the close path, so a failing buffer no 
longer leaks other resources:
     - `IncrementalChunkBuffer.close()` releases every underlying buffer (used 
with
       `ozone.client.stream.buffer.increment` > 0).
     - `BlockOutputStreamEntryPool.cleanup()` clears its stream entries even if 
the buffer pool fails to clear.
     - `ECKeyOutputStream.close()` cleans up the pool even if closing the 
current entry fails (its `markStreamClosed()`
       does not clean up, unlike `KeyOutputStream`), and always releases the 
encoder's native resources.
   - A use after release now fails instead of accessing memory the pool may 
have given to another owner, whose refCnt is
     also 1:
     - `CodecBuffer` accessors throw once the buffer is released. A zero 
capacity buffer, possibly the singleton, may
       still be used, as before.
     - `ChunkBufferImplWithByteBuffer` and `IncrementalChunkBuffer` throw on 
any access to their memory once closed,
       including through a `duplicate()`, which shares the release state of the 
buffer it came from. A wrapped
       `ByteBuffer` that the chunk buffer does not own is not affected.
   
   **Key decisions.**
   - **Keep the check, and move it before the release.** `release()` already 
requires a count of 1: otherwise its
     `else` branch fails for any non-empty buffer. So the earlier read checks 
the same thing, without the race. A double
     release of the same `CodecBuffer` is still caught by its `released` future.
   - **`close()` still fails if a buffer fails to close.** The Jira also 
suggested logging buffer-cleanup failures after
     the commit instead. That would hide cleanup bugs from callers, and with 
the race fixed there's no known failure left
     to tolerate.
   
   Co-authored-by: Claude Opus 5.5 <[email protected]>
   
   ## What is the link to the Apache JIRA
   
   https://issues.apache.org/jira/browse/HDDS-17212
   
   ## How was this patch tested?
   
   Unit + integration tests.
   
   - **Race reproduction** (standalone, not included): allocate direct 
`CodecBuffer`s on 8 threads and release each once
     on 8 others, for 20 s.
     | code | releases | failures |
     |---|---|---|
     | before | 74 million | 11 |
     | after, 2 runs | 146 million | 0 |
   
   
   


-- 
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