[call-v3] Channel filter construction returns pointers (#36355)

Currently channel filter construction returns a `StatusOr<T>`, this change makes it return a `StatusOr<P<T>>` where P is `unique_ptr`, `OrphanablePtr`, `RefCountedPtr`, `DualRefCountedPtr`, etc (most of the code really doesn't need to know, so I'm choosing to leave the flexibility).

That smart pointer is then stored in the channel stack instance, and dereferenced when needed.

This means that channel filters no longer need to be movable (which is a nice simplification), and puts these level-1 filters on a similar memory management track as the level-2 filters we have planned.

(this change also converts client load reporting to v3 apis -- it's a bit accidentally picked up, but seems ok to pull through too)

Closes #36355

COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/36355 from ctiller:objectify-me 0eb054b748
PiperOrigin-RevId: 625390977
pull/36345/head
Craig Tiller 8 months ago committed by Yash Tibrewal
parent 7561250649
commit 58b254dacf
  1. 7
      src/core/ext/filters/backend_metrics/backend_metric_filter.cc
  2. 4
      src/core/ext/filters/backend_metrics/backend_metric_filter.h
  3. 18
      src/core/ext/filters/channel_idle/legacy_channel_idle_filter.cc
  4. 21
      src/core/ext/filters/channel_idle/legacy_channel_idle_filter.h
  5. 13
      src/core/ext/filters/fault_injection/fault_injection_filter.cc
  6. 8
      src/core/ext/filters/fault_injection/fault_injection_filter.h
  7. 9
      src/core/ext/filters/http/client/http_client_filter.cc
  8. 10
      src/core/ext/filters/http/client/http_client_filter.h
  9. 7
      src/core/ext/filters/http/client_authority_filter.cc
  10. 9
      src/core/ext/filters/http/client_authority_filter.h
  11. 12
      src/core/ext/filters/http/message_compress/compression_filter.cc
  12. 16
      src/core/ext/filters/http/message_compress/compression_filter.h
  13. 4
      src/core/ext/filters/http/server/http_server_filter.cc
  14. 10
      src/core/ext/filters/http/server/http_server_filter.h
  15. 9
      src/core/ext/filters/load_reporting/server_load_reporting_filter.cc
  16. 2
      src/core/ext/filters/load_reporting/server_load_reporting_filter.h
  17. 19
      src/core/ext/filters/logging/logging_filter.cc
  18. 11
      src/core/ext/filters/logging/logging_filter.h
  19. 14
      src/core/ext/filters/message_size/message_size_filter.cc
  20. 14
      src/core/ext/filters/message_size/message_size_filter.h
  21. 15
      src/core/ext/filters/rbac/rbac_filter.cc
  22. 10
      src/core/ext/filters/rbac/rbac_filter.h
  23. 63
      src/core/ext/filters/server_config_selector/server_config_selector_filter.cc
  24. 7
      src/core/ext/filters/stateful_session/stateful_session_filter.cc
  25. 5
      src/core/ext/filters/stateful_session/stateful_session_filter.h
  26. 6
      src/core/lib/channel/promise_based_filter.cc
  27. 46
      src/core/lib/channel/promise_based_filter.h
  28. 10
      src/core/lib/channel/server_call_tracer_filter.cc
  29. 6
      src/core/lib/security/authorization/grpc_server_authz_filter.cc
  30. 12
      src/core/lib/security/authorization/grpc_server_authz_filter.h
  31. 22
      src/core/lib/security/transport/auth_filters.h
  32. 7
      src/core/lib/security/transport/client_auth_filter.cc
  33. 5
      src/core/lib/security/transport/server_auth_filter.cc
  34. 15
      src/core/lib/surface/channel_init.h
  35. 18
      src/core/lib/surface/lame_client.cc
  36. 14
      src/core/lib/surface/lame_client.h
  37. 58
      src/core/load_balancing/grpclb/client_load_reporting_filter.cc
  38. 27
      src/core/load_balancing/grpclb/client_load_reporting_filter.h
  39. 13
      src/core/resolver/xds/xds_resolver.cc
  40. 13
      src/core/service_config/service_config_channel_arg_filter.cc
  41. 8
      src/cpp/ext/filters/census/client_filter.cc
  42. 7
      src/cpp/ext/filters/census/client_filter.h
  43. 2
      test/core/filters/filter_test.h
  44. 27
      test/core/filters/filter_test_test.cc
  45. 6
      test/core/surface/channel_init_test.cc
  46. 15
      test/core/util/fake_stats_plugin.cc
  47. 16
      test/cpp/ext/otel/otel_test_library.cc

@ -25,6 +25,7 @@
#include <utility>
#include "absl/strings/string_view.h"
#include "third_party/grpc/src/core/lib/channel/promise_based_filter.h"
#include "upb/base/string_view.h"
#include "upb/mem/arena.hpp"
#include "xds/data/orca/v3/orca_load_report.upb.h"
@ -121,9 +122,9 @@ const grpc_channel_filter BackendMetricFilter::kFilter =
MakePromiseBasedFilter<BackendMetricFilter, FilterEndpoint::kServer>(
"backend_metric");
absl::StatusOr<BackendMetricFilter> BackendMetricFilter::Create(
const ChannelArgs&, ChannelFilter::Args) {
return BackendMetricFilter();
absl::StatusOr<std::unique_ptr<BackendMetricFilter>>
BackendMetricFilter::Create(const ChannelArgs&, ChannelFilter::Args) {
return std::make_unique<BackendMetricFilter>();
}
void BackendMetricFilter::Call::OnServerTrailingMetadata(ServerMetadata& md) {

@ -35,8 +35,8 @@ class BackendMetricFilter : public ImplementChannelFilter<BackendMetricFilter> {
public:
static const grpc_channel_filter kFilter;
static absl::StatusOr<BackendMetricFilter> Create(const ChannelArgs& args,
ChannelFilter::Args);
static absl::StatusOr<std::unique_ptr<BackendMetricFilter>> Create(
const ChannelArgs& args, ChannelFilter::Args);
class Call {
public:

@ -25,6 +25,7 @@
#include "absl/base/thread_annotations.h"
#include "absl/meta/type_traits.h"
#include "absl/random/random.h"
#include "absl/status/statusor.h"
#include "absl/types/optional.h"
#include <grpc/impl/channel_arg_names.h>
@ -133,18 +134,17 @@ struct LegacyMaxAgeFilter::Config {
// will be removed at that time also, so just disable the deprecation warning
// for now.
ABSL_INTERNAL_DISABLE_DEPRECATED_DECLARATION_WARNING
absl::StatusOr<LegacyClientIdleFilter> LegacyClientIdleFilter::Create(
const ChannelArgs& args, ChannelFilter::Args filter_args) {
LegacyClientIdleFilter filter(filter_args.channel_stack(),
GetClientIdleTimeout(args));
return absl::StatusOr<LegacyClientIdleFilter>(std::move(filter));
absl::StatusOr<std::unique_ptr<LegacyClientIdleFilter>>
LegacyClientIdleFilter::Create(const ChannelArgs& args,
ChannelFilter::Args filter_args) {
return std::make_unique<LegacyClientIdleFilter>(filter_args.channel_stack(),
GetClientIdleTimeout(args));
}
absl::StatusOr<LegacyMaxAgeFilter> LegacyMaxAgeFilter::Create(
absl::StatusOr<std::unique_ptr<LegacyMaxAgeFilter>> LegacyMaxAgeFilter::Create(
const ChannelArgs& args, ChannelFilter::Args filter_args) {
LegacyMaxAgeFilter filter(filter_args.channel_stack(),
Config::FromChannelArgs(args));
return absl::StatusOr<LegacyMaxAgeFilter>(std::move(filter));
return std::make_unique<LegacyMaxAgeFilter>(filter_args.channel_stack(),
Config::FromChannelArgs(args));
}
ABSL_INTERNAL_RESTORE_DEPRECATED_DECLARATION_WARNING

@ -42,6 +42,11 @@ namespace grpc_core {
class LegacyChannelIdleFilter : public ChannelFilter {
public:
LegacyChannelIdleFilter(grpc_channel_stack* channel_stack,
Duration client_idle_timeout)
: channel_stack_(channel_stack),
client_idle_timeout_(client_idle_timeout) {}
~LegacyChannelIdleFilter() override = default;
LegacyChannelIdleFilter(const LegacyChannelIdleFilter&) = delete;
@ -59,11 +64,6 @@ class LegacyChannelIdleFilter : public ChannelFilter {
using SingleSetActivityPtr =
SingleSetPtr<Activity, typename ActivityPtr::deleter_type>;
LegacyChannelIdleFilter(grpc_channel_stack* channel_stack,
Duration client_idle_timeout)
: channel_stack_(channel_stack),
client_idle_timeout_(client_idle_timeout) {}
grpc_channel_stack* channel_stack() { return channel_stack_; };
virtual void Shutdown();
@ -94,10 +94,9 @@ class LegacyClientIdleFilter final : public LegacyChannelIdleFilter {
public:
static const grpc_channel_filter kFilter;
static absl::StatusOr<LegacyClientIdleFilter> Create(
static absl::StatusOr<std::unique_ptr<LegacyClientIdleFilter>> Create(
const ChannelArgs& args, ChannelFilter::Args filter_args);
private:
using LegacyChannelIdleFilter::LegacyChannelIdleFilter;
};
@ -106,9 +105,12 @@ class LegacyMaxAgeFilter final : public LegacyChannelIdleFilter {
static const grpc_channel_filter kFilter;
struct Config;
static absl::StatusOr<LegacyMaxAgeFilter> Create(
static absl::StatusOr<std::unique_ptr<LegacyMaxAgeFilter>> Create(
const ChannelArgs& args, ChannelFilter::Args filter_args);
LegacyMaxAgeFilter(grpc_channel_stack* channel_stack,
const Config& max_age_config);
void PostInit() override;
private:
@ -128,9 +130,6 @@ class LegacyMaxAgeFilter final : public LegacyChannelIdleFilter {
LegacyMaxAgeFilter* filter_;
};
LegacyMaxAgeFilter(grpc_channel_stack* channel_stack,
const Config& max_age_config);
void Shutdown() override;
SingleSetActivityPtr max_age_activity_;

@ -29,6 +29,7 @@
#include "absl/meta/type_traits.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/numbers.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
@ -135,16 +136,16 @@ class FaultInjectionFilter::InjectionDecision {
FaultHandle active_fault_{false};
};
absl::StatusOr<FaultInjectionFilter> FaultInjectionFilter::Create(
const ChannelArgs&, ChannelFilter::Args filter_args) {
return FaultInjectionFilter(filter_args);
absl::StatusOr<std::unique_ptr<FaultInjectionFilter>>
FaultInjectionFilter::Create(const ChannelArgs&,
ChannelFilter::Args filter_args) {
return std::make_unique<FaultInjectionFilter>(filter_args);
}
FaultInjectionFilter::FaultInjectionFilter(ChannelFilter::Args filter_args)
: index_(filter_args.instance_id()),
service_config_parser_index_(
FaultInjectionServiceConfigParser::ParserIndex()),
mu_(new Mutex) {}
FaultInjectionServiceConfigParser::ParserIndex()) {}
// Construct a promise for one call.
ArenaPromise<absl::Status> FaultInjectionFilter::Call::OnClientInitialMetadata(
@ -226,7 +227,7 @@ FaultInjectionFilter::MakeInjectionDecision(
bool delay_request = delay != Duration::Zero();
bool abort_request = abort_code != GRPC_STATUS_OK;
if (delay_request || abort_request) {
MutexLock lock(mu_.get());
MutexLock lock(&mu_);
if (delay_request) {
delay_request =
UnderFraction(&delay_rand_generator_, delay_percentage_numerator,

@ -45,9 +45,11 @@ class FaultInjectionFilter
public:
static const grpc_channel_filter kFilter;
static absl::StatusOr<FaultInjectionFilter> Create(
static absl::StatusOr<std::unique_ptr<FaultInjectionFilter>> Create(
const ChannelArgs& args, ChannelFilter::Args filter_args);
explicit FaultInjectionFilter(ChannelFilter::Args filter_args);
// Construct a promise for one call.
class Call {
public:
@ -61,8 +63,6 @@ class FaultInjectionFilter
};
private:
explicit FaultInjectionFilter(ChannelFilter::Args filter_args);
class InjectionDecision;
InjectionDecision MakeInjectionDecision(
const ClientMetadata& initial_metadata);
@ -70,7 +70,7 @@ class FaultInjectionFilter
// The relative index of instances of the same filter.
size_t index_;
const size_t service_config_parser_index_;
std::unique_ptr<Mutex> mu_;
Mutex mu_;
absl::InsecureBitGen abort_rand_generator_ ABSL_GUARDED_BY(mu_);
absl::InsecureBitGen delay_rand_generator_ ABSL_GUARDED_BY(mu_);
};

@ -27,6 +27,7 @@
#include <vector>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/str_join.h"
@ -136,16 +137,16 @@ HttpClientFilter::HttpClientFilter(HttpSchemeMetadata::ValueType scheme,
Slice user_agent,
bool test_only_use_put_requests)
: scheme_(scheme),
user_agent_(std::move(user_agent)),
test_only_use_put_requests_(test_only_use_put_requests) {}
test_only_use_put_requests_(test_only_use_put_requests),
user_agent_(std::move(user_agent)) {}
absl::StatusOr<HttpClientFilter> HttpClientFilter::Create(
absl::StatusOr<std::unique_ptr<HttpClientFilter>> HttpClientFilter::Create(
const ChannelArgs& args, ChannelFilter::Args) {
auto* transport = args.GetObject<Transport>();
if (transport == nullptr) {
return absl::InvalidArgumentError("HttpClientFilter needs a transport");
}
return HttpClientFilter(
return std::make_unique<HttpClientFilter>(
SchemeFromArgs(args),
UserAgentFromArgs(args, transport->GetTransportName()),
args.GetInt(GRPC_ARG_TEST_ONLY_USE_PUT_REQUESTS).value_or(false));

@ -35,9 +35,12 @@ class HttpClientFilter : public ImplementChannelFilter<HttpClientFilter> {
public:
static const grpc_channel_filter kFilter;
static absl::StatusOr<HttpClientFilter> Create(
static absl::StatusOr<std::unique_ptr<HttpClientFilter>> Create(
const ChannelArgs& args, ChannelFilter::Args filter_args);
HttpClientFilter(HttpSchemeMetadata::ValueType scheme, Slice user_agent,
bool test_only_use_put_requests);
class Call {
public:
void OnClientInitialMetadata(ClientMetadata& md, HttpClientFilter* filter);
@ -49,12 +52,9 @@ class HttpClientFilter : public ImplementChannelFilter<HttpClientFilter> {
};
private:
HttpClientFilter(HttpSchemeMetadata::ValueType scheme, Slice user_agent,
bool test_only_use_put_requests);
HttpSchemeMetadata::ValueType scheme_;
Slice user_agent_;
bool test_only_use_put_requests_;
Slice user_agent_;
};
// A test-only channel arg to allow testing gRPC Core server behavior on PUT

@ -43,8 +43,8 @@ const NoInterceptor ClientAuthorityFilter::Call::OnClientToServerMessage;
const NoInterceptor ClientAuthorityFilter::Call::OnServerToClientMessage;
const NoInterceptor ClientAuthorityFilter::Call::OnFinalize;
absl::StatusOr<ClientAuthorityFilter> ClientAuthorityFilter::Create(
const ChannelArgs& args, ChannelFilter::Args) {
absl::StatusOr<std::unique_ptr<ClientAuthorityFilter>>
ClientAuthorityFilter::Create(const ChannelArgs& args, ChannelFilter::Args) {
absl::optional<absl::string_view> default_authority =
args.GetString(GRPC_ARG_DEFAULT_AUTHORITY);
if (!default_authority.has_value()) {
@ -52,7 +52,8 @@ absl::StatusOr<ClientAuthorityFilter> ClientAuthorityFilter::Create(
"GRPC_ARG_DEFAULT_AUTHORITY string channel arg. not found. Note that "
"direct channels must explicitly specify a value for this argument.");
}
return ClientAuthorityFilter(Slice::FromCopiedString(*default_authority));
return std::make_unique<ClientAuthorityFilter>(
Slice::FromCopiedString(*default_authority));
}
void ClientAuthorityFilter::Call::OnClientInitialMetadata(

@ -39,8 +39,11 @@ class ClientAuthorityFilter final
public:
static const grpc_channel_filter kFilter;
static absl::StatusOr<ClientAuthorityFilter> Create(const ChannelArgs& args,
ChannelFilter::Args);
static absl::StatusOr<std::unique_ptr<ClientAuthorityFilter>> Create(
const ChannelArgs& args, ChannelFilter::Args);
explicit ClientAuthorityFilter(Slice default_authority)
: default_authority_(std::move(default_authority)) {}
class Call {
public:
@ -54,8 +57,6 @@ class ClientAuthorityFilter final
};
private:
explicit ClientAuthorityFilter(Slice default_authority)
: default_authority_(std::move(default_authority)) {}
Slice default_authority_;
};

@ -72,14 +72,14 @@ const grpc_channel_filter ServerCompressionFilter::kFilter =
kFilterExaminesInboundMessages |
kFilterExaminesOutboundMessages>("compression");
absl::StatusOr<ClientCompressionFilter> ClientCompressionFilter::Create(
const ChannelArgs& args, ChannelFilter::Args) {
return ClientCompressionFilter(args);
absl::StatusOr<std::unique_ptr<ClientCompressionFilter>>
ClientCompressionFilter::Create(const ChannelArgs& args, ChannelFilter::Args) {
return std::make_unique<ClientCompressionFilter>(args);
}
absl::StatusOr<ServerCompressionFilter> ServerCompressionFilter::Create(
const ChannelArgs& args, ChannelFilter::Args) {
return ServerCompressionFilter(args);
absl::StatusOr<std::unique_ptr<ServerCompressionFilter>>
ServerCompressionFilter::Create(const ChannelArgs& args, ChannelFilter::Args) {
return std::make_unique<ServerCompressionFilter>(args);
}
ChannelCompression::ChannelCompression(const ChannelArgs& args)

@ -110,9 +110,12 @@ class ClientCompressionFilter final
public:
static const grpc_channel_filter kFilter;
static absl::StatusOr<ClientCompressionFilter> Create(
static absl::StatusOr<std::unique_ptr<ClientCompressionFilter>> Create(
const ChannelArgs& args, ChannelFilter::Args filter_args);
explicit ClientCompressionFilter(const ChannelArgs& args)
: compression_engine_(args) {}
// Construct a promise for one call.
class Call {
public:
@ -135,9 +138,6 @@ class ClientCompressionFilter final
};
private:
explicit ClientCompressionFilter(const ChannelArgs& args)
: compression_engine_(args) {}
ChannelCompression compression_engine_;
};
@ -146,9 +146,12 @@ class ServerCompressionFilter final
public:
static const grpc_channel_filter kFilter;
static absl::StatusOr<ServerCompressionFilter> Create(
static absl::StatusOr<std::unique_ptr<ServerCompressionFilter>> Create(
const ChannelArgs& args, ChannelFilter::Args filter_args);
explicit ServerCompressionFilter(const ChannelArgs& args)
: compression_engine_(args) {}
// Construct a promise for one call.
class Call {
public:
@ -171,9 +174,6 @@ class ServerCompressionFilter final
};
private:
explicit ServerCompressionFilter(const ChannelArgs& args)
: compression_engine_(args) {}
ChannelCompression compression_engine_;
};

@ -152,9 +152,9 @@ void HttpServerFilter::Call::OnServerTrailingMetadata(ServerMetadata& md) {
FilterOutgoingMetadata(&md);
}
absl::StatusOr<HttpServerFilter> HttpServerFilter::Create(
absl::StatusOr<std::unique_ptr<HttpServerFilter>> HttpServerFilter::Create(
const ChannelArgs& args, ChannelFilter::Args) {
return HttpServerFilter(
return std::make_unique<HttpServerFilter>(
args.GetBool(GRPC_ARG_SURFACE_USER_AGENT).value_or(true),
args.GetBool(
GRPC_ARG_DO_NOT_USE_UNLESS_YOU_HAVE_PERMISSION_FROM_GRPC_TEAM_ALLOW_BROKEN_PUT_REQUESTS)

@ -36,9 +36,13 @@ class HttpServerFilter : public ImplementChannelFilter<HttpServerFilter> {
public:
static const grpc_channel_filter kFilter;
static absl::StatusOr<HttpServerFilter> Create(
static absl::StatusOr<std::unique_ptr<HttpServerFilter>> Create(
const ChannelArgs& args, ChannelFilter::Args filter_args);
HttpServerFilter(bool surface_user_agent, bool allow_put_requests)
: surface_user_agent_(surface_user_agent),
allow_put_requests_(allow_put_requests) {}
class Call {
public:
ServerMetadataHandle OnClientInitialMetadata(ClientMetadata& md,
@ -51,10 +55,6 @@ class HttpServerFilter : public ImplementChannelFilter<HttpServerFilter> {
};
private:
HttpServerFilter(bool surface_user_agent, bool allow_put_requests)
: surface_user_agent_(surface_user_agent),
allow_put_requests_(allow_put_requests) {}
bool surface_user_agent_;
bool allow_put_requests_;
};

@ -76,10 +76,11 @@ const NoInterceptor ServerLoadReportingFilter::Call::OnServerInitialMetadata;
const NoInterceptor ServerLoadReportingFilter::Call::OnClientToServerMessage;
const NoInterceptor ServerLoadReportingFilter::Call::OnServerToClientMessage;
absl::StatusOr<ServerLoadReportingFilter> ServerLoadReportingFilter::Create(
const ChannelArgs& channel_args, ChannelFilter::Args) {
absl::StatusOr<std::unique_ptr<ServerLoadReportingFilter>>
ServerLoadReportingFilter::Create(const ChannelArgs& channel_args,
ChannelFilter::Args) {
// Find and record the peer_identity.
ServerLoadReportingFilter filter;
auto filter = std::make_unique<ServerLoadReportingFilter>();
const auto* auth_context = channel_args.GetObject<grpc_auth_context>();
if (auth_context != nullptr &&
grpc_auth_context_peer_is_authenticated(auth_context)) {
@ -88,7 +89,7 @@ absl::StatusOr<ServerLoadReportingFilter> ServerLoadReportingFilter::Create(
const grpc_auth_property* auth_property =
grpc_auth_property_iterator_next(&auth_it);
if (auth_property != nullptr) {
filter.peer_identity_ =
filter->peer_identity_ =
std::string(auth_property->value, auth_property->value_length);
}
}

@ -39,7 +39,7 @@ class ServerLoadReportingFilter
public:
static const grpc_channel_filter kFilter;
static absl::StatusOr<ServerLoadReportingFilter> Create(
static absl::StatusOr<std::unique_ptr<ServerLoadReportingFilter>> Create(
const ChannelArgs& args, ChannelFilter::Args);
// Getters.

@ -342,21 +342,23 @@ class CallData {
} // namespace
absl::StatusOr<ClientLoggingFilter> ClientLoggingFilter::Create(
const ChannelArgs& args, ChannelFilter::Args /*filter_args*/) {
absl::StatusOr<std::unique_ptr<ClientLoggingFilter>>
ClientLoggingFilter::Create(const ChannelArgs& args,
ChannelFilter::Args /*filter_args*/) {
absl::optional<absl::string_view> default_authority =
args.GetString(GRPC_ARG_DEFAULT_AUTHORITY);
if (default_authority.has_value()) {
return ClientLoggingFilter(std::string(default_authority.value()));
return std::make_unique<ClientLoggingFilter>(
std::string(default_authority.value()));
}
absl::optional<std::string> server_uri =
args.GetOwnedString(GRPC_ARG_SERVER_URI);
if (server_uri.has_value()) {
return ClientLoggingFilter(
return std::make_unique<ClientLoggingFilter>(
CoreConfiguration::Get().resolver_registry().GetDefaultAuthority(
*server_uri));
}
return ClientLoggingFilter("");
return std::make_unique<ClientLoggingFilter>("");
}
// Construct a promise for one call.
@ -445,9 +447,10 @@ const grpc_channel_filter ClientLoggingFilter::kFilter =
kFilterExaminesInboundMessages |
kFilterExaminesOutboundMessages>("logging");
absl::StatusOr<ServerLoggingFilter> ServerLoggingFilter::Create(
const ChannelArgs& /*args*/, ChannelFilter::Args /*filter_args*/) {
return ServerLoggingFilter();
absl::StatusOr<std::unique_ptr<ServerLoggingFilter>>
ServerLoggingFilter::Create(const ChannelArgs& /*args*/,
ChannelFilter::Args /*filter_args*/) {
return std::make_unique<ServerLoggingFilter>();
}
// Construct a promise for one call.

@ -39,24 +39,25 @@ class ClientLoggingFilter final : public ChannelFilter {
public:
static const grpc_channel_filter kFilter;
static absl::StatusOr<ClientLoggingFilter> Create(
static absl::StatusOr<std::unique_ptr<ClientLoggingFilter>> Create(
const ChannelArgs& args, ChannelFilter::Args /*filter_args*/);
explicit ClientLoggingFilter(std::string default_authority)
: default_authority_(std::move(default_authority)) {}
// Construct a promise for one call.
ArenaPromise<ServerMetadataHandle> MakeCallPromise(
CallArgs call_args, NextPromiseFactory next_promise_factory) override;
private:
explicit ClientLoggingFilter(std::string default_authority)
: default_authority_(std::move(default_authority)) {}
std::string default_authority_;
const std::string default_authority_;
};
class ServerLoggingFilter final : public ChannelFilter {
public:
static const grpc_channel_filter kFilter;
static absl::StatusOr<ServerLoggingFilter> Create(
static absl::StatusOr<std::unique_ptr<ServerLoggingFilter>> Create(
const ChannelArgs& args, ChannelFilter::Args /*filter_args*/);
// Construct a promise for one call.

@ -25,7 +25,6 @@
#include "absl/strings/str_format.h"
#include <grpc/grpc.h>
#include <grpc/impl/channel_arg_names.h>
#include <grpc/status.h>
#include <grpc/support/log.h>
@ -142,19 +141,20 @@ const grpc_channel_filter ClientMessageSizeFilter::kFilter =
MakePromiseBasedFilter<ClientMessageSizeFilter, FilterEndpoint::kClient,
kFilterExaminesOutboundMessages |
kFilterExaminesInboundMessages>("message_size");
const grpc_channel_filter ServerMessageSizeFilter::kFilter =
MakePromiseBasedFilter<ServerMessageSizeFilter, FilterEndpoint::kServer,
kFilterExaminesOutboundMessages |
kFilterExaminesInboundMessages>("message_size");
absl::StatusOr<ClientMessageSizeFilter> ClientMessageSizeFilter::Create(
const ChannelArgs& args, ChannelFilter::Args) {
return ClientMessageSizeFilter(args);
absl::StatusOr<std::unique_ptr<ClientMessageSizeFilter>>
ClientMessageSizeFilter::Create(const ChannelArgs& args, ChannelFilter::Args) {
return std::make_unique<ClientMessageSizeFilter>(args);
}
absl::StatusOr<ServerMessageSizeFilter> ServerMessageSizeFilter::Create(
const ChannelArgs& args, ChannelFilter::Args) {
return ServerMessageSizeFilter(args);
absl::StatusOr<std::unique_ptr<ServerMessageSizeFilter>>
ServerMessageSizeFilter::Create(const ChannelArgs& args, ChannelFilter::Args) {
return std::make_unique<ServerMessageSizeFilter>(args);
}
namespace {

@ -91,9 +91,12 @@ class ServerMessageSizeFilter final
public:
static const grpc_channel_filter kFilter;
static absl::StatusOr<ServerMessageSizeFilter> Create(
static absl::StatusOr<std::unique_ptr<ServerMessageSizeFilter>> Create(
const ChannelArgs& args, ChannelFilter::Args filter_args);
explicit ServerMessageSizeFilter(const ChannelArgs& args)
: parsed_config_(MessageSizeParsedConfig::GetFromChannelArgs(args)) {}
class Call {
public:
static const NoInterceptor OnClientInitialMetadata;
@ -107,8 +110,6 @@ class ServerMessageSizeFilter final
};
private:
explicit ServerMessageSizeFilter(const ChannelArgs& args)
: parsed_config_(MessageSizeParsedConfig::GetFromChannelArgs(args)) {}
const MessageSizeParsedConfig parsed_config_;
};
@ -117,9 +118,12 @@ class ClientMessageSizeFilter final
public:
static const grpc_channel_filter kFilter;
static absl::StatusOr<ClientMessageSizeFilter> Create(
static absl::StatusOr<std::unique_ptr<ClientMessageSizeFilter>> Create(
const ChannelArgs& args, ChannelFilter::Args filter_args);
explicit ClientMessageSizeFilter(const ChannelArgs& args)
: parsed_config_(MessageSizeParsedConfig::GetFromChannelArgs(args)) {}
class Call {
public:
explicit Call(ClientMessageSizeFilter* filter);
@ -136,8 +140,6 @@ class ClientMessageSizeFilter final
};
private:
explicit ClientMessageSizeFilter(const ChannelArgs& args)
: parsed_config_(MessageSizeParsedConfig::GetFromChannelArgs(args)) {}
const size_t service_config_parser_index_{MessageSizeParser::ParserIndex()};
const MessageSizeParsedConfig parsed_config_;
};

@ -82,14 +82,21 @@ RbacFilter::RbacFilter(size_t index,
service_config_parser_index_(RbacServiceConfigParser::ParserIndex()),
per_channel_evaluate_args_(std::move(per_channel_evaluate_args)) {}
absl::StatusOr<RbacFilter> RbacFilter::Create(const ChannelArgs& args,
ChannelFilter::Args filter_args) {
absl::StatusOr<std::unique_ptr<RbacFilter>> RbacFilter::Create(
const ChannelArgs& args, ChannelFilter::Args filter_args) {
auto* auth_context = args.GetObject<grpc_auth_context>();
if (auth_context == nullptr) {
return GRPC_ERROR_CREATE("No auth context found");
}
return RbacFilter(filter_args.instance_id(),
EvaluateArgs::PerChannelArgs(auth_context, args));
auto* transport = args.GetObject<Transport>();
if (transport == nullptr) {
// This should never happen since the transport is always set on the server
// side.
return GRPC_ERROR_CREATE("No transport configured");
}
return std::make_unique<RbacFilter>(
filter_args.instance_id(),
EvaluateArgs::PerChannelArgs(auth_context, args));
}
void RbacFilterRegister(CoreConfiguration::Builder* builder) {

@ -42,8 +42,11 @@ class RbacFilter : public ImplementChannelFilter<RbacFilter> {
// and enforces the RBAC policy.
static const grpc_channel_filter kFilterVtable;
static absl::StatusOr<RbacFilter> Create(const ChannelArgs& args,
ChannelFilter::Args filter_args);
static absl::StatusOr<std::unique_ptr<RbacFilter>> Create(
const ChannelArgs& args, ChannelFilter::Args filter_args);
RbacFilter(size_t index,
EvaluateArgs::PerChannelArgs per_channel_evaluate_args);
class Call {
public:
@ -57,9 +60,6 @@ class RbacFilter : public ImplementChannelFilter<RbacFilter> {
};
private:
RbacFilter(size_t index,
EvaluateArgs::PerChannelArgs per_channel_evaluate_args);
// The index of this filter instance among instances of the same filter.
size_t index_;
// Assigned index for service config data from the parser.

@ -49,19 +49,22 @@ namespace grpc_core {
namespace {
class ServerConfigSelectorFilter final
: public ImplementChannelFilter<ServerConfigSelectorFilter> {
: public ImplementChannelFilter<ServerConfigSelectorFilter>,
public InternallyRefCounted<ServerConfigSelectorFilter> {
public:
~ServerConfigSelectorFilter() override;
explicit ServerConfigSelectorFilter(
RefCountedPtr<ServerConfigSelectorProvider>
server_config_selector_provider);
ServerConfigSelectorFilter(const ServerConfigSelectorFilter&) = delete;
ServerConfigSelectorFilter& operator=(const ServerConfigSelectorFilter&) =
delete;
ServerConfigSelectorFilter(ServerConfigSelectorFilter&&) = default;
ServerConfigSelectorFilter& operator=(ServerConfigSelectorFilter&&) = default;
static absl::StatusOr<ServerConfigSelectorFilter> Create(
static absl::StatusOr<OrphanablePtr<ServerConfigSelectorFilter>> Create(
const ChannelArgs& args, ChannelFilter::Args);
void Orphan() override;
class Call {
public:
absl::Status OnClientInitialMetadata(ClientMetadata& md,
@ -74,70 +77,66 @@ class ServerConfigSelectorFilter final
};
absl::StatusOr<RefCountedPtr<ServerConfigSelector>> config_selector() {
MutexLock lock(&state_->mu);
return state_->config_selector.value();
MutexLock lock(&mu_);
return config_selector_.value();
}
private:
struct State {
Mutex mu;
absl::optional<absl::StatusOr<RefCountedPtr<ServerConfigSelector>>>
config_selector ABSL_GUARDED_BY(mu);
};
class ServerConfigSelectorWatcher
: public ServerConfigSelectorProvider::ServerConfigSelectorWatcher {
public:
explicit ServerConfigSelectorWatcher(std::shared_ptr<State> state)
: state_(state) {}
explicit ServerConfigSelectorWatcher(
RefCountedPtr<ServerConfigSelectorFilter> filter)
: filter_(filter) {}
void OnServerConfigSelectorUpdate(
absl::StatusOr<RefCountedPtr<ServerConfigSelector>> update) override {
MutexLock lock(&state_->mu);
state_->config_selector = std::move(update);
MutexLock lock(&filter_->mu_);
filter_->config_selector_ = std::move(update);
}
private:
std::shared_ptr<State> state_;
RefCountedPtr<ServerConfigSelectorFilter> filter_;
};
explicit ServerConfigSelectorFilter(
RefCountedPtr<ServerConfigSelectorProvider>
server_config_selector_provider);
RefCountedPtr<ServerConfigSelectorProvider> server_config_selector_provider_;
std::shared_ptr<State> state_;
Mutex mu_;
absl::optional<absl::StatusOr<RefCountedPtr<ServerConfigSelector>>>
config_selector_ ABSL_GUARDED_BY(mu_);
};
absl::StatusOr<ServerConfigSelectorFilter> ServerConfigSelectorFilter::Create(
const ChannelArgs& args, ChannelFilter::Args) {
absl::StatusOr<OrphanablePtr<ServerConfigSelectorFilter>>
ServerConfigSelectorFilter::Create(const ChannelArgs& args,
ChannelFilter::Args) {
ServerConfigSelectorProvider* server_config_selector_provider =
args.GetObject<ServerConfigSelectorProvider>();
if (server_config_selector_provider == nullptr) {
return absl::UnknownError("No ServerConfigSelectorProvider object found");
}
return ServerConfigSelectorFilter(server_config_selector_provider->Ref());
return MakeOrphanable<ServerConfigSelectorFilter>(
server_config_selector_provider->Ref());
}
ServerConfigSelectorFilter::ServerConfigSelectorFilter(
RefCountedPtr<ServerConfigSelectorProvider> server_config_selector_provider)
: server_config_selector_provider_(
std::move(server_config_selector_provider)),
state_(std::make_shared<State>()) {
std::move(server_config_selector_provider)) {
GPR_ASSERT(server_config_selector_provider_ != nullptr);
auto server_config_selector_watcher =
std::make_unique<ServerConfigSelectorWatcher>(state_);
std::make_unique<ServerConfigSelectorWatcher>(Ref());
auto config_selector = server_config_selector_provider_->Watch(
std::move(server_config_selector_watcher));
MutexLock lock(&state_->mu);
MutexLock lock(&mu_);
// It's possible for the watcher to have already updated config_selector_
if (!state_->config_selector.has_value()) {
state_->config_selector = std::move(config_selector);
if (!config_selector_.has_value()) {
config_selector_ = std::move(config_selector);
}
}
ServerConfigSelectorFilter::~ServerConfigSelectorFilter() {
void ServerConfigSelectorFilter::Orphan() {
if (server_config_selector_provider_ != nullptr) {
server_config_selector_provider_->CancelWatch();
}
Unref();
}
absl::Status ServerConfigSelectorFilter::Call::OnClientInitialMetadata(

@ -72,9 +72,10 @@ const grpc_channel_filter StatefulSessionFilter::kFilter =
kFilterExaminesServerInitialMetadata>(
"stateful_session_filter");
absl::StatusOr<StatefulSessionFilter> StatefulSessionFilter::Create(
const ChannelArgs&, ChannelFilter::Args filter_args) {
return StatefulSessionFilter(filter_args);
absl::StatusOr<std::unique_ptr<StatefulSessionFilter>>
StatefulSessionFilter::Create(const ChannelArgs&,
ChannelFilter::Args filter_args) {
return std::make_unique<StatefulSessionFilter>(filter_args);
}
StatefulSessionFilter::StatefulSessionFilter(ChannelFilter::Args filter_args)

@ -74,9 +74,11 @@ class StatefulSessionFilter
public:
static const grpc_channel_filter kFilter;
static absl::StatusOr<StatefulSessionFilter> Create(
static absl::StatusOr<std::unique_ptr<StatefulSessionFilter>> Create(
const ChannelArgs& args, ChannelFilter::Args filter_args);
explicit StatefulSessionFilter(ChannelFilter::Args filter_args);
class Call {
public:
void OnClientInitialMetadata(ClientMetadata& md,
@ -97,7 +99,6 @@ class StatefulSessionFilter
};
private:
explicit StatefulSessionFilter(ChannelFilter::Args filter_args);
// The relative index of instances of the same filter.
const size_t index_;
// Index of the service config parser.

@ -106,7 +106,7 @@ BaseCallData::BaseCallData(
? arena_->New<ReceiveMessage>(this, make_recv_interceptor())
: nullptr),
event_engine_(
static_cast<ChannelFilter*>(elem->channel_data)
ChannelFilterFromElem(elem)
->hack_until_per_channel_stack_event_engines_land_get_event_engine()) {
}
@ -1572,7 +1572,7 @@ void ClientCallData::Cancel(grpc_error_handle error, Flusher* flusher) {
// metadata and return some trailing metadata.
void ClientCallData::StartPromise(Flusher* flusher) {
GPR_ASSERT(send_initial_state_ == SendInitialState::kQueued);
ChannelFilter* filter = static_cast<ChannelFilter*>(elem()->channel_data);
ChannelFilter* filter = promise_filter_detail::ChannelFilterFromElem(elem());
// Construct the promise.
PollContext ctx(this, flusher);
@ -2369,7 +2369,7 @@ void ServerCallData::RecvInitialMetadataReady(grpc_error_handle error) {
// Start the promise.
ScopedContext context(this);
// Construct the promise.
ChannelFilter* filter = static_cast<ChannelFilter*>(elem()->channel_data);
ChannelFilter* filter = promise_filter_detail::ChannelFilterFromElem(elem());
FakeActivity(this).Run([this, filter] {
promise_ = filter->MakeCallPromise(
CallArgs{WrapMetadata(recv_initial_metadata_),

@ -1841,6 +1841,15 @@ struct BaseCallDataMethods {
}
};
// The type of object returned by a filter's Create method.
template <typename T>
using CreatedType = typename decltype(T::Create(ChannelArgs(), {}))::value_type;
template <typename GrpcChannelOrCallElement>
inline ChannelFilter* ChannelFilterFromElem(GrpcChannelOrCallElement* elem) {
return *static_cast<ChannelFilter**>(elem->channel_data);
}
template <typename CallData, uint8_t kFlags>
struct CallDataFilterWithFlagsMethods {
static absl::Status InitCallElem(grpc_call_element* elem,
@ -1865,32 +1874,25 @@ struct ChannelFilterMethods {
static ArenaPromise<ServerMetadataHandle> MakeCallPromise(
grpc_channel_element* elem, CallArgs call_args,
NextPromiseFactory next_promise_factory) {
return static_cast<ChannelFilter*>(elem->channel_data)
->MakeCallPromise(std::move(call_args),
std::move(next_promise_factory));
return ChannelFilterFromElem(elem)->MakeCallPromise(
std::move(call_args), std::move(next_promise_factory));
}
static void StartTransportOp(grpc_channel_element* elem,
grpc_transport_op* op) {
if (!static_cast<ChannelFilter*>(elem->channel_data)
->StartTransportOp(op)) {
if (!ChannelFilterFromElem(elem)->StartTransportOp(op)) {
grpc_channel_next_op(elem, op);
}
}
static void PostInitChannelElem(grpc_channel_stack*,
grpc_channel_element* elem) {
static_cast<ChannelFilter*>(elem->channel_data)->PostInit();
}
static void DestroyChannelElem(grpc_channel_element* elem) {
static_cast<ChannelFilter*>(elem->channel_data)->~ChannelFilter();
ChannelFilterFromElem(elem)->PostInit();
}
static void GetChannelInfo(grpc_channel_element* elem,
const grpc_channel_info* info) {
if (!static_cast<ChannelFilter*>(elem->channel_data)
->GetChannelInfo(info)) {
if (!ChannelFilterFromElem(elem)->GetChannelInfo(info)) {
grpc_channel_next_get_info(elem, info);
}
}
@ -1904,15 +1906,16 @@ struct ChannelFilterWithFlagsMethods {
auto status = F::Create(args->channel_args,
ChannelFilter::Args(args->channel_stack, elem));
if (!status.ok()) {
static_assert(
sizeof(promise_filter_detail::InvalidChannelFilter) <= sizeof(F),
"InvalidChannelFilter must fit in F");
new (elem->channel_data) promise_filter_detail::InvalidChannelFilter();
new (elem->channel_data) F*(nullptr);
return absl_status_to_grpc_error(status.status());
}
new (elem->channel_data) F(std::move(*status));
new (elem->channel_data) F*(status->release());
return absl::OkStatus();
}
static void DestroyChannelElem(grpc_channel_element* elem) {
CreatedType<F> channel_elem(DownCast<F*>(ChannelFilterFromElem(elem)));
}
};
} // namespace promise_filter_detail
@ -1958,7 +1961,8 @@ MakePromiseBasedFilter(const char* name) {
// post_init_channel_elem
promise_filter_detail::ChannelFilterMethods::PostInitChannelElem,
// destroy_channel_elem
promise_filter_detail::ChannelFilterMethods::DestroyChannelElem,
promise_filter_detail::ChannelFilterWithFlagsMethods<
F, kFlags>::DestroyChannelElem,
// get_channel_info
promise_filter_detail::ChannelFilterMethods::GetChannelInfo,
// name
@ -2004,7 +2008,8 @@ MakePromiseBasedFilter(const char* name) {
// post_init_channel_elem
promise_filter_detail::ChannelFilterMethods::PostInitChannelElem,
// destroy_channel_elem
promise_filter_detail::ChannelFilterMethods::DestroyChannelElem,
promise_filter_detail::ChannelFilterWithFlagsMethods<
F, kFlags>::DestroyChannelElem,
// get_channel_info
promise_filter_detail::ChannelFilterMethods::GetChannelInfo,
// name
@ -2046,7 +2051,8 @@ MakePromiseBasedFilter(const char* name) {
// post_init_channel_elem
promise_filter_detail::ChannelFilterMethods::PostInitChannelElem,
// destroy_channel_elem
promise_filter_detail::ChannelFilterMethods::DestroyChannelElem,
promise_filter_detail::ChannelFilterWithFlagsMethods<
F, kFlags>::DestroyChannelElem,
// get_channel_info
promise_filter_detail::ChannelFilterMethods::GetChannelInfo,
// name

@ -17,6 +17,7 @@
#include "src/core/lib/channel/server_call_tracer_filter.h"
#include <functional>
#include <memory>
#include <utility>
#include "absl/status/status.h"
@ -49,7 +50,7 @@ class ServerCallTracerFilter
public:
static const grpc_channel_filter kFilter;
static absl::StatusOr<ServerCallTracerFilter> Create(
static absl::StatusOr<std::unique_ptr<ServerCallTracerFilter>> Create(
const ChannelArgs& /*args*/, ChannelFilter::Args /*filter_args*/);
class Call {
@ -98,9 +99,10 @@ const grpc_channel_filter ServerCallTracerFilter::kFilter =
kFilterExaminesServerInitialMetadata>(
"server_call_tracer");
absl::StatusOr<ServerCallTracerFilter> ServerCallTracerFilter::Create(
const ChannelArgs& /*args*/, ChannelFilter::Args /*filter_args*/) {
return ServerCallTracerFilter();
absl::StatusOr<std::unique_ptr<ServerCallTracerFilter>>
ServerCallTracerFilter::Create(const ChannelArgs& /*args*/,
ChannelFilter::Args /*filter_args*/) {
return std::make_unique<ServerCallTracerFilter>();
}
} // namespace

@ -51,14 +51,14 @@ GrpcServerAuthzFilter::GrpcServerAuthzFilter(
per_channel_evaluate_args_(auth_context_.get(), args),
provider_(std::move(provider)) {}
absl::StatusOr<GrpcServerAuthzFilter> GrpcServerAuthzFilter::Create(
const ChannelArgs& args, ChannelFilter::Args) {
absl::StatusOr<std::unique_ptr<GrpcServerAuthzFilter>>
GrpcServerAuthzFilter::Create(const ChannelArgs& args, ChannelFilter::Args) {
auto* auth_context = args.GetObject<grpc_auth_context>();
auto* provider = args.GetObject<grpc_authorization_policy_provider>();
if (provider == nullptr) {
return absl::InvalidArgumentError("Failed to get authorization provider.");
}
return GrpcServerAuthzFilter(
return std::make_unique<GrpcServerAuthzFilter>(
auth_context != nullptr ? auth_context->Ref() : nullptr, args,
provider->Ref());
}

@ -37,8 +37,12 @@ class GrpcServerAuthzFilter final
public:
static const grpc_channel_filter kFilter;
static absl::StatusOr<GrpcServerAuthzFilter> Create(const ChannelArgs& args,
ChannelFilter::Args);
static absl::StatusOr<std::unique_ptr<GrpcServerAuthzFilter>> Create(
const ChannelArgs& args, ChannelFilter::Args);
GrpcServerAuthzFilter(
RefCountedPtr<grpc_auth_context> auth_context, const ChannelArgs& args,
RefCountedPtr<grpc_authorization_policy_provider> provider);
class Call {
public:
@ -52,10 +56,6 @@ class GrpcServerAuthzFilter final
};
private:
GrpcServerAuthzFilter(
RefCountedPtr<grpc_auth_context> auth_context, const ChannelArgs& args,
RefCountedPtr<grpc_authorization_policy_provider> provider);
bool IsAuthorized(ClientMetadata& initial_metadata);
RefCountedPtr<grpc_auth_context> auth_context_;

@ -42,18 +42,18 @@ class ClientAuthFilter final : public ChannelFilter {
public:
static const grpc_channel_filter kFilter;
static absl::StatusOr<ClientAuthFilter> Create(const ChannelArgs& args,
ChannelFilter::Args);
ClientAuthFilter(
RefCountedPtr<grpc_channel_security_connector> security_connector,
RefCountedPtr<grpc_auth_context> auth_context);
static absl::StatusOr<std::unique_ptr<ClientAuthFilter>> Create(
const ChannelArgs& args, ChannelFilter::Args);
// Construct a promise for one call.
ArenaPromise<ServerMetadataHandle> MakeCallPromise(
CallArgs call_args, NextPromiseFactory next_promise_factory) override;
private:
ClientAuthFilter(
RefCountedPtr<grpc_channel_security_connector> security_connector,
RefCountedPtr<grpc_auth_context> auth_context);
ArenaPromise<absl::StatusOr<CallArgs>> GetCallCredsMetadata(
CallArgs call_args);
@ -63,9 +63,6 @@ class ClientAuthFilter final : public ChannelFilter {
class ServerAuthFilter final : public ImplementChannelFilter<ServerAuthFilter> {
private:
ServerAuthFilter(RefCountedPtr<grpc_server_credentials> server_credentials,
RefCountedPtr<grpc_auth_context> auth_context);
class RunApplicationCode {
public:
RunApplicationCode(ServerAuthFilter* filter, ClientMetadata& metadata);
@ -98,8 +95,11 @@ class ServerAuthFilter final : public ImplementChannelFilter<ServerAuthFilter> {
public:
static const grpc_channel_filter kFilter;
static absl::StatusOr<ServerAuthFilter> Create(const ChannelArgs& args,
ChannelFilter::Args);
ServerAuthFilter(RefCountedPtr<grpc_server_credentials> server_credentials,
RefCountedPtr<grpc_auth_context> auth_context);
static absl::StatusOr<std::unique_ptr<ServerAuthFilter>> Create(
const ChannelArgs& args, ChannelFilter::Args);
class Call {
public:

@ -203,7 +203,7 @@ ArenaPromise<ServerMetadataHandle> ClientAuthFilter::MakeCallPromise(
next_promise_factory);
}
absl::StatusOr<ClientAuthFilter> ClientAuthFilter::Create(
absl::StatusOr<std::unique_ptr<ClientAuthFilter>> ClientAuthFilter::Create(
const ChannelArgs& args, ChannelFilter::Args) {
auto* sc = args.GetObject<grpc_security_connector>();
if (sc == nullptr) {
@ -215,8 +215,9 @@ absl::StatusOr<ClientAuthFilter> ClientAuthFilter::Create(
return absl::InvalidArgumentError(
"Auth context missing from client auth filter args");
}
return ClientAuthFilter(sc->RefAsSubclass<grpc_channel_security_connector>(),
auth_context->Ref());
return std::make_unique<ClientAuthFilter>(
sc->RefAsSubclass<grpc_channel_security_connector>(),
auth_context->Ref());
}
const grpc_channel_filter ClientAuthFilter::kFilter =

@ -212,12 +212,13 @@ ServerAuthFilter::ServerAuthFilter(
RefCountedPtr<grpc_auth_context> auth_context)
: server_credentials_(server_credentials), auth_context_(auth_context) {}
absl::StatusOr<ServerAuthFilter> ServerAuthFilter::Create(
absl::StatusOr<std::unique_ptr<ServerAuthFilter>> ServerAuthFilter::Create(
const ChannelArgs& args, ChannelFilter::Args) {
auto auth_context = args.GetObjectRef<grpc_auth_context>();
GPR_ASSERT(auth_context != nullptr);
auto creds = args.GetObjectRef<grpc_server_credentials>();
return ServerAuthFilter(std::move(creds), std::move(auth_context));
return std::make_unique<ServerAuthFilter>(std::move(creds),
std::move(auth_context));
}
} // namespace grpc_core

@ -285,6 +285,11 @@ class ChannelInit {
grpc_channel_stack_type type, const ChannelArgs& args) const;
private:
// The type of object returned by a filter's Create method.
template <typename T>
using CreatedType =
typename decltype(T::Create(ChannelArgs(), {}))::value_type;
struct Filter {
Filter(const grpc_channel_filter* filter, const ChannelFilterVtable* vtable,
std::vector<InclusionPredicate> predicates, bool skip_v3,
@ -328,17 +333,17 @@ class ChannelInit {
template <typename T>
const ChannelInit::ChannelFilterVtable
ChannelInit::VtableForType<T, absl::void_t<typename T::Call>>::kVtable = {
sizeof(T), alignof(T),
sizeof(CreatedType<T>), alignof(CreatedType<T>),
[](void* data, const ChannelArgs& args) -> absl::Status {
// TODO(ctiller): fill in ChannelFilter::Args (2nd arg)
absl::StatusOr<T> r = T::Create(args, {});
absl::StatusOr<CreatedType<T>> r = T::Create(args, {});
if (!r.ok()) return r.status();
new (data) T(std::move(*r));
new (data) CreatedType<T>(std::move(*r));
return absl::OkStatus();
},
[](void* data) { static_cast<T*>(data)->~T(); },
[](void* data) { Destruct(static_cast<CreatedType<T>*>(data)); },
[](void* data, CallFilters::StackBuilder& builder) {
builder.Add(static_cast<T*>(data));
builder.Add(static_cast<CreatedType<T>*>(data)->get());
}};
} // namespace grpc_core

@ -59,17 +59,15 @@ const grpc_channel_filter LameClientFilter::kFilter =
MakePromiseBasedFilter<LameClientFilter, FilterEndpoint::kClient,
kFilterIsLast>("lame-client");
absl::StatusOr<LameClientFilter> LameClientFilter::Create(
absl::StatusOr<std::unique_ptr<LameClientFilter>> LameClientFilter::Create(
const ChannelArgs& args, ChannelFilter::Args) {
return LameClientFilter(
return std::make_unique<LameClientFilter>(
*args.GetPointer<absl::Status>(GRPC_ARG_LAME_FILTER_ERROR));
}
LameClientFilter::LameClientFilter(absl::Status error)
: error_(std::move(error)), state_(std::make_unique<State>()) {}
LameClientFilter::State::State()
: state_tracker("lame_client", GRPC_CHANNEL_SHUTDOWN) {}
: error_(std::move(error)),
state_tracker_("lame_client", GRPC_CHANNEL_SHUTDOWN) {}
ArenaPromise<ServerMetadataHandle> LameClientFilter::MakeCallPromise(
CallArgs args, NextPromiseFactory) {
@ -92,13 +90,13 @@ bool LameClientFilter::GetChannelInfo(const grpc_channel_info*) { return true; }
bool LameClientFilter::StartTransportOp(grpc_transport_op* op) {
{
MutexLock lock(&state_->mu);
MutexLock lock(&mu_);
if (op->start_connectivity_watch != nullptr) {
state_->state_tracker.AddWatcher(op->start_connectivity_watch_state,
std::move(op->start_connectivity_watch));
state_tracker_.AddWatcher(op->start_connectivity_watch_state,
std::move(op->start_connectivity_watch));
}
if (op->stop_connectivity_watch != nullptr) {
state_->state_tracker.RemoveWatcher(op->stop_connectivity_watch);
state_tracker_.RemoveWatcher(op->stop_connectivity_watch);
}
}
if (op->send_ping.on_initiate != nullptr) {

@ -47,7 +47,9 @@ class LameClientFilter : public ChannelFilter {
public:
static const grpc_channel_filter kFilter;
static absl::StatusOr<LameClientFilter> Create(
explicit LameClientFilter(absl::Status error);
static absl::StatusOr<std::unique_ptr<LameClientFilter>> Create(
const ChannelArgs& args, ChannelFilter::Args filter_args);
ArenaPromise<ServerMetadataHandle> MakeCallPromise(
CallArgs call_args, NextPromiseFactory next_promise_factory) override;
@ -55,15 +57,9 @@ class LameClientFilter : public ChannelFilter {
bool GetChannelInfo(const grpc_channel_info*) override;
private:
explicit LameClientFilter(absl::Status error);
absl::Status error_;
struct State {
State();
Mutex mu;
ConnectivityStateTracker state_tracker ABSL_GUARDED_BY(mu);
};
std::unique_ptr<State> state_;
Mutex mu_;
ConnectivityStateTracker state_tracker_ ABSL_GUARDED_BY(mu_);
};
extern const grpc_arg_pointer_vtable kLameFilterErrorArgVtable;

@ -16,8 +16,6 @@
//
//
#include <grpc/support/port_platform.h>
#include "src/core/load_balancing/grpclb/client_load_reporting_filter.h"
#include <functional>
@ -27,7 +25,8 @@
#include "absl/types/optional.h"
#include "src/core/load_balancing/grpclb/grpclb_client_stats.h"
#include <grpc/support/port_platform.h>
#include "src/core/lib/channel/channel_stack.h"
#include "src/core/lib/gprpp/ref_counted_ptr.h"
#include "src/core/lib/promise/context.h"
@ -36,48 +35,47 @@
#include "src/core/lib/resource_quota/arena.h"
#include "src/core/lib/transport/metadata_batch.h"
#include "src/core/lib/transport/transport.h"
#include "src/core/load_balancing/grpclb/grpclb_client_stats.h"
namespace grpc_core {
const NoInterceptor ClientLoadReportingFilter::Call::OnServerToClientMessage;
const NoInterceptor ClientLoadReportingFilter::Call::OnClientToServerMessage;
const NoInterceptor ClientLoadReportingFilter::Call::OnFinalize;
const grpc_channel_filter ClientLoadReportingFilter::kFilter =
MakePromiseBasedFilter<ClientLoadReportingFilter, FilterEndpoint::kClient,
kFilterExaminesServerInitialMetadata>(
"client_load_reporting");
absl::StatusOr<ClientLoadReportingFilter> ClientLoadReportingFilter::Create(
const ChannelArgs&, ChannelFilter::Args) {
return ClientLoadReportingFilter();
absl::StatusOr<std::unique_ptr<ClientLoadReportingFilter>>
ClientLoadReportingFilter::Create(const ChannelArgs&, ChannelFilter::Args) {
return std::make_unique<ClientLoadReportingFilter>();
}
ArenaPromise<ServerMetadataHandle> ClientLoadReportingFilter::MakeCallPromise(
CallArgs call_args, NextPromiseFactory next_promise_factory) {
// Stats object to update.
RefCountedPtr<GrpcLbClientStats> client_stats;
void ClientLoadReportingFilter::Call::OnClientInitialMetadata(
ClientMetadata& client_initial_metadata) {
// Handle client initial metadata.
// Grab client stats object from metadata.
auto client_stats_md =
call_args.client_initial_metadata->Take(GrpcLbClientStatsMetadata());
client_initial_metadata.Take(GrpcLbClientStatsMetadata());
if (client_stats_md.has_value()) {
client_stats.reset(*client_stats_md);
client_stats_.reset(*client_stats_md);
}
}
auto* saw_initial_metadata = GetContext<Arena>()->New<bool>(false);
call_args.server_initial_metadata->InterceptAndMap(
[saw_initial_metadata](ServerMetadataHandle md) {
*saw_initial_metadata = true;
return md;
});
void ClientLoadReportingFilter::Call::OnServerInitialMetadata(ServerMetadata&) {
saw_initial_metadata_ = true;
}
return Map(next_promise_factory(std::move(call_args)),
[saw_initial_metadata, client_stats = std::move(client_stats)](
ServerMetadataHandle trailing_metadata) {
if (client_stats != nullptr) {
client_stats->AddCallFinished(
trailing_metadata->get(GrpcStreamNetworkState()) ==
GrpcStreamNetworkState::kNotSentOnWire,
*saw_initial_metadata);
}
return trailing_metadata;
});
void ClientLoadReportingFilter::Call::OnServerTrailingMetadata(
ServerMetadata& server_trailing_metadata) {
if (client_stats_ != nullptr) {
client_stats_->AddCallFinished(
server_trailing_metadata.get(GrpcStreamNetworkState()) ==
GrpcStreamNetworkState::kNotSentOnWire,
saw_initial_metadata_);
}
}
} // namespace grpc_core

@ -19,10 +19,10 @@
#ifndef GRPC_SRC_CORE_LOAD_BALANCING_GRPCLB_CLIENT_LOAD_REPORTING_FILTER_H
#define GRPC_SRC_CORE_LOAD_BALANCING_GRPCLB_CLIENT_LOAD_REPORTING_FILTER_H
#include <grpc/support/port_platform.h>
#include "absl/status/statusor.h"
#include <grpc/support/port_platform.h>
#include "src/core/lib/channel/channel_args.h"
#include "src/core/lib/channel/channel_fwd.h"
#include "src/core/lib/channel/promise_based_filter.h"
@ -31,16 +31,27 @@
namespace grpc_core {
class ClientLoadReportingFilter final : public ChannelFilter {
class ClientLoadReportingFilter final
: public ImplementChannelFilter<ClientLoadReportingFilter> {
public:
static const grpc_channel_filter kFilter;
static absl::StatusOr<ClientLoadReportingFilter> Create(
class Call {
public:
void OnClientInitialMetadata(ClientMetadata& client_initial_metadata);
void OnServerInitialMetadata(ServerMetadata& server_initial_metadata);
void OnServerTrailingMetadata(ServerMetadata& server_trailing_metadata);
static const NoInterceptor OnServerToClientMessage;
static const NoInterceptor OnClientToServerMessage;
static const NoInterceptor OnFinalize;
private:
RefCountedPtr<GrpcLbClientStats> client_stats_;
bool saw_initial_metadata_ = false;
};
static absl::StatusOr<std::unique_ptr<ClientLoadReportingFilter>> Create(
const ChannelArgs& args, ChannelFilter::Args filter_args);
// Construct a promise for one call.
ArenaPromise<ServerMetadataHandle> MakeCallPromise(
CallArgs call_args, NextPromiseFactory next_promise_factory) override;
};
} // namespace grpc_core

@ -317,9 +317,10 @@ class XdsResolver final : public Resolver {
public:
const static grpc_channel_filter kFilter;
static absl::StatusOr<ClusterSelectionFilter> Create(
const ChannelArgs& /* unused */, ChannelFilter::Args filter_args) {
return ClusterSelectionFilter(filter_args);
static absl::StatusOr<std::unique_ptr<ClusterSelectionFilter>> Create(
const ChannelArgs& /* unused */,
ChannelFilter::Args /* filter_args */) {
return std::make_unique<ClusterSelectionFilter>();
}
// Construct a promise for one call.
@ -332,12 +333,6 @@ class XdsResolver final : public Resolver {
static const NoInterceptor OnServerToClientMessage;
static const NoInterceptor OnFinalize;
};
private:
explicit ClusterSelectionFilter(ChannelFilter::Args filter_args)
: filter_args_(filter_args) {}
ChannelFilter::Args filter_args_;
};
RefCountedPtr<ClusterRef> GetOrCreateClusterRef(

@ -17,8 +17,6 @@
// This filter reads GRPC_ARG_SERVICE_CONFIG and populates ServiceConfigCallData
// in the call context per call for direct channels.
#include <grpc/support/port_platform.h>
#include <functional>
#include <memory>
#include <string>
@ -30,6 +28,7 @@
#include <grpc/impl/channel_arg_names.h>
#include <grpc/support/log.h>
#include <grpc/support/port_platform.h>
#include "src/core/ext/filters/message_size/message_size_filter.h"
#include "src/core/lib/channel/channel_args.h"
@ -42,13 +41,13 @@
#include "src/core/lib/promise/arena_promise.h"
#include "src/core/lib/promise/context.h"
#include "src/core/lib/resource_quota/arena.h"
#include "src/core/lib/surface/channel_stack_type.h"
#include "src/core/lib/transport/metadata_batch.h"
#include "src/core/lib/transport/transport.h"
#include "src/core/service_config/service_config.h"
#include "src/core/service_config/service_config_call_data.h"
#include "src/core/service_config/service_config_impl.h"
#include "src/core/service_config/service_config_parser.h"
#include "src/core/lib/surface/channel_stack_type.h"
#include "src/core/lib/transport/metadata_batch.h"
#include "src/core/lib/transport/transport.h"
namespace grpc_core {
@ -59,9 +58,9 @@ class ServiceConfigChannelArgFilter final
public:
static const grpc_channel_filter kFilter;
static absl::StatusOr<ServiceConfigChannelArgFilter> Create(
static absl::StatusOr<std::unique_ptr<ServiceConfigChannelArgFilter>> Create(
const ChannelArgs& args, ChannelFilter::Args) {
return ServiceConfigChannelArgFilter(args);
return std::make_unique<ServiceConfigChannelArgFilter>(args);
}
explicit ServiceConfigChannelArgFilter(const ChannelArgs& args) {

@ -84,11 +84,13 @@ const grpc_channel_filter OpenCensusClientFilter::kFilter =
grpc_core::FilterEndpoint::kClient, 0>(
"opencensus_client");
absl::StatusOr<OpenCensusClientFilter> OpenCensusClientFilter::Create(
const grpc_core::ChannelArgs& args, ChannelFilter::Args /*filter_args*/) {
absl::StatusOr<std::unique_ptr<OpenCensusClientFilter>>
OpenCensusClientFilter::Create(const grpc_core::ChannelArgs& args,
ChannelFilter::Args /*filter_args*/) {
bool observability_enabled =
args.GetInt(GRPC_ARG_ENABLE_OBSERVABILITY).value_or(true);
return OpenCensusClientFilter(/*tracing_enabled=*/observability_enabled);
return std::make_unique<OpenCensusClientFilter>(
/*tracing_enabled=*/observability_enabled);
}
grpc_core::ArenaPromise<grpc_core::ServerMetadataHandle>

@ -38,16 +38,17 @@ class OpenCensusClientFilter : public grpc_core::ChannelFilter {
public:
static const grpc_channel_filter kFilter;
static absl::StatusOr<OpenCensusClientFilter> Create(
static absl::StatusOr<std::unique_ptr<OpenCensusClientFilter>> Create(
const grpc_core::ChannelArgs& args, ChannelFilter::Args /*filter_args*/);
explicit OpenCensusClientFilter(bool tracing_enabled)
: tracing_enabled_(tracing_enabled) {}
grpc_core::ArenaPromise<grpc_core::ServerMetadataHandle> MakeCallPromise(
grpc_core::CallArgs call_args,
grpc_core::NextPromiseFactory next_promise_factory) override;
private:
explicit OpenCensusClientFilter(bool tracing_enabled)
: tracing_enabled_(tracing_enabled) {}
bool tracing_enabled_ = true;
};

@ -231,7 +231,7 @@ class FilterTest : public FilterTestBase {
absl::StatusOr<Channel> MakeChannel(const ChannelArgs& args) {
auto filter = Filter::Create(args, ChannelFilter::Args());
if (!filter.ok()) return filter.status();
return Channel(std::make_unique<Filter>(std::move(*filter)), this);
return Channel(std::move(*filter), this);
}
};

@ -49,9 +49,9 @@ class NoOpFilter final : public ChannelFilter {
return next(std::move(args));
}
static absl::StatusOr<NoOpFilter> Create(const ChannelArgs&,
ChannelFilter::Args) {
return NoOpFilter();
static absl::StatusOr<std::unique_ptr<NoOpFilter>> Create(
const ChannelArgs&, ChannelFilter::Args) {
return std::make_unique<NoOpFilter>();
}
};
using NoOpFilterTest = FilterTest<NoOpFilter>;
@ -70,9 +70,9 @@ class DelayStartFilter final : public ChannelFilter {
next);
}
static absl::StatusOr<DelayStartFilter> Create(const ChannelArgs&,
ChannelFilter::Args) {
return DelayStartFilter();
static absl::StatusOr<std::unique_ptr<DelayStartFilter>> Create(
const ChannelArgs&, ChannelFilter::Args) {
return std::make_unique<DelayStartFilter>();
}
};
using DelayStartFilterTest = FilterTest<DelayStartFilter>;
@ -86,9 +86,9 @@ class AddClientInitialMetadataFilter final : public ChannelFilter {
return next(std::move(args));
}
static absl::StatusOr<AddClientInitialMetadataFilter> Create(
static absl::StatusOr<std::unique_ptr<AddClientInitialMetadataFilter>> Create(
const ChannelArgs&, ChannelFilter::Args) {
return AddClientInitialMetadataFilter();
return absl::make_unique<AddClientInitialMetadataFilter>();
}
};
using AddClientInitialMetadataFilterTest =
@ -104,9 +104,9 @@ class AddServerTrailingMetadataFilter final : public ChannelFilter {
});
}
static absl::StatusOr<AddServerTrailingMetadataFilter> Create(
const ChannelArgs&, ChannelFilter::Args) {
return AddServerTrailingMetadataFilter();
static absl::StatusOr<std::unique_ptr<AddServerTrailingMetadataFilter>>
Create(const ChannelArgs&, ChannelFilter::Args) {
return absl::make_unique<AddServerTrailingMetadataFilter>();
}
};
using AddServerTrailingMetadataFilterTest =
@ -122,10 +122,9 @@ class AddServerInitialMetadataFilter final : public ChannelFilter {
});
return next(std::move(args));
}
static absl::StatusOr<AddServerInitialMetadataFilter> Create(
static absl::StatusOr<std::unique_ptr<AddServerInitialMetadataFilter>> Create(
const ChannelArgs&, ChannelFilter::Args) {
return AddServerInitialMetadataFilter();
return absl::make_unique<AddServerInitialMetadataFilter>();
}
};
using AddServerInitialMetadataFilterTest =

@ -15,6 +15,7 @@
#include "src/core/lib/surface/channel_init.h"
#include <map>
#include <memory>
#include <string>
#include "absl/strings/string_view.h"
@ -206,9 +207,10 @@ class TestFilter1 {
public:
explicit TestFilter1(int* p) : p_(p) {}
static absl::StatusOr<TestFilter1> Create(const ChannelArgs& args, Empty) {
static absl::StatusOr<std::unique_ptr<TestFilter1>> Create(
const ChannelArgs& args, Empty) {
EXPECT_EQ(args.GetInt("foo"), 1);
return TestFilter1(args.GetPointer<int>("p"));
return std::make_unique<TestFilter1>(args.GetPointer<int>("p"));
}
static const grpc_channel_filter kFilter;

@ -22,15 +22,16 @@ class FakeStatsClientFilter : public ChannelFilter {
public:
static const grpc_channel_filter kFilter;
static absl::StatusOr<FakeStatsClientFilter> Create(
explicit FakeStatsClientFilter(
FakeClientCallTracerFactory* fake_client_call_tracer_factory);
static absl::StatusOr<std::unique_ptr<FakeStatsClientFilter>> Create(
const ChannelArgs& /*args*/, ChannelFilter::Args /*filter_args*/);
ArenaPromise<ServerMetadataHandle> MakeCallPromise(
CallArgs call_args, NextPromiseFactory next_promise_factory) override;
private:
explicit FakeStatsClientFilter(
FakeClientCallTracerFactory* fake_client_call_tracer_factory);
FakeClientCallTracerFactory* const fake_client_call_tracer_factory_;
};
@ -38,13 +39,15 @@ const grpc_channel_filter FakeStatsClientFilter::kFilter =
MakePromiseBasedFilter<FakeStatsClientFilter, FilterEndpoint::kClient>(
"fake_stats_client");
absl::StatusOr<FakeStatsClientFilter> FakeStatsClientFilter::Create(
const ChannelArgs& args, ChannelFilter::Args /*filter_args*/) {
absl::StatusOr<std::unique_ptr<FakeStatsClientFilter>>
FakeStatsClientFilter::Create(const ChannelArgs& args,
ChannelFilter::Args /*filter_args*/) {
auto* fake_client_call_tracer_factory =
args.GetPointer<FakeClientCallTracerFactory>(
GRPC_ARG_INJECT_FAKE_CLIENT_CALL_TRACER_FACTORY);
GPR_ASSERT(fake_client_call_tracer_factory != nullptr);
return FakeStatsClientFilter(fake_client_call_tracer_factory);
return std::make_unique<FakeStatsClientFilter>(
fake_client_call_tracer_factory);
}
ArenaPromise<ServerMetadataHandle> FakeStatsClientFilter::MakeCallPromise(

@ -50,9 +50,15 @@ class AddLabelsFilter : public grpc_core::ChannelFilter {
public:
static const grpc_channel_filter kFilter;
static absl::StatusOr<AddLabelsFilter> Create(
explicit AddLabelsFilter(
std::map<grpc_core::ClientCallTracer::CallAttemptTracer::OptionalLabelKey,
grpc_core::RefCountedStringValue>
labels_to_inject)
: labels_to_inject_(std::move(labels_to_inject)) {}
static absl::StatusOr<std::unique_ptr<AddLabelsFilter>> Create(
const grpc_core::ChannelArgs& args, ChannelFilter::Args /*filter_args*/) {
return AddLabelsFilter(
return absl::make_unique<AddLabelsFilter>(
*args.GetPointer<std::map<
grpc_core::ClientCallTracer::CallAttemptTracer::OptionalLabelKey,
grpc_core::RefCountedStringValue>>(GRPC_ARG_LABELS_TO_INJECT));
@ -73,12 +79,6 @@ class AddLabelsFilter : public grpc_core::ChannelFilter {
}
private:
explicit AddLabelsFilter(
std::map<grpc_core::ClientCallTracer::CallAttemptTracer::OptionalLabelKey,
grpc_core::RefCountedStringValue>
labels_to_inject)
: labels_to_inject_(std::move(labels_to_inject)) {}
const std::map<
grpc_core::ClientCallTracer::CallAttemptTracer::OptionalLabelKey,
grpc_core::RefCountedStringValue>

Loading…
Cancel
Save