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
pull/27972/head
AJ Heller 3 years ago committed by GitHub
parent 4d5cbfb6c2
commit 39adc01a57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 76
      BUILD
  2. 2
      CMakeLists.txt
  3. 2
      Makefile
  4. 7
      build_autogenerated.yaml
  5. 1
      config.m4
  6. 1
      config.w32
  7. 4
      gRPC-C++.podspec
  8. 5
      gRPC-Core.podspec
  9. 3
      grpc.gemspec
  10. 2
      grpc.gyp
  11. 17
      include/grpc/event_engine/event_engine.h
  12. 3
      package.xml
  13. 13
      src/core/lib/event_engine/event_engine.cc
  14. 49
      src/core/lib/event_engine/event_engine_factory.cc
  15. 33
      src/core/lib/event_engine/event_engine_factory.h
  16. 30
      src/core/lib/iomgr/event_engine/iomgr.cc
  17. 42
      src/core/lib/iomgr/event_engine/iomgr.h
  18. 5
      src/core/lib/iomgr/event_engine/resolver.cc
  19. 7
      src/core/lib/iomgr/event_engine/tcp.cc
  20. 7
      src/core/lib/iomgr/event_engine/timer.cc
  21. 1
      src/python/grpcio/grpc_core_dependencies.py
  22. 1
      test/core/event_engine/BUILD
  23. 3
      tools/doxygen/Doxyfile.c++.internal
  24. 3
      tools/doxygen/Doxyfile.core.internal
  25. 2
      tools/run_tests/generated/tests.json

76
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",

2
CMakeLists.txt generated

@ -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

2
Makefile generated

@ -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 \

@ -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

1
config.m4 generated

@ -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 \

1
config.w32 generated

@ -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 " +

4
gRPC-C++.podspec generated

@ -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',

5
gRPC-Core.podspec generated

@ -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',

3
grpc.gemspec generated

@ -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 )

2
grpc.gyp generated

@ -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',

@ -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<EventEngine> 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<std::unique_ptr<EventEngine>()>* factory);
/// Create an EventEngine using the default factory
std::unique_ptr<EventEngine> CreateEventEngine();
} // namespace experimental
} // namespace grpc_event_engine

3
package.xml generated

@ -770,6 +770,8 @@
<file baseinstalldir="/" name="src/core/lib/event_engine/endpoint_config.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/event_engine/endpoint_config_internal.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/event_engine/event_engine.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/event_engine/event_engine_factory.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/event_engine/event_engine_factory.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/event_engine/sockaddr.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/event_engine/sockaddr.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/gpr/alloc.cc" role="src" />
@ -901,7 +903,6 @@
<file baseinstalldir="/" name="src/core/lib/iomgr/event_engine/endpoint.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/iomgr/event_engine/endpoint.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/iomgr/event_engine/iomgr.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/iomgr/event_engine/iomgr.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/iomgr/event_engine/pollset.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/iomgr/event_engine/pollset.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/iomgr/event_engine/promise.h" role="src" />

@ -13,17 +13,10 @@
// limitations under the License.
#include <grpc/support/port_platform.h>
#include "absl/strings/str_format.h"
#include "absl/strings/str_join.h"
#include <grpc/event_engine/endpoint_config.h>
#include <grpc/event_engine/event_engine.h>
#include <grpc/event_engine/port.h>
#include <grpc/impl/codegen/grpc_types.h>
#include <grpc/support/log.h>
#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<grpc_event_engine::experimental::EventEngine>
DefaultEventEngineFactory() {
// TODO(nnoble): delete when uv-ee is merged
abort();
}
} // namespace experimental
} // namespace grpc_event_engine

@ -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 <grpc/support/port_platform.h>
#include <grpc/event_engine/event_engine.h>
#include "src/core/lib/gprpp/sync.h"
namespace grpc_event_engine {
namespace experimental {
namespace {
std::function<std::unique_ptr<EventEngine>()>* 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<std::unique_ptr<EventEngine>()>* factory) {
grpc_core::MutexLock lock(g_mu);
g_event_engine_factory = factory;
}
std::unique_ptr<EventEngine> 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

@ -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 <grpc/support/port_platform.h>
#include <grpc/event_engine/event_engine.h>
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

@ -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<grpc_event_engine::experimental::EventEngine>
event_engine) {
GPR_ASSERT(g_event_engine == nullptr);
g_event_engine = event_engine.release();
}
} // namespace grpc_core
#endif // GRPC_USE_EVENT_ENGINE

@ -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 <grpc/support/port_platform.h>
#include <grpc/event_engine/event_engine.h>
/// 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<grpc_event_engine::experimental::EventEngine> event_engine);
} // namespace grpc_core
#endif // GRPC_CORE_LIB_IOMGR_EVENT_ENGINE_IOMGR_H

@ -19,9 +19,9 @@
#include <grpc/event_engine/event_engine.h>
#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<EventEngine::DNSResolver> dns_resolver =
grpc_iomgr_event_engine()->GetDNSResolver();
GetDefaultEventEngine()->GetDNSResolver();
if (dns_resolver == nullptr) {
grpc_core::ExecCtx::Run(
DEBUG_LOCATION, on_done,

@ -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<WrappedInternalSliceAllocatorFactory>(
slice_allocator_factory);
EventEngine* event_engine = grpc_iomgr_event_engine();
EventEngine* event_engine = GetDefaultEventEngine();
absl::StatusOr<std::unique_ptr<EventEngine::Listener>> listener =
event_engine->CreateListener(
[server](std::unique_ptr<EventEngine::Endpoint> ee_endpoint,

@ -16,9 +16,9 @@
#ifdef GRPC_USE_EVENT_ENGINE
#include <grpc/event_engine/event_engine.h>
#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);
}

@ -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',

@ -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",

@ -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 \

@ -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 \

@ -4265,7 +4265,7 @@
"posix",
"windows"
],
"uses_polling": true
"uses_polling": false
},
{
"args": [],

Loading…
Cancel
Save