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/core/Stoppable.h>
24 #include <ripple/nodestore/Backend.h>
25 #include <ripple/nodestore/NodeObject.h>
26 #include <ripple/nodestore/Scheduler.h>
27 #include <ripple/protocol/SystemParameters.h>
28 
29 #include <thread>
30 
31 namespace ripple {
32 
33 class Ledger;
34 
35 namespace NodeStore {
36 
50 class Database : public Stoppable
51 {
52 public:
53  Database() = delete;
54 
64  Database(
65  std::string name,
66  Stoppable& parent,
67  Scheduler& scheduler,
68  int readThreads,
69  Section const& config,
70  beast::Journal j);
71 
76  virtual ~Database();
77 
82  virtual std::string
83  getName() const = 0;
84 
86  virtual void
87  import(Database& source) = 0;
88 
92  virtual std::int32_t
93  getWriteLoad() const = 0;
94 
107  virtual void
108  store(
109  NodeObjectType type,
110  Blob&& data,
111  uint256 const& hash,
112  std::uint32_t ledgerSeq) = 0;
113 
114  /* Check if two ledgers are in the same database
115 
116  If these two sequence numbers map to the same database,
117  the result of a fetch with either sequence number would
118  be identical.
119 
120  @param s1 The first sequence number
121  @param s2 The second sequence number
122 
123  @return 'true' if both ledgers would be in the same DB
124 
125  */
126  virtual bool
128 
129  virtual void
130  sync() = 0;
131 
145  uint256 const& hash,
146  std::uint32_t ledgerSeq = 0,
147  FetchType fetchType = FetchType::synchronous);
148 
161  void
162  asyncFetch(
163  uint256 const& hash,
164  std::uint32_t ledgerSeq,
165  std::function<void(std::shared_ptr<NodeObject> const&)>&& callback);
166 
172  virtual bool
173  storeLedger(std::shared_ptr<Ledger const> const& srcLedger) = 0;
174 
176  virtual void
177  sweep() = 0;
178 
185  {
186  return storeCount_;
187  }
188 
191  {
192  return fetchTotalCount_;
193  }
194 
197  {
198  return fetchHitCount_;
199  }
200 
202  getStoreSize() const
203  {
204  return storeSz_;
205  }
206 
208  getFetchSize() const
209  {
210  return fetchSz_;
211  }
212 
213  void
215 
217  int
218  fdRequired() const
219  {
220  return fdRequired_;
221  }
222 
223  void
224  onStop() override;
225 
226  void
227  onChildrenStopped() override;
228 
233  {
234  return earliestLedgerSeq_;
235  }
236 
237 protected:
240  int fdRequired_{0};
241 
244 
245  void
246  stopReadThreads();
247 
248  void
250  {
251  assert(count <= sz);
252  storeCount_ += count;
253  storeSz_ += sz;
254  }
255 
256  // Called by the public import function
257  void
258  importInternal(Backend& dstBackend, Database& srcDB);
259 
260  // Called by the public storeLedger function
261  bool
262  storeLedger(Ledger const& srcLedger, std::shared_ptr<Backend> dstBackend);
263 
264  void
265  updateFetchMetrics(uint64_t fetches, uint64_t hits, uint64_t duration)
266  {
267  fetchTotalCount_ += fetches;
268  fetchHitCount_ += hits;
269  fetchDurationUs_ += duration;
270  }
271 
272 private:
278 
281 
282  // reads to do
283  std::map<
284  uint256,
289 
290  // last read
292 
294  bool readShut_{false};
295 
296  // The default is 32570 to match the XRP ledger network's earliest
297  // allowed sequence. Alternate networks may set this value.
299 
302  uint256 const& hash,
303  std::uint32_t ledgerSeq,
304  FetchReport& fetchReport) = 0;
305 
313  virtual void
315 
322  getCounters() const
323  {
324  return std::nullopt;
325  }
326 
327  void
328  threadEntry();
329 };
330 
331 } // namespace NodeStore
332 } // namespace ripple
333 
334 #endif
ripple::NodeStore::Database::getFetchSize
std::uint32_t getFetchSize() const
Definition: Database.h:208
ripple::Section
Holds a collection of configuration values.
Definition: BasicConfig.h:43
ripple::NodeStore::Database::readLastHash_
uint256 readLastHash_
Definition: Database.h:291
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:184
ripple::NodeStore::Database::fetchDurationUs_
std::atomic< std::uint64_t > fetchDurationUs_
Definition: Database.h:276
ripple::NodeStore::Database
Persistency layer for NodeObject.
Definition: Database.h:50
std::string
STL class.
ripple::NodeStore::Database::readShut_
bool readShut_
Definition: Database.h:294
std::shared_ptr< NodeObject >
std::pair
ripple::NodeStore::Database::fdRequired_
int fdRequired_
Definition: Database.h:240
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:288
ripple::NodeStore::Database::fetchTotalCount_
std::atomic< std::uint64_t > fetchTotalCount_
Definition: Database.h:275
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:94
ripple::NodeStore::Database::sync
virtual void sync()=0
ripple::NodeStore::Database::fetchSz_
std::atomic< std::uint32_t > fetchSz_
Definition: Database.h:243
ripple::NodeStore::FetchReport
Contains information about a fetch operation.
Definition: ripple/nodestore/Scheduler.h:32
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:242
ripple::NodeStore::Database::storeCount_
std::atomic< std::uint64_t > storeCount_
Definition: Database.h:273
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:457
ripple::NodeStore::Database::updateFetchMetrics
void updateFetchMetrics(uint64_t fetches, uint64_t hits, uint64_t duration)
Definition: Database.h:265
ripple::NodeStore::Database::readLock_
std::mutex readLock_
Definition: Database.h:279
ripple::NodeStore::Database::onChildrenStopped
void onChildrenStopped() override
Override called when all children have stopped.
Definition: Database.cpp:72
ripple::base_uint< 256 >
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:202
ripple::NodeStore::Database::storeSz_
std::atomic< std::uint64_t > storeSz_
Definition: Database.h:274
ripple::Stoppable
Provides an interface for starting and stopping.
Definition: Stoppable.h:201
ripple::NodeStore::Database::importInternal
void importInternal(Backend &dstBackend, Database &srcDB)
Definition: Database.cpp:106
thread
ripple::Ledger
Holds a ledger.
Definition: Ledger.h:77
ripple::NodeStore::Database::readCondVar_
std::condition_variable readCondVar_
Definition: Database.h:280
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::int32_t
ripple::NodeStore::Database::earliestLedgerSeq
std::uint32_t earliestLedgerSeq() const
Definition: Database.h:232
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:145
ripple::NodeStore::Database::~Database
virtual ~Database()
Destroy the node store.
Definition: Database.cpp:52
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:249
ripple::NodeStore::FetchType
FetchType
Definition: ripple/nodestore/Scheduler.h:29
ripple::NodeStore::Database::threadEntry
void threadEntry()
Definition: Database.cpp:270
ripple::NodeStore::Database::stopReadThreads
void stopReadThreads()
Definition: Database.cpp:78
ripple::NodeStore::Database::getCounters
virtual std::optional< Backend::Counters< std::uint64_t > > getCounters() const
Retrieve backend read and write stats.
Definition: Database.h:322
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::NodeStore::Database::onStop
void onStop() override
Override called when the stop notification is issued.
Definition: Database.cpp:64
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:238
ripple::NodeStore::Database::readThreads_
std::vector< std::thread > readThreads_
Definition: Database.h:293
ripple::NodeStore::Database::storeDurationUs_
std::atomic< std::uint64_t > storeDurationUs_
Definition: Database.h:277
ripple::NodeStore::Database::getCountsJson
void getCountsJson(Json::Value &obj)
Definition: Database.cpp:319
std::condition_variable
ripple::NodeStore::Database::earliestLedgerSeq_
const std::uint32_t earliestLedgerSeq_
Definition: Database.h:298
ripple::NodeStore::Database::getFetchHitCount
std::uint32_t getFetchHitCount() const
Definition: Database.h:196
std::optional
std::mutex
STL class.
ripple::NodeStore::Database::scheduler_
Scheduler & scheduler_
Definition: Database.h:239
ripple::NodeStore::Database::getFetchTotalCount
std::uint32_t getFetchTotalCount() const
Definition: Database.h:190
ripple::NodeStore::Database::isSameDB
virtual bool isSameDB(std::uint32_t s1, std::uint32_t s2)=0
ripple::NodeStore::FetchType::synchronous
@ synchronous
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:218
ripple::NodeStore::Backend
A backend used for the NodeStore.
Definition: Backend.h:39