-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make cluster manager know about local cluster (optional) #149
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,11 +43,21 @@ ClusterManagerImpl::ClusterManagerImpl(const Json::Object& config, Stats::Store& | |
loadCluster(cluster, stats, dns_resolver, ssl_context_manager, runtime, random); | ||
} | ||
|
||
Optional<std::string> local_cluster_name; | ||
if (config.hasObject("local_cluster_name")) { | ||
local_cluster_name.value(config.getString("local_cluster_name")); | ||
if (get(local_cluster_name.value()) == nullptr) { | ||
throw EnvoyException( | ||
fmt::format("local cluster '{}' must be defined", local_cluster_name.value())); | ||
} | ||
} | ||
|
||
tls.set(thread_local_slot_, | ||
[this, &stats, &runtime, &random, local_zone_name, local_address]( | ||
[this, &stats, &runtime, &random, local_zone_name, local_address, local_cluster_name]( | ||
Event::Dispatcher& dispatcher) -> ThreadLocal::ThreadLocalObjectPtr { | ||
return ThreadLocal::ThreadLocalObjectPtr{new ThreadLocalClusterManagerImpl( | ||
*this, dispatcher, runtime, random, local_zone_name, local_address)}; | ||
*this, dispatcher, runtime, random, local_zone_name, local_address, | ||
local_cluster_name)}; | ||
}); | ||
|
||
// To avoid threading issues, for those clusters that start with hosts already in them (like | ||
|
@@ -209,12 +219,29 @@ Http::AsyncClient& ClusterManagerImpl::httpAsyncClientForCluster(const std::stri | |
ClusterManagerImpl::ThreadLocalClusterManagerImpl::ThreadLocalClusterManagerImpl( | ||
ClusterManagerImpl& parent, Event::Dispatcher& dispatcher, Runtime::Loader& runtime, | ||
Runtime::RandomGenerator& random, const std::string& local_zone_name, | ||
const std::string& local_address) | ||
const std::string& local_address, const Optional<std::string>& local_cluster_name) | ||
: parent_(parent), dispatcher_(dispatcher) { | ||
// If local cluster is defined then we need to initialize it first. | ||
if (local_cluster_name.valid()) { | ||
auto& local_cluster = parent.primary_clusters_[local_cluster_name.value()]; | ||
thread_local_clusters_[local_cluster_name.value()].reset( | ||
new ClusterEntry(*this, *local_cluster, runtime, random, parent.stats_, dispatcher, | ||
local_zone_name, local_address, nullptr)); | ||
} | ||
|
||
const ClusterEntry* local_cluster = local_cluster_name.valid() | ||
? thread_local_clusters_[local_cluster_name.value()].get() | ||
: nullptr; | ||
|
||
for (auto& cluster : parent.primary_clusters_) { | ||
thread_local_clusters_[cluster.first].reset(new ClusterEntry(*this, *cluster.second, runtime, | ||
random, parent.stats_, dispatcher, | ||
local_zone_name, local_address)); | ||
// If local cluster name is set then we already initialized this cluster. | ||
if (local_cluster_name.valid() && local_cluster_name == cluster.first) { | ||
continue; | ||
} | ||
|
||
thread_local_clusters_[cluster.first].reset( | ||
new ClusterEntry(*this, *cluster.second, runtime, random, parent.stats_, dispatcher, | ||
local_zone_name, local_address, local_cluster)); | ||
} | ||
|
||
for (auto& cluster : thread_local_clusters_) { | ||
|
@@ -281,23 +308,31 @@ void ClusterManagerImpl::ThreadLocalClusterManagerImpl::shutdown() { | |
ClusterManagerImpl::ThreadLocalClusterManagerImpl::ClusterEntry::ClusterEntry( | ||
ThreadLocalClusterManagerImpl& parent, const Cluster& cluster, Runtime::Loader& runtime, | ||
Runtime::RandomGenerator& random, Stats::Store& stats_store, Event::Dispatcher& dispatcher, | ||
const std::string& local_zone_name, const std::string& local_address) | ||
const std::string& local_zone_name, const std::string& local_address, | ||
const ClusterEntry* local_cluster) | ||
: parent_(parent), primary_cluster_(cluster), | ||
http_async_client_( | ||
cluster, stats_store, dispatcher, local_zone_name, parent.parent_, runtime, random, | ||
Router::ShadowWriterPtr{new Router::ShadowWriterImpl(parent.parent_)}, local_address) { | ||
|
||
const HostSet* local_host_set = nullptr; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unless you are going to store const ClusterEntry* somewhere just pass const HostSet* to this function then you avoid doing the same if check again here as you do above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
if (local_cluster) { | ||
local_host_set = &local_cluster->host_set_; | ||
} | ||
|
||
switch (cluster.lbType()) { | ||
case LoadBalancerType::LeastRequest: { | ||
lb_.reset(new LeastRequestLoadBalancer(host_set_, cluster.stats(), runtime, random)); | ||
lb_.reset( | ||
new LeastRequestLoadBalancer(host_set_, local_host_set, cluster.stats(), runtime, random)); | ||
break; | ||
} | ||
case LoadBalancerType::Random: { | ||
lb_.reset(new RandomLoadBalancer(host_set_, cluster.stats(), runtime, random)); | ||
lb_.reset(new RandomLoadBalancer(host_set_, local_host_set, cluster.stats(), runtime, random)); | ||
break; | ||
} | ||
case LoadBalancerType::RoundRobin: { | ||
lb_.reset(new RoundRobinLoadBalancer(host_set_, cluster.stats(), runtime, random)); | ||
lb_.reset( | ||
new RoundRobinLoadBalancer(host_set_, local_host_set, cluster.stats(), runtime, random)); | ||
break; | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: local_cluster_name.value()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed