Skip to content

Commit 1f9e2fd

Browse files
rgs1htuch
authored andcommitted
Wire up panic mode subset to receive updates (#6221)
The initial diff missed handling added/removed hosts, this handles that. While at this, lets move the fallback subset creation to the constructor so that it's consistent with the panic mode subset. Reported-by: @nezdolik Signed-off-by: Raul Gutierrez Segales <[email protected]>
1 parent 06c47cc commit 1f9e2fd

File tree

2 files changed

+49
-25
lines changed

2 files changed

+49
-25
lines changed

source/common/upstream/subset_lb.cc

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,26 @@ SubsetLoadBalancer::SubsetLoadBalancer(
3535
scale_locality_weight_(subsets.scaleLocalityWeight()) {
3636
ASSERT(subsets.isEnabled());
3737

38+
if (fallback_policy_ != envoy::api::v2::Cluster::LbSubsetConfig::NO_FALLBACK) {
39+
HostPredicate predicate;
40+
if (fallback_policy_ == envoy::api::v2::Cluster::LbSubsetConfig::ANY_ENDPOINT) {
41+
predicate = [](const Host&) -> bool { return true; };
42+
43+
ENVOY_LOG(debug, "subset lb: creating any-endpoint fallback load balancer");
44+
} else {
45+
predicate = [this](const Host& host) -> bool {
46+
return hostMatches(default_subset_metadata_, host);
47+
};
48+
49+
ENVOY_LOG(debug, "subset lb: creating fallback load balancer for {}",
50+
describeMetadata(default_subset_metadata_));
51+
}
52+
53+
fallback_subset_ = std::make_unique<LbSubsetEntry>();
54+
fallback_subset_->priority_subset_ = std::make_unique<PrioritySubsetImpl>(
55+
*this, predicate, locality_weight_aware_, scale_locality_weight_);
56+
}
57+
3858
if (subsets.panicModeAny()) {
3959
HostPredicate predicate = [](const Host&) -> bool { return true; };
4060

@@ -186,34 +206,18 @@ SubsetLoadBalancer::LbSubsetEntryPtr SubsetLoadBalancer::findSubset(
186206

187207
void SubsetLoadBalancer::updateFallbackSubset(uint32_t priority, const HostVector& hosts_added,
188208
const HostVector& hosts_removed) {
189-
if (fallback_policy_ == envoy::api::v2::Cluster::LbSubsetConfig::NO_FALLBACK) {
209+
if (fallback_subset_ == nullptr) {
190210
ENVOY_LOG(debug, "subset lb: fallback load balancer disabled");
191211
return;
192212
}
193213

194-
if (fallback_subset_ == nullptr) {
195-
// First update: create the default host subset.
196-
HostPredicate predicate;
197-
if (fallback_policy_ == envoy::api::v2::Cluster::LbSubsetConfig::ANY_ENDPOINT) {
198-
predicate = [](const Host&) -> bool { return true; };
199-
200-
ENVOY_LOG(debug, "subset lb: creating any-endpoint fallback load balancer");
201-
} else {
202-
predicate = std::bind(&SubsetLoadBalancer::hostMatches, this, default_subset_metadata_,
203-
std::placeholders::_1);
204-
205-
ENVOY_LOG(debug, "subset lb: creating fallback load balancer for {}",
206-
describeMetadata(default_subset_metadata_));
207-
}
214+
// Add/remove hosts.
215+
fallback_subset_->priority_subset_->update(priority, hosts_added, hosts_removed);
208216

209-
fallback_subset_.reset(new LbSubsetEntry());
210-
fallback_subset_->priority_subset_.reset(
211-
new PrioritySubsetImpl(*this, predicate, locality_weight_aware_, scale_locality_weight_));
212-
return;
217+
// Same thing for the panic mode subset.
218+
if (panic_mode_subset_ != nullptr) {
219+
panic_mode_subset_->priority_subset_->update(priority, hosts_added, hosts_removed);
213220
}
214-
215-
// Subsequent updates: add/remove hosts.
216-
fallback_subset_->priority_subset_->update(priority, hosts_added, hosts_removed);
217221
}
218222

219223
// Iterates over the added and removed hosts, looking up an LbSubsetEntryPtr for each. For every
@@ -249,9 +253,9 @@ void SubsetLoadBalancer::processSubsets(
249253
if (entry->initialized()) {
250254
update_cb(entry);
251255
} else {
252-
HostPredicate predicate =
253-
std::bind(&SubsetLoadBalancer::hostMatches, this, kvs, std::placeholders::_1);
254-
256+
HostPredicate predicate = [this, kvs](const Host& host) -> bool {
257+
return hostMatches(kvs, host);
258+
};
255259
new_cb(entry, predicate, kvs, adding_hosts);
256260
}
257261
}

test/common/upstream/subset_lb_test.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,26 @@ TEST_F(SubsetLoadBalancerTest, FallbackPanicMode) {
490490
EXPECT_EQ(0U, stats_.lb_subsets_selected_.value());
491491
}
492492

493+
TEST_P(SubsetLoadBalancerTest, FallbackPanicModeWithUpdates) {
494+
EXPECT_CALL(subset_info_, fallbackPolicy())
495+
.WillRepeatedly(Return(envoy::api::v2::Cluster::LbSubsetConfig::DEFAULT_SUBSET));
496+
EXPECT_CALL(subset_info_, panicModeAny()).WillRepeatedly(Return(true));
497+
498+
// The default subset will be empty.
499+
const ProtobufWkt::Struct default_subset = makeDefaultSubset({{"version", "none"}});
500+
EXPECT_CALL(subset_info_, defaultSubset()).WillRepeatedly(ReturnRef(default_subset));
501+
502+
init({{"tcp://127.0.0.1:80", {{"version", "default"}}}});
503+
EXPECT_TRUE(lb_->chooseHost(nullptr) != nullptr);
504+
505+
// Removing current host, adding a new one.
506+
HostSharedPtr added_host = makeHost("tcp://127.0.0.2:8000", {{"version", "new"}});
507+
modifyHosts({added_host}, {host_set_.hosts_[0]});
508+
509+
EXPECT_EQ(1, host_set_.hosts_.size());
510+
EXPECT_EQ(added_host, lb_->chooseHost(nullptr));
511+
}
512+
493513
TEST_P(SubsetLoadBalancerTest, FallbackDefaultSubsetAfterUpdate) {
494514
EXPECT_CALL(subset_info_, fallbackPolicy())
495515
.WillRepeatedly(Return(envoy::api::v2::Cluster::LbSubsetConfig::DEFAULT_SUBSET));

0 commit comments

Comments
 (0)