rippled
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 <ripple/app/ledger/LedgerMaster.h>
24 #include <ripple/app/misc/SHAMapStore.h>
25 #include <ripple/app/rdb/RelationalDatabase.h>
26 #include <ripple/app/rdb/State.h>
27 #include <ripple/core/DatabaseCon.h>
28 #include <ripple/nodestore/DatabaseRotating.h>
29 
30 #include <ripple/nodestore/Scheduler.h>
31 #include <atomic>
32 #include <chrono>
33 #include <condition_variable>
34 #include <thread>
35 
36 namespace ripple {
37 
38 class NetworkOPs;
39 
41 {
42 private:
44  {
45  public:
46  soci::session sqlDb_;
49 
50  // Just instantiate without any logic in case online delete is not
51  // configured
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
61  getCanDelete();
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;
96  mutable std::mutex mutex_;
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 
122 public:
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 
139  setCanDelete(LedgerIndex seq) override
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  {
157  return state_db_.getState().lastRotated;
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 
179 private:
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  {
200  key, 0, NodeStore::FetchType::synchronous, true);
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
221  freshenCaches();
222  void
223  clearPrior(LedgerIndex lastRotated);
224 
233  [[nodiscard]] HealthResult
234  healthWait();
235 
236 public:
237  void
238  start() override
239  {
240  if (deleteInterval_)
242  }
243 
244  void
245  stop() override;
246 };
247 
248 } // namespace ripple
249 
250 #endif
ripple::SHAMapStoreImp::start
void start() override
Definition: SHAMapStoreImp.h:238
ripple::NetworkOPs
Provides server functionality for clients.
Definition: NetworkOPs.h:86
ripple::SHAMapStoreImp::SavedStateDB::setCanDelete
LedgerIndex setCanDelete(LedgerIndex canDelete)
Definition: SHAMapStoreImp.cpp:54
ripple::Application
Definition: Application.h:115
ripple::SHAMapStoreImp::dbPaths
void dbPaths()
Definition: SHAMapStoreImp.cpp:428
ripple::SHAMapStoreImp::healthy_
bool healthy_
Definition: SHAMapStoreImp.h:93
ripple::SHAMapStoreImp::scheduler_
NodeStore::Scheduler & scheduler_
Definition: SHAMapStoreImp.h:87
ripple::SHAMapStoreImp::SHAMapStoreImp
SHAMapStoreImp(Application &app, NodeStore::Scheduler &scheduler, beast::Journal journal)
Definition: SHAMapStoreImp.cpp:85
std::string
STL class.
std::shared_ptr
STL class.
ripple::TaggedCache
Map/cache combination.
Definition: Application.h:64
ripple::SHAMapStoreImp::newLedger_
std::shared_ptr< Ledger const > newLedger_
Definition: SHAMapStoreImp.h:97
ripple::SHAMapStoreImp::onLedgerClosed
void onLedgerClosed(std::shared_ptr< Ledger const > const &ledger) override
Called by LedgerMaster every time a ledger validates.
Definition: SHAMapStoreImp.cpp:235
ripple::SHAMapStoreImp::minimumDeletionInterval_
static const std::uint32_t minimumDeletionInterval_
Definition: SHAMapStoreImp.h:81
ripple::SHAMapStoreImp::dbPrefix_
const std::string dbPrefix_
Definition: SHAMapStoreImp.h:77
ripple::SHAMapStoreImp::ledgerMaster_
LedgerMaster * ledgerMaster_
Definition: SHAMapStoreImp.h:116
ripple::detail::BasicFullBelowCache
Remembers which tree keys have all descendants resident.
Definition: FullBelowCache.h:38
ripple::LedgerMaster
Definition: LedgerMaster.h:70
ripple::SHAMapStoreImp::mutex_
std::mutex mutex_
Definition: SHAMapStoreImp.h:96
ripple::SHAMapStoreImp::makeNodeStore
std::unique_ptr< NodeStore::Database > makeNodeStore(int readThreads) override
Definition: SHAMapStoreImp.cpp:170
ripple::SHAMapStoreImp::setCanDelete
LedgerIndex setCanDelete(LedgerIndex seq) override
Highest ledger that may be deleted.
Definition: SHAMapStoreImp.h:139
std::chrono::milliseconds
ripple::SHAMapStoreImp::SavedStateDB::journal_
const beast::Journal journal_
Definition: SHAMapStoreImp.h:48
ripple::SHAMapStore
class to create database, launch online delete thread, and related SQLite database
Definition: SHAMapStore.h:36
ripple::SHAMapStoreImp::stop
void stop() override
Definition: SHAMapStoreImp.cpp:702
ripple::SHAMapStoreImp
Definition: SHAMapStoreImp.h:40
ripple::SHAMapStoreImp::journal_
const beast::Journal journal_
Definition: SHAMapStoreImp.h:88
ripple::SHAMapStoreImp::cond_
std::condition_variable cond_
Definition: SHAMapStoreImp.h:94
ripple::SHAMapStoreImp::freshenCaches
void freshenCaches()
Definition: SHAMapStoreImp.cpp:608
std::function
ripple::SHAMapStoreImp::SavedStateDB::getCanDelete
LedgerIndex getCanDelete()
Definition: SHAMapStoreImp.cpp:46
ripple::SHAMapStoreImp::SavedStateDB::mutex_
std::mutex mutex_
Definition: SHAMapStoreImp.h:47
ripple::SHAMapStoreImp::nodeStoreName_
static constexpr auto nodeStoreName_
Definition: SHAMapStoreImp.h:120
ripple::SHAMapStoreImp::healthWait
HealthResult healthWait()
Definition: SHAMapStoreImp.cpp:680
beast::Journal::getNullSink
static Sink & getNullSink()
Returns a Sink which does nothing.
Definition: beast_Journal.cpp:72
ripple::SHAMapStoreImp::run
void run()
Definition: SHAMapStoreImp.cpp:280
ripple::SHAMapStoreImp::copyNode
bool copyNode(std::uint64_t &nodeCount, SHAMapTreeNode const &node)
Definition: SHAMapStoreImp.cpp:262
ripple::SHAMapStoreImp::dbRotating_
NodeStore::DatabaseRotating * dbRotating_
Definition: SHAMapStoreImp.h:89
ripple::SHAMapStoreImp::app_
Application & app_
Definition: SHAMapStoreImp.h:72
ripple::SHAMapStoreImp::clampFetchDepth
std::uint32_t clampFetchDepth(std::uint32_t fetch_depth) const override
Definition: SHAMapStoreImp.h:129
ripple::SHAMapStoreImp::getCanDelete
LedgerIndex getCanDelete() override
Highest ledger that may be deleted.
Definition: SHAMapStoreImp.h:163
ripple::SHAMapStoreImp::HealthResult
HealthResult
This is a health check for online deletion that waits until rippled is stable before returning.
Definition: SHAMapStoreImp.h:232
ripple::SHAMapStoreImp::clearSql
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.
Definition: SHAMapStoreImp.cpp:550
ripple::SHAMapStoreImp::recoveryWaitTime_
std::chrono::seconds recoveryWaitTime_
If the node is out of sync during an online_delete healthWait() call, sleep the thread for this time,...
Definition: SHAMapStoreImp.h:111
ripple::SHAMapStoreImp::stop_
bool stop_
Definition: SHAMapStoreImp.h:92
ripple::SHAMapStoreImp::SavedStateDB::SavedStateDB
SavedStateDB()
Definition: SHAMapStoreImp.h:52
thread
ripple::SHAMapStoreImp::advisoryDelete_
bool advisoryDelete_
Definition: SHAMapStoreImp.h:103
ripple::SHAMapStoreImp::advisoryDelete
bool advisoryDelete() const override
Whether advisory delete is enabled.
Definition: SHAMapStoreImp.h:147
chrono
ripple::SHAMapStoreImp::minimumOnline_
std::atomic< LedgerIndex > minimumOnline_
Definition: SHAMapStoreImp.h:85
ripple::SHAMapStoreImp::SavedStateDB::setState
void setState(SavedState const &state)
Definition: SHAMapStoreImp.cpp:70
ripple::SHAMapStoreImp::treeNodeCache_
TreeNodeCache * treeNodeCache_
Definition: SHAMapStoreImp.h:118
ripple::SHAMapTreeNode
Definition: SHAMapTreeNode.h:53
ripple::SHAMapStoreImp::working_
std::atomic< bool > working_
Definition: SHAMapStoreImp.h:98
ripple::SHAMapStoreImp::deleteInterval_
std::uint32_t deleteInterval_
Definition: SHAMapStoreImp.h:102
ripple::SHAMapStoreImp::ageThreshold_
std::chrono::seconds ageThreshold_
Definition: SHAMapStoreImp.h:106
ripple::SHAMapStoreImp::clearPrior
void clearPrior(LedgerIndex lastRotated)
Definition: SHAMapStoreImp.cpp:617
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::uint32_t
atomic
ripple::NodeStore::DatabaseRotating
Definition: DatabaseRotating.h:33
ripple::NodeStore::Scheduler
Scheduling for asynchronous backend activity.
Definition: ripple/nodestore/Scheduler.h:60
ripple::SHAMapStoreImp::checkHealthInterval_
const std::uint64_t checkHealthInterval_
Definition: SHAMapStoreImp.h:79
ripple::SHAMapStoreImp::state_db_
SavedStateDB state_db_
Definition: SHAMapStoreImp.h:90
ripple::SHAMapStoreImp::SavedStateDB::getState
SavedState getState()
Definition: SHAMapStoreImp.cpp:62
ripple::SHAMapStoreImp::SavedStateDB::sqlDb_
soci::session sqlDb_
Definition: SHAMapStoreImp.h:46
std::min
T min(T... args)
ripple::SHAMapStoreImp::backOff_
std::chrono::milliseconds backOff_
Definition: SHAMapStoreImp.h:105
ripple::SHAMapStoreImp::thread_
std::thread thread_
Definition: SHAMapStoreImp.h:91
ripple::SHAMapStoreImp::SavedStateDB::init
void init(BasicConfig const &config, std::string const &dbName)
Definition: SHAMapStoreImp.cpp:37
ripple::SHAMapStoreImp::dbName_
const std::string dbName_
Definition: SHAMapStoreImp.h:75
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::SHAMapStoreImp::stopping
@ stopping
Definition: SHAMapStoreImp.h:232
ripple::SHAMapStoreImp::fdRequired
int fdRequired() const override
Returns the number of file descriptors that are needed.
Definition: SHAMapStoreImp.cpp:256
ripple::SHAMapStoreImp::SavedStateDB::setLastRotated
void setLastRotated(LedgerIndex seq)
Definition: SHAMapStoreImp.cpp:77
condition_variable
ripple::SHAMapStoreImp::getLastRotated
LedgerIndex getLastRotated() override
Maximum ledger that has been deleted, or will be deleted if currently in the act of online deletion.
Definition: SHAMapStoreImp.h:155
ripple::SHAMapStoreImp::canDelete_
std::atomic< LedgerIndex > canDelete_
Definition: SHAMapStoreImp.h:99
ripple::SHAMapStoreImp::minimumDeletionIntervalSA_
static const std::uint32_t minimumDeletionIntervalSA_
Definition: SHAMapStoreImp.h:83
ripple::SavedState::lastRotated
LedgerIndex lastRotated
Definition: State.h:37
ripple::SHAMapStoreImp::deleteBatch_
std::uint32_t deleteBatch_
Definition: SHAMapStoreImp.h:104
ripple::SHAMapStoreImp::fullBelowCache_
FullBelowCache * fullBelowCache_
Definition: SHAMapStoreImp.h:117
ripple::SHAMapStoreImp::SavedStateDB
Definition: SHAMapStoreImp.h:43
ripple::SHAMapStoreImp::minimumOnline
std::optional< LedgerIndex > minimumOnline() const override
The minimum ledger to try and maintain in our database.
Definition: SHAMapStoreImp.cpp:716
ripple::SHAMapStoreImp::clearCaches
void clearCaches(LedgerIndex validatedSeq)
Definition: SHAMapStoreImp.cpp:601
std::optional
ripple::SHAMapStoreImp::makeBackendRotating
std::unique_ptr< NodeStore::Backend > makeBackendRotating(std::string path=std::string())
Definition: SHAMapStoreImp.cpp:521
std::mutex
STL class.
ripple::NodeStore::Database::fetchNodeObject
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:252
ripple::SHAMapStoreImp::fdRequired_
int fdRequired_
Definition: SHAMapStoreImp.h:100
ripple::SHAMapStoreImp::netOPs_
NetworkOPs * netOPs_
Definition: SHAMapStoreImp.h:115
std::unique_ptr
STL class.
ripple::NodeStore::FetchType::synchronous
@ synchronous
ripple::SHAMapStoreImp::freshenCache
bool freshenCache(CacheInstance &cache)
Definition: SHAMapStoreImp.h:193
ripple::SavedState
Definition: State.h:33
ripple::BasicConfig
Holds unparsed configuration information.
Definition: BasicConfig.h:215
ripple::SHAMapStoreImp::rendezvous
void rendezvous() const override
Definition: SHAMapStoreImp.cpp:246
ripple::SHAMapStoreImp::rendezvous_
std::condition_variable rendezvous_
Definition: SHAMapStoreImp.h:95
ripple::SHAMapStoreImp::keepGoing
@ keepGoing
Definition: SHAMapStoreImp.h:232