xrpld
Loading...
Searching...
No Matches
SHAMapStoreImp.h
1#pragma once
2
3#include <xrpld/app/ledger/LedgerMaster.h>
4#include <xrpld/app/misc/SHAMapStore.h>
5
6#include <xrpl/nodestore/DatabaseRotating.h>
7#include <xrpl/nodestore/Scheduler.h>
8#include <xrpl/rdb/DatabaseCon.h>
9#include <xrpl/server/State.h>
10#include <xrpl/shamap/FullBelowCache.h>
11#include <xrpl/shamap/TreeNodeCache.h>
12
13#include <atomic>
14#include <chrono>
15#include <thread>
16
17namespace xrpl {
18
19class NetworkOPs;
20
22{
23private:
25 {
26 public:
27 soci::session sqlDb;
30
31 // Just instantiate without any logic in case online delete is not
32 // configured
33 explicit SavedStateDB() : journal{beast::Journal::getNullSink()}
34 {
35 }
36
37 // opens database and, if necessary, creates & initializes its tables.
38 void
39 init(BasicConfig const& config, std::string const& dbName);
40 // get/set the ledger index that we can delete up to and including
44 setCanDelete(LedgerIndex canDelete);
46 getState();
47 void
48 setState(SavedState const& state);
49 void
51 };
52
54
55 // name of state database
56 std::string const dbName_ = "state";
57 // prefix of on-disk nodestore backend instances
58 std::string const dbPrefix_ = "rippledb"; // cspell: disable-line
59 // check health/stop status as records are copied
61 // minimum # of ledgers to maintain for health of network
63 // minimum # of ledgers required for standalone mode.
65 // minimum ledger to maintain online.
67
73 bool stop_ = false;
74 bool healthy_ = true;
81 int fdRequired_ = 0;
82
84 bool advisoryDelete_ = false;
93
94 // these do not exist upon SHAMapStore creation, but do exist
95 // as of run() or before
96 NetworkOPs* netOPs_ = nullptr;
100
101 static constexpr auto kNodeStoreName = "NodeStore";
102
103public:
105
107 clampFetchDepth(std::uint32_t fetchDepth) const override
108 {
109 return (deleteInterval_ != 0u) ? std::min(fetchDepth, deleteInterval_) : fetchDepth;
110 }
111
113 makeNodeStore(int readThreads) override;
114
117 {
118 if (advisoryDelete_)
119 canDelete_ = seq;
120 return stateDb_.setCanDelete(seq);
121 }
122
123 bool
124 advisoryDelete() const override
125 {
126 return advisoryDelete_;
127 }
128
129 // All ledgers prior to this one are eligible
130 // for deletion in the next rotation
132 getLastRotated() override
133 {
134 return stateDb_.getState().lastRotated;
135 }
136
137 // All ledgers before and including this are unprotected
138 // and online delete may delete them if appropriate
140 getCanDelete() override
141 {
142 return canDelete_;
143 }
144
145 void
146 onLedgerClosed(std::shared_ptr<Ledger const> const& ledger) override;
147
148 void
149 rendezvous() const override;
150 int
151 fdRequired() const override;
152
154 minimumOnline() const override;
155
156private:
157 // callback for visitNodes
158 bool
159 copyNode(std::uint64_t& nodeCount, SHAMapTreeNode const& node);
160 void
161 run();
162 void
163 dbPaths();
164
167
168 template <class CacheInstance>
169 bool
170 freshenCache(CacheInstance& cache)
171 {
172 std::uint64_t check = 0;
173
174 for (auto const& key : cache.getKeys())
175 {
176 dbRotating_->fetchNodeObject(key, 0, NodeStore::FetchType::Synchronous, true);
178 return true;
179 }
180
181 return false;
182 }
183
188 void
189 clearSql(
190 LedgerIndex lastRotated,
191 std::string const& tableName,
192 std::function<std::optional<LedgerIndex>()> const& getMinSeq,
193 std::function<void(LedgerIndex)> const& deleteBeforeSeq);
194 void
195 clearCaches(LedgerIndex validatedSeq);
196 void
198 void
199 clearPrior(LedgerIndex lastRotated);
200
209 [[nodiscard]] HealthResult
210 healthWait();
211
212public:
213 void
214 start() override
215 {
216 if (deleteInterval_ != 0u)
218 }
219
220 void
221 stop() override;
222};
223
224} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
Holds unparsed configuration information.
Provides server functionality for clients.
Definition NetworkOPs.h:71
Scheduling for asynchronous backend activity.
void setState(SavedState const &state)
void init(BasicConfig const &config, std::string const &dbName)
LedgerIndex setCanDelete(LedgerIndex canDelete)
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::uint32_t clampFetchDepth(std::uint32_t fetchDepth) const override
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_
std::uint32_t deleteInterval_
static std::uint32_t const kMinimumDeletionIntervalSa
int fdRequired() const override
Returns the number of file descriptors that are needed.
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_
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::condition_variable rendezvous_
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.
static constexpr auto kNodeStoreName
std::string const dbPrefix_
NodeStore::Scheduler & scheduler_
HealthResult
This is a health check for online deletion that waits until xrpld is stable before returning.
void onLedgerClosed(std::shared_ptr< Ledger const > const &ledger) override
Called by LedgerMaster every time a ledger validates.
static std::uint32_t const kMinimumDeletionInterval
bool freshenCache(CacheInstance &cache)
void clearPrior(LedgerIndex lastRotated)
SHAMapStoreImp(Application &app, NodeStore::Scheduler &scheduler, beast::Journal journal)
class to create database, launch online delete thread, and related SQLite database
Definition SHAMapStore.h:19
T min(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::uint32_t LedgerIndex
A ledger index.
Definition Protocol.h:259
detail::BasicFullBelowCache FullBelowCache
TaggedCache< uint256, SHAMapTreeNode, false, intr_ptr::SharedWeakUnionPtr< SHAMapTreeNode >, SHAMapTreeNodePtr > TreeNodeCache