mirror of
https://github.com/XRPLF/clio.git
synced 2025-11-20 19:56:00 +00:00
Many changes around ledger_data and book_offers
* populate caches on startup * implement new algorithms for Postgres * use one shift interval instead of two * block etl at flag ledger until writes are finished * write one flag ledger ahead * abandon ledger_diff component of ledger_data
This commit is contained in:
@@ -2,8 +2,7 @@
|
||||
|
||||
namespace Backend {
|
||||
BackendIndexer::BackendIndexer(boost::json::object const& config)
|
||||
: keyShift_(config.at("keyshift").as_int64())
|
||||
, bookShift_(config.at("bookshift").as_int64())
|
||||
: shift_(config.at("indexer_shift").as_int64())
|
||||
{
|
||||
work_.emplace(ioc_);
|
||||
ioThread_ = std::thread{[this]() { ioc_.run(); }};
|
||||
@@ -19,11 +18,12 @@ void
|
||||
BackendIndexer::addKey(ripple::uint256 const& key)
|
||||
{
|
||||
keys.insert(key);
|
||||
keysCumulative.insert(key);
|
||||
}
|
||||
void
|
||||
BackendIndexer::deleteKey(ripple::uint256 const& key)
|
||||
{
|
||||
keys.erase(key);
|
||||
keysCumulative.erase(key);
|
||||
}
|
||||
|
||||
void
|
||||
@@ -31,46 +31,108 @@ BackendIndexer::addBookOffer(
|
||||
ripple::uint256 const& book,
|
||||
ripple::uint256 const& offerKey)
|
||||
{
|
||||
booksToOffers[book].insert(offerKey);
|
||||
books[book].insert(offerKey);
|
||||
booksCumulative[book].insert(offerKey);
|
||||
}
|
||||
void
|
||||
BackendIndexer::deleteBookOffer(
|
||||
ripple::uint256 const& book,
|
||||
ripple::uint256 const& offerKey)
|
||||
{
|
||||
booksToOffers[book].erase(offerKey);
|
||||
booksToDeletedOffers[book].insert(offerKey);
|
||||
booksCumulative[book].erase(offerKey);
|
||||
}
|
||||
|
||||
void
|
||||
BackendIndexer::clearCaches()
|
||||
{
|
||||
keysCumulative = {};
|
||||
booksCumulative = {};
|
||||
}
|
||||
void
|
||||
BackendIndexer::populateCaches(BackendInterface const& backend)
|
||||
{
|
||||
if (keysCumulative.size() > 0)
|
||||
{
|
||||
BOOST_LOG_TRIVIAL(info)
|
||||
<< __func__ << " caches already populated. returning";
|
||||
return;
|
||||
}
|
||||
auto tip = backend.fetchLatestLedgerSequence();
|
||||
if (!tip)
|
||||
return;
|
||||
std::optional<ripple::uint256> cursor;
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
auto [objects, curCursor] =
|
||||
backend.fetchLedgerPage(cursor, *tip, 2048);
|
||||
BOOST_LOG_TRIVIAL(debug) << __func__ << " fetched a page";
|
||||
cursor = curCursor;
|
||||
for (auto& obj : objects)
|
||||
{
|
||||
keysCumulative.insert(obj.key);
|
||||
if (isOffer(obj.blob))
|
||||
{
|
||||
auto book = getBook(obj.blob);
|
||||
booksCumulative[book].insert(obj.key);
|
||||
}
|
||||
}
|
||||
if (!cursor)
|
||||
break;
|
||||
}
|
||||
catch (DatabaseTimeout const& e)
|
||||
{
|
||||
BOOST_LOG_TRIVIAL(warning)
|
||||
<< __func__ << " Database timeout fetching keys";
|
||||
std::this_thread::sleep_for(std::chrono::seconds(2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
BackendIndexer::writeNext(
|
||||
uint32_t ledgerSequence,
|
||||
BackendInterface const& backend)
|
||||
{
|
||||
BOOST_LOG_TRIVIAL(info)
|
||||
<< __func__
|
||||
<< " starting. sequence = " << std::to_string(ledgerSequence);
|
||||
bool isFlag = (ledgerSequence % (1 << shift_)) == 0;
|
||||
if (!backend.fetchLedgerRange())
|
||||
{
|
||||
isFlag = true;
|
||||
}
|
||||
|
||||
if (isFlag)
|
||||
{
|
||||
uint32_t nextSeq =
|
||||
((ledgerSequence >> shift_ << shift_) + (1 << shift_));
|
||||
BOOST_LOG_TRIVIAL(info)
|
||||
<< __func__ << " actually doing the write. keysCumulative.size() = "
|
||||
<< std::to_string(keysCumulative.size());
|
||||
backend.writeKeys(keysCumulative, nextSeq);
|
||||
BOOST_LOG_TRIVIAL(info) << __func__ << " wrote keys";
|
||||
|
||||
backend.writeBooks(booksCumulative, nextSeq);
|
||||
BOOST_LOG_TRIVIAL(info) << __func__ << " wrote books";
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
BackendIndexer::finish(uint32_t ledgerSequence, BackendInterface const& backend)
|
||||
{
|
||||
if (ledgerSequence >> keyShift_ << keyShift_ == ledgerSequence)
|
||||
bool isFlag = ledgerSequence % (1 << shift_) == 0;
|
||||
if (!backend.fetchLedgerRange())
|
||||
{
|
||||
std::unordered_set<ripple::uint256> keysCopy = keys;
|
||||
boost::asio::post(ioc_, [=, &backend]() {
|
||||
BOOST_LOG_TRIVIAL(info) << "Indexer - writing keys. Ledger = "
|
||||
<< std::to_string(ledgerSequence);
|
||||
backend.writeKeys(keysCopy, ledgerSequence);
|
||||
BOOST_LOG_TRIVIAL(info) << "Indexer - wrote keys. Ledger = "
|
||||
<< std::to_string(ledgerSequence);
|
||||
});
|
||||
isFlag = true;
|
||||
}
|
||||
if (ledgerSequence >> bookShift_ << bookShift_ == ledgerSequence)
|
||||
{
|
||||
std::unordered_map<ripple::uint256, std::unordered_set<ripple::uint256>>
|
||||
booksToOffersCopy = booksToOffers;
|
||||
std::unordered_map<ripple::uint256, std::unordered_set<ripple::uint256>>
|
||||
booksToDeletedOffersCopy = booksToDeletedOffers;
|
||||
boost::asio::post(ioc_, [=, &backend]() {
|
||||
BOOST_LOG_TRIVIAL(info) << "Indexer - writing books. Ledger = "
|
||||
<< std::to_string(ledgerSequence);
|
||||
backend.writeBooks(booksToOffersCopy, ledgerSequence);
|
||||
backend.writeBooks(booksToDeletedOffersCopy, ledgerSequence);
|
||||
BOOST_LOG_TRIVIAL(info) << "Indexer - wrote books. Ledger = "
|
||||
<< std::to_string(ledgerSequence);
|
||||
});
|
||||
booksToDeletedOffers = {};
|
||||
}
|
||||
}
|
||||
uint32_t nextSeq = ((ledgerSequence >> shift_ << shift_) + (1 << shift_));
|
||||
uint32_t curSeq = isFlag ? ledgerSequence : nextSeq;
|
||||
backend.writeKeys(keys, curSeq);
|
||||
keys = {};
|
||||
backend.writeBooks(books, curSeq);
|
||||
books = {};
|
||||
|
||||
} // namespace Backend
|
||||
} // namespace Backend
|
||||
|
||||
Reference in New Issue
Block a user