3#include "data/BackendInterface.hpp"
5#include "etl/LedgerPublisherInterface.hpp"
6#include "etl/SystemState.hpp"
7#include "etl/impl/Loading.hpp"
8#include "feed/SubscriptionManagerInterface.hpp"
9#include "util/Assert.hpp"
10#include "util/Mutex.hpp"
11#include "util/async/AnyExecutionContext.hpp"
12#include "util/async/AnyStrand.hpp"
13#include "util/log/Logger.hpp"
14#include "util/prometheus/Counter.hpp"
15#include "util/prometheus/Prometheus.hpp"
17#include <boost/asio/io_context.hpp>
18#include <boost/asio/post.hpp>
19#include <boost/asio/strand.hpp>
20#include <fmt/format.h>
21#include <xrpl/basics/chrono.h>
22#include <xrpl/protocol/Fees.h>
23#include <xrpl/protocol/LedgerHeader.h>
24#include <xrpl/protocol/SField.h>
25#include <xrpl/protocol/STObject.h>
26#include <xrpl/protocol/Serializer.h>
37#include <shared_mutex>
62 std::atomic_bool stop_{
false};
64 std::shared_ptr<BackendInterface> backend_;
65 std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions_;
66 std::reference_wrapper<SystemState const> state_;
70 std::reference_wrapper<util::prometheus::CounterInt> lastPublishSeconds_ =
72 "etl_last_publish_seconds",
74 "Seconds since epoch of the last published ledger"
85 std::shared_ptr<BackendInterface> backend,
86 std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions,
89 : publishStrand_{ctx.makeStrand()}
90 , backend_{std::move(backend)}
91 , subscriptions_{std::move(subscriptions)}
92 , state_{std::cref(state)}
107 uint32_t ledgerSequence,
108 std::optional<uint32_t> maxAttempts,
109 std::chrono::steady_clock::duration attemptsDelay = std::chrono::seconds{1}
112 LOG(log_.
info()) <<
"Attempting to publish ledger = " << ledgerSequence;
113 size_t numAttempts = 0;
115 auto range = backend_->hardFetchLedgerRangeNoThrow();
117 if (!range || range->maxSequence < ledgerSequence) {
119 LOG(log_.
debug()) <<
"Trying to publish. Could not find ledger with sequence = "
124 if (maxAttempts && numAttempts >= maxAttempts) {
126 <<
"Failed to publish ledger after " << numAttempts <<
" attempts.";
129 std::this_thread::sleep_for(attemptsDelay);
134 return backend_->fetchLedgerBySequence(ledgerSequence, yield);
139 "Ledger must exist in database. Ledger sequence = {}",
162 publishStrand_.submit([
this, lgrInfo = lgrInfo] {
163 LOG(log_.info()) <<
"Publishing ledger " << std::to_string(lgrInfo.seq);
165 setLastClose(lgrInfo.closeTime);
170 static constexpr std::uint32_t kMaxLedgerAgeSeconds = 600;
171 if (age < kMaxLedgerAgeSeconds) {
172 std::optional<ripple::Fees> fees =
174 return backend_->fetchFees(lgrInfo.seq, yield);
176 ASSERT(fees.has_value(),
"Fees must exist for ledger {}", lgrInfo.seq);
179 return backend_->fetchAllTransactionsInLedger(lgrInfo.seq, yield);
182 auto const ledgerRange = backend_->fetchLedgerRange();
183 ASSERT(ledgerRange.has_value(),
"Ledger range must exist");
186 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>(
230 std::chrono::time_point<std::chrono::system_clock>
233 return std::chrono::time_point<std::chrono::system_clock>{
234 std::chrono::seconds{lastPublishSeconds_.get().value()}
244 auto closeTime = lastCloseTime_.lock()->time_since_epoch().count();
245 auto now = std::chrono::duration_cast<std::chrono::seconds>(
246 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 =
289 duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
290 lastPublishSeconds_.get().set(nowSeconds);
294 setLastPublishedSequence(std::optional<uint32_t> lastPublishedSequence)
296 auto lastPublishSeq = lastPublishedSequence_.lock();
297 *lastPublishSeq = lastPublishedSequence;
static constexpr std::uint32_t kRippleEpochStart
The ripple epoch start timestamp. Midnight on 1st January 2000.
Definition DBHelpers.hpp:273
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:211
std::chrono::time_point< std::chrono::system_clock > getLastPublish() const override
Get last publish time as a time point.
Definition LedgerPublisher.hpp:231
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:83
void publish(ripple::LedgerHeader const &lgrInfo)
Publish the passed ledger asynchronously.
Definition LedgerPublisher.hpp:160
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:106
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:242
std::uint32_t lastPublishAgeSeconds() const override
Get time passed since last publish, in seconds.
Definition LedgerPublisher.hpp:219
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:78
Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::DBG severity.
Definition Logger.cpp:497
Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::NFO severity.
Definition Logger.cpp:502
A container for data that is protected by a mutex. Inspired by Mutex in Rust.
Definition Mutex.hpp:82
Lock< ProtectedDataType const, LockType, MutexType > lock() const
Lock the mutex and get a lock object allowing access to the protected data.
Definition Mutex.hpp:120
A type-erased execution context.
Definition AnyExecutionContext.hpp:22
A type-erased execution context.
Definition AnyStrand.hpp:21
auto synchronousAndRetryOnTimeout(FnType &&func)
Synchronously execute the given function object and retry until no DatabaseTimeout is thrown.
Definition BackendInterface.hpp:117
The interface of a scheduler for the extraction process.
Definition LedgerPublisherInterface.hpp:12
Represents the state of the ETL subsystem.
Definition SystemState.hpp:20