rippled
Loading...
Searching...
No Matches
SHAMapStoreImp.h
1#ifndef XRPL_APP_MISC_SHAMAPSTOREIMP_H_INCLUDED
2#define XRPL_APP_MISC_SHAMAPSTOREIMP_H_INCLUDED
3
4#include <xrpld/app/ledger/LedgerMaster.h>
5#include <xrpld/app/misc/SHAMapStore.h>
6#include <xrpld/app/rdb/State.h>
7#include <xrpld/core/DatabaseCon.h>
8
9#include <xrpl/nodestore/DatabaseRotating.h>
10#include <xrpl/nodestore/Scheduler.h>
11
12#include <atomic>
13#include <chrono>
14#include <thread>
15
16namespace xrpl {
17
18class NetworkOPs;
19
21{
22private:
24 {
25 public:
26 soci::session sqlDb_;
29
30 // Just instantiate without any logic in case online delete is not
31 // configured
32 explicit SavedStateDB() : journal_{beast::Journal::getNullSink()}
33 {
34 }
35
36 // opens database and, if necessary, creates & initializes its tables.
37 void
38 init(BasicConfig const& config, std::string const& dbName);
39 // get/set the ledger index that we can delete up to and including
43 setCanDelete(LedgerIndex canDelete);
45 getState();
46 void
47 setState(SavedState const& state);
48 void
50 };
51
53
54 // name of state database
55 std::string const dbName_ = "state";
56 // prefix of on-disk nodestore backend instances
57 std::string const dbPrefix_ = "rippledb"; // cspell: disable-line
58 // check health/stop status as records are copied
60 // minimum # of ledgers to maintain for health of network
62 // minimum # of ledgers required for standalone mode.
64 // minimum ledger to maintain online.
66
72 bool stop_ = false;
73 bool healthy_ = true;
80 int fdRequired_ = 0;
81
83 bool advisoryDelete_ = false;
92
93 // these do not exist upon SHAMapStore creation, but do exist
94 // as of run() or before
95 NetworkOPs* netOPs_ = nullptr;
99
100 static constexpr auto nodeStoreName_ = "NodeStore";
101
102public:
104 Application& app,
105 NodeStore::Scheduler& scheduler,
106 beast::Journal journal);
107
109 clampFetchDepth(std::uint32_t fetch_depth) const override
110 {
111 return deleteInterval_ ? std::min(fetch_depth, deleteInterval_)
112 : fetch_depth;
113 }
114
116 makeNodeStore(int readThreads) override;
117
120 {
121 if (advisoryDelete_)
122 canDelete_ = seq;
123 return state_db_.setCanDelete(seq);
124 }
125
126 bool
127 advisoryDelete() const override
128 {
129 return advisoryDelete_;
130 }
131
132 // All ledgers prior to this one are eligible
133 // for deletion in the next rotation
135 getLastRotated() override
136 {
138 }
139
140 // All ledgers before and including this are unprotected
141 // and online delete may delete them if appropriate
143 getCanDelete() override
144 {
145 return canDelete_;
146 }
147
148 void
149 onLedgerClosed(std::shared_ptr<Ledger const> const& ledger) override;
150
151 void
152 rendezvous() const override;
153 int
154 fdRequired() const override;
155
157 minimumOnline() const override;
158
159private:
160 // callback for visitNodes
161 bool
162 copyNode(std::uint64_t& nodeCount, SHAMapTreeNode const& node);
163 void
164 run();
165 void
166 dbPaths();
167
170
171 template <class CacheInstance>
172 bool
173 freshenCache(CacheInstance& cache)
174 {
175 std::uint64_t check = 0;
176
177 for (auto const& key : cache.getKeys())
178 {
181 if (!(++check % checkHealthInterval_) && healthWait() == stopping)
182 return true;
183 }
184
185 return false;
186 }
187
192 void
193 clearSql(
194 LedgerIndex lastRotated,
195 std::string const& TableName,
196 std::function<std::optional<LedgerIndex>()> const& getMinSeq,
197 std::function<void(LedgerIndex)> const& deleteBeforeSeq);
198 void
199 clearCaches(LedgerIndex validatedSeq);
200 void
202 void
203 clearPrior(LedgerIndex lastRotated);
204
213 [[nodiscard]] HealthResult
214 healthWait();
215
216public:
217 void
218 start() override
219 {
220 if (deleteInterval_)
222 }
223
224 void
225 stop() override;
226};
227
228} // namespace xrpl
229
230#endif
A generic endpoint for log messages.
Definition Journal.h:41
Holds unparsed configuration information.
Provides server functionality for clients.
Definition NetworkOPs.h:70
std::shared_ptr< NodeObject > fetchNodeObject(uint256 const &hash, std::uint32_t ledgerSeq=0, FetchType fetchType=FetchType::synchronous, bool duplicate=false)
Fetch a node object.
Definition Database.cpp:221
Scheduling for asynchronous backend activity.
void setState(SavedState const &state)
void init(BasicConfig const &config, std::string const &dbName)
LedgerIndex setCanDelete(LedgerIndex canDelete)
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.
LedgerIndex getLastRotated() override
Maximum ledger that has been deleted, or will be deleted if currently in the act of online deletion.
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_
void start() override
LedgerIndex setCanDelete(LedgerIndex seq) override
Highest ledger that may be deleted.
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.
bool advisoryDelete() const override
Whether advisory delete is enabled.
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_
LedgerIndex getCanDelete() override
Highest ledger that may be deleted.
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)
std::uint32_t clampFetchDepth(std::uint32_t fetch_depth) const override
void clearPrior(LedgerIndex lastRotated)
class to create database, launch online delete thread, and related SQLite database
Definition SHAMapStore.h:19
Map/cache combination.
Definition TaggedCache.h:43
Remembers which tree keys have all descendants resident.
T min(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
LedgerIndex lastRotated
Definition State.h:18