Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 15 additions & 13 deletions source/common/upstream/subset_lb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,21 @@ void SubsetLoadBalancer::rebuildSingle() {
for (const auto& host_set : original_priority_set_.hostSetsPerPriority()) {
for (const auto& host : host_set->hosts()) {
MetadataConstSharedPtr metadata = host->metadata();
const auto& filter_metadata = metadata->filter_metadata();
auto filter_it = filter_metadata.find(Config::MetadataFilters::get().ENVOY_LB);
if (filter_it != filter_metadata.end()) {
const auto& fields = filter_it->second.fields();
auto fields_it = fields.find(single_key_);
if (fields_it != fields.end()) {
auto [iterator, did_insert] =
single_host_per_subset_map_.try_emplace(fields_it->second, host);
UNREFERENCED_PARAMETER(iterator);
if (!did_insert) {
// Two hosts with the same metadata value were found. Ignore all but one of them, and
// set a metric for how many times this happened.
collision_count++;
if (metadata != nullptr) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe do

if (metadata == nullptr) {
  continue;
}

to reduce the nesting?

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.

sure, will do.

const auto& filter_metadata = metadata->filter_metadata();
auto filter_it = filter_metadata.find(Config::MetadataFilters::get().ENVOY_LB);
if (filter_it != filter_metadata.end()) {
const auto& fields = filter_it->second.fields();
auto fields_it = fields.find(single_key_);
if (fields_it != fields.end()) {
auto [iterator, did_insert] =
single_host_per_subset_map_.try_emplace(fields_it->second, host);
UNREFERENCED_PARAMETER(iterator);
if (!did_insert) {
// Two hosts with the same metadata value were found. Ignore all but one of them, and
// set a metric for how many times this happened.
collision_count++;
}
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions test/integration/http_subset_lb_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,26 @@ TEST_P(HttpSubsetLbIntegrationTest, SubsetLoadBalancer) {
runTest(type_b_request_headers_, "b");
}

// Tests subset-compatible load balancer policy with empty metadata
Comment thread
asraa marked this conversation as resolved.
Outdated
TEST_P(HttpSubsetLbIntegrationTest, SubsetLoadBalancer_null_metadata) {
Comment thread
asraa marked this conversation as resolved.
Outdated
config_helper_.addConfigModifier([&](envoy::config::bootstrap::v3::Bootstrap& bootstrap) {
auto* static_resources = bootstrap.mutable_static_resources();
auto* cluster = static_resources->mutable_clusters(0);

// Set single_host_per_subset to be true
auto* subset_selector = cluster->mutable_lb_subset_config()->mutable_subset_selectors(0);
subset_selector->set_single_host_per_subset(true);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Seems like this crash is limited to when this option is enabled, could we be more explicit about this in the comments or test name? Maybe even add a test case where this is disabled for completeness sake.

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.

Yes, this crash only happens when single_host_per_subset is set to be true. Without it subset_lb.cc:rebuildSingle() will bailout early hence no crash. I will add some comments here to cover this.


// Clear the metadata for each host
auto* load_assignment = cluster->mutable_load_assignment();
auto* endpoints = load_assignment->mutable_endpoints(0);
for (uint32_t i = 0; i < num_hosts_; i++) {
auto* lb_endpoint = endpoints->mutable_lb_endpoints(i);
lb_endpoint->clear_metadata();
}
});

initialize();
}
Comment thread
asraa marked this conversation as resolved.

} // namespace Envoy