rippled
Loading...
Searching...
No Matches
SHAMapStoreImp.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_APP_MISC_SHAMAPSTOREIMP_H_INCLUDED
21#define RIPPLE_APP_MISC_SHAMAPSTOREIMP_H_INCLUDED
22
23#include <xrpld/app/ledger/LedgerMaster.h>
24#include <xrpld/app/misc/SHAMapStore.h>
25#include <xrpld/app/rdb/State.h>
26#include <xrpld/core/DatabaseCon.h>
27
28#include <xrpl/nodestore/DatabaseRotating.h>
29#include <xrpl/nodestore/Scheduler.h>
30
31#include <atomic>
32#include <chrono>
33#include <thread>
34
35namespace ripple {
36
37class NetworkOPs;
38
40{
41private:
43 {
44 public:
45 soci::session sqlDb_;
48
49 // Just instantiate without any logic in case online delete is not
50 // configured
51 explicit SavedStateDB() : journal_{beast::Journal::getNullSink()}
52 {
53 }
54
55 // opens database and, if necessary, creates & initializes its tables.
56 void
57 init(BasicConfig const& config, std::string const& dbName);
58 // get/set the ledger index that we can delete up to and including
62 setCanDelete(LedgerIndex canDelete);
64 getState();
65 void
66 setState(SavedState const& state);
67 void
69 };
70
72
73 // name of state database
74 std::string const dbName_ = "state";
75 // prefix of on-disk nodestore backend instances
76 std::string const dbPrefix_ = "rippledb";
77 // check health/stop status as records are copied
79 // minimum # of ledgers to maintain for health of network
81 // minimum # of ledgers required for standalone mode.
83 // minimum ledger to maintain online.
85
91 bool stop_ = false;
92 bool healthy_ = true;
99 int fdRequired_ = 0;
100
102 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 nodeStoreName_ = "NodeStore";
120
121public:
123 Application& app,
124 NodeStore::Scheduler& scheduler,
125 beast::Journal journal);
126
128 clampFetchDepth(std::uint32_t fetch_depth) const override
129 {
130 return deleteInterval_ ? std::min(fetch_depth, deleteInterval_)
131 : fetch_depth;
132 }
133
135 makeNodeStore(int readThreads) override;
136
139 {
140 if (advisoryDelete_)
141 canDelete_ = seq;
142 return state_db_.setCanDelete(seq);
143 }
144
145 bool
146 advisoryDelete() const override
147 {
148 return advisoryDelete_;
149 }
150
151 // All ledgers prior to this one are eligible
152 // for deletion in the next rotation
154 getLastRotated() override
155 {
157 }
158
159 // All ledgers before and including this are unprotected
160 // and online delete may delete them if appropriate
162 getCanDelete() override
163 {
164 return canDelete_;
165 }
166
167 void
168 onLedgerClosed(std::shared_ptr<Ledger const> const& ledger) override;
169
170 void
171 rendezvous() const override;
172 int
173 fdRequired() const override;
174
176 minimumOnline() const override;
177
178private:
179 // callback for visitNodes
180 bool
181 copyNode(std::uint64_t& nodeCount, SHAMapTreeNode const& node);
182 void
183 run();
184 void
185 dbPaths();
186
189
190 template <class CacheInstance>
191 bool
192 freshenCache(CacheInstance& cache)
193 {
194 std::uint64_t check = 0;
195
196 for (auto const& key : cache.getKeys())
197 {
200 if (!(++check % checkHealthInterval_) && healthWait() == stopping)
201 return true;
202 }
203
204 return false;
205 }
206
211 void
212 clearSql(
213 LedgerIndex lastRotated,
214 std::string const& TableName,
215 std::function<std::optional<LedgerIndex>()> const& getMinSeq,
216 std::function<void(LedgerIndex)> const& deleteBeforeSeq);
217 void
218 clearCaches(LedgerIndex validatedSeq);
219 void
221 void
222 clearPrior(LedgerIndex lastRotated);
223
232 [[nodiscard]] HealthResult
233 healthWait();
234
235public:
236 void
237 start() override
238 {
239 if (deleteInterval_)
241 }
242
243 void
244 stop() override;
245};
246
247} // namespace ripple
248
249#endif
A generic endpoint for log messages.
Definition Journal.h:60
Holds unparsed configuration information.
Provides server functionality for clients.
Definition NetworkOPs.h:89
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:240
Scheduling for asynchronous backend activity.
LedgerIndex setCanDelete(LedgerIndex canDelete)
void setState(SavedState const &state)
void init(BasicConfig const &config, std::string const &dbName)
std::condition_variable rendezvous_
std::condition_variable cond_
bool freshenCache(CacheInstance &cache)
void rendezvous() const override
NodeStore::DatabaseRotating * dbRotating_
std::chrono::milliseconds backOff_
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.
bool advisoryDelete() const override
Whether advisory delete is enabled.
static constexpr auto nodeStoreName_
void clearPrior(LedgerIndex lastRotated)
std::atomic< LedgerIndex > canDelete_
std::unique_ptr< NodeStore::Backend > makeBackendRotating(std::string path=std::string())
TreeNodeCache * treeNodeCache_
std::uint32_t deleteInterval_
LedgerIndex setCanDelete(LedgerIndex seq) override
Highest ledger that may be deleted.
LedgerMaster * ledgerMaster_
void onLedgerClosed(std::shared_ptr< Ledger const > const &ledger) override
Called by LedgerMaster every time a ledger validates.
std::chrono::seconds recoveryWaitTime_
If the node is out of sync during an online_delete healthWait() call, sleep the thread for this time,...
std::string const dbPrefix_
std::uint32_t clampFetchDepth(std::uint32_t fetch_depth) const override
LedgerIndex getCanDelete() override
Highest ledger that may be deleted.
std::chrono::seconds ageThreshold_
std::atomic< LedgerIndex > minimumOnline_
std::uint64_t const checkHealthInterval_
std::unique_ptr< NodeStore::Database > makeNodeStore(int readThreads) override
std::string const dbName_
std::atomic< bool > working_
FullBelowCache * fullBelowCache_
LedgerIndex getLastRotated() override
Maximum ledger that has been deleted, or will be deleted if currently in the act of online deletion.
std::optional< LedgerIndex > minimumOnline() const override
The minimum ledger to try and maintain in our database.
HealthResult
This is a health check for online deletion that waits until rippled is stable before returning.
beast::Journal const journal_
NodeStore::Scheduler & scheduler_
static std::uint32_t const minimumDeletionIntervalSA_
int fdRequired() const override
Returns the number of file descriptors that are needed.
bool copyNode(std::uint64_t &nodeCount, SHAMapTreeNode const &node)
std::shared_ptr< Ledger const > newLedger_
static std::uint32_t const minimumDeletionInterval_
void clearCaches(LedgerIndex validatedSeq)
class to create database, launch online delete thread, and related SQLite database
Definition SHAMapStore.h:38
Map/cache combination.
Definition TaggedCache.h:62
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:25
LedgerIndex lastRotated
Definition State.h:37