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/RelationalDBInterface.h>
26 #include <ripple/app/rdb/RelationalDBInterface_global.h>
27 #include <ripple/core/DatabaseCon.h>
28 #include <ripple/nodestore/DatabaseRotating.h>
29 #include <atomic>
30 #include <chrono>
31 #include <condition_variable>
32 #include <thread>
33 
34 namespace ripple {
35 
36 class NetworkOPs;
37 
39 {
40 private:
42 
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;
113 
114  // these do not exist upon SHAMapStore creation, but do exist
115  // as of run() or before
116  NetworkOPs* netOPs_ = nullptr;
120 
121  static constexpr auto nodeStoreName_ = "NodeStore";
122 
123 public:
125  Application& app,
126  NodeStore::Scheduler& scheduler,
127  beast::Journal journal);
128 
130  clampFetchDepth(std::uint32_t fetch_depth) const override
131  {
132  return deleteInterval_ ? std::min(fetch_depth, deleteInterval_)
133  : fetch_depth;
134  }
135 
137  makeNodeStore(std::int32_t readThreads) override;
138 
140  setCanDelete(LedgerIndex seq) override
141  {
142  if (advisoryDelete_)
143  canDelete_ = seq;
144  return state_db_.setCanDelete(seq);
145  }
146 
147  bool
148  advisoryDelete() const override
149  {
150  return advisoryDelete_;
151  }
152 
153  // All ledgers prior to this one are eligible
154  // for deletion in the next rotation
156  getLastRotated() override
157  {
158  return state_db_.getState().lastRotated;
159  }
160 
161  // All ledgers before and including this are unprotected
162  // and online delete may delete them if appropriate
164  getCanDelete() override
165  {
166  return canDelete_;
167  }
168 
169  void
170  onLedgerClosed(std::shared_ptr<Ledger const> const& ledger) override;
171 
172  void
173  rendezvous() const override;
174  int
175  fdRequired() const override;
176 
178  minimumOnline() const override;
179 
180 private:
181  // callback for visitNodes
182  bool
183  copyNode(std::uint64_t& nodeCount, SHAMapTreeNode const& node);
184  void
185  run();
186  void
187  dbPaths();
188 
191 
192  template <class CacheInstance>
193  bool
194  freshenCache(CacheInstance& cache)
195  {
196  std::uint64_t check = 0;
197 
198  for (auto const& key : cache.getKeys())
199  {
201  if (!(++check % checkHealthInterval_) && health())
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 
225  // If rippled is not healthy, defer rotate-delete.
226  // If already unhealthy, do not change state on further check.
227  // Assume that, once unhealthy, a necessary step has been
228  // aborted, so the online-delete process needs to restart
229  // at next ledger.
230  // If recoveryWaitTime_ is set, this may sleep to give rippled
231  // time to recover, so never call it from any thread other than
232  // the main "run()".
233  Health
234  health();
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:51
ripple::SHAMapStoreImp::health
Health health()
Definition: SHAMapStoreImp.cpp:668
ripple::Application
Definition: Application.h:115
ripple::SHAMapStoreImp::dbPaths
void dbPaths()
Definition: SHAMapStoreImp.cpp:419
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:82
std::string
STL class.
std::shared_ptr
STL class.
ripple::TaggedCache
Map/cache combination.
Definition: Application.h:63
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:215
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:117
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::setCanDelete
LedgerIndex setCanDelete(LedgerIndex seq) override
Highest ledger that may be deleted.
Definition: SHAMapStoreImp.h:140
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::recoveryWaitTime_
std::optional< std::chrono::seconds > recoveryWaitTime_
If set, and the node is out of sync during an online_delete health check, sleep the thread for this t...
Definition: SHAMapStoreImp.h:112
ripple::SHAMapStoreImp::stop
void stop() override
Definition: SHAMapStoreImp.cpp:712
ripple::SHAMapStoreImp
Definition: SHAMapStoreImp.h:38
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:594
std::function
ripple::SHAMapStoreImp::SavedStateDB::getCanDelete
LedgerIndex getCanDelete()
Definition: SHAMapStoreImp.cpp:43
ripple::SHAMapStoreImp::SavedStateDB::mutex_
std::mutex mutex_
Definition: SHAMapStoreImp.h:47
ripple::SHAMapStoreImp::nodeStoreName_
static constexpr auto nodeStoreName_
Definition: SHAMapStoreImp.h:121
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:256
ripple::SHAMapStoreImp::copyNode
bool copyNode(std::uint64_t &nodeCount, SHAMapTreeNode const &node)
Definition: SHAMapStoreImp.cpp:242
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:130
ripple::SHAMapStoreImp::getCanDelete
LedgerIndex getCanDelete() override
Highest ledger that may be deleted.
Definition: SHAMapStoreImp.h:164
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:536
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::ok
@ ok
Definition: SHAMapStoreImp.h:41
ripple::SHAMapStoreImp::advisoryDelete
bool advisoryDelete() const override
Whether advisory delete is enabled.
Definition: SHAMapStoreImp.h:148
chrono
ripple::SHAMapStoreImp::minimumOnline_
std::atomic< LedgerIndex > minimumOnline_
Definition: SHAMapStoreImp.h:85
ripple::SHAMapStoreImp::SavedStateDB::setState
void setState(SavedState const &state)
Definition: SHAMapStoreImp.cpp:67
ripple::SHAMapStoreImp::treeNodeCache_
TreeNodeCache * treeNodeCache_
Definition: SHAMapStoreImp.h:119
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:603
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::uint8_t
atomic
ripple::NodeStore::DatabaseRotating
Definition: DatabaseRotating.h:33
ripple::NodeStore::Scheduler
Scheduling for asynchronous backend activity.
Definition: ripple/nodestore/Scheduler.h:60
ripple::NodeStore::Database::fetchNodeObject
std::shared_ptr< NodeObject > fetchNodeObject(uint256 const &hash, std::uint32_t ledgerSeq=0, FetchType fetchType=FetchType::synchronous)
Fetch a node object.
Definition: Database.cpp:158
ripple::SHAMapStoreImp::Health
Health
Definition: SHAMapStoreImp.h:41
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:59
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:34
ripple::SHAMapStoreImp::dbName_
const std::string dbName_
Definition: SHAMapStoreImp.h:75
ripple::SHAMapStoreImp::unhealthy
@ unhealthy
Definition: SHAMapStoreImp.h:41
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::SHAMapStoreImp::fdRequired
int fdRequired() const override
Returns the number of file descriptors that are needed.
Definition: SHAMapStoreImp.cpp:236
ripple::SHAMapStoreImp::SavedStateDB::setLastRotated
void setLastRotated(LedgerIndex seq)
Definition: SHAMapStoreImp.cpp:74
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:156
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: RelationalDBInterface_global.h:173
ripple::SHAMapStoreImp::deleteBatch_
std::uint32_t deleteBatch_
Definition: SHAMapStoreImp.h:104
ripple::SHAMapStoreImp::fullBelowCache_
FullBelowCache * fullBelowCache_
Definition: SHAMapStoreImp.h:118
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:726
ripple::SHAMapStoreImp::clearCaches
void clearCaches(LedgerIndex validatedSeq)
Definition: SHAMapStoreImp.cpp:587
std::optional< std::chrono::seconds >
ripple::SHAMapStoreImp::makeBackendRotating
std::unique_ptr< NodeStore::Backend > makeBackendRotating(std::string path=std::string())
Definition: SHAMapStoreImp.cpp:507
std::mutex
STL class.
ripple::SHAMapStoreImp::stopping
@ stopping
Definition: SHAMapStoreImp.h:41
ripple::SHAMapStoreImp::makeNodeStore
std::unique_ptr< NodeStore::Database > makeNodeStore(std::int32_t readThreads) override
Definition: SHAMapStoreImp.cpp:167
ripple::SHAMapStoreImp::fdRequired_
int fdRequired_
Definition: SHAMapStoreImp.h:100
ripple::SHAMapStoreImp::netOPs_
NetworkOPs * netOPs_
Definition: SHAMapStoreImp.h:116
std::unique_ptr
STL class.
ripple::SHAMapStoreImp::freshenCache
bool freshenCache(CacheInstance &cache)
Definition: SHAMapStoreImp.h:194
ripple::SavedState
Definition: RelationalDBInterface_global.h:169
ripple::BasicConfig
Holds unparsed configuration information.
Definition: BasicConfig.h:215
ripple::SHAMapStoreImp::rendezvous
void rendezvous() const override
Definition: SHAMapStoreImp.cpp:226
ripple::SHAMapStoreImp::rendezvous_
std::condition_variable rendezvous_
Definition: SHAMapStoreImp.h:95