| 1 |
// Copyright 2022 The gRPC Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
#ifndef GRPC_CORE_LIB_EVENT_ENGINE_POSIX_ENGINE_POSIX_ENGINE_H |
| 15 |
#define GRPC_CORE_LIB_EVENT_ENGINE_POSIX_ENGINE_POSIX_ENGINE_H |
| 16 |
#include <grpc/support/port_platform.h> |
| 17 |
|
| 18 |
#include <atomic> |
| 19 |
#include <cstdint> |
| 20 |
#include <memory> |
| 21 |
#include <string> |
| 22 |
#include <utility> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
#include "absl/base/thread_annotations.h" |
| 26 |
#include "absl/container/flat_hash_map.h" |
| 27 |
#include "absl/functional/any_invocable.h" |
| 28 |
#include "absl/hash/hash.h" |
| 29 |
#include "absl/status/status.h" |
| 30 |
#include "absl/status/statusor.h" |
| 31 |
#include "absl/strings/string_view.h" |
| 32 |
|
| 33 |
#include <grpc/event_engine/endpoint_config.h> |
| 34 |
#include <grpc/event_engine/event_engine.h> |
| 35 |
#include <grpc/event_engine/memory_allocator.h> |
| 36 |
|
| 37 |
#include "src/core/lib/event_engine/handle_containers.h" |
| 38 |
#include "src/core/lib/event_engine/posix_engine/event_poller.h" |
| 39 |
#include "src/core/lib/event_engine/posix_engine/timer_manager.h" |
| 40 |
#include "src/core/lib/event_engine/thread_pool.h" |
| 41 |
#include "src/core/lib/gprpp/sync.h" |
| 42 |
#include "src/core/lib/iomgr/port.h" |
| 43 |
#include "src/core/lib/surface/init_internally.h" |
| 44 |
|
| 45 |
#ifdef GRPC_POSIX_SOCKET_TCP |
| 46 |
#include "src/core/lib/event_engine/posix_engine/posix_engine_closure.h" |
| 47 |
#include "src/core/lib/event_engine/posix_engine/tcp_socket_utils.h" |
| 48 |
#endif // GRPC_POSIX_SOCKET_TCP |
| 49 |
|
| 50 |
namespace grpc_event_engine { |
| 51 |
namespace experimental { |
| 52 |
|
| 53 |
#ifdef GRPC_POSIX_SOCKET_TCP |
| 54 |
// A helper class to handle asynchronous connect operations. |
| 55 |
class AsyncConnect { |
| 56 |
public: |
| 57 |
AsyncConnect(EventEngine::OnConnectCallback on_connect, |
| 58 |
std::shared_ptr<EventEngine> engine, ThreadPool* executor, |
| 59 |
grpc_event_engine::posix_engine::EventHandle* fd, |
| 60 |
MemoryAllocator&& allocator, |
| 61 |
const grpc_event_engine::posix_engine::PosixTcpOptions& options, |
| 62 |
std::string resolved_addr_str, int64_t connection_handle) |
| 63 |
: on_connect_(std::move(on_connect)), |
| 64 |
engine_(engine), |
| 65 |
executor_(executor), |
| 66 |
fd_(fd), |
| 67 |
allocator_(std::move(allocator)), |
| 68 |
options_(options), |
| 69 |
resolved_addr_str_(resolved_addr_str), |
| 70 |
connection_handle_(connection_handle), |
| 71 |
connect_cancelled_(false) {} |
| 72 |
|
| 73 |
void Start(EventEngine::Duration timeout); |
| 74 |
~AsyncConnect(); |
| 75 |
|
| 76 |
private: |
| 77 |
friend class PosixEventEngine; |
| 78 |
void OnTimeoutExpired(absl::Status status); |
| 79 |
|
| 80 |
void OnWritable(absl::Status status) ABSL_NO_THREAD_SAFETY_ANALYSIS; |
| 81 |
|
| 82 |
grpc_core::Mutex mu_; |
| 83 |
grpc_event_engine::posix_engine::PosixEngineClosure* on_writable_ = nullptr; |
| 84 |
EventEngine::OnConnectCallback on_connect_; |
| 85 |
std::shared_ptr<EventEngine> engine_; |
| 86 |
ThreadPool* executor_; |
| 87 |
EventEngine::TaskHandle alarm_handle_; |
| 88 |
int refs_{2}; |
| 89 |
grpc_event_engine::posix_engine::EventHandle* fd_; |
| 90 |
MemoryAllocator allocator_; |
| 91 |
grpc_event_engine::posix_engine::PosixTcpOptions options_; |
| 92 |
std::string resolved_addr_str_; |
| 93 |
int64_t connection_handle_; |
| 94 |
bool connect_cancelled_; |
| 95 |
}; |
| 96 |
|
| 97 |
// A helper class to manager lifetime of the poller associated with the |
| 98 |
// posix event engine. |
| 99 |
class PosixEnginePollerManager |
| 100 |
: public grpc_event_engine::posix_engine::Scheduler { |
| 101 |
public: |
| 102 |
explicit PosixEnginePollerManager(std::shared_ptr<ThreadPool> executor); |
| 103 |
explicit PosixEnginePollerManager( |
| 104 |
grpc_event_engine::posix_engine::PosixEventPoller* poller); |
| 105 |
grpc_event_engine::posix_engine::PosixEventPoller* Poller() { |
| 106 |
return poller_; |
| 107 |
} |
| 108 |
|
| 109 |
ThreadPool* Executor() { return executor_.get(); } |
| 110 |
|
| 111 |
void Run(experimental::EventEngine::Closure* closure) override; |
| 112 |
void Run(absl::AnyInvocable<void()>) override; |
| 113 |
|
| 114 |
bool IsShuttingDown() { |
| 115 |
return poller_state_.load(std::memory_order_acquire) == |
| 116 |
PollerState::kShuttingDown; |
| 117 |
} |
| 118 |
void TriggerShutdown(); |
| 119 |
|
| 120 |
~PosixEnginePollerManager() override; |
| 121 |
|
| 122 |
private: |
| 123 |
enum class PollerState { kExternal, kOk, kShuttingDown }; |
| 124 |
grpc_event_engine::posix_engine::PosixEventPoller* poller_ = nullptr; |
| 125 |
std::atomic<PollerState> poller_state_{PollerState::kOk}; |
| 126 |
std::shared_ptr<ThreadPool> executor_; |
| 127 |
}; |
| 128 |
#endif // GRPC_POSIX_SOCKET_TCP |
| 129 |
|
| 130 |
// An iomgr-based Posix EventEngine implementation. |
| 131 |
// All methods require an ExecCtx to already exist on the thread's stack. |
| 132 |
// TODO(ctiller): KeepsGrpcInitialized is an interim measure to ensure that |
| 133 |
// event engine is shut down before we shut down iomgr. |
| 134 |
class PosixEventEngine final : public EventEngine, |
| 135 |
public grpc_core::KeepsGrpcInitialized { |
| 136 |
public: |
| 137 |
class PosixDNSResolver : public EventEngine::DNSResolver { |
| 138 |
public: |
| 139 |
~PosixDNSResolver() override; |
| 140 |
LookupTaskHandle LookupHostname(LookupHostnameCallback on_resolve, |
| 141 |
absl::string_view name, |
| 142 |
absl::string_view default_port, |
| 143 |
Duration timeout) override; |
| 144 |
LookupTaskHandle LookupSRV(LookupSRVCallback on_resolve, |
| 145 |
absl::string_view name, |
| 146 |
Duration timeout) override; |
| 147 |
LookupTaskHandle LookupTXT(LookupTXTCallback on_resolve, |
| 148 |
absl::string_view name, |
| 149 |
Duration timeout) override; |
| 150 |
bool CancelLookup(LookupTaskHandle handle) override; |
| 151 |
}; |
| 152 |
|
| 153 |
#ifdef GRPC_POSIX_SOCKET_TCP |
| 154 |
// Constructs an event engine which does not own the poller. Do not call this |
| 155 |
// constructor directly. Instead use the MakeTestOnlyPosixEventEngine static |
| 156 |
// method. Its expected to be used only in tests. |
| 157 |
explicit PosixEventEngine( |
| 158 |
grpc_event_engine::posix_engine::PosixEventPoller* poller); |
| 159 |
PosixEventEngine(); |
| 160 |
#else // GRPC_POSIX_SOCKET_TCP |
| 161 |
PosixEventEngine(); |
| 162 |
#endif // GRPC_POSIX_SOCKET_TCP |
| 163 |
|
| 164 |
~PosixEventEngine() override; |
| 165 |
|
| 166 |
absl::StatusOr<std::unique_ptr<Listener>> CreateListener( |
| 167 |
Listener::AcceptCallback on_accept, |
| 168 |
absl::AnyInvocable<void(absl::Status)> on_shutdown, |
| 169 |
const EndpointConfig& config, |
| 170 |
std::unique_ptr<MemoryAllocatorFactory> memory_allocator_factory) |
| 171 |
override; |
| 172 |
|
| 173 |
ConnectionHandle Connect(OnConnectCallback on_connect, |
| 174 |
const ResolvedAddress& addr, |
| 175 |
const EndpointConfig& args, |
| 176 |
MemoryAllocator memory_allocator, |
| 177 |
Duration timeout) override; |
| 178 |
|
| 179 |
bool CancelConnect(ConnectionHandle handle) override; |
| 180 |
bool IsWorkerThread() override; |
| 181 |
std::unique_ptr<DNSResolver> GetDNSResolver( |
| 182 |
const DNSResolver::ResolverOptions& options) override; |
| 183 |
void Run(Closure* closure) override; |
| 184 |
void Run(absl::AnyInvocable<void()> closure) override; |
| 185 |
TaskHandle RunAfter(Duration when, Closure* closure) override; |
| 186 |
TaskHandle RunAfter(Duration when, |
| 187 |
absl::AnyInvocable<void()> closure) override; |
| 188 |
bool Cancel(TaskHandle handle) override; |
| 189 |
|
| 190 |
#ifdef GRPC_POSIX_SOCKET_TCP |
| 191 |
// The posix event engine returned by this method would not own the poller |
| 192 |
// and would not be in-charge of driving the poller by calling its Work(..) |
| 193 |
// method. Instead its upto the test to drive the poller. The returned posix |
| 194 |
// event engine will also not attempt to shutdown the poller since it does not |
| 195 |
// own it. |
| 196 |
static std::shared_ptr<PosixEventEngine> MakeTestOnlyPosixEventEngine( |
| 197 |
grpc_event_engine::posix_engine::PosixEventPoller* test_only_poller) { |
| 198 |
return std::make_shared<PosixEventEngine>(test_only_poller); |
| 199 |
} |
| 200 |
#endif // GRPC_POSIX_SOCKET_TCP |
| 201 |
|
| 202 |
private: |
| 203 |
struct ClosureData; |
| 204 |
EventEngine::TaskHandle RunAfterInternal(Duration when, |
| 205 |
absl::AnyInvocable<void()> cb); |
| 206 |
|
| 207 |
#ifdef GRPC_POSIX_SOCKET_TCP |
| 208 |
friend class AsyncConnect; |
| 209 |
struct ConnectionShard { |
| 210 |
grpc_core::Mutex mu; |
| 211 |
absl::flat_hash_map<int64_t, AsyncConnect*> pending_connections |
| 212 |
ABSL_GUARDED_BY(&mu); |
| 213 |
}; |
| 214 |
|
| 215 |
static void PollerWorkInternal( |
| 216 |
std::shared_ptr<PosixEnginePollerManager> poller_manager); |
| 217 |
|
| 218 |
ConnectionHandle ConnectInternal( |
| 219 |
grpc_event_engine::posix_engine::PosixSocketWrapper sock, |
| 220 |
OnConnectCallback on_connect, ResolvedAddress addr, |
| 221 |
MemoryAllocator&& allocator, |
| 222 |
const grpc_event_engine::posix_engine::PosixTcpOptions& options, |
| 223 |
Duration timeout); |
| 224 |
|
| 225 |
void OnConnectFinishInternal(int connection_handle); |
| 226 |
|
| 227 |
std::vector<ConnectionShard> connection_shards_; |
| 228 |
std::atomic<int64_t> last_connection_id_{1}; |
| 229 |
|
| 230 |
#endif // GRPC_POSIX_SOCKET_TCP |
| 231 |
|
| 232 |
grpc_core::Mutex mu_; |
| 233 |
TaskHandleSet known_handles_ ABSL_GUARDED_BY(mu_); |
| 234 |
std::atomic<intptr_t> aba_token_{0}; |
| 235 |
std::shared_ptr<ThreadPool> executor_; |
| 236 |
posix_engine::TimerManager timer_manager_; |
| 237 |
#ifdef GRPC_POSIX_SOCKET_TCP |
| 238 |
std::shared_ptr<PosixEnginePollerManager> poller_manager_; |
| 239 |
#endif // GRPC_POSIX_SOCKET_TCP |
| 240 |
}; |
| 241 |
|
| 242 |
} // namespace experimental |
| 243 |
} // namespace grpc_event_engine |
| 244 |
|
| 245 |
#endif // GRPC_CORE_LIB_EVENT_ENGINE_POSIX_ENGINE_POSIX_ENGINE_H |