Andrey Yarovoy created HDDS-17805:
-------------------------------------

             Summary: Abandoned in-flight write requests can resend client 
buffer memory that was reused or released
                 Key: HDDS-17805
                 URL: https://issues.apache.org/jira/browse/HDDS-17805
             Project: Apache Ozone
          Issue Type: Bug
          Components: Ozone Client
            Reporter: Andrey Yarovoy
            Assignee: Andrey Yarovoy


With \{{ozone.UnsafeByteOperations.enabled=true}} (the default), 
\{{BlockOutputStream#writeChunkToContainer}} sends 
\{{chunk.toByteString(bufferPool.byteStringConversion())}}, a zero-copy 
\{{ByteString}} that wraps the pooled direct memory of the \{{BufferPool}} 
buffer. The request keeps referencing that memory until the request is dropped, 
not only until its reply arrives.

On the normal path this is safe. A buffer is reused only after 
\{{watchForCommit}} shows that the requests are committed 
(\{{CommitWatcher#releaseBuffers}}, 
\{{AbstractCommitWatcher#releaseBuffersOnException}} up to the min replicated 
index). It is released to Netty only in \{{BufferPool#clearBufferPool}}, after 
\{{close()}} has waited for the last commit.

On the failure path it is not:
# A pipeline fails, for example with a timeout. 
\{{KeyOutputStream#handleExceptionInternal}} keeps the acknowledged length of 
the block, writes the unacknowledged data again from the same buffers to a new 
block, and cleans up the old entry with \{{invalidateClient = retryFailure}}. 
That is true only for \{{RaftRetryFailureException}}.
# So for other failures the old pipeline's \{{XceiverClientRatis}} stays open 
in the \{{XceiverClientManager}} cache, shared with other streams, and its 
\{{RaftClient}} can keep retrying the pending write requests.
# Once the retry is committed on the new pipeline, the buffers are cleared and 
reused for new data. At \{{close()}} they are released to Netty, which can hand 
the memory to any other \{{CodecBuffer}} in the JVM.
# A late resend of an old request then carries different bytes under the 
original chunk's \{{ChecksumData}}.

h3. Impact

- The Ratis retry cache keeps a request that was already applied from being 
applied again. A request that was not applied yet is beyond the acknowledged 
length kept for the block, so the bad bytes land only beyond the committed 
length of an abandoned or truncated replica. Readers are bounded by the OM 
length, so key data is not affected.
- It is the same "replica has more than the committed length" state as 
HDDS-16974: any code that trusts a replica's chunk list instead of the 
committed length sees it.
- Datanodes do not detect it: \{{hdds.datanode.chunk.data.validation.check}} is 
\{{false}} by default, so the chunk is written without verifying its checksum.
- The client reads memory it no longer owns, which is a use after release by 
definition. HDDS-17212 adds fail-fast checks for stale \{{CodecBuffer}} and 
\{{ChunkBuffer}} references, but a \{{ByteString}} already handed to a request 
cannot be checked.

h3. Options

*A. Invalidate the abandoned pipeline's client.* Clean up an abandoned block's 
entry with \{{invalidateClient=true}} for every failure, not only 
\{{RaftRetryFailureException}}. Closing the \{{RaftClient}} fails its pending 
requests and stops resends before the buffers are reused.
- Pro: a small change in \{{KeyOutputStream}} and \{{ECKeyOutputStream}} 
exception handling.
- Con: the client is cached and shared. Invalidating it fails in-flight 
requests of other streams on the same pipeline, which then go through their own 
retry handling. It needs a mini-cluster test with concurrent writers.

*B. Ownership of buffer memory per request.* Retain the \{{ByteBuf}} of the 
\{{CodecBuffer}} for each request that wraps its memory, and release it when 
the reply future completes or the client is closed. \{{BufferPool}} reuses or 
releases a buffer only once no request references it.
- Pro: correct for every path, including future ones, with no effect on other 
streams.
- Con: invasive. It touches the \{{ByteString}} conversion, 
\{{BlockOutputStream}} and \{{ECBlockOutputStream}} futures, \{{CommitWatcher}} 
and \{{BufferPool}}. It also needs a guarantee from Ratis about when a request 
is dropped.

*C. Copy on retry.* Use \{{ByteStringConversion#safeWrap}} for the data written 
again to the new block, and keep the old buffers untouched until the old 
requests complete or time out.
- Con: a copy on the failure path only, but buffers stay pinned for up to the 
Ratis request timeout.

h3. Suggested approach

Start with A, guarded by a test with two streams sharing a pipeline, and 
measure the effect on the other streams. If that is not acceptable, B is the 
complete fix. Enabling \{{hdds.datanode.chunk.data.validation.check}} makes the 
issue detectable in the meantime, at a datanode CPU cost.

h3. References

- \{{hadoop-hdds/client/.../BlockOutputStream.java}}: 
\{{writeChunkToContainer}}, the zero-copy \{{ByteString}}.
- \{{hadoop-hdds/client/.../CommitWatcher.java}}: \{{releaseBuffers}}, reuse 
after the commit.
- \{{hadoop-ozone/client/.../KeyOutputStream.java}}: 
\{{handleExceptionInternal}}, \{{streamEntry.cleanup(retryFailure)}}.
- Related: HDDS-17212 (fail-fast use-after-release checks), HDDS-16974 (replica 
listing data beyond the committed length).



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

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

Reply via email to