Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "envoy/upstream/load_balancer.h"

#include "source/common/http/header_utility.h"
#include "source/common/router/metadatamatchcriteria_impl.h"
#include "source/common/upstream/load_balancer_impl.h"
#include "source/extensions/filters/network/thrift_proxy/conn_manager.h"
#include "source/extensions/filters/network/thrift_proxy/filters/filter.h"
Expand Down Expand Up @@ -271,7 +272,23 @@ class Router : public Tcp::ConnectionPool::UpstreamCallbacks,
// Upstream::LoadBalancerContext
const Network::Connection* downstreamConnection() const override;
const Envoy::Router::MetadataMatchCriteria* metadataMatchCriteria() override {
return route_entry_ ? route_entry_->metadataMatchCriteria() : nullptr;
const Envoy::Router::MetadataMatchCriteria* route_criteria =
(route_entry_ != nullptr) ? route_entry_->metadataMatchCriteria() : nullptr;

// Support request subset lb in thrift
Comment thread
xiaoma2015 marked this conversation as resolved.
Outdated
const auto& request_metadata = callbacks_->streamInfo().dynamicMetadata().filter_metadata();
const auto filter_it = request_metadata.find(Envoy::Config::MetadataFilters::get().ENVOY_LB);

if (filter_it != request_metadata.end() && route_criteria != nullptr) {
metadata_match_criteria_ = route_criteria->mergeMatchCriteria(filter_it->second);
} else if (filter_it != request_metadata.end()) {
metadata_match_criteria_ =
std::make_unique<Envoy::Router::MetadataMatchCriteriaImpl>(filter_it->second);
} else {
return route_criteria;
Comment thread
xiaoma2015 marked this conversation as resolved.
Outdated
}

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.

May be following form can make this branches more clear:

if (filter_it != request_metadata.end()) {
  metadata_match_criteria_ = route_criteria != nullptr ? ... : ...;
  // or
  // if (route_criteria != nullptr) { .... }
} else {
  return route_criteria;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

got it


return metadata_match_criteria_.get();

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.

we can simplify this a bit:

    if (filter_it == request_metadata.end()) {
      return route_criteria;
    }

    metadata_match_criteria_ = route_criteria != nullptr ? route_criteria->mergeMatchCriteria(filter_it->second) : std::make_unique<Envoy::Router::MetadataMatchCriteriaImpl>(filter_it->second);
    return metadata_match_criteria_.get();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

using ifelse is better?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

thx for review
using ifelse is better?

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.

Yeah looks great -- I am just a fan of the ternary operator 😁.

}

// Tcp::ConnectionPool::UpstreamCallbacks
Expand All @@ -287,6 +304,7 @@ class Router : public Tcp::ConnectionPool::UpstreamCallbacks,
std::unique_ptr<UpstreamResponseCallbacksImpl> upstream_response_callbacks_{};
RouteConstSharedPtr route_{};
const RouteEntry* route_entry_{};
Envoy::Router::MetadataMatchCriteriaConstPtr metadata_match_criteria_;

std::unique_ptr<UpstreamRequest> upstream_request_;
Buffer::OwnedImpl upstream_request_buffer_;
Expand Down
43 changes: 43 additions & 0 deletions test/extensions/filters/network/thrift_proxy/router_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,35 @@ class ThriftRouterTestBase {
metadata_->setSequenceId(sequence_id);
}

void verifyMetadataMatchCriteriaFromRequest(bool route_entry_has_match) {
ProtobufWkt::Struct request_struct, route_struct;
Comment thread
xiaoma2015 marked this conversation as resolved.
Outdated
ProtobufWkt::Value val;

// Populate metadata like StreamInfo.setDynamicMetadata() would.
auto& fields_map = *request_struct.mutable_fields();
val.set_string_value("v3.1");
fields_map["version"] = val;
val.set_string_value("devel");
fields_map["stage"] = val;
(*callbacks_.stream_info_.metadata_
.mutable_filter_metadata())[Envoy::Config::MetadataFilters::get().ENVOY_LB] =
request_struct;

// Populate route entry's metadata which will be overridden.
val.set_string_value("v3.0");
fields_map = *request_struct.mutable_fields();
fields_map["version"] = val;
Envoy::Router::MetadataMatchCriteriaImpl route_entry_matches(route_struct);

if (route_entry_has_match) {
ON_CALL(callbacks_.route_->route_entry_, metadataMatchCriteria())
.WillByDefault(Return(&route_entry_matches));
} else {
ON_CALL(callbacks_.route_->route_entry_, metadataMatchCriteria())
.WillByDefault(Return(nullptr));
}
Comment on lines +159 to +228

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.

So, where is the verify of metadata match criteria?

@xiaoma2015 xiaoma2015 Oct 12, 2021

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

test code coming soon

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

/updated

}

void startRequest(MessageType msg_type, std::string method = "method",
const bool strip_service_name = false,
const TransportType transport_type = TransportType::Framed,
Expand Down Expand Up @@ -697,6 +726,20 @@ TEST_F(ThriftRouterTest, NoCluster) {
EXPECT_EQ(1U, context_.scope().counterFromString("test.unknown_cluster").value());
}

TEST_F(ThriftRouterTest, MetadataMatchCriteriaFromRequest) {
Comment thread
xiaoma2015 marked this conversation as resolved.
initializeRouter();
initializeMetadata(MessageType::Call);

verifyMetadataMatchCriteriaFromRequest(true);
}

TEST_F(ThriftRouterTest, MetadataMatchCriteriaFromRequestNoRouteEntryMatch) {
Comment thread
xiaoma2015 marked this conversation as resolved.
initializeRouter();
initializeMetadata(MessageType::Call);

verifyMetadataMatchCriteriaFromRequest(false);
}

TEST_F(ThriftRouterTest, ClusterMaintenanceMode) {
initializeRouter();
initializeMetadata(MessageType::Call);
Expand Down