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
79  {
80  return index_;
81  }
82 
83  boost::filesystem::path const&
84  getDir() const
85  {
86  return dir_;
87  }
88 
89  std::tuple<
93  getBackendAll() const;
94 
96  getBackend() const;
97 
100  bool
101  isBackendComplete() const;
102 
104  pCache() const;
105 
107  nCache() const;
108 
113  fileInfo() const;
114 
117  bool
118  isFinal() const;
119 
122  bool
123  isLegacy() const;
124 
132  bool
133  finalize(
134  bool const writeSQLite,
135  boost::optional<uint256> const& referenceHash);
136 
137  void
139  {
140  stop_ = true;
141  }
142 
146  void
148  {
149  removeOnDestroy_ = true;
150  }
151 
152  // Current shard version
153  static constexpr std::uint32_t version{2};
154 
155  // The finalKey is a hard coded value of zero. It is used to store
156  // finalizing shard data to the backend. The data contains a version,
157  // last ledger's hash, and the first and last ledger sequences.
158  static uint256 const finalKey;
159 
160 private:
161  struct AcquireInfo
162  {
163  // SQLite database to track information about what has been acquired
165 
166  // Tracks the sequences of ledgers acquired and stored in the backend
168  };
169 
172 
173  // Shard Index
175 
176  // First ledger sequence in the shard
178 
179  // Last ledger sequence in the shard
181 
182  // The maximum number of ledgers the shard can store
183  // The earliest shard may store fewer ledgers than subsequent shards
185 
186  // Database positive cache
188 
189  // Database negative cache
191 
192  // Path to database files
193  boost::filesystem::path const dir_;
194 
195  // Storage space utilized by the shard
197 
198  // Number of file descriptors required by the shard
200 
201  // NuDB key/value store for node objects
203 
204  // Ledger SQLite database used for indexes
206 
207  // Transaction SQLite database used for indexes
209 
210  // Tracking information used only when acquiring a shard from the network.
211  // If the shard is final, this member will be null.
213 
215 
216  // True if backend has stored all ledgers pertaining to the shard
217  bool backendComplete_{false};
218 
219  // Older shard without an acquire database or final key
220  // Eventually there will be no need for this and should be removed
221  bool legacy_{false};
222 
223  // True if the backend has a final key stored
224  bool final_{false};
225 
226  // Determines if the shard needs to stop processing for shutdown
228 
229  // Determines if the shard directory should be removed in the destructor
231 
232  // Set the backend cache
233  // Lock over mutex_ required
234  void
236 
237  // Open/Create SQLite databases
238  // Lock over mutex_ required
239  bool
241 
242  // Write SQLite entries for this ledger
243  // Lock over mutex_ required
244  bool
245  storeSQLite(
246  std::shared_ptr<Ledger const> const& ledger,
248 
249  // Set storage and file descriptor usage stats
250  // Lock over mutex_ required
251  void
253 
254  // Validate this ledger by walking its SHAMaps and verifying Merkle trees
255  bool
256  valLedger(
257  std::shared_ptr<Ledger const> const& ledger,
258  std::shared_ptr<Ledger const> const& next) const;
259 
260  // Fetches from backend and log errors based on status codes
262  valFetch(uint256 const& hash) const;
263 };
264 
265 } // namespace NodeStore
266 } // namespace ripple
267 
268 #endif
ripple::NodeStore::Shard::getBackend
std::shared_ptr< Backend > getBackend() const
Definition: Shard.cpp:338
ripple::NodeStore::Shard::dir_
const boost::filesystem::path dir_
Definition: Shard.h:193
ripple::Application
Definition: Application.h:97
std::shared_ptr
STL class.
ripple::NodeStore::Shard::lgrSQLiteDB_
std::unique_ptr< DatabaseCon > lgrSQLiteDB_
Definition: Shard.h:205
ripple::NodeStore::Shard::removeOnDestroy_
std::atomic< bool > removeOnDestroy_
Definition: Shard.h:230
ripple::TaggedCache
Map/cache combination.
Definition: TaggedCache.h:52
ripple::NodeStore::Shard::setBackendCache
void setBackendCache(std::lock_guard< std::recursive_mutex > const &lock)
Definition: Shard.cpp:630
ripple::NodeStore::Shard::valLedger
bool valLedger(std::shared_ptr< Ledger const > const &ledger, std::shared_ptr< Ledger const > const &next) const
Definition: Shard.cpp:928
std::pair
ripple::NodeStore::Shard::store
bool store(std::shared_ptr< Ledger const > const &ledger)
Definition: Shard.cpp:251
ripple::NodeStore::Shard::backendComplete_
bool backendComplete_
Definition: Shard.h:217
ripple::NodeStore::Shard::getDir
boost::filesystem::path const & getDir() const
Definition: Shard.h:84
ripple::NodeStore::Shard::final_
bool final_
Definition: Shard.h:224
ripple::NodeStore::Shard::initSQLite
bool initSQLite(std::lock_guard< std::recursive_mutex > const &lock)
Definition: Shard.cpp:664
std::recursive_mutex
STL class.
std::lock_guard
STL class.
tuple
ripple::NodeStore::Shard::stop
void stop()
Definition: Shard.h:138
ripple::NodeStore::Shard::finalKey
static const uint256 finalKey
Definition: Shard.h:158
ripple::NodeStore::Shard::lastSeq_
const std::uint32_t lastSeq_
Definition: Shard.h:180
ripple::NodeStore::Shard::AcquireInfo
Definition: Shard.h:161
ripple::NodeStore::Shard::AcquireInfo::storedSeqs
RangeSet< std::uint32_t > storedSeqs
Definition: Shard.h:167
ripple::NodeStore::Shard::removeOnDestroy
void removeOnDestroy()
If called, the shard directory will be removed when the shard is destroyed.
Definition: Shard.h:147
ripple::NodeStore::Shard::isLegacy
bool isLegacy() const
Returns true if the shard is older, without final key data.
Definition: Shard.cpp:386
ripple::NodeStore::Shard::version
static constexpr std::uint32_t version
Definition: Shard.h:153
ripple::base_uint< 256 >
ripple::NodeStore::Shard::pCache
std::shared_ptr< PCache > pCache() const
Definition: Shard.cpp:354
ripple::NodeStore::Shard::j_
const beast::Journal j_
Definition: Shard.h:214
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:393
ripple::NodeStore::Shard::fileSz_
std::uint64_t fileSz_
Definition: Shard.h:196
ripple::NodeStore::Shard::valFetch
std::shared_ptr< NodeObject > valFetch(uint256 const &hash) const
Definition: Shard.cpp:1008
ripple::NodeStore::Shard::sweep
void sweep()
Definition: Shard.cpp:316
ripple::NodeStore::DatabaseShard
A collection of historical shards.
Definition: DatabaseShard.h:37
ripple::NodeStore::Shard::nCache_
std::shared_ptr< NCache > nCache_
Definition: Shard.h:190
ripple::NodeStore::Shard::backend_
std::shared_ptr< Backend > backend_
Definition: Shard.h:202
ripple::NodeStore::Shard::firstSeq_
const std::uint32_t firstSeq_
Definition: Shard.h:177
ripple::NodeStore::Shard::pCache_
std::shared_ptr< PCache > pCache_
Definition: Shard.h:187
ripple::NodeStore::Shard::isFinal
bool isFinal() const
Returns true if the shard is complete, validated, and immutable.
Definition: Shard.cpp:379
ripple::NodeStore::Shard::AcquireInfo::SQLiteDB
std::unique_ptr< DatabaseCon > SQLiteDB
Definition: Shard.h:164
ripple::NodeStore::Shard::maxLedgers_
const std::uint32_t maxLedgers_
Definition: Shard.h:184
ripple::NodeStore::Shard::~Shard
~Shard()
Definition: Shard.cpp:56
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:212
ripple::NodeStore::Shard::app_
Application & app_
Definition: Shard.h:170
ripple::NodeStore::Shard::isBackendComplete
bool isBackendComplete() const
Returns true if all shard ledgers have been stored in the backend.
Definition: Shard.cpp:347
ripple::NodeStore::Shard::mutex_
std::recursive_mutex mutex_
Definition: Shard.h:171
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:231
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:904
ripple::NodeStore::Shard::legacy_
bool legacy_
Definition: Shard.h:221
ripple::NodeStore::Shard::fdRequired_
std::uint32_t fdRequired_
Definition: Shard.h:199
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:372
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:78
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:208
ripple::NodeStore::Shard::containsLedger
bool containsLedger(std::uint32_t seq) const
Definition: Shard.cpp:302
ripple::NodeStore::Shard::index_
const std::uint32_t index_
Definition: Shard.h:174
std::unique_ptr
STL class.
ripple::NodeStore::Shard::stop_
std::atomic< bool > stop_
Definition: Shard.h:227
ripple::NodeStore::Shard::nCache
std::shared_ptr< NCache > nCache() const
Definition: Shard.cpp:363
ripple::NodeStore::Shard::getBackendAll
std::tuple< std::shared_ptr< Backend >, std::shared_ptr< PCache >, std::shared_ptr< NCache > > getBackendAll() const
Definition: Shard.cpp:329
ripple::NodeStore::Shard::storeSQLite
bool storeSQLite(std::shared_ptr< Ledger const > const &ledger, std::lock_guard< std::recursive_mutex > const &lock)
Definition: Shard.cpp:736