dao-jun commented on PR #22760:
URL: https://github.com/apache/pulsar/pull/22760#issuecomment-2125646839

   Just add additional context to help understand it quickly.
   
   
[CopyingEncoder](https://github.com/apache/pulsar/blob/master/pulsar-common/src/main/java/org/apache/pulsar/common/protocol/ByteBufPair.java#L138-L158)
 is used to handle the case of `SslHandler`. Because `SslHandler` could compose 
ByteBufs to one(The first input ByteBuf). 
   For instance, we write `buf1`,`buf2`,`buf3` to the handler, it will write 
`buf2`,`buf3` to `buf1`. If we are enable `EntryCache`, this will pollute the 
entries. So we use `buf.copy` to make a new instance and copy the bytes to 
handle the case.
   
   However, `buf.copy` is not thread safe, multi thread copying will lead to 
data corruption.
   
   The solution is pass a `ReadOnlyByteBuf` to `SslHandler` to disable ByteBuf 
compose(write `buf2`,`buf3` to `buf1`), although netty considered the input 
ByteBuf can be not writable, but it seems there are some problems with the code:
   ```java
       private static boolean attemptCopyToCumulation(ByteBuf cumulation, 
ByteBuf next, int wrapDataSize) {
           final int inReadableBytes = next.readableBytes();
           final int cumulationCapacity = cumulation.capacity();
           if (wrapDataSize - cumulation.readableBytes() >= inReadableBytes &&
                   // Avoid using the same buffer if next's data would make 
cumulation exceed the wrapDataSize.
                   // Only copy if there is enough space available and the 
capacity is large enough, and attempt to
                   // resize if the capacity is small.
                   (cumulation.isWritable(inReadableBytes) && 
cumulationCapacity >= wrapDataSize ||
                           cumulationCapacity < wrapDataSize &&
                                   
ensureWritableSuccess(cumulation.ensureWritable(inReadableBytes, false)))) {
               cumulation.writeBytes(next);
               next.release();
               return true;
           }
           return false;
       }
   ```
   it should return false immediately if 
`cumulation.isWritable(inReadableBytes)` == false.
   Waiting the fix PR https://github.com/netty/netty/pull/14071 merged.
   


-- 
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: commits-unsubscr...@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to