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;
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(int 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  key, 0, NodeStore::FetchType::synchronous, true);
202  if (!(++check % checkHealthInterval_) && stopping())
203  return true;
204  }
205 
206  return false;
207  }
208 
213  void
214  clearSql(
215  LedgerIndex lastRotated,
216  std::string const& TableName,
217  std::function<std::optional<LedgerIndex>()> const& getMinSeq,
218  std::function<void(LedgerIndex)> const& deleteBeforeSeq);
219  void
220  clearCaches(LedgerIndex validatedSeq);
221  void
222  freshenCaches();
223  void
224  clearPrior(LedgerIndex lastRotated);
225 
233  bool
234  stopping();
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:53
ripple::Application
Definition: Application.h:115
ripple::SHAMapStoreImp::dbPaths
void dbPaths()
Definition: SHAMapStoreImp.cpp:414
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:84
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:234
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::makeNodeStore
std::unique_ptr< NodeStore::Database > makeNodeStore(int readThreads) override
Definition: SHAMapStoreImp.cpp:169
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::stop
void stop() override
Definition: SHAMapStoreImp.cpp:688
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:594
std::function
ripple::SHAMapStoreImp::SavedStateDB::getCanDelete
LedgerIndex getCanDelete()
Definition: SHAMapStoreImp.cpp:45
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::stopping
bool stopping()
This is a health check for online deletion that waits until rippled is stable until returning.
Definition: SHAMapStoreImp.cpp:666
ripple::SHAMapStoreImp::run
void run()
Definition: SHAMapStoreImp.cpp:279
ripple::SHAMapStoreImp::copyNode
bool copyNode(std::uint64_t &nodeCount, SHAMapTreeNode const &node)
Definition: SHAMapStoreImp.cpp:261
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::recoveryWaitTime_
std::chrono::seconds recoveryWaitTime_
If the node is out of sync during an online_delete health check, sleep the thread for this time,...
Definition: SHAMapStoreImp.h:112
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: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:69
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::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:61
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:36
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::fdRequired
int fdRequired() const override
Returns the number of file descriptors that are needed.
Definition: SHAMapStoreImp.cpp:255
ripple::SHAMapStoreImp::SavedStateDB::setLastRotated
void setLastRotated(LedgerIndex seq)
Definition: SHAMapStoreImp.cpp:76
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: State.h:37
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:702
ripple::SHAMapStoreImp::clearCaches
void clearCaches(LedgerIndex validatedSeq)
Definition: SHAMapStoreImp.cpp:587
std::optional
ripple::SHAMapStoreImp::makeBackendRotating
std::unique_ptr< NodeStore::Backend > makeBackendRotating(std::string path=std::string())
Definition: SHAMapStoreImp.cpp:507
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:226
ripple::SHAMapStoreImp::fdRequired_
int fdRequired_
Definition: SHAMapStoreImp.h:100
ripple::SHAMapStoreImp::netOPs_
NetworkOPs * netOPs_
Definition: SHAMapStoreImp.h:116
std::unique_ptr
STL class.
ripple::NodeStore::FetchType::synchronous
@ synchronous
ripple::SHAMapStoreImp::freshenCache
bool freshenCache(CacheInstance &cache)
Definition: SHAMapStoreImp.h:194
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:245
ripple::SHAMapStoreImp::rendezvous_
std::condition_variable rendezvous_
Definition: SHAMapStoreImp.h:95