From 9296f96c60c1cdcbff47e5899d508e9ebdac5bc1 Mon Sep 17 00:00:00 2001 From: Itamar Kaminski Date: Mon, 25 Jan 2021 18:49:57 +0200 Subject: [PATCH] Revert "event: Extract DispatcherBase interface (#14446)" This reverts commit e73281dc992113ea5ae27324600e81bcb7e8ab14. --- include/envoy/event/dispatcher.h | 412 ++++++++++++++++--------------- 1 file changed, 212 insertions(+), 200 deletions(-) diff --git a/include/envoy/event/dispatcher.h b/include/envoy/event/dispatcher.h index b9dd1ade0e5d..36e34f78bd1a 100644 --- a/include/envoy/event/dispatcher.h +++ b/include/envoy/event/dispatcher.h @@ -73,211 +73,223 @@ class DispatcherBase { uint32_t events) PURE; /** - * Abstract event dispatching loop. + * Allocates a timer. @see Timer for docs on how to use the timer. + * @param cb supplies the callback to invoke when the timer fires. */ - class Dispatcher : public DispatcherBase { - public: - /** - * Allocates a timer. @see Timer for docs on how to use the timer. - * @param cb supplies the callback to invoke when the timer fires. - */ - virtual Event::TimerPtr createTimer(TimerCb cb) PURE; - - /** - * Allocates a schedulable callback. @see SchedulableCallback for docs on how to use the wrapped - * callback. - * @param cb supplies the callback to invoke when the SchedulableCallback is triggered on the - * event loop. - */ - virtual Event::SchedulableCallbackPtr createSchedulableCallback(std::function cb) PURE; - - /** - * Sets a tracked object, which is currently operating in this Dispatcher. - * This should be cleared with another call to setTrackedObject() when the object is done doing - * work. Calling setTrackedObject(nullptr) results in no object being tracked. - * - * This is optimized for performance, to avoid allocation where we do scoped object tracking. - * - * @return The previously tracked object or nullptr if there was none. - */ - virtual const ScopeTrackedObject* setTrackedObject(const ScopeTrackedObject* object) PURE; - - /** - * Validates that an operation is thread-safe with respect to this dispatcher; i.e. that the - * current thread of execution is on the same thread upon which the dispatcher loop is running. - */ - virtual bool isThreadSafe() const PURE; - - /** - * Returns a recently cached MonotonicTime value. - */ - virtual MonotonicTime approximateMonotonicTime() const PURE; - }; + virtual Event::TimerPtr createTimer(TimerCb cb) PURE; + + /** + * Allocates a scaled timer. @see Timer for docs on how to use the timer. + * @param timer_type the type of timer to create. + * @param cb supplies the callback to invoke when the timer fires. + */ + virtual Event::TimerPtr createScaledTimer(Event::ScaledTimerType timer_type, TimerCb cb) PURE; + + /** + * Allocates a scaled timer. @see Timer for docs on how to use the timer. + * @param minimum the rule for computing the minimum value of the timer. + * @param cb supplies the callback to invoke when the timer fires. + */ + virtual Event::TimerPtr createScaledTimer(Event::ScaledTimerMinimum minimum, TimerCb cb) PURE; + + /** + * Allocates a schedulable callback. @see SchedulableCallback for docs on how to use the wrapped + * callback. + * @param cb supplies the callback to invoke when the SchedulableCallback is triggered on the + * event loop. + */ + virtual Event::SchedulableCallbackPtr createSchedulableCallback(std::function cb) PURE; + + /** + * Appends a tracked object to the current stack of tracked objects operating + * in the dispatcher. + * + * It's recommended to use ScopeTrackerScopeState to manage the object's tracking. If directly + * invoking, there needs to be a subsequent call to popTrackedObject(). + */ + virtual void pushTrackedObject(const ScopeTrackedObject* object) PURE; + + /** + * Removes the top of the stack of tracked object and asserts that it was expected. + */ + virtual void popTrackedObject(const ScopeTrackedObject* expected_object) PURE; + + /** + * Validates that an operation is thread-safe with respect to this dispatcher; i.e. that the + * current thread of execution is on the same thread upon which the dispatcher loop is running. + */ + virtual bool isThreadSafe() const PURE; + + /** + * Returns a recently cached MonotonicTime value. + */ + virtual MonotonicTime approximateMonotonicTime() const PURE; +}; + +/** + * Abstract event dispatching loop. + */ +class Dispatcher : public DispatcherBase { +public: + /** + * Returns the name that identifies this dispatcher, such as "worker_2" or "main_thread". + * @return const std::string& the name that identifies this dispatcher. + */ + virtual const std::string& name() PURE; + + /** + * Register a watchdog for this dispatcher. The dispatcher is responsible for touching the + * watchdog at least once per touch interval. Dispatcher implementations may choose to touch more + * often to avoid spurious miss events when processing long callback queues. + * @param min_touch_interval Touch interval for the watchdog. + */ + virtual void registerWatchdog(const Server::WatchDogSharedPtr& watchdog, + std::chrono::milliseconds min_touch_interval) PURE; + + /** + * Returns a time-source to use with this dispatcher. + */ + virtual TimeSource& timeSource() PURE; + + /** + * Initializes stats for this dispatcher. Note that this can't generally be done at construction + * time, since the main and worker thread dispatchers are constructed before + * ThreadLocalStoreImpl::initializeThreading. + * @param scope the scope to contain the new per-dispatcher stats created here. + * @param prefix the stats prefix to identify this dispatcher. If empty, the dispatcher will be + * identified by its name. + */ + virtual void initializeStats(Stats::Scope& scope, + const absl::optional& prefix = absl::nullopt) PURE; + + /** + * Clears any items in the deferred deletion queue. + */ + virtual void clearDeferredDeleteList() PURE; + + /** + * Wraps an already-accepted socket in an instance of Envoy's server Network::Connection. + * @param socket supplies an open file descriptor and connection metadata to use for the + * connection. Takes ownership of the socket. + * @param transport_socket supplies a transport socket to be used by the connection. + * @param stream_info info object for the server connection + * @return Network::ConnectionPtr a server connection that is owned by the caller. + */ + virtual Network::ServerConnectionPtr + createServerConnection(Network::ConnectionSocketPtr&& socket, + Network::TransportSocketPtr&& transport_socket, + StreamInfo::StreamInfo& stream_info) PURE; + + /** + * Creates an instance of Envoy's Network::ClientConnection. Does NOT initiate the connection; + * the caller must then call connect() on the returned Network::ClientConnection. + * @param address supplies the address to connect to. + * @param source_address supplies an address to bind to or nullptr if no bind is necessary. + * @param transport_socket supplies a transport socket to be used by the connection. + * @param options the socket options to be set on the underlying socket before anything is sent + * on the socket. + * @return Network::ClientConnectionPtr a client connection that is owned by the caller. + */ + virtual Network::ClientConnectionPtr + createClientConnection(Network::Address::InstanceConstSharedPtr address, + Network::Address::InstanceConstSharedPtr source_address, + Network::TransportSocketPtr&& transport_socket, + const Network::ConnectionSocket::OptionsSharedPtr& options) PURE; + + /** + * Creates an async DNS resolver. The resolver should only be used on the thread that runs this + * dispatcher. + * @param resolvers supplies the addresses of DNS resolvers that this resolver should use. If left + * empty, it will not use any specific resolvers, but use defaults (/etc/resolv.conf) + * @param use_tcp_for_dns_lookups if set to true, tcp will be used to perform dns lookups. + * Otherwise, udp is used. + * @return Network::DnsResolverSharedPtr that is owned by the caller. + */ + virtual Network::DnsResolverSharedPtr + createDnsResolver(const std::vector& resolvers, + bool use_tcp_for_dns_lookups) PURE; + + /** + * @return Filesystem::WatcherPtr a filesystem watcher owned by the caller. + */ + virtual Filesystem::WatcherPtr createFilesystemWatcher() PURE; + + /** + * Creates a listener on a specific port. + * @param socket supplies the socket to listen on. + * @param cb supplies the callbacks to invoke for listener events. + * @param bind_to_port controls whether the listener binds to a transport port or not. + * @param backlog_size controls listener pending connections backlog + * @return Network::ListenerPtr a new listener that is owned by the caller. + */ + virtual Network::ListenerPtr createListener(Network::SocketSharedPtr&& socket, + Network::TcpListenerCallbacks& cb, bool bind_to_port, + uint32_t backlog_size) PURE; /** - * Abstract event dispatching loop. + * Creates a logical udp listener on a specific port. + * @param socket supplies the socket to listen on. + * @param cb supplies the udp listener callbacks to invoke for listener events. + * @return Network::ListenerPtr a new listener that is owned by the caller. */ - class Dispatcher : public DispatcherBase { - public: - /** - * Returns the name that identifies this dispatcher, such as "worker_2" or "main_thread". - * @return const std::string& the name that identifies this dispatcher. - */ - virtual const std::string& name() PURE; - - /** - * Register a watchdog for this dispatcher. The dispatcher is responsible for touching the - * watchdog at least once per touch interval. Dispatcher implementations may choose to touch - * more often to avoid spurious miss events when processing long callback queues. - * @param min_touch_interval Touch interval for the watchdog. - */ - virtual void registerWatchdog(const Server::WatchDogSharedPtr& watchdog, - std::chrono::milliseconds min_touch_interval) PURE; - - /** - * Returns a time-source to use with this dispatcher. - */ - virtual TimeSource& timeSource() PURE; - - /** - * Initializes stats for this dispatcher. Note that this can't generally be done at construction - * time, since the main and worker thread dispatchers are constructed before - * ThreadLocalStoreImpl::initializeThreading. - * @param scope the scope to contain the new per-dispatcher stats created here. - * @param prefix the stats prefix to identify this dispatcher. If empty, the dispatcher will be - * identified by its name. - */ - virtual void initializeStats(Stats::Scope& scope, - const absl::optional& prefix = absl::nullopt) PURE; - - /** - * Clears any items in the deferred deletion queue. - */ - virtual void clearDeferredDeleteList() PURE; - - /** - * Wraps an already-accepted socket in an instance of Envoy's server Network::Connection. - * @param socket supplies an open file descriptor and connection metadata to use for the - * connection. Takes ownership of the socket. - * @param transport_socket supplies a transport socket to be used by the connection. - * @param stream_info info object for the server connection - * @return Network::ConnectionPtr a server connection that is owned by the caller. - */ - virtual Network::ServerConnectionPtr - createServerConnection(Network::ConnectionSocketPtr&& socket, - Network::TransportSocketPtr&& transport_socket, - StreamInfo::StreamInfo& stream_info) PURE; - - /** - * Creates an instance of Envoy's Network::ClientConnection. Does NOT initiate the connection; - * the caller must then call connect() on the returned Network::ClientConnection. - * @param address supplies the address to connect to. - * @param source_address supplies an address to bind to or nullptr if no bind is necessary. - * @param transport_socket supplies a transport socket to be used by the connection. - * @param options the socket options to be set on the underlying socket before anything is sent - * on the socket. - * @return Network::ClientConnectionPtr a client connection that is owned by the caller. - */ - virtual Network::ClientConnectionPtr - createClientConnection(Network::Address::InstanceConstSharedPtr address, - Network::Address::InstanceConstSharedPtr source_address, - Network::TransportSocketPtr&& transport_socket, - const Network::ConnectionSocket::OptionsSharedPtr& options) PURE; - - /** - * Creates an async DNS resolver. The resolver should only be used on the thread that runs this - * dispatcher. - * @param resolvers supplies the addresses of DNS resolvers that this resolver should use. If - * left empty, it will not use any specific resolvers, but use defaults (/etc/resolv.conf) - * @param use_tcp_for_dns_lookups if set to true, tcp will be used to perform dns lookups. - * Otherwise, udp is used. - * @return Network::DnsResolverSharedPtr that is owned by the caller. - */ - virtual Network::DnsResolverSharedPtr - createDnsResolver(const std::vector& resolvers, - bool use_tcp_for_dns_lookups) PURE; - - /** - * @return Filesystem::WatcherPtr a filesystem watcher owned by the caller. - */ - virtual Filesystem::WatcherPtr createFilesystemWatcher() PURE; - - /** - * Creates a listener on a specific port. - * @param socket supplies the socket to listen on. - * @param cb supplies the callbacks to invoke for listener events. - * @param bind_to_port controls whether the listener binds to a transport port or not. - * @param backlog_size controls listener pending connections backlog - * @return Network::ListenerPtr a new listener that is owned by the caller. - */ - virtual Network::ListenerPtr createListener(Network::SocketSharedPtr&& socket, - Network::TcpListenerCallbacks& cb, - bool bind_to_port, uint32_t backlog_size) PURE; - - /** - * Creates a logical udp listener on a specific port. - * @param socket supplies the socket to listen on. - * @param cb supplies the udp listener callbacks to invoke for listener events. - * @return Network::ListenerPtr a new listener that is owned by the caller. - */ - virtual Network::UdpListenerPtr createUdpListener(Network::SocketSharedPtr socket, - Network::UdpListenerCallbacks& cb) PURE; - /** - * Submits an item for deferred delete. @see DeferredDeletable. - */ - virtual void deferredDelete(DeferredDeletablePtr&& to_delete) PURE; - - /** - * Exits the event loop. - */ - virtual void exit() PURE; - - /** - * Listens for a signal event. Only a single dispatcher in the process can listen for signals. - * If more than one dispatcher calls this routine in the process the behavior is undefined. - * - * @param signal_num supplies the signal to listen on. - * @param cb supplies the callback to invoke when the signal fires. - * @return SignalEventPtr a signal event that is owned by the caller. - */ - virtual SignalEventPtr listenForSignal(signal_t signal_num, SignalCb cb) PURE; - - /** - * Posts a functor to the dispatcher. This is safe cross thread. The functor runs in the context - * of the dispatcher event loop which may be on a different thread than the caller. - */ - virtual void post(PostCb callback) PURE; - - /** - * Runs the event loop. This will not return until exit() is called either from within a - * callback or from a different thread. - * @param type specifies whether to run in blocking mode (run() will not return until exit() is - * called) or non-blocking mode where only active events will be executed and then - * run() will return. - */ - enum class RunType { - Block, // Runs the event-loop until there are no pending events. - NonBlock, // Checks for any pending events to activate, executes them, - // then exits. Exits immediately if there are no pending or - // active events. - RunUntilExit // Runs the event-loop until loopExit() is called, blocking - // until there are pending or active events. - }; - virtual void run(RunType type) PURE; - - /** - * Returns a factory which connections may use for watermark buffer creation. - * @return the watermark buffer factory for this dispatcher. - */ - virtual Buffer::WatermarkFactory& getWatermarkFactory() PURE; - - /** - * Updates approximate monotonic time to current value. - */ - virtual void updateApproximateMonotonicTime() PURE; + virtual Network::UdpListenerPtr createUdpListener(Network::SocketSharedPtr socket, + Network::UdpListenerCallbacks& cb) PURE; + /** + * Submits an item for deferred delete. @see DeferredDeletable. + */ + virtual void deferredDelete(DeferredDeletablePtr&& to_delete) PURE; + + /** + * Exits the event loop. + */ + virtual void exit() PURE; + + /** + * Listens for a signal event. Only a single dispatcher in the process can listen for signals. + * If more than one dispatcher calls this routine in the process the behavior is undefined. + * + * @param signal_num supplies the signal to listen on. + * @param cb supplies the callback to invoke when the signal fires. + * @return SignalEventPtr a signal event that is owned by the caller. + */ + virtual SignalEventPtr listenForSignal(signal_t signal_num, SignalCb cb) PURE; + + /** + * Posts a functor to the dispatcher. This is safe cross thread. The functor runs in the context + * of the dispatcher event loop which may be on a different thread than the caller. + */ + virtual void post(PostCb callback) PURE; + + /** + * Runs the event loop. This will not return until exit() is called either from within a callback + * or from a different thread. + * @param type specifies whether to run in blocking mode (run() will not return until exit() is + * called) or non-blocking mode where only active events will be executed and then + * run() will return. + */ + enum class RunType { + Block, // Runs the event-loop until there are no pending events. + NonBlock, // Checks for any pending events to activate, executes them, + // then exits. Exits immediately if there are no pending or + // active events. + RunUntilExit // Runs the event-loop until loopExit() is called, blocking + // until there are pending or active events. }; + virtual void run(RunType type) PURE; - using DispatcherPtr = std::unique_ptr; + /** + * Returns a factory which connections may use for watermark buffer creation. + * @return the watermark buffer factory for this dispatcher. + */ + virtual Buffer::WatermarkFactory& getWatermarkFactory() PURE; + + /** + * Updates approximate monotonic time to current value. + */ + virtual void updateApproximateMonotonicTime() PURE; +}; + +using DispatcherPtr = std::unique_ptr; } // namespace Event -} // namespace Event +} // namespace Envoy