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