From 8b4a8ebdf96ac7771b13a3e50b4974532d787d4c Mon Sep 17 00:00:00 2001 From: Nana Pang Date: Thu, 15 Aug 2024 14:47:14 -0700 Subject: [PATCH] [C++ BUILD] Internal changes for base context propagation. PiperOrigin-RevId: 663460841 --- BUILD | 21 +++++++ CMakeLists.txt | 13 +++++ build_autogenerated.yaml | 13 +++++ gRPC-C++.podspec | 2 + include/grpcpp/support/callback_common.h | 26 ++++++++- include/grpcpp/support/global_callback_hook.h | 58 +++++++++++++++++++ src/cpp/client/global_callback_hook.cc | 36 ++++++++++++ tools/doxygen/Doxyfile.c++ | 1 + tools/doxygen/Doxyfile.c++.internal | 2 + 9 files changed, 170 insertions(+), 2 deletions(-) create mode 100644 include/grpcpp/support/global_callback_hook.h create mode 100644 src/cpp/client/global_callback_hook.cc diff --git a/BUILD b/BUILD index eb4bc77f081..7cd081fb527 100644 --- a/BUILD +++ b/BUILD @@ -907,6 +907,7 @@ grpc_cc_library( ], visibility = ["@grpc:grpc++_public_hdrs"], deps = [ + "global_callback_hook", "grpc_public_hdrs", "//src/core:gpr_atm", ], @@ -951,6 +952,7 @@ grpc_cc_library( tags = ["nofixdeps"], visibility = ["@grpc:public"], deps = [ + "global_callback_hook", "grpc++_base", "//src/core:gpr_atm", "//src/core:slice", @@ -1260,6 +1262,7 @@ grpc_cc_library( deps = [ "channel_arg_names", "generic_stub_internal", + "global_callback_hook", "gpr", "grpc++_base_unsecure", "grpc++_codegen_proto", @@ -2455,6 +2458,7 @@ grpc_cc_library( "config", "exec_ctx", "generic_stub_internal", + "global_callback_hook", "gpr", "grpc", "grpc++_codegen_proto", @@ -2544,6 +2548,7 @@ grpc_cc_library( "config", "exec_ctx", "generic_stub_internal", + "global_callback_hook", "gpr", "grpc_base", "grpc_core_credentials_header", @@ -4913,6 +4918,22 @@ grpc_cc_library( ], ) +grpc_cc_library( + name = "global_callback_hook", + srcs = [ + "src/cpp/client/global_callback_hook.cc", + ], + hdrs = [ + "include/grpcpp/support/global_callback_hook.h", + ], + external_deps = [ + "absl/base:no_destructor", + "absl/log:check", + "absl/functional:function_ref", + ], + language = "c++", +) + # TODO(yashykt): Remove the UPB definitions from here once they are no longer needed ### UPB Targets diff --git a/CMakeLists.txt b/CMakeLists.txt index da48ca9f836..99a85a30376 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4182,6 +4182,7 @@ add_library(grpc++ src/cpp/client/create_channel.cc src/cpp/client/create_channel_internal.cc src/cpp/client/create_channel_posix.cc + src/cpp/client/global_callback_hook.cc src/cpp/client/insecure_credentials.cc src/cpp/client/secure_credentials.cc src/cpp/client/xds_credentials.cc @@ -4465,6 +4466,7 @@ foreach(_hdr include/grpcpp/support/client_callback.h include/grpcpp/support/client_interceptor.h include/grpcpp/support/config.h + include/grpcpp/support/global_callback_hook.h include/grpcpp/support/interceptor.h include/grpcpp/support/message_allocator.h include/grpcpp/support/method_handler.h @@ -4938,6 +4940,7 @@ add_library(grpc++_unsecure src/cpp/client/create_channel.cc src/cpp/client/create_channel_internal.cc src/cpp/client/create_channel_posix.cc + src/cpp/client/global_callback_hook.cc src/cpp/client/insecure_credentials.cc src/cpp/common/alarm.cc src/cpp/common/channel_arguments.cc @@ -5209,6 +5212,7 @@ foreach(_hdr include/grpcpp/support/client_callback.h include/grpcpp/support/client_interceptor.h include/grpcpp/support/config.h + include/grpcpp/support/global_callback_hook.h include/grpcpp/support/interceptor.h include/grpcpp/support/message_allocator.h include/grpcpp/support/method_handler.h @@ -8266,6 +8270,7 @@ add_executable(binder_transport_test src/cpp/client/create_channel.cc src/cpp/client/create_channel_internal.cc src/cpp/client/create_channel_posix.cc + src/cpp/client/global_callback_hook.cc src/cpp/client/insecure_credentials.cc src/cpp/client/secure_credentials.cc src/cpp/common/alarm.cc @@ -13405,6 +13410,7 @@ add_executable(endpoint_binder_pool_test src/cpp/client/create_channel.cc src/cpp/client/create_channel_internal.cc src/cpp/client/create_channel_posix.cc + src/cpp/client/global_callback_hook.cc src/cpp/client/insecure_credentials.cc src/cpp/client/secure_credentials.cc src/cpp/common/alarm.cc @@ -14260,6 +14266,7 @@ add_executable(fake_binder_test src/cpp/client/create_channel.cc src/cpp/client/create_channel_internal.cc src/cpp/client/create_channel_posix.cc + src/cpp/client/global_callback_hook.cc src/cpp/client/insecure_credentials.cc src/cpp/client/secure_credentials.cc src/cpp/common/alarm.cc @@ -32271,6 +32278,7 @@ add_executable(transport_stream_receiver_test src/cpp/client/create_channel.cc src/cpp/client/create_channel_internal.cc src/cpp/client/create_channel_posix.cc + src/cpp/client/global_callback_hook.cc src/cpp/client/insecure_credentials.cc src/cpp/client/secure_credentials.cc src/cpp/common/alarm.cc @@ -33157,6 +33165,7 @@ add_executable(wire_reader_test src/cpp/client/create_channel.cc src/cpp/client/create_channel_internal.cc src/cpp/client/create_channel_posix.cc + src/cpp/client/global_callback_hook.cc src/cpp/client/insecure_credentials.cc src/cpp/client/secure_credentials.cc src/cpp/common/alarm.cc @@ -33267,6 +33276,7 @@ add_executable(wire_writer_test src/cpp/client/create_channel.cc src/cpp/client/create_channel_internal.cc src/cpp/client/create_channel_posix.cc + src/cpp/client/global_callback_hook.cc src/cpp/client/insecure_credentials.cc src/cpp/client/secure_credentials.cc src/cpp/common/alarm.cc @@ -33847,6 +33857,7 @@ add_executable(xds_client_test ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/percent.grpc.pb.cc ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/percent.pb.h ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/percent.grpc.pb.h + src/cpp/client/global_callback_hook.cc src/cpp/util/status.cc test/core/xds/xds_client_test.cc test/core/xds/xds_transport_fake.cc @@ -34142,6 +34153,7 @@ add_executable(xds_cluster_resource_type_test ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/wrr_locality.grpc.pb.cc ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/wrr_locality.pb.h ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/wrr_locality.grpc.pb.h + src/cpp/client/global_callback_hook.cc src/cpp/util/status.cc test/core/xds/xds_cluster_resource_type_test.cc ) @@ -35138,6 +35150,7 @@ add_executable(xds_endpoint_resource_type_test ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/percent.grpc.pb.cc ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/percent.pb.h ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/percent.grpc.pb.h + src/cpp/client/global_callback_hook.cc src/cpp/util/status.cc test/core/xds/xds_endpoint_resource_type_test.cc ) diff --git a/build_autogenerated.yaml b/build_autogenerated.yaml index b95f89cafd6..abcef83cc2a 100644 --- a/build_autogenerated.yaml +++ b/build_autogenerated.yaml @@ -3836,6 +3836,7 @@ libs: - include/grpcpp/support/client_callback.h - include/grpcpp/support/client_interceptor.h - include/grpcpp/support/config.h + - include/grpcpp/support/global_callback_hook.h - include/grpcpp/support/interceptor.h - include/grpcpp/support/message_allocator.h - include/grpcpp/support/method_handler.h @@ -3915,6 +3916,7 @@ libs: - src/cpp/client/create_channel.cc - src/cpp/client/create_channel_internal.cc - src/cpp/client/create_channel_posix.cc + - src/cpp/client/global_callback_hook.cc - src/cpp/client/insecure_credentials.cc - src/cpp/client/secure_credentials.cc - src/cpp/client/xds_credentials.cc @@ -4267,6 +4269,7 @@ libs: - include/grpcpp/support/client_callback.h - include/grpcpp/support/client_interceptor.h - include/grpcpp/support/config.h + - include/grpcpp/support/global_callback_hook.h - include/grpcpp/support/interceptor.h - include/grpcpp/support/message_allocator.h - include/grpcpp/support/method_handler.h @@ -4303,6 +4306,7 @@ libs: - src/cpp/client/create_channel.cc - src/cpp/client/create_channel_internal.cc - src/cpp/client/create_channel_posix.cc + - src/cpp/client/global_callback_hook.cc - src/cpp/client/insecure_credentials.cc - src/cpp/common/alarm.cc - src/cpp/common/channel_arguments.cc @@ -6182,6 +6186,7 @@ targets: - src/cpp/client/create_channel.cc - src/cpp/client/create_channel_internal.cc - src/cpp/client/create_channel_posix.cc + - src/cpp/client/global_callback_hook.cc - src/cpp/client/insecure_credentials.cc - src/cpp/client/secure_credentials.cc - src/cpp/common/alarm.cc @@ -9668,6 +9673,7 @@ targets: - src/cpp/client/create_channel.cc - src/cpp/client/create_channel_internal.cc - src/cpp/client/create_channel_posix.cc + - src/cpp/client/global_callback_hook.cc - src/cpp/client/insecure_credentials.cc - src/cpp/client/secure_credentials.cc - src/cpp/common/alarm.cc @@ -10143,6 +10149,7 @@ targets: - src/cpp/client/create_channel.cc - src/cpp/client/create_channel_internal.cc - src/cpp/client/create_channel_posix.cc + - src/cpp/client/global_callback_hook.cc - src/cpp/client/insecure_credentials.cc - src/cpp/client/secure_credentials.cc - src/cpp/common/alarm.cc @@ -20379,6 +20386,7 @@ targets: - src/cpp/client/create_channel.cc - src/cpp/client/create_channel_internal.cc - src/cpp/client/create_channel_posix.cc + - src/cpp/client/global_callback_hook.cc - src/cpp/client/insecure_credentials.cc - src/cpp/client/secure_credentials.cc - src/cpp/common/alarm.cc @@ -20798,6 +20806,7 @@ targets: - src/cpp/client/create_channel.cc - src/cpp/client/create_channel_internal.cc - src/cpp/client/create_channel_posix.cc + - src/cpp/client/global_callback_hook.cc - src/cpp/client/insecure_credentials.cc - src/cpp/client/secure_credentials.cc - src/cpp/common/alarm.cc @@ -20909,6 +20918,7 @@ targets: - src/cpp/client/create_channel.cc - src/cpp/client/create_channel_internal.cc - src/cpp/client/create_channel_posix.cc + - src/cpp/client/global_callback_hook.cc - src/cpp/client/insecure_credentials.cc - src/cpp/client/secure_credentials.cc - src/cpp/common/alarm.cc @@ -21232,6 +21242,7 @@ targets: - src/proto/grpc/testing/xds/v3/base.proto - src/proto/grpc/testing/xds/v3/discovery.proto - src/proto/grpc/testing/xds/v3/percent.proto + - src/cpp/client/global_callback_hook.cc - src/cpp/util/status.cc - test/core/xds/xds_client_test.cc - test/core/xds/xds_transport_fake.cc @@ -21329,6 +21340,7 @@ targets: - src/proto/grpc/testing/xds/v3/tls.proto - src/proto/grpc/testing/xds/v3/typed_struct.proto - src/proto/grpc/testing/xds/v3/wrr_locality.proto + - src/cpp/client/global_callback_hook.cc - src/cpp/util/status.cc - test/core/xds/xds_cluster_resource_type_test.cc deps: @@ -21676,6 +21688,7 @@ targets: - src/proto/grpc/testing/xds/v3/endpoint.proto - src/proto/grpc/testing/xds/v3/health_check.proto - src/proto/grpc/testing/xds/v3/percent.proto + - src/cpp/client/global_callback_hook.cc - src/cpp/util/status.cc - test/core/xds/xds_endpoint_resource_type_test.cc deps: diff --git a/gRPC-C++.podspec b/gRPC-C++.podspec index fe2176500b4..122a26ff39d 100644 --- a/gRPC-C++.podspec +++ b/gRPC-C++.podspec @@ -206,6 +206,7 @@ Pod::Spec.new do |s| 'include/grpcpp/support/client_callback.h', 'include/grpcpp/support/client_interceptor.h', 'include/grpcpp/support/config.h', + 'include/grpcpp/support/global_callback_hook.h', 'include/grpcpp/support/interceptor.h', 'include/grpcpp/support/message_allocator.h', 'include/grpcpp/support/method_handler.h', @@ -1378,6 +1379,7 @@ Pod::Spec.new do |s| 'src/cpp/client/create_channel_internal.cc', 'src/cpp/client/create_channel_internal.h', 'src/cpp/client/create_channel_posix.cc', + 'src/cpp/client/global_callback_hook.cc', 'src/cpp/client/insecure_credentials.cc', 'src/cpp/client/secure_credentials.cc', 'src/cpp/client/secure_credentials.h', diff --git a/include/grpcpp/support/callback_common.h b/include/grpcpp/support/callback_common.h index 0de9cbcf050..a333407bb4e 100644 --- a/include/grpcpp/support/callback_common.h +++ b/include/grpcpp/support/callback_common.h @@ -30,6 +30,7 @@ #include #include #include +#include #include namespace grpc { @@ -127,7 +128,18 @@ class CallbackWithStatusTag : public grpc_completion_queue_functor { auto status = std::move(status_); func_ = nullptr; // reset to clear this out for sure status_ = Status(); // reset to clear this out for sure - CatchingCallback(std::move(func), std::move(status)); + GetGlobalCallbackHook()->RunCallback( + call_, [func = std::move(func), status = std::move(status)]() { +#if GRPC_ALLOW_EXCEPTIONS + try { + func(status); + } catch (...) { + // nothing to return or change here, just don't crash the library + } +#else // GRPC_ALLOW_EXCEPTIONS + func(status); +#endif // GRPC_ALLOW_EXCEPTIONS + }); grpc_call_unref(call_); } }; @@ -214,7 +226,17 @@ class CallbackWithSuccessTag : public grpc_completion_queue_functor { #endif if (do_callback) { - CatchingCallback(func_, ok); + GetGlobalCallbackHook()->RunCallback(call_, [this, ok]() { +#if GRPC_ALLOW_EXCEPTIONS + try { + func_(ok); + } catch (...) { + // nothing to return or change here, just don't crash the library + } +#else // GRPC_ALLOW_EXCEPTIONS + func_(ok); +#endif // GRPC_ALLOW_EXCEPTIONS + }); } } }; diff --git a/include/grpcpp/support/global_callback_hook.h b/include/grpcpp/support/global_callback_hook.h new file mode 100644 index 00000000000..c453bc807f4 --- /dev/null +++ b/include/grpcpp/support/global_callback_hook.h @@ -0,0 +1,58 @@ +// Copyright 2024 gRPC authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef GRPCPP_SUPPORT_GLOBAL_CALLBACK_HOOK_H +#define GRPCPP_SUPPORT_GLOBAL_CALLBACK_HOOK_H + +#include "absl/functional/function_ref.h" + +struct grpc_call; + +namespace grpc { + +class GlobalCallbackHook { + public: + virtual ~GlobalCallbackHook() = default; + virtual void RunCallback(grpc_call* call, + absl::FunctionRef callback) = 0; + + protected: + // An exception-safe way of invoking a user-specified callback function. + template + void CatchingCallback(Func&& func, Args&&... args) { +#if GRPC_ALLOW_EXCEPTIONS + try { + func(std::forward(args)...); + } catch (...) { + // nothing to return or change here, just don't crash the library + } +#else // GRPC_ALLOW_EXCEPTIONS + func(std::forward(args)...); +#endif // GRPC_ALLOW_EXCEPTIONS + } +}; + +class DefaultGlobalCallbackHook final : public GlobalCallbackHook { + public: + void RunCallback(grpc_call* call, + absl::FunctionRef callback) override { + CatchingCallback(callback); + } +}; + +std::shared_ptr GetGlobalCallbackHook(); +void SetGlobalCallbackHook(GlobalCallbackHook* hook); +} // namespace grpc + +#endif // GRPCPP_SUPPORT_GLOBAL_CALLBACK_HOOK_H diff --git a/src/cpp/client/global_callback_hook.cc b/src/cpp/client/global_callback_hook.cc new file mode 100644 index 00000000000..2431508b7b3 --- /dev/null +++ b/src/cpp/client/global_callback_hook.cc @@ -0,0 +1,36 @@ +// Copyright 2024 gRPC authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "absl/base/no_destructor.h" +#include "absl/log/check.h" + +#include + +namespace grpc { + +static absl::NoDestructor> g_callback_hook( + std::make_shared()); + +std::shared_ptr GetGlobalCallbackHook() { + return *g_callback_hook; +} + +void SetGlobalCallbackHook(GlobalCallbackHook* hook) { + CHECK(hook != nullptr); + CHECK(hook != (*g_callback_hook).get()); + *g_callback_hook = std::shared_ptr(hook); +} +} // namespace grpc diff --git a/tools/doxygen/Doxyfile.c++ b/tools/doxygen/Doxyfile.c++ index 40501c821bc..af16cffbd10 100644 --- a/tools/doxygen/Doxyfile.c++ +++ b/tools/doxygen/Doxyfile.c++ @@ -1073,6 +1073,7 @@ include/grpcpp/support/channel_arguments.h \ include/grpcpp/support/client_callback.h \ include/grpcpp/support/client_interceptor.h \ include/grpcpp/support/config.h \ +include/grpcpp/support/global_callback_hook.h \ include/grpcpp/support/interceptor.h \ include/grpcpp/support/message_allocator.h \ include/grpcpp/support/method_handler.h \ diff --git a/tools/doxygen/Doxyfile.c++.internal b/tools/doxygen/Doxyfile.c++.internal index 68987e28551..7843c17dae2 100644 --- a/tools/doxygen/Doxyfile.c++.internal +++ b/tools/doxygen/Doxyfile.c++.internal @@ -1073,6 +1073,7 @@ include/grpcpp/support/channel_arguments.h \ include/grpcpp/support/client_callback.h \ include/grpcpp/support/client_interceptor.h \ include/grpcpp/support/config.h \ +include/grpcpp/support/global_callback_hook.h \ include/grpcpp/support/interceptor.h \ include/grpcpp/support/message_allocator.h \ include/grpcpp/support/method_handler.h \ @@ -3046,6 +3047,7 @@ src/cpp/client/create_channel.cc \ src/cpp/client/create_channel_internal.cc \ src/cpp/client/create_channel_internal.h \ src/cpp/client/create_channel_posix.cc \ +src/cpp/client/global_callback_hook.cc \ src/cpp/client/insecure_credentials.cc \ src/cpp/client/secure_credentials.cc \ src/cpp/client/secure_credentials.h \