-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Kafka codec #4950
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
Merged
Kafka codec #4950
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
4ec6b33
WIP: Kafka codec
adamkotwasinski aa51c6b
Fix compile error after rebases
adamkotwasinski 074f868
Remove all request types except OffsetCommit v0..v1 (for review)
adamkotwasinski f341df7
Remove bytes buffers (for review - unused in these requests)
adamkotwasinski 12ece4f
Apply review fixes
adamkotwasinski 431a64c
Apply review fixes
adamkotwasinski ac5f851
Introduce composite deserializers for 2, 3, 4 delegates
adamkotwasinski 8cb67ee
Review fixes:
adamkotwasinski 7e3f54c
Apply review fixes:
adamkotwasinski d0534c3
Introduce generated code for Kafka requests:
adamkotwasinski db7763e
Add python tool for generating Kafka request classes; keep all versio…
adamkotwasinski 00aac5b
In case of parse errors, consume rest of request properly; apply clan…
adamkotwasinski 255f120
Merge remote-tracking branch 'envoy/master' into mergetest
adamkotwasinski bbf0e08
Fix formatting and clang-tidy
adamkotwasinski 76cc44f
Fix spelling
adamkotwasinski 6f86d76
Fixes after review: string_view used instead of raw pointers; documen…
adamkotwasinski 23c1b9d
Merge remote-tracking branch 'envoy/master' into HEAD
adamkotwasinski f369bfa
Fixes: spelling, clang-tidy, documentation, cleaning up internal API;…
adamkotwasinski 28641b4
Download whole Kafka specification; test fixes
adamkotwasinski 3b9e324
Activate kafka tests in builds; review fixes: documentation, formatting
adamkotwasinski 89c1a59
Attempt to point to Kafka codec in extension build; fix buffer overfl…
adamkotwasinski acdbcb3
Explicitly provide type of test values in generated Kafka tests
adamkotwasinski f5f5f32
Reorganize test code
adamkotwasinski 20c7bf1
Create separate test class for each of Kafka tests; add missing forma…
adamkotwasinski 81c97c8
Put Kafka tests in dedicated namespaces to avoid duplicate mock class…
adamkotwasinski 130d2ac
Merge remote-tracking branch 'envoy/master' into codec
adamkotwasinski d47c2c2
Kick CI
adamkotwasinski 145f28d
Kick CI
adamkotwasinski 11d9288
Kick CI
adamkotwasinski ebe2134
Merge remote-tracking branch 'envoy/master' into codec
adamkotwasinski 7f1abca
Put generated files in directories named 'external', so they do not g…
adamkotwasinski 36f76c7
Add missing test to NullableArrayDeserializer
adamkotwasinski 98c7752
Refactoring:
adamkotwasinski 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
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
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 |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| licenses(["notice"]) # Apache 2 | ||
|
|
||
| # Kafka network filter. | ||
| # Public docs: docs/root/configuration/network_filters/kafka_filter.rst | ||
|
|
||
| load( | ||
| "//bazel:envoy_build_system.bzl", | ||
| "envoy_cc_library", | ||
| "envoy_package", | ||
| ) | ||
|
|
||
| envoy_package() | ||
|
|
||
| envoy_cc_library( | ||
| name = "kafka_request_codec_lib", | ||
| srcs = ["request_codec.cc"], | ||
| hdrs = [ | ||
| "codec.h", | ||
| "request_codec.h", | ||
| ], | ||
| deps = [ | ||
| ":kafka_request_parser_lib", | ||
| "//source/common/buffer:buffer_lib", | ||
| ], | ||
| ) | ||
|
|
||
| envoy_cc_library( | ||
| name = "kafka_request_parser_lib", | ||
| srcs = [ | ||
| "external/kafka_request_resolver.cc", | ||
| "kafka_request_parser.cc", | ||
| ], | ||
| hdrs = [ | ||
| "external/requests.h", | ||
| "kafka_request_parser.h", | ||
| ], | ||
| deps = [ | ||
| ":kafka_request_lib", | ||
| ":parser_lib", | ||
| "//source/common/common:assert_lib", | ||
| "//source/common/common:minimal_logger_lib", | ||
| ], | ||
| ) | ||
|
|
||
| envoy_cc_library( | ||
| name = "kafka_request_lib", | ||
| srcs = [ | ||
| ], | ||
| hdrs = [ | ||
| "kafka_request.h", | ||
| ], | ||
| deps = [ | ||
| ":serialization_lib", | ||
| ], | ||
| ) | ||
|
|
||
| genrule( | ||
| name = "kafka_generated_source", | ||
| srcs = [ | ||
| "@kafka_source//:request_protocol_files", | ||
| ], | ||
| outs = [ | ||
| "external/requests.h", | ||
| "external/kafka_request_resolver.cc", | ||
| ], | ||
| cmd = """ | ||
| ./$(location :kafka_code_generator) generate-source \ | ||
| $(location external/requests.h) $(location external/kafka_request_resolver.cc) \ | ||
| $(SRCS) | ||
| """, | ||
| tools = [ | ||
| ":kafka_code_generator", | ||
| ], | ||
| ) | ||
|
|
||
| py_binary( | ||
| name = "kafka_code_generator", | ||
| srcs = ["protocol_code_generator/kafka_generator.py"], | ||
| data = glob(["protocol_code_generator/*.j2"]), | ||
| main = "protocol_code_generator/kafka_generator.py", | ||
| deps = ["@com_github_pallets_jinja//:jinja2"], | ||
| ) | ||
|
|
||
| envoy_cc_library( | ||
| name = "parser_lib", | ||
| hdrs = ["parser.h"], | ||
| deps = [ | ||
| "//source/common/common:minimal_logger_lib", | ||
| ], | ||
| ) | ||
|
|
||
| envoy_cc_library( | ||
| name = "serialization_lib", | ||
| hdrs = [ | ||
| "external/serialization_composite.h", | ||
| "serialization.h", | ||
| ], | ||
| deps = [ | ||
| ":kafka_types_lib", | ||
| "//include/envoy/buffer:buffer_interface", | ||
| "//source/common/common:byte_order_lib", | ||
| ], | ||
| ) | ||
|
|
||
| genrule( | ||
| name = "serialization_composite_generated_source", | ||
| srcs = [], | ||
| outs = [ | ||
| "external/serialization_composite.h", | ||
| ], | ||
| cmd = """ | ||
| ./$(location :serialization_composite_generator) generate-source \ | ||
| $(location external/serialization_composite.h) | ||
| """, | ||
| tools = [ | ||
| ":serialization_composite_generator", | ||
| ], | ||
| ) | ||
|
|
||
| py_binary( | ||
| name = "serialization_composite_generator", | ||
| srcs = ["serialization_code_generator/serialization_composite_generator.py"], | ||
| data = glob(["serialization_code_generator/*.j2"]), | ||
| main = "serialization_code_generator/serialization_composite_generator.py", | ||
| deps = ["@com_github_pallets_jinja//:jinja2"], | ||
| ) | ||
|
|
||
| envoy_cc_library( | ||
| name = "kafka_types_lib", | ||
| hdrs = [ | ||
| "kafka_types.h", | ||
| ], | ||
| external_deps = ["abseil_optional"], | ||
| deps = [ | ||
| "//source/common/common:macros", | ||
| ], | ||
| ) |
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 |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #pragma once | ||
|
|
||
| #include "envoy/buffer/buffer.h" | ||
| #include "envoy/common/pure.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Extensions { | ||
| namespace NetworkFilters { | ||
| namespace Kafka { | ||
|
|
||
| /** | ||
| * Kafka message decoder. | ||
| */ | ||
| class MessageDecoder { | ||
| public: | ||
| virtual ~MessageDecoder() = default; | ||
|
|
||
| /** | ||
| * Processes given buffer attempting to decode messages contained within. | ||
| * @param data buffer instance. | ||
| */ | ||
| virtual void onData(Buffer::Instance& data) PURE; | ||
| }; | ||
|
|
||
| /** | ||
| * Kafka message encoder. | ||
| * @param MessageType encoded message type (request or response). | ||
| */ | ||
| template <typename MessageType> class MessageEncoder { | ||
| public: | ||
| virtual ~MessageEncoder() = default; | ||
|
|
||
| /** | ||
| * Encodes given message. | ||
| * @param message message to be encoded. | ||
| */ | ||
| virtual void encode(const MessageType& message) PURE; | ||
| }; | ||
|
|
||
| } // namespace Kafka | ||
| } // namespace NetworkFilters | ||
| } // namespace Extensions | ||
| } // namespace Envoy | ||
112 changes: 112 additions & 0 deletions
112
source/extensions/filters/network/kafka/kafka_request.h
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 |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| #pragma once | ||
|
|
||
| #include "envoy/common/exception.h" | ||
|
|
||
| #include "extensions/filters/network/kafka/external/serialization_composite.h" | ||
| #include "extensions/filters/network/kafka/serialization.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Extensions { | ||
| namespace NetworkFilters { | ||
| namespace Kafka { | ||
|
|
||
| /** | ||
| * Represents fields that are present in every Kafka request message. | ||
| * @see http://kafka.apache.org/protocol.html#protocol_messages | ||
| */ | ||
| struct RequestHeader { | ||
mattklein123 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| int16_t api_key_; | ||
| int16_t api_version_; | ||
| int32_t correlation_id_; | ||
| NullableString client_id_; | ||
|
|
||
| bool operator==(const RequestHeader& rhs) const { | ||
| return api_key_ == rhs.api_key_ && api_version_ == rhs.api_version_ && | ||
| correlation_id_ == rhs.correlation_id_ && client_id_ == rhs.client_id_; | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Carries information that could be extracted during the failed parse. | ||
| */ | ||
| class RequestParseFailure { | ||
| public: | ||
| RequestParseFailure(const RequestHeader& request_header) : request_header_{request_header} {}; | ||
|
|
||
| /** | ||
| * Request's header. | ||
| */ | ||
| const RequestHeader request_header_; | ||
| }; | ||
|
|
||
| typedef std::shared_ptr<RequestParseFailure> RequestParseFailureSharedPtr; | ||
|
|
||
| /** | ||
| * Abstract Kafka request. | ||
| * Contains data present in every request (the header with request key, version, etc.). | ||
| * @see http://kafka.apache.org/protocol.html#protocol_messages | ||
| */ | ||
| class AbstractRequest { | ||
| public: | ||
| virtual ~AbstractRequest() = default; | ||
|
|
||
| /** | ||
| * Constructs a request with given header data. | ||
| * @param request_header request's header. | ||
| */ | ||
| AbstractRequest(const RequestHeader& request_header) : request_header_{request_header} {}; | ||
|
|
||
| /** | ||
| * Encode the contents of this message into a given buffer. | ||
| * @param dst buffer instance to keep serialized message | ||
| */ | ||
| virtual size_t encode(Buffer::Instance& dst) const PURE; | ||
|
|
||
| /** | ||
| * Request's header. | ||
| */ | ||
| const RequestHeader request_header_; | ||
| }; | ||
|
|
||
| typedef std::shared_ptr<AbstractRequest> AbstractRequestSharedPtr; | ||
|
|
||
| /** | ||
| * Concrete request that carries data particular to given request type. | ||
| * @param Data concrete request data type. | ||
| */ | ||
| template <typename Data> class Request : public AbstractRequest { | ||
| public: | ||
| /** | ||
| * Request header fields need to be initialized by user in case of newly created requests. | ||
| */ | ||
| Request(const RequestHeader& request_header, const Data& data) | ||
| : AbstractRequest{request_header}, data_{data} {}; | ||
|
|
||
| /** | ||
| * Encodes given request into a buffer, with any extra configuration carried by the context. | ||
| */ | ||
| size_t encode(Buffer::Instance& dst) const override { | ||
| EncodingContext context{request_header_.api_version_}; | ||
| size_t written{0}; | ||
| // Encode request header. | ||
| written += context.encode(request_header_.api_key_, dst); | ||
| written += context.encode(request_header_.api_version_, dst); | ||
| written += context.encode(request_header_.correlation_id_, dst); | ||
| written += context.encode(request_header_.client_id_, dst); | ||
| // Encode request-specific data. | ||
| written += context.encode(data_, dst); | ||
| return written; | ||
| } | ||
|
|
||
| bool operator==(const Request<Data>& rhs) const { | ||
| return request_header_ == rhs.request_header_ && data_ == rhs.data_; | ||
| }; | ||
|
|
||
| private: | ||
| const Data data_; | ||
| }; | ||
|
|
||
| } // namespace Kafka | ||
| } // namespace NetworkFilters | ||
| } // namespace Extensions | ||
| } // namespace Envoy | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.