From 46a5bc74dbd3fe3112c2f90061266af4bf8ffb13 Mon Sep 17 00:00:00 2001 From: Ed Hennis Date: Tue, 4 Feb 2025 16:54:26 -0500 Subject: [PATCH] refactor: acquireAsync will dispatch the job, not the other way around --- src/test/app/LedgerReplay_test.cpp | 2 + src/xrpld/app/consensus/RCLConsensus.cpp | 11 ++--- src/xrpld/app/consensus/RCLValidations.cpp | 15 +++---- src/xrpld/app/ledger/InboundLedgers.h | 2 + .../app/ledger/detail/InboundLedgers.cpp | 44 ++++++++++++------- 5 files changed, 41 insertions(+), 33 deletions(-) diff --git a/src/test/app/LedgerReplay_test.cpp b/src/test/app/LedgerReplay_test.cpp index 9edaaec0a5..39ea8eb403 100644 --- a/src/test/app/LedgerReplay_test.cpp +++ b/src/test/app/LedgerReplay_test.cpp @@ -91,6 +91,8 @@ public: virtual void acquireAsync( + JobType type, + std::string const& name, uint256 const& hash, std::uint32_t seq, InboundLedger::Reason reason) override diff --git a/src/xrpld/app/consensus/RCLConsensus.cpp b/src/xrpld/app/consensus/RCLConsensus.cpp index 6823101411..cbb7a13bb2 100644 --- a/src/xrpld/app/consensus/RCLConsensus.cpp +++ b/src/xrpld/app/consensus/RCLConsensus.cpp @@ -118,15 +118,12 @@ RCLConsensus::Adaptor::acquireLedger(LedgerHash const& hash) // Tell the ledger acquire system that we need the consensus ledger acquiringLedger_ = hash; - app_.getJobQueue().addJob( + app_.getInboundLedgers().acquireAsync( jtADVANCE, "getConsensusLedger1", - [id = hash, &app = app_, this]() { - JLOG(j_.debug()) - << "JOB advanceLedger getConsensusLedger1 started"; - app.getInboundLedgers().acquireAsync( - id, 0, InboundLedger::Reason::CONSENSUS); - }); + hash, + 0, + InboundLedger::Reason::CONSENSUS); } return std::nullopt; } diff --git a/src/xrpld/app/consensus/RCLValidations.cpp b/src/xrpld/app/consensus/RCLValidations.cpp index d6a8747c20..ab3e61b900 100644 --- a/src/xrpld/app/consensus/RCLValidations.cpp +++ b/src/xrpld/app/consensus/RCLValidations.cpp @@ -120,15 +120,12 @@ RCLValidationsAdaptor::acquire(LedgerHash const& hash) JLOG(j_.warn()) << "Need validated ledger for preferred ledger analysis " << hash; - Application* pApp = &app_; - - app_.getJobQueue().addJob( - jtADVANCE, "getConsensusLedger2", [pApp, hash, this]() { - JLOG(j_.debug()) - << "JOB advanceLedger getConsensusLedger2 started"; - pApp->getInboundLedgers().acquireAsync( - hash, 0, InboundLedger::Reason::CONSENSUS); - }); + app_.getInboundLedgers().acquireAsync( + jtADVANCE, + "getConsensusLedger2", + hash, + 0, + InboundLedger::Reason::CONSENSUS); return std::nullopt; } diff --git a/src/xrpld/app/ledger/InboundLedgers.h b/src/xrpld/app/ledger/InboundLedgers.h index 6eba2eec87..52177133d6 100644 --- a/src/xrpld/app/ledger/InboundLedgers.h +++ b/src/xrpld/app/ledger/InboundLedgers.h @@ -28,6 +28,8 @@ public: // instead. Inbound ledger acquisition is asynchronous anyway. virtual void acquireAsync( + JobType type, + std::string const& name, uint256 const& hash, std::uint32_t seq, InboundLedger::Reason reason) = 0; diff --git a/src/xrpld/app/ledger/detail/InboundLedgers.cpp b/src/xrpld/app/ledger/detail/InboundLedgers.cpp index 93f787399d..ad16c970ec 100644 --- a/src/xrpld/app/ledger/detail/InboundLedgers.cpp +++ b/src/xrpld/app/ledger/detail/InboundLedgers.cpp @@ -123,28 +123,38 @@ public: void acquireAsync( + JobType type, + std::string const& name, uint256 const& hash, std::uint32_t seq, InboundLedger::Reason reason) override { - if (CanProcess const check{acquiresMutex_, pendingAcquires_, hash}) + if (auto check = std::make_shared( + acquiresMutex_, pendingAcquires_, hash); + *check) { - try - { - acquire(hash, seq, reason); - } - catch (std::exception const& e) - { - JLOG(j_.warn()) - << "Exception thrown for acquiring new inbound ledger " - << hash << ": " << e.what(); - } - catch (...) - { - JLOG(j_.warn()) << "Unknown exception thrown for acquiring new " - "inbound ledger " - << hash; - } + app_.getJobQueue().addJob( + type, name, [check, name, hash, seq, reason, this]() { + JLOG(j_.debug()) + << "JOB acquireAsync " << name << " started "; + try + { + acquire(hash, seq, reason); + } + catch (std::exception const& e) + { + JLOG(j_.warn()) << "Exception thrown for acquiring new " + "inbound ledger " + << hash << ": " << e.what(); + } + catch (...) + { + JLOG(j_.warn()) + << "Unknown exception thrown for acquiring new " + "inbound ledger " + << hash; + } + }); } }