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;
91 // Used to prevent ledger gaps from forming during online deletion. Keeps
92 // track of the last validated ledger that was processed without gaps. There
93 // are no guarantees about gaps while online delete is not running. For
94 // that, use advisory_delete and check for gaps externally.
96 // Used to prevent the circuit breaker from tripping too quickly.
104 int fdRequired_ = 0;
105
107 bool advisoryDelete_ = false;
124
125 // these do not exist upon SHAMapStore creation, but do exist
126 // as of run() or before
127 NetworkOPs* netOPs_ = nullptr;
131
132 static constexpr auto kNodeStoreName = "NodeStore";
133
134public:
136
138 clampFetchDepth(std::uint32_t fetchDepth) const override
139 {
140 return (deleteInterval_ != 0u) ? std::min(fetchDepth, deleteInterval_) : fetchDepth;
141 }
142
144 makeNodeStore(int readThreads) override;
145
148 {
149 if (advisoryDelete_)
150 canDelete_ = seq;
151 return stateDb_.setCanDelete(seq);
152 }
153
154 bool
155 advisoryDelete() const override
156 {
157 return advisoryDelete_;
158 }
159
160 // All ledgers prior to this one are eligible
161 // for deletion in the next rotation
163 getLastRotated() override
164 {
165 return stateDb_.getState().lastRotated;
166 }
167
168 // All ledgers before and including this are unprotected
169 // and online delete may delete them if appropriate
171 getCanDelete() override
172 {
173 return canDelete_;
174 }
175
176 void
177 onLedgerClosed(std::shared_ptr<Ledger const> const& ledger) override;
178
179 [[nodiscard]]
180 bool
181 rendezvous(std::optional<std::chrono::milliseconds> const& timeout = {}) const override;
182 int
183 fdRequired() const override;
184
186 minimumOnline() const override;
187
188private:
189 // callback for visitNodes
190 bool
191 copyNode(std::uint64_t& nodeCount, SHAMapTreeNode const& node);
192 void
193 run();
194 void
195 dbPaths();
196
199
200 template <class CacheInstance>
201 bool
202 freshenCache(CacheInstance& cache)
203 {
204 std::uint64_t check = 0;
205
206 for (auto const& key : cache.getKeys())
207 {
208 dbRotating_->fetchNodeObject(key, 0, node_store::FetchType::Synchronous, true);
210 return true;
211 }
212
213 return false;
214 }
215
221 void
222 clearSql(
223 LedgerIndex lastRotated,
224 std::string const& tableName,
225 std::function<std::optional<LedgerIndex>()> const& getMinSeq,
226 std::function<void(LedgerIndex)> const& deleteBeforeSeq);
227 void
228 clearCaches(LedgerIndex validatedSeq);
229 void
231 void
232 clearPrior(LedgerIndex lastRotated);
233
242 [[nodiscard]] HealthResult
243 healthWait();
244
245public:
246 void
247 start() override
248 {
249 if (deleteInterval_ != 0u)
251 }
252
253 void
254 stop() override;
255};
256
257} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Holds unparsed configuration information.
Tracks the current ledger and any ledgers in the process of closing.
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, or any recent ledgers are not available during an online_delete healthWai...
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_
LedgerIndex lastSuccessfulHealthCheck_
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_
LedgerIndex lastGoodValidatedLedger_
HealthResult healthWait()
void clearCaches(LedgerIndex validatedSeq)
std::chrono::seconds ageThreshold_
LedgerMaster * ledgerMaster_
beast::Journal const journal_
LedgerIndex getCanDelete() override
Highest ledger that may be deleted.
bool rendezvous(std::optional< std::chrono::milliseconds > const &timeout={}) const override
static constexpr auto kNodeStoreName
std::string const dbPrefix_
node_store::Scheduler & scheduler_
std::uint32_t maxWaitingLedgers_
If the rotation stays "unhealthy" for a very long time, the process is aborted, and tried again later...
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:25
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:382
detail::BasicFullBelowCache FullBelowCache
TaggedCache< UInt256, SHAMapTreeNode, false, intr_ptr::SharedWeakUnionPtr< SHAMapTreeNode >, SHAMapTreeNodePtr > TreeNodeCache