22#include "data/BackendInterface.hpp"
24#include "data/Types.hpp"
25#include "etl/SystemState.hpp"
26#include "etlng/LedgerPublisherInterface.hpp"
27#include "etlng/impl/Loading.hpp"
28#include "feed/SubscriptionManagerInterface.hpp"
29#include "util/Assert.hpp"
30#include "util/Mutex.hpp"
31#include "util/log/Logger.hpp"
32#include "util/prometheus/Counter.hpp"
33#include "util/prometheus/Prometheus.hpp"
35#include <boost/asio/io_context.hpp>
36#include <boost/asio/post.hpp>
37#include <boost/asio/strand.hpp>
39#include <xrpl/basics/chrono.h>
40#include <xrpl/protocol/Fees.h>
41#include <xrpl/protocol/LedgerHeader.h>
42#include <xrpl/protocol/SField.h>
43#include <xrpl/protocol/STObject.h>
44#include <xrpl/protocol/Serializer.h>
54#include <shared_mutex>
60namespace etlng::impl {
76 boost::asio::strand<boost::asio::io_context::executor_type> publishStrand_;
78 std::shared_ptr<BackendInterface> backend_;
79 std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions_;
80 std::reference_wrapper<etl::SystemState const> state_;
85 "etl_last_publish_seconds",
87 "Seconds since epoch of the last published ledger"
97 boost::asio::io_context& ioc,
98 std::shared_ptr<BackendInterface> backend,
99 std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions,
102 : publishStrand_{boost::asio::make_strand(ioc)}
103 , backend_{std::move(backend)}
104 , subscriptions_{std::move(subscriptions)}
105 , state_{std::cref(state)}
120 uint32_t ledgerSequence,
121 std::optional<uint32_t> maxAttempts,
122 std::chrono::steady_clock::duration attemptsDelay = std::chrono::seconds{1}
125 LOG(log_.
info()) <<
"Attempting to publish ledger = " << ledgerSequence;
126 size_t numAttempts = 0;
127 while (not state_.get().isStopping) {
128 auto range = backend_->hardFetchLedgerRangeNoThrow();
130 if (!range || range->maxSequence < ledgerSequence) {
132 LOG(log_.
debug()) <<
"Trying to publish. Could not find ledger with sequence = " << ledgerSequence;
135 if (maxAttempts && numAttempts >= maxAttempts) {
136 LOG(log_.
debug()) <<
"Failed to publish ledger after " << numAttempts <<
" attempts.";
139 std::this_thread::sleep_for(attemptsDelay);
144 return backend_->fetchLedgerBySequence(ledgerSequence, yield);
147 ASSERT(lgr.has_value(),
"Ledger must exist in database. Ledger sequence = {}", ledgerSequence);
165 boost::asio::post(publishStrand_, [
this, lgrInfo = lgrInfo]() {
166 LOG(log_.
info()) <<
"Publishing ledger " << std::to_string(lgrInfo.seq);
168 setLastClose(lgrInfo.closeTime);
172 static constexpr std::uint32_t kMAX_LEDGER_AGE_SECONDS = 600;
173 if (age < kMAX_LEDGER_AGE_SECONDS) {
175 return backend_->fetchFees(lgrInfo.seq, yield);
177 ASSERT(fees.has_value(),
"Fees must exist for ledger {}", lgrInfo.seq);
180 return backend_->fetchAllTransactionsInLedger(lgrInfo.seq, yield);
183 auto const ledgerRange = backend_->fetchLedgerRange();
184 ASSERT(ledgerRange.has_value(),
"Ledger range must exist");
186 auto const range = fmt::format(
"{}-{}", ledgerRange->minSequence, ledgerRange->maxSequence);
187 subscriptions_->pubLedger(lgrInfo, *fees, range, transactions.size());
190 std::ranges::sort(transactions, [](
auto const& t1,
auto const& t2) {
191 ripple::SerialIter iter1{t1.metadata.data(), t1.metadata.size()};
192 ripple::STObject
const object1(iter1, ripple::sfMetadata);
193 ripple::SerialIter iter2{t2.metadata.data(), t2.metadata.size()};
194 ripple::STObject
const object2(iter2, ripple::sfMetadata);
195 return object1.getFieldU32(ripple::sfTransactionIndex) <
196 object2.getFieldU32(ripple::sfTransactionIndex);
199 for (
auto const& txAndMeta : transactions)
200 subscriptions_->pubTransaction(txAndMeta, lgrInfo);
202 subscriptions_->pubBookChanges(lgrInfo, transactions);
204 setLastPublishTime();
205 LOG(log_.
info()) <<
"Published ledger " << lgrInfo.seq;
207 LOG(log_.
info()) <<
"Skipping publishing ledger " << lgrInfo.seq;
212 setLastPublishedSequence(lgrInfo.seq);
221 return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() -
getLastPublish())
228 std::chrono::time_point<std::chrono::system_clock>
231 return std::chrono::time_point<std::chrono::system_clock>{std::chrono::seconds{lastPublishSeconds_.get().value()
241 auto closeTime = lastCloseTime_.
lock()->time_since_epoch().count();
242 auto now = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch())
253 std::optional<uint32_t>
256 return *lastPublishedSequence_.lock();
261 setLastClose(std::chrono::time_point<ripple::NetClock> lastCloseTime)
263 auto closeTime = lastCloseTime_.
lock<std::scoped_lock>();
264 *closeTime = lastCloseTime;
270 using namespace std::chrono;
271 auto const nowSeconds = duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
272 lastPublishSeconds_.get().set(nowSeconds);
276 setLastPublishedSequence(std::optional<uint32_t> lastPublishedSequence)
278 auto lastPublishSeq = lastPublishedSequence_.lock();
279 *lastPublishSeq = lastPublishedSequence;
static constexpr std::uint32_t kRIPPLE_EPOCH_START
The ripple epoch start timestamp. Midnight on 1st January 2000.
Definition DBHelpers.hpp:318
static util::prometheus::CounterInt & counterInt(std::string name, util::prometheus::Labels labels, std::optional< std::string > description=std::nullopt)
Get an integer based counter metric. It will be created if it doesn't exist.
Definition Prometheus.cpp:194
Publishes ledgers in a synchronized fashion.
Definition LedgerPublisher.hpp:73
std::optional< uint32_t > getLastPublishedSequence() const
Get the sequence of the last schueduled ledger to publish, Be aware that the ledger may not have been...
Definition LedgerPublisher.hpp:254
LedgerPublisher(boost::asio::io_context &ioc, std::shared_ptr< BackendInterface > backend, std::shared_ptr< feed::SubscriptionManagerInterface > subscriptions, etl::SystemState const &state)
Create an instance of the publisher.
Definition LedgerPublisher.hpp:96
void publish(ripple::LedgerHeader const &lgrInfo)
Publish the passed ledger asynchronously.
Definition LedgerPublisher.hpp:163
std::uint32_t lastCloseAgeSeconds() const override
Get time passed since last ledger close, in seconds.
Definition LedgerPublisher.hpp:239
std::uint32_t lastPublishAgeSeconds() const override
Get time passed since last publish, in seconds.
Definition LedgerPublisher.hpp:219
std::chrono::time_point< std::chrono::system_clock > getLastPublish() const override
Get last publish time as a time point.
Definition LedgerPublisher.hpp:229
bool publish(uint32_t ledgerSequence, std::optional< uint32_t > maxAttempts, std::chrono::steady_clock::duration attemptsDelay=std::chrono::seconds{1}) override
Attempt to read the specified ledger from the database, and then publish that ledger to the ledgers s...
Definition LedgerPublisher.hpp:119
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:111
Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::DBG severity.
Definition Logger.cpp:214
Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::NFO severity.
Definition Logger.cpp:219
A container for data that is protected by a mutex. Inspired by Mutex in Rust.
Definition Mutex.hpp:96
Lock< ProtectedDataType const, LockType, MutexType > lock() const
Lock the mutex and get a lock object allowing access to the protected data.
Definition Mutex.hpp:134
auto synchronousAndRetryOnTimeout(FnType &&func)
Synchronously execute the given function object and retry until no DatabaseTimeout is thrown.
Definition BackendInterface.hpp:132
Represents the state of the ETL subsystem.
Definition SystemState.hpp:33
The interface of a scheduler for the extraction process.
Definition LedgerPublisherInterface.hpp:31