From f99bc55ffede5ea22d61cf65453f338e1bf1c4b7 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Tue, 2 Apr 2024 11:38:58 -0700 Subject: [PATCH] [channel-init] Add an escape hatch to have v2-only filters (#36225) Closes #36225 COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/36225 from ctiller:skipv3 b4857c3019bbc3481a8c90043fdaaa4ec14ad53d PiperOrigin-RevId: 621245586 --- .../legacy_channel_idle_filter.cc | 4 ++-- src/core/lib/surface/channel_init.cc | 13 ++++++------ src/core/lib/surface/channel_init.h | 20 +++++++++++++++++-- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/core/ext/filters/channel_idle/legacy_channel_idle_filter.cc b/src/core/ext/filters/channel_idle/legacy_channel_idle_filter.cc index e693d73d1f2..a1e6fd07160 100644 --- a/src/core/ext/filters/channel_idle/legacy_channel_idle_filter.cc +++ b/src/core/ext/filters/channel_idle/legacy_channel_idle_filter.cc @@ -296,13 +296,13 @@ const grpc_channel_filter LegacyMaxAgeFilter::kFilter = void RegisterLegacyChannelIdleFilters(CoreConfiguration::Builder* builder) { builder->channel_init() - ->RegisterFilter(GRPC_CLIENT_CHANNEL) + ->RegisterV2Filter(GRPC_CLIENT_CHANNEL) .ExcludeFromMinimalStack() .If([](const ChannelArgs& channel_args) { return GetClientIdleTimeout(channel_args) != Duration::Infinity(); }); builder->channel_init() - ->RegisterFilter(GRPC_SERVER_CHANNEL) + ->RegisterV2Filter(GRPC_SERVER_CHANNEL) .ExcludeFromMinimalStack() .If([](const ChannelArgs& channel_args) { return LegacyMaxAgeFilter::Config::FromChannelArgs(channel_args) diff --git a/src/core/lib/surface/channel_init.cc b/src/core/lib/surface/channel_init.cc index cb74f821f79..abd78dc76b1 100644 --- a/src/core/lib/surface/channel_init.cc +++ b/src/core/lib/surface/channel_init.cc @@ -141,9 +141,9 @@ ChannelInit::StackConfig ChannelInit::BuildStackConfig( GPR_ASSERT(registration->after_.empty()); GPR_ASSERT(registration->before_.empty()); GPR_ASSERT(!registration->before_all_); - terminal_filters.emplace_back(registration->filter_, nullptr, - std::move(registration->predicates_), - registration->registration_source_); + terminal_filters.emplace_back( + registration->filter_, nullptr, std::move(registration->predicates_), + registration->skip_v3_, registration->registration_source_); } else { dependencies[registration->filter_]; // Ensure it's in the map. } @@ -223,9 +223,9 @@ ChannelInit::StackConfig ChannelInit::BuildStackConfig( while (!dependencies.empty()) { auto filter = take_ready_dependency(); auto* registration = filter_to_registration[filter]; - filters.emplace_back(filter, registration->vtable_, - std::move(registration->predicates_), - registration->registration_source_); + filters.emplace_back( + filter, registration->vtable_, std::move(registration->predicates_), + registration->skip_v3_, registration->registration_source_); for (auto& p : dependencies) { p.second.erase(filter); } @@ -414,6 +414,7 @@ absl::StatusOr ChannelInit::CreateStackSegment( size_t channel_data_alignment = 0; // Based on predicates build a list of filters to include in this segment. for (const auto& filter : stack_config.filters) { + if (filter.skip_v3) continue; if (!filter.CheckPredicates(args)) continue; if (filter.vtable == nullptr) { return absl::InvalidArgumentError( diff --git a/src/core/lib/surface/channel_init.h b/src/core/lib/surface/channel_init.h index 5b7b317b4c2..052c30e8012 100644 --- a/src/core/lib/surface/channel_init.h +++ b/src/core/lib/surface/channel_init.h @@ -162,6 +162,10 @@ class ChannelInit { // Add a predicate that ensures this filter does not appear in the minimal // stack. FilterRegistration& ExcludeFromMinimalStack(); + FilterRegistration& SkipV3() { + skip_v3_ = true; + return *this; + } private: friend class ChannelInit; @@ -172,6 +176,7 @@ class ChannelInit { std::vector predicates_; bool terminal_ = false; bool before_all_ = false; + bool skip_v3_ = false; SourceLocation registration_source_; }; @@ -195,6 +200,15 @@ class ChannelInit { registration_source); } + // Filter does not participate in v3 + template + FilterRegistration& RegisterV2Filter( + grpc_channel_stack_type type, SourceLocation registration_source = {}) { + return RegisterFilter(type, &Filter::kFilter, nullptr, + registration_source) + .SkipV3(); + } + // Register a post processor for the builder. // These run after the main graph has been placed into the builder. // At most one filter per slot per channel stack type can be added. @@ -274,16 +288,18 @@ class ChannelInit { private: struct Filter { Filter(const grpc_channel_filter* filter, const ChannelFilterVtable* vtable, - std::vector predicates, + std::vector predicates, bool skip_v3, SourceLocation registration_source) : filter(filter), vtable(vtable), predicates(std::move(predicates)), - registration_source(registration_source) {} + registration_source(registration_source), + skip_v3(skip_v3) {} const grpc_channel_filter* filter; const ChannelFilterVtable* vtable; std::vector predicates; SourceLocation registration_source; + bool skip_v3 = false; bool CheckPredicates(const ChannelArgs& args) const; }; struct StackConfig {