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#include <xrpld/nodestore/DatabaseRotating.h>
28#include <xrpld/nodestore/Scheduler.h>
29
30#include <atomic>
31#include <chrono>
32#include <thread>
33
34namespace ripple {
35
36class NetworkOPs;
37
39{
40private:
42 {
43 public:
44 soci::session sqlDb_;
47
48 // Just instantiate without any logic in case online delete is not
49 // configured
50 explicit SavedStateDB() : journal_{beast::Journal::getNullSink()}
51 {
52 }
53
54 // opens database and, if necessary, creates & initializes its tables.
55 void
56 init(BasicConfig const& config, std::string const& dbName);
57 // get/set the ledger index that we can delete up to and including
61 setCanDelete(LedgerIndex canDelete);
63 getState();
64 void
65 setState(SavedState const& state);
66 void
68 };
69
71
72 // name of state database
73 std::string const dbName_ = "state";
74 // prefix of on-disk nodestore backend instances
75 std::string const dbPrefix_ = "rippledb";
76 // check health/stop status as records are copied
78 // minimum # of ledgers to maintain for health of network
80 // minimum # of ledgers required for standalone mode.
82 // minimum ledger to maintain online.
84
90 bool stop_ = false;
91 bool healthy_ = true;
98 int fdRequired_ = 0;
99
101 bool advisoryDelete_ = false;
110
111 // these do not exist upon SHAMapStore creation, but do exist
112 // as of run() or before
113 NetworkOPs* netOPs_ = nullptr;
117
118 static constexpr auto nodeStoreName_ = "NodeStore";
119
120public:
122 Application& app,
123 NodeStore::Scheduler& scheduler,
124 beast::Journal journal);
125
127 clampFetchDepth(std::uint32_t fetch_depth) const override
128 {
129 return deleteInterval_ ? std::min(fetch_depth, deleteInterval_)
130 : fetch_depth;
131 }
132
134 makeNodeStore(int readThreads) override;
135
138 {
139 if (advisoryDelete_)
140 canDelete_ = seq;
141 return state_db_.setCanDelete(seq);
142 }
143
144 bool
145 advisoryDelete() const override
146 {
147 return advisoryDelete_;
148 }
149
150 // All ledgers prior to this one are eligible
151 // for deletion in the next rotation
153 getLastRotated() override
154 {
156 }
157
158 // All ledgers before and including this are unprotected
159 // and online delete may delete them if appropriate
161 getCanDelete() override
162 {
163 return canDelete_;
164 }
165
166 void
167 onLedgerClosed(std::shared_ptr<Ledger const> const& ledger) override;
168
169 void
170 rendezvous() const override;
171 int
172 fdRequired() const override;
173
175 minimumOnline() const override;
176
177private:
178 // callback for visitNodes
179 bool
180 copyNode(std::uint64_t& nodeCount, SHAMapTreeNode const& node);
181 void
182 run();
183 void
184 dbPaths();
185
188
189 template <class CacheInstance>
190 bool
191 freshenCache(CacheInstance& cache)
192 {
193 std::uint64_t check = 0;
194
195 for (auto const& key : cache.getKeys())
196 {
199 if (!(++check % checkHealthInterval_) && healthWait() == stopping)
200 return true;
201 }
202
203 return false;
204 }
205
210 void
211 clearSql(
212 LedgerIndex lastRotated,
213 std::string const& TableName,
214 std::function<std::optional<LedgerIndex>()> const& getMinSeq,
215 std::function<void(LedgerIndex)> const& deleteBeforeSeq);
216 void
217 clearCaches(LedgerIndex validatedSeq);
218 void
220 void
221 clearPrior(LedgerIndex lastRotated);
222
231 [[nodiscard]] HealthResult
232 healthWait();
233
234public:
235 void
236 start() override
237 {
238 if (deleteInterval_)
240 }
241
242 void
243 stop() override;
244};
245
246} // namespace ripple
247
248#endif
A generic endpoint for log messages.
Definition: Journal.h:60
Holds unparsed configuration information.
Definition: BasicConfig.h:218
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:241
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: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:26
LedgerIndex lastRotated
Definition: State.h:37