Relocate shared EventEngine APIs (#28721)

Partially collapse `event_engine_factory.cc` into `event_engine.cc`. Add a
new function `DefaultEventEngineFactory` which is used to set a default
event engine factory at link time, separate from the factory that can be
set at run time. Implemenet this function in
`default_event_engine_factory.cc`.

This allows alternative default event engine factories to be implemented
without requiring the duplication of the implementations of
`SetDefaultEventEngineFactory`, `CreateEventEngine`, and
`GetDefaultEventEngine`.
pull/28738/head
Tamir Duberstein 3 years ago committed by GitHub
parent be723121fc
commit 5c474b0c84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 63
      BUILD
  2. 4
      CMakeLists.txt
  3. 4
      Makefile
  4. 4
      build_autogenerated.yaml
  5. 2
      config.m4
  6. 2
      config.w32
  7. 2
      gRPC-Core.podspec
  8. 2
      grpc.gemspec
  9. 4
      grpc.gyp
  10. 4
      include/grpc/event_engine/event_engine.h
  11. 2
      package.xml
  12. 27
      src/core/lib/event_engine/default_event_engine_factory.cc
  13. 28
      src/core/lib/event_engine/event_engine.cc
  14. 50
      src/core/lib/event_engine/event_engine_factory.cc
  15. 3
      src/core/lib/event_engine/event_engine_factory.h
  16. 2
      src/python/grpcio/grpc_core_dependencies.py
  17. 2
      tools/doxygen/Doxyfile.c++.internal
  18. 2
      tools/doxygen/Doxyfile.core.internal

63
BUILD

@ -1709,13 +1709,8 @@ 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",
],
name = "event_engine_base_hdrs",
hdrs = GRPC_PUBLIC_EVENT_ENGINE_HDRS + GRPC_PUBLIC_HDRS,
external_deps = [
"absl/status",
"absl/status:statusor",
@ -1723,26 +1718,16 @@ grpc_cc_library(
],
deps = [
"gpr_base",
"gpr_platform",
"grpc_codegen",
],
)
grpc_cc_library(
name = "uri_parser",
srcs = [
"src/core/lib/uri/uri_parser.cc",
],
name = "default_event_engine_factory_hdrs",
hdrs = [
"src/core/lib/uri/uri_parser.h",
],
external_deps = [
"absl/status:statusor",
"absl/strings",
"absl/strings:str_format",
"src/core/lib/event_engine/event_engine_factory.h",
],
visibility = ["@grpc:alt_grpc_base_legacy"],
deps = [
"event_engine_base_hdrs",
"gpr_base",
],
)
@ -1750,16 +1735,46 @@ grpc_cc_library(
grpc_cc_library(
name = "default_event_engine_factory",
srcs = [
"src/core/lib/event_engine/event_engine_factory.cc",
"src/core/lib/event_engine/default_event_engine_factory.cc",
],
hdrs = [],
external_deps = [
# TODO(hork): uv, in a subsequent PR
],
deps = [
"event_engine_base",
"default_event_engine_factory_hdrs",
"gpr_base",
],
)
grpc_cc_library(
name = "event_engine_base",
srcs = [
"src/core/lib/event_engine/event_engine.cc",
],
deps = [
"default_event_engine_factory",
"default_event_engine_factory_hdrs",
"event_engine_base_hdrs",
"gpr_base",
],
)
grpc_cc_library(
name = "uri_parser",
srcs = [
"src/core/lib/uri/uri_parser.cc",
],
hdrs = [
"src/core/lib/uri/uri_parser.h",
],
external_deps = [
"absl/status:statusor",
"absl/strings",
"absl/strings:str_format",
],
visibility = ["@grpc:alt_grpc_base_legacy"],
deps = [
"gpr_base",
"gpr_platform",
],
)

4
CMakeLists.txt generated

@ -1968,8 +1968,8 @@ add_library(grpc
src/core/lib/debug/stats_data.cc
src/core/lib/debug/trace.cc
src/core/lib/event_engine/channel_args_endpoint_config.cc
src/core/lib/event_engine/default_event_engine_factory.cc
src/core/lib/event_engine/event_engine.cc
src/core/lib/event_engine/event_engine_factory.cc
src/core/lib/event_engine/memory_allocator.cc
src/core/lib/event_engine/sockaddr.cc
src/core/lib/http/format_request.cc
@ -2619,8 +2619,8 @@ add_library(grpc_unsecure
src/core/lib/debug/stats_data.cc
src/core/lib/debug/trace.cc
src/core/lib/event_engine/channel_args_endpoint_config.cc
src/core/lib/event_engine/default_event_engine_factory.cc
src/core/lib/event_engine/event_engine.cc
src/core/lib/event_engine/event_engine_factory.cc
src/core/lib/event_engine/memory_allocator.cc
src/core/lib/event_engine/sockaddr.cc
src/core/lib/http/format_request.cc

4
Makefile generated

@ -1447,8 +1447,8 @@ LIBGRPC_SRC = \
src/core/lib/debug/stats_data.cc \
src/core/lib/debug/trace.cc \
src/core/lib/event_engine/channel_args_endpoint_config.cc \
src/core/lib/event_engine/default_event_engine_factory.cc \
src/core/lib/event_engine/event_engine.cc \
src/core/lib/event_engine/event_engine_factory.cc \
src/core/lib/event_engine/memory_allocator.cc \
src/core/lib/event_engine/sockaddr.cc \
src/core/lib/http/format_request.cc \
@ -1945,8 +1945,8 @@ LIBGRPC_UNSECURE_SRC = \
src/core/lib/debug/stats_data.cc \
src/core/lib/debug/trace.cc \
src/core/lib/event_engine/channel_args_endpoint_config.cc \
src/core/lib/event_engine/default_event_engine_factory.cc \
src/core/lib/event_engine/event_engine.cc \
src/core/lib/event_engine/event_engine_factory.cc \
src/core/lib/event_engine/memory_allocator.cc \
src/core/lib/event_engine/sockaddr.cc \
src/core/lib/http/format_request.cc \

@ -1480,8 +1480,8 @@ libs:
- src/core/lib/debug/stats_data.cc
- src/core/lib/debug/trace.cc
- src/core/lib/event_engine/channel_args_endpoint_config.cc
- src/core/lib/event_engine/default_event_engine_factory.cc
- src/core/lib/event_engine/event_engine.cc
- src/core/lib/event_engine/event_engine_factory.cc
- src/core/lib/event_engine/memory_allocator.cc
- src/core/lib/event_engine/sockaddr.cc
- src/core/lib/http/format_request.cc
@ -2301,8 +2301,8 @@ libs:
- src/core/lib/debug/stats_data.cc
- src/core/lib/debug/trace.cc
- src/core/lib/event_engine/channel_args_endpoint_config.cc
- src/core/lib/event_engine/default_event_engine_factory.cc
- src/core/lib/event_engine/event_engine.cc
- src/core/lib/event_engine/event_engine_factory.cc
- src/core/lib/event_engine/memory_allocator.cc
- src/core/lib/event_engine/sockaddr.cc
- src/core/lib/http/format_request.cc

2
config.m4 generated

@ -465,8 +465,8 @@ if test "$PHP_GRPC" != "no"; then
src/core/lib/debug/stats_data.cc \
src/core/lib/debug/trace.cc \
src/core/lib/event_engine/channel_args_endpoint_config.cc \
src/core/lib/event_engine/default_event_engine_factory.cc \
src/core/lib/event_engine/event_engine.cc \
src/core/lib/event_engine/event_engine_factory.cc \
src/core/lib/event_engine/memory_allocator.cc \
src/core/lib/event_engine/sockaddr.cc \
src/core/lib/gpr/alloc.cc \

2
config.w32 generated

@ -431,8 +431,8 @@ if (PHP_GRPC != "no") {
"src\\core\\lib\\debug\\stats_data.cc " +
"src\\core\\lib\\debug\\trace.cc " +
"src\\core\\lib\\event_engine\\channel_args_endpoint_config.cc " +
"src\\core\\lib\\event_engine\\default_event_engine_factory.cc " +
"src\\core\\lib\\event_engine\\event_engine.cc " +
"src\\core\\lib\\event_engine\\event_engine_factory.cc " +
"src\\core\\lib\\event_engine\\memory_allocator.cc " +
"src\\core\\lib\\event_engine\\sockaddr.cc " +
"src\\core\\lib\\gpr\\alloc.cc " +

2
gRPC-Core.podspec generated

@ -1022,8 +1022,8 @@ Pod::Spec.new do |s|
'src/core/lib/debug/trace.h',
'src/core/lib/event_engine/channel_args_endpoint_config.cc',
'src/core/lib/event_engine/channel_args_endpoint_config.h',
'src/core/lib/event_engine/default_event_engine_factory.cc',
'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/memory_allocator.cc',
'src/core/lib/event_engine/sockaddr.cc',

2
grpc.gemspec generated

@ -941,8 +941,8 @@ Gem::Specification.new do |s|
s.files += %w( src/core/lib/debug/trace.h )
s.files += %w( src/core/lib/event_engine/channel_args_endpoint_config.cc )
s.files += %w( src/core/lib/event_engine/channel_args_endpoint_config.h )
s.files += %w( src/core/lib/event_engine/default_event_engine_factory.cc )
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/memory_allocator.cc )
s.files += %w( src/core/lib/event_engine/sockaddr.cc )

4
grpc.gyp generated

@ -895,8 +895,8 @@
'src/core/lib/debug/stats_data.cc',
'src/core/lib/debug/trace.cc',
'src/core/lib/event_engine/channel_args_endpoint_config.cc',
'src/core/lib/event_engine/default_event_engine_factory.cc',
'src/core/lib/event_engine/event_engine.cc',
'src/core/lib/event_engine/event_engine_factory.cc',
'src/core/lib/event_engine/memory_allocator.cc',
'src/core/lib/event_engine/sockaddr.cc',
'src/core/lib/http/format_request.cc',
@ -1365,8 +1365,8 @@
'src/core/lib/debug/stats_data.cc',
'src/core/lib/debug/trace.cc',
'src/core/lib/event_engine/channel_args_endpoint_config.cc',
'src/core/lib/event_engine/default_event_engine_factory.cc',
'src/core/lib/event_engine/event_engine.cc',
'src/core/lib/event_engine/event_engine_factory.cc',
'src/core/lib/event_engine/memory_allocator.cc',
'src/core/lib/event_engine/sockaddr.cc',
'src/core/lib/http/format_request.cc',

@ -378,7 +378,7 @@ class EventEngine {
virtual bool Cancel(TaskHandle handle) = 0;
};
/// Replace gRPC's default EventEngine factory
/// 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
@ -390,7 +390,7 @@ class EventEngine {
void SetDefaultEventEngineFactory(
const std::function<std::unique_ptr<EventEngine>()>* factory);
/// Create an EventEngine using the default factory
/// Create an EventEngine using the default factory.
std::unique_ptr<EventEngine> CreateEventEngine();
} // namespace experimental

2
package.xml generated

@ -921,8 +921,8 @@
<file baseinstalldir="/" name="src/core/lib/debug/trace.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/event_engine/channel_args_endpoint_config.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/event_engine/channel_args_endpoint_config.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/event_engine/default_event_engine_factory.cc" 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/memory_allocator.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/event_engine/sockaddr.cc" role="src" />

@ -0,0 +1,27 @@
// Copyright 2022 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 "src/core/lib/event_engine/event_engine_factory.h"
namespace grpc_event_engine {
namespace experimental {
std::unique_ptr<EventEngine> DefaultEventEngineFactory() {
// TODO(hork): call LibuvEventEngineFactory
return nullptr;
}
} // namespace experimental
} // namespace grpc_event_engine

@ -17,6 +17,9 @@
#include <grpc/event_engine/port.h>
#include <grpc/support/log.h>
#include "src/core/lib/event_engine/event_engine_factory.h"
#include "src/core/lib/gprpp/sync.h"
namespace grpc_event_engine {
namespace experimental {
@ -33,5 +36,30 @@ const struct sockaddr* EventEngine::ResolvedAddress::address() const {
socklen_t EventEngine::ResolvedAddress::size() const { return size_; }
namespace {
const std::function<std::unique_ptr<EventEngine>()>* g_event_engine_factory =
nullptr;
grpc_core::Mutex* g_mu = new grpc_core::Mutex();
} // namespace
void SetDefaultEventEngineFactory(
const 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) {
return (*g_event_engine_factory)();
}
return DefaultEventEngineFactory();
}
EventEngine* GetDefaultEventEngine() {
static EventEngine* default_event_engine = CreateEventEngine().release();
return default_event_engine;
}
} // namespace experimental
} // namespace grpc_event_engine

@ -1,50 +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.
#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 {
const 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(
const 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

@ -27,6 +27,9 @@ namespace experimental {
/// Strongly consider whether you could use \a CreateEventEngine instead.
EventEngine* GetDefaultEventEngine();
/// Create an EventEngine using the default factory provided at link time.
std::unique_ptr<EventEngine> DefaultEventEngineFactory();
} // namespace experimental
} // namespace grpc_event_engine

@ -440,8 +440,8 @@ CORE_SOURCE_FILES = [
'src/core/lib/debug/stats_data.cc',
'src/core/lib/debug/trace.cc',
'src/core/lib/event_engine/channel_args_endpoint_config.cc',
'src/core/lib/event_engine/default_event_engine_factory.cc',
'src/core/lib/event_engine/event_engine.cc',
'src/core/lib/event_engine/event_engine_factory.cc',
'src/core/lib/event_engine/memory_allocator.cc',
'src/core/lib/event_engine/sockaddr.cc',
'src/core/lib/gpr/alloc.cc',

@ -1920,8 +1920,8 @@ src/core/lib/debug/trace.cc \
src/core/lib/debug/trace.h \
src/core/lib/event_engine/channel_args_endpoint_config.cc \
src/core/lib/event_engine/channel_args_endpoint_config.h \
src/core/lib/event_engine/default_event_engine_factory.cc \
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/memory_allocator.cc \
src/core/lib/event_engine/sockaddr.cc \

@ -1716,8 +1716,8 @@ src/core/lib/debug/trace.cc \
src/core/lib/debug/trace.h \
src/core/lib/event_engine/channel_args_endpoint_config.cc \
src/core/lib/event_engine/channel_args_endpoint_config.h \
src/core/lib/event_engine/default_event_engine_factory.cc \
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/memory_allocator.cc \
src/core/lib/event_engine/sockaddr.cc \

Loading…
Cancel
Save