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>
38#include <xrpl/basics/chrono.h>
39#include <xrpl/protocol/Fees.h>
40#include <xrpl/protocol/LedgerHeader.h>
41#include <xrpl/protocol/SField.h>
42#include <xrpl/protocol/STObject.h>
43#include <xrpl/protocol/Serializer.h>
53#include <shared_mutex>
59namespace etlng::impl {
75 boost::asio::strand<boost::asio::io_context::executor_type> publishStrand_;
77 std::shared_ptr<BackendInterface> backend_;
78 std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions_;
79 std::reference_wrapper<etl::SystemState const> state_;
84 "etl_last_publish_seconds",
86 "Seconds since epoch of the last published ledger"
96 boost::asio::io_context& ioc,
97 std::shared_ptr<BackendInterface> backend,
98 std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions,
101 : publishStrand_{boost::asio::make_strand(ioc)}
102 , backend_{std::move(backend)}
103 , subscriptions_{std::move(subscriptions)}
104 , state_{std::cref(state)}
119 uint32_t ledgerSequence,
120 std::optional<uint32_t> maxAttempts,
121 std::chrono::steady_clock::duration attemptsDelay = std::chrono::seconds{1}
124 LOG(log_.
info()) <<
"Attempting to publish ledger = " << ledgerSequence;
125 size_t numAttempts = 0;
126 while (not state_.get().isStopping) {
127 auto range = backend_->hardFetchLedgerRangeNoThrow();
129 if (!range || range->maxSequence < ledgerSequence) {
131 LOG(log_.
debug()) <<
"Trying to publish. Could not find ledger with sequence = " << ledgerSequence;
134 if (maxAttempts && numAttempts >= maxAttempts) {
135 LOG(log_.
debug()) <<
"Failed to publish ledger after " << numAttempts <<
" attempts.";
138 std::this_thread::sleep_for(attemptsDelay);
143 return backend_->fetchLedgerBySequence(ledgerSequence, yield);
146 ASSERT(lgr.has_value(),
"Ledger must exist in database. Ledger sequence = {}", ledgerSequence);
164 boost::asio::post(publishStrand_, [
this, lgrInfo = lgrInfo]() {
165 LOG(log_.
info()) <<
"Publishing ledger " << std::to_string(lgrInfo.seq);
168 if (not state_.get().isWriting)
169 backend_->updateRange(lgrInfo.seq);
171 setLastClose(lgrInfo.closeTime);
175 static constexpr std::uint32_t kMAX_LEDGER_AGE_SECONDS = 600;
176 if (age < kMAX_LEDGER_AGE_SECONDS) {
178 return backend_->fetchFees(lgrInfo.seq, yield);
180 ASSERT(fees.has_value(),
"Fees must exist for ledger {}", lgrInfo.seq);
183 return backend_->fetchAllTransactionsInLedger(lgrInfo.seq, yield);
186 auto const ledgerRange = backend_->fetchLedgerRange();
187 ASSERT(ledgerRange.has_value(),
"Ledger range must exist");
189 auto const range = fmt::format(
"{}-{}", ledgerRange->minSequence, ledgerRange->maxSequence);
190 subscriptions_->pubLedger(lgrInfo, *fees, range, transactions.size());
193 std::ranges::sort(transactions, [](
auto const& t1,
auto const& t2) {
194 ripple::SerialIter iter1{t1.metadata.data(), t1.metadata.size()};
195 ripple::STObject
const object1(iter1, ripple::sfMetadata);
196 ripple::SerialIter iter2{t2.metadata.data(), t2.metadata.size()};
197 ripple::STObject
const object2(iter2, ripple::sfMetadata);
198 return object1.getFieldU32(ripple::sfTransactionIndex) <
199 object2.getFieldU32(ripple::sfTransactionIndex);
202 for (
auto const& txAndMeta : transactions)
203 subscriptions_->pubTransaction(txAndMeta, lgrInfo);
205 subscriptions_->pubBookChanges(lgrInfo, transactions);
207 setLastPublishTime();
208 LOG(log_.
info()) <<
"Published ledger " << lgrInfo.seq;
210 LOG(log_.
info()) <<
"Skipping publishing ledger " << lgrInfo.seq;
215 setLastPublishedSequence(lgrInfo.seq);
224 return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() -
getLastPublish())
231 std::chrono::time_point<std::chrono::system_clock>
234 return std::chrono::time_point<std::chrono::system_clock>{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>(std::chrono::system_clock::now().time_since_epoch())
256 std::optional<uint32_t>
259 return *lastPublishedSequence_.lock();
264 setLastClose(std::chrono::time_point<ripple::NetClock> lastCloseTime)
266 auto closeTime = lastCloseTime_.
lock<std::scoped_lock>();
267 *closeTime = lastCloseTime;
273 using namespace std::chrono;
274 auto const nowSeconds = duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
275 lastPublishSeconds_.get().set(nowSeconds);
279 setLastPublishedSequence(std::optional<uint32_t> lastPublishedSequence)
281 auto lastPublishSeq = lastPublishedSequence_.lock();
282 *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:72
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:257
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:95
void publish(ripple::LedgerHeader const &lgrInfo)
Publish the passed ledger asynchronously.
Definition LedgerPublisher.hpp:162
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:222
std::chrono::time_point< std::chrono::system_clock > getLastPublish() const override
Get last publish time as a time point.
Definition LedgerPublisher.hpp:232
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:118
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