Skip to content
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

[router] Add SRDS configUpdate impl #7451

Merged
merged 71 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
6a3f13b
add scope key-builder impl and test
stevenzzzz Jun 11, 2019
cc806d7
Merge branch 'master' into add-key-builder
stevenzzzz Jun 11, 2019
bb55a44
fix typo
stevenzzzz Jun 11, 2019
8d7b4da
move impl code from header to cc file.
stevenzzzz Jun 12, 2019
8bcddce
add review fixes and unit test case for keybuidler
stevenzzzz Jun 12, 2019
b1d0d5c
clang-tidy fix
stevenzzzz Jun 12, 2019
1007ee0
use expect_debug_death for assertion test
stevenzzzz Jun 12, 2019
184ff2f
fix failed test in opt mode
stevenzzzz Jun 12, 2019
fe6b643
add assert around addFragment and test, and a few review fix
stevenzzzz Jun 13, 2019
fc98e56
Merge branch 'master' into add-key-builder
stevenzzzz Jun 19, 2019
951bd28
Merge branch 'master' into add-key-builder
stevenzzzz Jun 20, 2019
75718d9
fix based on feedbacks
stevenzzzz Jun 21, 2019
d84839d
fix nits
stevenzzzz Jun 25, 2019
27ff0a3
switch from constructing proto memeber by value(copy-elision) to more…
stevenzzzz Jun 25, 2019
36a6e72
refactor scoped config impl a little bit, andd SRDS config update impl
stevenzzzz Jul 2, 2019
46e23f2
Merge branch 'master' into scoped-config-impl-refactor
stevenzzzz Jul 2, 2019
74038f4
fix typo
stevenzzzz Jul 2, 2019
ff7ccdb
fix srds integration test and add some more test on scopekey
stevenzzzz Jul 8, 2019
06bd690
Merge branch 'CLEAN' into scoped-config-impl-refactor
stevenzzzz Jul 8, 2019
cf38720
save review fixes
stevenzzzz Jul 9, 2019
5cad3ee
add conflict detection for scope keys
stevenzzzz Jul 9, 2019
50e8fb5
Review feedbacks fix, and some more changes to the config provider fr…
stevenzzzz Jul 12, 2019
ac65ba2
Merge branch 'master' into scoped-config-impl-refactor
stevenzzzz Jul 12, 2019
cb5324f
make SRDS the SotW onConfigUpdate API a quasi-incremental one per dic…
stevenzzzz Jul 12, 2019
5c8a546
fix clang-tidy bug
stevenzzzz Jul 12, 2019
4a1fb01
revert the quasi-incremental delta onConfigUpdate change after offlin…
stevenzzzz Jul 15, 2019
fdd0a74
Merge branch 'CLEAN' into scoped-config-impl-refactor
stevenzzzz Jul 15, 2019
467a6a1
refactor config provider framework
stevenzzzz Jul 24, 2019
7dea1a0
some renames and stale comment fix
stevenzzzz Jul 24, 2019
c354a1e
merge PR #7704, add init_manager injection when creating RdsRouteConf…
stevenzzzz Jul 25, 2019
2b5f572
Merge branch 'master' of https://github.com/envoyproxy/envoy into add…
stevenzzzz Jul 25, 2019
9dd4214
fix a bug in update-propagation: new version should overwrite old ver…
stevenzzzz Jul 29, 2019
5d2f8a2
make a ScopeRouteInfo owns a RouteConfigProvider, as per the config p…
stevenzzzz Jul 29, 2019
86c455e
fixes for review feedbacks
stevenzzzz Jul 30, 2019
60dd433
fix comment
stevenzzzz Jul 30, 2019
6c1a672
snowp comments
stevenzzzz Jul 30, 2019
c31de28
Merge branch 'master' of https://github.com/envoyproxy/envoy into CLEAN
stevenzzzz Jul 30, 2019
abfe558
Merge branch 'master' into config-provider-update
stevenzzzz Jul 30, 2019
69df754
merge main & config-provider-update into srds onConfigUpdate PR
stevenzzzz Jul 30, 2019
5f357bd
some cleanup around noop watcher
stevenzzzz Jul 30, 2019
dd3b97d
fix-format
stevenzzzz Jul 30, 2019
ee09a5b
add more comment
stevenzzzz Aug 1, 2019
46ad9af
fix typo, add back missing constructor in config_provider_test
stevenzzzz Aug 1, 2019
47a417d
Merge branch 'master' of https://github.com/envoyproxy/envoy
stevenzzzz Aug 1, 2019
9c1f616
Merge branch 'master' of https://github.com/envoyproxy/envoy
stevenzzzz Aug 1, 2019
ae2ac31
merge with upstream master
stevenzzzz Aug 1, 2019
b1acd70
expect death when a null fragment is added into scopekey
stevenzzzz Aug 1, 2019
d08bdb2
fix per andres comments
stevenzzzz Aug 2, 2019
eb65b89
fix pedant spelling check errors.
stevenzzzz Aug 2, 2019
07bdfee
fix failing add null fragment assertion test. expect_death ==> expect…
stevenzzzz Aug 2, 2019
40b214e
only death test in debug mode for add null fragment to scopeKey
stevenzzzz Aug 2, 2019
4e57308
fix clang tidy errors around router/mock.h by removing empty destruct…
stevenzzzz Aug 6, 2019
710c7e8
move empty ctors,destructors back to cc file
stevenzzzz Aug 8, 2019
53081b2
Merge branch 'master' of https://github.com/envoyproxy/envoy
stevenzzzz Aug 8, 2019
2593bfa
merge with upstream master
stevenzzzz Aug 8, 2019
2dd6714
fix a race condition, in SRDS onConfigUpdate, when calling applyConfi…
stevenzzzz Aug 12, 2019
cef747d
add more review fixes
stevenzzzz Aug 12, 2019
bb0cdb8
disable cleanup timer on SRDS subscription destruction
stevenzzzz Aug 13, 2019
fa6ecb1
update comment on initManager overriding
stevenzzzz Aug 13, 2019
efaa891
A bunch of update after several issues found:
stevenzzzz Aug 20, 2019
bf4dc23
fix-format, and fix update for htuch's comments around ScopeKey hash …
stevenzzzz Aug 20, 2019
e329f49
Merge branch 'master' of https://github.com/envoyproxy/envoy into sco…
stevenzzzz Aug 20, 2019
c4719d8
split SRDS onConfigUpdate into smaller functions and some minor comme…
stevenzzzz Aug 21, 2019
56e2876
cleanup comment and remove unused header.
stevenzzzz Aug 21, 2019
d26d9b7
fix a bug introduced in the most recent function splitting, adjust th…
stevenzzzz Aug 21, 2019
4ab7005
fixes feedbacks from Andres
stevenzzzz Aug 22, 2019
bf703fc
fix typo
stevenzzzz Aug 22, 2019
2b375d4
fix nits from htuch feedbacks
stevenzzzz Aug 22, 2019
c7a241c
change back scope_name assignment to value, as protobuf returns a ref…
stevenzzzz Aug 22, 2019
c2ff015
fix nits
stevenzzzz Aug 22, 2019
eb5af9c
merge w/ upstream master
stevenzzzz Aug 22, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 17 additions & 27 deletions source/common/config/config_provider_impl.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#include <memory>

#include "envoy/config/config_provider.h"
#include "envoy/config/config_provider_manager.h"
#include "envoy/init/manager.h"
Expand Down Expand Up @@ -146,9 +144,7 @@ class MutableConfigProviderCommonBase;
* shared ownership of the underlying subscription.
*
*/
class ConfigSubscriptionCommonBase
: protected Logger::Loggable<Logger::Id::config>,
public std::enable_shared_from_this<ConfigSubscriptionCommonBase> {
class ConfigSubscriptionCommonBase : protected Logger::Loggable<Logger::Id::config> {
public:
// Callback for updating a Config implementation held in each worker thread, the callback is
// called in applyConfigUpdate() with the current version Config, and is expected to return the
Expand Down Expand Up @@ -224,21 +220,14 @@ class ConfigSubscriptionCommonBase
*/
void applyConfigUpdate(
const ConfigUpdateCb& update_fn, const Event::PostCb& complete_cb = []() {}) {
// It is safe to call shared_from_this here as this is in main thread, and destruction of a
// ConfigSubscriptionCommonBase owner (i.e., a provider) happens in main thread as well.
auto shared_this = shared_from_this();
tls_->runOnAllThreads(
[this, update_fn]() {
tls_->getTyped<ThreadLocalConfig>().config_ = update_fn(this->getConfig());
// NOTE: there is a known race condition between *this* subscription be teared down in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: s/be teared/being torn/, s/been executed/being executed/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

// main thread and the posted callback been executed before the destruction. See more
// details in https://github.com/envoyproxy/envoy/issues/7902
tls_->getTyped<ThreadLocalConfig>().config_ = update_fn(getConfig());
},
// During the update propagation, a subscription may get teared down in main thread due to
// all owners/providers destructed in a xDS update (e.g. LDS demolishes a
// RouteConfigProvider and its subscription).
// If such a race condition happens, holding a reference to the "*this" subscription
// instance in this cb will ensure the shared "*this" gets posted back to main thread, after
// all the workers finish calling the update_fn, at which point it's safe to destruct
// "*this" instance.
[shared_this, complete_cb]() { complete_cb(); });
complete_cb);
}

void setLastUpdated() { last_updated_ = time_source_.systemTime(); }
Expand Down Expand Up @@ -287,8 +276,8 @@ class ConfigSubscriptionInstance : public ConfigSubscriptionCommonBase {

/**
* Must be called by the derived class' constructor.
* @param initial_config supplies an initial Envoy::Config::ConfigProvider::Config associated with
* the underlying subscription, shared across all providers and workers.
* @param initial_config supplies an initial Envoy::Config::ConfigProvider::Config associated
* with the underlying subscription, shared across all providers and workers.
*/
void initialize(const ConfigProvider::ConfigConstSharedPtr& initial_config) {
tls_->set([initial_config](Event::Dispatcher&) -> ThreadLocal::ThreadLocalObjectSharedPtr {
Expand All @@ -302,7 +291,8 @@ class ConfigSubscriptionInstance : public ConfigSubscriptionCommonBase {
* @param config_proto supplies the newly received config proto.
* @param config_name supplies the name associated with the config.
* @param version_info supplies the version associated with the config.
* @return bool false when the config proto has no delta from the previous config, true otherwise.
* @return bool false when the config proto has no delta from the previous config, true
* otherwise.
*/
bool checkAndApplyConfigUpdate(const Protobuf::Message& config_proto,
const std::string& config_name, const std::string& version_info);
Expand Down Expand Up @@ -369,8 +359,8 @@ class MutableConfigProviderCommonBase : public ConfigProvider {
};

/**
* Provides generic functionality required by all config provider managers, such as managing shared
* lifetime of subscriptions and dynamic config providers, along with determining which
* Provides generic functionality required by all config provider managers, such as managing
* shared lifetime of subscriptions and dynamic config providers, along with determining which
* subscriptions should be associated with newly instantiated providers.
*
* The implementation of this class is not thread safe. Note that ImmutableConfigProviderBase
Expand All @@ -379,9 +369,9 @@ class MutableConfigProviderCommonBase : public ConfigProvider {
*
* All config processing is done on the main thread, so instantiation of *ConfigProvider* objects
* via createStaticConfigProvider() and createXdsConfigProvider() is naturally thread safe. Care
* must be taken with regards to destruction of these objects, since it must also happen on the main
* thread _prior_ to destruction of the ConfigProviderManagerImplBase object from which they were
* created.
* must be taken with regards to destruction of these objects, since it must also happen on the
* main thread _prior_ to destruction of the ConfigProviderManagerImplBase object from which they
* were created.
*
* This class can not be instantiated directly; instead, it provides the foundation for
* dynamic config provider implementations which derive from it.
Expand Down Expand Up @@ -415,8 +405,8 @@ class ConfigProviderManagerImplBase : public ConfigProviderManager, public Singl
const ConfigProviderSet& immutableConfigProviders(ConfigProviderInstanceType type) const;

/**
* Returns the subscription associated with the config_source_proto; if none exists, a new one is
* allocated according to the subscription_factory_fn.
* Returns the subscription associated with the config_source_proto; if none exists, a new one
* is allocated according to the subscription_factory_fn.
* @param config_source_proto supplies the proto specifying the config subscription parameters.
* @param init_manager supplies the init manager.
* @param subscription_factory_fn supplies a function to be called when a new subscription needs
Expand Down
3 changes: 3 additions & 0 deletions source/common/router/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ envoy_cc_library(
"//include/envoy/singleton:instance_interface",
"//include/envoy/thread_local:thread_local_interface",
"//source/common/common:assert_lib",
"//source/common/common:callback_impl_lib",
"//source/common/common:minimal_logger_lib",
"//source/common/config:rds_json_lib",
"//source/common/config:subscription_factory_lib",
Expand Down Expand Up @@ -168,12 +169,14 @@ envoy_cc_library(
srcs = ["scoped_rds.cc"],
hdrs = ["scoped_rds.h"],
deps = [
":rds_lib",
":scoped_config_lib",
"//include/envoy/config:config_provider_interface",
"//include/envoy/config:subscription_interface",
"//include/envoy/router:route_config_provider_manager_interface",
"//include/envoy/stats:stats_interface",
"//source/common/common:assert_lib",
"//source/common/common:cleanup_lib",
"//source/common/common:minimal_logger_lib",
"//source/common/config:config_provider_lib",
"//source/common/init:manager_lib",
Expand Down
1 change: 1 addition & 0 deletions source/common/router/rds_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ void RdsRouteConfigSubscription::onConfigUpdate(
}
vhds_subscription_.release();
}
update_callback_manager_.runCallbacks();
}

init_target_.ready();
Expand Down
11 changes: 11 additions & 0 deletions source/common/router/rds_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "envoy/stats/scope.h"
#include "envoy/thread_local/thread_local.h"

#include "common/common/callback_impl.h"
#include "common/common/logger.h"
#include "common/init/target_impl.h"
#include "common/protobuf/utility.h"
Expand All @@ -31,6 +32,9 @@
namespace Envoy {
namespace Router {

// For friend class declaration in RdsRouteConfigSubscription.
class ScopedRdsConfigSubscription;

/**
* Route configuration provider utilities.
*/
Expand Down Expand Up @@ -122,6 +126,10 @@ class RdsRouteConfigSubscription : Envoy::Config::SubscriptionCallbacks,
.name();
}

Common::CallbackHandle* addUpdateCallback(std::function<void()> callback) {
return update_callback_manager_.add(callback);
}

RdsRouteConfigSubscription(
const envoy::config::filter::network::http_connection_manager::v2::Rds& rds,
const uint64_t manager_identifier, Server::Configuration::FactoryContext& factory_context,
Expand All @@ -143,8 +151,11 @@ class RdsRouteConfigSubscription : Envoy::Config::SubscriptionCallbacks,
VhdsSubscriptionPtr vhds_subscription_;
RouteConfigUpdatePtr config_update_info_;
ProtobufMessage::ValidationVisitor& validation_visitor_;
Common::CallbackManager<> update_callback_manager_;

friend class RouteConfigProviderManagerImpl;
// Access to addUpdateCallback
friend class ScopedRdsConfigSubscription;
};

using RdsRouteConfigSubscriptionSharedPtr = std::shared_ptr<RdsRouteConfigSubscription>;
Expand Down
19 changes: 18 additions & 1 deletion source/common/router/scoped_config_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ HeaderValueExtractorImpl::computeFragment(const Http::HeaderMap& headers) const
return nullptr;
}

ScopedRouteInfo::ScopedRouteInfo(envoy::api::v2::ScopedRouteConfiguration&& config_proto,
ConfigConstSharedPtr&& route_config)
: config_proto_(std::move(config_proto)), route_config_(std::move(route_config)) {
// TODO(stevenzzzz): Maybe worth a KeyBuilder abstraction when there are more than one type of
// Fragment.
for (const auto& fragment : config_proto_.key().fragments()) {
switch (fragment.type_case()) {
case envoy::api::v2::ScopedRouteConfiguration::Key::Fragment::kStringKey:
scope_key_.addFragment(std::make_unique<StringKeyFragment>(fragment.string_key()));
break;
default:
NOT_REACHED_GCOVR_EXCL_LINE;
}
}
}

ScopeKeyBuilderImpl::ScopeKeyBuilderImpl(ScopedRoutes::ScopeKeyBuilder&& config)
: ScopeKeyBuilderBase(std::move(config)) {
for (const auto& fragment_builder : config_.fragments()) {
Expand Down Expand Up @@ -102,6 +118,7 @@ void ScopedConfigImpl::addOrUpdateRoutingScope(
const ScopedRouteInfoConstSharedPtr& scoped_route_info) {
const auto iter = scoped_route_info_by_name_.find(scoped_route_info->scopeName());
if (iter != scoped_route_info_by_name_.end()) {
ASSERT(scoped_route_info_by_key_.contains(iter->second->scopeKey().hash()));
scoped_route_info_by_key_.erase(iter->second->scopeKey().hash());
htuch marked this conversation as resolved.
Show resolved Hide resolved
}
scoped_route_info_by_name_[scoped_route_info->scopeName()] = scoped_route_info;
Expand All @@ -111,7 +128,7 @@ void ScopedConfigImpl::addOrUpdateRoutingScope(
void ScopedConfigImpl::removeRoutingScope(const std::string& scope_name) {
const auto iter = scoped_route_info_by_name_.find(scope_name);
if (iter != scoped_route_info_by_name_.end()) {
ASSERT(scoped_route_info_by_key_.count(iter->second->scopeKey().hash()) == 1);
ASSERT(scoped_route_info_by_key_.contains(iter->second->scopeKey().hash()));
scoped_route_info_by_key_.erase(iter->second->scopeKey().hash());
stevenzzzz marked this conversation as resolved.
Show resolved Hide resolved
scoped_route_info_by_name_.erase(iter);
}
Expand Down
34 changes: 9 additions & 25 deletions source/common/router/scoped_config_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ class ScopeKey {
private:
// Update the key's hash with the new fragment hash.
void updateHash(const ScopeKeyFragmentBase& fragment) {
absl::uint128 buffer = absl::MakeUint128(hash_, fragment.hash());
hash_ = HashUtil::xxHash64(absl::string_view(reinterpret_cast<char*>(&buffer), 16));
std::stringbuf buffer;
buffer.sputn(reinterpret_cast<const char*>(&hash_), sizeof(hash_));
const auto& fragment_hash = fragment.hash();
buffer.sputn(reinterpret_cast<const char*>(&fragment_hash), sizeof(fragment_hash));
hash_ = HashUtil::xxHash64(buffer.str());
}

uint64_t hash_{0};
Expand Down Expand Up @@ -147,36 +150,17 @@ class ScopeKeyBuilderImpl : public ScopeKeyBuilderBase {
class ScopedRouteInfo {
public:
ScopedRouteInfo(envoy::api::v2::ScopedRouteConfiguration&& config_proto,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const envoy::api::v2::ScopedRouteConfiguration?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, the constructor is expecting a rvalue to be moved into the proto member (per htuch@ pointing it out Envoy prefers more visible && than the copy-elision envoy::api::v2::ScopedRouteConfiguration parameter).

std::unique_ptr<RouteConfigProvider>&& route_provider)
: config_proto_(std::move(config_proto)), route_provider_(std::move(route_provider)) {
ASSERT(route_provider_ != nullptr, "ScopedRouteInfo expects a valid RouteConfigProvider.");
ASSERT(
!route_provider_->configInfo().has_value() ||
route_provider_->config()->name() == config_proto_.route_configuration_name(),
fmt::format("RouteConfigProvider's name '{}' doesn't match route_configuration_name '{}'.",
route_provider_->config()->name(), config_proto_.route_configuration_name()));
// TODO(stevenzzzz): Maybe worth a KeyBuilder abstraction when there are more than one type of
// Fragment.
for (const auto& fragment : config_proto_.key().fragments()) {
switch (fragment.type_case()) {
case envoy::api::v2::ScopedRouteConfiguration::Key::Fragment::kStringKey:
scope_key_.addFragment(std::make_unique<StringKeyFragment>(fragment.string_key()));
break;
default:
NOT_REACHED_GCOVR_EXCL_LINE;
}
}
}
ConfigConstSharedPtr&& route_config);

Router::ConfigConstSharedPtr routeConfig() const { return route_provider_->config(); }
ConfigConstSharedPtr routeConfig() const { return route_config_; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: usually we return const ConfigConstSharedPtr&
And leave the caller to decide if a copy(bump ref count) is needed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This config is supposed to be shared between workers which do the actually routing, they need to take a share of the ownership of it.
but I don't have a strong feeling as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me make it clear.

This config is supposed to be shared between workers which do the actually routing, they need to take a share of the ownership of it.
By They I believe you refers the caller of routeConfig(). They could copy if they need.

Of course it can never be wrong if you do the early copy at the routeConfig()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returning an reference also means the function makes a promise to callers about the life circle of the return object (personally I dont like returning reference), just imagine what if underlying obj is updated in one thread, and is read in another thread(we are distributing a scopedRouteInfo across threads).
But in this specific case, the ownership will be distributed anyway and the immutability of ScopedRouteInfo is guaranteed by the SRDS subscription, so I agree we can return a reference here.

const ScopeKey& scopeKey() const { return scope_key_; }
const envoy::api::v2::ScopedRouteConfiguration& configProto() const { return config_proto_; }
const std::string& scopeName() const { return config_proto_.name(); }

private:
const envoy::api::v2::ScopedRouteConfiguration config_proto_;
envoy::api::v2::ScopedRouteConfiguration config_proto_;
ScopeKey scope_key_;
std::unique_ptr<RouteConfigProvider> route_provider_;
ConfigConstSharedPtr route_config_;
};
using ScopedRouteInfoConstSharedPtr = std::shared_ptr<const ScopedRouteInfo>;
// Ordered map for consistent config dumping.
Expand Down
Loading