xrpld
Loading...
Searching...
No Matches
SHAMapStoreImp.h
1#pragma once
2
3#include <xrpld/app/ledger/LedgerMaster.h>
4#include <xrpld/app/main/Application.h>
5#include <xrpld/app/misc/SHAMapStore.h>
6
7#include <xrpl/beast/utility/Journal.h>
8#include <xrpl/config/BasicConfig.h>
9#include <xrpl/ledger/Ledger.h>
10#include <xrpl/nodestore/Backend.h>
11#include <xrpl/nodestore/Database.h>
12#include <xrpl/nodestore/DatabaseRotating.h>
13#include <xrpl/nodestore/Scheduler.h>
14#include <xrpl/protocol/Protocol.h>
15#include <xrpl/rdb/DatabaseCon.h>
16#include <xrpl/server/State.h>
17#include <xrpl/shamap/FullBelowCache.h>
18#include <xrpl/shamap/SHAMapTreeNode.h>
19#include <xrpl/shamap/TreeNodeCache.h>
20
21#include <algorithm>
22#include <atomic>
23#include <chrono>
24#include <condition_variable>
25#include <cstdint>
26#include <functional>
27#include <memory>
28#include <mutex>
29#include <optional>
30#include <string>
31#include <thread>
32
33namespace xrpl {
34
35class NetworkOPs;
36
38{
39private:
41 {
42 public:
43 soci::session sqlDb;
46
47 // Just instantiate without any logic in case online delete is not
48 // configured
49 explicit SavedStateDB() : journal{beast::Journal::getNullSink()}
50 {
51 }
52
53 // opens database and, if necessary, creates & initializes its tables.
54 void
55 init(BasicConfig const& config, std::string const& dbName);
56 // get/set the ledger index that we can delete up to and including
60 setCanDelete(LedgerIndex canDelete);
62 getState();
63 void
64 setState(SavedState const& state);
65 void
67 };
68
70
71 // name of state database
72 std::string const dbName_ = "state";
73 // prefix of on-disk nodestore backend instances
74 std::string const dbPrefix_ = "rippledb"; // cspell: disable-line
75 // check health/stop status as records are copied
77 // minimum # of ledgers to maintain for health of network
79 // minimum # of ledgers required for standalone mode.
81 // minimum ledger to maintain online.
83
89 bool stop_ = false;
90 bool healthy_ = true;
97 int fdRequired_ = 0;
98
100 bool advisoryDelete_ = false;
111
112 // these do not exist upon SHAMapStore creation, but do exist
113 // as of run() or before
114 NetworkOPs* netOPs_ = nullptr;
118
119 static constexpr auto kNodeStoreName = "NodeStore";
120
121public:
123
125 clampFetchDepth(std::uint32_t fetchDepth) const override
126 {
127 return (deleteInterval_ != 0u) ? std::min(fetchDepth, deleteInterval_) : fetchDepth;
128 }
129
131 makeNodeStore(int readThreads) override;
132
135 {
136 if (advisoryDelete_)
137 canDelete_ = seq;
138 return stateDb_.setCanDelete(seq);
139 }
140
141 bool
142 advisoryDelete() const override
143 {
144 return advisoryDelete_;
145 }
146
147 // All ledgers prior to this one are eligible
148 // for deletion in the next rotation
150 getLastRotated() override
151 {
152 return stateDb_.getState().lastRotated;
153 }
154
155 // All ledgers before and including this are unprotected
156 // and online delete may delete them if appropriate
158 getCanDelete() override
159 {
160 return canDelete_;
161 }
162
163 void
164 onLedgerClosed(std::shared_ptr<Ledger const> const& ledger) override;
165
166 void
167 rendezvous() const override;
168 int
169 fdRequired() const override;
170
172 minimumOnline() const override;
173
174private:
175 // callback for visitNodes
176 bool
177 copyNode(std::uint64_t& nodeCount, SHAMapTreeNode const& node);
178 void
179 run();
180 void
181 dbPaths();
182
185
186 template <class CacheInstance>
187 bool
188 freshenCache(CacheInstance& cache)
189 {
190 std::uint64_t check = 0;
191
192 for (auto const& key : cache.getKeys())
193 {
194 dbRotating_->fetchNodeObject(key, 0, node_store::FetchType::Synchronous, true);
196 return true;
197 }
198
199 return false;
200 }
201
207 void
208 clearSql(
209 LedgerIndex lastRotated,
210 std::string const& tableName,
211 std::function<std::optional<LedgerIndex>()> const& getMinSeq,
212 std::function<void(LedgerIndex)> const& deleteBeforeSeq);
213 void
214 clearCaches(LedgerIndex validatedSeq);
215 void
217 void
218 clearPrior(LedgerIndex lastRotated);
219
228 [[nodiscard]] HealthResult
229 healthWait();
230
231public:
232 void
233 start() override
234 {
235 if (deleteInterval_ != 0u)
237 }
238
239 void
240 stop() override;
241};
242
243} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Holds unparsed configuration information.
Provides server functionality for clients.
Definition NetworkOPs.h:82
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::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_
std::unique_ptr< node_store::Database > makeNodeStore(int readThreads) override
static std::uint32_t const kMinimumDeletionIntervalSa
int fdRequired() const override
Returns the number of file descriptors that are needed.
std::chrono::milliseconds backOff_
std::atomic< LedgerIndex > canDelete_
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_
node_store::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.
node_store::DatabaseRotating * dbRotating_
std::unique_ptr< node_store::Backend > makeBackendRotating(std::string path=std::string())
static std::uint32_t const kMinimumDeletionInterval
SHAMapStoreImp(Application &app, node_store::Scheduler &scheduler, beast::Journal journal)
bool freshenCache(CacheInstance &cache)
void clearPrior(LedgerIndex lastRotated)
class to create database, launch online delete thread, and related SQLite database
Definition SHAMapStore.h:24
Scheduling for asynchronous backend activity.
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:370
detail::BasicFullBelowCache FullBelowCache
TaggedCache< uint256, SHAMapTreeNode, false, intr_ptr::SharedWeakUnionPtr< SHAMapTreeNode >, SHAMapTreeNodePtr > TreeNodeCache