Skip to content

Commit

Permalink
ffffffffffffffffffffffff
Browse files Browse the repository at this point in the history
Signed-off-by: Tony Allen <tony@allen.gg>
  • Loading branch information
tonya11en committed Jan 5, 2024
1 parent dfaa3b6 commit b12317f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
3 changes: 1 addition & 2 deletions source/common/upstream/load_balancer_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1302,8 +1302,7 @@ HostConstSharedPtr LeastRequestLoadBalancer::unweightedHostPick(const HostVector
// Use a full scan if:
// - it's explicitly configured, or
// - the number of choices is equal to or larger than the number of hosts.
const auto has_full_scan_configured = selection_method_ ==
envoy::extensions::load_balancing_policies::least_request::v3::LeastRequest_SelectionMethod_FULL_SCAN;
const auto has_full_scan_configured = true;
if (has_full_scan_configured || (hosts_to_use.size() <= choice_count_)) {
// Choose a random index to start from preventing always picking the first host in the list.
const int rand_idx = random_.random() % hosts_to_use.size();
Expand Down
63 changes: 62 additions & 1 deletion test/common/upstream/load_balancer_simulation_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,58 @@ struct LRLBTestParams {
std::vector<double> expected_selection_probs;
};

void tallenTestHax(const uint64_t num_requests, std::vector<int>&& active_rqs) {

NiceMock<MockTimeSystem> time_source_;
HostVector hosts;

absl::node_hash_map<HostConstSharedPtr, uint64_t> host_hits;
std::shared_ptr<MockClusterInfo> info{new NiceMock<MockClusterInfo>()};

for (size_t ii = 0; ii < active_rqs.size(); ++ii) {
auto active_rq_count = active_rqs[ii];
auto h = makeTestHost(info, fmt::format("tcp://1.2.3.4:{}", ii), time_source_, 1 /* weight */);
hosts.push_back(h);
hosts.back()->stats().rq_active_.set(active_rq_count);
host_hits[h] = 0;
}

HostVectorConstSharedPtr updated_hosts{new HostVector(hosts)};
HostsPerLocalitySharedPtr updated_locality_hosts{new HostsPerLocalityImpl(hosts)};
PrioritySetImpl priority_set;
priority_set.updateHosts(
0,
updateHostsParams(updated_hosts, updated_locality_hosts,
std::make_shared<const HealthyHostVector>(*updated_hosts),
updated_locality_hosts),
{}, hosts, {}, absl::nullopt);

Stats::IsolatedStoreImpl stats_store;
ClusterLbStatNames stat_names(stats_store.symbolTable());
ClusterLbStats lb_stats{stat_names, *stats_store.rootScope()};
NiceMock<Runtime::MockLoader> runtime;
auto time_source = std::make_unique<NiceMock<MockTimeSystem>>();
Random::RandomGeneratorImpl random;
envoy::config::cluster::v3::Cluster::LeastRequestLbConfig least_request_lb_config;
envoy::config::cluster::v3::Cluster::CommonLbConfig common_config;
LeastRequestLoadBalancer lb_{
priority_set, nullptr, lb_stats, runtime, random, common_config, least_request_lb_config,
*time_source};

for (uint64_t i = 0; i < num_requests; i++) {
host_hits[lb_.chooseHost(nullptr)]++;
}

std::vector<double> observed_pcts;
std::cout << "================================\n";
std::cout << fmt::format("n={}\n", num_requests);
for (const auto& host : host_hits) {
double hit_pct = static_cast<double>(host.second) / num_requests;
std::cout << fmt::format("{}: weight={} active_rqs={}, pick_ratio={}\n", host.first->hostname(),
host.first->weight(), host.first->stats().rq_active_.value(), hit_pct);
}
}

void leastRequestLBWeightTest(LRLBTestParams params) {
constexpr uint64_t num_requests = 100000;

Expand Down Expand Up @@ -131,7 +183,16 @@ void leastRequestLBWeightTest(LRLBTestParams params) {
}

// Simulate weighted LR load balancer and verify expected selection probabilities.
TEST(LeastRequestLoadBalancerWeightTest, Weight) {
TEST(LeastRequestLoadBalancerWeightTest, TallenHax) {
tallenTestHax(1e6, {1, 1, 1, 1, 1});
tallenTestHax(1e6, {9, 1, 1, 1, 1});
tallenTestHax(1e6, {9, 9, 1, 1, 1});
tallenTestHax(1e6, {9, 9, 9, 1, 1});
tallenTestHax(1e6, {9, 9, 9, 9, 1});
}

// Simulate weighted LR load balancer and verify expected selection probabilities.
TEST(DISABLED_LeastRequestLoadBalancerWeightTest, Weight) {
LRLBTestParams params;

// No active requests or weight differences. This should look like uniform random LB.
Expand Down

0 comments on commit b12317f

Please sign in to comment.