Redesign stoppable object pattern

This commit is contained in:
John Freeman
2021-05-26 17:45:41 -05:00
committed by manojsdoshi
parent c10c0be11b
commit a2a37a928a
92 changed files with 781 additions and 2460 deletions

View File

@@ -25,7 +25,6 @@
#include <ripple/protocol/jss.h>
namespace ripple {
namespace detail {
/*
@@ -51,8 +50,8 @@ class LedgerCleanerImp : public LedgerCleaner
std::thread thread_;
enum class State : char { readyToClean = 0, startCleaning, cleaning };
State state_ = State::readyToClean;
enum class State : char { notCleaning = 0, cleaning };
State state_ = State::notCleaning;
bool shouldExit_ = false;
// The lowest ledger in the range we're checking.
@@ -72,34 +71,25 @@ class LedgerCleanerImp : public LedgerCleaner
//--------------------------------------------------------------------------
public:
LedgerCleanerImp(
Application& app,
Stoppable& stoppable,
beast::Journal journal)
: LedgerCleaner(stoppable), app_(app), j_(journal)
LedgerCleanerImp(Application& app, beast::Journal journal)
: app_(app), j_(journal)
{
}
~LedgerCleanerImp() override
{
if (thread_.joinable())
LogicError("LedgerCleanerImp::onStop not called.");
LogicError("LedgerCleanerImp::stop not called.");
}
//--------------------------------------------------------------------------
//
// Stoppable
//
//--------------------------------------------------------------------------
void
onStart() override
start() override
{
thread_ = std::thread{&LedgerCleanerImp::run, this};
}
void
onStop() override
stop() override
{
JLOG(j_.info()) << "Stopping";
{
@@ -142,7 +132,7 @@ public:
//--------------------------------------------------------------------------
void
doClean(Json::Value const& params) override
clean(Json::Value const& params) override
{
LedgerIndex minRange = 0;
LedgerIndex maxRange = 0;
@@ -214,11 +204,8 @@ public:
if (params.isMember(jss::stop) && params[jss::stop].asBool())
minRange_ = maxRange_ = 0;
if (state_ == State::readyToClean)
{
state_ = State::startCleaning;
wakeup_.notify_one();
}
state_ = State::cleaning;
wakeup_.notify_one();
}
}
@@ -228,36 +215,26 @@ public:
//
//--------------------------------------------------------------------------
private:
void
init()
{
JLOG(j_.debug()) << "Initializing";
}
void
run()
{
beast::setCurrentThreadName("LedgerCleaner");
JLOG(j_.debug()) << "Started";
init();
while (true)
{
{
std::unique_lock<std::mutex> lock(mutex_);
state_ = State::notCleaning;
wakeup_.wait(lock, [this]() {
return (shouldExit_ || state_ == State::startCleaning);
return (shouldExit_ || state_ == State::cleaning);
});
if (shouldExit_)
break;
state_ = State::cleaning;
assert(state_ == State::cleaning);
}
doLedgerCleaner();
}
stopped();
}
// VFALCO TODO This should return std::optional<uint256>
@@ -399,7 +376,7 @@ private:
void
doLedgerCleaner()
{
auto shouldExit = [this]() {
auto shouldExit = [this] {
std::lock_guard lock(mutex_);
return shouldExit_;
};
@@ -413,12 +390,11 @@ private:
bool doNodes;
bool doTxns;
while (app_.getFeeTrack().isLoadedLocal())
if (app_.getFeeTrack().isLoadedLocal())
{
JLOG(j_.debug()) << "Waiting for load to subside";
std::this_thread::sleep_for(std::chrono::seconds(5));
if (shouldExit())
return;
continue;
}
{
@@ -427,7 +403,6 @@ private:
(minRange_ == 0))
{
minRange_ = maxRange_ = 0;
state_ = State::readyToClean;
return;
}
ledgerIndex = maxRange_;
@@ -476,21 +451,10 @@ private:
}
};
//------------------------------------------------------------------------------
LedgerCleaner::LedgerCleaner(Stoppable& parent)
: Stoppable("LedgerCleaner", parent)
, beast::PropertyStream::Source("ledgercleaner")
{
}
LedgerCleaner::~LedgerCleaner() = default;
std::unique_ptr<LedgerCleaner>
make_LedgerCleaner(Application& app, Stoppable& parent, beast::Journal journal)
make_LedgerCleaner(Application& app, beast::Journal journal)
{
return std::make_unique<LedgerCleanerImp>(app, parent, journal);
return std::make_unique<LedgerCleanerImp>(app, journal);
}
} // namespace detail
} // namespace ripple