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 2 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
1 change: 1 addition & 0 deletions source/common/router/scoped_config_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ void ThreadLocalScopedConfigImpl::addOrUpdateRoutingScope(
void ThreadLocalScopedConfigImpl::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);
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
16 changes: 9 additions & 7 deletions source/common/router/scoped_config_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,14 @@ class ScopeKey {
// String fragment.
class StringKeyFragment : public ScopeKeyFragmentBase {
public:
explicit StringKeyFragment(absl::string_view value) : value_(value) {}
explicit StringKeyFragment(absl::string_view value)
: value_(value), hash_(HashUtil::xxHash64(value_)) {}

uint64_t hash() const override { return HashUtil::xxHash64(value_); }
uint64_t hash() const override { return hash_; }

private:
const std::string value_;
const uint64_t hash_;
};

/**
Expand Down Expand Up @@ -148,11 +150,11 @@ class ScopedRouteInfo {
std::shared_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(),
absl::StrFormat(
"RouteConfigProvider's name '%s' doesn't match route_configuration_name '%s'.",
route_provider_->config()->name(), config_proto_.route_configuration_name()));
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()) {
Expand Down
129 changes: 81 additions & 48 deletions source/common/router/scoped_rds.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "common/common/assert.h"
#include "common/common/logger.h"
#include "common/common/utility.h"

// Types are deeply nested under Envoy::Config::ConfigProvider; use 'using-directives' across all
// ConfigProvider related types for consistency.
Expand Down Expand Up @@ -98,77 +99,109 @@ void ScopedRdsConfigSubscription::onConfigUpdate(
const Protobuf::RepeatedPtrField<envoy::api::v2::Resource>& added_resources,
const Protobuf::RepeatedPtrField<std::string>& removed_resources,
const std::string& version_info) {

// Dedup.
bool any_applied = false;
std::vector<std::string> exception_msgs;
absl::flat_hash_set<std::string> unique_resource_names;
for (const auto& resource : added_resources) {
// TODO(stevenzzzzz): we need to discuss about what to do when two ScopedRouteConfiguration
// which has the same ScopeKey.
if (!unique_resource_names.insert(resource.name()).second) {
throw EnvoyException(
fmt::format("duplicate scoped route configuration {} found", resource.name()));
}
}

std::vector<envoy::api::v2::ScopedRouteConfiguration> scoped_route_configs;
for (const auto& resource : added_resources) {
auto scoped_route_config = MessageUtil::anyConvert<envoy::api::v2::ScopedRouteConfiguration>(
resource.resource(), validation_visitor_);
MessageUtil::validate(scoped_route_config);
scoped_route_configs.emplace_back(std::move(scoped_route_config));
}

envoy::config::filter::network::http_connection_manager::v2::Rds rds;
rds.mutable_config_source()->MergeFrom(rds_config_source_);
for (auto& scoped_route_config : scoped_route_configs) {
ENVOY_LOG(debug, "srds: add/update scoped_route '{}'", scoped_route_config.name());
rds.set_route_config_name(scoped_route_config.route_configuration_name());
ScopedRouteInfoConstSharedPtr scoped_route_info = std::make_shared<ScopedRouteInfo>(
std::move(scoped_route_config),
srds_config_provider_manager_.getRouteConfigProvider(factory_context_, rds, stat_prefix_));
scoped_route_map_[scoped_route_info->scopeName()] = scoped_route_info;
applyDeltaConfigUpdate([scoped_route_info](const ConfigProvider::ConfigConstSharedPtr& config) {
auto* thread_local_scoped_config = const_cast<ThreadLocalScopedConfigImpl*>(
static_cast<const ThreadLocalScopedConfigImpl*>(config.get()));
for (const auto& resource : added_resources) {
envoy::api::v2::ScopedRouteConfiguration scoped_route_config;
try {
scoped_route_config = MessageUtil::anyConvert<envoy::api::v2::ScopedRouteConfiguration>(
resource.resource(), validation_visitor_);
MessageUtil::validate(scoped_route_config);
if (!unique_resource_names.insert(scoped_route_config.name()).second) {
throw EnvoyException(fmt::format("duplicate scoped route configuration '{}' found",
scoped_route_config.name()));
}
rds.set_route_config_name(scoped_route_config.route_configuration_name());
ScopedRouteInfoConstSharedPtr scoped_route_info = std::make_shared<ScopedRouteInfo>(
std::move(scoped_route_config), srds_config_provider_manager_.createRouteConfigProvider(
factory_context_, rds, stat_prefix_));
// Detect if there is key conflict between two scopes.
Copy link
Member

Choose a reason for hiding this comment

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

This is subtle and worth expanding on in comments. What's the expected behavior from the operator?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

add some more comment.

auto iter = scope_name_by_hash_.find(scoped_route_info->scopeKey().hash());
if (iter != scope_name_by_hash_.end() && iter->second != scoped_route_info->scopeName()) {
throw EnvoyException(
fmt::format("scope key conflict found, first scope is '{}', second scope is '{}'",
iter->second, scoped_route_info->scopeName()));
}
scope_name_by_hash_[scoped_route_info->scopeKey().hash()] = scoped_route_info->scopeName();
scoped_route_map_[scoped_route_info->scopeName()] = scoped_route_info;
applyDeltaConfigUpdate(
[scoped_route_info](const ConfigProvider::ConfigConstSharedPtr& config) {
auto* thread_local_scoped_config = const_cast<ThreadLocalScopedConfigImpl*>(
static_cast<const ThreadLocalScopedConfigImpl*>(config.get()));

thread_local_scoped_config->addOrUpdateRoutingScope(scoped_route_info);
});
thread_local_scoped_config->addOrUpdateRoutingScope(scoped_route_info);
});
any_applied = true;
ENVOY_LOG(debug, "srds: add/update scoped_route '{}'", scoped_route_info->scopeName());
} catch (const EnvoyException& e) {
exception_msgs.emplace_back(fmt::format("{}", e.what()));
}
}
for (const auto& scoped_route_name : removed_resources) {
ENVOY_LOG(debug, "srds: remove scoped route '{}'", scoped_route_name);
scoped_route_map_.erase(scoped_route_name);
applyDeltaConfigUpdate([scoped_route_name](const ConfigProvider::ConfigConstSharedPtr& config) {
auto* thread_local_scoped_config = const_cast<ThreadLocalScopedConfigImpl*>(
static_cast<const ThreadLocalScopedConfigImpl*>(config.get()));
thread_local_scoped_config->removeRoutingScope(scoped_route_name);
});
for (const auto& scope_name : removed_resources) {
auto iter = scoped_route_map_.find(scope_name);
if (iter != scoped_route_map_.end()) {
scope_name_by_hash_.erase(iter->second->scopeKey().hash());
scoped_route_map_.erase(iter);
applyDeltaConfigUpdate([scope_name](const ConfigProvider::ConfigConstSharedPtr& config) {
auto* thread_local_scoped_config = const_cast<ThreadLocalScopedConfigImpl*>(
static_cast<const ThreadLocalScopedConfigImpl*>(config.get()));
thread_local_scoped_config->removeRoutingScope(scope_name);
htuch marked this conversation as resolved.
Show resolved Hide resolved
});
any_applied = true;
ENVOY_LOG(debug, "srds: remove scoped route '{}'", scope_name);
}
htuch marked this conversation as resolved.
Show resolved Hide resolved
}
ConfigSubscriptionCommonBase::onConfigUpdate();
setLastConfigInfo(absl::optional<LastConfigInfo>({absl::nullopt, version_info}));
if (any_applied) {
setLastConfigInfo(absl::optional<LastConfigInfo>({absl::nullopt, version_info}));
}
stats_.config_reload_.inc();
if (!exception_msgs.empty()) {
throw EnvoyException(fmt::format("Error adding/updating scoped route(s): {}",
StringUtil::join(exception_msgs, ", ")));
}
}

// TODO(stevenzzzz): consider generalizing this function as it overlaps with
// CdsApiImpl::onConfigUpdate.
void ScopedRdsConfigSubscription::onConfigUpdate(
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems to be replicating the logic in CdsApiImpl::onConfigUpdate. How about generalizing into a utility function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd leave a cleanup-TODO to do that in another PR.

htuch marked this conversation as resolved.
Show resolved Hide resolved
const Protobuf::RepeatedPtrField<ProtobufWkt::Any>& resources,
const std::string& version_info) {
std::vector<envoy::api::v2::ScopedRouteConfiguration> scoped_routes;
absl::flat_hash_map<std::string, envoy::api::v2::ScopedRouteConfiguration> scoped_routes;
absl::flat_hash_map<uint64_t, std::string> scope_key_by_key_hash;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be scope_name_by_key_hash for consistency?

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

for (const auto& resource_any : resources) {
// Throws (thus rejects all) on any error.
htuch marked this conversation as resolved.
Show resolved Hide resolved
auto scoped_route = MessageUtil::anyConvert<envoy::api::v2::ScopedRouteConfiguration>(
resource_any, validation_visitor_);
MessageUtil::validate(scoped_route);
scoped_routes.emplace_back(std::move(scoped_route));
const std::string scope_name = scoped_route.name();
auto scope_config_inserted = scoped_routes.try_emplace(scope_name, std::move(scoped_route));
if (!scope_config_inserted.second) {
throw EnvoyException(
fmt::format("duplicate scoped route configuration '{}' found", scoped_route.name()));
}
const envoy::api::v2::ScopedRouteConfiguration& scoped_route_config =
scope_config_inserted.first->second;
uint64_t key_fingerprint = MessageUtil::hash(scoped_route_config.key());
Copy link
Member

Choose a reason for hiding this comment

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

Nit: const

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

if (!scope_key_by_key_hash.try_emplace(key_fingerprint, scope_name).second) {
throw EnvoyException(
fmt::format("scope key conflict found, first scope is '{}', second scope is '{}'",
scope_key_by_key_hash[key_fingerprint], scope_name));
}
}
ScopedRouteMap scoped_routes_to_remove = scoped_route_map_;
Protobuf::RepeatedPtrField<envoy::api::v2::Resource> to_add_repeated;
Protobuf::RepeatedPtrField<std::string> to_remove_repeated;

for (auto& scoped_route : scoped_routes) {
const std::string& scoped_route_name = scoped_route.name();
scoped_routes_to_remove.erase(scoped_route_name);
for (auto& iter : scoped_routes) {
const std::string& scope_name = iter.first;
scoped_routes_to_remove.erase(scope_name);
auto* to_add = to_add_repeated.Add();
to_add->set_name(scoped_route.name());
to_add->set_name(scope_name);
to_add->set_version(version_info);
to_add->mutable_resource()->PackFrom(scoped_route);
to_add->mutable_resource()->PackFrom(iter.second);
}

for (const auto& scoped_route : scoped_routes_to_remove) {
Expand Down
32 changes: 10 additions & 22 deletions source/common/router/scoped_rds.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ class ScopedRdsConfigSubscription : public Envoy::Config::DeltaConfigSubscriptio
.name();
}

// ScopedRouteInfo by scope name.
ScopedRouteMap scoped_route_map_;
// A map of (hash, scope-name), used to detect the key conflict between scopes.
absl::flat_hash_map<uint64_t, std::string> scope_name_by_hash_;
// For creating RDS subscriptions.
Server::Configuration::FactoryContext& factory_context_;
const std::string name_;
Expand Down Expand Up @@ -194,33 +197,18 @@ class ScopedRoutesConfigProviderManager : public Envoy::Config::ConfigProviderMa
return route_config_provider_manager_;
}

std::shared_ptr<RouteConfigProvider>
getRouteConfigProvider(Server::Configuration::FactoryContext& factory_context,
envoy::config::filter::network::http_connection_manager::v2::Rds& rds,
std::string stat_name) {
const uint64_t route_provider_id = MessageUtil::hash(rds);
auto iter = cached_route_providers_.find(route_provider_id);
if (iter != cached_route_providers_.end()) {
return iter->second;
}
std::shared_ptr<RouteConfigProvider> provider =
route_config_provider_manager_.createRdsRouteConfigProvider(rds, factory_context,
stat_name);
cached_route_providers_.try_emplace(route_provider_id, provider);
return provider;
}
void
deleteRouteConfigSource(envoy::config::filter::network::http_connection_manager::v2::Rds& rds) {
const uint64_t route_provider_id = MessageUtil::hash(rds);
if (!cached_route_providers_.erase(route_provider_id)) {
ENVOY_LOG_MISC(warn, "Try to delete non-existing route config source {}", rds.DebugString());
}
std::shared_ptr<RouteConfigProvider> createRouteConfigProvider(
Server::Configuration::FactoryContext& factory_context,
const envoy::config::filter::network::http_connection_manager::v2::Rds& rds,
const std::string& stat_name) {
return route_config_provider_manager_.createRdsRouteConfigProvider(rds, factory_context,
stat_name);
}

private:
RouteConfigProviderManager& route_config_provider_manager_;
// From ConfigSource fingerprint to shared RouteConfigProvider.
absl::flat_hash_map<uint64_t, std::shared_ptr<RouteConfigProvider>> cached_route_providers_;
absl::flat_hash_map<std::string, std::shared_ptr<RouteConfigProvider>> cached_route_providers_;
Copy link
Contributor

Choose a reason for hiding this comment

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

This appears to be unused now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops, removed.

};

// The optional argument passed to the ConfigProviderManager::create*() functions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include "envoy/config/filter/network/http_connection_manager/v2/http_connection_manager.pb.validate.h"
#include "envoy/filesystem/filesystem.h"
#include "envoy/registry/registry.h"
#include "envoy/server/admin.h"

#include "common/access_log/access_log_impl.h"
Expand Down Expand Up @@ -104,7 +103,8 @@ HttpConnectionManagerFilterConfigFactory::createFilterFactoryFromProtoTyped(

// This lambda captures the shared_ptrs created above, thus preserving the
// reference count.
// Keep in mind the lambda capture list **doesn't** determine the destruction order, the
// Keep in mind the lambda capture list **doesn't** determine the destruction order, but it's fine
// as these captured objects are also global singletons.
return [scoped_routes_config_provider_manager, route_config_provider_manager, date_provider,
filter_config, &context](Network::FilterManager& filter_manager) -> void {
filter_manager.addReadFilter(Network::ReadFilterSharedPtr{new Http::ConnectionManagerImpl(
Expand Down
Loading