1#include <xrpld/app/ledger/TransactionMaster.h>
2#include <xrpld/app/misc/NetworkOPs.h>
3#include <xrpld/app/misc/SHAMapStoreImp.h>
4#include <xrpld/app/rdb/State.h>
5#include <xrpld/app/rdb/backend/SQLiteDatabase.h>
6#include <xrpld/core/ConfigSections.h>
8#include <xrpl/beast/core/CurrentThreadName.h>
9#include <xrpl/nodestore/Scheduler.h>
10#include <xrpl/nodestore/detail/DatabaseRotatingImp.h>
11#include <xrpl/shamap/SHAMapMissingNode.h>
13#include <boost/algorithm/string/predicate.hpp>
79 if (boost::iequals(
get(section,
"type"),
"RocksDB"))
81 if (!section.exists(
"cache_mb"))
86 if (!section.exists(
"filter_bits") && (config.NODE_SIZE >= 2))
87 section.set(
"filter_bits",
"10");
113 Throw<std::runtime_error>(
"online_delete must be at least " +
std::to_string(minInterval));
118 Throw<std::runtime_error>(
119 "online_delete must not be less than ledger_history "
135 if (!nscfg.exists(
"cache_size"))
138 if (!nscfg.exists(
"cache_age"))
150 state.
writableDb = writableBackend->getName();
151 state.
archiveDb = archiveBackend->getName();
160 std::move(writableBackend),
161 std::move(archiveBackend),
257 LedgerIndex const validatedSeq = validatedLedger->header().seq;
260 lastRotated = validatedSeq;
264 bool const readyToRotate =
270 JLOG(
journal_.
warn()) <<
"rotating validatedSeq " << validatedSeq <<
" lastRotated " << lastRotated
284 validatedLedger->stateMap().snapShot(
false)->visitNodes(
289 JLOG(
journal_.
error()) <<
"Missing node while copying ledger before rotate: " << e.
what();
296 JLOG(
journal_.
debug()) <<
"copied ledger " << validatedSeq <<
" nodecount " << nodeCount;
303 JLOG(
journal_.
debug()) << validatedSeq <<
" freshened caches";
307 JLOG(
journal_.
debug()) << validatedSeq <<
" new backend " << newBackend->getName();
313 lastRotated = validatedSeq;
326 JLOG(
journal_.
warn()) <<
"finished rotation " << validatedSeq;
335 boost::filesystem::path dbPath =
get(section,
"path");
337 if (boost::filesystem::exists(dbPath))
339 if (!boost::filesystem::is_directory(dbPath))
341 journal_.
error() <<
"node db path must be a directory. " << dbPath.string();
342 Throw<std::runtime_error>(
"node db path must be a directory.");
347 boost::filesystem::create_directories(dbPath);
358 using namespace boost::filesystem;
359 auto const stored{path(sPath)};
360 if (stored.parent_path() == dbPath)
363 sPath = (dbPath / stored.filename()).
string();
374 bool writableDbExists =
false;
375 bool archiveDbExists =
false;
378 for (boost::filesystem::directory_iterator it(dbPath); it != boost::filesystem::directory_iterator(); ++it)
381 writableDbExists =
true;
383 archiveDbExists =
true;
391 boost::filesystem::path stateDbPathName =
app_.
config().
legacy(
"database_path");
393 stateDbPathName +=
"*";
396 <<
" writableDbExists " << writableDbExists <<
" archiveDbExists " << archiveDbExists <<
'\n'
398 <<
"The existing data is in a corrupted state.\n"
399 <<
"To resume operation, remove the files matching " << stateDbPathName.
string()
400 <<
" and contents of the directory " <<
get(section,
"path") <<
'\n'
401 <<
"Optionally, you can move those files to another\n"
402 <<
"location if you wish to analyze or back up the data.\n"
403 <<
"However, there is no guarantee that the data in its\n"
404 <<
"existing form is usable.";
406 Throw<std::runtime_error>(
"state db error");
410 for (boost::filesystem::path& p : pathsToDelete)
411 boost::filesystem::remove_all(p);
418 boost::filesystem::path newPath;
426 boost::filesystem::path p =
get(section,
"path");
429 newPath = boost::filesystem::unique_path(p);
431 section.set(
"path", newPath.string());
449 XRPL_ASSERT(
deleteInterval_,
"xrpl::SHAMapStoreImp::clearSql : nonzero delete interval");
453 JLOG(
journal_.
trace()) <<
"Begin: Look up lowest value of: " << TableName;
454 auto m = getMinSeq();
455 JLOG(
journal_.
trace()) <<
"End: Look up lowest value of: " << TableName;
463 if (min == lastRotated)
466 JLOG(
journal_.
trace()) <<
"Nothing to delete from " << TableName;
470 JLOG(
journal_.
debug()) <<
"start deleting in: " << TableName <<
" from " << min <<
" to " << lastRotated;
471 while (min < lastRotated)
475 <<
" from: " << TableName;
476 deleteBeforeSeq(min);
478 <<
" from: " << TableName;
481 if (min < lastRotated)
486 JLOG(
journal_.
debug()) <<
"finished deleting from: " << TableName;
511 JLOG(
journal_.
trace()) <<
"Begin: Clear internal ledgers up to " << lastRotated;
513 JLOG(
journal_.
trace()) <<
"End: Clear internal ledgers up to " << lastRotated;
520 Throw<std::runtime_error>(
"Failed to get relational database");
543 "AccountTransactions",
561 <<
". age " << age.count() <<
's';
A generic endpoint for log messages.
Stream trace() const
Severity stream access functions.
virtual Config & config()=0
virtual LedgerMaster & getLedgerMaster()=0
virtual Family & getNodeFamily()=0
virtual TransactionMaster & getMasterTransaction()=0
virtual RelationalDatabase & getRelationalDatabase()=0
virtual NetworkOPs & getOPs()=0
Holds unparsed configuration information.
void legacy(std::string const §ion, std::string value)
Set a value that is not a key/value pair.
Section & section(std::string const &name)
Returns the section with the given name.
int getValueFor(SizedItem item, std::optional< std::size_t > node=std::nullopt) const
Retrieve the default value for the item at the specified node size.
virtual std::shared_ptr< TreeNodeCache > getTreeNodeCache()=0
Return a pointer to the Family Tree Node Cache.
virtual std::shared_ptr< FullBelowCache > getFullBelowCache()=0
Return a pointer to the Family Full Below Cache.
std::optional< LedgerIndex > minSqlSeq()
std::chrono::seconds getValidatedLedgerAge()
void clearPriorLedgers(LedgerIndex seq)
void clearLedgerCachePrior(LedgerIndex seq)
beast::Journal journal(std::string const &name)
virtual OperatingMode getOperatingMode() const =0
virtual std::string strOperatingMode(OperatingMode const mode, bool const admin=false) const =0
virtual void rotate(std::unique_ptr< NodeStore::Backend > &&newBackend, std::function< void(std::string const &writableName, std::string const &archiveName)> const &f)=0
Rotates the backends.
Persistency layer for NodeObject.
std::shared_ptr< NodeObject > fetchNodeObject(uint256 const &hash, std::uint32_t ledgerSeq=0, FetchType fetchType=FetchType::synchronous, bool duplicate=false)
Fetch a node object.
static Manager & instance()
Returns the instance of the manager singleton.
virtual std::unique_ptr< Database > make_Database(std::size_t burstSize, Scheduler &scheduler, int readThreads, Section const &backendParameters, beast::Journal journal)=0
Construct a NodeStore database.
virtual std::unique_ptr< Backend > make_Backend(Section const ¶meters, std::size_t burstSize, Scheduler &scheduler, beast::Journal journal)=0
Create a backend.
Scheduling for asynchronous backend activity.
virtual std::optional< LedgerIndex > getMinLedgerSeq()=0
getMinLedgerSeq Returns the minimum ledger sequence in the Ledgers table.
uint256 const & as_uint256() const
void setState(SavedState const &state)
void init(BasicConfig const &config, std::string const &dbName)
LedgerIndex setCanDelete(LedgerIndex canDelete)
void setLastRotated(LedgerIndex seq)
LedgerIndex getCanDelete()
void clearSql(LedgerIndex lastRotated, std::string const &TableName, std::function< std::optional< LedgerIndex >()> const &getMinSeq, std::function< void(LedgerIndex)> const &deleteBeforeSeq)
delete from sqlite table in batches to not lock the db excessively.
bool copyNode(std::uint64_t &nodeCount, SHAMapTreeNode const &node)
std::unique_ptr< NodeStore::Backend > makeBackendRotating(std::string path=std::string())
std::atomic< bool > working_
std::condition_variable cond_
std::uint32_t deleteBatch_
std::atomic< LedgerIndex > minimumOnline_
std::chrono::seconds recoveryWaitTime_
If the node is out of sync during an online_delete healthWait() call, sleep the thread for this time,...
FullBelowCache * fullBelowCache_
std::optional< LedgerIndex > minimumOnline() const override
The minimum ledger to try and maintain in our database.
TreeNodeCache * treeNodeCache_
static constexpr auto nodeStoreName_
std::uint32_t deleteInterval_
int fdRequired() const override
Returns the number of file descriptors that are needed.
static std::uint32_t const minimumDeletionInterval_
std::unique_ptr< NodeStore::Database > makeNodeStore(int readThreads) override
std::chrono::milliseconds backOff_
std::atomic< LedgerIndex > canDelete_
NodeStore::DatabaseRotating * dbRotating_
std::shared_ptr< Ledger const > newLedger_
std::string const dbName_
std::condition_variable rendezvous_
static std::uint32_t const minimumDeletionIntervalSA_
std::uint64_t const checkHealthInterval_
HealthResult healthWait()
void clearCaches(LedgerIndex validatedSeq)
std::chrono::seconds ageThreshold_
void rendezvous() const override
LedgerMaster * ledgerMaster_
beast::Journal const journal_
std::string const dbPrefix_
NodeStore::Scheduler & scheduler_
HealthResult
This is a health check for online deletion that waits until rippled is stable before returning.
void onLedgerClosed(std::shared_ptr< Ledger const > const &ledger) override
Called by LedgerMaster every time a ledger validates.
bool freshenCache(CacheInstance &cache)
void clearPrior(LedgerIndex lastRotated)
SHAMapStoreImp(Application &app, NodeStore::Scheduler &scheduler, beast::Journal journal)
SHAMapHash const & getHash() const
Return the hash of this node.
virtual std::optional< LedgerIndex > getTransactionsMinLedgerSeq()=0
getTransactionsMinLedgerSeq Returns the minimum ledger sequence stored in the Transactions table.
virtual void deleteAccountTransactionsBeforeLedgerSeq(LedgerIndex ledgerSeq)=0
deleteAccountTransactionsBeforeLedgerSeq Deletes all account transactions with a sequence number less...
virtual void deleteBeforeLedgerSeq(LedgerIndex ledgerSeq)=0
deleteBeforeLedgerSeq Deletes all ledgers with a sequence number less than or equal to the given ledg...
virtual std::optional< LedgerIndex > getAccountTransactionsMinLedgerSeq()=0
getAccountTransactionsMinLedgerSeq Returns the minimum ledger sequence stored in the AccountTransacti...
virtual void deleteTransactionsBeforeLedgerSeq(LedgerIndex ledgerSeq)=0
deleteTransactionsBeforeLedgerSeq Deletes all transactions with a sequence number less than or equal ...
Holds a collection of configuration values.
void set(std::string const &key, std::string const &value)
Set a key/value pair.
TaggedCache< uint256, Transaction > & getCache()
void setCurrentThreadName(std::string_view newThreadName)
Changes the name of the caller thread.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
T get(Section const §ion, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
void setLastRotated(soci::session &session, LedgerIndex seq)
setLastRotated Updates the last rotated ledger sequence.
void setSavedState(soci::session &session, SavedState const &state)
setSavedState Saves the given state.
void initStateDB(soci::session &session, BasicConfig const &config, std::string const &dbName)
initStateDB Opens a session with the State database.
std::unique_ptr< SHAMapStore > make_SHAMapStore(Application &app, NodeStore::Scheduler &scheduler, beast::Journal journal)
SavedState getSavedState(soci::session &session)
getSavedState Returns the saved state.
constexpr auto megabytes(T value) noexcept
bool get_if_exists(Section const §ion, std::string const &name, T &v)
LedgerIndex setCanDelete(soci::session &session, LedgerIndex canDelete)
setCanDelete Updates the ledger sequence which can be deleted.
OperatingMode
Specifies the mode under which the server believes it's operating.
@ FULL
we have the ledger and can even validate
LedgerIndex getCanDelete(soci::session &session)
getCanDelete Returns the ledger sequence which can be deleted.
static std::string nodeDatabase()