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 
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 
46  {
47  public:
48  soci::session sqlDb_;
51 
52  // Just instantiate without any logic in case online delete is not
53  // configured
55  {
56  }
57 
58  // opens database and, if necessary, creates & initializes its tables.
59  void
60  init(BasicConfig const& config, std::string const& dbName);
61  // get/set the ledger index that we can delete up to and including
63  getCanDelete();
65  setCanDelete(LedgerIndex canDelete);
67  getState();
68  void
69  setState(SavedState const& state);
70  void
72  };
73 
75 
76  // name of state database
77  std::string const dbName_ = "state";
78  // prefix of on-disk nodestore backend instances
79  std::string const dbPrefix_ = "rippledb";
80  // check health/stop status as records are copied
82  // minimum # of ledgers to maintain for health of network
84  // minimum # of ledgers required for standalone mode.
86  // minimum ledger to maintain online.
88 
94  bool stop_ = false;
95  bool healthy_ = true;
98  mutable std::mutex mutex_;
102  int fdRequired_ = 0;
103 
105  bool advisoryDelete_ = false;
115 
116  // these do not exist upon SHAMapStore creation, but do exist
117  // as of run() or before
118  NetworkOPs* netOPs_ = nullptr;
122 
123  static constexpr auto nodeStoreName_ = "NodeStore";
124 
125 public:
127  Application& app,
128  NodeStore::Scheduler& scheduler,
129  beast::Journal journal);
130 
132  clampFetchDepth(std::uint32_t fetch_depth) const override
133  {
134  return deleteInterval_ ? std::min(fetch_depth, deleteInterval_)
135  : fetch_depth;
136  }
137 
139  makeNodeStore(std::int32_t readThreads) override;
140 
142  setCanDelete(LedgerIndex seq) override
143  {
144  if (advisoryDelete_)
145  canDelete_ = seq;
146  return state_db_.setCanDelete(seq);
147  }
148 
149  bool
150  advisoryDelete() const override
151  {
152  return advisoryDelete_;
153  }
154 
155  // All ledgers prior to this one are eligible
156  // for deletion in the next rotation
158  getLastRotated() override
159  {
160  return state_db_.getState().lastRotated;
161  }
162 
163  // All ledgers before and including this are unprotected
164  // and online delete may delete them if appropriate
166  getCanDelete() override
167  {
168  return canDelete_;
169  }
170 
171  void
172  onLedgerClosed(std::shared_ptr<Ledger const> const& ledger) override;
173 
174  void
175  rendezvous() const override;
176  int
177  fdRequired() const override;
178 
180  minimumOnline() const override;
181 
182 private:
183  // callback for visitNodes
184  bool
185  copyNode(std::uint64_t& nodeCount, SHAMapTreeNode const& node);
186  void
187  run();
188  void
189  dbPaths();
190 
193 
194  template <class CacheInstance>
195  bool
196  freshenCache(CacheInstance& cache)
197  {
198  std::uint64_t check = 0;
199 
200  for (auto const& key : cache.getKeys())
201  {
203  key, 0, NodeStore::FetchType::synchronous, true);
204  if (!(++check % checkHealthInterval_) && health())
205  return true;
206  }
207 
208  return false;
209  }
210 
215  void
216  clearSql(
217  LedgerIndex lastRotated,
218  std::string const& TableName,
219  std::function<std::optional<LedgerIndex>()> const& getMinSeq,
220  std::function<void(LedgerIndex)> const& deleteBeforeSeq);
221  void
222  clearCaches(LedgerIndex validatedSeq);
223  void
224  freshenCaches();
225  void
226  clearPrior(LedgerIndex lastRotated);
227 
228  // If rippled is not healthy, defer rotate-delete.
229  // If already unhealthy, do not change state on further check.
230  // Assume that, once unhealthy, a necessary step has been
231  // aborted, so the online-delete process needs to restart
232  // at next ledger.
233  // If recoveryWaitTime_ is set, this may sleep to give rippled
234  // time to recover, so never call it from any thread other than
235  // the main "run()".
236  Health
237  health();
238 
239 public:
240  void
241  start() override
242  {
243  if (deleteInterval_)
245  }
246 
247  void
248  stop() override;
249 };
250 
251 } // namespace ripple
252 
253 #endif
ripple::SHAMapStoreImp::start
void start() override
Definition: SHAMapStoreImp.h:241
ripple::NetworkOPs
Provides server functionality for clients.
Definition: NetworkOPs.h:86
ripple::SHAMapStoreImp::SavedStateDB::setCanDelete
LedgerIndex setCanDelete(LedgerIndex canDelete)
Definition: SHAMapStoreImp.cpp:53
ripple::SHAMapStoreImp::health
Health health()
Definition: SHAMapStoreImp.cpp:691
ripple::Application
Definition: Application.h:115
ripple::SHAMapStoreImp::dbPaths
void dbPaths()
Definition: SHAMapStoreImp.cpp:442
ripple::SHAMapStoreImp::healthy_
bool healthy_
Definition: SHAMapStoreImp.h:95
ripple::SHAMapStoreImp::scheduler_
NodeStore::Scheduler & scheduler_
Definition: SHAMapStoreImp.h:89
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:99
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:83
ripple::SHAMapStoreImp::dbPrefix_
const std::string dbPrefix_
Definition: SHAMapStoreImp.h:79
ripple::SHAMapStoreImp::ledgerMaster_
LedgerMaster * ledgerMaster_
Definition: SHAMapStoreImp.h:119
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:98
ripple::SHAMapStoreImp::setCanDelete
LedgerIndex setCanDelete(LedgerIndex seq) override
Highest ledger that may be deleted.
Definition: SHAMapStoreImp.h:142
std::chrono::milliseconds
ripple::SHAMapStoreImp::SavedStateDB::journal_
const beast::Journal journal_
Definition: SHAMapStoreImp.h:50
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:114
ripple::SHAMapStoreImp::stop
void stop() override
Definition: SHAMapStoreImp.cpp:735
ripple::SHAMapStoreImp
Definition: SHAMapStoreImp.h:40
ripple::SHAMapStoreImp::journal_
const beast::Journal journal_
Definition: SHAMapStoreImp.h:90
ripple::SHAMapStoreImp::cond_
std::condition_variable cond_
Definition: SHAMapStoreImp.h:96
ripple::SHAMapStoreImp::freshenCaches
void freshenCaches()
Definition: SHAMapStoreImp.cpp:617
std::function
ripple::SHAMapStoreImp::SavedStateDB::getCanDelete
LedgerIndex getCanDelete()
Definition: SHAMapStoreImp.cpp:45
ripple::SHAMapStoreImp::SavedStateDB::mutex_
std::mutex mutex_
Definition: SHAMapStoreImp.h:49
ripple::SHAMapStoreImp::nodeStoreName_
static constexpr auto nodeStoreName_
Definition: SHAMapStoreImp.h:123
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: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:91
ripple::SHAMapStoreImp::app_
Application & app_
Definition: SHAMapStoreImp.h:74
ripple::SHAMapStoreImp::clampFetchDepth
std::uint32_t clampFetchDepth(std::uint32_t fetch_depth) const override
Definition: SHAMapStoreImp.h:132
ripple::SHAMapStoreImp::getCanDelete
LedgerIndex getCanDelete() override
Highest ledger that may be deleted.
Definition: SHAMapStoreImp.h:166
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:559
ripple::SHAMapStoreImp::stop_
bool stop_
Definition: SHAMapStoreImp.h:94
ripple::SHAMapStoreImp::SavedStateDB::SavedStateDB
SavedStateDB()
Definition: SHAMapStoreImp.h:54
thread
ripple::SHAMapStoreImp::advisoryDelete_
bool advisoryDelete_
Definition: SHAMapStoreImp.h:105
ripple::SHAMapStoreImp::ok
@ ok
Definition: SHAMapStoreImp.h:43
ripple::SHAMapStoreImp::advisoryDelete
bool advisoryDelete() const override
Whether advisory delete is enabled.
Definition: SHAMapStoreImp.h:150
chrono
ripple::SHAMapStoreImp::minimumOnline_
std::atomic< LedgerIndex > minimumOnline_
Definition: SHAMapStoreImp.h:87
ripple::SHAMapStoreImp::SavedStateDB::setState
void setState(SavedState const &state)
Definition: SHAMapStoreImp.cpp:69
ripple::SHAMapStoreImp::treeNodeCache_
TreeNodeCache * treeNodeCache_
Definition: SHAMapStoreImp.h:121
ripple::SHAMapTreeNode
Definition: SHAMapTreeNode.h:53
ripple::SHAMapStoreImp::working_
std::atomic< bool > working_
Definition: SHAMapStoreImp.h:100
ripple::SHAMapStoreImp::deleteInterval_
std::uint32_t deleteInterval_
Definition: SHAMapStoreImp.h:104
ripple::SHAMapStoreImp::ageThreshold_
std::chrono::seconds ageThreshold_
Definition: SHAMapStoreImp.h:108
ripple::SHAMapStoreImp::clearPrior
void clearPrior(LedgerIndex lastRotated)
Definition: SHAMapStoreImp.cpp:626
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::SHAMapStoreImp::Health
Health
Definition: SHAMapStoreImp.h:43
ripple::SHAMapStoreImp::checkHealthInterval_
const std::uint64_t checkHealthInterval_
Definition: SHAMapStoreImp.h:81
ripple::SHAMapStoreImp::state_db_
SavedStateDB state_db_
Definition: SHAMapStoreImp.h:92
ripple::SHAMapStoreImp::SavedStateDB::getState
SavedState getState()
Definition: SHAMapStoreImp.cpp:61
ripple::SHAMapStoreImp::SavedStateDB::sqlDb_
soci::session sqlDb_
Definition: SHAMapStoreImp.h:48
std::min
T min(T... args)
ripple::SHAMapStoreImp::backOff_
std::chrono::milliseconds backOff_
Definition: SHAMapStoreImp.h:107
ripple::SHAMapStoreImp::thread_
std::thread thread_
Definition: SHAMapStoreImp.h:93
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:77
ripple::SHAMapStoreImp::unhealthy
@ unhealthy
Definition: SHAMapStoreImp.h:43
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:158
ripple::SHAMapStoreImp::canDelete_
std::atomic< LedgerIndex > canDelete_
Definition: SHAMapStoreImp.h:101
ripple::SHAMapStoreImp::minimumDeletionIntervalSA_
static const std::uint32_t minimumDeletionIntervalSA_
Definition: SHAMapStoreImp.h:85
ripple::SavedState::lastRotated
LedgerIndex lastRotated
Definition: RelationalDBInterface_global.h:173
ripple::SHAMapStoreImp::deleteBatch_
std::uint32_t deleteBatch_
Definition: SHAMapStoreImp.h:106
ripple::SHAMapStoreImp::fullBelowCache_
FullBelowCache * fullBelowCache_
Definition: SHAMapStoreImp.h:120
ripple::SHAMapStoreImp::SavedStateDB
Definition: SHAMapStoreImp.h:45
ripple::SHAMapStoreImp::minimumOnline
std::optional< LedgerIndex > minimumOnline() const override
The minimum ledger to try and maintain in our database.
Definition: SHAMapStoreImp.cpp:749
ripple::SHAMapStoreImp::clearCaches
void clearCaches(LedgerIndex validatedSeq)
Definition: SHAMapStoreImp.cpp:610
std::optional< std::chrono::seconds >
ripple::SHAMapStoreImp::makeBackendRotating
std::unique_ptr< NodeStore::Backend > makeBackendRotating(std::string path=std::string())
Definition: SHAMapStoreImp.cpp:530
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:158
ripple::SHAMapStoreImp::stopping
@ stopping
Definition: SHAMapStoreImp.h:43
ripple::SHAMapStoreImp::makeNodeStore
std::unique_ptr< NodeStore::Database > makeNodeStore(std::int32_t readThreads) override
Definition: SHAMapStoreImp.cpp:169
ripple::SHAMapStoreImp::fdRequired_
int fdRequired_
Definition: SHAMapStoreImp.h:102
ripple::SHAMapStoreImp::netOPs_
NetworkOPs * netOPs_
Definition: SHAMapStoreImp.h:118
std::unique_ptr
STL class.
ripple::NodeStore::FetchType::synchronous
@ synchronous
ripple::SHAMapStoreImp::freshenCache
bool freshenCache(CacheInstance &cache)
Definition: SHAMapStoreImp.h:196
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:245
ripple::SHAMapStoreImp::rendezvous_
std::condition_variable rendezvous_
Definition: SHAMapStoreImp.h:97