-
Notifications
You must be signed in to change notification settings - Fork 5.3k
kafka: add support for metadata request in mesh-filter #17597
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 6 commits into
envoyproxy:main
from
adamkotwasinski:kafka-mesh-metadata
Aug 17, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1ce062b
kafka: add support for metadata request in mesh-filter
adamkotwasinski 643c8ff
Missing includes
adamkotwasinski 98a5a35
More includes
adamkotwasinski eb6cf25
Remove waiting tag
adamkotwasinski ea1a383
Coverage
adamkotwasinski d671966
Remove waiting tag
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
66 changes: 66 additions & 0 deletions
66
source/extensions/filters/network/kafka/mesh/command_handlers/metadata.cc
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,66 @@ | ||
| #include "source/extensions/filters/network/kafka/mesh/command_handlers/metadata.h" | ||
|
|
||
| #include "source/extensions/filters/network/kafka/external/responses.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Extensions { | ||
| namespace NetworkFilters { | ||
| namespace Kafka { | ||
| namespace Mesh { | ||
|
|
||
| MetadataRequestHolder::MetadataRequestHolder( | ||
| AbstractRequestListener& filter, const UpstreamKafkaConfiguration& configuration, | ||
| const std::shared_ptr<Request<MetadataRequest>> request) | ||
| : BaseInFlightRequest{filter}, configuration_{configuration}, request_{request} {} | ||
|
|
||
| // Metadata requests are immediately ready for answer (as they do not need to reach upstream). | ||
| void MetadataRequestHolder::startProcessing() { notifyFilter(); } | ||
|
|
||
| bool MetadataRequestHolder::finished() const { return true; } | ||
|
|
||
| constexpr int32_t ENVOY_BROKER_ID = 0; | ||
| constexpr int32_t NO_ERROR = 0; | ||
|
|
||
| // Cornerstone of how the mesh-filter actually works. | ||
| // We pretend to be one-node Kafka cluster, with Envoy instance being the only member. | ||
| // What means all the Kafka future traffic will go through this instance. | ||
| AbstractResponseSharedPtr MetadataRequestHolder::computeAnswer() const { | ||
| const auto& header = request_->request_header_; | ||
| const ResponseMetadata metadata = {header.api_key_, header.api_version_, header.correlation_id_}; | ||
|
|
||
| const auto advertised_address = configuration_.getAdvertisedAddress(); | ||
| MetadataResponseBroker broker = {ENVOY_BROKER_ID, advertised_address.first, | ||
| advertised_address.second}; | ||
| std::vector<MetadataResponseTopic> response_topics; | ||
| if (request_->data_.topics_) { | ||
| for (const auto& topic : *(request_->data_.topics_)) { | ||
| const std::string& topic_name = topic.name_; | ||
| std::vector<MetadataResponsePartition> topic_partitions; | ||
| const absl::optional<ClusterConfig> cluster_config = | ||
| configuration_.computeClusterConfigForTopic(topic_name); | ||
| if (!cluster_config) { | ||
| // Someone is requesting topics that are not known to our configuration. | ||
| // So we do not attach any metadata, this will cause clients failures downstream as they | ||
| // will never be able to get metadata for these topics. | ||
| continue; | ||
| } | ||
| for (int32_t partition_id = 0; partition_id < cluster_config->partition_count_; | ||
| ++partition_id) { | ||
| // Every partition is hosted by this proxy-broker. | ||
| MetadataResponsePartition partition = { | ||
| NO_ERROR, partition_id, broker.node_id_, {broker.node_id_}, {broker.node_id_}}; | ||
| topic_partitions.push_back(partition); | ||
| } | ||
| MetadataResponseTopic response_topic = {NO_ERROR, topic_name, false, topic_partitions}; | ||
| response_topics.push_back(response_topic); | ||
| } | ||
| } | ||
| MetadataResponse data = {{broker}, broker.node_id_, response_topics}; | ||
| return std::make_shared<Response<MetadataResponse>>(metadata, data); | ||
| } | ||
|
|
||
| } // namespace Mesh | ||
| } // namespace Kafka | ||
| } // namespace NetworkFilters | ||
| } // namespace Extensions | ||
| } // namespace Envoy |
37 changes: 37 additions & 0 deletions
37
source/extensions/filters/network/kafka/mesh/command_handlers/metadata.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,37 @@ | ||
| #pragma once | ||
|
|
||
| #include "source/extensions/filters/network/kafka/external/requests.h" | ||
| #include "source/extensions/filters/network/kafka/mesh/abstract_command.h" | ||
| #include "source/extensions/filters/network/kafka/mesh/upstream_config.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Extensions { | ||
| namespace NetworkFilters { | ||
| namespace Kafka { | ||
| namespace Mesh { | ||
|
|
||
| class MetadataRequestHolder : public BaseInFlightRequest { | ||
| public: | ||
| MetadataRequestHolder(AbstractRequestListener& filter, | ||
| const UpstreamKafkaConfiguration& configuration, | ||
| const std::shared_ptr<Request<MetadataRequest>> request); | ||
|
|
||
| void startProcessing() override; | ||
|
|
||
| bool finished() const override; | ||
|
|
||
| AbstractResponseSharedPtr computeAnswer() const override; | ||
|
|
||
| private: | ||
| // Configuration used to provide data for response. | ||
| const UpstreamKafkaConfiguration& configuration_; | ||
|
|
||
| // Original request. | ||
| const std::shared_ptr<Request<MetadataRequest>> request_; | ||
| }; | ||
|
|
||
| } // namespace Mesh | ||
| } // namespace Kafka | ||
| } // namespace NetworkFilters | ||
| } // namespace Extensions | ||
| } // namespace Envoy |
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
56 changes: 56 additions & 0 deletions
56
source/extensions/filters/network/kafka/mesh/upstream_config.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,56 @@ | ||
| #pragma once | ||
|
|
||
| #include <map> | ||
| #include <memory> | ||
| #include <string> | ||
| #include <utility> | ||
|
|
||
| #include "envoy/common/pure.h" | ||
|
|
||
| #include "absl/types/optional.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Extensions { | ||
| namespace NetworkFilters { | ||
| namespace Kafka { | ||
| namespace Mesh { | ||
|
|
||
| // Minor helper structure that contains information about upstream Kafka clusters. | ||
| struct ClusterConfig { | ||
|
|
||
| // Cluster name, as it appears in configuration input. | ||
| std::string name_; | ||
|
|
||
| // How many partitions do we expect for every one of the topics present in given upstream cluster. | ||
| // Impl note: this could be replaced with creating (shared?) AdminClient and having it reach out | ||
| // upstream to get configuration (or we could just send a correct request via codec). The response | ||
| // would need to be cached (as this data is frequently requested). | ||
| int32_t partition_count_; | ||
|
|
||
| // The configuration that will be passed to upstream client for given cluster. | ||
| // This allows us to reference different clusters with different configs (e.g. linger.ms). | ||
| // This map always contains entry with key 'bootstrap.servers', as this is the only mandatory | ||
| // producer property. | ||
| std::map<std::string, std::string> upstream_producer_properties_; | ||
| }; | ||
|
|
||
| /** | ||
| * Keeps the configuration related to upstream Kafka clusters. | ||
| * Impl note: current matching from topic to cluster is based on prefix matching but more complex | ||
| * rules could be added. | ||
| */ | ||
| class UpstreamKafkaConfiguration { | ||
| public: | ||
| virtual ~UpstreamKafkaConfiguration() = default; | ||
| virtual absl::optional<ClusterConfig> | ||
| computeClusterConfigForTopic(const std::string& topic) const PURE; | ||
| virtual std::pair<std::string, int32_t> getAdvertisedAddress() const PURE; | ||
| }; | ||
|
|
||
| using UpstreamKafkaConfigurationSharedPtr = std::shared_ptr<const UpstreamKafkaConfiguration>; | ||
|
|
||
| } // namespace Mesh | ||
| } // namespace Kafka | ||
| } // namespace NetworkFilters | ||
| } // namespace Extensions | ||
| } // namespace Envoy | ||
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
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.
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.
Instances of this object will come from parsing config https://github.com/envoyproxy/envoy/pull/11936/files#diff-61bfe914c466f4da651a8940ca523c8517e792d0d498f606517c8f3ae9e06148R18
(the constructor call in https://github.com/envoyproxy/envoy/pull/11936/files#diff-925e5a6d6596e3c3dfe8145c80fa2c6ba955068b2c0a911351fa1cd911eda77eR19)