/proc/self/cwd/source/extensions/clusters/eds/eds.cc
Line | Count | Source (jump to first uncovered line) |
1 | | #include "source/extensions/clusters/eds/eds.h" |
2 | | |
3 | | #include "envoy/common/exception.h" |
4 | | #include "envoy/config/cluster/v3/cluster.pb.h" |
5 | | #include "envoy/config/core/v3/config_source.pb.h" |
6 | | #include "envoy/service/discovery/v3/discovery.pb.h" |
7 | | |
8 | | #include "source/common/common/assert.h" |
9 | | #include "source/common/common/utility.h" |
10 | | #include "source/common/config/api_version.h" |
11 | | #include "source/common/config/decoded_resource_impl.h" |
12 | | |
13 | | namespace Envoy { |
14 | | namespace Upstream { |
15 | | |
16 | | EdsClusterImpl::EdsClusterImpl(const envoy::config::cluster::v3::Cluster& cluster, |
17 | | ClusterFactoryContext& cluster_context) |
18 | | : BaseDynamicClusterImpl(cluster, cluster_context), |
19 | | Envoy::Config::SubscriptionBase<envoy::config::endpoint::v3::ClusterLoadAssignment>( |
20 | | cluster_context.messageValidationVisitor(), "cluster_name"), |
21 | | local_info_(cluster_context.serverFactoryContext().localInfo()), |
22 | | eds_resources_cache_( |
23 | | Runtime::runtimeFeatureEnabled("envoy.restart_features.use_eds_cache_for_ads") |
24 | | ? cluster_context.clusterManager().edsResourcesCache() |
25 | 14 | : absl::nullopt) { |
26 | 14 | Event::Dispatcher& dispatcher = cluster_context.serverFactoryContext().mainThreadDispatcher(); |
27 | 14 | assignment_timeout_ = dispatcher.createTimer([this]() -> void { onAssignmentTimeout(); }); |
28 | 14 | const auto& eds_config = cluster.eds_cluster_config().eds_config(); |
29 | 14 | if (Config::SubscriptionFactory::isPathBasedConfigSource( |
30 | 14 | eds_config.config_source_specifier_case())) { |
31 | 0 | initialize_phase_ = InitializePhase::Primary; |
32 | 14 | } else { |
33 | 14 | initialize_phase_ = InitializePhase::Secondary; |
34 | 14 | } |
35 | 14 | const auto resource_name = getResourceName(); |
36 | 14 | subscription_ = |
37 | 14 | cluster_context.clusterManager().subscriptionFactory().subscriptionFromConfigSource( |
38 | 14 | eds_config, Grpc::Common::typeUrl(resource_name), info_->statsScope(), *this, |
39 | 14 | resource_decoder_, {}); |
40 | 14 | } |
41 | | |
42 | 14 | EdsClusterImpl::~EdsClusterImpl() { |
43 | 14 | if (using_cached_resource_) { |
44 | | // Clear the callback as the subscription is no longer valid. |
45 | 0 | eds_resources_cache_->removeCallback(edsServiceName(), this); |
46 | 0 | } |
47 | 14 | } |
48 | | |
49 | 14 | void EdsClusterImpl::startPreInit() { subscription_->start({edsServiceName()}); } |
50 | | |
51 | 14 | void EdsClusterImpl::BatchUpdateHelper::batchUpdate(PrioritySet::HostUpdateCb& host_update_cb) { |
52 | 14 | absl::flat_hash_set<std::string> all_new_hosts; |
53 | 14 | PriorityStateManager priority_state_manager(parent_, parent_.local_info_, &host_update_cb); |
54 | 14 | for (const auto& locality_lb_endpoint : cluster_load_assignment_.endpoints()) { |
55 | 14 | parent_.validateEndpointsForZoneAwareRouting(locality_lb_endpoint); |
56 | | |
57 | 14 | priority_state_manager.initializePriorityFor(locality_lb_endpoint); |
58 | | |
59 | 14 | if (locality_lb_endpoint.has_leds_cluster_locality_config()) { |
60 | | // The locality uses LEDS, fetch its dynamic data, which must be ready, or otherwise |
61 | | // the batchUpdate method should not have been called. |
62 | 0 | const auto& leds_config = locality_lb_endpoint.leds_cluster_locality_config(); |
63 | | |
64 | | // The batchUpdate call must be performed after all the endpoints of all localities |
65 | | // were received. |
66 | 0 | ASSERT(parent_.leds_localities_.find(leds_config) != parent_.leds_localities_.end() && |
67 | 0 | parent_.leds_localities_[leds_config]->isUpdated()); |
68 | 0 | for (const auto& [_, lb_endpoint] : |
69 | 0 | parent_.leds_localities_[leds_config]->getEndpointsMap()) { |
70 | 0 | updateLocalityEndpoints(lb_endpoint, locality_lb_endpoint, priority_state_manager, |
71 | 0 | all_new_hosts); |
72 | 0 | } |
73 | 14 | } else { |
74 | 14 | for (const auto& lb_endpoint : locality_lb_endpoint.lb_endpoints()) { |
75 | 14 | updateLocalityEndpoints(lb_endpoint, locality_lb_endpoint, priority_state_manager, |
76 | 14 | all_new_hosts); |
77 | 14 | } |
78 | 14 | } |
79 | 14 | } |
80 | | |
81 | | // Track whether we rebuilt any LB structures. |
82 | 14 | bool cluster_rebuilt = false; |
83 | | |
84 | | // Get the map of all the latest existing hosts, which is used to filter out the existing |
85 | | // hosts in the process of updating cluster memberships. |
86 | 14 | HostMapConstSharedPtr all_hosts = parent_.prioritySet().crossPriorityHostMap(); |
87 | 14 | ASSERT(all_hosts != nullptr); |
88 | | |
89 | 14 | const uint32_t overprovisioning_factor = PROTOBUF_GET_WRAPPED_OR_DEFAULT( |
90 | 14 | cluster_load_assignment_.policy(), overprovisioning_factor, kDefaultOverProvisioningFactor); |
91 | 14 | const bool weighted_priority_health = |
92 | 14 | cluster_load_assignment_.policy().weighted_priority_health(); |
93 | | |
94 | 14 | LocalityWeightsMap empty_locality_map; |
95 | | |
96 | | // Loop over all priorities that exist in the new configuration. |
97 | 14 | auto& priority_state = priority_state_manager.priorityState(); |
98 | 28 | for (size_t i = 0; i < priority_state.size(); ++i) { |
99 | 14 | if (parent_.locality_weights_map_.size() <= i) { |
100 | 14 | parent_.locality_weights_map_.resize(i + 1); |
101 | 14 | } |
102 | 14 | if (priority_state[i].first != nullptr) { |
103 | 14 | cluster_rebuilt |= parent_.updateHostsPerLocality( |
104 | 14 | i, weighted_priority_health, overprovisioning_factor, *priority_state[i].first, |
105 | 14 | parent_.locality_weights_map_[i], priority_state[i].second, priority_state_manager, |
106 | 14 | *all_hosts, all_new_hosts); |
107 | 14 | } else { |
108 | | // If the new update contains a priority with no hosts, call the update function with an empty |
109 | | // set of hosts. |
110 | 0 | cluster_rebuilt |= |
111 | 0 | parent_.updateHostsPerLocality(i, weighted_priority_health, overprovisioning_factor, {}, |
112 | 0 | parent_.locality_weights_map_[i], empty_locality_map, |
113 | 0 | priority_state_manager, *all_hosts, all_new_hosts); |
114 | 0 | } |
115 | 14 | } |
116 | | |
117 | | // Loop over all priorities not present in the config that already exists. This will |
118 | | // empty out any remaining priority that the config update did not refer to. |
119 | 14 | for (size_t i = priority_state.size(); i < parent_.priority_set_.hostSetsPerPriority().size(); |
120 | 14 | ++i) { |
121 | 0 | if (parent_.locality_weights_map_.size() <= i) { |
122 | 0 | parent_.locality_weights_map_.resize(i + 1); |
123 | 0 | } |
124 | 0 | cluster_rebuilt |= parent_.updateHostsPerLocality( |
125 | 0 | i, weighted_priority_health, overprovisioning_factor, {}, parent_.locality_weights_map_[i], |
126 | 0 | empty_locality_map, priority_state_manager, *all_hosts, all_new_hosts); |
127 | 0 | } |
128 | | |
129 | 14 | if (!cluster_rebuilt) { |
130 | 0 | parent_.info_->configUpdateStats().update_no_rebuild_.inc(); |
131 | 0 | } |
132 | | |
133 | | // If we didn't setup to initialize when our first round of health checking is complete, just |
134 | | // do it now. |
135 | 14 | parent_.onPreInitComplete(); |
136 | 14 | } |
137 | | |
138 | | void EdsClusterImpl::BatchUpdateHelper::updateLocalityEndpoints( |
139 | | const envoy::config::endpoint::v3::LbEndpoint& lb_endpoint, |
140 | | const envoy::config::endpoint::v3::LocalityLbEndpoints& locality_lb_endpoint, |
141 | 14 | PriorityStateManager& priority_state_manager, absl::flat_hash_set<std::string>& all_new_hosts) { |
142 | 14 | const auto address = parent_.resolveProtoAddress(lb_endpoint.endpoint().address()); |
143 | 14 | std::vector<Network::Address::InstanceConstSharedPtr> address_list; |
144 | 14 | if (!lb_endpoint.endpoint().additional_addresses().empty()) { |
145 | 0 | address_list.push_back(address); |
146 | 0 | for (const auto& additional_address : lb_endpoint.endpoint().additional_addresses()) { |
147 | 0 | address_list.emplace_back(parent_.resolveProtoAddress(additional_address.address())); |
148 | 0 | } |
149 | 0 | } |
150 | | |
151 | | // When the configuration contains duplicate hosts, only the first one will be retained. |
152 | 14 | const auto address_as_string = address->asString(); |
153 | 14 | if (all_new_hosts.contains(address_as_string)) { |
154 | 0 | return; |
155 | 0 | } |
156 | | |
157 | 14 | priority_state_manager.registerHostForPriority(lb_endpoint.endpoint().hostname(), address, |
158 | 14 | address_list, locality_lb_endpoint, lb_endpoint, |
159 | 14 | parent_.time_source_); |
160 | 14 | all_new_hosts.emplace(address_as_string); |
161 | 14 | } |
162 | | |
163 | | absl::Status |
164 | | EdsClusterImpl::onConfigUpdate(const std::vector<Config::DecodedResourceRef>& resources, |
165 | 14 | const std::string&) { |
166 | 14 | if (resources.empty()) { |
167 | 0 | ENVOY_LOG(debug, "Missing ClusterLoadAssignment for {} in onConfigUpdate()", edsServiceName()); |
168 | 0 | info_->configUpdateStats().update_empty_.inc(); |
169 | 0 | onPreInitComplete(); |
170 | 0 | return absl::OkStatus(); |
171 | 0 | } |
172 | 14 | if (resources.size() != 1) { |
173 | 0 | return absl::InvalidArgumentError( |
174 | 0 | fmt::format("Unexpected EDS resource length: {}", resources.size())); |
175 | 0 | } |
176 | | |
177 | 14 | envoy::config::endpoint::v3::ClusterLoadAssignment cluster_load_assignment = |
178 | 14 | dynamic_cast<const envoy::config::endpoint::v3::ClusterLoadAssignment&>( |
179 | 14 | resources[0].get().resource()); |
180 | 14 | if (cluster_load_assignment.cluster_name() != edsServiceName()) { |
181 | 0 | return absl::InvalidArgumentError(fmt::format("Unexpected EDS cluster (expecting {}): {}", |
182 | 0 | edsServiceName(), |
183 | 0 | cluster_load_assignment.cluster_name())); |
184 | 0 | } |
185 | | // Validate that each locality doesn't have both LEDS and endpoints defined. |
186 | | // TODO(adisuissa): This is only needed for the API v3 support. In future major versions |
187 | | // the oneof definition will take care of it. |
188 | 14 | for (const auto& locality : cluster_load_assignment.endpoints()) { |
189 | 14 | if (locality.has_leds_cluster_locality_config() && locality.lb_endpoints_size() > 0) { |
190 | 0 | return absl::InvalidArgumentError(fmt::format( |
191 | 0 | "A ClusterLoadAssignment for cluster {} cannot include both LEDS (resource: {}) and a " |
192 | 0 | "list of endpoints.", |
193 | 0 | edsServiceName(), locality.leds_cluster_locality_config().leds_collection_name())); |
194 | 0 | } |
195 | 14 | } |
196 | | |
197 | | // Disable timer (if enabled) as we have received new assignment. |
198 | 14 | if (assignment_timeout_->enabled()) { |
199 | 0 | assignment_timeout_->disableTimer(); |
200 | 0 | if (eds_resources_cache_.has_value()) { |
201 | 0 | eds_resources_cache_->disableExpiryTimer(edsServiceName()); |
202 | 0 | } |
203 | 0 | } |
204 | | // Check if endpoint_stale_after is set. |
205 | 14 | const uint64_t stale_after_ms = |
206 | 14 | PROTOBUF_GET_MS_OR_DEFAULT(cluster_load_assignment.policy(), endpoint_stale_after, 0); |
207 | 14 | if (stale_after_ms > 0) { |
208 | | // Stat to track how often we receive valid assignment_timeout in response. |
209 | 0 | info_->configUpdateStats().assignment_timeout_received_.inc(); |
210 | 0 | assignment_timeout_->enableTimer(std::chrono::milliseconds(stale_after_ms)); |
211 | 0 | if (eds_resources_cache_.has_value()) { |
212 | 0 | eds_resources_cache_->setExpiryTimer(edsServiceName(), |
213 | 0 | std::chrono::milliseconds(stale_after_ms)); |
214 | 0 | } |
215 | 0 | } |
216 | | |
217 | | // Pause LEDS messages until the EDS config is finished processing. |
218 | 14 | Config::ScopedResume maybe_resume_leds; |
219 | 14 | if (transport_factory_context_->clusterManager().adsMux()) { |
220 | 14 | const auto type_url = Config::getTypeUrl<envoy::config::endpoint::v3::LbEndpoint>(); |
221 | 14 | maybe_resume_leds = transport_factory_context_->clusterManager().adsMux()->pause(type_url); |
222 | 14 | } |
223 | | |
224 | 14 | update(cluster_load_assignment); |
225 | | // If previously used a cached version, remove the subscription from the cache's |
226 | | // callbacks. |
227 | 14 | if (using_cached_resource_) { |
228 | 0 | eds_resources_cache_->removeCallback(edsServiceName(), this); |
229 | 0 | using_cached_resource_ = false; |
230 | 0 | } |
231 | 14 | return absl::OkStatus(); |
232 | 14 | } |
233 | | |
234 | | void EdsClusterImpl::update( |
235 | 14 | const envoy::config::endpoint::v3::ClusterLoadAssignment& cluster_load_assignment) { |
236 | | // Compare the current set of LEDS localities (localities using LEDS) to the one received in the |
237 | | // update. A LEDS locality can either be added, removed, or kept. If it is added we add a |
238 | | // subscription to it, and if it is removed we delete the subscription. |
239 | 14 | LedsConfigSet cla_leds_configs; |
240 | | |
241 | 14 | for (const auto& locality : cluster_load_assignment.endpoints()) { |
242 | 14 | if (locality.has_leds_cluster_locality_config()) { |
243 | 0 | cla_leds_configs.emplace(locality.leds_cluster_locality_config()); |
244 | 0 | } |
245 | 14 | } |
246 | | |
247 | | // Remove the LEDS localities that are not needed anymore. |
248 | 14 | absl::erase_if(leds_localities_, [&cla_leds_configs](const auto& item) { |
249 | 0 | auto const& [leds_config, _] = item; |
250 | | // Returns true if the leds_config isn't in the cla_leds_configs |
251 | 0 | return cla_leds_configs.find(leds_config) == cla_leds_configs.end(); |
252 | 0 | }); |
253 | | |
254 | | // In case LEDS is used, store the cluster load assignment as a field |
255 | | // (optimize for no-copy). |
256 | 14 | const envoy::config::endpoint::v3::ClusterLoadAssignment* used_load_assignment; |
257 | 14 | if (!cla_leds_configs.empty() || eds_resources_cache_.has_value()) { |
258 | 0 | cluster_load_assignment_ = std::make_unique<envoy::config::endpoint::v3::ClusterLoadAssignment>( |
259 | 0 | std::move(cluster_load_assignment)); |
260 | 0 | used_load_assignment = cluster_load_assignment_.get(); |
261 | 14 | } else { |
262 | 14 | cluster_load_assignment_ = nullptr; |
263 | 14 | used_load_assignment = &cluster_load_assignment; |
264 | 14 | } |
265 | | |
266 | | // Add all the LEDS localities that are new. |
267 | 14 | for (const auto& leds_config : cla_leds_configs) { |
268 | 0 | if (leds_localities_.find(leds_config) == leds_localities_.end()) { |
269 | 0 | ENVOY_LOG(trace, "Found new LEDS config in EDS onConfigUpdate() for cluster {}: {}", |
270 | 0 | edsServiceName(), leds_config.DebugString()); |
271 | | |
272 | | // Create a new LEDS subscription and add it to the subscriptions map. |
273 | 0 | LedsSubscriptionPtr leds_locality_subscription = std::make_unique<LedsSubscription>( |
274 | 0 | leds_config, edsServiceName(), *transport_factory_context_, info_->statsScope(), |
275 | 0 | [&, used_load_assignment]() { |
276 | | // Called upon an update to the locality. |
277 | 0 | if (validateAllLedsUpdated()) { |
278 | 0 | BatchUpdateHelper helper(*this, *used_load_assignment); |
279 | 0 | priority_set_.batchHostUpdate(helper); |
280 | 0 | } |
281 | 0 | }); |
282 | 0 | leds_localities_.emplace(leds_config, std::move(leds_locality_subscription)); |
283 | 0 | } |
284 | 0 | } |
285 | | |
286 | | // If all the LEDS localities are updated, the EDS update can occur. If not, then when the last |
287 | | // LEDS locality will be updated, it will trigger the EDS update helper. |
288 | 14 | if (!validateAllLedsUpdated()) { |
289 | 0 | return; |
290 | 0 | } |
291 | | |
292 | 14 | BatchUpdateHelper helper(*this, *used_load_assignment); |
293 | 14 | priority_set_.batchHostUpdate(helper); |
294 | 14 | } |
295 | | |
296 | | absl::Status |
297 | | EdsClusterImpl::onConfigUpdate(const std::vector<Config::DecodedResourceRef>& added_resources, |
298 | 4 | const Protobuf::RepeatedPtrField<std::string>&, const std::string&) { |
299 | 4 | return onConfigUpdate(added_resources, ""); |
300 | 4 | } |
301 | | |
302 | 0 | void EdsClusterImpl::onAssignmentTimeout() { |
303 | | // We can no longer use the assignments, remove them. |
304 | | // TODO(vishalpowar) This is not going to work for incremental updates, and we |
305 | | // need to instead change the health status to indicate the assignments are |
306 | | // stale. |
307 | | // TODO(snowp): This should probably just use xDS TTLs? |
308 | 0 | envoy::config::endpoint::v3::ClusterLoadAssignment resource; |
309 | 0 | resource.set_cluster_name(edsServiceName()); |
310 | 0 | update(resource); |
311 | |
|
312 | 0 | if (eds_resources_cache_.has_value()) { |
313 | | // Clear the resource so it won't be used, and its watchers will be notified. |
314 | 0 | eds_resources_cache_->removeResource(edsServiceName()); |
315 | 0 | } |
316 | | // Stat to track how often we end up with stale assignments. |
317 | 0 | info_->configUpdateStats().assignment_stale_.inc(); |
318 | 0 | } |
319 | | |
320 | 0 | void EdsClusterImpl::onCachedResourceRemoved(absl::string_view resource_name) { |
321 | 0 | ASSERT(resource_name == edsServiceName()); |
322 | | // Disable the timer if previously started. |
323 | 0 | if (assignment_timeout_->enabled()) { |
324 | 0 | assignment_timeout_->disableTimer(); |
325 | 0 | eds_resources_cache_->disableExpiryTimer(edsServiceName()); |
326 | 0 | } |
327 | 0 | envoy::config::endpoint::v3::ClusterLoadAssignment resource; |
328 | 0 | resource.set_cluster_name(edsServiceName()); |
329 | 0 | update(resource); |
330 | 0 | } |
331 | | |
332 | 0 | void EdsClusterImpl::reloadHealthyHostsHelper(const HostSharedPtr& host) { |
333 | | // Here we will see if we have a host that has been marked for deletion by service discovery |
334 | | // but has been stabilized due to passing active health checking. If such a host is now |
335 | | // failing active health checking we can remove it during this health check update. |
336 | 0 | HostSharedPtr host_to_exclude = host; |
337 | 0 | if (host_to_exclude != nullptr && |
338 | 0 | host_to_exclude->healthFlagGet(Host::HealthFlag::FAILED_ACTIVE_HC) && |
339 | 0 | host_to_exclude->healthFlagGet(Host::HealthFlag::PENDING_DYNAMIC_REMOVAL)) { |
340 | | // Empty for clarity. |
341 | 0 | } else { |
342 | | // Do not exclude and remove the host during the update. |
343 | 0 | host_to_exclude = nullptr; |
344 | 0 | } |
345 | |
|
346 | 0 | const auto& host_sets = prioritySet().hostSetsPerPriority(); |
347 | 0 | for (size_t priority = 0; priority < host_sets.size(); ++priority) { |
348 | 0 | const auto& host_set = host_sets[priority]; |
349 | | |
350 | | // Filter current hosts in case we need to exclude a host. |
351 | 0 | HostVectorSharedPtr hosts_copy(new HostVector()); |
352 | 0 | std::copy_if(host_set->hosts().begin(), host_set->hosts().end(), |
353 | 0 | std::back_inserter(*hosts_copy), |
354 | 0 | [&host_to_exclude](const HostSharedPtr& host) { return host_to_exclude != host; }); |
355 | | |
356 | | // Setup a hosts to remove vector in case we need to exclude a host. |
357 | 0 | HostVector hosts_to_remove; |
358 | 0 | if (hosts_copy->size() != host_set->hosts().size()) { |
359 | 0 | ASSERT(hosts_copy->size() == host_set->hosts().size() - 1); |
360 | 0 | hosts_to_remove.emplace_back(host_to_exclude); |
361 | 0 | } |
362 | | |
363 | | // Filter hosts per locality in case we need to exclude a host. |
364 | 0 | HostsPerLocalityConstSharedPtr hosts_per_locality_copy = host_set->hostsPerLocality().filter( |
365 | 0 | {[&host_to_exclude](const Host& host) { return &host != host_to_exclude.get(); }})[0]; |
366 | |
|
367 | 0 | prioritySet().updateHosts( |
368 | 0 | priority, HostSetImpl::partitionHosts(hosts_copy, hosts_per_locality_copy), |
369 | 0 | host_set->localityWeights(), {}, hosts_to_remove, absl::nullopt, absl::nullopt); |
370 | 0 | } |
371 | 0 | } |
372 | | |
373 | | bool EdsClusterImpl::updateHostsPerLocality( |
374 | | const uint32_t priority, bool weighted_priority_health, const uint32_t overprovisioning_factor, |
375 | | const HostVector& new_hosts, LocalityWeightsMap& locality_weights_map, |
376 | | LocalityWeightsMap& new_locality_weights_map, PriorityStateManager& priority_state_manager, |
377 | 14 | const HostMap& all_hosts, const absl::flat_hash_set<std::string>& all_new_hosts) { |
378 | 14 | const auto& host_set = priority_set_.getOrCreateHostSet(priority, overprovisioning_factor); |
379 | 14 | HostVectorSharedPtr current_hosts_copy(new HostVector(host_set.hosts())); |
380 | | |
381 | 14 | HostVector hosts_added; |
382 | 14 | HostVector hosts_removed; |
383 | | // We need to trigger updateHosts with the new host vectors if they have changed. We also do this |
384 | | // when the locality weight map or the overprovisioning factor. Note calling updateDynamicHostList |
385 | | // is responsible for both determining whether there was a change and to perform the actual update |
386 | | // to current_hosts_copy, so it must be called even if we know that we need to update (e.g. if the |
387 | | // overprovisioning factor changes). |
388 | | // |
389 | | // TODO(htuch): We eagerly update all the host sets here on weight changes, which may have |
390 | | // performance implications, since this has the knock on effect that we rebuild the load balancers |
391 | | // and locality scheduler. See the comment in BaseDynamicClusterImpl::updateDynamicHostList |
392 | | // about this. In the future we may need to do better here. |
393 | 14 | const bool hosts_updated = updateDynamicHostList(new_hosts, *current_hosts_copy, hosts_added, |
394 | 14 | hosts_removed, all_hosts, all_new_hosts); |
395 | 14 | if (hosts_updated || host_set.weightedPriorityHealth() != weighted_priority_health || |
396 | 14 | host_set.overprovisioningFactor() != overprovisioning_factor || |
397 | 14 | locality_weights_map != new_locality_weights_map) { |
398 | 14 | ASSERT(std::all_of(current_hosts_copy->begin(), current_hosts_copy->end(), |
399 | 14 | [&](const auto& host) { return host->priority() == priority; })); |
400 | 14 | locality_weights_map = new_locality_weights_map; |
401 | 14 | ENVOY_LOG(debug, |
402 | 14 | "EDS hosts or locality weights changed for cluster: {} current hosts {} priority {}", |
403 | 14 | info_->name(), host_set.hosts().size(), host_set.priority()); |
404 | | |
405 | 14 | priority_state_manager.updateClusterPrioritySet( |
406 | 14 | priority, std::move(current_hosts_copy), hosts_added, hosts_removed, absl::nullopt, |
407 | 14 | weighted_priority_health, overprovisioning_factor); |
408 | 14 | return true; |
409 | 14 | } |
410 | 0 | return false; |
411 | 14 | } |
412 | | |
413 | | void EdsClusterImpl::onConfigUpdateFailed(Envoy::Config::ConfigUpdateFailureReason reason, |
414 | 0 | const EnvoyException*) { |
415 | 0 | ASSERT(Envoy::Config::ConfigUpdateFailureReason::ConnectionFailure != reason); |
416 | | // Config failure may happen if Envoy times out waiting for the EDS resource. |
417 | | // If it is a timeout, the eds resources cache is enabled, |
418 | | // and there is a cached ClusterLoadAssignment, then the cached assignment should be used. |
419 | 0 | if (reason == Envoy::Config::ConfigUpdateFailureReason::FetchTimedout && |
420 | 0 | eds_resources_cache_.has_value()) { |
421 | 0 | ENVOY_LOG(trace, "onConfigUpdateFailed due to timeout for {}, looking for cached resources", |
422 | 0 | edsServiceName()); |
423 | 0 | auto cached_resource = eds_resources_cache_->getResource(edsServiceName(), this); |
424 | 0 | if (cached_resource.has_value()) { |
425 | 0 | ENVOY_LOG( |
426 | 0 | debug, |
427 | 0 | "Did not receive EDS response on time, using cached ClusterLoadAssignment for cluster {}", |
428 | 0 | edsServiceName()); |
429 | 0 | envoy::config::endpoint::v3::ClusterLoadAssignment cached_load_assignment = |
430 | 0 | dynamic_cast<const envoy::config::endpoint::v3::ClusterLoadAssignment&>(*cached_resource); |
431 | 0 | info_->configUpdateStats().assignment_use_cached_.inc(); |
432 | 0 | using_cached_resource_ = true; |
433 | 0 | update(cached_load_assignment); |
434 | 0 | return; |
435 | 0 | } |
436 | 0 | } |
437 | | // We need to allow server startup to continue, even if we have a bad config. |
438 | 0 | onPreInitComplete(); |
439 | 0 | } |
440 | | |
441 | | absl::StatusOr<std::pair<ClusterImplBaseSharedPtr, ThreadAwareLoadBalancerPtr>> |
442 | | EdsClusterFactory::createClusterImpl(const envoy::config::cluster::v3::Cluster& cluster, |
443 | 14 | ClusterFactoryContext& context) { |
444 | | // TODO(kbaichoo): EDS cluster should be able to support loading it's |
445 | | // configuration from the CustomClusterType protobuf. Currently it does not. |
446 | | // See: https://github.com/envoyproxy/envoy/issues/28752 |
447 | 14 | if (!cluster.has_eds_cluster_config()) { |
448 | 0 | return absl::InvalidArgumentError("cannot create an EDS cluster without an EDS config"); |
449 | 0 | } |
450 | | |
451 | 14 | return std::make_pair(std::make_unique<EdsClusterImpl>(cluster, context), nullptr); |
452 | 14 | } |
453 | | |
454 | 14 | bool EdsClusterImpl::validateAllLedsUpdated() const { |
455 | | // Iterate through all LEDS based localities, and if they are all updated return true. |
456 | 14 | for (const auto& [_, leds_subscription] : leds_localities_) { |
457 | 0 | if (!leds_subscription->isUpdated()) { |
458 | 0 | return false; |
459 | 0 | } |
460 | 0 | } |
461 | 14 | return true; |
462 | 14 | } |
463 | | |
464 | | /** |
465 | | * Static registration for the Eds cluster factory. @see RegisterFactory. |
466 | | */ |
467 | | REGISTER_FACTORY(EdsClusterFactory, ClusterFactory); |
468 | | |
469 | | } // namespace Upstream |
470 | | } // namespace Envoy |