rippled
Database.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_DATABASE_H_INCLUDED
21 #define RIPPLE_NODESTORE_DATABASE_H_INCLUDED
22 
23 #include <ripple/basics/KeyCache.h>
24 #include <ripple/basics/TaggedCache.h>
25 #include <ripple/nodestore/Backend.h>
26 #include <ripple/nodestore/NodeObject.h>
27 #include <ripple/nodestore/Scheduler.h>
28 #include <ripple/protocol/SystemParameters.h>
29 
30 #include <condition_variable>
31 #include <thread>
32 
33 namespace ripple {
34 
35 class Ledger;
36 
37 namespace NodeStore {
38 
52 class Database
53 {
54 public:
55  Database() = delete;
56 
64  Database(
65  Scheduler& scheduler,
66  int readThreads,
67  Section const& config,
68  beast::Journal j);
69 
74  virtual ~Database();
75 
80  virtual std::string
81  getName() const = 0;
82 
84  virtual void
85  importDatabase(Database& source) = 0;
86 
90  virtual std::int32_t
91  getWriteLoad() const = 0;
92 
105  virtual void
106  store(
107  NodeObjectType type,
108  Blob&& data,
109  uint256 const& hash,
110  std::uint32_t ledgerSeq) = 0;
111 
112  /* Check if two ledgers are in the same database
113 
114  If these two sequence numbers map to the same database,
115  the result of a fetch with either sequence number would
116  be identical.
117 
118  @param s1 The first sequence number
119  @param s2 The second sequence number
120 
121  @return 'true' if both ledgers would be in the same DB
122 
123  */
124  virtual bool
126 
127  virtual void
128  sync() = 0;
129 
143  uint256 const& hash,
144  std::uint32_t ledgerSeq = 0,
145  FetchType fetchType = FetchType::synchronous);
146 
159  void
160  asyncFetch(
161  uint256 const& hash,
162  std::uint32_t ledgerSeq,
163  std::function<void(std::shared_ptr<NodeObject> const&)>&& callback);
164 
170  virtual bool
171  storeLedger(std::shared_ptr<Ledger const> const& srcLedger) = 0;
172 
174  virtual void
175  sweep() = 0;
176 
183  {
184  return storeCount_;
185  }
186 
189  {
190  return fetchTotalCount_;
191  }
192 
195  {
196  return fetchHitCount_;
197  }
198 
200  getStoreSize() const
201  {
202  return storeSz_;
203  }
204 
206  getFetchSize() const
207  {
208  return fetchSz_;
209  }
210 
211  void
213 
215  int
216  fdRequired() const
217  {
218  return fdRequired_;
219  }
220 
221  virtual void
222  stop();
223 
224  bool
225  isStopping() const;
226 
229  [[nodiscard]] std::uint32_t
230  ledgersPerShard() const noexcept
231  {
232  return ledgersPerShard_;
233  }
234 
237  [[nodiscard]] std::uint32_t
238  earliestLedgerSeq() const noexcept
239  {
240  return earliestLedgerSeq_;
241  }
242 
245  [[nodiscard]] std::uint32_t
246  earliestShardIndex() const noexcept
247  {
248  return earliestShardIndex_;
249  }
250 
256  [[nodiscard]] std::uint32_t
257  firstLedgerSeq(std::uint32_t shardIndex) const noexcept
258  {
259  assert(shardIndex >= earliestShardIndex_);
260  if (shardIndex <= earliestShardIndex_)
261  return earliestLedgerSeq_;
262  return 1 + (shardIndex * ledgersPerShard_);
263  }
264 
270  [[nodiscard]] std::uint32_t
271  lastLedgerSeq(std::uint32_t shardIndex) const noexcept
272  {
273  assert(shardIndex >= earliestShardIndex_);
274  return (shardIndex + 1) * ledgersPerShard_;
275  }
276 
282  [[nodiscard]] std::uint32_t
283  seqToShardIndex(std::uint32_t ledgerSeq) const noexcept
284  {
285  assert(ledgerSeq >= earliestLedgerSeq_);
286  return (ledgerSeq - 1) / ledgersPerShard_;
287  }
288 
297  [[nodiscard]] std::uint32_t
298  maxLedgers(std::uint32_t shardIndex) const noexcept;
299 
300 protected:
303  int fdRequired_{0};
304 
307 
308  // The default is DEFAULT_LEDGERS_PER_SHARD (16384) to match the XRP ledger
309  // network. Can be set through the configuration file using the
310  // 'ledgers_per_shard' field under the 'node_db' and 'shard_db' stanzas.
311  // If specified, the value must be a multiple of 256 and equally assigned
312  // in both stanzas. Only unit tests or alternate networks should change
313  // this value.
315 
316  // The default is XRP_LEDGER_EARLIEST_SEQ (32570) to match the XRP ledger
317  // network's earliest allowed ledger sequence. Can be set through the
318  // configuration file using the 'earliest_seq' field under the 'node_db'
319  // and 'shard_db' stanzas. If specified, the value must be greater than zero
320  // and equally assigned in both stanzas. Only unit tests or alternate
321  // networks should change this value.
323 
324  // The earliest shard index
326 
327  void
329  {
330  assert(count <= sz);
331  storeCount_ += count;
332  storeSz_ += sz;
333  }
334 
335  // Called by the public import function
336  void
337  importInternal(Backend& dstBackend, Database& srcDB);
338 
339  // Called by the public storeLedger function
340  bool
341  storeLedger(Ledger const& srcLedger, std::shared_ptr<Backend> dstBackend);
342 
343  void
344  updateFetchMetrics(uint64_t fetches, uint64_t hits, uint64_t duration)
345  {
346  fetchTotalCount_ += fetches;
347  fetchHitCount_ += hits;
348  fetchDurationUs_ += duration;
349  }
350 
351 private:
357 
360 
361  // reads to do
362  std::map<
363  uint256,
368 
369  // last read
371 
373  bool readStopping_{false};
374 
377  uint256 const& hash,
378  std::uint32_t ledgerSeq,
379  FetchReport& fetchReport) = 0;
380 
388  virtual void
390 
397  getCounters() const
398  {
399  return std::nullopt;
400  }
401 
402  void
403  threadEntry();
404 };
405 
406 } // namespace NodeStore
407 } // namespace ripple
408 
409 #endif
ripple::NodeStore::Database::getFetchSize
std::uint32_t getFetchSize() const
Definition: Database.h:206
ripple::Section
Holds a collection of configuration values.
Definition: BasicConfig.h:42
ripple::NodeStore::Database::lastLedgerSeq
std::uint32_t lastLedgerSeq(std::uint32_t shardIndex) const noexcept
Calculates the last ledger sequence for a given shard index.
Definition: Database.h:271
ripple::NodeStore::Database::readLastHash_
uint256 readLastHash_
Definition: Database.h:370
ripple::NodeStore::Database::getWriteLoad
virtual std::int32_t getWriteLoad() const =0
Retrieve the estimated number of pending write operations.
ripple::NodeStore::Database::getStoreCount
std::uint64_t getStoreCount() const
Gather statistics pertaining to read and write activities.
Definition: Database.h:182
ripple::NodeStore::Database::fetchDurationUs_
std::atomic< std::uint64_t > fetchDurationUs_
Definition: Database.h:355
ripple::NodeStore::Database
Persistency layer for NodeObject.
Definition: Database.h:52
std::string
STL class.
std::shared_ptr< NodeObject >
ripple::NodeStore::Database::ledgersPerShard_
const std::uint32_t ledgersPerShard_
Definition: Database.h:314
std::pair
ripple::NodeStore::Database::fdRequired_
int fdRequired_
Definition: Database.h:303
std::vector< unsigned char >
ripple::NodeObjectType
NodeObjectType
The types of node objects.
Definition: NodeObject.h:32
ripple::NodeStore::Database::read_
std::map< uint256, std::vector< std::pair< std::uint32_t, std::function< void(std::shared_ptr< NodeObject > const &)> > > > read_
Definition: Database.h:367
ripple::NodeStore::Database::fetchTotalCount_
std::atomic< std::uint64_t > fetchTotalCount_
Definition: Database.h:354
ripple::NodeStore::Database::asyncFetch
void asyncFetch(uint256 const &hash, std::uint32_t ledgerSeq, std::function< void(std::shared_ptr< NodeObject > const &)> &&callback)
Fetch an object without waiting.
Definition: Database.cpp:107
ripple::NodeStore::Database::sync
virtual void sync()=0
ripple::NodeStore::Database::fetchSz_
std::atomic< std::uint32_t > fetchSz_
Definition: Database.h:306
ripple::NodeStore::Database::stop
virtual void stop()
Definition: Database.cpp:89
ripple::NodeStore::FetchReport
Contains information about a fetch operation.
Definition: ripple/nodestore/Scheduler.h:32
ripple::NodeStore::Database::readStopping_
bool readStopping_
Definition: Database.h:373
std::function
ripple::NodeStore::Database::store
virtual void store(NodeObjectType type, Blob &&data, uint256 const &hash, std::uint32_t ledgerSeq)=0
Store the object.
ripple::NodeStore::Database::fetchHitCount_
std::atomic< std::uint32_t > fetchHitCount_
Definition: Database.h:305
ripple::NodeStore::Database::storeCount_
std::atomic< std::uint64_t > storeCount_
Definition: Database.h:352
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:526
ripple::NodeStore::Database::updateFetchMetrics
void updateFetchMetrics(uint64_t fetches, uint64_t hits, uint64_t duration)
Definition: Database.h:344
ripple::NodeStore::Database::readLock_
std::mutex readLock_
Definition: Database.h:358
ripple::base_uint
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:74
ripple::NodeStore::Database::getName
virtual std::string getName() const =0
Retrieve the name associated with this backend.
ripple::NodeStore::Database::getStoreSize
std::uint64_t getStoreSize() const
Definition: Database.h:200
ripple::NodeStore::Database::storeSz_
std::atomic< std::uint64_t > storeSz_
Definition: Database.h:353
ripple::NodeStore::Database::firstLedgerSeq
std::uint32_t firstLedgerSeq(std::uint32_t shardIndex) const noexcept
Calculates the first ledger sequence for a given shard index.
Definition: Database.h:257
ripple::NodeStore::Database::importInternal
void importInternal(Backend &dstBackend, Database &srcDB)
Definition: Database.cpp:119
thread
ripple::Ledger
Holds a ledger.
Definition: Ledger.h:76
ripple::NodeStore::Database::readCondVar_
std::condition_variable readCondVar_
Definition: Database.h:359
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::int32_t
std::atomic< std::uint32_t >
std::map
STL class.
ripple::NodeStore::Scheduler
Scheduling for asynchronous backend activity.
Definition: ripple/nodestore/Scheduler.h:60
ripple::NodeStore::Database::fetchNodeObject
std::shared_ptr< NodeObject > fetchNodeObject(uint256 const &hash, std::uint32_t ledgerSeq=0, FetchType fetchType=FetchType::synchronous)
Fetch a node object.
Definition: Database.cpp:158
ripple::NodeStore::Database::~Database
virtual ~Database()
Destroy the node store.
Definition: Database.cpp:57
ripple::NodeStore::Database::for_each
virtual void for_each(std::function< void(std::shared_ptr< NodeObject >)> f)=0
Visit every object in the database This is usually called during import.
ripple::NodeStore::Database::sweep
virtual void sweep()=0
Remove expired entries from the positive and negative caches.
ripple::NodeStore::Database::storeStats
void storeStats(std::uint64_t count, std::uint64_t sz)
Definition: Database.h:328
ripple::NodeStore::FetchType
FetchType
Definition: ripple/nodestore/Scheduler.h:29
ripple::NodeStore::Database::isStopping
bool isStopping() const
Definition: Database.cpp:69
ripple::NodeStore::Database::threadEntry
void threadEntry()
Definition: Database.cpp:283
ripple::NodeStore::Database::getCounters
virtual std::optional< Backend::Counters< std::uint64_t > > getCounters() const
Retrieve backend read and write stats.
Definition: Database.h:397
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::NodeStore::Database::storeLedger
virtual bool storeLedger(std::shared_ptr< Ledger const > const &srcLedger)=0
Store a ledger from a different database.
ripple::NodeStore::Database::j_
const beast::Journal j_
Definition: Database.h:301
ripple::NodeStore::Database::readThreads_
std::vector< std::thread > readThreads_
Definition: Database.h:372
ripple::NodeStore::Database::seqToShardIndex
std::uint32_t seqToShardIndex(std::uint32_t ledgerSeq) const noexcept
Calculates the shard index for a given ledger sequence.
Definition: Database.h:283
ripple::NodeStore::Database::storeDurationUs_
std::atomic< std::uint64_t > storeDurationUs_
Definition: Database.h:356
ripple::NodeStore::Database::earliestShardIndex
std::uint32_t earliestShardIndex() const noexcept
Definition: Database.h:246
ripple::NodeStore::Database::getCountsJson
void getCountsJson(Json::Value &obj)
Definition: Database.cpp:329
condition_variable
ripple::NodeStore::Database::earliestLedgerSeq_
const std::uint32_t earliestLedgerSeq_
Definition: Database.h:322
ripple::NodeStore::Database::getFetchHitCount
std::uint32_t getFetchHitCount() const
Definition: Database.h:194
std::optional
std::mutex
STL class.
ripple::NodeStore::Database::earliestShardIndex_
const std::uint32_t earliestShardIndex_
Definition: Database.h:325
ripple::NodeStore::Database::scheduler_
Scheduler & scheduler_
Definition: Database.h:302
ripple::NodeStore::Database::getFetchTotalCount
std::uint32_t getFetchTotalCount() const
Definition: Database.h:188
ripple::NodeStore::Database::earliestLedgerSeq
std::uint32_t earliestLedgerSeq() const noexcept
Definition: Database.h:238
ripple::NodeStore::Database::isSameDB
virtual bool isSameDB(std::uint32_t s1, std::uint32_t s2)=0
ripple::NodeStore::Database::maxLedgers
std::uint32_t maxLedgers(std::uint32_t shardIndex) const noexcept
Calculates the maximum ledgers for a given shard index.
Definition: Database.cpp:76
ripple::NodeStore::FetchType::synchronous
@ synchronous
ripple::NodeStore::Database::importDatabase
virtual void importDatabase(Database &source)=0
Import objects from another database.
ripple::NodeStore::Database::ledgersPerShard
std::uint32_t ledgersPerShard() const noexcept
Definition: Database.h:230
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::NodeStore::Database::Database
Database()=delete
ripple::NodeStore::Database::fdRequired
int fdRequired() const
Returns the number of file descriptors the database expects to need.
Definition: Database.h:216
ripple::NodeStore::Backend
A backend used for the NodeStore.
Definition: Backend.h:39