/proc/self/cwd/source/common/router/rds_impl.h
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | #include <cstdint> |
4 | | #include <functional> |
5 | | #include <memory> |
6 | | #include <queue> |
7 | | #include <string> |
8 | | |
9 | | #include "envoy/admin/v3/config_dump.pb.h" |
10 | | #include "envoy/config/core/v3/config_source.pb.h" |
11 | | #include "envoy/config/route/v3/route.pb.h" |
12 | | #include "envoy/config/route/v3/route.pb.validate.h" |
13 | | #include "envoy/config/subscription.h" |
14 | | #include "envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.pb.h" |
15 | | #include "envoy/http/codes.h" |
16 | | #include "envoy/local_info/local_info.h" |
17 | | #include "envoy/router/rds.h" |
18 | | #include "envoy/router/route_config_provider_manager.h" |
19 | | #include "envoy/router/route_config_update_receiver.h" |
20 | | #include "envoy/server/admin.h" |
21 | | #include "envoy/server/filter_config.h" |
22 | | #include "envoy/service/discovery/v3/discovery.pb.h" |
23 | | #include "envoy/singleton/instance.h" |
24 | | #include "envoy/stats/scope.h" |
25 | | #include "envoy/thread_local/thread_local.h" |
26 | | |
27 | | #include "source/common/common/callback_impl.h" |
28 | | #include "source/common/common/cleanup.h" |
29 | | #include "source/common/common/logger.h" |
30 | | #include "source/common/init/manager_impl.h" |
31 | | #include "source/common/init/target_impl.h" |
32 | | #include "source/common/init/watcher_impl.h" |
33 | | #include "source/common/protobuf/utility.h" |
34 | | #include "source/common/rds/common/proto_traits_impl.h" |
35 | | #include "source/common/rds/rds_route_config_provider_impl.h" |
36 | | #include "source/common/rds/rds_route_config_subscription.h" |
37 | | #include "source/common/rds/route_config_provider_manager.h" |
38 | | #include "source/common/rds/route_config_update_receiver_impl.h" |
39 | | #include "source/common/rds/static_route_config_provider_impl.h" |
40 | | #include "source/common/router/route_provider_manager.h" |
41 | | #include "source/common/router/vhds.h" |
42 | | |
43 | | #include "absl/container/node_hash_map.h" |
44 | | #include "absl/container/node_hash_set.h" |
45 | | |
46 | | namespace Envoy { |
47 | | namespace Router { |
48 | | |
49 | | // For friend class declaration in RdsRouteConfigSubscription. |
50 | | class ScopedRdsConfigSubscription; |
51 | | |
52 | | /** |
53 | | * A class that fetches the route configuration dynamically using the RDS API and updates them to |
54 | | * RDS config providers. |
55 | | */ |
56 | | |
57 | | class RdsRouteConfigSubscription : public Rds::RdsRouteConfigSubscription { |
58 | | public: |
59 | | RdsRouteConfigSubscription( |
60 | | RouteConfigUpdatePtr&& config_update, |
61 | | Envoy::Config::OpaqueResourceDecoderSharedPtr&& resource_decoder, |
62 | | const envoy::extensions::filters::network::http_connection_manager::v3::Rds& rds, |
63 | | const uint64_t manager_identifier, |
64 | | Server::Configuration::ServerFactoryContext& factory_context, const std::string& stat_prefix, |
65 | | Rds::RouteConfigProviderManager& route_config_provider_manager); |
66 | | ~RdsRouteConfigSubscription() override; |
67 | | |
68 | 106 | RouteConfigUpdatePtr& routeConfigUpdate() { return config_update_info_; } |
69 | | void updateOnDemand(const std::string& aliases); |
70 | | void maybeCreateInitManager(const std::string& version_info, |
71 | | std::unique_ptr<Init::ManagerImpl>& init_manager, |
72 | | std::unique_ptr<Cleanup>& resume_rds); |
73 | | |
74 | | private: |
75 | | absl::Status beforeProviderUpdate(std::unique_ptr<Init::ManagerImpl>& noop_init_manager, |
76 | | std::unique_ptr<Cleanup>& resume_rds) override; |
77 | | void afterProviderUpdate() override; |
78 | | |
79 | | ABSL_MUST_USE_RESULT Common::CallbackHandlePtr |
80 | 0 | addUpdateCallback(std::function<absl::Status()> callback) { |
81 | 0 | return update_callback_manager_.add(callback); |
82 | 0 | } |
83 | | |
84 | | VhdsSubscriptionPtr vhds_subscription_; |
85 | | RouteConfigUpdatePtr config_update_info_; |
86 | | Common::CallbackManager<> update_callback_manager_; |
87 | | |
88 | | // Access to addUpdateCallback |
89 | | friend class ScopedRdsConfigSubscription; |
90 | | }; |
91 | | |
92 | | using RdsRouteConfigSubscriptionSharedPtr = std::shared_ptr<RdsRouteConfigSubscription>; |
93 | | |
94 | | struct UpdateOnDemandCallback { |
95 | | const std::string alias_; |
96 | | Event::Dispatcher& thread_local_dispatcher_; |
97 | | std::weak_ptr<Http::RouteConfigUpdatedCallback> cb_; |
98 | | }; |
99 | | |
100 | | /** |
101 | | * Implementation of RouteConfigProvider that fetches the route configuration dynamically using |
102 | | * the subscription. |
103 | | */ |
104 | | class RdsRouteConfigProviderImpl : public RouteConfigProvider, |
105 | | Logger::Loggable<Logger::Id::router> { |
106 | | public: |
107 | | RdsRouteConfigProviderImpl(RdsRouteConfigSubscriptionSharedPtr&& subscription, |
108 | | Server::Configuration::ServerFactoryContext& factory_context); |
109 | | |
110 | | RdsRouteConfigSubscription& subscription(); |
111 | | |
112 | | // Router::RouteConfigProvider |
113 | 2 | Rds::ConfigConstSharedPtr config() const override { return base_.config(); } |
114 | 84 | const absl::optional<ConfigInfo>& configInfo() const override { return base_.configInfo(); } |
115 | 18 | SystemTime lastUpdated() const override { return base_.lastUpdated(); } |
116 | | |
117 | | absl::Status onConfigUpdate() override; |
118 | | ConfigConstSharedPtr configCast() const override; |
119 | | void requestVirtualHostsUpdate( |
120 | | const std::string& for_domain, Event::Dispatcher& thread_local_dispatcher, |
121 | | std::weak_ptr<Http::RouteConfigUpdatedCallback> route_config_updated_cb) override; |
122 | | |
123 | | private: |
124 | | Rds::RdsRouteConfigProviderImpl base_; |
125 | | |
126 | | RouteConfigUpdatePtr& config_update_info_; |
127 | | Server::Configuration::ServerFactoryContext& factory_context_; |
128 | | std::list<UpdateOnDemandCallback> config_update_callbacks_; |
129 | | // A flag used to determine if this instance of RdsRouteConfigProviderImpl hasn't been |
130 | | // deallocated. Please also see a comment in requestVirtualHostsUpdate() method implementation. |
131 | | std::shared_ptr<bool> still_alive_{std::make_shared<bool>(true)}; |
132 | | }; |
133 | | |
134 | | using RdsRouteConfigProviderImplSharedPtr = std::shared_ptr<RdsRouteConfigProviderImpl>; |
135 | | |
136 | | class RdsFactoryImpl : public RdsFactory { |
137 | | public: |
138 | 72 | std::string name() const override { return "envoy.rds_factory.default"; } |
139 | | virtual RouteConfigProviderSharedPtr createRdsRouteConfigProvider( |
140 | | const envoy::extensions::filters::network::http_connection_manager::v3::Rds& rds, |
141 | | Server::Configuration::ServerFactoryContext& factory_context, const std::string& stat_prefix, |
142 | | Init::Manager& init_manager, ProtoTraitsImpl& proto_traits, |
143 | | Rds::RouteConfigProviderManager& manager) override; |
144 | | }; |
145 | | |
146 | | DECLARE_FACTORY(RdsFactoryImpl); |
147 | | |
148 | | } // namespace Router |
149 | | } // namespace Envoy |