-
Notifications
You must be signed in to change notification settings - Fork 84
network: support draining connections after triggered DNS refresh #2225
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6c6bccc
internal setup for connection draining
goaway 32f8799
green
goaway 7623d39
configuration wiring and tests + fixes
goaway fb52423
format
goaway e42b881
spelling
goaway b897c04
Merge branch 'main' into ms/drain-cxns
goaway 2af1ba2
fix and add tests
goaway afec7b7
format
goaway fff6d27
add log event to drain
goaway 0bfb557
rename drainConnections to resetConnectivityState
goaway d75e77b
add additional clarifying comments
goaway 383f2d5
fixes and comments
goaway d034970
format and todos
goaway File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,6 +82,15 @@ Configurator::NetworkState Configurator::network_state_{1, ENVOY_NET_GENERIC, Ma | |
|
|
||
| envoy_netconf_t Configurator::setPreferredNetwork(envoy_network_t network) { | ||
| Thread::LockGuard lock{network_state_.mutex_}; | ||
|
|
||
| // TODO(goaway): Re-enable this guard. There's some concern that this will miss network updates | ||
| // moving from offline to online states. We should address this then re-enable this guard to | ||
| // avoid unnecessary cache refresh and connection drain. | ||
| // if (network_state_.network_ == network) { | ||
| // // Provide a non-current key preventing further scheduled effects (e.g. DNS refresh). | ||
| // return network_state_.configuration_key_ - 1; | ||
| //} | ||
|
|
||
| ENVOY_LOG_EVENT(debug, "netconf_network_change", std::to_string(network)); | ||
|
|
||
| network_state_.configuration_key_++; | ||
|
|
@@ -167,13 +176,43 @@ void Configurator::reportNetworkUsage(envoy_netconf_t configuration_key, bool ne | |
|
|
||
| // If configuration state changed, refresh dns. | ||
| if (configuration_updated) { | ||
| refreshDns(configuration_key); | ||
| refreshDns(configuration_key, false); | ||
| } | ||
| } | ||
|
|
||
| void Configurator::onDnsResolutionComplete( | ||
| const std::string& host, const Extensions::Common::DynamicForwardProxy::DnsHostInfoSharedPtr&, | ||
| Network::DnsResolver::ResolutionStatus) { | ||
| if (enable_drain_post_dns_refresh_ && pending_drain_) { | ||
| pending_drain_ = false; | ||
|
|
||
| // We ignore whether DNS resolution has succeeded here. If it failed, we may be offline and | ||
| // should probably drain connections. If it succeeds, we may have new DNS entries and so we | ||
| // drain connections. It may be possible to refine this logic in the future. | ||
| // TODO(goaway): check the set of cached hosts from the last triggered DNS refresh for this | ||
| // host, and if present, remove it and trigger connection drain for this host specifically. | ||
| ENVOY_LOG_EVENT(debug, "netconf_post_dns_drain_cx", host); | ||
| cluster_manager_.drainConnections(nullptr); | ||
| } | ||
| } | ||
|
|
||
| void Configurator::setDrainPostDnsRefreshEnabled(bool enabled) { | ||
| enable_drain_post_dns_refresh_ = enabled; | ||
| if (!enabled) { | ||
| pending_drain_ = false; | ||
| } else if (!dns_callbacks_handle_) { | ||
| // Register callbacks once, on demand, using the handle as a sentinel. There may not be | ||
| // a DNS cache during initialization, but if one is available, it should always exist by the | ||
| // time this function is called from the NetworkConfigurationFilter. | ||
| if (auto dns_cache = dnsCache()) { | ||
| dns_callbacks_handle_ = dns_cache->addUpdateCallbacks(*this); | ||
|
Member
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. For simplicity I think it would be simpler to just always have the callbacks registered but up to you.
Contributor
Author
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. Added clarifying comment to the code here. |
||
| } | ||
| } | ||
| } | ||
|
|
||
| void Configurator::setInterfaceBindingEnabled(bool enabled) { enable_interface_binding_ = enabled; } | ||
|
|
||
| void Configurator::refreshDns(envoy_netconf_t configuration_key) { | ||
| void Configurator::refreshDns(envoy_netconf_t configuration_key, bool drain_connections) { | ||
| { | ||
| Thread::LockGuard lock{network_state_.mutex_}; | ||
|
|
||
|
|
@@ -188,12 +227,33 @@ void Configurator::refreshDns(envoy_netconf_t configuration_key) { | |
| } | ||
| } | ||
|
|
||
| if (auto dns_cache = dns_cache_manager_->lookUpCacheByName(BaseDnsCache)) { | ||
| if (auto dns_cache = dnsCache()) { | ||
| ENVOY_LOG_EVENT(debug, "netconf_refresh_dns", std::to_string(configuration_key)); | ||
| pending_drain_ = drain_connections && enable_drain_post_dns_refresh_; | ||
| // TODO(goaway): capture the list of currently cached hosts here in a set, to be checked | ||
| // for draining when DNS resolutions occur. | ||
| dns_cache->forceRefreshHosts(); | ||
| } else { | ||
| } | ||
| } | ||
|
|
||
| Extensions::Common::DynamicForwardProxy::DnsCacheSharedPtr Configurator::dnsCache() { | ||
| auto cache = dns_cache_manager_->lookUpCacheByName(BaseDnsCache); | ||
| if (!cache) { | ||
| ENVOY_LOG_EVENT(warn, "netconf_dns_cache_missing", BaseDnsCache); | ||
| } | ||
| return cache; | ||
| } | ||
|
|
||
| void Configurator::resetConnectivityState() { | ||
| envoy_netconf_t configuration_key; | ||
| { | ||
| Thread::LockGuard lock{network_state_.mutex_}; | ||
| network_state_.remaining_faults_ = 1; | ||
| network_state_.socket_mode_ = DefaultPreferredNetworkMode; | ||
| configuration_key = ++network_state_.configuration_key_; | ||
| } | ||
|
|
||
| refreshDns(configuration_key, true); | ||
| } | ||
|
|
||
| std::vector<InterfacePair> Configurator::enumerateV4Interfaces() { | ||
|
|
@@ -347,7 +407,8 @@ ConfiguratorSharedPtr ConfiguratorFactory::get() { | |
| SINGLETON_MANAGER_REGISTERED_NAME(network_configurator), [&] { | ||
| Extensions::Common::DynamicForwardProxy::DnsCacheManagerFactoryImpl cache_manager_factory{ | ||
| context_}; | ||
| return std::make_shared<Configurator>(cache_manager_factory.get()); | ||
| return std::make_shared<Configurator>(context_.clusterManager(), | ||
| cache_manager_factory.get()); | ||
| }); | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is
onDnsResolutionCompleteguaranteed to always be invoked on the same thread assetDrainPostDnsRefreshEnabled? If not, should we guard this with a mutex?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.
Generally speaking, all C++ code in Envoy Mobile should be assumed to run on a single thread with only a very few exceptions. As it happens, I added a clarifying comment on the subject for this class elsewhere in the PR:
https://github.com/envoyproxy/envoy-mobile/pull/2225/files#diff-41a6b7732eaa28a4c8cd16a941d7d7cb70b91428a7a47ff352a1bd9b3c9e5446R66