22#include "data/BackendInterface.hpp"
24#include "etl/LedgerPublisherInterface.hpp"
25#include "etl/SystemState.hpp"
26#include "etl/impl/Loading.hpp"
27#include "feed/SubscriptionManagerInterface.hpp"
28#include "util/Assert.hpp"
29#include "util/Mutex.hpp"
30#include "util/async/AnyExecutionContext.hpp"
31#include "util/async/AnyStrand.hpp"
32#include "util/log/Logger.hpp"
33#include "util/prometheus/Counter.hpp"
34#include "util/prometheus/Prometheus.hpp"
36#include <boost/asio/io_context.hpp>
37#include <boost/asio/post.hpp>
38#include <boost/asio/strand.hpp>
39#include <fmt/format.h>
40#include <xrpl/basics/chrono.h>
41#include <xrpl/protocol/Fees.h>
42#include <xrpl/protocol/LedgerHeader.h>
43#include <xrpl/protocol/SField.h>
44#include <xrpl/protocol/STObject.h>
45#include <xrpl/protocol/Serializer.h>
56#include <shared_mutex>
80 std::atomic_bool stop_{
false};
82 std::shared_ptr<BackendInterface> backend_;
83 std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions_;
84 std::reference_wrapper<SystemState const> state_;
89 "etl_last_publish_seconds",
91 "Seconds since epoch of the last published ledger"
102 std::shared_ptr<BackendInterface> backend,
103 std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions,
106 : publishStrand_{ctx.makeStrand()}
107 , backend_{std::move(backend)}
108 , subscriptions_{std::move(subscriptions)}
109 , state_{std::cref(state)}
124 uint32_t ledgerSequence,
125 std::optional<uint32_t> maxAttempts,
126 std::chrono::steady_clock::duration attemptsDelay = std::chrono::seconds{1}
129 LOG(log_.
info()) <<
"Attempting to publish ledger = " << ledgerSequence;
130 size_t numAttempts = 0;
132 auto range = backend_->hardFetchLedgerRangeNoThrow();
134 if (!range || range->maxSequence < ledgerSequence) {
136 LOG(log_.
debug()) <<
"Trying to publish. Could not find ledger with sequence = " << ledgerSequence;
139 if (maxAttempts && numAttempts >= maxAttempts) {
140 LOG(log_.
debug()) <<
"Failed to publish ledger after " << numAttempts <<
" attempts.";
143 std::this_thread::sleep_for(attemptsDelay);
148 return backend_->fetchLedgerBySequence(ledgerSequence, yield);
151 ASSERT(lgr.has_value(),
"Ledger must exist in database. Ledger sequence = {}", ledgerSequence);
169 publishStrand_.submit([
this, lgrInfo = lgrInfo] {
170 LOG(log_.info()) <<
"Publishing ledger " << std::to_string(lgrInfo.seq);
172 setLastClose(lgrInfo.closeTime);
176 static constexpr std::uint32_t kMAX_LEDGER_AGE_SECONDS = 600;
177 if (age < kMAX_LEDGER_AGE_SECONDS) {
179 return backend_->fetchFees(lgrInfo.seq, yield);
181 ASSERT(fees.has_value(),
"Fees must exist for ledger {}", lgrInfo.seq);
184 return backend_->fetchAllTransactionsInLedger(lgrInfo.seq, yield);
187 auto const ledgerRange = backend_->fetchLedgerRange();
188 ASSERT(ledgerRange.has_value(),
"Ledger range must exist");
190 auto const range = fmt::format(
"{}-{}", ledgerRange->minSequence, ledgerRange->maxSequence);
191 subscriptions_->pubLedger(lgrInfo, *fees, range, transactions.size());
194 std::ranges::sort(transactions, [](
auto const& t1,
auto const& t2) {
195 ripple::SerialIter iter1{t1.metadata.data(), t1.metadata.size()};
196 ripple::STObject
const object1(iter1, ripple::sfMetadata);
197 ripple::SerialIter iter2{t2.metadata.data(), t2.metadata.size()};
198 ripple::STObject
const object2(iter2, ripple::sfMetadata);
199 return object1.getFieldU32(ripple::sfTransactionIndex) <
200 object2.getFieldU32(ripple::sfTransactionIndex);
203 for (
auto const& txAndMeta : transactions)
204 subscriptions_->pubTransaction(txAndMeta, lgrInfo);
206 subscriptions_->pubBookChanges(lgrInfo, transactions);
208 setLastPublishTime();
209 LOG(log_.info()) <<
"Published ledger " << lgrInfo.seq;
211 LOG(log_.info()) <<
"Skipping publishing ledger " << lgrInfo.seq;
216 setLastPublishedSequence(lgrInfo.seq);
225 return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() -
getLastPublish())
232 std::chrono::time_point<std::chrono::system_clock>
235 return std::chrono::time_point<std::chrono::system_clock>{
236 std::chrono::seconds{lastPublishSeconds_.get().value()}
246 auto closeTime = lastCloseTime_.lock()->time_since_epoch().count();
247 auto now = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch())
258 std::optional<uint32_t>
261 return *lastPublishedSequence_.lock();
278 setLastClose(std::chrono::time_point<ripple::NetClock> lastCloseTime)
280 auto closeTime = lastCloseTime_.
lock<std::scoped_lock>();
281 *closeTime = lastCloseTime;
287 using namespace std::chrono;
288 auto const nowSeconds = duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
289 lastPublishSeconds_.get().set(nowSeconds);
293 setLastPublishedSequence(std::optional<uint32_t> lastPublishedSequence)
295 auto lastPublishSeq = lastPublishedSequence_.lock();
296 *lastPublishSeq = lastPublishedSequence;
static constexpr std::uint32_t kRIPPLE_EPOCH_START
The ripple epoch start timestamp. Midnight on 1st January 2000.
Definition DBHelpers.hpp:272
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:200
std::chrono::time_point< std::chrono::system_clock > getLastPublish() const override
Get last publish time as a time point.
Definition LedgerPublisher.hpp:233
LedgerPublisher(util::async::AnyExecutionContext ctx, std::shared_ptr< BackendInterface > backend, std::shared_ptr< feed::SubscriptionManagerInterface > subscriptions, SystemState const &state)
Create an instance of the publisher.
Definition LedgerPublisher.hpp:100
void publish(ripple::LedgerHeader const &lgrInfo)
Publish the passed ledger asynchronously.
Definition LedgerPublisher.hpp:167
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:259
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:123
void stop()
Stops publishing.
Definition LedgerPublisher.hpp:271
std::uint32_t lastCloseAgeSeconds() const override
Get time passed since last ledger close, in seconds.
Definition LedgerPublisher.hpp:244
std::uint32_t lastPublishAgeSeconds() const override
Get time passed since last publish, in seconds.
Definition LedgerPublisher.hpp:223
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:95
Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::DBG severity.
Definition Logger.cpp:481
Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::NFO severity.
Definition Logger.cpp:486
A container for data that is protected by a mutex. Inspired by Mutex in Rust.
Definition Mutex.hpp:101
Lock< ProtectedDataType const, LockType, MutexType > lock() const
Lock the mutex and get a lock object allowing access to the protected data.
Definition Mutex.hpp:139
A type-erased execution context.
Definition AnyExecutionContext.hpp:41
A type-erased execution context.
Definition AnyStrand.hpp:40
auto synchronousAndRetryOnTimeout(FnType &&func)
Synchronously execute the given function object and retry until no DatabaseTimeout is thrown.
Definition BackendInterface.hpp:131
The interface of a scheduler for the extraction process.
Definition LedgerPublisherInterface.hpp:31
Represents the state of the ETL subsystem.
Definition SystemState.hpp:38