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