diff --git a/include/envoy/upstream/upstream.h b/include/envoy/upstream/upstream.h index 001d6df94cc2..9b219486d0e4 100644 --- a/include/envoy/upstream/upstream.h +++ b/include/envoy/upstream/upstream.h @@ -156,18 +156,18 @@ class HostSet { virtual const std::vector& healthyHosts() const PURE; /** - * @return hosts per zone, index 0 is dedicated to local zone hosts. - * If there are no hosts in local zone for upstream cluster hostPerZone() will @return + * @return hosts per locality, index 0 is dedicated to local locality hosts. + * If there are no hosts in local locality for upstream cluster hostsPerLocality() will @return * empty vector. * - * Note, that we sort zones in alphabetical order starting from index 1. + * Note, that we sort localities in lexicographic order starting from index 1. */ - virtual const std::vector>& hostsPerZone() const PURE; + virtual const std::vector>& hostsPerLocality() const PURE; /** - * @return same as hostsPerZone but only contains healthy hosts. + * @return same as hostsPerLocality but only contains healthy hosts. */ - virtual const std::vector>& healthyHostsPerZone() const PURE; + virtual const std::vector>& healthyHostsPerLocality() const PURE; }; /** diff --git a/source/common/upstream/cluster_manager_impl.cc b/source/common/upstream/cluster_manager_impl.cc index 01763da49171..9e1793bd1313 100644 --- a/source/common/upstream/cluster_manager_impl.cc +++ b/source/common/upstream/cluster_manager_impl.cc @@ -379,16 +379,16 @@ void ClusterManagerImpl::postThreadLocalClusterUpdate( HostVectorConstSharedPtr hosts_copy(new std::vector(primary_cluster.hosts())); HostVectorConstSharedPtr healthy_hosts_copy( new std::vector(primary_cluster.healthyHosts())); - HostListsConstSharedPtr hosts_per_zone_copy( - new std::vector>(primary_cluster.hostsPerZone())); - HostListsConstSharedPtr healthy_hosts_per_zone_copy( - new std::vector>(primary_cluster.healthyHostsPerZone())); + HostListsConstSharedPtr hosts_per_locality_copy( + new std::vector>(primary_cluster.hostsPerLocality())); + HostListsConstSharedPtr healthy_hosts_per_locality_copy( + new std::vector>(primary_cluster.healthyHostsPerLocality())); - tls_->runOnAllThreads([this, name, hosts_copy, healthy_hosts_copy, hosts_per_zone_copy, - healthy_hosts_per_zone_copy, hosts_added, hosts_removed]() -> void { + tls_->runOnAllThreads([this, name, hosts_copy, healthy_hosts_copy, hosts_per_locality_copy, + healthy_hosts_per_locality_copy, hosts_added, hosts_removed]() -> void { ThreadLocalClusterManagerImpl::updateClusterMembership( - name, hosts_copy, healthy_hosts_copy, hosts_per_zone_copy, healthy_hosts_per_zone_copy, - hosts_added, hosts_removed, *tls_); + name, hosts_copy, healthy_hosts_copy, hosts_per_locality_copy, + healthy_hosts_per_locality_copy, hosts_added, hosts_removed, *tls_); }); } @@ -512,7 +512,7 @@ void ClusterManagerImpl::ThreadLocalClusterManagerImpl::drainConnPools( void ClusterManagerImpl::ThreadLocalClusterManagerImpl::updateClusterMembership( const std::string& name, HostVectorConstSharedPtr hosts, HostVectorConstSharedPtr healthy_hosts, - HostListsConstSharedPtr hosts_per_zone, HostListsConstSharedPtr healthy_hosts_per_zone, + HostListsConstSharedPtr hosts_per_locality, HostListsConstSharedPtr healthy_hosts_per_locality, const std::vector& hosts_added, const std::vector& hosts_removed, ThreadLocal::Slot& tls) { @@ -520,7 +520,8 @@ void ClusterManagerImpl::ThreadLocalClusterManagerImpl::updateClusterMembership( ASSERT(config.thread_local_clusters_.find(name) != config.thread_local_clusters_.end()); config.thread_local_clusters_[name]->host_set_.updateHosts( - hosts, healthy_hosts, hosts_per_zone, healthy_hosts_per_zone, hosts_added, hosts_removed); + hosts, healthy_hosts, hosts_per_locality, healthy_hosts_per_locality, hosts_added, + hosts_removed); } ClusterManagerImpl::ThreadLocalClusterManagerImpl::ClusterEntry::ClusterEntry( diff --git a/source/common/upstream/cluster_manager_impl.h b/source/common/upstream/cluster_manager_impl.h index d033d0dfa3f7..5dec13b338e3 100644 --- a/source/common/upstream/cluster_manager_impl.h +++ b/source/common/upstream/cluster_manager_impl.h @@ -205,8 +205,8 @@ class ClusterManagerImpl : public ClusterManager, Logger::Loggable& hosts_added, const std::vector& hosts_removed, ThreadLocal::Slot& tls); diff --git a/source/common/upstream/eds.cc b/source/common/upstream/eds.cc index fb14f87145b7..6072215b1fe6 100644 --- a/source/common/upstream/eds.cc +++ b/source/common/upstream/eds.cc @@ -72,30 +72,31 @@ void EdsClusterImpl::onConfigUpdate(const ResourceVector& resources) { if (updateDynamicHostList(new_hosts, *current_hosts_copy, hosts_added, hosts_removed, health_checker_ != nullptr)) { ENVOY_LOG(debug, "EDS hosts changed for cluster: {} ({})", info_->name(), hosts().size()); - HostListsSharedPtr per_zone(new std::vector>()); + HostListsSharedPtr per_locality(new std::vector>()); - // If local zone name is not defined then skip populating per zone hosts. - if (!local_info_.zoneName().empty()) { - std::map> hosts_per_zone; + // If local locality is not defined then skip populating per locality hosts. + const Locality local_locality(local_info_.node().locality()); + if (!local_locality.empty()) { + std::map> hosts_per_locality; for (const HostSharedPtr& host : *current_hosts_copy) { - hosts_per_zone[host->locality().zone()].push_back(host); + hosts_per_locality[Locality(host->locality())].push_back(host); } - // Populate per_zone hosts only if upstream cluster has hosts in the same zone. - if (hosts_per_zone.find(local_info_.zoneName()) != hosts_per_zone.end()) { - per_zone->push_back(hosts_per_zone[local_info_.zoneName()]); + // Populate per_locality hosts only if upstream cluster has hosts in the same locality. + if (hosts_per_locality.find(local_locality) != hosts_per_locality.end()) { + per_locality->push_back(hosts_per_locality[local_locality]); - for (auto& entry : hosts_per_zone) { - if (local_info_.zoneName() != entry.first) { - per_zone->push_back(entry.second); + for (auto& entry : hosts_per_locality) { + if (local_locality != entry.first) { + per_locality->push_back(entry.second); } } } } - updateHosts(current_hosts_copy, createHealthyHostList(*current_hosts_copy), per_zone, - createHealthyHostLists(*per_zone), hosts_added, hosts_removed); + updateHosts(current_hosts_copy, createHealthyHostList(*current_hosts_copy), per_locality, + createHealthyHostLists(*per_locality), hosts_added, hosts_removed); if (initialize_callback_ && health_checker_ && pending_health_checks_ == 0) { pending_health_checks_ = hosts().size(); diff --git a/source/common/upstream/load_balancer_impl.cc b/source/common/upstream/load_balancer_impl.cc index f0f71cc19cae..9aed130c9b5a 100644 --- a/source/common/upstream/load_balancer_impl.cc +++ b/source/common/upstream/load_balancer_impl.cc @@ -25,11 +25,11 @@ LoadBalancerBase::LoadBalancerBase(const HostSet& host_set, const HostSet* local if (local_host_set_) { host_set_.addMemberUpdateCb( [this](const std::vector&, const std::vector&) -> void { - regenerateZoneRoutingStructures(); + regenerateLocalityRoutingStructures(); }); local_host_set_member_update_cb_handle_ = local_host_set_->addMemberUpdateCb( [this](const std::vector&, const std::vector&) -> void { - regenerateZoneRoutingStructures(); + regenerateLocalityRoutingStructures(); }); } } @@ -40,44 +40,45 @@ LoadBalancerBase::~LoadBalancerBase() { } } -void LoadBalancerBase::regenerateZoneRoutingStructures() { +void LoadBalancerBase::regenerateLocalityRoutingStructures() { stats_.lb_recalculate_zone_structures_.inc(); - // Do not perform any calculations if we cannot perform zone routing based on non runtime params. - if (earlyExitNonZoneRouting()) { - zone_routing_state_ = ZoneRoutingState::NoZoneRouting; + // Do not perform any calculations if we cannot perform locality routing based on non runtime + // params. + if (earlyExitNonLocalityRouting()) { + locality_routing_state_ = LocalityRoutingState::NoLocalityRouting; return; } - size_t num_zones = host_set_.healthyHostsPerZone().size(); - ASSERT(num_zones > 0); + size_t num_localities = host_set_.healthyHostsPerLocality().size(); + ASSERT(num_localities > 0); - uint64_t local_percentage[num_zones]; - calculateZonePercentage(local_host_set_->healthyHostsPerZone(), local_percentage); + uint64_t local_percentage[num_localities]; + calculateLocalityPercentage(local_host_set_->healthyHostsPerLocality(), local_percentage); - uint64_t upstream_percentage[num_zones]; - calculateZonePercentage(host_set_.healthyHostsPerZone(), upstream_percentage); + uint64_t upstream_percentage[num_localities]; + calculateLocalityPercentage(host_set_.healthyHostsPerLocality(), upstream_percentage); - // If we have lower percent of hosts in the local cluster in the same zone, - // we can push all of the requests directly to upstream cluster in the same zone. + // If we have lower percent of hosts in the local cluster in the same locality, + // we can push all of the requests directly to upstream cluster in the same locality. if (upstream_percentage[0] >= local_percentage[0]) { - zone_routing_state_ = ZoneRoutingState::ZoneDirect; + locality_routing_state_ = LocalityRoutingState::LocalityDirect; return; } - zone_routing_state_ = ZoneRoutingState::ZoneResidual; + locality_routing_state_ = LocalityRoutingState::LocalityResidual; - // If we cannot route all requests to the same zone, calculate what percentage can be routed. + // If we cannot route all requests to the same locality, calculate what percentage can be routed. // For example, if local percentage is 20% and upstream is 10% // we can route only 50% of requests directly. local_percent_to_route_ = upstream_percentage[0] * 10000 / local_percentage[0]; - // Local zone does not have additional capacity (we have already routed what we could). - // Now we need to figure out how much traffic we can route cross zone and to which exact zone - // we should route. Percentage of requests routed cross zone to a specific zone needed be - // proportional to the residual capacity upstream zone has. + // Local locality does not have additional capacity (we have already routed what we could). + // Now we need to figure out how much traffic we can route cross locality and to which exact + // locality we should route. Percentage of requests routed cross locality to a specific locality + // needed be proportional to the residual capacity upstream locality has. // - // residual_capacity contains capacity left in a given zone, we keep accumulating residual + // residual_capacity contains capacity left in a given locality, we keep accumulating residual // capacity to make search for sampled value easier. // For example, if we have the following upstream and local percentage: // local_percentage: 40000 40000 20000 @@ -86,41 +87,42 @@ void LoadBalancerBase::regenerateZoneRoutingStructures() { // bucket sizes (residual capacity). For simplicity of finding where specific // sampled value is, we accumulate values in residual capacity. This is what it will look like: // residual_capacity: 0 10000 15000 - // Now to find a zone to route (bucket) we could simply iterate over residual_capacity searching - // where sampled value is placed. - residual_capacity_.resize(num_zones); + // Now to find a locality to route (bucket) we could simply iterate over residual_capacity + // searching where sampled value is placed. + residual_capacity_.resize(num_localities); - // Local zone (index 0) does not have residual capacity as we have routed all we could. + // Local locality (index 0) does not have residual capacity as we have routed all we could. residual_capacity_[0] = 0; - for (size_t i = 1; i < num_zones; ++i) { - // Only route to the zones that have additional capacity. + for (size_t i = 1; i < num_localities; ++i) { + // Only route to the localities that have additional capacity. if (upstream_percentage[i] > local_percentage[i]) { residual_capacity_[i] = residual_capacity_[i - 1] + upstream_percentage[i] - local_percentage[i]; } else { - // Zone with index "i" does not have residual capacity, but we keep accumulating previous + // Locality with index "i" does not have residual capacity, but we keep accumulating previous // values to make search easier on the next step. residual_capacity_[i] = residual_capacity_[i - 1]; } } }; -bool LoadBalancerBase::earlyExitNonZoneRouting() { - if (host_set_.healthyHostsPerZone().size() < 2) { +bool LoadBalancerBase::earlyExitNonLocalityRouting() { + if (host_set_.healthyHostsPerLocality().size() < 2) { return true; } - if (host_set_.healthyHostsPerZone()[0].empty()) { + if (host_set_.healthyHostsPerLocality()[0].empty()) { return true; } - // Same number of zones should be for local and upstream cluster. - if (host_set_.healthyHostsPerZone().size() != local_host_set_->healthyHostsPerZone().size()) { + // Same number of localities should be for local and upstream cluster. + if (host_set_.healthyHostsPerLocality().size() != + local_host_set_->healthyHostsPerLocality().size()) { stats_.lb_zone_number_differs_.inc(); return true; } - // Do not perform zone routing for small clusters. + // Do not perform locality routing for small clusters. uint64_t min_cluster_size = runtime_.snapshot().getInteger(RuntimeMinClusterSize, 6U); if (host_set_.healthyHosts().size() < min_cluster_size) { stats_.lb_zone_cluster_too_small_.inc(); @@ -145,65 +147,66 @@ bool LoadBalancerUtility::isGlobalPanic(const HostSet& host_set, Runtime::Loader return false; } -void LoadBalancerBase::calculateZonePercentage( - const std::vector>& hosts_per_zone, uint64_t* ret) { +void LoadBalancerBase::calculateLocalityPercentage( + const std::vector>& hosts_per_locality, uint64_t* ret) { uint64_t total_hosts = 0; - for (const auto& zone_hosts : hosts_per_zone) { - total_hosts += zone_hosts.size(); + for (const auto& locality_hosts : hosts_per_locality) { + total_hosts += locality_hosts.size(); } size_t i = 0; - for (const auto& zone_hosts : hosts_per_zone) { - ret[i++] = total_hosts > 0 ? 10000ULL * zone_hosts.size() / total_hosts : 0; + for (const auto& locality_hosts : hosts_per_locality) { + ret[i++] = total_hosts > 0 ? 10000ULL * locality_hosts.size() / total_hosts : 0; } } -const std::vector& LoadBalancerBase::tryChooseLocalZoneHosts() { - ASSERT(zone_routing_state_ != ZoneRoutingState::NoZoneRouting); +const std::vector& LoadBalancerBase::tryChooseLocalLocalityHosts() { + ASSERT(locality_routing_state_ != LocalityRoutingState::NoLocalityRouting); - // At this point it's guaranteed to be at least 2 zones. - size_t number_of_zones = host_set_.healthyHostsPerZone().size(); + // At this point it's guaranteed to be at least 2 localities. + size_t number_of_localities = host_set_.healthyHostsPerLocality().size(); - ASSERT(number_of_zones >= 2U); - ASSERT(local_host_set_->healthyHostsPerZone().size() == host_set_.healthyHostsPerZone().size()); + ASSERT(number_of_localities >= 2U); + ASSERT(local_host_set_->healthyHostsPerLocality().size() == + host_set_.healthyHostsPerLocality().size()); - // Try to push all of the requests to the same zone first. - if (zone_routing_state_ == ZoneRoutingState::ZoneDirect) { + // Try to push all of the requests to the same locality first. + if (locality_routing_state_ == LocalityRoutingState::LocalityDirect) { stats_.lb_zone_routing_all_directly_.inc(); - return host_set_.healthyHostsPerZone()[0]; + return host_set_.healthyHostsPerLocality()[0]; } - ASSERT(zone_routing_state_ == ZoneRoutingState::ZoneResidual); + ASSERT(locality_routing_state_ == LocalityRoutingState::LocalityResidual); - // If we cannot route all requests to the same zone, we already calculated how much we can - // push to the local zone, check if we can push to local zone on current iteration. + // If we cannot route all requests to the same locality, we already calculated how much we can + // push to the local locality, check if we can push to local locality on current iteration. if (random_.random() % 10000 < local_percent_to_route_) { stats_.lb_zone_routing_sampled_.inc(); - return host_set_.healthyHostsPerZone()[0]; + return host_set_.healthyHostsPerLocality()[0]; } - // At this point we must route cross zone as we cannot route to the local zone. + // At this point we must route cross locality as we cannot route to the local locality. stats_.lb_zone_routing_cross_zone_.inc(); // This is *extremely* unlikely but possible due to rounding errors when calculating - // zone percentages. In this case just select random zone. - if (residual_capacity_[number_of_zones - 1] == 0) { + // locality percentages. In this case just select random locality. + if (residual_capacity_[number_of_localities - 1] == 0) { stats_.lb_zone_no_capacity_left_.inc(); - return host_set_.healthyHostsPerZone()[random_.random() % number_of_zones]; + return host_set_.healthyHostsPerLocality()[random_.random() % number_of_localities]; } - // Random sampling to select specific zone for cross zone traffic based on the additional - // capacity in zones. - uint64_t threshold = random_.random() % residual_capacity_[number_of_zones - 1]; + // Random sampling to select specific locality for cross locality traffic based on the additional + // capacity in localities. + uint64_t threshold = random_.random() % residual_capacity_[number_of_localities - 1]; - // This potentially can be optimized to be O(log(N)) where N is the number of zones. + // This potentially can be optimized to be O(log(N)) where N is the number of localities. // Linear scan should be faster for smaller N, in most of the scenarios N will be small. int i = 0; while (threshold > residual_capacity_[i]) { i++; } - return host_set_.healthyHostsPerZone()[i]; + return host_set_.healthyHostsPerLocality()[i]; } const std::vector& LoadBalancerBase::hostsToUse() { @@ -214,7 +217,7 @@ const std::vector& LoadBalancerBase::hostsToUse() { return host_set_.hosts(); } - if (zone_routing_state_ == ZoneRoutingState::NoZoneRouting) { + if (locality_routing_state_ == LocalityRoutingState::NoLocalityRouting) { return host_set_.healthyHosts(); } @@ -227,7 +230,7 @@ const std::vector& LoadBalancerBase::hostsToUse() { return host_set_.healthyHosts(); } - return tryChooseLocalZoneHosts(); + return tryChooseLocalLocalityHosts(); } HostConstSharedPtr RoundRobinLoadBalancer::chooseHost(const LoadBalancerContext*) { diff --git a/source/common/upstream/load_balancer_impl.h b/source/common/upstream/load_balancer_impl.h index 3c6c446f2404..487459856f67 100644 --- a/source/common/upstream/load_balancer_impl.h +++ b/source/common/upstream/load_balancer_impl.h @@ -42,36 +42,37 @@ class LoadBalancerBase { Runtime::RandomGenerator& random_; private: - enum class ZoneRoutingState { NoZoneRouting, ZoneDirect, ZoneResidual }; + enum class LocalityRoutingState { NoLocalityRouting, LocalityDirect, LocalityResidual }; /** - * @return decision on quick exit from zone aware routing based on cluster configuration. + * @return decision on quick exit from locality aware routing based on cluster configuration. * This gets recalculated on update callback. */ - bool earlyExitNonZoneRouting(); + bool earlyExitNonLocalityRouting(); /** - * Try to select upstream hosts from the same zone. + * Try to select upstream hosts from the same locality. */ - const std::vector& tryChooseLocalZoneHosts(); + const std::vector& tryChooseLocalLocalityHosts(); /** - * @return (number of hosts in a given zone)/(total number of hosts) in ret param. + * @return (number of hosts in a given locality)/(total number of hosts) in ret param. * The result is stored as integer number and scaled by 10000 multiplier for better precision. * Caller is responsible for allocation/de-allocation of ret. */ - void calculateZonePercentage(const std::vector>& hosts_per_zone, - uint64_t* ret); + void + calculateLocalityPercentage(const std::vector>& hosts_per_locality, + uint64_t* ret); /** - * Regenerate zone aware routing structures for fast decisions on upstream zone selection. + * Regenerate locality aware routing structures for fast decisions on upstream locality selection. */ - void regenerateZoneRoutingStructures(); + void regenerateLocalityRoutingStructures(); const HostSet& host_set_; const HostSet* local_host_set_; uint64_t local_percent_to_route_{}; - ZoneRoutingState zone_routing_state_{ZoneRoutingState::NoZoneRouting}; + LocalityRoutingState locality_routing_state_{LocalityRoutingState::NoLocalityRouting}; std::vector residual_capacity_; Common::CallbackHandle* local_host_set_member_update_cb_handle_{}; }; diff --git a/source/common/upstream/upstream_impl.cc b/source/common/upstream/upstream_impl.cc index cd70bdc9d771..69ebf25519cf 100644 --- a/source/common/upstream/upstream_impl.cc +++ b/source/common/upstream/upstream_impl.cc @@ -285,10 +285,10 @@ void ClusterImplBase::setOutlierDetector(const Outlier::DetectorSharedPtr& outli void ClusterImplBase::reloadHealthyHosts() { HostVectorConstSharedPtr hosts_copy(new std::vector(hosts())); - HostListsConstSharedPtr hosts_per_zone_copy( - new std::vector>(hostsPerZone())); - updateHosts(hosts_copy, createHealthyHostList(hosts()), hosts_per_zone_copy, - createHealthyHostLists(hostsPerZone()), {}, {}); + HostListsConstSharedPtr hosts_per_locality_copy( + new std::vector>(hostsPerLocality())); + updateHosts(hosts_copy, createHealthyHostList(hosts()), hosts_per_locality_copy, + createHealthyHostLists(hostsPerLocality()), {}, {}); } ClusterInfoImpl::ResourceManagers::ResourceManagers(const envoy::api::v2::Cluster& config, diff --git a/source/common/upstream/upstream_impl.h b/source/common/upstream/upstream_impl.h index a4563a02f118..7a39ff838200 100644 --- a/source/common/upstream/upstream_impl.h +++ b/source/common/upstream/upstream_impl.h @@ -35,6 +35,21 @@ namespace Envoy { namespace Upstream { +// Wrapper around envoy::api::v2::Locality to make it easier to compare for ordering in std::map and +// in tests to construct literals. +// TODO(htuch): Consider making this reference based when we have a single string implementation. +class Locality : public std::tuple { +public: + Locality(const std::string& region, const std::string& zone, const std::string& sub_zone) + : std::tuple(region, zone, sub_zone) {} + Locality(const envoy::api::v2::Locality& locality) + : std::tuple(locality.region(), locality.zone(), + locality.sub_zone()) {} + bool empty() const { + return std::get<0>(*this).empty() && std::get<1>(*this).empty() && std::get<2>(*this).empty(); + } +}; + /** * Null implementation of HealthCheckHostMonitor. */ @@ -158,29 +173,29 @@ class HostSetImpl : public virtual HostSet { public: HostSetImpl() : hosts_(new std::vector()), healthy_hosts_(new std::vector()), - hosts_per_zone_(new std::vector>()), - healthy_hosts_per_zone_(new std::vector>()) {} + hosts_per_locality_(new std::vector>()), + healthy_hosts_per_locality_(new std::vector>()) {} void updateHosts(HostVectorConstSharedPtr hosts, HostVectorConstSharedPtr healthy_hosts, - HostListsConstSharedPtr hosts_per_zone, - HostListsConstSharedPtr healthy_hosts_per_zone, + HostListsConstSharedPtr hosts_per_locality, + HostListsConstSharedPtr healthy_hosts_per_locality, const std::vector& hosts_added, const std::vector& hosts_removed) { hosts_ = hosts; healthy_hosts_ = healthy_hosts; - hosts_per_zone_ = hosts_per_zone; - healthy_hosts_per_zone_ = healthy_hosts_per_zone; + hosts_per_locality_ = hosts_per_locality; + healthy_hosts_per_locality_ = healthy_hosts_per_locality; runUpdateCallbacks(hosts_added, hosts_removed); } // Upstream::HostSet const std::vector& hosts() const override { return *hosts_; } const std::vector& healthyHosts() const override { return *healthy_hosts_; } - const std::vector>& hostsPerZone() const override { - return *hosts_per_zone_; + const std::vector>& hostsPerLocality() const override { + return *hosts_per_locality_; } - const std::vector>& healthyHostsPerZone() const override { - return *healthy_hosts_per_zone_; + const std::vector>& healthyHostsPerLocality() const override { + return *healthy_hosts_per_locality_; } Common::CallbackHandle* addMemberUpdateCb(MemberUpdateCb callback) const override { return member_update_cb_helper_.add(callback); @@ -195,8 +210,8 @@ class HostSetImpl : public virtual HostSet { private: HostVectorConstSharedPtr hosts_; HostVectorConstSharedPtr healthy_hosts_; - HostListsConstSharedPtr hosts_per_zone_; - HostListsConstSharedPtr healthy_hosts_per_zone_; + HostListsConstSharedPtr hosts_per_locality_; + HostListsConstSharedPtr healthy_hosts_per_locality_; mutable Common::CallbackManager&, const std::vector&> member_update_cb_helper_; diff --git a/test/common/router/rds_impl_test.cc b/test/common/router/rds_impl_test.cc index f78fc382ad5b..5c9d88e8b423 100644 --- a/test/common/router/rds_impl_test.cc +++ b/test/common/router/rds_impl_test.cc @@ -148,8 +148,8 @@ TEST_F(RdsImplTest, LocalInfoNotDefined) { } )EOF"; - local_info_.cluster_name_ = ""; - local_info_.node_name_ = ""; + local_info_.node_.set_cluster(""); + local_info_.node_.set_id(""); EXPECT_THROW(RouteConfigProviderUtil::create(parseHttpConnectionManagerFromJson(config_json), runtime_, cm_, store_, "foo.", init_manager_, *route_config_provider_manager_), diff --git a/test/common/upstream/cds_api_impl_test.cc b/test/common/upstream/cds_api_impl_test.cc index a26bca6ae5f4..30b0e4409d59 100644 --- a/test/common/upstream/cds_api_impl_test.cc +++ b/test/common/upstream/cds_api_impl_test.cc @@ -103,8 +103,8 @@ TEST_F(CdsApiImplTest, InvalidOptions) { )EOF"; Json::ObjectSharedPtr config = Json::Factory::loadFromString(config_json); - local_info_.cluster_name_ = ""; - local_info_.node_name_ = ""; + local_info_.node_.set_cluster(""); + local_info_.node_.set_id(""); envoy::api::v2::ConfigSource cds_config; Config::Utility::translateCdsConfig(*config, cds_config); EXPECT_THROW( diff --git a/test/common/upstream/eds_test.cc b/test/common/upstream/eds_test.cc index 7bf23abc8c5d..3479466e9eb5 100644 --- a/test/common/upstream/eds_test.cc +++ b/test/common/upstream/eds_test.cc @@ -32,7 +32,7 @@ class EdsTest : public testing::Test { envoy::api::v2::ConfigSource eds_config; eds_config.mutable_api_config_source()->add_cluster_name("eds"); eds_config.mutable_api_config_source()->mutable_refresh_delay()->set_seconds(1); - local_info_.zone_name_ = "us-east-1a"; + local_info_.node_.mutable_locality()->set_zone("us-east-1a"); eds_cluster_ = parseSdsClusterFromJson(json_config, eds_config); cluster_.reset(new EdsClusterImpl(eds_cluster_, runtime_, stats_, ssl_context_manager_, local_info_, cm_, dispatcher_, random_, false)); @@ -204,5 +204,68 @@ TEST_F(EdsTest, EndpointLocality) { } } +// Validate that onConfigUpdate() updates bins hosts per locality as expected. +TEST_F(EdsTest, EndpointHostsPerLocality) { + Protobuf::RepeatedPtrField resources; + auto* cluster_load_assignment = resources.Add(); + cluster_load_assignment->set_cluster_name("fare"); + uint32_t port = 1000; + auto add_hosts_to_locality = [cluster_load_assignment, + &port](const std::string& region, const std::string& zone, + const std::string& sub_zone, uint32_t n) { + auto* endpoints = cluster_load_assignment->add_endpoints(); + auto* locality = endpoints->mutable_locality(); + locality->set_region(region); + locality->set_zone(zone); + locality->set_sub_zone(sub_zone); + + for (uint32_t i = 0; i < n; ++i) { + auto* socket_address = endpoints->add_lb_endpoints() + ->mutable_endpoint() + ->mutable_address() + ->mutable_socket_address(); + socket_address->set_address("1.2.3.4"); + socket_address->set_port_value(port++); + } + }; + + add_hosts_to_locality("oceania", "koala", "ingsoc", 2); + add_hosts_to_locality("", "us-east-1a", "", 1); + + bool initialized = false; + cluster_->setInitializedCb([&initialized] { initialized = true; }); + EXPECT_NO_THROW(cluster_->onConfigUpdate(resources)); + EXPECT_TRUE(initialized); + + EXPECT_EQ(2, cluster_->hostsPerLocality().size()); + EXPECT_EQ(1, cluster_->hostsPerLocality()[0].size()); + EXPECT_EQ(Locality("", "us-east-1a", ""), + Locality(cluster_->hostsPerLocality()[0][0]->locality())); + EXPECT_EQ(2, cluster_->hostsPerLocality()[1].size()); + EXPECT_EQ(Locality("oceania", "koala", "ingsoc"), + Locality(cluster_->hostsPerLocality()[1][0]->locality())); + EXPECT_EQ(Locality("oceania", "koala", "ingsoc"), + Locality(cluster_->hostsPerLocality()[1][1]->locality())); + + add_hosts_to_locality("oceania", "koala", "eucalyptus", 3); + add_hosts_to_locality("general", "koala", "ingsoc", 5); + + EXPECT_NO_THROW(cluster_->onConfigUpdate(resources)); + + EXPECT_EQ(4, cluster_->hostsPerLocality().size()); + EXPECT_EQ(1, cluster_->hostsPerLocality()[0].size()); + EXPECT_EQ(Locality("", "us-east-1a", ""), + Locality(cluster_->hostsPerLocality()[0][0]->locality())); + EXPECT_EQ(5, cluster_->hostsPerLocality()[1].size()); + EXPECT_EQ(Locality("general", "koala", "ingsoc"), + Locality(cluster_->hostsPerLocality()[1][0]->locality())); + EXPECT_EQ(3, cluster_->hostsPerLocality()[2].size()); + EXPECT_EQ(Locality("oceania", "koala", "eucalyptus"), + Locality(cluster_->hostsPerLocality()[2][0]->locality())); + EXPECT_EQ(2, cluster_->hostsPerLocality()[3].size()); + EXPECT_EQ(Locality("oceania", "koala", "ingsoc"), + Locality(cluster_->hostsPerLocality()[3][0]->locality())); +} + } // namespace Upstream } // namespace Envoy diff --git a/test/common/upstream/load_balancer_impl_test.cc b/test/common/upstream/load_balancer_impl_test.cc index e89b51794ae5..a9c97370a8ff 100644 --- a/test/common/upstream/load_balancer_impl_test.cc +++ b/test/common/upstream/load_balancer_impl_test.cc @@ -97,15 +97,15 @@ TEST_F(RoundRobinLoadBalancerTest, ZoneAwareSmallCluster) { new std::vector({makeTestHost(cluster_.info_, "tcp://127.0.0.1:80"), makeTestHost(cluster_.info_, "tcp://127.0.0.1:81"), makeTestHost(cluster_.info_, "tcp://127.0.0.1:82")})); - HostListsSharedPtr hosts_per_zone(new std::vector>( + HostListsSharedPtr hosts_per_locality(new std::vector>( {{makeTestHost(cluster_.info_, "tcp://127.0.0.1:81")}, {makeTestHost(cluster_.info_, "tcp://127.0.0.1:80")}, {makeTestHost(cluster_.info_, "tcp://127.0.0.1:82")}})); cluster_.hosts_ = *hosts; cluster_.healthy_hosts_ = *hosts; - cluster_.healthy_hosts_per_zone_ = *hosts_per_zone; - local_cluster_hosts_->updateHosts(hosts, hosts, hosts_per_zone, hosts_per_zone, + cluster_.healthy_hosts_per_locality_ = *hosts_per_locality; + local_cluster_hosts_->updateHosts(hosts, hosts, hosts_per_locality, hosts_per_locality, empty_host_vector_, empty_host_vector_); EXPECT_CALL(runtime_.snapshot_, getInteger("upstream.healthy_panic_threshold", 50)) @@ -125,9 +125,9 @@ TEST_F(RoundRobinLoadBalancerTest, ZoneAwareSmallCluster) { EXPECT_CALL(runtime_.snapshot_, getInteger("upstream.zone_routing.min_cluster_size", 6)) .WillRepeatedly(Return(1)); // Trigger reload. - local_cluster_hosts_->updateHosts(hosts, hosts, hosts_per_zone, hosts_per_zone, + local_cluster_hosts_->updateHosts(hosts, hosts, hosts_per_locality, hosts_per_locality, empty_host_vector_, empty_host_vector_); - EXPECT_EQ(cluster_.healthy_hosts_per_zone_[0][0], lb_->chooseHost(nullptr)); + EXPECT_EQ(cluster_.healthy_hosts_per_locality_[0][0], lb_->chooseHost(nullptr)); } TEST_F(RoundRobinLoadBalancerTest, NoZoneAwareDifferentZoneSize) { @@ -136,19 +136,20 @@ TEST_F(RoundRobinLoadBalancerTest, NoZoneAwareDifferentZoneSize) { new std::vector({makeTestHost(cluster_.info_, "tcp://127.0.0.1:80"), makeTestHost(cluster_.info_, "tcp://127.0.0.1:81"), makeTestHost(cluster_.info_, "tcp://127.0.0.1:82")})); - HostListsSharedPtr upstream_hosts_per_zone(new std::vector>( + HostListsSharedPtr upstream_hosts_per_locality(new std::vector>( {{makeTestHost(cluster_.info_, "tcp://127.0.0.1:81")}, {makeTestHost(cluster_.info_, "tcp://127.0.0.1:80")}, {makeTestHost(cluster_.info_, "tcp://127.0.0.1:82")}})); - HostListsSharedPtr local_hosts_per_zone(new std::vector>( + HostListsSharedPtr local_hosts_per_locality(new std::vector>( {{makeTestHost(cluster_.info_, "tcp://127.0.0.1:81")}, {makeTestHost(cluster_.info_, "tcp://127.0.0.1:80")}})); cluster_.healthy_hosts_ = *hosts; cluster_.hosts_ = *hosts; - cluster_.healthy_hosts_per_zone_ = *upstream_hosts_per_zone; - local_cluster_hosts_->updateHosts(hosts, hosts, local_hosts_per_zone, local_hosts_per_zone, - empty_host_vector_, empty_host_vector_); + cluster_.healthy_hosts_per_locality_ = *upstream_hosts_per_locality; + local_cluster_hosts_->updateHosts(hosts, hosts, local_hosts_per_locality, + local_hosts_per_locality, empty_host_vector_, + empty_host_vector_); EXPECT_CALL(runtime_.snapshot_, getInteger("upstream.healthy_panic_threshold", 50)) .WillRepeatedly(Return(50)); @@ -165,7 +166,7 @@ TEST_F(RoundRobinLoadBalancerTest, ZoneAwareRoutingLargeZoneSwitchOnOff) { new std::vector({makeTestHost(cluster_.info_, "tcp://127.0.0.1:80"), makeTestHost(cluster_.info_, "tcp://127.0.0.1:81"), makeTestHost(cluster_.info_, "tcp://127.0.0.1:82")})); - HostListsSharedPtr hosts_per_zone(new std::vector>( + HostListsSharedPtr hosts_per_locality(new std::vector>( {{makeTestHost(cluster_.info_, "tcp://127.0.0.1:81")}, {makeTestHost(cluster_.info_, "tcp://127.0.0.1:80")}, {makeTestHost(cluster_.info_, "tcp://127.0.0.1:82")}})); @@ -179,14 +180,14 @@ TEST_F(RoundRobinLoadBalancerTest, ZoneAwareRoutingLargeZoneSwitchOnOff) { cluster_.healthy_hosts_ = *hosts; cluster_.hosts_ = *hosts; - cluster_.healthy_hosts_per_zone_ = *hosts_per_zone; - local_cluster_hosts_->updateHosts(hosts, hosts, hosts_per_zone, hosts_per_zone, + cluster_.healthy_hosts_per_locality_ = *hosts_per_locality; + local_cluster_hosts_->updateHosts(hosts, hosts, hosts_per_locality, hosts_per_locality, empty_host_vector_, empty_host_vector_); // There is only one host in the given zone for zone aware routing. - EXPECT_EQ(cluster_.healthy_hosts_per_zone_[0][0], lb_->chooseHost(nullptr)); + EXPECT_EQ(cluster_.healthy_hosts_per_locality_[0][0], lb_->chooseHost(nullptr)); EXPECT_EQ(1U, stats_.lb_zone_routing_all_directly_.value()); - EXPECT_EQ(cluster_.healthy_hosts_per_zone_[0][0], lb_->chooseHost(nullptr)); + EXPECT_EQ(cluster_.healthy_hosts_per_locality_[0][0], lb_->chooseHost(nullptr)); EXPECT_EQ(2U, stats_.lb_zone_routing_all_directly_.value()); // Disable runtime global zone routing. @@ -208,14 +209,14 @@ TEST_F(RoundRobinLoadBalancerTest, ZoneAwareRoutingSmallZone) { makeTestHost(cluster_.info_, "tcp://127.0.0.1:1"), makeTestHost(cluster_.info_, "tcp://127.0.0.1:2")})); - HostListsSharedPtr upstream_hosts_per_zone(new std::vector>( + HostListsSharedPtr upstream_hosts_per_locality(new std::vector>( {{makeTestHost(cluster_.info_, "tcp://127.0.0.1:81")}, {makeTestHost(cluster_.info_, "tcp://127.0.0.1:80"), makeTestHost(cluster_.info_, "tcp://127.0.0.1:82")}, {makeTestHost(cluster_.info_, "tcp://127.0.0.1:83"), makeTestHost(cluster_.info_, "tcp://127.0.0.1:84")}})); - HostListsSharedPtr local_hosts_per_zone(new std::vector>( + HostListsSharedPtr local_hosts_per_locality(new std::vector>( {{makeTestHost(cluster_.info_, "tcp://127.0.0.1:0")}, {makeTestHost(cluster_.info_, "tcp://127.0.0.1:1")}, {makeTestHost(cluster_.info_, "tcp://127.0.0.1:2")}})); @@ -229,18 +230,19 @@ TEST_F(RoundRobinLoadBalancerTest, ZoneAwareRoutingSmallZone) { cluster_.healthy_hosts_ = *upstream_hosts; cluster_.hosts_ = *upstream_hosts; - cluster_.healthy_hosts_per_zone_ = *upstream_hosts_per_zone; - local_cluster_hosts_->updateHosts(local_hosts, local_hosts, local_hosts_per_zone, - local_hosts_per_zone, empty_host_vector_, empty_host_vector_); + cluster_.healthy_hosts_per_locality_ = *upstream_hosts_per_locality; + local_cluster_hosts_->updateHosts(local_hosts, local_hosts, local_hosts_per_locality, + local_hosts_per_locality, empty_host_vector_, + empty_host_vector_); // There is only one host in the given zone for zone aware routing. EXPECT_CALL(random_, random()).WillOnce(Return(100)); - EXPECT_EQ(cluster_.healthy_hosts_per_zone_[0][0], lb_->chooseHost(nullptr)); + EXPECT_EQ(cluster_.healthy_hosts_per_locality_[0][0], lb_->chooseHost(nullptr)); EXPECT_EQ(1U, stats_.lb_zone_routing_sampled_.value()); // Force request out of small zone. EXPECT_CALL(random_, random()).WillOnce(Return(9999)).WillOnce(Return(2)); - EXPECT_EQ(cluster_.healthy_hosts_per_zone_[1][1], lb_->chooseHost(nullptr)); + EXPECT_EQ(cluster_.healthy_hosts_per_locality_[1][1], lb_->chooseHost(nullptr)); EXPECT_EQ(1U, stats_.lb_zone_routing_cross_zone_.value()); } @@ -255,8 +257,8 @@ TEST_F(RoundRobinLoadBalancerTest, LowPrecisionForDistribution) { HostVectorSharedPtr local_hosts( new std::vector({makeTestHost(cluster_.info_, "tcp://127.0.0.1:0")})); - HostListsSharedPtr upstream_hosts_per_zone(new std::vector>()); - HostListsSharedPtr local_hosts_per_zone(new std::vector>()); + HostListsSharedPtr upstream_hosts_per_locality(new std::vector>()); + HostListsSharedPtr local_hosts_per_locality(new std::vector>()); EXPECT_CALL(runtime_.snapshot_, getInteger("upstream.healthy_panic_threshold", 50)) .WillRepeatedly(Return(50)); @@ -275,31 +277,32 @@ TEST_F(RoundRobinLoadBalancerTest, LowPrecisionForDistribution) { for (int i = 0; i < 45000; ++i) { current[i] = host; } - local_hosts_per_zone->push_back(current); + local_hosts_per_locality->push_back(current); current.resize(55000); for (int i = 0; i < 55000; ++i) { current[i] = host; } - local_hosts_per_zone->push_back(current); + local_hosts_per_locality->push_back(current); current.resize(44999); for (int i = 0; i < 44999; ++i) { current[i] = host; } - upstream_hosts_per_zone->push_back(current); + upstream_hosts_per_locality->push_back(current); current.resize(55001); for (int i = 0; i < 55001; ++i) { current[i] = host; } - upstream_hosts_per_zone->push_back(current); + upstream_hosts_per_locality->push_back(current); - cluster_.healthy_hosts_per_zone_ = *upstream_hosts_per_zone; + cluster_.healthy_hosts_per_locality_ = *upstream_hosts_per_locality; // To trigger update callback. - local_cluster_hosts_->updateHosts(local_hosts, local_hosts, local_hosts_per_zone, - local_hosts_per_zone, empty_host_vector_, empty_host_vector_); + local_cluster_hosts_->updateHosts(local_hosts, local_hosts, local_hosts_per_locality, + local_hosts_per_locality, empty_host_vector_, + empty_host_vector_); // Force request out of small zone and to randomly select zone. EXPECT_CALL(random_, random()).WillOnce(Return(9999)).WillOnce(Return(2)); @@ -311,13 +314,13 @@ TEST_F(RoundRobinLoadBalancerTest, NoZoneAwareRoutingOneZone) { init(true); HostVectorSharedPtr hosts( new std::vector({makeTestHost(cluster_.info_, "tcp://127.0.0.1:80")})); - HostListsSharedPtr hosts_per_zone(new std::vector>( + HostListsSharedPtr hosts_per_locality(new std::vector>( {{makeTestHost(cluster_.info_, "tcp://127.0.0.1:81")}})); cluster_.healthy_hosts_ = *hosts; cluster_.hosts_ = *hosts; - cluster_.healthy_hosts_per_zone_ = *hosts_per_zone; - local_cluster_hosts_->updateHosts(hosts, hosts, hosts_per_zone, hosts_per_zone, + cluster_.healthy_hosts_per_locality_ = *hosts_per_locality; + local_cluster_hosts_->updateHosts(hosts, hosts, hosts_per_locality, hosts_per_locality, empty_host_vector_, empty_host_vector_); EXPECT_EQ(cluster_.healthy_hosts_[0], lb_->chooseHost(nullptr)); } @@ -327,15 +330,15 @@ TEST_F(RoundRobinLoadBalancerTest, NoZoneAwareRoutingNotHealthy) { HostVectorSharedPtr hosts( new std::vector({makeTestHost(cluster_.info_, "tcp://127.0.0.1:80"), makeTestHost(cluster_.info_, "tcp://127.0.0.2:80")})); - HostListsSharedPtr hosts_per_zone(new std::vector>( + HostListsSharedPtr hosts_per_locality(new std::vector>( {{}, {makeTestHost(cluster_.info_, "tcp://127.0.0.1:80"), makeTestHost(cluster_.info_, "tcp://127.0.0.2:80")}})); cluster_.healthy_hosts_ = *hosts; cluster_.hosts_ = *hosts; - cluster_.healthy_hosts_per_zone_ = *hosts_per_zone; - local_cluster_hosts_->updateHosts(hosts, hosts, hosts_per_zone, hosts_per_zone, + cluster_.healthy_hosts_per_locality_ = *hosts_per_locality; + local_cluster_hosts_->updateHosts(hosts, hosts, hosts_per_locality, hosts_per_locality, empty_host_vector_, empty_host_vector_); // local zone has no healthy hosts, take from the all healthy hosts. @@ -350,10 +353,11 @@ TEST_F(RoundRobinLoadBalancerTest, NoZoneAwareRoutingLocalEmpty) { makeTestHost(cluster_.info_, "tcp://127.0.0.1:81")})); HostVectorSharedPtr local_hosts(new std::vector({}, {})); - HostListsSharedPtr upstream_hosts_per_zone(new std::vector>( + HostListsSharedPtr upstream_hosts_per_locality(new std::vector>( {{makeTestHost(cluster_.info_, "tcp://127.0.0.1:80")}, {makeTestHost(cluster_.info_, "tcp://127.0.0.1:81")}})); - HostListsSharedPtr local_hosts_per_zone(new std::vector>({{}, {}})); + HostListsSharedPtr local_hosts_per_locality( + new std::vector>({{}, {}})); EXPECT_CALL(runtime_.snapshot_, getInteger("upstream.healthy_panic_threshold", 50)) .WillOnce(Return(50)) @@ -365,9 +369,10 @@ TEST_F(RoundRobinLoadBalancerTest, NoZoneAwareRoutingLocalEmpty) { cluster_.healthy_hosts_ = *upstream_hosts; cluster_.hosts_ = *upstream_hosts; - cluster_.healthy_hosts_per_zone_ = *upstream_hosts_per_zone; - local_cluster_hosts_->updateHosts(local_hosts, local_hosts, local_hosts_per_zone, - local_hosts_per_zone, empty_host_vector_, empty_host_vector_); + cluster_.healthy_hosts_per_locality_ = *upstream_hosts_per_locality; + local_cluster_hosts_->updateHosts(local_hosts, local_hosts, local_hosts_per_locality, + local_hosts_per_locality, empty_host_vector_, + empty_host_vector_); // Local cluster is not OK, we'll do regular routing. EXPECT_EQ(cluster_.healthy_hosts_[0], lb_->chooseHost(nullptr)); diff --git a/test/common/upstream/load_balancer_simulation_test.cc b/test/common/upstream/load_balancer_simulation_test.cc index 9a6ea7f5c7b6..00c8a42dc78a 100644 --- a/test/common/upstream/load_balancer_simulation_test.cc +++ b/test/common/upstream/load_balancer_simulation_test.cc @@ -82,8 +82,8 @@ class DISABLED_SimulationTest : public testing::Test { per_zone_upstream->push_back((*upstream_per_zone_hosts)[zone]); } - cluster_.hosts_per_zone_ = *per_zone_upstream; - cluster_.healthy_hosts_per_zone_ = *per_zone_upstream; + cluster_.hosts_per_locality_ = *per_zone_upstream; + cluster_.healthy_hosts_per_locality_ = *per_zone_upstream; // Populate host set for originating cluster. HostListsSharedPtr per_zone_local(new std::vector>()); diff --git a/test/common/upstream/logical_dns_cluster_test.cc b/test/common/upstream/logical_dns_cluster_test.cc index 752f8d30c7af..b64982241253 100644 --- a/test/common/upstream/logical_dns_cluster_test.cc +++ b/test/common/upstream/logical_dns_cluster_test.cc @@ -170,8 +170,8 @@ TEST_F(LogicalDnsClusterTest, Basic) { EXPECT_EQ(1UL, cluster_->hosts().size()); EXPECT_EQ(1UL, cluster_->healthyHosts().size()); - EXPECT_EQ(0UL, cluster_->hostsPerZone().size()); - EXPECT_EQ(0UL, cluster_->healthyHostsPerZone().size()); + EXPECT_EQ(0UL, cluster_->hostsPerLocality().size()); + EXPECT_EQ(0UL, cluster_->healthyHostsPerLocality().size()); EXPECT_EQ(cluster_->hosts()[0], cluster_->healthyHosts()[0]); HostSharedPtr logical_host = cluster_->hosts()[0]; diff --git a/test/common/upstream/original_dst_cluster_test.cc b/test/common/upstream/original_dst_cluster_test.cc index 76c8a7ae4386..537c126e975b 100644 --- a/test/common/upstream/original_dst_cluster_test.cc +++ b/test/common/upstream/original_dst_cluster_test.cc @@ -136,8 +136,8 @@ TEST_F(OriginalDstClusterTest, NoContext) { EXPECT_EQ(0UL, cluster_->hosts().size()); EXPECT_EQ(0UL, cluster_->healthyHosts().size()); - EXPECT_EQ(0UL, cluster_->hostsPerZone().size()); - EXPECT_EQ(0UL, cluster_->healthyHostsPerZone().size()); + EXPECT_EQ(0UL, cluster_->hostsPerLocality().size()); + EXPECT_EQ(0UL, cluster_->healthyHostsPerLocality().size()); // No downstream connection => no host. { @@ -194,8 +194,8 @@ TEST_F(OriginalDstClusterTest, Membership) { EXPECT_EQ(0UL, cluster_->hosts().size()); EXPECT_EQ(0UL, cluster_->healthyHosts().size()); - EXPECT_EQ(0UL, cluster_->hostsPerZone().size()); - EXPECT_EQ(0UL, cluster_->healthyHostsPerZone().size()); + EXPECT_EQ(0UL, cluster_->hostsPerLocality().size()); + EXPECT_EQ(0UL, cluster_->healthyHostsPerLocality().size()); EXPECT_CALL(membership_updated_, ready()); @@ -219,8 +219,8 @@ TEST_F(OriginalDstClusterTest, Membership) { EXPECT_EQ(1UL, cluster_->hosts().size()); EXPECT_EQ(1UL, cluster_->healthyHosts().size()); - EXPECT_EQ(0UL, cluster_->hostsPerZone().size()); - EXPECT_EQ(0UL, cluster_->healthyHostsPerZone().size()); + EXPECT_EQ(0UL, cluster_->hostsPerLocality().size()); + EXPECT_EQ(0UL, cluster_->healthyHostsPerLocality().size()); EXPECT_EQ(host, cluster_->hosts()[0]); EXPECT_EQ(local_address, *cluster_->hosts()[0]->address()); @@ -277,8 +277,8 @@ TEST_F(OriginalDstClusterTest, Membership2) { EXPECT_EQ(0UL, cluster_->hosts().size()); EXPECT_EQ(0UL, cluster_->healthyHosts().size()); - EXPECT_EQ(0UL, cluster_->hostsPerZone().size()); - EXPECT_EQ(0UL, cluster_->healthyHostsPerZone().size()); + EXPECT_EQ(0UL, cluster_->hostsPerLocality().size()); + EXPECT_EQ(0UL, cluster_->healthyHostsPerLocality().size()); // Host gets the local address of the downstream connection. @@ -313,8 +313,8 @@ TEST_F(OriginalDstClusterTest, Membership2) { EXPECT_EQ(2UL, cluster_->hosts().size()); EXPECT_EQ(2UL, cluster_->healthyHosts().size()); - EXPECT_EQ(0UL, cluster_->hostsPerZone().size()); - EXPECT_EQ(0UL, cluster_->healthyHostsPerZone().size()); + EXPECT_EQ(0UL, cluster_->hostsPerLocality().size()); + EXPECT_EQ(0UL, cluster_->healthyHostsPerLocality().size()); EXPECT_EQ(host1, cluster_->hosts()[0]); EXPECT_EQ(local_address1, *cluster_->hosts()[0]->address()); @@ -361,8 +361,8 @@ TEST_F(OriginalDstClusterTest, Connection) { EXPECT_EQ(0UL, cluster_->hosts().size()); EXPECT_EQ(0UL, cluster_->healthyHosts().size()); - EXPECT_EQ(0UL, cluster_->hostsPerZone().size()); - EXPECT_EQ(0UL, cluster_->healthyHostsPerZone().size()); + EXPECT_EQ(0UL, cluster_->hostsPerLocality().size()); + EXPECT_EQ(0UL, cluster_->healthyHostsPerLocality().size()); EXPECT_CALL(membership_updated_, ready()); diff --git a/test/common/upstream/sds_test.cc b/test/common/upstream/sds_test.cc index d5dbdb5b4665..ca8cbd05abc6 100644 --- a/test/common/upstream/sds_test.cc +++ b/test/common/upstream/sds_test.cc @@ -50,7 +50,7 @@ class SdsTest : public testing::Test { )EOF"; timer_ = new Event::MockTimer(&dispatcher_); - local_info_.zone_name_ = "us-east-1a"; + local_info_.node_.mutable_locality()->set_zone("us-east-1a"); envoy::api::v2::ConfigSource eds_config; eds_config.mutable_api_config_source()->add_cluster_name("sds"); eds_config.mutable_api_config_source()->mutable_refresh_delay()->set_seconds(1); @@ -151,10 +151,10 @@ TEST_F(SdsTest, NoHealthChecker) { EXPECT_EQ(13UL, cluster_->hosts().size()); EXPECT_EQ(13UL, cluster_->healthyHosts().size()); EXPECT_EQ(13UL, cluster_->info()->stats().membership_healthy_.value()); - EXPECT_EQ(3UL, cluster_->healthyHostsPerZone().size()); - EXPECT_EQ(4UL, cluster_->healthyHostsPerZone()[0].size()); - EXPECT_EQ(5UL, cluster_->healthyHostsPerZone()[1].size()); - EXPECT_EQ(4UL, cluster_->healthyHostsPerZone()[2].size()); + EXPECT_EQ(3UL, cluster_->healthyHostsPerLocality().size()); + EXPECT_EQ(4UL, cluster_->healthyHostsPerLocality()[0].size()); + EXPECT_EQ(5UL, cluster_->healthyHostsPerLocality()[1].size()); + EXPECT_EQ(4UL, cluster_->healthyHostsPerLocality()[2].size()); // Hosts in SDS and static clusters should have empty hostname EXPECT_EQ("", cluster_->hosts()[0]->hostname()); @@ -184,10 +184,10 @@ TEST_F(SdsTest, NoHealthChecker) { EXPECT_EQ("us-east-1d", canary_host->locality().zone()); EXPECT_EQ(50U, canary_host->weight()); EXPECT_EQ(50UL, cluster_->info()->stats().max_host_weight_.value()); - EXPECT_EQ(3UL, cluster_->healthyHostsPerZone().size()); - EXPECT_EQ(4UL, cluster_->healthyHostsPerZone()[0].size()); - EXPECT_EQ(5UL, cluster_->healthyHostsPerZone()[1].size()); - EXPECT_EQ(4UL, cluster_->healthyHostsPerZone()[2].size()); + EXPECT_EQ(3UL, cluster_->healthyHostsPerLocality().size()); + EXPECT_EQ(4UL, cluster_->healthyHostsPerLocality()[0].size()); + EXPECT_EQ(5UL, cluster_->healthyHostsPerLocality()[1].size()); + EXPECT_EQ(4UL, cluster_->healthyHostsPerLocality()[2].size()); // Now test the failure case, our cluster size should not change. setupRequest(); @@ -198,10 +198,10 @@ TEST_F(SdsTest, NoHealthChecker) { EXPECT_EQ(13UL, cluster_->hosts().size()); EXPECT_EQ(50U, canary_host->weight()); EXPECT_EQ(50UL, cluster_->info()->stats().max_host_weight_.value()); - EXPECT_EQ(3UL, cluster_->healthyHostsPerZone().size()); - EXPECT_EQ(4UL, cluster_->healthyHostsPerZone()[0].size()); - EXPECT_EQ(5UL, cluster_->healthyHostsPerZone()[1].size()); - EXPECT_EQ(4UL, cluster_->healthyHostsPerZone()[2].size()); + EXPECT_EQ(3UL, cluster_->healthyHostsPerLocality().size()); + EXPECT_EQ(4UL, cluster_->healthyHostsPerLocality()[0].size()); + EXPECT_EQ(5UL, cluster_->healthyHostsPerLocality()[1].size()); + EXPECT_EQ(4UL, cluster_->healthyHostsPerLocality()[2].size()); // 503 response. setupRequest(); @@ -215,10 +215,10 @@ TEST_F(SdsTest, NoHealthChecker) { EXPECT_EQ(13UL, cluster_->hosts().size()); EXPECT_EQ(50U, canary_host->weight()); EXPECT_EQ(50UL, cluster_->info()->stats().max_host_weight_.value()); - EXPECT_EQ(3UL, cluster_->healthyHostsPerZone().size()); - EXPECT_EQ(4UL, cluster_->healthyHostsPerZone()[0].size()); - EXPECT_EQ(5UL, cluster_->healthyHostsPerZone()[1].size()); - EXPECT_EQ(4UL, cluster_->healthyHostsPerZone()[2].size()); + EXPECT_EQ(3UL, cluster_->healthyHostsPerLocality().size()); + EXPECT_EQ(4UL, cluster_->healthyHostsPerLocality()[0].size()); + EXPECT_EQ(5UL, cluster_->healthyHostsPerLocality()[1].size()); + EXPECT_EQ(4UL, cluster_->healthyHostsPerLocality()[2].size()); } TEST_F(SdsTest, HealthChecker) { @@ -247,11 +247,11 @@ TEST_F(SdsTest, HealthChecker) { EXPECT_EQ(0UL, cluster_->healthyHosts().size()); EXPECT_EQ(0UL, cluster_->info()->stats().membership_healthy_.value()); EXPECT_EQ(0UL, numHealthy()); - EXPECT_EQ(3UL, cluster_->healthyHostsPerZone().size()); - EXPECT_EQ(3UL, cluster_->hostsPerZone().size()); - EXPECT_EQ(0UL, cluster_->healthyHostsPerZone()[0].size()); - EXPECT_EQ(0UL, cluster_->healthyHostsPerZone()[1].size()); - EXPECT_EQ(0UL, cluster_->healthyHostsPerZone()[2].size()); + EXPECT_EQ(3UL, cluster_->healthyHostsPerLocality().size()); + EXPECT_EQ(3UL, cluster_->hostsPerLocality().size()); + EXPECT_EQ(0UL, cluster_->healthyHostsPerLocality()[0].size()); + EXPECT_EQ(0UL, cluster_->healthyHostsPerLocality()[1].size()); + EXPECT_EQ(0UL, cluster_->healthyHostsPerLocality()[2].size()); // Now run through and make all the hosts healthy except for the first one. for (size_t i = 1; i < cluster_->hosts().size(); i++) { @@ -261,10 +261,10 @@ TEST_F(SdsTest, HealthChecker) { EXPECT_EQ(12UL, cluster_->healthyHosts().size()); EXPECT_EQ(12UL, cluster_->info()->stats().membership_healthy_.value()); - EXPECT_EQ(3UL, cluster_->healthyHostsPerZone().size()); - EXPECT_EQ(3UL, cluster_->healthyHostsPerZone()[0].size()); - EXPECT_EQ(5UL, cluster_->healthyHostsPerZone()[1].size()); - EXPECT_EQ(4UL, cluster_->healthyHostsPerZone()[2].size()); + EXPECT_EQ(3UL, cluster_->healthyHostsPerLocality().size()); + EXPECT_EQ(3UL, cluster_->healthyHostsPerLocality()[0].size()); + EXPECT_EQ(5UL, cluster_->healthyHostsPerLocality()[1].size()); + EXPECT_EQ(4UL, cluster_->healthyHostsPerLocality()[2].size()); // Do the last one now which should fire the initialized event. EXPECT_CALL(membership_updated_, ready()); @@ -273,10 +273,10 @@ TEST_F(SdsTest, HealthChecker) { EXPECT_EQ("hash_5f3725fa79001155", cluster_->versionInfo()); EXPECT_EQ(13UL, cluster_->healthyHosts().size()); EXPECT_EQ(13UL, cluster_->info()->stats().membership_healthy_.value()); - EXPECT_EQ(3UL, cluster_->healthyHostsPerZone().size()); - EXPECT_EQ(4UL, cluster_->healthyHostsPerZone()[0].size()); - EXPECT_EQ(5UL, cluster_->healthyHostsPerZone()[1].size()); - EXPECT_EQ(4UL, cluster_->healthyHostsPerZone()[2].size()); + EXPECT_EQ(3UL, cluster_->healthyHostsPerLocality().size()); + EXPECT_EQ(4UL, cluster_->healthyHostsPerLocality()[0].size()); + EXPECT_EQ(5UL, cluster_->healthyHostsPerLocality()[1].size()); + EXPECT_EQ(4UL, cluster_->healthyHostsPerLocality()[2].size()); // Now we will remove some hosts, but since they are all healthy, they shouldn't actually be gone. setupRequest(); @@ -293,10 +293,10 @@ TEST_F(SdsTest, HealthChecker) { EXPECT_EQ(13UL, cluster_->healthyHosts().size()); EXPECT_EQ(13UL, cluster_->info()->stats().membership_healthy_.value()); EXPECT_EQ(13UL, numHealthy()); - EXPECT_EQ(3UL, cluster_->healthyHostsPerZone().size()); - EXPECT_EQ(4UL, cluster_->healthyHostsPerZone()[0].size()); - EXPECT_EQ(5UL, cluster_->healthyHostsPerZone()[1].size()); - EXPECT_EQ(4UL, cluster_->healthyHostsPerZone()[2].size()); + EXPECT_EQ(3UL, cluster_->healthyHostsPerLocality().size()); + EXPECT_EQ(4UL, cluster_->healthyHostsPerLocality()[0].size()); + EXPECT_EQ(5UL, cluster_->healthyHostsPerLocality()[1].size()); + EXPECT_EQ(4UL, cluster_->healthyHostsPerLocality()[2].size()); // Now set one of the removed hosts to unhealthy, and return the same query again, this should // remove it. @@ -314,10 +314,10 @@ TEST_F(SdsTest, HealthChecker) { EXPECT_EQ(12UL, cluster_->healthyHosts().size()); EXPECT_EQ(12UL, cluster_->info()->stats().membership_healthy_.value()); EXPECT_EQ(12UL, numHealthy()); - EXPECT_EQ(3UL, cluster_->healthyHostsPerZone().size()); - EXPECT_EQ(3UL, cluster_->healthyHostsPerZone()[0].size()); - EXPECT_EQ(5UL, cluster_->healthyHostsPerZone()[1].size()); - EXPECT_EQ(4UL, cluster_->healthyHostsPerZone()[2].size()); + EXPECT_EQ(3UL, cluster_->healthyHostsPerLocality().size()); + EXPECT_EQ(3UL, cluster_->healthyHostsPerLocality()[0].size()); + EXPECT_EQ(5UL, cluster_->healthyHostsPerLocality()[1].size()); + EXPECT_EQ(4UL, cluster_->healthyHostsPerLocality()[2].size()); // Now add back one of the hosts that was previously missing but we still have and make sure // nothing changes. @@ -334,10 +334,10 @@ TEST_F(SdsTest, HealthChecker) { EXPECT_EQ(12UL, cluster_->healthyHosts().size()); EXPECT_EQ(12UL, cluster_->info()->stats().membership_healthy_.value()); EXPECT_EQ(12UL, numHealthy()); - EXPECT_EQ(3UL, cluster_->healthyHostsPerZone().size()); - EXPECT_EQ(3UL, cluster_->healthyHostsPerZone()[0].size()); - EXPECT_EQ(5UL, cluster_->healthyHostsPerZone()[1].size()); - EXPECT_EQ(4UL, cluster_->healthyHostsPerZone()[2].size()); + EXPECT_EQ(3UL, cluster_->healthyHostsPerLocality().size()); + EXPECT_EQ(3UL, cluster_->healthyHostsPerLocality()[0].size()); + EXPECT_EQ(5UL, cluster_->healthyHostsPerLocality()[1].size()); + EXPECT_EQ(4UL, cluster_->healthyHostsPerLocality()[2].size()); } TEST_F(SdsTest, Failure) { diff --git a/test/common/upstream/upstream_impl_test.cc b/test/common/upstream/upstream_impl_test.cc index 56ac6246353b..67e1034177d2 100644 --- a/test/common/upstream/upstream_impl_test.cc +++ b/test/common/upstream/upstream_impl_test.cc @@ -239,8 +239,8 @@ TEST(StrictDnsClusterImplTest, Basic) { ContainerEq(hostListToAddresses(cluster.hosts()))); EXPECT_EQ(2UL, cluster.healthyHosts().size()); - EXPECT_EQ(0UL, cluster.hostsPerZone().size()); - EXPECT_EQ(0UL, cluster.healthyHostsPerZone().size()); + EXPECT_EQ(0UL, cluster.hostsPerLocality().size()); + EXPECT_EQ(0UL, cluster.healthyHostsPerLocality().size()); for (const HostSharedPtr& host : cluster.hosts()) { EXPECT_EQ(cluster.info().get(), &host->cluster()); @@ -480,8 +480,8 @@ TEST(StaticClusterImplTest, UrlConfig) { EXPECT_THAT(std::list({"10.0.0.1:11001", "10.0.0.2:11002"}), ContainerEq(hostListToAddresses(cluster.hosts()))); EXPECT_EQ(2UL, cluster.healthyHosts().size()); - EXPECT_EQ(0UL, cluster.hostsPerZone().size()); - EXPECT_EQ(0UL, cluster.healthyHostsPerZone().size()); + EXPECT_EQ(0UL, cluster.hostsPerLocality().size()); + EXPECT_EQ(0UL, cluster.healthyHostsPerLocality().size()); cluster.hosts()[0]->healthChecker().setUnhealthy(); } diff --git a/test/mocks/local_info/mocks.cc b/test/mocks/local_info/mocks.cc index b98d86e06da3..c5addcc3912c 100644 --- a/test/mocks/local_info/mocks.cc +++ b/test/mocks/local_info/mocks.cc @@ -5,18 +5,21 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" +using testing::Invoke; using testing::Return; -using testing::ReturnPointee; using testing::ReturnRef; namespace Envoy { namespace LocalInfo { MockLocalInfo::MockLocalInfo() : address_(new Network::Address::Ipv4Instance("127.0.0.1")) { + node_.set_id("node_name"); + node_.set_cluster("cluster_name"); + node_.mutable_locality()->set_zone("zone_name"); ON_CALL(*this, address()).WillByDefault(Return(address_)); - ON_CALL(*this, zoneName()).WillByDefault(ReturnPointee(&zone_name_)); - ON_CALL(*this, clusterName()).WillByDefault(ReturnPointee(&cluster_name_)); - ON_CALL(*this, nodeName()).WillByDefault(ReturnPointee(&node_name_)); + ON_CALL(*this, zoneName()).WillByDefault(Invoke([this] { return node_.locality().zone(); })); + ON_CALL(*this, clusterName()).WillByDefault(Invoke([this] { return node_.cluster(); })); + ON_CALL(*this, nodeName()).WillByDefault(Invoke([this] { return node_.id(); })); ON_CALL(*this, node()).WillByDefault(ReturnRef(node_)); } diff --git a/test/mocks/local_info/mocks.h b/test/mocks/local_info/mocks.h index 93ad61a9ea81..d1853bffafec 100644 --- a/test/mocks/local_info/mocks.h +++ b/test/mocks/local_info/mocks.h @@ -21,9 +21,6 @@ class MockLocalInfo : public LocalInfo { MOCK_CONST_METHOD0(node, envoy::api::v2::Node&()); Network::Address::InstanceConstSharedPtr address_; - std::string zone_name_{"zone_name"}; - std::string cluster_name_{"cluster_name"}; - std::string node_name_{"node_name"}; // TODO(htuch): Make this behave closer to the real implementation, with the various property // methods using node_ as the source of truth. envoy::api::v2::Node node_; diff --git a/test/mocks/upstream/mocks.cc b/test/mocks/upstream/mocks.cc index 3cba6823cbf0..b1d42004bc56 100644 --- a/test/mocks/upstream/mocks.cc +++ b/test/mocks/upstream/mocks.cc @@ -89,8 +89,8 @@ MockCluster::MockCluster() { })); ON_CALL(*this, hosts()).WillByDefault(ReturnRef(hosts_)); ON_CALL(*this, healthyHosts()).WillByDefault(ReturnRef(healthy_hosts_)); - ON_CALL(*this, hostsPerZone()).WillByDefault(ReturnRef(hosts_per_zone_)); - ON_CALL(*this, healthyHostsPerZone()).WillByDefault(ReturnRef(healthy_hosts_per_zone_)); + ON_CALL(*this, hostsPerLocality()).WillByDefault(ReturnRef(hosts_per_locality_)); + ON_CALL(*this, healthyHostsPerLocality()).WillByDefault(ReturnRef(healthy_hosts_per_locality_)); ON_CALL(*this, info()).WillByDefault(Return(info_)); ON_CALL(*this, setInitializedCb(_)) .WillByDefault(Invoke([this](std::function callback) -> void { diff --git a/test/mocks/upstream/mocks.h b/test/mocks/upstream/mocks.h index cd811d755c37..68142018c97f 100644 --- a/test/mocks/upstream/mocks.h +++ b/test/mocks/upstream/mocks.h @@ -40,8 +40,8 @@ class MockCluster : public Cluster { MOCK_CONST_METHOD1(addMemberUpdateCb, Common::CallbackHandle*(MemberUpdateCb callback)); MOCK_CONST_METHOD0(hosts, const std::vector&()); MOCK_CONST_METHOD0(healthyHosts, const std::vector&()); - MOCK_CONST_METHOD0(hostsPerZone, const std::vector>&()); - MOCK_CONST_METHOD0(healthyHostsPerZone, const std::vector>&()); + MOCK_CONST_METHOD0(hostsPerLocality, const std::vector>&()); + MOCK_CONST_METHOD0(healthyHostsPerLocality, const std::vector>&()); // Upstream::Cluster MOCK_CONST_METHOD0(info, ClusterInfoConstSharedPtr()); @@ -53,8 +53,8 @@ class MockCluster : public Cluster { std::vector hosts_; std::vector healthy_hosts_; - std::vector> hosts_per_zone_; - std::vector> healthy_hosts_per_zone_; + std::vector> hosts_per_locality_; + std::vector> healthy_hosts_per_locality_; Common::CallbackManager&, const std::vector&> member_update_cb_helper_; std::shared_ptr info_{new NiceMock()}; diff --git a/test/server/configuration_impl_test.cc b/test/server/configuration_impl_test.cc index 0d8da5887331..3251e6039a17 100644 --- a/test/server/configuration_impl_test.cc +++ b/test/server/configuration_impl_test.cc @@ -153,7 +153,7 @@ TEST_F(ConfigurationImplTest, ServiceClusterNotSetWhenLSTracing) { envoy::api::v2::Bootstrap bootstrap = TestUtility::parseBootstrapFromJson(json); - server_.local_info_.cluster_name_ = ""; + server_.local_info_.node_.set_cluster(""); MainImpl config; EXPECT_THROW(config.initialize(bootstrap, server_, cluster_manager_factory_), EnvoyException); } @@ -176,7 +176,7 @@ TEST_F(ConfigurationImplTest, NullTracerSetWhenTracingConfigurationAbsent) { envoy::api::v2::Bootstrap bootstrap = TestUtility::parseBootstrapFromJson(json); - server_.local_info_.cluster_name_ = ""; + server_.local_info_.node_.set_cluster(""); MainImpl config; config.initialize(bootstrap, server_, cluster_manager_factory_); @@ -212,7 +212,7 @@ TEST_F(ConfigurationImplTest, NullTracerSetWhenHttpKeyAbsentFromTracerConfigurat envoy::api::v2::Bootstrap bootstrap = TestUtility::parseBootstrapFromJson(json); - server_.local_info_.cluster_name_ = ""; + server_.local_info_.node_.set_cluster(""); MainImpl config; config.initialize(bootstrap, server_, cluster_manager_factory_);