rippled
Shard.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2017 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_NODESTORE_SHARD_H_INCLUDED
21 #define RIPPLE_NODESTORE_SHARD_H_INCLUDED
22 
23 #include <ripple/app/ledger/Ledger.h>
24 #include <ripple/basics/BasicConfig.h>
25 #include <ripple/basics/RangeSet.h>
26 #include <ripple/core/DatabaseCon.h>
27 #include <ripple/nodestore/NodeObject.h>
28 #include <ripple/nodestore/Scheduler.h>
29 
30 #include <boost/filesystem.hpp>
31 #include <nudb/nudb.hpp>
32 
33 #include <atomic>
34 #include <tuple>
35 
36 namespace ripple {
37 namespace NodeStore {
38 
41 class DatabaseShard;
42 
43 /* A range of historical ledgers backed by a node store.
44  Shards are indexed and store `ledgersPerShard`.
45  Shard `i` stores ledgers starting with sequence: `1 + (i * ledgersPerShard)`
46  and ending with sequence: `(i + 1) * ledgersPerShard`.
47  Once a shard has all its ledgers, it is never written to again.
48 
49  Public functions can be called concurrently from any thread.
50 */
51 class Shard final
52 {
53 public:
54  Shard(
55  Application& app,
56  DatabaseShard const& db,
58  boost::filesystem::path const& dir,
59  beast::Journal j);
60 
61  Shard(
62  Application& app,
63  DatabaseShard const& db,
65  beast::Journal j);
66 
67  ~Shard();
68 
69  bool
70  open(Scheduler& scheduler, nudb::context& ctx);
71 
72  void
73  closeAll();
74 
75  boost::optional<std::uint32_t>
76  prepare();
77 
78  bool
79  store(std::shared_ptr<Ledger const> const& ledger);
80 
81  bool
82  containsLedger(std::uint32_t seq) const;
83 
84  void
85  sweep();
86 
88  index() const
89  {
90  return index_;
91  }
92 
93  boost::filesystem::path const&
94  getDir() const
95  {
96  return dir_;
97  }
98 
99  std::tuple<
103  getBackendAll() const;
104 
106  getBackend() const;
107 
110  bool
111  isBackendComplete() const;
112 
114  pCache() const;
115 
117  nCache() const;
118 
123  fileInfo() const;
124 
127  bool
128  isFinal() const;
129 
132  bool
133  isLegacy() const;
134 
142  bool
143  finalize(
144  bool const writeSQLite,
145  boost::optional<uint256> const& referenceHash);
146 
147  void
149  {
150  stop_ = true;
151  }
152 
156  void
158  {
159  removeOnDestroy_ = true;
160  }
161 
162  // Current shard version
163  static constexpr std::uint32_t version{2};
164 
165  // The finalKey is a hard coded value of zero. It is used to store
166  // finalizing shard data to the backend. The data contains a version,
167  // last ledger's hash, and the first and last ledger sequences.
168  static uint256 const finalKey;
169 
170 private:
171  struct AcquireInfo
172  {
173  // SQLite database to track information about what has been acquired
175 
176  // Tracks the sequences of ledgers acquired and stored in the backend
178  };
179 
182 
183  // Shard Index
185 
186  // First ledger sequence in the shard
188 
189  // Last ledger sequence in the shard
191 
192  // The maximum number of ledgers the shard can store
193  // The earliest shard may store fewer ledgers than subsequent shards
195 
196  // Database positive cache
198 
199  // Database negative cache
201 
202  // Path to database files
203  boost::filesystem::path const dir_;
204 
205  // Storage space utilized by the shard
207 
208  // Number of file descriptors required by the shard
210 
211  // NuDB key/value store for node objects
213 
214  // Ledger SQLite database used for indexes
216 
217  // Transaction SQLite database used for indexes
219 
220  // Tracking information used only when acquiring a shard from the network.
221  // If the shard is final, this member will be null.
223 
225 
226  // True if backend has stored all ledgers pertaining to the shard
227  bool backendComplete_{false};
228 
229  // Older shard without an acquire database or final key
230  // Eventually there will be no need for this and should be removed
231  bool legacy_{false};
232 
233  // True if the backend has a final key stored
234  bool final_{false};
235 
236  // Determines if the shard needs to stop processing for shutdown
238 
239  // Determines if the shard directory should be removed in the destructor
241 
242  // Open/Create SQLite databases
243  // Lock over mutex_ required
244  bool
246 
247  // Write SQLite entries for this ledger
248  // Lock over mutex_ required
249  bool
250  storeSQLite(
251  std::shared_ptr<Ledger const> const& ledger,
253 
254  // Set storage and file descriptor usage stats
255  // Lock over mutex_ required
256  void
258 
259  // Validate this ledger by walking its SHAMaps and verifying Merkle trees
260  bool
261  valLedger(
262  std::shared_ptr<Ledger const> const& ledger,
263  std::shared_ptr<Ledger const> const& next) const;
264 
265  // Fetches from backend and log errors based on status codes
267  valFetch(uint256 const& hash) const;
268 };
269 
270 } // namespace NodeStore
271 } // namespace ripple
272 
273 #endif
ripple::NodeStore::Shard::getBackend
std::shared_ptr< Backend > getBackend() const
Definition: Shard.cpp:365
ripple::NodeStore::Shard::dir_
const boost::filesystem::path dir_
Definition: Shard.h:203
ripple::Application
Definition: Application.h:97
std::shared_ptr
STL class.
ripple::NodeStore::Shard::lgrSQLiteDB_
std::unique_ptr< DatabaseCon > lgrSQLiteDB_
Definition: Shard.h:215
ripple::NodeStore::Shard::removeOnDestroy_
std::atomic< bool > removeOnDestroy_
Definition: Shard.h:240
ripple::TaggedCache
Map/cache combination.
Definition: TaggedCache.h:52
ripple::NodeStore::Shard::valLedger
bool valLedger(std::shared_ptr< Ledger const > const &ledger, std::shared_ptr< Ledger const > const &next) const
Definition: Shard.cpp:934
ripple::NodeStore::Shard::Shard
Shard(Application &app, DatabaseShard const &db, std::uint32_t index, boost::filesystem::path const &dir, beast::Journal j)
Definition: Shard.cpp:45
std::pair
ripple::NodeStore::Shard::store
bool store(std::shared_ptr< Ledger const > const &ledger)
Definition: Shard.cpp:279
ripple::NodeStore::Shard::backendComplete_
bool backendComplete_
Definition: Shard.h:227
ripple::NodeStore::Shard::getDir
boost::filesystem::path const & getDir() const
Definition: Shard.h:94
ripple::NodeStore::Shard::final_
bool final_
Definition: Shard.h:234
ripple::NodeStore::Shard::initSQLite
bool initSQLite(std::lock_guard< std::recursive_mutex > const &lock)
Definition: Shard.cpp:670
std::recursive_mutex
STL class.
std::lock_guard
STL class.
tuple
ripple::NodeStore::Shard::stop
void stop()
Definition: Shard.h:148
ripple::NodeStore::Shard::finalKey
static const uint256 finalKey
Definition: Shard.h:168
ripple::NodeStore::Shard::lastSeq_
const std::uint32_t lastSeq_
Definition: Shard.h:190
ripple::NodeStore::Shard::AcquireInfo
Definition: Shard.h:171
ripple::NodeStore::Shard::AcquireInfo::storedSeqs
RangeSet< std::uint32_t > storedSeqs
Definition: Shard.h:177
ripple::NodeStore::Shard::removeOnDestroy
void removeOnDestroy()
If called, the shard directory will be removed when the shard is destroyed.
Definition: Shard.h:157
ripple::NodeStore::Shard::isLegacy
bool isLegacy() const
Returns true if the shard is older, without final key data.
Definition: Shard.cpp:413
ripple::NodeStore::Shard::version
static constexpr std::uint32_t version
Definition: Shard.h:163
ripple::base_uint< 256 >
ripple::NodeStore::Shard::pCache
std::shared_ptr< PCache > pCache() const
Definition: Shard.cpp:381
ripple::NodeStore::Shard::j_
const beast::Journal j_
Definition: Shard.h:224
ripple::NodeStore::Shard::finalize
bool finalize(bool const writeSQLite, boost::optional< uint256 > const &referenceHash)
Finalize shard by walking its ledgers and verifying each Merkle tree.
Definition: Shard.cpp:420
ripple::NodeStore::Shard::fileSz_
std::uint64_t fileSz_
Definition: Shard.h:206
ripple::NodeStore::Shard::valFetch
std::shared_ptr< NodeObject > valFetch(uint256 const &hash) const
Definition: Shard.cpp:1014
ripple::NodeStore::Shard::sweep
void sweep()
Definition: Shard.cpp:343
ripple::NodeStore::Shard::closeAll
void closeAll()
Definition: Shard.cpp:250
ripple::NodeStore::DatabaseShard
A collection of historical shards.
Definition: DatabaseShard.h:37
ripple::NodeStore::Shard::nCache_
std::shared_ptr< NCache > nCache_
Definition: Shard.h:200
ripple::NodeStore::Shard::backend_
std::shared_ptr< Backend > backend_
Definition: Shard.h:212
ripple::NodeStore::Shard::firstSeq_
const std::uint32_t firstSeq_
Definition: Shard.h:187
ripple::NodeStore::Shard::pCache_
std::shared_ptr< PCache > pCache_
Definition: Shard.h:197
ripple::NodeStore::Shard::isFinal
bool isFinal() const
Returns true if the shard is complete, validated, and immutable.
Definition: Shard.cpp:406
ripple::NodeStore::Shard::AcquireInfo::SQLiteDB
std::unique_ptr< DatabaseCon > SQLiteDB
Definition: Shard.h:174
ripple::NodeStore::Shard::maxLedgers_
const std::uint32_t maxLedgers_
Definition: Shard.h:194
ripple::NodeStore::Shard::~Shard
~Shard()
Definition: Shard.cpp:65
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::uint32_t
atomic
ripple::NodeStore::Scheduler
Scheduling for asynchronous backend activity.
Definition: ripple/nodestore/Scheduler.h:57
ripple::NodeStore::Shard::acquireInfo_
std::unique_ptr< AcquireInfo > acquireInfo_
Definition: Shard.h:222
ripple::NodeStore::Shard::app_
Application & app_
Definition: Shard.h:180
ripple::NodeStore::Shard::isBackendComplete
bool isBackendComplete() const
Returns true if all shard ledgers have been stored in the backend.
Definition: Shard.cpp:374
ripple::NodeStore::Shard::mutex_
std::recursive_mutex mutex_
Definition: Shard.h:181
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::NodeStore::Shard::prepare
boost::optional< std::uint32_t > prepare()
Definition: Shard.cpp:259
ripple::NodeStore::Shard::setFileStats
void setFileStats(std::lock_guard< std::recursive_mutex > const &lock)
Definition: Shard.cpp:910
ripple::NodeStore::Shard::legacy_
bool legacy_
Definition: Shard.h:231
ripple::NodeStore::Shard::fdRequired_
std::uint32_t fdRequired_
Definition: Shard.h:209
ripple::NodeStore::Shard::index
std::uint32_t index() const
Definition: Shard.h:88
ripple::NodeStore::Shard::fileInfo
std::pair< std::uint64_t, std::uint32_t > fileInfo() const
Returns a pair where the first item describes the storage space utilized and the second item is the n...
Definition: Shard.cpp:399
ripple::RangeSet
boost::icl::interval_set< T, std::less, ClosedInterval< T > > RangeSet
A set of closed intervals over the domain T.
Definition: RangeSet.h:69
ripple::NodeStore::Shard::open
bool open(Scheduler &scheduler, nudb::context &ctx)
Definition: Shard.cpp:87
ripple::KeyCache
Maintains a cache of keys with no associated data.
Definition: KeyCache.h:43
ripple::NodeStore::Shard
Definition: Shard.h:51
ripple::NodeStore::Shard::txSQLiteDB_
std::unique_ptr< DatabaseCon > txSQLiteDB_
Definition: Shard.h:218
ripple::NodeStore::Shard::containsLedger
bool containsLedger(std::uint32_t seq) const
Definition: Shard.cpp:329
ripple::NodeStore::Shard::index_
const std::uint32_t index_
Definition: Shard.h:184
std::unique_ptr
STL class.
ripple::NodeStore::Shard::stop_
std::atomic< bool > stop_
Definition: Shard.h:237
ripple::NodeStore::Shard::nCache
std::shared_ptr< NCache > nCache() const
Definition: Shard.cpp:390
ripple::NodeStore::Shard::getBackendAll
std::tuple< std::shared_ptr< Backend >, std::shared_ptr< PCache >, std::shared_ptr< NCache > > getBackendAll() const
Definition: Shard.cpp:356
ripple::NodeStore::Shard::storeSQLite
bool storeSQLite(std::shared_ptr< Ledger const > const &ledger, std::lock_guard< std::recursive_mutex > const &lock)
Definition: Shard.cpp:742