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>
81 std::atomic_bool stop_{
false};
83 std::shared_ptr<BackendInterface> backend_;
84 std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions_;
85 std::reference_wrapper<SystemState const> state_;
89 std::reference_wrapper<util::prometheus::CounterInt> lastPublishSeconds_ =
91 "etl_last_publish_seconds",
93 "Seconds since epoch of the last published ledger"
104 std::shared_ptr<BackendInterface> backend,
105 std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions,
108 : publishStrand_{ctx.makeStrand()}
109 , backend_{std::move(backend)}
110 , subscriptions_{std::move(subscriptions)}
111 , state_{std::cref(state)}
126 uint32_t ledgerSequence,
127 std::optional<uint32_t> maxAttempts,
128 std::chrono::steady_clock::duration attemptsDelay = std::chrono::seconds{1}
131 LOG(log_.
info()) <<
"Attempting to publish ledger = " << ledgerSequence;
132 size_t numAttempts = 0;
134 auto range = backend_->hardFetchLedgerRangeNoThrow();
136 if (!range || range->maxSequence < ledgerSequence) {
138 LOG(log_.
debug()) <<
"Trying to publish. Could not find ledger with sequence = "
143 if (maxAttempts && numAttempts >= maxAttempts) {
145 <<
"Failed to publish ledger after " << numAttempts <<
" attempts.";
148 std::this_thread::sleep_for(attemptsDelay);
153 return backend_->fetchLedgerBySequence(ledgerSequence, yield);
158 "Ledger must exist in database. Ledger sequence = {}",
179 publishStrand_.submit([
this, lgrInfo = lgrInfo] {
180 LOG(log_.info()) <<
"Publishing ledger " << std::to_string(lgrInfo.seq);
182 setLastClose(lgrInfo.closeTime);
187 static constexpr std::uint32_t kMAX_LEDGER_AGE_SECONDS = 600;
188 if (age < kMAX_LEDGER_AGE_SECONDS) {
189 std::optional<ripple::Fees> fees =
191 return backend_->fetchFees(lgrInfo.seq, yield);
193 ASSERT(fees.has_value(),
"Fees must exist for ledger {}", lgrInfo.seq);
196 return backend_->fetchAllTransactionsInLedger(lgrInfo.seq, yield);
199 auto const ledgerRange = backend_->fetchLedgerRange();
200 ASSERT(ledgerRange.has_value(),
"Ledger range must exist");
203 fmt::format(
"{}-{}", ledgerRange->minSequence, ledgerRange->maxSequence);
204 subscriptions_->pubLedger(lgrInfo, *fees, range, transactions.size());
207 std::ranges::sort(transactions, [](
auto const& t1,
auto const& t2) {
208 ripple::SerialIter iter1{t1.metadata.data(), t1.metadata.size()};
209 ripple::STObject
const object1(iter1, ripple::sfMetadata);
210 ripple::SerialIter iter2{t2.metadata.data(), t2.metadata.size()};
211 ripple::STObject
const object2(iter2, ripple::sfMetadata);
212 return object1.getFieldU32(ripple::sfTransactionIndex) <
213 object2.getFieldU32(ripple::sfTransactionIndex);
216 for (
auto const& txAndMeta : transactions)
217 subscriptions_->pubTransaction(txAndMeta, lgrInfo);
219 subscriptions_->pubBookChanges(lgrInfo, transactions);
221 setLastPublishTime();
222 LOG(log_.info()) <<
"Published ledger " << lgrInfo.seq;
224 LOG(log_.info()) <<
"Skipping publishing ledger " << lgrInfo.seq;
229 setLastPublishedSequence(lgrInfo.seq);
238 return std::chrono::duration_cast<std::chrono::seconds>(
247 std::chrono::time_point<std::chrono::system_clock>
250 return std::chrono::time_point<std::chrono::system_clock>{
251 std::chrono::seconds{lastPublishSeconds_.get().value()}
261 auto closeTime = lastCloseTime_.lock()->time_since_epoch().count();
262 auto now = std::chrono::duration_cast<std::chrono::seconds>(
263 std::chrono::system_clock::now().time_since_epoch()
275 std::optional<uint32_t>
278 return *lastPublishedSequence_.lock();
295 setLastClose(std::chrono::time_point<ripple::NetClock> lastCloseTime)
297 auto closeTime = lastCloseTime_.
lock<std::scoped_lock>();
298 *closeTime = lastCloseTime;
304 using namespace std::chrono;
305 auto const nowSeconds =
306 duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
307 lastPublishSeconds_.get().set(nowSeconds);
311 setLastPublishedSequence(std::optional<uint32_t> lastPublishedSequence)
313 auto lastPublishSeq = lastPublishedSequence_.lock();
314 *lastPublishSeq = lastPublishedSequence;
static constexpr std::uint32_t kRIPPLE_EPOCH_START
The ripple epoch start timestamp. Midnight on 1st January 2000.
Definition DBHelpers.hpp:292
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:230
std::chrono::time_point< std::chrono::system_clock > getLastPublish() const override
Get last publish time as a time point.
Definition LedgerPublisher.hpp:248
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:102
void publish(ripple::LedgerHeader const &lgrInfo)
Publish the passed ledger asynchronously.
Definition LedgerPublisher.hpp:177
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:276
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:125
void stop()
Stops publishing.
Definition LedgerPublisher.hpp:288
std::uint32_t lastCloseAgeSeconds() const override
Get time passed since last ledger close, in seconds.
Definition LedgerPublisher.hpp:259
std::uint32_t lastPublishAgeSeconds() const override
Get time passed since last publish, in seconds.
Definition LedgerPublisher.hpp:236
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:96
Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::DBG severity.
Definition Logger.cpp:502
Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::NFO severity.
Definition Logger.cpp:507
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:136
The interface of a scheduler for the extraction process.
Definition LedgerPublisherInterface.hpp:31
Represents the state of the ETL subsystem.
Definition SystemState.hpp:38