rippled
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
11#include <atomic>
12#include <chrono>
13#include <thread>
14
15namespace xrpl {
16
17class NetworkOPs;
18
20{
21private:
23 {
24 public:
25 soci::session sqlDb_;
28
29 // Just instantiate without any logic in case online delete is not
30 // configured
31 explicit SavedStateDB() : journal_{beast::Journal::getNullSink()}
32 {
33 }
34
35 // opens database and, if necessary, creates & initializes its tables.
36 void
37 init(BasicConfig const& config, std::string const& dbName);
38 // get/set the ledger index that we can delete up to and including
42 setCanDelete(LedgerIndex canDelete);
44 getState();
45 void
46 setState(SavedState const& state);
47 void
49 };
50
52
53 // name of state database
54 std::string const dbName_ = "state";
55 // prefix of on-disk nodestore backend instances
56 std::string const dbPrefix_ = "rippledb"; // cspell: disable-line
57 // check health/stop status as records are copied
59 // minimum # of ledgers to maintain for health of network
61 // minimum # of ledgers required for standalone mode.
63 // minimum ledger to maintain online.
65
71 bool stop_ = false;
72 bool healthy_ = true;
79 int fdRequired_ = 0;
80
82 bool advisoryDelete_ = false;
91
92 // these do not exist upon SHAMapStore creation, but do exist
93 // as of run() or before
94 NetworkOPs* netOPs_ = nullptr;
96
97 static constexpr auto nodeStoreName_ = "NodeStore";
98
99public:
101
103 clampFetchDepth(std::uint32_t fetch_depth) const override
104 {
105 return deleteInterval_ ? std::min(fetch_depth, deleteInterval_) : fetch_depth;
106 }
107
109 makeNodeStore(int readThreads) override;
110
113 {
114 if (advisoryDelete_)
115 canDelete_ = seq;
116 return state_db_.setCanDelete(seq);
117 }
118
119 bool
120 advisoryDelete() const override
121 {
122 return advisoryDelete_;
123 }
124
125 // All ledgers prior to this one are eligible
126 // for deletion in the next rotation
128 getLastRotated() override
129 {
131 }
132
133 // All ledgers before and including this are unprotected
134 // and online delete may delete them if appropriate
136 getCanDelete() override
137 {
138 return canDelete_;
139 }
140
141 void
142 onLedgerClosed(std::shared_ptr<Ledger const> const& ledger) override;
143
144 void
145 rendezvous() const override;
146 int
147 fdRequired() const override;
148
150 minimumOnline() const override;
151
152private:
153 // callback for visitNodes
154 bool
155 copyNode(std::uint64_t& nodeCount, SHAMapTreeNode const& node);
156 void
157 run();
158 void
159 dbPaths();
160
163
164 template <class CacheInstance>
165 bool
166 freshenCache(CacheInstance& cache)
167 {
168 std::uint64_t check = 0;
169
170 for (auto const& key : cache.getKeys())
171 {
173 if (!(++check % checkHealthInterval_) && healthWait() == stopping)
174 return true;
175 }
176
177 return false;
178 }
179
184 void
185 clearSql(
186 LedgerIndex lastRotated,
187 std::string const& TableName,
188 std::function<std::optional<LedgerIndex>()> const& getMinSeq,
189 std::function<void(LedgerIndex)> const& deleteBeforeSeq);
190 void
191 clearCaches(LedgerIndex validatedSeq);
192 void
194 void
195 clearPrior(LedgerIndex lastRotated);
196
205 [[nodiscard]] HealthResult
206 healthWait();
207
208public:
209 void
210 start() override
211 {
212 if (deleteInterval_)
214 }
215
216 void
217 stop() override;
218};
219
220} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:40
Holds unparsed configuration information.
Provides server functionality for clients.
Definition NetworkOPs.h:71
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:202
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,...
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.
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:18
T min(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
LedgerIndex lastRotated
Definition State.h:15