-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Kafka codec: precompute request size before serialization, so we do n… #6862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mattklein123
merged 2 commits into
envoyproxy:master
from
adamkotwasinski:kafka-serializer-improvements
May 10, 2019
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,7 +57,13 @@ class AbstractRequest { | |
| AbstractRequest(const RequestHeader& request_header) : request_header_{request_header} {}; | ||
|
|
||
| /** | ||
| * Encode the contents of this message into a given buffer. | ||
| * Computes the size of this request, if it were to be serialized. | ||
| * @return serialized size of request | ||
| */ | ||
| virtual size_t computeSize() const PURE; | ||
|
|
||
| /** | ||
| * Encode the contents of this request into a given buffer. | ||
| * @param dst buffer instance to keep serialized message | ||
| */ | ||
| virtual size_t encode(Buffer::Instance& dst) const PURE; | ||
|
|
@@ -82,6 +88,22 @@ template <typename Data> class Request : public AbstractRequest { | |
| Request(const RequestHeader& request_header, const Data& data) | ||
| : AbstractRequest{request_header}, data_{data} {}; | ||
|
|
||
| /** | ||
| * Compute the size of request, what includes both the request header and its real data. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/what/which ? |
||
| */ | ||
| size_t computeSize() const override { | ||
| const EncodingContext context{request_header_.api_version_}; | ||
| size_t result = 0; | ||
| // Compute size of header. | ||
| result += context.computeSize(request_header_.api_key_); | ||
| result += context.computeSize(request_header_.api_version_); | ||
| result += context.computeSize(request_header_.correlation_id_); | ||
| result += context.computeSize(request_header_.client_id_); | ||
| // Compute size of request data. | ||
| result += context.computeSize(data_); | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Encodes given request into a buffer, with any extra configuration carried by the context. | ||
| */ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,14 +80,9 @@ void RequestDecoder::doParse(const Buffer::RawSlice& slice) { | |
| } | ||
|
|
||
| void RequestEncoder::encode(const AbstractRequest& message) { | ||
| Buffer::OwnedImpl data_buffer; | ||
| // TODO(adamkotwasinski) Precompute the size instead of using temporary buffer. | ||
| // When we have the 'computeSize' method, then we can push encoding request's size into | ||
| // Request::encode | ||
| int32_t data_len = message.encode(data_buffer); // Encode data and compute data length. | ||
| EncodingContext encoder{-1}; | ||
| encoder.encode(data_len, output_); // Encode data length into result. | ||
| output_.add(data_buffer); // Copy encoded data into result. | ||
| const int32_t size = htobe32(message.computeSize()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unsigned? Same elsewhere where applicable. |
||
| output_.add(&size, sizeof(size)); // Encode data length. | ||
| message.encode(output_); // Encode data. | ||
| } | ||
|
|
||
| } // namespace Kafka | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer explicit types.
uint64_toruint32_t? Same elsewhere.