Line data Source code
1 : #pragma once 2 : 3 : #include "envoy/common/time.h" 4 : #include "envoy/config/core/v3/base.pb.h" 5 : #include "envoy/config/endpoint/v3/endpoint_components.pb.h" 6 : 7 : #include "source/common/upstream/upstream_impl.h" 8 : 9 : namespace Envoy { 10 : namespace Upstream { 11 : 12 : /** 13 : * A host implementation that can have its address changed in order to create temporal "real" 14 : * hosts. 15 : */ 16 : class LogicalHost : public HostImpl { 17 : public: 18 : LogicalHost( 19 : const ClusterInfoConstSharedPtr& cluster, const std::string& hostname, 20 : const Network::Address::InstanceConstSharedPtr& address, 21 : const std::vector<Network::Address::InstanceConstSharedPtr>& address_list, 22 : const envoy::config::endpoint::v3::LocalityLbEndpoints& locality_lb_endpoint, 23 : const envoy::config::endpoint::v3::LbEndpoint& lb_endpoint, 24 : const Network::TransportSocketOptionsConstSharedPtr& override_transport_socket_options, 25 : TimeSource& time_source) 26 : : HostImpl(cluster, hostname, address, 27 : // TODO(zyfjeff): Created through metadata shared pool 28 : std::make_shared<const envoy::config::core::v3::Metadata>(lb_endpoint.metadata()), 29 : lb_endpoint.load_balancing_weight().value(), locality_lb_endpoint.locality(), 30 : lb_endpoint.endpoint().health_check_config(), locality_lb_endpoint.priority(), 31 : lb_endpoint.health_status(), time_source), 32 0 : override_transport_socket_options_(override_transport_socket_options) { 33 0 : setAddressList(address_list); 34 0 : } 35 : 36 : // Set the new address. Updates are typically rare so a R/W lock is used for address updates. 37 : // Note that the health check address update requires no lock to be held since it is only 38 : // used on the main thread, but we do so anyway since it shouldn't be perf critical and will 39 : // future proof the code. 40 : void setNewAddresses(const Network::Address::InstanceConstSharedPtr& address, 41 : const std::vector<Network::Address::InstanceConstSharedPtr>& address_list, 42 0 : const envoy::config::endpoint::v3::LbEndpoint& lb_endpoint) { 43 0 : const auto& health_check_config = lb_endpoint.endpoint().health_check_config(); 44 0 : auto health_check_address = resolveHealthCheckAddress(health_check_config, address); 45 0 : absl::WriterMutexLock lock(&address_lock_); 46 0 : setAddress(address); 47 0 : setAddressList(address_list); 48 0 : /* TODO: the health checker only gets the first address in the list and 49 0 : * will not walk the full happy eyeballs list. We should eventually fix 50 0 : * this. */ 51 0 : setHealthCheckAddress(health_check_address); 52 0 : } 53 : 54 : // Upstream::Host 55 : CreateConnectionData createConnection( 56 : Event::Dispatcher& dispatcher, const Network::ConnectionSocket::OptionsSharedPtr& options, 57 : Network::TransportSocketOptionsConstSharedPtr transport_socket_options) const override; 58 : 59 : // Upstream::HostDescription 60 0 : Network::Address::InstanceConstSharedPtr address() const override { 61 0 : absl::ReaderMutexLock lock(&address_lock_); 62 0 : return HostImpl::address(); 63 0 : } 64 0 : Network::Address::InstanceConstSharedPtr healthCheckAddress() const override { 65 0 : absl::ReaderMutexLock lock(&address_lock_); 66 0 : return HostImpl::healthCheckAddress(); 67 0 : } 68 : 69 : private: 70 : const Network::TransportSocketOptionsConstSharedPtr override_transport_socket_options_; 71 : mutable absl::Mutex address_lock_; 72 : }; 73 : 74 : using LogicalHostSharedPtr = std::shared_ptr<LogicalHost>; 75 : 76 : /** 77 : * A real host that forwards most of its calls to a logical host, but returns a snapped address. 78 : */ 79 : class RealHostDescription : public HostDescription { 80 : public: 81 : RealHostDescription(Network::Address::InstanceConstSharedPtr address, 82 : HostConstSharedPtr logical_host) 83 0 : : address_(address), logical_host_(logical_host) {} 84 : 85 : // Upstream:HostDescription 86 0 : bool canary() const override { return logical_host_->canary(); } 87 0 : void canary(bool) override {} 88 0 : MetadataConstSharedPtr metadata() const override { return logical_host_->metadata(); } 89 0 : void metadata(MetadataConstSharedPtr) override {} 90 : 91 0 : Network::UpstreamTransportSocketFactory& transportSocketFactory() const override { 92 0 : return logical_host_->transportSocketFactory(); 93 0 : } 94 0 : const ClusterInfo& cluster() const override { return logical_host_->cluster(); } 95 0 : bool canCreateConnection(Upstream::ResourcePriority priority) const override { 96 0 : return logical_host_->canCreateConnection(priority); 97 0 : } 98 0 : HealthCheckHostMonitor& healthChecker() const override { return logical_host_->healthChecker(); } 99 0 : Outlier::DetectorHostMonitor& outlierDetector() const override { 100 0 : return logical_host_->outlierDetector(); 101 0 : } 102 0 : HostStats& stats() const override { return logical_host_->stats(); } 103 0 : LoadMetricStats& loadMetricStats() const override { return logical_host_->loadMetricStats(); } 104 0 : const std::string& hostnameForHealthChecks() const override { 105 0 : return logical_host_->hostnameForHealthChecks(); 106 0 : } 107 0 : const std::string& hostname() const override { return logical_host_->hostname(); } 108 0 : Network::Address::InstanceConstSharedPtr address() const override { return address_; } 109 0 : const std::vector<Network::Address::InstanceConstSharedPtr>& addressList() const override { 110 0 : return logical_host_->addressList(); 111 0 : } 112 0 : const envoy::config::core::v3::Locality& locality() const override { 113 0 : return logical_host_->locality(); 114 0 : } 115 0 : Stats::StatName localityZoneStatName() const override { 116 0 : return logical_host_->localityZoneStatName(); 117 0 : } 118 0 : Network::Address::InstanceConstSharedPtr healthCheckAddress() const override { 119 : // Should never be called since real hosts are used only for forwarding. 120 0 : return nullptr; 121 0 : } 122 0 : absl::optional<MonotonicTime> lastHcPassTime() const override { 123 0 : return logical_host_->lastHcPassTime(); 124 0 : } 125 0 : uint32_t priority() const override { return logical_host_->priority(); } 126 0 : void priority(uint32_t) override {} 127 : 128 : private: 129 : const Network::Address::InstanceConstSharedPtr address_; 130 : const HostConstSharedPtr logical_host_; 131 : }; 132 : 133 : } // namespace Upstream 134 : } // namespace Envoy