Skip to content

Commit

Permalink
Update gauges when a subset LB is destroyed (#3917)
Browse files Browse the repository at this point in the history
Without these updates, the gauges will be incorrect after a hot
restart.

Fixes #3916
  • Loading branch information
rgs1 authored and mattklein123 committed Jul 20, 2018
1 parent 20296c5 commit b8e019f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
12 changes: 11 additions & 1 deletion source/common/upstream/subset_lb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,17 @@ SubsetLoadBalancer::SubsetLoadBalancer(
});
}

SubsetLoadBalancer::~SubsetLoadBalancer() { original_priority_set_callback_handle_->remove(); }
SubsetLoadBalancer::~SubsetLoadBalancer() {
original_priority_set_callback_handle_->remove();

// Ensure gauges reflect correct values.
forEachSubset(subsets_, [&](LbSubsetEntryPtr entry) {
if (entry->initialized() && entry->active()) {
stats_.lb_subsets_removed_.inc();
stats_.lb_subsets_active_.dec();
}
});
}

void SubsetLoadBalancer::refreshSubsets() {
for (auto& host_set : original_priority_set_.hostSetsPerPriority()) {
Expand Down
20 changes: 20 additions & 0 deletions test/common/upstream/subset_lb_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,26 @@ TEST_F(SubsetLoadBalancerTest, EnabledLocalityWeightAwareness) {
EXPECT_EQ(host_set_.healthy_hosts_per_locality_->get()[1][0], lb_->chooseHost(&context));
}

TEST_P(SubsetLoadBalancerTest, GaugesUpdatedOnDestroy) {
EXPECT_CALL(subset_info_, fallbackPolicy())
.WillRepeatedly(Return(envoy::api::v2::Cluster::LbSubsetConfig::ANY_ENDPOINT));

std::vector<std::set<std::string>> subset_keys = {{"version"}};
EXPECT_CALL(subset_info_, subsetKeys()).WillRepeatedly(ReturnRef(subset_keys));

init({
{"tcp://127.0.0.1:80", {{"version", "1.0"}}},
});

EXPECT_EQ(1U, stats_.lb_subsets_active_.value());
EXPECT_EQ(0U, stats_.lb_subsets_removed_.value());

lb_ = nullptr;

EXPECT_EQ(0U, stats_.lb_subsets_active_.value());
EXPECT_EQ(1U, stats_.lb_subsets_removed_.value());
}

INSTANTIATE_TEST_CASE_P(UpdateOrderings, SubsetLoadBalancerTest,
testing::ValuesIn({REMOVES_FIRST, SIMULTANEOUS}));

Expand Down

0 comments on commit b8e019f

Please sign in to comment.