Repository navigation
core: Keep getByteBuffer() support when coalescing small buffers - #13108
Conversation
Since grpc#12924, CompositeReadableBuffer coalesces runs of small buffers into a byte array wrapped with ReadableBuffers.wrap(byte[]). That wrapper did not support getByteBuffer(), so once a message contained coalesced buffers, byteBufferSupported() returned false for the whole message and marshallers reading it through HasByteBuffer had to copy it. The byte array wrapper now returns its unread bytes as a ByteBuffer slice of the backing array, as Netty does for heap ByteBufs.
shivaspeaks
left a comment
There was a problem hiding this comment.
LGTM. Thanks for the contribution.
Or we could wrap the coalescedBytes with ByteBuffer before wrapping in ReadableBuffer, like ReadableBuffers.wrap(ByteBuffer.wrap(coalescedBytes)); in CompositeReadableBuffer.
|
I would prefer the alternative of |
Per review, leave ReadableBuffers.wrap(byte[]) as it was and fix only the coalescing. CompositeReadableBuffer now wraps the merged bytes with ReadableBuffers.wrap(ByteBuffer.wrap(...)), which supports getByteBuffer(), so merging small buffers no longer turns off byteBufferSupported().
|
Thanks @shivaspeaks and @kannanjgithub. Switched to |
Problem
Since grpc/grpc-java#12924 (also in 1.82.4, 1.83.1 and 1.84.x),
CompositeReadableBuffer.enqueueBuffer()merges runs of small (< 1 KiB) buffers. A merge happens when 1000 have queued, or when a buffer of at least 1 KiB follows two or more of them.coalesceTailSmallBuffers()wraps the merged bytes withReadableBuffers.wrap(byte[]), which doesn't supportgetByteBuffer().CompositeReadableBuffer.byteBufferSupported()requires every component to support it, so after a merge the message stream fromMessageDeframerreports noHasByteBufferaccess, even though Netty and Cronet frames support it.This affects custom marshallers that parse in place through
HasByteBuffer/Detachable(grpc/grpc-java#8102). They silently fall back to copying whenever a message picks up merged small frames. For example, in LightProto'sGrpcNettyTransportTest(streamnative/lightproto#48), a ~64 KiB response that starts with merged small buffers is copied instead of parsed in place on every run.Example
The large buffer triggers the merge:
Change
coalesceTailSmallBuffers()now wraps the merged bytes as aByteBuffer:ReadableBuffers.wrap(byte[])is unchanged. The merged array is new and never written again, so handing out views of it is safe for marshallers that detach and keep the buffers. The merged buffer reports ByteBuffer support even when its inputs didn't, so with OkHttp a message made up only of merged chunks now reports it too.Testing
CompositeReadableBufferTest: two small buffers before a 1 KiB buffer, and 1000 small buffers, all ByteBuffer-backed. Both tests check thatbyteBufferSupported()stays true after the merge and thatgetByteBuffer()returns the merged bytes in order. Both fail without the fix../gradlew -PskipCodegen=true -PskipAndroid=true :grpc-core:check(all grpc-core tests, checkstyle, animal-sniffer) passes on JDK 17.