From a26a17595791b86f6f9258b5340ef90a7e8f339e Mon Sep 17 00:00:00 2001 From: Nathan Nichols Date: Fri, 17 Jul 2020 10:50:27 -0500 Subject: [PATCH] Add GRPCServer to Stoppable Hierarchy --- src/ripple/app/main/Application.cpp | 3 +-- src/ripple/app/main/GRPCServer.cpp | 17 ++++++++++++++--- src/ripple/app/main/GRPCServer.h | 17 ++++++++++++----- src/ripple/core/Stoppable.h | 17 +++++++++-------- 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/ripple/app/main/Application.cpp b/src/ripple/app/main/Application.cpp index e87fa586e..2094d5194 100644 --- a/src/ripple/app/main/Application.cpp +++ b/src/ripple/app/main/Application.cpp @@ -439,7 +439,7 @@ public: logs_->journal("Application"), std::chrono::milliseconds(100), get_io_service()) - , grpcServer_(std::make_unique(*this)) + , grpcServer_(std::make_unique(*this, *m_jobQueue)) { add(m_resourceManager.get()); @@ -1316,7 +1316,6 @@ ApplicationImp::setup() logs_->silent(config_->silent()); m_jobQueue->setThreadCount(config_->WORKERS, config_->standalone()); - grpcServer_->run(); if (!config_->standalone()) timeKeeper_->run(config_->SNTP_SERVERS); diff --git a/src/ripple/app/main/GRPCServer.cpp b/src/ripple/app/main/GRPCServer.cpp index 007e5f0c4..a0f1b483c 100644 --- a/src/ripple/app/main/GRPCServer.cpp +++ b/src/ripple/app/main/GRPCServer.cpp @@ -18,6 +18,7 @@ //============================================================================== #include +#include #include namespace ripple { @@ -438,25 +439,35 @@ GRPCServerImpl::start() } void -GRPCServer::run() +GRPCServer::onStart() { // Start the server and setup listeners - if ((running_ = impl_.start())) + if (running_ = impl_.start(); running_) { thread_ = std::thread([this]() { // Start the event loop and begin handling requests + beast::setCurrentThreadName("rippled: grpc"); this->impl_.handleRpcs(); }); } } -GRPCServer::~GRPCServer() +void +GRPCServer::onStop() { if (running_) { impl_.shutdown(); thread_.join(); + running_ = false; } + + stopped(); +} + +GRPCServer::~GRPCServer() +{ + assert(!running_); } } // namespace ripple diff --git a/src/ripple/app/main/GRPCServer.h b/src/ripple/app/main/GRPCServer.h index bb06784c2..032a6ba46 100644 --- a/src/ripple/app/main/GRPCServer.h +++ b/src/ripple/app/main/GRPCServer.h @@ -22,6 +22,7 @@ #include #include +#include #include #include #include @@ -234,10 +235,13 @@ private: }; // GRPCServerImpl -class GRPCServer +class GRPCServer : public Stoppable { public: - explicit GRPCServer(Application& app) : impl_(app){}; + explicit GRPCServer(Application& app, Stoppable& parent) + : Stoppable("GRPCServer", parent), impl_(app) + { + } GRPCServer(const GRPCServer&) = delete; @@ -245,14 +249,17 @@ public: operator=(const GRPCServer&) = delete; void - run(); + onStart() override; - ~GRPCServer(); + void + onStop() override; + + ~GRPCServer() override; private: GRPCServerImpl impl_; std::thread thread_; - bool running_; + bool running_ = false; }; } // namespace ripple #endif diff --git a/src/ripple/core/Stoppable.h b/src/ripple/core/Stoppable.h index 705d09a7e..cf2395653 100644 --- a/src/ripple/core/Stoppable.h +++ b/src/ripple/core/Stoppable.h @@ -173,7 +173,7 @@ class RootStoppable; @note A Stoppable may not be restarted. The form of the Stoppable tree in the rippled application evolves as - the source code changes and reacts to new demands. As of March in 2017 + the source code changes and reacts to new demands. As of July in 2020 the Stoppable tree had this form: @code @@ -186,13 +186,14 @@ class RootStoppable; | JobQueue | - +--------+-----------+-----------+-----------+-------+---+----------+ - | | | | | | | - | NetworkOPs | InboundLedgers | OrderbookDB | - | | | | - Overlay InboundTransactions LedgerMaster Database - | | | - PeerFinder LedgerCleaner TaskQueue + +------+---------+--------+----------+---+-----------+--+---+ + | | | | | | | | + | NetworkOPs | InboundLedgers | | OrderbookDB | + | | | GRPCServer | + | | | Database + Overlay InboundTransactions LedgerMaster | + | | | + PeerFinder LedgerCleaner TaskQueue @endcode */