HostInfo HighestWeightHostSelector::GetHost()

in src/host_selector/highest_weight_host_selector.cc [20:45]


HostInfo HighestWeightHostSelector::GetHost(std::vector<HostInfo> hosts, bool is_writer, std::unordered_map<std::string, std::string>) {
    std::vector<HostInfo> eligible_hosts;
    eligible_hosts.reserve(hosts.size());

    std::copy_if(hosts.begin(), hosts.end(), std::back_inserter(eligible_hosts), [&is_writer](const HostInfo& host) {
        return host.IsHostUp() && is_writer == host.IsHostWriter();
    });

    if (eligible_hosts.empty()) {
        throw std::runtime_error("No eligible hosts found in list");
    }

    auto highest_weight_host = std::max_element(
        eligible_hosts.begin(),
        eligible_hosts.end(),
        [](const HostInfo& a, const HostInfo& b) {
            return a.GetWeight() < b.GetWeight();
        }
    );

    if (highest_weight_host == eligible_hosts.end()) {
        throw std::runtime_error("No eligible hosts found in list");
    }

    return *highest_weight_host;
}