From 39adc01a5739c6840eae0e8e24ecf8cc62b045c2 Mon Sep 17 00:00:00 2001 From: AJ Heller Date: Tue, 9 Nov 2021 10:46:51 -0800 Subject: [PATCH] Reintroduce the EventEngine default factory (#27920) * Reintroduce the EventEngine default factory An application can provide an EventEngine factory function that allows gRPC internals to create EventEngines as needed. This factory would be used when no EventEngine is provided for some given channel or server, and where an EventEngine otherwise could not be provided by the application. Note that there currently is no API to provide an EventEngine per channel or per server. I've also deleted some previous iterations on global EventEngine and EventEngine factory ideas. This new code lives in a public API, and coexists with iomgr instead of being isolated to an EventEngine-specific iomgr implementation. * add proper namespaces, and fix description * put factory functions in their own file (for replaceability) * add synchronization * generate_projects.sh * extract event_engine_base and event_engine_factory targets Also separate iomgr/event_engine files in the BUILD, with comments * gpr_platform * move all EE factory declarations to event_engine_base Makes internal hackery easier. * add missing deps * reorder dep alphabetically * comment style change --- BUILD | 76 +++++++++++++++---- CMakeLists.txt | 2 + Makefile | 2 + build_autogenerated.yaml | 7 +- config.m4 | 1 + config.w32 | 1 + gRPC-C++.podspec | 4 +- gRPC-Core.podspec | 5 +- grpc.gemspec | 3 +- grpc.gyp | 2 + include/grpc/event_engine/event_engine.h | 17 ++++- package.xml | 3 +- src/core/lib/event_engine/event_engine.cc | 13 ---- .../lib/event_engine/event_engine_factory.cc | 49 ++++++++++++ .../lib/event_engine/event_engine_factory.h | 33 ++++++++ src/core/lib/iomgr/event_engine/iomgr.cc | 30 ++------ src/core/lib/iomgr/event_engine/iomgr.h | 42 ---------- src/core/lib/iomgr/event_engine/resolver.cc | 5 +- src/core/lib/iomgr/event_engine/tcp.cc | 7 +- src/core/lib/iomgr/event_engine/timer.cc | 7 +- src/python/grpcio/grpc_core_dependencies.py | 1 + test/core/event_engine/BUILD | 1 + tools/doxygen/Doxyfile.c++.internal | 3 +- tools/doxygen/Doxyfile.core.internal | 3 +- tools/run_tests/generated/tests.json | 2 +- 25 files changed, 201 insertions(+), 118 deletions(-) create mode 100644 src/core/lib/event_engine/event_engine_factory.cc create mode 100644 src/core/lib/event_engine/event_engine_factory.h delete mode 100644 src/core/lib/iomgr/event_engine/iomgr.h diff --git a/BUILD b/BUILD index 020da669833..fb69e09b03a 100644 --- a/BUILD +++ b/BUILD @@ -1590,6 +1590,42 @@ grpc_cc_library( ], ) +grpc_cc_library( + name = "event_engine_base", + srcs = [ + "src/core/lib/event_engine/event_engine.cc", + ], + hdrs = GRPC_PUBLIC_EVENT_ENGINE_HDRS + GRPC_PUBLIC_HDRS + [ + "src/core/lib/event_engine/event_engine_factory.h", + ], + external_deps = [ + "absl/status", + "absl/status:statusor", + "absl/time", + ], + deps = [ + "gpr_base", + "gpr_platform", + "grpc_codegen", + ], +) + +grpc_cc_library( + name = "default_event_engine_factory", + srcs = [ + "src/core/lib/event_engine/event_engine_factory.cc", + ], + hdrs = [], + external_deps = [ + # TODO(hork): uv, in a subsequent PR + ], + deps = [ + "event_engine_base", + "gpr_base", + "gpr_platform", + ], +) + grpc_cc_library( name = "grpc_base", srcs = [ @@ -1616,7 +1652,6 @@ grpc_cc_library( "src/core/lib/debug/stats.cc", "src/core/lib/debug/stats_data.cc", "src/core/lib/event_engine/endpoint_config.cc", - "src/core/lib/event_engine/event_engine.cc", "src/core/lib/event_engine/sockaddr.cc", "src/core/lib/http/format_request.cc", "src/core/lib/http/httpcli.cc", @@ -1637,14 +1672,6 @@ grpc_cc_library( "src/core/lib/iomgr/ev_poll_posix.cc", "src/core/lib/iomgr/ev_posix.cc", "src/core/lib/iomgr/ev_windows.cc", - "src/core/lib/iomgr/event_engine/closure.cc", - "src/core/lib/iomgr/event_engine/endpoint.cc", - "src/core/lib/iomgr/event_engine/iomgr.cc", - "src/core/lib/iomgr/event_engine/pollset.cc", - "src/core/lib/iomgr/event_engine/resolved_address_internal.cc", - "src/core/lib/iomgr/event_engine/resolver.cc", - "src/core/lib/iomgr/event_engine/tcp.cc", - "src/core/lib/iomgr/event_engine/timer.cc", "src/core/lib/iomgr/executor/mpmcqueue.cc", "src/core/lib/iomgr/executor/threadpool.cc", "src/core/lib/iomgr/fork_posix.cc", @@ -1748,6 +1775,18 @@ grpc_cc_library( "src/core/lib/transport/transport.cc", "src/core/lib/transport/transport_op_string.cc", "src/core/lib/uri/uri_parser.cc", + ] + + # TODO(hork): delete the iomgr glue code when EventEngine is fully + # integrated, or when it becomes obvious the glue code is unnecessary. + [ + "src/core/lib/iomgr/event_engine/closure.cc", + "src/core/lib/iomgr/event_engine/endpoint.cc", + "src/core/lib/iomgr/event_engine/iomgr.cc", + "src/core/lib/iomgr/event_engine/pollset.cc", + "src/core/lib/iomgr/event_engine/resolved_address_internal.cc", + "src/core/lib/iomgr/event_engine/resolver.cc", + "src/core/lib/iomgr/event_engine/tcp.cc", + "src/core/lib/iomgr/event_engine/timer.cc", ], hdrs = [ "src/core/lib/transport/error_utils.h", @@ -1767,7 +1806,6 @@ grpc_cc_library( "src/core/lib/channel/context.h", "src/core/lib/channel/handshaker.h", "src/core/lib/channel/status_util.h", - "src/core/lib/slice/slice_split.h", "src/core/lib/compression/algorithm_metadata.h", "src/core/lib/compression/compression_args.h", "src/core/lib/compression/compression_internal.h", @@ -1796,12 +1834,6 @@ grpc_cc_library( "src/core/lib/iomgr/ev_epollex_linux.h", "src/core/lib/iomgr/ev_poll_posix.h", "src/core/lib/iomgr/ev_posix.h", - "src/core/lib/iomgr/event_engine/closure.h", - "src/core/lib/iomgr/event_engine/endpoint.h", - "src/core/lib/iomgr/event_engine/iomgr.h", - "src/core/lib/iomgr/event_engine/pollset.h", - "src/core/lib/iomgr/event_engine/promise.h", - "src/core/lib/iomgr/event_engine/resolved_address_internal.h", "src/core/lib/iomgr/executor/mpmcqueue.h", "src/core/lib/iomgr/executor/threadpool.h", "src/core/lib/iomgr/gethostname.h", @@ -1854,6 +1886,7 @@ grpc_cc_library( "src/core/lib/iomgr/work_serializer.h", "src/core/lib/slice/b64.h", "src/core/lib/slice/percent_encoding.h", + "src/core/lib/slice/slice_split.h", "src/core/lib/surface/api_trace.h", "src/core/lib/surface/builtins.h", "src/core/lib/surface/call.h", @@ -1896,6 +1929,15 @@ grpc_cc_library( "src/core/lib/iomgr/executor.h", "src/core/lib/iomgr/combiner.h", "src/core/lib/iomgr/iomgr_internal.h", + ] + + # TODO(hork): delete the iomgr glue code when EventEngine is fully + # integrated, or when it becomes obvious the glue code is unnecessary. + [ + "src/core/lib/iomgr/event_engine/closure.h", + "src/core/lib/iomgr/event_engine/endpoint.h", + "src/core/lib/iomgr/event_engine/pollset.h", + "src/core/lib/iomgr/event_engine/promise.h", + "src/core/lib/iomgr/event_engine/resolved_address_internal.h", ], external_deps = [ "absl/container:flat_hash_map", @@ -1918,8 +1960,10 @@ grpc_cc_library( "chunked_vector", "closure", "config", + "default_event_engine_factory", "dual_ref_counted", "error", + "event_engine_base", "exec_ctx", "gpr_base", "gpr_codegen", diff --git a/CMakeLists.txt b/CMakeLists.txt index 1b82006d95b..7dbf82d0568 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1830,6 +1830,7 @@ add_library(grpc src/core/lib/debug/trace.cc src/core/lib/event_engine/endpoint_config.cc src/core/lib/event_engine/event_engine.cc + src/core/lib/event_engine/event_engine_factory.cc src/core/lib/event_engine/sockaddr.cc src/core/lib/http/format_request.cc src/core/lib/http/httpcli.cc @@ -2469,6 +2470,7 @@ add_library(grpc_unsecure src/core/lib/debug/trace.cc src/core/lib/event_engine/endpoint_config.cc src/core/lib/event_engine/event_engine.cc + src/core/lib/event_engine/event_engine_factory.cc src/core/lib/event_engine/sockaddr.cc src/core/lib/http/format_request.cc src/core/lib/http/httpcli.cc diff --git a/Makefile b/Makefile index a8b09cc7944..4d957f0d501 100644 --- a/Makefile +++ b/Makefile @@ -1382,6 +1382,7 @@ LIBGRPC_SRC = \ src/core/lib/debug/trace.cc \ src/core/lib/event_engine/endpoint_config.cc \ src/core/lib/event_engine/event_engine.cc \ + src/core/lib/event_engine/event_engine_factory.cc \ src/core/lib/event_engine/sockaddr.cc \ src/core/lib/http/format_request.cc \ src/core/lib/http/httpcli.cc \ @@ -1867,6 +1868,7 @@ LIBGRPC_UNSECURE_SRC = \ src/core/lib/debug/trace.cc \ src/core/lib/event_engine/endpoint_config.cc \ src/core/lib/event_engine/event_engine.cc \ + src/core/lib/event_engine/event_engine_factory.cc \ src/core/lib/event_engine/sockaddr.cc \ src/core/lib/http/format_request.cc \ src/core/lib/http/httpcli.cc \ diff --git a/build_autogenerated.yaml b/build_autogenerated.yaml index 4c5ada85748..63c4387a11b 100644 --- a/build_autogenerated.yaml +++ b/build_autogenerated.yaml @@ -751,6 +751,7 @@ libs: - src/core/lib/debug/stats_data.h - src/core/lib/debug/trace.h - src/core/lib/event_engine/endpoint_config_internal.h + - src/core/lib/event_engine/event_engine_factory.h - src/core/lib/event_engine/sockaddr.h - src/core/lib/gprpp/atomic_utils.h - src/core/lib/gprpp/bitset.h @@ -785,7 +786,6 @@ libs: - src/core/lib/iomgr/ev_posix.h - src/core/lib/iomgr/event_engine/closure.h - src/core/lib/iomgr/event_engine/endpoint.h - - src/core/lib/iomgr/event_engine/iomgr.h - src/core/lib/iomgr/event_engine/pollset.h - src/core/lib/iomgr/event_engine/promise.h - src/core/lib/iomgr/event_engine/resolved_address_internal.h @@ -1297,6 +1297,7 @@ libs: - src/core/lib/debug/trace.cc - src/core/lib/event_engine/endpoint_config.cc - src/core/lib/event_engine/event_engine.cc + - src/core/lib/event_engine/event_engine_factory.cc - src/core/lib/event_engine/sockaddr.cc - src/core/lib/http/format_request.cc - src/core/lib/http/httpcli.cc @@ -1805,6 +1806,7 @@ libs: - src/core/lib/debug/stats_data.h - src/core/lib/debug/trace.h - src/core/lib/event_engine/endpoint_config_internal.h + - src/core/lib/event_engine/event_engine_factory.h - src/core/lib/event_engine/sockaddr.h - src/core/lib/gprpp/atomic_utils.h - src/core/lib/gprpp/bitset.h @@ -1839,7 +1841,6 @@ libs: - src/core/lib/iomgr/ev_posix.h - src/core/lib/iomgr/event_engine/closure.h - src/core/lib/iomgr/event_engine/endpoint.h - - src/core/lib/iomgr/event_engine/iomgr.h - src/core/lib/iomgr/event_engine/pollset.h - src/core/lib/iomgr/event_engine/promise.h - src/core/lib/iomgr/event_engine/resolved_address_internal.h @@ -2072,6 +2073,7 @@ libs: - src/core/lib/debug/trace.cc - src/core/lib/event_engine/endpoint_config.cc - src/core/lib/event_engine/event_engine.cc + - src/core/lib/event_engine/event_engine_factory.cc - src/core/lib/event_engine/sockaddr.cc - src/core/lib/http/format_request.cc - src/core/lib/http/httpcli.cc @@ -5582,6 +5584,7 @@ targets: - test/core/event_engine/endpoint_config_test.cc deps: - grpc_test_util + uses_polling: false - name: error_details_test gtest: true build: test diff --git a/config.m4 b/config.m4 index 9983cead2f1..8820ee91a17 100644 --- a/config.m4 +++ b/config.m4 @@ -390,6 +390,7 @@ if test "$PHP_GRPC" != "no"; then src/core/lib/debug/trace.cc \ src/core/lib/event_engine/endpoint_config.cc \ src/core/lib/event_engine/event_engine.cc \ + src/core/lib/event_engine/event_engine_factory.cc \ src/core/lib/event_engine/sockaddr.cc \ src/core/lib/gpr/alloc.cc \ src/core/lib/gpr/atm.cc \ diff --git a/config.w32 b/config.w32 index 07270cf01eb..73f49fc6add 100644 --- a/config.w32 +++ b/config.w32 @@ -356,6 +356,7 @@ if (PHP_GRPC != "no") { "src\\core\\lib\\debug\\trace.cc " + "src\\core\\lib\\event_engine\\endpoint_config.cc " + "src\\core\\lib\\event_engine\\event_engine.cc " + + "src\\core\\lib\\event_engine\\event_engine_factory.cc " + "src\\core\\lib\\event_engine\\sockaddr.cc " + "src\\core\\lib\\gpr\\alloc.cc " + "src\\core\\lib\\gpr\\atm.cc " + diff --git a/gRPC-C++.podspec b/gRPC-C++.podspec index 87236742b4e..1d1f5b7a8df 100644 --- a/gRPC-C++.podspec +++ b/gRPC-C++.podspec @@ -581,6 +581,7 @@ Pod::Spec.new do |s| 'src/core/lib/debug/stats_data.h', 'src/core/lib/debug/trace.h', 'src/core/lib/event_engine/endpoint_config_internal.h', + 'src/core/lib/event_engine/event_engine_factory.h', 'src/core/lib/event_engine/sockaddr.h', 'src/core/lib/gpr/alloc.h', 'src/core/lib/gpr/env.h', @@ -643,7 +644,6 @@ Pod::Spec.new do |s| 'src/core/lib/iomgr/ev_posix.h', 'src/core/lib/iomgr/event_engine/closure.h', 'src/core/lib/iomgr/event_engine/endpoint.h', - 'src/core/lib/iomgr/event_engine/iomgr.h', 'src/core/lib/iomgr/event_engine/pollset.h', 'src/core/lib/iomgr/event_engine/promise.h', 'src/core/lib/iomgr/event_engine/resolved_address_internal.h', @@ -1274,6 +1274,7 @@ Pod::Spec.new do |s| 'src/core/lib/debug/stats_data.h', 'src/core/lib/debug/trace.h', 'src/core/lib/event_engine/endpoint_config_internal.h', + 'src/core/lib/event_engine/event_engine_factory.h', 'src/core/lib/event_engine/sockaddr.h', 'src/core/lib/gpr/alloc.h', 'src/core/lib/gpr/env.h', @@ -1336,7 +1337,6 @@ Pod::Spec.new do |s| 'src/core/lib/iomgr/ev_posix.h', 'src/core/lib/iomgr/event_engine/closure.h', 'src/core/lib/iomgr/event_engine/endpoint.h', - 'src/core/lib/iomgr/event_engine/iomgr.h', 'src/core/lib/iomgr/event_engine/pollset.h', 'src/core/lib/iomgr/event_engine/promise.h', 'src/core/lib/iomgr/event_engine/resolved_address_internal.h', diff --git a/gRPC-Core.podspec b/gRPC-Core.podspec index 6663750b85e..1cbc14a79d2 100644 --- a/gRPC-Core.podspec +++ b/gRPC-Core.podspec @@ -870,6 +870,8 @@ Pod::Spec.new do |s| 'src/core/lib/event_engine/endpoint_config.cc', 'src/core/lib/event_engine/endpoint_config_internal.h', 'src/core/lib/event_engine/event_engine.cc', + 'src/core/lib/event_engine/event_engine_factory.cc', + 'src/core/lib/event_engine/event_engine_factory.h', 'src/core/lib/event_engine/sockaddr.cc', 'src/core/lib/event_engine/sockaddr.h', 'src/core/lib/gpr/alloc.cc', @@ -1001,7 +1003,6 @@ Pod::Spec.new do |s| 'src/core/lib/iomgr/event_engine/endpoint.cc', 'src/core/lib/iomgr/event_engine/endpoint.h', 'src/core/lib/iomgr/event_engine/iomgr.cc', - 'src/core/lib/iomgr/event_engine/iomgr.h', 'src/core/lib/iomgr/event_engine/pollset.cc', 'src/core/lib/iomgr/event_engine/pollset.h', 'src/core/lib/iomgr/event_engine/promise.h', @@ -1790,6 +1791,7 @@ Pod::Spec.new do |s| 'src/core/lib/debug/stats_data.h', 'src/core/lib/debug/trace.h', 'src/core/lib/event_engine/endpoint_config_internal.h', + 'src/core/lib/event_engine/event_engine_factory.h', 'src/core/lib/event_engine/sockaddr.h', 'src/core/lib/gpr/alloc.h', 'src/core/lib/gpr/env.h', @@ -1852,7 +1854,6 @@ Pod::Spec.new do |s| 'src/core/lib/iomgr/ev_posix.h', 'src/core/lib/iomgr/event_engine/closure.h', 'src/core/lib/iomgr/event_engine/endpoint.h', - 'src/core/lib/iomgr/event_engine/iomgr.h', 'src/core/lib/iomgr/event_engine/pollset.h', 'src/core/lib/iomgr/event_engine/promise.h', 'src/core/lib/iomgr/event_engine/resolved_address_internal.h', diff --git a/grpc.gemspec b/grpc.gemspec index 57ff8b9b9f9..aa5ef703e5d 100644 --- a/grpc.gemspec +++ b/grpc.gemspec @@ -790,6 +790,8 @@ Gem::Specification.new do |s| s.files += %w( src/core/lib/event_engine/endpoint_config.cc ) s.files += %w( src/core/lib/event_engine/endpoint_config_internal.h ) s.files += %w( src/core/lib/event_engine/event_engine.cc ) + s.files += %w( src/core/lib/event_engine/event_engine_factory.cc ) + s.files += %w( src/core/lib/event_engine/event_engine_factory.h ) s.files += %w( src/core/lib/event_engine/sockaddr.cc ) s.files += %w( src/core/lib/event_engine/sockaddr.h ) s.files += %w( src/core/lib/gpr/alloc.cc ) @@ -921,7 +923,6 @@ Gem::Specification.new do |s| s.files += %w( src/core/lib/iomgr/event_engine/endpoint.cc ) s.files += %w( src/core/lib/iomgr/event_engine/endpoint.h ) s.files += %w( src/core/lib/iomgr/event_engine/iomgr.cc ) - s.files += %w( src/core/lib/iomgr/event_engine/iomgr.h ) s.files += %w( src/core/lib/iomgr/event_engine/pollset.cc ) s.files += %w( src/core/lib/iomgr/event_engine/pollset.h ) s.files += %w( src/core/lib/iomgr/event_engine/promise.h ) diff --git a/grpc.gyp b/grpc.gyp index 1c5540fa2d1..d4f8e5c3841 100644 --- a/grpc.gyp +++ b/grpc.gyp @@ -823,6 +823,7 @@ 'src/core/lib/debug/trace.cc', 'src/core/lib/event_engine/endpoint_config.cc', 'src/core/lib/event_engine/event_engine.cc', + 'src/core/lib/event_engine/event_engine_factory.cc', 'src/core/lib/event_engine/sockaddr.cc', 'src/core/lib/http/format_request.cc', 'src/core/lib/http/httpcli.cc', @@ -1283,6 +1284,7 @@ 'src/core/lib/debug/trace.cc', 'src/core/lib/event_engine/endpoint_config.cc', 'src/core/lib/event_engine/event_engine.cc', + 'src/core/lib/event_engine/event_engine_factory.cc', 'src/core/lib/event_engine/sockaddr.cc', 'src/core/lib/http/format_request.cc', 'src/core/lib/http/httpcli.cc', diff --git a/include/grpc/event_engine/event_engine.h b/include/grpc/event_engine/event_engine.h index 2b8acabef42..c1825d42e2e 100644 --- a/include/grpc/event_engine/event_engine.h +++ b/include/grpc/event_engine/event_engine.h @@ -378,9 +378,20 @@ class EventEngine { virtual bool Cancel(TaskHandle handle) = 0; }; -// TODO(hork): finalize the API and document it. We need to firm up the story -// around user-provided EventEngines. -std::shared_ptr DefaultEventEngineFactory(); +/// Replace gRPC's default EventEngine factory +/// +/// Applications may call \a SetDefaultEventEngineFactory at any time to replace +/// the default factory used within gRPC. EventEngines will be created when +/// necessary, when they are otherwise not provided by the application. +/// +/// To be certain that none of the gRPC-provided built-in EventEngines are +/// created, applications must set a custom EventEngine factory method *before* +/// grpc is initialized. +void SetDefaultEventEngineFactory( + const std::function()>* factory); + +/// Create an EventEngine using the default factory +std::unique_ptr CreateEventEngine(); } // namespace experimental } // namespace grpc_event_engine diff --git a/package.xml b/package.xml index e5e1da43915..839dae7ea29 100644 --- a/package.xml +++ b/package.xml @@ -770,6 +770,8 @@ + + @@ -901,7 +903,6 @@ - diff --git a/src/core/lib/event_engine/event_engine.cc b/src/core/lib/event_engine/event_engine.cc index cce18ab4587..447544c7b43 100644 --- a/src/core/lib/event_engine/event_engine.cc +++ b/src/core/lib/event_engine/event_engine.cc @@ -13,17 +13,10 @@ // limitations under the License. #include -#include "absl/strings/str_format.h" -#include "absl/strings/str_join.h" - -#include #include #include -#include #include -#include "src/core/lib/event_engine/sockaddr.h" - namespace grpc_event_engine { namespace experimental { @@ -40,11 +33,5 @@ const struct sockaddr* EventEngine::ResolvedAddress::address() const { socklen_t EventEngine::ResolvedAddress::size() const { return size_; } -std::shared_ptr -DefaultEventEngineFactory() { - // TODO(nnoble): delete when uv-ee is merged - abort(); -} - } // namespace experimental } // namespace grpc_event_engine diff --git a/src/core/lib/event_engine/event_engine_factory.cc b/src/core/lib/event_engine/event_engine_factory.cc new file mode 100644 index 00000000000..3d29543b698 --- /dev/null +++ b/src/core/lib/event_engine/event_engine_factory.cc @@ -0,0 +1,49 @@ +// Copyright 2021 The 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 + +#include "src/core/lib/gprpp/sync.h" + +namespace grpc_event_engine { +namespace experimental { + +namespace { +std::function()>* g_event_engine_factory = nullptr; +grpc_core::Mutex* g_mu = new grpc_core::Mutex(); +} // namespace + +EventEngine* GetDefaultEventEngine() { + static EventEngine* default_event_engine = CreateEventEngine().release(); + return default_event_engine; +} + +void SetDefaultEventEngineFactory( + std::function()>* factory) { + grpc_core::MutexLock lock(g_mu); + g_event_engine_factory = factory; +} + +std::unique_ptr CreateEventEngine() { + grpc_core::MutexLock lock(g_mu); + if (g_event_engine_factory == nullptr) { + // TODO(hork): call LibuvEventEngineFactory + abort(); + } + return (*g_event_engine_factory)(); +} + +} // namespace experimental +} // namespace grpc_event_engine diff --git a/src/core/lib/event_engine/event_engine_factory.h b/src/core/lib/event_engine/event_engine_factory.h new file mode 100644 index 00000000000..92e25e0252a --- /dev/null +++ b/src/core/lib/event_engine/event_engine_factory.h @@ -0,0 +1,33 @@ +// Copyright 2021 The 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 GRPC_CORE_LIB_EVENT_ENGINE_EVENT_ENGINE_FACTORY_H +#define GRPC_CORE_LIB_EVENT_ENGINE_EVENT_ENGINE_FACTORY_H + +#include + +#include + +namespace grpc_event_engine { +namespace experimental { + +/// Access the shared global EventEngine instance. +/// +/// The concept of a global EventEngine may go away in a post-iomgr world. +/// Strongly consider whether you could use \a CreateEventEngine instead. +EventEngine* GetDefaultEventEngine(); + +} // namespace experimental +} // namespace grpc_event_engine + +#endif // GRPC_CORE_LIB_EVENT_ENGINE_EVENT_ENGINE_FACTORY_H diff --git a/src/core/lib/iomgr/event_engine/iomgr.cc b/src/core/lib/iomgr/event_engine/iomgr.cc index 272043285ca..c392100855b 100644 --- a/src/core/lib/iomgr/event_engine/iomgr.cc +++ b/src/core/lib/iomgr/event_engine/iomgr.cc @@ -18,7 +18,6 @@ #include "src/core/lib/debug/trace.h" #include "src/core/lib/iomgr/closure.h" -#include "src/core/lib/iomgr/event_engine/iomgr.h" #include "src/core/lib/iomgr/iomgr_internal.h" #include "src/core/lib/iomgr/tcp_client.h" #include "src/core/lib/iomgr/tcp_server.h" @@ -38,25 +37,21 @@ grpc_core::DebugOnlyTraceFlag grpc_polling_trace(false, "polling"); namespace { -using ::grpc_event_engine::experimental::DefaultEventEngineFactory; using ::grpc_event_engine::experimental::EventEngine; - -EventEngine* g_event_engine = nullptr; +using ::grpc_event_engine::experimental::GetDefaultEventEngine; // TODO(nnoble): Instantiate the default EventEngine if none have been provided. -void iomgr_platform_init(void) { GPR_ASSERT(g_event_engine != nullptr); } +void iomgr_platform_init(void) {} void iomgr_platform_flush(void) {} -void iomgr_platform_shutdown(void) { - delete g_event_engine; - g_event_engine = nullptr; -} +void iomgr_platform_shutdown(void) {} void iomgr_platform_shutdown_background_closure(void) {} bool iomgr_platform_is_any_background_poller_thread(void) { - return g_event_engine->IsWorkerThread(); + return grpc_event_engine::experimental::GetDefaultEventEngine() + ->IsWorkerThread(); } bool iomgr_platform_add_closure_to_background_poller( @@ -86,19 +81,4 @@ void grpc_set_default_iomgr_platform() { bool grpc_iomgr_run_in_background() { return false; } -grpc_event_engine::experimental::EventEngine* grpc_iomgr_event_engine() { - return g_event_engine; -} - -namespace grpc_core { - -void SetDefaultEventEngine( - std::unique_ptr - event_engine) { - GPR_ASSERT(g_event_engine == nullptr); - g_event_engine = event_engine.release(); -} - -} // namespace grpc_core - #endif // GRPC_USE_EVENT_ENGINE diff --git a/src/core/lib/iomgr/event_engine/iomgr.h b/src/core/lib/iomgr/event_engine/iomgr.h deleted file mode 100644 index 8026c28586c..00000000000 --- a/src/core/lib/iomgr/event_engine/iomgr.h +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright 2021 The 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 GRPC_CORE_LIB_IOMGR_EVENT_ENGINE_IOMGR_H -#define GRPC_CORE_LIB_IOMGR_EVENT_ENGINE_IOMGR_H -#include - -#include - -/// This can be called anywhere in the EventEngine-based iomgr impl where we -/// need to access the global EventEngine instance. -grpc_event_engine::experimental::EventEngine* grpc_iomgr_event_engine(); - -namespace grpc_core { - -/// Set the default \a EventEngine. -/// -/// The iomgr interfaces conceptually expose a global singleton iomgr instance -/// that is shared throughout gRPC. To accomodate an EventEngine-based iomgr -/// implementation, this method sets the default EventEngine that will be used. -/// The default EventEngine can only be set once during the lifetime of gRPC. -/// This method must be called before \a grpc_init() (truly, before -/// \a grpc_iomgr_init()). This engine is shut down along with iomgr. -/// -/// This is an internal method, not intended for public use. Public APIs are -/// being planned. -void SetDefaultEventEngine( - std::unique_ptr event_engine); - -} // namespace grpc_core - -#endif // GRPC_CORE_LIB_IOMGR_EVENT_ENGINE_IOMGR_H diff --git a/src/core/lib/iomgr/event_engine/resolver.cc b/src/core/lib/iomgr/event_engine/resolver.cc index 628c305e106..4cea6ce7c8a 100644 --- a/src/core/lib/iomgr/event_engine/resolver.cc +++ b/src/core/lib/iomgr/event_engine/resolver.cc @@ -19,9 +19,9 @@ #include #include "src/core/lib/address_utils/sockaddr_utils.h" +#include "src/core/lib/event_engine/event_engine_factory.h" #include "src/core/lib/gprpp/sync.h" #include "src/core/lib/iomgr/error.h" -#include "src/core/lib/iomgr/event_engine/iomgr.h" #include "src/core/lib/iomgr/event_engine/promise.h" #include "src/core/lib/iomgr/event_engine/resolved_address_internal.h" #include "src/core/lib/iomgr/resolve_address.h" @@ -32,6 +32,7 @@ namespace { using ::grpc_event_engine::experimental::CreateGRPCResolvedAddress; using ::grpc_event_engine::experimental::EventEngine; +using ::grpc_event_engine::experimental::GetDefaultEventEngine; using ::grpc_event_engine::experimental::Promise; /// A fire-and-forget class representing an individual DNS request. @@ -80,7 +81,7 @@ void resolve_address(const char* addr, const char* default_port, grpc_closure* on_done, grpc_resolved_addresses** addresses) { std::unique_ptr dns_resolver = - grpc_iomgr_event_engine()->GetDNSResolver(); + GetDefaultEventEngine()->GetDNSResolver(); if (dns_resolver == nullptr) { grpc_core::ExecCtx::Run( DEBUG_LOCATION, on_done, diff --git a/src/core/lib/iomgr/event_engine/tcp.cc b/src/core/lib/iomgr/event_engine/tcp.cc index 4be130fa47d..775152b7ee5 100644 --- a/src/core/lib/iomgr/event_engine/tcp.cc +++ b/src/core/lib/iomgr/event_engine/tcp.cc @@ -18,11 +18,11 @@ #include "src/core/lib/address_utils/sockaddr_utils.h" #include "src/core/lib/event_engine/endpoint_config_internal.h" +#include "src/core/lib/event_engine/event_engine_factory.h" #include "src/core/lib/event_engine/sockaddr.h" #include "src/core/lib/gprpp/ref_counted.h" #include "src/core/lib/iomgr/event_engine/closure.h" #include "src/core/lib/iomgr/event_engine/endpoint.h" -#include "src/core/lib/iomgr/event_engine/iomgr.h" #include "src/core/lib/iomgr/event_engine/pollset.h" #include "src/core/lib/iomgr/resolve_address.h" #include "src/core/lib/iomgr/tcp_client.h" @@ -35,6 +35,7 @@ extern grpc_core::TraceFlag grpc_tcp_trace; namespace { using ::grpc_event_engine::experimental::ChannelArgsEndpointConfig; using ::grpc_event_engine::experimental::EventEngine; +using ::grpc_event_engine::experimental::GetDefaultEventEngine; using ::grpc_event_engine::experimental::GrpcClosureToStatusCallback; using ::grpc_event_engine::experimental::SliceAllocator; using ::grpc_event_engine::experimental::SliceAllocatorFactory; @@ -152,7 +153,7 @@ void tcp_connect(grpc_closure* on_connect, grpc_endpoint** endpoint, absl::Time ee_deadline = grpc_core::ToAbslTime( grpc_millis_to_timespec(deadline, GPR_CLOCK_MONOTONIC)); ChannelArgsEndpointConfig endpoint_config(channel_args); - absl::Status connected = grpc_iomgr_event_engine()->Connect( + absl::Status connected = GetDefaultEventEngine()->Connect( ee_on_connect, ra, endpoint_config, std::move(ee_slice_allocator), ee_deadline); if (!connected.ok()) { @@ -172,7 +173,7 @@ grpc_error_handle tcp_server_create( auto ee_slice_allocator_factory = absl::make_unique( slice_allocator_factory); - EventEngine* event_engine = grpc_iomgr_event_engine(); + EventEngine* event_engine = GetDefaultEventEngine(); absl::StatusOr> listener = event_engine->CreateListener( [server](std::unique_ptr ee_endpoint, diff --git a/src/core/lib/iomgr/event_engine/timer.cc b/src/core/lib/iomgr/event_engine/timer.cc index bb7dbfb9ba4..6ac49f11c93 100644 --- a/src/core/lib/iomgr/event_engine/timer.cc +++ b/src/core/lib/iomgr/event_engine/timer.cc @@ -16,9 +16,9 @@ #ifdef GRPC_USE_EVENT_ENGINE #include +#include "src/core/lib/event_engine/event_engine_factory.h" #include "src/core/lib/iomgr/error.h" #include "src/core/lib/iomgr/event_engine/closure.h" -#include "src/core/lib/iomgr/event_engine/iomgr.h" #include "src/core/lib/iomgr/exec_ctx.h" #include "src/core/lib/iomgr/timer.h" #include "src/core/lib/surface/init.h" @@ -26,11 +26,12 @@ namespace { using ::grpc_event_engine::experimental::EventEngine; +using ::grpc_event_engine::experimental::GetDefaultEventEngine; using ::grpc_event_engine::experimental::GrpcClosureToCallback; void timer_init(grpc_timer* timer, grpc_millis deadline, grpc_closure* closure) { - timer->ee_task_handle = grpc_iomgr_event_engine()->RunAt( + timer->ee_task_handle = GetDefaultEventEngine()->RunAt( grpc_core::ToAbslTime( grpc_millis_to_timespec(deadline, GPR_CLOCK_REALTIME)), GrpcClosureToCallback(closure)); @@ -39,7 +40,7 @@ void timer_init(grpc_timer* timer, grpc_millis deadline, void timer_cancel(grpc_timer* timer) { auto handle = timer->ee_task_handle; - if (!grpc_iomgr_event_engine()->Cancel(handle)) { + if (!GetDefaultEventEngine()->Cancel(handle)) { grpc_core::ExecCtx::Run(DEBUG_LOCATION, timer->closure, GRPC_ERROR_CANCELLED); } diff --git a/src/python/grpcio/grpc_core_dependencies.py b/src/python/grpcio/grpc_core_dependencies.py index 78d603dbcd1..e9e255c4bc9 100644 --- a/src/python/grpcio/grpc_core_dependencies.py +++ b/src/python/grpcio/grpc_core_dependencies.py @@ -365,6 +365,7 @@ CORE_SOURCE_FILES = [ 'src/core/lib/debug/trace.cc', 'src/core/lib/event_engine/endpoint_config.cc', 'src/core/lib/event_engine/event_engine.cc', + 'src/core/lib/event_engine/event_engine_factory.cc', 'src/core/lib/event_engine/sockaddr.cc', 'src/core/lib/gpr/alloc.cc', 'src/core/lib/gpr/atm.cc', diff --git a/test/core/event_engine/BUILD b/test/core/event_engine/BUILD index af352f09fed..685d96a995f 100644 --- a/test/core/event_engine/BUILD +++ b/test/core/event_engine/BUILD @@ -23,6 +23,7 @@ grpc_cc_test( srcs = ["endpoint_config_test.cc"], external_deps = ["gtest"], language = "C++", + uses_polling = False, deps = [ "//:grpc", "//test/core/util:grpc_test_util", diff --git a/tools/doxygen/Doxyfile.c++.internal b/tools/doxygen/Doxyfile.c++.internal index ba2c80096c5..717be0fccc1 100644 --- a/tools/doxygen/Doxyfile.c++.internal +++ b/tools/doxygen/Doxyfile.c++.internal @@ -1764,6 +1764,8 @@ src/core/lib/debug/trace.h \ src/core/lib/event_engine/endpoint_config.cc \ src/core/lib/event_engine/endpoint_config_internal.h \ src/core/lib/event_engine/event_engine.cc \ +src/core/lib/event_engine/event_engine_factory.cc \ +src/core/lib/event_engine/event_engine_factory.h \ src/core/lib/event_engine/sockaddr.cc \ src/core/lib/event_engine/sockaddr.h \ src/core/lib/gpr/alloc.cc \ @@ -1895,7 +1897,6 @@ src/core/lib/iomgr/event_engine/closure.h \ src/core/lib/iomgr/event_engine/endpoint.cc \ src/core/lib/iomgr/event_engine/endpoint.h \ src/core/lib/iomgr/event_engine/iomgr.cc \ -src/core/lib/iomgr/event_engine/iomgr.h \ src/core/lib/iomgr/event_engine/pollset.cc \ src/core/lib/iomgr/event_engine/pollset.h \ src/core/lib/iomgr/event_engine/promise.h \ diff --git a/tools/doxygen/Doxyfile.core.internal b/tools/doxygen/Doxyfile.core.internal index a7d456f84dc..f13563fa2ce 100644 --- a/tools/doxygen/Doxyfile.core.internal +++ b/tools/doxygen/Doxyfile.core.internal @@ -1561,6 +1561,8 @@ src/core/lib/debug/trace.h \ src/core/lib/event_engine/endpoint_config.cc \ src/core/lib/event_engine/endpoint_config_internal.h \ src/core/lib/event_engine/event_engine.cc \ +src/core/lib/event_engine/event_engine_factory.cc \ +src/core/lib/event_engine/event_engine_factory.h \ src/core/lib/event_engine/sockaddr.cc \ src/core/lib/event_engine/sockaddr.h \ src/core/lib/gpr/README.md \ @@ -1695,7 +1697,6 @@ src/core/lib/iomgr/event_engine/closure.h \ src/core/lib/iomgr/event_engine/endpoint.cc \ src/core/lib/iomgr/event_engine/endpoint.h \ src/core/lib/iomgr/event_engine/iomgr.cc \ -src/core/lib/iomgr/event_engine/iomgr.h \ src/core/lib/iomgr/event_engine/pollset.cc \ src/core/lib/iomgr/event_engine/pollset.h \ src/core/lib/iomgr/event_engine/promise.h \ diff --git a/tools/run_tests/generated/tests.json b/tools/run_tests/generated/tests.json index bac4b2297a1..d62c123f745 100644 --- a/tools/run_tests/generated/tests.json +++ b/tools/run_tests/generated/tests.json @@ -4265,7 +4265,7 @@ "posix", "windows" ], - "uses_polling": true + "uses_polling": false }, { "args": [],