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/core/DatabaseCon.h>
26 #include <ripple/core/Stoppable.h>
27 #include <ripple/nodestore/DatabaseRotating.h>
28 #include <atomic>
29 #include <chrono>
30 #include <condition_variable>
31 #include <thread>
32 
33 namespace ripple {
34 
35 class NetworkOPs;
36 
37 class SHAMapStoreImp : public Stoppable, public SHAMapStore
38 {
39 private:
40  struct SavedState
41  {
45  };
46 
48 
50  {
51  public:
52  soci::session session_;
55 
56  // Just instantiate without any logic in case online delete is not
57  // configured
59  {
60  }
61 
62  // opens database and, if necessary, creates & initializes its tables.
63  void
64  init(BasicConfig const& config, std::string const& dbName);
65  // get/set the ledger index that we can delete up to and including
67  getCanDelete();
69  setCanDelete(LedgerIndex canDelete);
70  SavedState
71  getState();
72  void
73  setState(SavedState const& state);
74  void
76  };
77 
79 
80  // name of state database
81  std::string const dbName_ = "state";
82  // prefix of on-disk nodestore backend instances
83  std::string const dbPrefix_ = "rippledb";
84  // check health/stop status as records are copied
86  // minimum # of ledgers to maintain for health of network
88  // minimum # of ledgers required for standalone mode.
90  // minimum ledger to maintain online.
92 
98  bool stop_ = false;
99  bool healthy_ = true;
106  int fdRequired_ = 0;
107 
109  bool advisoryDelete_ = false;
118  boost::optional<std::chrono::seconds> recoveryWaitTime_;
119 
120  // these do not exist upon SHAMapStore creation, but do exist
121  // as of run() or before
122  NetworkOPs* netOPs_ = nullptr;
127  DatabaseCon* ledgerDb_ = nullptr;
128 
129  static constexpr auto nodeStoreName_ = "NodeStore";
130 
131 public:
133  Application& app,
134  Stoppable& parent,
135  NodeStore::Scheduler& scheduler,
136  beast::Journal journal);
137 
139  {
140  if (thread_.joinable())
141  thread_.join();
142  }
143 
145  clampFetchDepth(std::uint32_t fetch_depth) const override
146  {
147  return deleteInterval_ ? std::min(fetch_depth, deleteInterval_)
148  : fetch_depth;
149  }
150 
152  makeNodeStore(std::string const& name, std::int32_t readThreads) override;
153 
155  setCanDelete(LedgerIndex seq) override
156  {
157  if (advisoryDelete_)
158  canDelete_ = seq;
159  return state_db_.setCanDelete(seq);
160  }
161 
162  bool
163  advisoryDelete() const override
164  {
165  return advisoryDelete_;
166  }
167 
168  // All ledgers prior to this one are eligible
169  // for deletion in the next rotation
171  getLastRotated() override
172  {
173  return state_db_.getState().lastRotated;
174  }
175 
176  // All ledgers before and including this are unprotected
177  // and online delete may delete them if appropriate
179  getCanDelete() override
180  {
181  return canDelete_;
182  }
183 
184  void
185  onLedgerClosed(std::shared_ptr<Ledger const> const& ledger) override;
186 
187  void
188  rendezvous() const override;
189  int
190  fdRequired() const override;
191 
192  boost::optional<LedgerIndex>
193  minimumOnline() const override;
194 
195 private:
196  // callback for visitNodes
197  bool
198  copyNode(std::uint64_t& nodeCount, SHAMapTreeNode const& node);
199  void
200  run();
201  void
202  dbPaths();
203 
206 
207  template <class CacheInstance>
208  bool
209  freshenCache(CacheInstance& cache)
210  {
211  std::uint64_t check = 0;
212 
213  for (auto const& key : cache.getKeys())
214  {
216  if (!(++check % checkHealthInterval_) && health())
217  return true;
218  }
219 
220  return false;
221  }
222 
227  void
228  clearSql(
229  DatabaseCon& database,
230  LedgerIndex lastRotated,
231  std::string const& minQuery,
232  std::string const& deleteQuery);
233  void
234  clearCaches(LedgerIndex validatedSeq);
235  void
236  freshenCaches();
237  void
238  clearPrior(LedgerIndex lastRotated);
239 
240  // If rippled is not healthy, defer rotate-delete.
241  // If already unhealthy, do not change state on further check.
242  // Assume that, once unhealthy, a necessary step has been
243  // aborted, so the online-delete process needs to restart
244  // at next ledger.
245  // If recoveryWaitTime_ is set, this may sleep to give rippled
246  // time to recover, so never call it from any thread other than
247  // the main "run()".
248  Health
249  health();
250 
251  //
252  // Stoppable
253  //
254  void
255  onStart() override
256  {
257  if (deleteInterval_)
259  }
260 
261  // Called when the application begins shutdown
262  void
263  onStop() override;
264  // Called when all child Stoppable objects have stoped
265 };
266 
267 } // namespace ripple
268 
269 #endif
ripple::NetworkOPs
Provides server functionality for clients.
Definition: NetworkOPs.h:88
ripple::SHAMapStoreImp::SavedStateDB::setCanDelete
LedgerIndex setCanDelete(LedgerIndex canDelete)
Definition: SHAMapStoreImp.cpp:100
ripple::SHAMapStoreImp::health
Health health()
Definition: SHAMapStoreImp.cpp:716
ripple::Application
Definition: Application.h:101
ripple::SHAMapStoreImp::dbPaths
void dbPaths()
Definition: SHAMapStoreImp.cpp:478
ripple::SHAMapStoreImp::SHAMapStoreImp
SHAMapStoreImp(Application &app, Stoppable &parent, NodeStore::Scheduler &scheduler, beast::Journal journal)
Definition: SHAMapStoreImp.cpp:149
ripple::SHAMapStoreImp::healthy_
bool healthy_
Definition: SHAMapStoreImp.h:99
ripple::SHAMapStoreImp::scheduler_
NodeStore::Scheduler & scheduler_
Definition: SHAMapStoreImp.h:93
std::string
STL class.
ripple::SHAMapStoreImp::ledgerDb_
DatabaseCon * ledgerDb_
Definition: SHAMapStoreImp.h:127
std::shared_ptr
STL class.
ripple::TaggedCache
Map/cache combination.
Definition: TaggedCache.h:52
ripple::SHAMapStoreImp::newLedger_
std::shared_ptr< Ledger const > newLedger_
Definition: SHAMapStoreImp.h:103
ripple::SHAMapStoreImp::onLedgerClosed
void onLedgerClosed(std::shared_ptr< Ledger const > const &ledger) override
Called by LedgerMaster every time a ledger validates.
Definition: SHAMapStoreImp.cpp:290
ripple::SHAMapStoreImp::~SHAMapStoreImp
~SHAMapStoreImp()
Definition: SHAMapStoreImp.h:138
ripple::SHAMapStoreImp::minimumDeletionInterval_
static const std::uint32_t minimumDeletionInterval_
Definition: SHAMapStoreImp.h:87
ripple::SHAMapStoreImp::dbPrefix_
const std::string dbPrefix_
Definition: SHAMapStoreImp.h:83
ripple::SHAMapStoreImp::ledgerMaster_
LedgerMaster * ledgerMaster_
Definition: SHAMapStoreImp.h:123
ripple::detail::BasicFullBelowCache
Remembers which tree keys have all descendants resident.
Definition: FullBelowCache.h:37
ripple::LedgerMaster
Definition: LedgerMaster.h:72
ripple::SHAMapStoreImp::mutex_
std::mutex mutex_
Definition: SHAMapStoreImp.h:102
ripple::SHAMapStoreImp::onStart
void onStart() override
Override called during start.
Definition: SHAMapStoreImp.h:255
ripple::SHAMapStoreImp::setCanDelete
LedgerIndex setCanDelete(LedgerIndex seq) override
Highest ledger that may be deleted.
Definition: SHAMapStoreImp.h:155
std::chrono::milliseconds
ripple::SHAMapStoreImp::SavedStateDB::journal_
const beast::Journal journal_
Definition: SHAMapStoreImp.h:54
ripple::SHAMapStore
class to create database, launch online delete thread, and related SQLite database
Definition: SHAMapStore.h:37
ripple::SHAMapStoreImp::transactionDb_
DatabaseCon * transactionDb_
Definition: SHAMapStoreImp.h:126
ripple::SHAMapStoreImp
Definition: SHAMapStoreImp.h:37
ripple::SHAMapStoreImp::journal_
const beast::Journal journal_
Definition: SHAMapStoreImp.h:94
ripple::SHAMapStoreImp::cond_
std::condition_variable cond_
Definition: SHAMapStoreImp.h:100
ripple::SHAMapStoreImp::freshenCaches
void freshenCaches()
Definition: SHAMapStoreImp.cpp:661
ripple::SHAMapStoreImp::SavedStateDB::getCanDelete
LedgerIndex getCanDelete()
Definition: SHAMapStoreImp.cpp:87
ripple::SHAMapStoreImp::SavedStateDB::mutex_
std::mutex mutex_
Definition: SHAMapStoreImp.h:53
ripple::SHAMapStoreImp::nodeStoreName_
static constexpr auto nodeStoreName_
Definition: SHAMapStoreImp.h:129
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:331
ripple::SHAMapStoreImp::copyNode
bool copyNode(std::uint64_t &nodeCount, SHAMapTreeNode const &node)
Definition: SHAMapStoreImp.cpp:317
ripple::SHAMapStoreImp::dbRotating_
NodeStore::DatabaseRotating * dbRotating_
Definition: SHAMapStoreImp.h:95
ripple::SHAMapStoreImp::clearSql
void clearSql(DatabaseCon &database, LedgerIndex lastRotated, std::string const &minQuery, std::string const &deleteQuery)
delete from sqlite table in batches to not lock the db excessively.
Definition: SHAMapStoreImp.cpp:594
ripple::SHAMapStoreImp::app_
Application & app_
Definition: SHAMapStoreImp.h:78
ripple::SHAMapStoreImp::clampFetchDepth
std::uint32_t clampFetchDepth(std::uint32_t fetch_depth) const override
Definition: SHAMapStoreImp.h:145
ripple::SHAMapStoreImp::getCanDelete
LedgerIndex getCanDelete() override
Highest ledger that may be deleted.
Definition: SHAMapStoreImp.h:179
std::thread::joinable
T joinable(T... args)
ripple::Stoppable
Provides an interface for starting and stopping.
Definition: Stoppable.h:201
ripple::SHAMapStoreImp::stop_
bool stop_
Definition: SHAMapStoreImp.h:98
ripple::SHAMapStoreImp::SavedStateDB::SavedStateDB
SavedStateDB()
Definition: SHAMapStoreImp.h:58
thread
ripple::SHAMapStoreImp::SavedStateDB::session_
soci::session session_
Definition: SHAMapStoreImp.h:52
ripple::SHAMapStoreImp::advisoryDelete_
bool advisoryDelete_
Definition: SHAMapStoreImp.h:109
ripple::SHAMapStoreImp::ok
@ ok
Definition: SHAMapStoreImp.h:47
ripple::SHAMapStoreImp::advisoryDelete
bool advisoryDelete() const override
Whether advisory delete is enabled.
Definition: SHAMapStoreImp.h:163
chrono
ripple::SHAMapStoreImp::minimumOnline_
std::atomic< LedgerIndex > minimumOnline_
Definition: SHAMapStoreImp.h:91
ripple::SHAMapStoreImp::SavedStateDB::setState
void setState(SavedState const &state)
Definition: SHAMapStoreImp.cpp:126
ripple::SHAMapStoreImp::treeNodeCache_
TreeNodeCache * treeNodeCache_
Definition: SHAMapStoreImp.h:125
ripple::SHAMapTreeNode
Definition: SHAMapTreeNode.h:133
ripple::SHAMapStoreImp::working_
std::atomic< bool > working_
Definition: SHAMapStoreImp.h:104
ripple::SHAMapStoreImp::deleteInterval_
std::uint32_t deleteInterval_
Definition: SHAMapStoreImp.h:108
ripple::SHAMapStoreImp::onStop
void onStop() override
Override called when the stop notification is issued.
Definition: SHAMapStoreImp.cpp:760
ripple::SHAMapStoreImp::ageThreshold_
std::chrono::seconds ageThreshold_
Definition: SHAMapStoreImp.h:112
ripple::SHAMapStoreImp::minimumOnline
boost::optional< LedgerIndex > minimumOnline() const override
The minimum ledger to try and maintain in our database.
Definition: SHAMapStoreImp.cpp:781
ripple::SHAMapStoreImp::SavedState::writableDb
std::string writableDb
Definition: SHAMapStoreImp.h:42
ripple::SHAMapStoreImp::clearPrior
void clearPrior(LedgerIndex lastRotated)
Definition: SHAMapStoreImp.cpp:670
ripple::SHAMapStoreImp::SavedStateDB::getState
SavedState getState()
Definition: SHAMapStoreImp.cpp:111
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::uint32_t
atomic
ripple::SHAMapStoreImp::SavedState
Definition: SHAMapStoreImp.h:40
ripple::SHAMapStoreImp::recoveryWaitTime_
boost::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:118
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:145
ripple::SHAMapStoreImp::Health
Health
Definition: SHAMapStoreImp.h:47
ripple::SHAMapStoreImp::checkHealthInterval_
const std::uint64_t checkHealthInterval_
Definition: SHAMapStoreImp.h:85
ripple::SHAMapStoreImp::state_db_
SavedStateDB state_db_
Definition: SHAMapStoreImp.h:96
std::min
T min(T... args)
ripple::SHAMapStoreImp::backOff_
std::chrono::milliseconds backOff_
Definition: SHAMapStoreImp.h:111
ripple::SHAMapStoreImp::thread_
std::thread thread_
Definition: SHAMapStoreImp.h:97
ripple::SHAMapStoreImp::SavedState::archiveDb
std::string archiveDb
Definition: SHAMapStoreImp.h:43
ripple::SHAMapStoreImp::SavedStateDB::init
void init(BasicConfig const &config, std::string const &dbName)
Definition: SHAMapStoreImp.cpp:32
ripple::SHAMapStoreImp::dbName_
const std::string dbName_
Definition: SHAMapStoreImp.h:81
ripple::SHAMapStoreImp::unhealthy
@ unhealthy
Definition: SHAMapStoreImp.h:47
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::SHAMapStoreImp::SavedState::lastRotated
LedgerIndex lastRotated
Definition: SHAMapStoreImp.h:44
ripple::SHAMapStoreImp::fdRequired
int fdRequired() const override
Returns the number of file descriptors that are needed.
Definition: SHAMapStoreImp.cpp:311
ripple::SHAMapStoreImp::SavedStateDB::setLastRotated
void setLastRotated(LedgerIndex seq)
Definition: SHAMapStoreImp.cpp:139
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:171
ripple::SHAMapStoreImp::canDelete_
std::atomic< LedgerIndex > canDelete_
Definition: SHAMapStoreImp.h:105
ripple::SHAMapStoreImp::minimumDeletionIntervalSA_
static const std::uint32_t minimumDeletionIntervalSA_
Definition: SHAMapStoreImp.h:89
ripple::DatabaseCon
Definition: DatabaseCon.h:81
ripple::SHAMapStoreImp::deleteBatch_
std::uint32_t deleteBatch_
Definition: SHAMapStoreImp.h:110
ripple::SHAMapStoreImp::fullBelowCache_
FullBelowCache * fullBelowCache_
Definition: SHAMapStoreImp.h:124
ripple::SHAMapStoreImp::SavedStateDB
Definition: SHAMapStoreImp.h:49
ripple::SHAMapStoreImp::clearCaches
void clearCaches(LedgerIndex validatedSeq)
Definition: SHAMapStoreImp.cpp:654
ripple::SHAMapStoreImp::makeBackendRotating
std::unique_ptr< NodeStore::Backend > makeBackendRotating(std::string path=std::string())
Definition: SHAMapStoreImp.cpp:566
std::mutex
STL class.
ripple::SHAMapStoreImp::makeNodeStore
std::unique_ptr< NodeStore::Database > makeNodeStore(std::string const &name, std::int32_t readThreads) override
Definition: SHAMapStoreImp.cpp:236
ripple::SHAMapStoreImp::stopping
@ stopping
Definition: SHAMapStoreImp.h:47
ripple::SHAMapStoreImp::fdRequired_
int fdRequired_
Definition: SHAMapStoreImp.h:106
ripple::SHAMapStoreImp::netOPs_
NetworkOPs * netOPs_
Definition: SHAMapStoreImp.h:122
std::unique_ptr
STL class.
ripple::SHAMapStoreImp::freshenCache
bool freshenCache(CacheInstance &cache)
Definition: SHAMapStoreImp.h:209
ripple::BasicConfig
Holds unparsed configuration information.
Definition: BasicConfig.h:178
ripple::SHAMapStoreImp::rendezvous
void rendezvous() const override
Definition: SHAMapStoreImp.cpp:301
std::thread::join
T join(T... args)
ripple::SHAMapStoreImp::rendezvous_
std::condition_variable rendezvous_
Definition: SHAMapStoreImp.h:101