20 #include <ripple/app/ledger/TransactionMaster.h>
21 #include <ripple/app/misc/NetworkOPs.h>
22 #include <ripple/app/misc/SHAMapStoreImp.h>
23 #include <ripple/app/rdb/State.h>
24 #include <ripple/app/rdb/backend/SQLiteDatabase.h>
25 #include <ripple/beast/core/CurrentThreadName.h>
26 #include <ripple/core/ConfigSections.h>
27 #include <ripple/core/Pg.h>
28 #include <ripple/nodestore/impl/DatabaseRotatingImp.h>
30 #include <ripple/nodestore/Scheduler.h>
32 #include <boost/algorithm/string/predicate.hpp>
99 Throw<std::runtime_error>(
101 "] entry in configuration file");
105 if (boost::iequals(
get(section,
"type"),
"RocksDB"))
107 if (!section.exists(
"cache_mb"))
114 if (!section.exists(
"filter_bits") && (config.NODE_SIZE >= 2))
115 section.set(
"filter_bits",
"10");
124 Throw<std::runtime_error>(
125 "Reporting does not support online_delete. Remove "
126 "online_delete info from config");
145 auto const minInterval = config.standalone()
150 Throw<std::runtime_error>(
151 "online_delete must be at least " +
157 Throw<std::runtime_error>(
158 "online_delete must not be less than ledger_history "
174 if (!nscfg.exists(
"cache_size"))
180 if (!nscfg.exists(
"cache_age"))
192 Throw<std::runtime_error>(
193 "Reporting does not support online_delete. Remove "
194 "online_delete info from config");
201 state.
writableDb = writableBackend->getName();
202 state.
archiveDb = archiveBackend->getName();
208 auto dbr = std::make_unique<NodeStore::DatabaseRotatingImp>(
211 std::move(writableBackend),
212 std::move(archiveBackend),
284 Throw<std::runtime_error>(
285 "Reporting does not support online_delete. Remove "
286 "online_delete info from config");
320 LedgerIndex const validatedSeq = validatedLedger->info().seq;
323 lastRotated = validatedSeq;
327 bool const readyToRotate =
333 bool const waitForImport = readyToRotate && [
this, lastRotated] {
336 if (
auto sequence = shardStore->getDatabaseImportSequence())
337 return sequence <= lastRotated - 1;
346 <<
"NOT rotating validatedSeq " << validatedSeq
347 <<
" as rotation would interfere with ShardStore import";
351 if (readyToRotate && !waitForImport)
354 <<
"rotating validatedSeq " << validatedSeq <<
" lastRotated "
366 validatedLedger->stateMap().snapShot(
false)->visitNodes(
std::bind(
370 std::placeholders::_1));
375 <<
" nodecount " << nodeCount;
382 JLOG(
journal_.
debug()) << validatedSeq <<
" freshened caches";
387 << validatedSeq <<
" new backend " << newBackend->getName();
393 lastRotated = validatedSeq;
398 savedState.
writableDb = newBackend->getName();
399 savedState.
archiveDb = writableBackendName;
405 return std::move(newBackend);
408 JLOG(
journal_.
warn()) <<
"finished rotation " << validatedSeq;
417 boost::filesystem::path dbPath =
get(section,
"path");
419 if (boost::filesystem::exists(dbPath))
421 if (!boost::filesystem::is_directory(dbPath))
424 <<
"node db path must be a directory. " << dbPath.string();
425 Throw<std::runtime_error>(
"node db path must be a directory.");
430 boost::filesystem::create_directories(dbPath);
441 using namespace boost::filesystem;
442 auto const stored{path(sPath)};
443 if (stored.parent_path() == dbPath)
446 sPath = (dbPath / stored.filename()).
string();
457 bool writableDbExists =
false;
458 bool archiveDbExists =
false;
461 for (boost::filesystem::directory_iterator it(dbPath);
462 it != boost::filesystem::directory_iterator();
466 writableDbExists =
true;
468 archiveDbExists =
true;
475 (writableDbExists != archiveDbExists) ||
478 boost::filesystem::path stateDbPathName =
481 stateDbPathName +=
"*";
484 <<
"state db error:\n"
485 <<
" writableDbExists " << writableDbExists <<
" archiveDbExists "
486 << archiveDbExists <<
'\n'
487 <<
" writableDb '" << state.
writableDb <<
"' archiveDb '"
489 <<
"The existing data is in a corrupted state.\n"
490 <<
"To resume operation, remove the files matching "
491 << stateDbPathName.
string() <<
" and contents of the directory "
492 <<
get(section,
"path") <<
'\n'
493 <<
"Optionally, you can move those files to another\n"
494 <<
"location if you wish to analyze or back up the data.\n"
495 <<
"However, there is no guarantee that the data in its\n"
496 <<
"existing form is usable.";
498 Throw<std::runtime_error>(
"state db error");
502 for (boost::filesystem::path& p : pathsToDelete)
503 boost::filesystem::remove_all(p);
510 boost::filesystem::path newPath;
518 boost::filesystem::path p =
get(section,
"path");
521 newPath = boost::filesystem::unique_path(p);
523 section.set(
"path", newPath.string());
547 <<
"Begin: Look up lowest value of: " << TableName;
548 auto m = getMinSeq();
549 JLOG(
journal_.
trace()) <<
"End: Look up lowest value of: " << TableName;
555 if (min > lastRotated ||
stopping())
557 if (min == lastRotated)
560 JLOG(
journal_.
trace()) <<
"Nothing to delete from " << TableName;
564 JLOG(
journal_.
debug()) <<
"start deleting in: " << TableName <<
" from "
565 << min <<
" to " << lastRotated;
566 while (min < lastRotated)
571 <<
" rows with LedgerSeq < " << min <<
" from: " << TableName;
572 deleteBeforeSeq(min);
574 <<
"End: Delete up to " <<
deleteBatch_ <<
" rows with LedgerSeq < "
575 << min <<
" from: " << TableName;
578 if (min < lastRotated)
583 JLOG(
journal_.
debug()) <<
"finished deleting from: " << TableName;
608 Throw<std::runtime_error>(
609 "Reporting does not support online_delete. Remove "
610 "online_delete info from config");
615 JLOG(
journal_.
trace()) <<
"Begin: Clear internal ledgers up to "
618 JLOG(
journal_.
trace()) <<
"End: Clear internal ledgers up to "
627 Throw<std::runtime_error>(
"Failed to get relational database");
654 "AccountTransactions",
675 <<
"s for node to stabilize. state: "
677 <<
". age " << age.count() <<
's';
719 return std::make_unique<SHAMapStoreImp>(app, scheduler, journal);
Holds a collection of configuration values.
LedgerIndex setCanDelete(LedgerIndex canDelete)
LedgerIndex setCanDelete(soci::session &session, LedgerIndex canDelete)
setCanDelete Updates the ledger sequence which can be deleted.
virtual Family & getNodeFamily()=0
virtual std::shared_ptr< TreeNodeCache > getTreeNodeCache(std::uint32_t ledgerSeq)=0
Return a pointer to the Family Tree Node Cache.
NodeStore::Scheduler & scheduler_
SHAMapStoreImp(Application &app, NodeStore::Scheduler &scheduler, beast::Journal journal)
Persistency layer for NodeObject.
void clearLedgerCachePrior(LedgerIndex seq)
Stream trace() const
Severity stream access functions.
std::shared_ptr< Ledger const > newLedger_
void onLedgerClosed(std::shared_ptr< Ledger const > const &ledger) override
Called by LedgerMaster every time a ledger validates.
static const std::uint32_t minimumDeletionInterval_
const std::string dbPrefix_
LedgerMaster * ledgerMaster_
void setSavedState(soci::session &session, SavedState const &state)
setSavedState Saves the given state.
std::unique_ptr< NodeStore::Database > makeNodeStore(int readThreads) override
virtual NodeStore::DatabaseShard * getShardStore()=0
virtual OperatingMode getOperatingMode() const =0
const beast::Journal journal_
std::condition_variable cond_
virtual std::shared_ptr< FullBelowCache > getFullBelowCache(std::uint32_t ledgerSeq)=0
Return a pointer to the Family Full Below Cache.
LedgerIndex getCanDelete()
virtual std::optional< LedgerIndex > getAccountTransactionsMinLedgerSeq()=0
getAccountTransactionsMinLedgerSeq Returns the minimum ledger sequence stored in the AccountTransacti...
static constexpr auto nodeStoreName_
bool stopping()
This is a health check for online deletion that waits until rippled is stable until returning.
virtual NetworkOPs & getOPs()=0
bool copyNode(std::uint64_t &nodeCount, SHAMapTreeNode const &node)
NodeStore::DatabaseRotating * dbRotating_
bool get_if_exists(Section const §ion, std::string const &name, T &v)
virtual std::optional< LedgerIndex > getTransactionsMinLedgerSeq()=0
getTransactionsMinLedgerSeq Returns the minimum ledger sequence stored in the Transactions table.
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.
std::chrono::seconds recoveryWaitTime_
If the node is out of sync during an online_delete health check, sleep the thread for this time,...
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 LedgerMaster & getLedgerMaster()=0
virtual Config & config()=0
constexpr auto megabytes(T value) noexcept
std::atomic< LedgerIndex > minimumOnline_
virtual RelationalDatabase & getRelationalDatabase()=0
virtual void deleteAccountTransactionsBeforeLedgerSeq(LedgerIndex ledgerSeq)=0
deleteAccountTransactionsBeforeLedgerSeq Deletes all account transactions with a sequence number less...
void setState(SavedState const &state)
TreeNodeCache * treeNodeCache_
std::atomic< bool > working_
std::uint32_t deleteInterval_
std::chrono::seconds ageThreshold_
virtual std::optional< LedgerIndex > getMinLedgerSeq()=0
getMinLedgerSeq Returns the minimum ledger sequence in the Ledgers table.
std::optional< LedgerIndex > minSqlSeq()
void initStateDB(soci::session &session, BasicConfig const &config, std::string const &dbName)
initStateDB Opens a session with the State database.
void legacy(std::string const §ion, std::string value)
Set a value that is not a key/value pair.
TaggedCache< uint256, Transaction > & getCache()
void clearPrior(LedgerIndex lastRotated)
A generic endpoint for log messages.
Scheduling for asynchronous backend activity.
SavedState getSavedState(soci::session &session)
getSavedState Returns the saved state.
const std::uint64_t checkHealthInterval_
SHAMapHash const & getHash() const
Return the hash of this node.
std::chrono::milliseconds backOff_
void init(BasicConfig const &config, std::string const &dbName)
std::chrono::seconds getValidatedLedgerAge()
const std::string dbName_
void setLastRotated(soci::session &session, LedgerIndex seq)
setLastRotated Updates the last rotated ledger sequence.
void setCurrentThreadName(std::string_view name)
Changes the name of the caller thread.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
int fdRequired() const override
Returns the number of file descriptors that are needed.
void set(std::string const &key, std::string const &value)
Set a key/value pair.
virtual std::unique_ptr< Backend > make_Backend(Section const ¶meters, std::size_t burstSize, Scheduler &scheduler, beast::Journal journal)=0
Create a backend.
beast::Journal journal(std::string const &name)
LedgerIndex getCanDelete(soci::session &session)
getCanDelete Returns the ledger sequence which can be deleted.
void setLastRotated(LedgerIndex seq)
virtual void deleteTransactionsBeforeLedgerSeq(LedgerIndex ledgerSeq)=0
deleteTransactionsBeforeLedgerSeq Deletes all transactions with a sequence number less than or equal ...
std::atomic< LedgerIndex > canDelete_
static const std::uint32_t minimumDeletionIntervalSA_
virtual void deleteBeforeLedgerSeq(LedgerIndex ledgerSeq)=0
deleteBeforeLedgerSeq Deletes all ledgers with a sequence number less than or equal to the given ledg...
std::uint32_t deleteBatch_
FullBelowCache * fullBelowCache_
std::optional< LedgerIndex > minimumOnline() const override
The minimum ledger to try and maintain in our database.
void clearCaches(LedgerIndex validatedSeq)
virtual std::string strOperatingMode(OperatingMode const mode, bool const admin=false) const =0
OperatingMode
Specifies the mode under which the server believes it's operating.
std::unique_ptr< NodeStore::Backend > makeBackendRotating(std::string path=std::string())
std::shared_ptr< NodeObject > fetchNodeObject(uint256 const &hash, std::uint32_t ledgerSeq=0, FetchType fetchType=FetchType::synchronous, bool duplicate=false)
Fetch a node object.
uint256 const & as_uint256() const
static Manager & instance()
Returns the instance of the manager singleton.
virtual void rotateWithLock(std::function< std::unique_ptr< NodeStore::Backend >(std::string const &writableBackendName)> const &f)=0
Rotates the backends.
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.
std::unique_ptr< SHAMapStore > make_SHAMapStore(Application &app, NodeStore::Scheduler &scheduler, beast::Journal journal)
bool freshenCache(CacheInstance &cache)
void clearPriorLedgers(LedgerIndex seq)
Holds unparsed configuration information.
static std::string nodeDatabase()
void rendezvous() const override
T & get(EitherAmount &amt)
Section & section(std::string const &name)
Returns the section with the given name.
std::condition_variable rendezvous_
virtual TransactionMaster & getMasterTransaction()=0
@ FULL
we have the ledger and can even validate