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  beast::Journal j);
59 
60  ~Shard();
61 
62  bool
63  open(Scheduler& scheduler, nudb::context& ctx);
64 
65  boost::optional<std::uint32_t>
66  prepare();
67 
68  bool
69  store(std::shared_ptr<Ledger const> const& ledger);
70 
71  bool
72  containsLedger(std::uint32_t seq) const;
73 
74  void
75  sweep();
76 
78  index() const {return index_;}
79 
80  boost::filesystem::path const&
81  getDir() const {return dir_;}
82 
83  std::tuple<
87  getBackendAll() const;
88 
90  getBackend() const;
91 
94  bool
95  isBackendComplete() const;
96 
98  pCache() const;
99 
101  nCache() const;
102 
107  fileInfo() const;
108 
111  bool
112  isFinal() const;
113 
116  bool
117  isLegacy() const;
118 
124  bool
125  finalize(const bool writeSQLite);
126 
127  void
128  stop() {stop_ = true;}
129 
133  void
135 
136  // Current shard version
137  static constexpr std::uint32_t version {2};
138 
139  // The finalKey is a hard coded value of zero. It is used to store
140  // finalizing shard data to the backend. The data contains a version,
141  // last ledger's hash, and the first and last ledger sequences.
142  static uint256 const finalKey;
143 
144 private:
145  struct AcquireInfo
146  {
147  // SQLite database to track information about what has been acquired
149 
150  // Tracks the sequences of ledgers acquired and stored in the backend
152  };
153 
156 
157  // Shard Index
159 
160  // First ledger sequence in the shard
162 
163  // Last ledger sequence in the shard
165 
166  // The maximum number of ledgers the shard can store
167  // The earliest shard may store fewer ledgers than subsequent shards
169 
170  // Database positive cache
172 
173  // Database negative cache
175 
176  // Path to database files
177  boost::filesystem::path const dir_;
178 
179  // Storage space utilized by the shard
181 
182  // Number of file descriptors required by the shard
184 
185  // NuDB key/value store for node objects
187 
188  // Ledger SQLite database used for indexes
190 
191  // Transaction SQLite database used for indexes
193 
194  // Tracking information used only when acquiring a shard from the network.
195  // If the shard is complete, this member will be null.
197 
199 
200  // True if backend has stored all ledgers pertaining to the shard
201  bool backendComplete_ {false};
202 
203  // Older shard without an acquire database or final key
204  // Eventually there will be no need for this and should be removed
205  bool legacy_ {false};
206 
207  // True if the backend has a final key stored
208  bool final_ {false};
209 
210  // Determines if the shard needs to stop processing for shutdown
212 
213  // Determines if the shard directory should be removed in the destructor
215 
216  // Set the backend cache
217  // Lock over mutex_ required
218  void
220 
221  // Open/Create SQLite databases
222  // Lock over mutex_ required
223  bool
225 
226  // Write SQLite entries for this ledger
227  // Lock over mutex_ required
228  bool
229  storeSQLite(
230  std::shared_ptr<Ledger const> const& ledger,
232 
233  // Set storage and file descriptor usage stats
234  // Lock over mutex_ required
235  void
237 
238  // Validate this ledger by walking its SHAMaps and verifying Merkle trees
239  bool
240  valLedger(
241  std::shared_ptr<Ledger const> const& ledger,
242  std::shared_ptr<Ledger const> const& next) const;
243 
244  // Fetches from backend and log errors based on status codes
246  valFetch(uint256 const& hash) const;
247 };
248 
249 } // namespace NodeStore
250 } // namespace ripple
251 
252 #endif
ripple::NodeStore::Shard::getBackend
std::shared_ptr< Backend > getBackend() const
Definition: Shard.cpp:355
ripple::NodeStore::Shard::dir_
const boost::filesystem::path dir_
Definition: Shard.h:177
ripple::Application
Definition: Application.h:85
std::shared_ptr
STL class.
ripple::NodeStore::Shard::lgrSQLiteDB_
std::unique_ptr< DatabaseCon > lgrSQLiteDB_
Definition: Shard.h:189
ripple::NodeStore::Shard::removeOnDestroy_
std::atomic< bool > removeOnDestroy_
Definition: Shard.h:214
ripple::TaggedCache
Map/cache combination.
Definition: TaggedCache.h:55
ripple::NodeStore::Shard::setBackendCache
void setBackendCache(std::lock_guard< std::recursive_mutex > const &lock)
Definition: Shard.cpp:646
ripple::NodeStore::Shard::valLedger
bool valLedger(std::shared_ptr< Ledger const > const &ledger, std::shared_ptr< Ledger const > const &next) const
Definition: Shard.cpp:969
std::pair
ripple::NodeStore::Shard::store
bool store(std::shared_ptr< Ledger const > const &ledger)
Definition: Shard.cpp:262
ripple::NodeStore::Shard::backendComplete_
bool backendComplete_
Definition: Shard.h:201
ripple::NodeStore::Shard::getDir
boost::filesystem::path const & getDir() const
Definition: Shard.h:81
ripple::NodeStore::Shard::final_
bool final_
Definition: Shard.h:208
ripple::NodeStore::Shard::initSQLite
bool initSQLite(std::lock_guard< std::recursive_mutex > const &lock)
Definition: Shard.cpp:680
std::recursive_mutex
STL class.
std::lock_guard
STL class.
tuple
ripple::NodeStore::Shard::stop
void stop()
Definition: Shard.h:128
ripple::NodeStore::Shard::finalKey
static const uint256 finalKey
Definition: Shard.h:142
ripple::NodeStore::Shard::lastSeq_
const std::uint32_t lastSeq_
Definition: Shard.h:164
ripple::NodeStore::Shard::AcquireInfo
Definition: Shard.h:145
ripple::NodeStore::Shard::AcquireInfo::storedSeqs
RangeSet< std::uint32_t > storedSeqs
Definition: Shard.h:151
ripple::NodeStore::Shard::removeOnDestroy
void removeOnDestroy()
If called, the shard directory will be removed when the shard is destroyed.
Definition: Shard.h:134
ripple::NodeStore::Shard::isLegacy
bool isLegacy() const
Returns true if the shard is older, without final key data.
Definition: Shard.cpp:403
ripple::NodeStore::Shard::version
static constexpr std::uint32_t version
Definition: Shard.h:137
ripple::base_uint< 256 >
ripple::NodeStore::Shard::pCache
std::shared_ptr< PCache > pCache() const
Definition: Shard.cpp:371
ripple::NodeStore::Shard::j_
const beast::Journal j_
Definition: Shard.h:198
ripple::NodeStore::Shard::fileSz_
std::uint64_t fileSz_
Definition: Shard.h:180
ripple::NodeStore::Shard::valFetch
std::shared_ptr< NodeObject > valFetch(uint256 const &hash) const
Definition: Shard.cpp:1050
ripple::NodeStore::Shard::sweep
void sweep()
Definition: Shard.cpp:333
ripple::NodeStore::DatabaseShard
A collection of historical shards.
Definition: DatabaseShard.h:37
ripple::NodeStore::Shard::nCache_
std::shared_ptr< NCache > nCache_
Definition: Shard.h:174
ripple::NodeStore::Shard::backend_
std::shared_ptr< Backend > backend_
Definition: Shard.h:186
ripple::NodeStore::Shard::firstSeq_
const std::uint32_t firstSeq_
Definition: Shard.h:161
ripple::NodeStore::Shard::pCache_
std::shared_ptr< PCache > pCache_
Definition: Shard.h:171
ripple::NodeStore::Shard::isFinal
bool isFinal() const
Returns true if the shard is complete, validated, and immutable.
Definition: Shard.cpp:396
ripple::NodeStore::Shard::AcquireInfo::SQLiteDB
std::unique_ptr< DatabaseCon > SQLiteDB
Definition: Shard.h:148
ripple::NodeStore::Shard::maxLedgers_
const std::uint32_t maxLedgers_
Definition: Shard.h:168
ripple::NodeStore::Shard::~Shard
~Shard()
Definition: Shard.cpp:55
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:60
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:196
ripple::NodeStore::Shard::app_
Application & app_
Definition: Shard.h:154
ripple::NodeStore::Shard::isBackendComplete
bool isBackendComplete() const
Returns true if all shard ledgers have been stored in the backend.
Definition: Shard.cpp:364
ripple::NodeStore::Shard::mutex_
std::recursive_mutex mutex_
Definition: Shard.h:155
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:241
ripple::NodeStore::Shard::Shard
Shard(Application &app, DatabaseShard const &db, std::uint32_t index, beast::Journal j)
Definition: Shard.cpp:37
ripple::NodeStore::Shard::setFileStats
void setFileStats(std::lock_guard< std::recursive_mutex > const &lock)
Definition: Shard.cpp:943
ripple::NodeStore::Shard::legacy_
bool legacy_
Definition: Shard.h:205
ripple::NodeStore::Shard::fdRequired_
std::uint32_t fdRequired_
Definition: Shard.h:183
ripple::NodeStore::Shard::index
std::uint32_t index() const
Definition: Shard.h:78
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:389
ripple::RangeSet
boost::icl::interval_set< T, std::less, ClosedInterval< T > > RangeSet
A set of closed intervals over the domain T.
Definition: RangeSet.h:70
ripple::NodeStore::Shard::open
bool open(Scheduler &scheduler, nudb::context &ctx)
Definition: Shard.cpp:79
ripple::KeyCache< uint256 >
ripple::NodeStore::Shard
Definition: Shard.h:51
ripple::NodeStore::Shard::txSQLiteDB_
std::unique_ptr< DatabaseCon > txSQLiteDB_
Definition: Shard.h:192
ripple::NodeStore::Shard::containsLedger
bool containsLedger(std::uint32_t seq) const
Definition: Shard.cpp:319
ripple::NodeStore::Shard::index_
const std::uint32_t index_
Definition: Shard.h:158
std::unique_ptr
STL class.
ripple::NodeStore::Shard::stop_
std::atomic< bool > stop_
Definition: Shard.h:211
ripple::NodeStore::Shard::nCache
std::shared_ptr< NCache > nCache() const
Definition: Shard.cpp:380
ripple::NodeStore::Shard::getBackendAll
std::tuple< std::shared_ptr< Backend >, std::shared_ptr< PCache >, std::shared_ptr< NCache > > getBackendAll() const
Definition: Shard.cpp:346
ripple::NodeStore::Shard::finalize
bool finalize(const bool writeSQLite)
Finalize shard by walking its ledgers and verifying each Merkle tree.
Definition: Shard.cpp:410
ripple::NodeStore::Shard::storeSQLite
bool storeSQLite(std::shared_ptr< Ledger const > const &ledger, std::lock_guard< std::recursive_mutex > const &lock)
Definition: Shard.cpp:756