Line data Source code
1 : #pragma once 2 : 3 : #include <memory> 4 : 5 : #include "envoy/config/endpoint/v3/endpoint_components.pb.h" 6 : #include "envoy/config/endpoint/v3/endpoint_components.pb.validate.h" 7 : #include "envoy/config/subscription.h" 8 : #include "envoy/local_info/local_info.h" 9 : #include "envoy/stats/scope.h" 10 : #include "envoy/stats/stats_macros.h" 11 : 12 : #include "source/common/config/subscription_base.h" 13 : #include "source/common/upstream/upstream_impl.h" 14 : 15 : namespace Envoy { 16 : namespace Upstream { 17 : 18 : /** 19 : * All per-cluster LEDS stats. @see stats_macros.h 20 : * These will be added to the subscription stats. 21 : */ 22 : #define ALL_LEDS_STATS(COUNTER) COUNTER(update_empty) 23 : 24 : /** 25 : * Struct definition for all per-cluster LEDS stats. @see stats_macros.h 26 : */ 27 : struct LedsStats { 28 : ALL_LEDS_STATS(GENERATE_COUNTER_STRUCT) 29 : }; 30 : 31 : /* 32 : * A single subscription for all LEDS resources of a specific SourceConfig that 33 : * fetches updates from a Locality Endpoint Discovery Service. 34 : * Multiple subscriptions with the same LEDS collection name can use a single 35 : * subscription. 36 : */ 37 : class LedsSubscription 38 : : private Envoy::Config::SubscriptionBase<envoy::config::endpoint::v3::LbEndpoint>, 39 : private Logger::Loggable<Logger::Id::upstream> { 40 : public: 41 : using UpdateCb = std::function<void()>; 42 : using LbEndpointsMap = absl::flat_hash_map<std::string, envoy::config::endpoint::v3::LbEndpoint>; 43 : 44 : LedsSubscription(const envoy::config::endpoint::v3::LedsClusterLocalityConfig& leds_config, 45 : const std::string& cluster_name, 46 : Server::Configuration::TransportSocketFactoryContext& factory_context, 47 : Stats::Scope& stats_scope, const UpdateCb& callback); 48 : 49 : // Returns the map between registered LEDS resource names and their endpoints data. 50 0 : const LbEndpointsMap& getEndpointsMap() const { return endpoints_map_; } 51 : 52 : // Returns true iff the endpoints were updated. 53 0 : bool isUpdated() const { return initial_update_attempt_complete_; } 54 : 55 : private: 56 : // Config::SubscriptionCallbacks 57 : absl::Status onConfigUpdate(const std::vector<Config::DecodedResourceRef>&, 58 0 : const std::string&) override { 59 0 : return absl::OkStatus(); 60 0 : } 61 : absl::Status onConfigUpdate(const std::vector<Config::DecodedResourceRef>& added_resources, 62 : const Protobuf::RepeatedPtrField<std::string>& removed_resources, 63 : const std::string&) override; 64 : void onConfigUpdateFailed(Envoy::Config::ConfigUpdateFailureReason reason, 65 : const EnvoyException* e) override; 66 : 67 : const LocalInfo::LocalInfo& local_info_; 68 : const std::string cluster_name_; 69 : // LEDS stats scope must outlive the subscription. 70 : Stats::ScopeSharedPtr stats_scope_; 71 : LedsStats stats_; 72 : // A map between a LEDS resource name to the LbEndpoint data. 73 : LbEndpointsMap endpoints_map_; 74 : // A callback function activated after an update is received (either successful or 75 : // unsuccessful). 76 : const UpdateCb callback_; 77 : // Once the endpoints of the locality are updated, it is considered active. 78 : bool initial_update_attempt_complete_{false}; 79 : Config::SubscriptionPtr subscription_; 80 : }; 81 : 82 : using LedsSubscriptionPtr = std::unique_ptr<LedsSubscription>; 83 : 84 : } // namespace Upstream 85 : } // namespace Envoy