-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
eds.h
125 lines (107 loc) · 5.43 KB
/
eds.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#pragma once
#include <memory>
#include "envoy/config/cluster/v3/cluster.pb.h"
#include "envoy/config/core/v3/base.pb.h"
#include "envoy/config/core/v3/config_source.pb.h"
#include "envoy/config/endpoint/v3/endpoint.pb.h"
#include "envoy/config/endpoint/v3/endpoint.pb.validate.h"
#include "envoy/config/subscription.h"
#include "envoy/config/subscription_factory.h"
#include "envoy/local_info/local_info.h"
#include "envoy/secret/secret_manager.h"
#include "envoy/service/discovery/v3/discovery.pb.h"
#include "envoy/stats/scope.h"
#include "envoy/upstream/locality.h"
#include "source/common/config/subscription_base.h"
#include "source/common/upstream/cluster_factory_impl.h"
#include "source/common/upstream/upstream_impl.h"
#include "source/extensions/clusters/eds/leds.h"
namespace Envoy {
namespace Upstream {
/**
* Cluster implementation that reads host information from the Endpoint Discovery Service.
*/
class EdsClusterImpl
: public BaseDynamicClusterImpl,
Envoy::Config::SubscriptionBase<envoy::config::endpoint::v3::ClusterLoadAssignment> {
public:
EdsClusterImpl(Server::Configuration::ServerFactoryContext& server_context,
const envoy::config::cluster::v3::Cluster& cluster,
ClusterFactoryContext& cluster_context, Runtime::Loader& runtime,
bool added_via_api);
// Upstream::Cluster
InitializePhase initializePhase() const override { return initialize_phase_; }
private:
// Config::SubscriptionCallbacks
void onConfigUpdate(const std::vector<Config::DecodedResourceRef>& resources,
const std::string& version_info) override;
void onConfigUpdate(const std::vector<Config::DecodedResourceRef>& added_resources,
const Protobuf::RepeatedPtrField<std::string>& removed_resources,
const std::string& system_version_info) override;
void onConfigUpdateFailed(Envoy::Config::ConfigUpdateFailureReason reason,
const EnvoyException* e) override;
using LocalityWeightsMap = absl::node_hash_map<envoy::config::core::v3::Locality, uint32_t,
LocalityHash, LocalityEqualTo>;
bool updateHostsPerLocality(const uint32_t priority, const uint32_t overprovisioning_factor,
const HostVector& new_hosts, LocalityWeightsMap& locality_weights_map,
LocalityWeightsMap& new_locality_weights_map,
PriorityStateManager& priority_state_manager,
const HostMap& all_hosts,
const absl::flat_hash_set<std::string>& all_new_hosts);
bool validateUpdateSize(int num_resources);
const std::string& edsServiceName() const {
const std::string& name = info_->edsServiceName();
return !name.empty() ? name : info_->name();
}
// ClusterImplBase
void reloadHealthyHostsHelper(const HostSharedPtr& host) override;
void startPreInit() override;
void onAssignmentTimeout();
// Returns true iff all the LEDS based localities were updated.
bool validateAllLedsUpdated() const;
class BatchUpdateHelper : public PrioritySet::BatchUpdateCb {
public:
BatchUpdateHelper(
EdsClusterImpl& parent,
const envoy::config::endpoint::v3::ClusterLoadAssignment& cluster_load_assignment)
: parent_(parent), cluster_load_assignment_(cluster_load_assignment) {}
// Upstream::PrioritySet::BatchUpdateCb
void batchUpdate(PrioritySet::HostUpdateCb& host_update_cb) override;
private:
void updateLocalityEndpoints(
const envoy::config::endpoint::v3::LbEndpoint& lb_endpoint,
const envoy::config::endpoint::v3::LocalityLbEndpoints& locality_lb_endpoint,
PriorityStateManager& priority_state_manager,
absl::flat_hash_set<std::string>& all_new_hosts);
EdsClusterImpl& parent_;
const envoy::config::endpoint::v3::ClusterLoadAssignment& cluster_load_assignment_;
};
Config::SubscriptionPtr subscription_;
const LocalInfo::LocalInfo& local_info_;
std::vector<LocalityWeightsMap> locality_weights_map_;
Event::TimerPtr assignment_timeout_;
InitializePhase initialize_phase_;
using LedsConfigSet = absl::flat_hash_set<envoy::config::endpoint::v3::LedsClusterLocalityConfig,
MessageUtil, MessageUtil>;
using LedsConfigMap = absl::flat_hash_map<envoy::config::endpoint::v3::LedsClusterLocalityConfig,
LedsSubscriptionPtr, MessageUtil, MessageUtil>;
// Maps between a LEDS configuration (ConfigSource + collection name) to the locality endpoints
// data.
LedsConfigMap leds_localities_;
// TODO(adisuissa): Avoid saving the entire cluster load assignment, only the
// relevant parts of the config for each locality. Note that this field must
// be set when LEDS is used.
std::unique_ptr<envoy::config::endpoint::v3::ClusterLoadAssignment> cluster_load_assignment_;
};
using EdsClusterImplSharedPtr = std::shared_ptr<EdsClusterImpl>;
class EdsClusterFactory : public ClusterFactoryImplBase {
public:
EdsClusterFactory() : ClusterFactoryImplBase("envoy.cluster.eds") {}
private:
std::pair<ClusterImplBaseSharedPtr, ThreadAwareLoadBalancerPtr>
createClusterImpl(Server::Configuration::ServerFactoryContext& server_context,
const envoy::config::cluster::v3::Cluster& cluster,
ClusterFactoryContext& context) override;
};
} // namespace Upstream
} // namespace Envoy