LCOV - code coverage report
Current view: top level - source/extensions/clusters/static - static_cluster.cc (source / functions) Hit Total Coverage
Test: coverage.dat Lines: 41 47 87.2 %
Date: 2024-01-05 06:35:25 Functions: 3 3 100.0 %

          Line data    Source code
       1             : #include "source/extensions/clusters/static/static_cluster.h"
       2             : 
       3             : #include "envoy/common/exception.h"
       4             : #include "envoy/config/cluster/v3/cluster.pb.h"
       5             : #include "envoy/config/endpoint/v3/endpoint.pb.h"
       6             : 
       7             : namespace Envoy {
       8             : namespace Upstream {
       9             : 
      10             : StaticClusterImpl::StaticClusterImpl(const envoy::config::cluster::v3::Cluster& cluster,
      11             :                                      ClusterFactoryContext& context)
      12             :     : ClusterImplBase(cluster, context),
      13             :       priority_state_manager_(
      14         131 :           new PriorityStateManager(*this, context.serverFactoryContext().localInfo(), nullptr)) {
      15         131 :   const envoy::config::endpoint::v3::ClusterLoadAssignment& cluster_load_assignment =
      16         131 :       cluster.load_assignment();
      17         131 :   overprovisioning_factor_ = PROTOBUF_GET_WRAPPED_OR_DEFAULT(
      18         131 :       cluster_load_assignment.policy(), overprovisioning_factor, kDefaultOverProvisioningFactor);
      19         131 :   weighted_priority_health_ = cluster_load_assignment.policy().weighted_priority_health();
      20             : 
      21         131 :   Event::Dispatcher& dispatcher = context.serverFactoryContext().mainThreadDispatcher();
      22             : 
      23         131 :   for (const auto& locality_lb_endpoint : cluster_load_assignment.endpoints()) {
      24         131 :     validateEndpointsForZoneAwareRouting(locality_lb_endpoint);
      25         131 :     priority_state_manager_->initializePriorityFor(locality_lb_endpoint);
      26         131 :     for (const auto& lb_endpoint : locality_lb_endpoint.lb_endpoints()) {
      27         131 :       priority_state_manager_->registerHostForPriority(
      28         131 :           lb_endpoint.endpoint().hostname(), resolveProtoAddress(lb_endpoint.endpoint().address()),
      29         131 :           {}, locality_lb_endpoint, lb_endpoint, dispatcher.timeSource());
      30         131 :     }
      31         131 :   }
      32         131 : }
      33             : 
      34         131 : void StaticClusterImpl::startPreInit() {
      35             :   // At this point see if we have a health checker. If so, mark all the hosts unhealthy and
      36             :   // then fire update callbacks to start the health checking process.
      37         131 :   const auto& health_checker_flag =
      38         131 :       health_checker_ != nullptr
      39         131 :           ? absl::optional<Upstream::Host::HealthFlag>(Host::HealthFlag::FAILED_ACTIVE_HC)
      40         131 :           : absl::nullopt;
      41             : 
      42         131 :   auto& priority_state = priority_state_manager_->priorityState();
      43         262 :   for (size_t i = 0; i < priority_state.size(); ++i) {
      44         131 :     if (priority_state[i].first == nullptr) {
      45           0 :       priority_state[i].first = std::make_unique<HostVector>();
      46           0 :     }
      47         131 :     priority_state_manager_->updateClusterPrioritySet(
      48         131 :         i, std::move(priority_state[i].first), absl::nullopt, absl::nullopt, health_checker_flag,
      49         131 :         weighted_priority_health_, overprovisioning_factor_);
      50         131 :   }
      51         131 :   priority_state_manager_.reset();
      52             : 
      53         131 :   onPreInitComplete();
      54         131 : }
      55             : 
      56             : absl::StatusOr<std::pair<ClusterImplBaseSharedPtr, ThreadAwareLoadBalancerPtr>>
      57             : StaticClusterFactory::createClusterImpl(const envoy::config::cluster::v3::Cluster& cluster,
      58         131 :                                         ClusterFactoryContext& context) {
      59         131 :   const envoy::config::endpoint::v3::ClusterLoadAssignment& cluster_load_assignment =
      60         131 :       cluster.load_assignment();
      61         131 :   for (const auto& locality_lb_endpoint : cluster_load_assignment.endpoints()) {
      62             :     // TODO(adisuissa): Implement LEDS support for STATIC clusters.
      63         131 :     if (locality_lb_endpoint.has_leds_cluster_locality_config()) {
      64           0 :       return absl::InvalidArgumentError(
      65           0 :           fmt::format("LEDS is only supported when EDS is used. Static cluster {} cannot use LEDS.",
      66           0 :                       cluster.name()));
      67           0 :     }
      68         131 :   }
      69         131 :   return std::make_pair(std::shared_ptr<StaticClusterImpl>(new StaticClusterImpl(cluster, context)),
      70         131 :                         nullptr);
      71         131 : }
      72             : 
      73             : /**
      74             :  * Static registration for the static cluster factory. @see RegisterFactory.
      75             :  */
      76             : REGISTER_FACTORY(StaticClusterFactory, ClusterFactory);
      77             : 
      78             : } // namespace Upstream
      79             : } // namespace Envoy

Generated by: LCOV version 1.15