20 #include <ripple/app/misc/SHAMapStoreImp.h>
22 #include <ripple/app/ledger/TransactionMaster.h>
23 #include <ripple/app/misc/NetworkOPs.h>
24 #include <ripple/app/rdb/State.h>
25 #include <ripple/app/rdb/backend/SQLiteDatabase.h>
26 #include <ripple/beast/core/CurrentThreadName.h>
27 #include <ripple/core/ConfigSections.h>
28 #include <ripple/core/Pg.h>
29 #include <ripple/nodestore/Scheduler.h>
30 #include <ripple/nodestore/impl/DatabaseRotatingImp.h>
31 #include <ripple/shamap/SHAMapMissingNode.h>
33 #include <boost/algorithm/string/predicate.hpp>
100 Throw<std::runtime_error>(
102 "] entry in configuration file");
106 if (boost::iequals(
get(section,
"type"),
"RocksDB"))
108 if (!section.exists(
"cache_mb"))
115 if (!section.exists(
"filter_bits") && (config.NODE_SIZE >= 2))
116 section.set(
"filter_bits",
"10");
125 Throw<std::runtime_error>(
126 "Reporting does not support online_delete. Remove "
127 "online_delete info from config");
146 auto const minInterval = config.standalone()
151 Throw<std::runtime_error>(
152 "online_delete must be at least " +
158 Throw<std::runtime_error>(
159 "online_delete must not be less than ledger_history "
175 if (!nscfg.exists(
"cache_size"))
181 if (!nscfg.exists(
"cache_age"))
193 Throw<std::runtime_error>(
194 "Reporting does not support online_delete. Remove "
195 "online_delete info from config");
202 state.
writableDb = writableBackend->getName();
203 state.
archiveDb = archiveBackend->getName();
209 auto dbr = std::make_unique<NodeStore::DatabaseRotatingImp>(
212 std::move(writableBackend),
213 std::move(archiveBackend),
285 Throw<std::runtime_error>(
286 "Reporting does not support online_delete. Remove "
287 "online_delete info from config");
321 LedgerIndex const validatedSeq = validatedLedger->info().seq;
324 lastRotated = validatedSeq;
328 bool const readyToRotate =
334 bool const waitForImport = readyToRotate && [
this, lastRotated] {
337 if (
auto sequence = shardStore->getDatabaseImportSequence())
338 return sequence <= lastRotated - 1;
347 <<
"NOT rotating validatedSeq " << validatedSeq
348 <<
" as rotation would interfere with ShardStore import";
352 if (readyToRotate && !waitForImport)
355 <<
"rotating validatedSeq " << validatedSeq <<
" lastRotated "
370 validatedLedger->stateMap().snapShot(
false)->visitNodes(
375 std::placeholders::_1));
380 <<
"Missing node while copying ledger before rotate: "
389 <<
" nodecount " << nodeCount;
396 JLOG(
journal_.
debug()) << validatedSeq <<
" freshened caches";
401 << validatedSeq <<
" new backend " << newBackend->getName();
407 lastRotated = validatedSeq;
412 savedState.
writableDb = newBackend->getName();
413 savedState.
archiveDb = writableBackendName;
419 return std::move(newBackend);
422 JLOG(
journal_.
warn()) <<
"finished rotation " << validatedSeq;
431 boost::filesystem::path dbPath =
get(section,
"path");
433 if (boost::filesystem::exists(dbPath))
435 if (!boost::filesystem::is_directory(dbPath))
438 <<
"node db path must be a directory. " << dbPath.string();
439 Throw<std::runtime_error>(
"node db path must be a directory.");
444 boost::filesystem::create_directories(dbPath);
455 using namespace boost::filesystem;
456 auto const stored{path(sPath)};
457 if (stored.parent_path() == dbPath)
460 sPath = (dbPath / stored.filename()).
string();
471 bool writableDbExists =
false;
472 bool archiveDbExists =
false;
475 for (boost::filesystem::directory_iterator it(dbPath);
476 it != boost::filesystem::directory_iterator();
480 writableDbExists =
true;
482 archiveDbExists =
true;
489 (writableDbExists != archiveDbExists) ||
492 boost::filesystem::path stateDbPathName =
495 stateDbPathName +=
"*";
498 <<
"state db error:\n"
499 <<
" writableDbExists " << writableDbExists <<
" archiveDbExists "
500 << archiveDbExists <<
'\n'
501 <<
" writableDb '" << state.
writableDb <<
"' archiveDb '"
503 <<
"The existing data is in a corrupted state.\n"
504 <<
"To resume operation, remove the files matching "
505 << stateDbPathName.
string() <<
" and contents of the directory "
506 <<
get(section,
"path") <<
'\n'
507 <<
"Optionally, you can move those files to another\n"
508 <<
"location if you wish to analyze or back up the data.\n"
509 <<
"However, there is no guarantee that the data in its\n"
510 <<
"existing form is usable.";
512 Throw<std::runtime_error>(
"state db error");
516 for (boost::filesystem::path& p : pathsToDelete)
517 boost::filesystem::remove_all(p);
524 boost::filesystem::path newPath;
532 boost::filesystem::path p =
get(section,
"path");
535 newPath = boost::filesystem::unique_path(p);
537 section.set(
"path", newPath.string());
561 <<
"Begin: Look up lowest value of: " << TableName;
562 auto m = getMinSeq();
563 JLOG(
journal_.
trace()) <<
"End: Look up lowest value of: " << TableName;
571 if (min == lastRotated)
574 JLOG(
journal_.
trace()) <<
"Nothing to delete from " << TableName;
578 JLOG(
journal_.
debug()) <<
"start deleting in: " << TableName <<
" from "
579 << min <<
" to " << lastRotated;
580 while (min < lastRotated)
585 <<
" rows with LedgerSeq < " << min <<
" from: " << TableName;
586 deleteBeforeSeq(min);
588 <<
"End: Delete up to " <<
deleteBatch_ <<
" rows with LedgerSeq < "
589 << min <<
" from: " << TableName;
592 if (min < lastRotated)
597 JLOG(
journal_.
debug()) <<
"finished deleting from: " << TableName;
622 Throw<std::runtime_error>(
623 "Reporting does not support online_delete. Remove "
624 "online_delete info from config");
629 JLOG(
journal_.
trace()) <<
"Begin: Clear internal ledgers up to "
632 JLOG(
journal_.
trace()) <<
"End: Clear internal ledgers up to "
641 Throw<std::runtime_error>(
"Failed to get relational database");
668 "AccountTransactions",
689 <<
"s for node to stabilize. state: "
691 <<
". age " << age.count() <<
's';
733 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_
HealthResult healthWait()
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.
HealthResult
This is a health check for online deletion that waits until rippled is stable before returning.
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 healthWait() call, 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