Skip to content

MINOR: Remove connection id from Send and consolidate request/message utils#9714

Merged
ijuma merged 8 commits intoapache:trunkfrom
ijuma:remove-connection-id-from-send
Dec 9, 2020
Merged

MINOR: Remove connection id from Send and consolidate request/message utils#9714
ijuma merged 8 commits intoapache:trunkfrom
ijuma:remove-connection-id-from-send

Conversation

@ijuma
Copy link
Copy Markdown
Member

@ijuma ijuma commented Dec 8, 2020

Connection id is now only present in NetworkSend, which is now
the class used by Selector/NetworkClient/KafkaChannel (which
works well since NetworkReceive is the class used for
received data).

The previous NetworkSend was also responsible for adding a size
prefix. This logic is already present in SendBuilder, but for the
minority of cases where SendBuilder is not used (including
a number of tests), we now have ByteBufferSend.sizePrefixed().

With regards to the request/message utilities:

  • Renamed toByteBuffer/toBytes in MessageUtil to
    toVersionPrefixedByteBuffer/toVersionPrefixedBytes for clarity.
  • Introduced new MessageUtil.toByteBuffer that does not include
    the version as the prefix.
  • Renamed serializeBody in AbstractRequest/Response to
    serialize for symmetry with parse.
  • Introduced RequestTestUtils and moved relevant methods from
    TestUtils.
  • Moved serializeWithHeader methods that were only used in
    tests to RequestTestUtils.
  • Deleted MessageTestUtil.

Finally, a couple of changes to simplify coding patterns:

  • Added flip() and buffer() to ByteBufferAccessor.
  • Added MessageSizeAccumulator.sizeExcludingZeroCopy.
  • Used lambdas instead of TestCondition.
  • Used Arrays.copyOf instead of System.arraycopy in MessageUtil.

Committer Checklist (excluded from commit message)

  • Verify design and implementation
  • Verify test coverage and CI build status
  • Verify documentation (including upgrade notes)

@ijuma ijuma force-pushed the remove-connection-id-from-send branch from 23ebe94 to 87db298 Compare December 8, 2020 20:32
@ijuma ijuma force-pushed the remove-connection-id-from-send branch from 87db298 to 9b849c4 Compare December 8, 2020 23:50
@ijuma ijuma marked this pull request as ready for review December 9, 2020 00:39
@ijuma ijuma changed the title MINOR: Remove connection id from Send and consolidate serialization utils MINOR: Remove connection id from Send and consolidate request/message utils Dec 9, 2020
@ijuma ijuma requested a review from chia7712 December 9, 2020 03:49
Copy link
Copy Markdown
Member

@chia7712 chia7712 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ijuma Thanks for this great cleanup. Please take a look at following minor comments. Thanks!


int headerSize = header.size(cache, headerVersion);
int messageSize = apiMessage.size(cache, apiVersion);
ByteBufferAccessor writable = new ByteBufferAccessor(ByteBuffer.allocate(4 + headerSize + messageSize));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why it need 4 byte if it exclude size prefix?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch. This was a copy and paste bug and we had no tests verifying this. I have fixed it and added a test.

@@ -46,8 +45,7 @@ public class MultiRecordsSend implements Send {
* Construct a MultiRecordsSend for the given destination from a queue of Send objects. The queue will be
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment need to be updated.

@@ -24,11 +24,6 @@
*/
public interface Send {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove "destination" from the docs

// `openOrClosingChannel` can be None if the selector closed the connection because it was idle for too long
if (openOrClosingChannel(connectionId).isDefined) {
selector.send(responseSend)
selector.send(new NetworkSend(response.request.context.connectionId, responseSend))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line#955 already has connectionId.

handleChannelMuteEvent(send.destination, ChannelMuteEvent.RESPONSE_SENT)
tryUnmuteChannel(send.destination)
handleChannelMuteEvent(send.destinationId(), ChannelMuteEvent.RESPONSE_SENT)
tryUnmuteChannel(send.destinationId())
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about keeping "parameterless"? fewer changes and more clear.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the IDE added this when I renamed it or something. Fixed.

final int numNodes,
final Map<String, Errors> topicErrors,
final Map<String, Integer> topicPartitionCounts,
final short responseVersion) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused argument

Copy link
Copy Markdown
Member Author

@ijuma ijuma Dec 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a test bug from the previous PR. Will fix so that it's used.

public static ByteBuffer serializeRequestHeader(RequestHeader header) {
ObjectSerializationCache serializationCache = new ObjectSerializationCache();
ByteBuffer buffer = ByteBuffer.allocate(header.size(serializationCache));
header.write(buffer, serializationCache);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RequestHeader#write has only 2 usages and both of them are in test scope. It should be fine to remove RequestHeader#write from production.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's about conceptual integrity for the class. It should provide a mechanism for serialization that doesn't require reaching into its internal structures.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough

@ijuma
Copy link
Copy Markdown
Member Author

ijuma commented Dec 9, 2020

@chia7712 Thanks for the review, I'm particularly glad you caught the 4 wasted bytes. :) I pushed updates and left a comment in the one case I haven't addressed.

Copy link
Copy Markdown
Member

@chia7712 chia7712 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ijuma overall +1 and thanks for this great refactor. please take a look at last comment :)

/**
* A size delimited Send that consists of a 4 byte network-ordered size N followed by N bytes of content.
*/
public class SizeDelimitedSend extends ByteBufferSend {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about making SizeDelimitedSend be a static method in ByteBufferSend? For example:

    public static Send withSizeDelimited(ByteBuffer buffer) {
        ByteBuffer sizeBuffer = ByteBuffer.allocate(4);
        sizeBuffer.putInt(0, buffer.remaining());
        return new ByteBufferSend(sizeBuffer, buffer);
    }

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had the same thought and initially did that, but it's an issue for many tests since they verify the properties of the ByteBuffer and it is not accessible if we do this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but it's an issue for many tests since they verify the properties of the ByteBuffer and it is not accessible if we do this.

Can it be resolved if we return ByteBuffer rather than Send?

Copy link
Copy Markdown
Member Author

@ijuma ijuma Dec 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has to be a Send too. I looked at this a bit closer and we can just return ByteBufferSend from the static factory method. The thing I attempted before was to combine at the NetworkSend level and that doesn't work since NetworkSend can work with Sends that are not backed by a ByteBuffer. But it actually works fine at ByteBufferSend level. Thanks for the suggestion, please check the update.

@chia7712
Copy link
Copy Markdown
Member

chia7712 commented Dec 9, 2020

  • 1 to latest commit

Copy link
Copy Markdown
Contributor

@hachikuji hachikuji left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice cleanup! I didn't look too carefully, but it seems like a step in the right direction. Pulling destination out of Send is something I've wanted to do for some time.

@ijuma
Copy link
Copy Markdown
Member Author

ijuma commented Dec 9, 2020

Tests passed, merging to trunk.

@ijuma ijuma merged commit 1f98112 into apache:trunk Dec 9, 2020
@ijuma ijuma deleted the remove-connection-id-from-send branch December 9, 2020 19:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants