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/RelationalDatabase.h>
26#include <xrpld/app/rdb/State.h>
27#include <xrpld/core/DatabaseCon.h>
28#include <xrpld/nodestore/DatabaseRotating.h>
29
30#include <xrpld/nodestore/Scheduler.h>
31#include <atomic>
32#include <chrono>
33#include <condition_variable>
34#include <thread>
35
36namespace ripple {
37
38class NetworkOPs;
39
41{
42private:
44 {
45 public:
46 soci::session sqlDb_;
49
50 // Just instantiate without any logic in case online delete is not
51 // configured
52 explicit SavedStateDB() : journal_{beast::Journal::getNullSink()}
53 {
54 }
55
56 // opens database and, if necessary, creates & initializes its tables.
57 void
58 init(BasicConfig const& config, std::string const& dbName);
59 // get/set the ledger index that we can delete up to and including
63 setCanDelete(LedgerIndex canDelete);
65 getState();
66 void
67 setState(SavedState const& state);
68 void
70 };
71
73
74 // name of state database
75 std::string const dbName_ = "state";
76 // prefix of on-disk nodestore backend instances
77 std::string const dbPrefix_ = "rippledb";
78 // check health/stop status as records are copied
80 // minimum # of ledgers to maintain for health of network
82 // minimum # of ledgers required for standalone mode.
84 // minimum ledger to maintain online.
86
92 bool stop_ = false;
93 bool healthy_ = true;
100 int fdRequired_ = 0;
101
103 bool advisoryDelete_ = false;
112
113 // these do not exist upon SHAMapStore creation, but do exist
114 // as of run() or before
115 NetworkOPs* netOPs_ = nullptr;
119
120 static constexpr auto nodeStoreName_ = "NodeStore";
121
122public:
124 Application& app,
125 NodeStore::Scheduler& scheduler,
126 beast::Journal journal);
127
129 clampFetchDepth(std::uint32_t fetch_depth) const override
130 {
131 return deleteInterval_ ? std::min(fetch_depth, deleteInterval_)
132 : fetch_depth;
133 }
134
136 makeNodeStore(int readThreads) override;
137
140 {
141 if (advisoryDelete_)
142 canDelete_ = seq;
143 return state_db_.setCanDelete(seq);
144 }
145
146 bool
147 advisoryDelete() const override
148 {
149 return advisoryDelete_;
150 }
151
152 // All ledgers prior to this one are eligible
153 // for deletion in the next rotation
155 getLastRotated() override
156 {
158 }
159
160 // All ledgers before and including this are unprotected
161 // and online delete may delete them if appropriate
163 getCanDelete() override
164 {
165 return canDelete_;
166 }
167
168 void
169 onLedgerClosed(std::shared_ptr<Ledger const> const& ledger) override;
170
171 void
172 rendezvous() const override;
173 int
174 fdRequired() const override;
175
177 minimumOnline() const override;
178
179private:
180 // callback for visitNodes
181 bool
182 copyNode(std::uint64_t& nodeCount, SHAMapTreeNode const& node);
183 void
184 run();
185 void
186 dbPaths();
187
190
191 template <class CacheInstance>
192 bool
193 freshenCache(CacheInstance& cache)
194 {
195 std::uint64_t check = 0;
196
197 for (auto const& key : cache.getKeys())
198 {
201 if (!(++check % checkHealthInterval_) && healthWait() == stopping)
202 return true;
203 }
204
205 return false;
206 }
207
212 void
213 clearSql(
214 LedgerIndex lastRotated,
215 std::string const& TableName,
216 std::function<std::optional<LedgerIndex>()> const& getMinSeq,
217 std::function<void(LedgerIndex)> const& deleteBeforeSeq);
218 void
219 clearCaches(LedgerIndex validatedSeq);
220 void
222 void
223 clearPrior(LedgerIndex lastRotated);
224
233 [[nodiscard]] HealthResult
234 healthWait();
235
236public:
237 void
238 start() override
239 {
240 if (deleteInterval_)
242 }
243
244 void
245 stop() override;
246};
247
248} // namespace ripple
249
250#endif
A generic endpoint for log messages.
Definition: Journal.h:59
Holds unparsed configuration information.
Definition: BasicConfig.h:219
Provides server functionality for clients.
Definition: NetworkOPs.h:87
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:239
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_
void start() override
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 deleteBatch_
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:37
Map/cache combination.
Definition: TaggedCache.h:57
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:26
LedgerIndex lastRotated
Definition: State.h:36