Skip to content

Commit fe83128

Browse files
committed
Small changes after code review.
Added unit tests to make sure that excluded_hosts are not taken into account when calculating load in TotalPanic mode. Signed-off-by: Christoph Pakulski <[email protected]>
1 parent 792bc88 commit fe83128

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

source/common/upstream/load_balancer_impl.cc

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,22 @@ void LoadBalancerBase::recalculateLoadInTotalPanic() {
291291
uint32_t total_load = 100;
292292
for (size_t i = 0; i < per_priority_panic_.size(); i++) {
293293
const HostSet& host_set = *priority_set_.hostSetsPerPriority()[i];
294-
auto hosts_num = host_set.hosts().size() - host_set.excludedHosts().size();
294+
const auto hosts_num = host_set.hosts().size();
295295

296-
uint32_t priority_load = 100 * hosts_num / total_hosts_num;
296+
const uint32_t priority_load = 100 * hosts_num / total_hosts_num;
297297
per_priority_load_.healthy_priority_load_.get()[i] = priority_load;
298298
per_priority_load_.degraded_priority_load_.get()[i] = 0;
299299
total_load -= priority_load;
300300
}
301-
// Add to priority zero whatever if left.
302-
per_priority_load_.healthy_priority_load_.get()[0] += total_load;
301+
302+
// Find the first priority which is not empty and add remaining load.
303+
for (size_t i = 0; i < per_priority_panic_.size(); i++) {
304+
const HostSet& host_set = *priority_set_.hostSetsPerPriority()[i];
305+
if (0 != host_set.hosts().size()) {
306+
per_priority_load_.healthy_priority_load_.get()[i] += total_load;
307+
break;
308+
}
309+
}
303310
}
304311

305312
std::pair<HostSet&, LoadBalancerBase::HostAvailability>

test/common/upstream/load_balancer_impl_test.cc

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ class LoadBalancerBaseTest : public LoadBalancerTestBase {
8686
}
8787

8888
for (; i < (num_healthy_hosts + num_degraded_hosts + num_excluded_hosts); ++i) {
89-
host_set.degraded_hosts_.push_back(host_set.hosts_[i]);
9089
host_set.excluded_hosts_.push_back(host_set.hosts_[i]);
9190
}
9291
host_set.runCallbacks({}, {});
@@ -310,7 +309,6 @@ TEST_P(LoadBalancerBaseTest, GentleFailover) {
310309
ASSERT_THAT(getPanic(), ElementsAre(false, false));
311310

312311
// Health P=0 == 100*1.4 == 35 P=1 == 35
313-
// Since 4 hosts are excluded and are unhealthy, P=0 should be considered fully unavailable.
314312
// Total health = 35% is less than 100%.
315313
// All priorities are in panic mode (situation called TotalPanic)
316314
// Load is distributed based on number of hosts regardless of their health status.
@@ -320,6 +318,19 @@ TEST_P(LoadBalancerBaseTest, GentleFailover) {
320318
updateHostSet(failover_host_set_, 4 /* num_hosts */, 1 /* num_healthy_hosts */);
321319
ASSERT_THAT(getLoadPercentage(), ElementsAre(50, 50));
322320
ASSERT_THAT(getPanic(), ElementsAre(true, true));
321+
322+
// Make sure that in TotalPanic mode (all levels are in Panic),
323+
// load distribution depends only on number of hosts.
324+
// excluded_hosts should not be taken into account.
325+
// P=0 has 4 hosts with 1 excluded, P=1 has 6 hosts with 2 excluded.
326+
// P=0 should receive 4/(4+6)=40% of traffic
327+
// P=1 should receive 6/(4+6)=60% of traffic
328+
updateHostSet(host_set_, 4 /* num_hosts */, 0 /* num_healthy_hosts */, 0 /* num_degraded_hosts */,
329+
1 /* num_excluded_hosts */);
330+
updateHostSet(failover_host_set_, 6 /* num_hosts */, 1 /* num_healthy_hosts */,
331+
0 /* num_degraded_hosts */, 2 /* num_excluded_hosts */);
332+
ASSERT_THAT(getLoadPercentage(), ElementsAre(40, 60));
333+
ASSERT_THAT(getPanic(), ElementsAre(true, true));
323334
}
324335

325336
TEST_P(LoadBalancerBaseTest, GentleFailoverWithExtraLevels) {

0 commit comments

Comments
 (0)