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/core/Stoppable.h>
26 #include <ripple/nodestore/Backend.h>
27 #include <ripple/nodestore/NodeObject.h>
28 #include <ripple/nodestore/Scheduler.h>
29 #include <ripple/nodestore/impl/Tuning.h>
30 #include <ripple/protocol/SystemParameters.h>
31 
32 #include <thread>
33 
34 namespace ripple {
35 
36 class Ledger;
37 
38 namespace NodeStore {
39 
53 class Database : public Stoppable
54 {
55 public:
56  Database() = delete;
57 
67  Database(
68  std::string name,
69  Stoppable& parent,
70  Scheduler& scheduler,
71  int readThreads,
72  Section const& config,
73  beast::Journal j);
74 
79  virtual ~Database();
80 
85  virtual std::string
86  getName() const = 0;
87 
89  virtual void
90  import(Database& source) = 0;
91 
95  virtual std::int32_t
96  getWriteLoad() const = 0;
97 
110  virtual void
111  store(
112  NodeObjectType type,
113  Blob&& data,
114  uint256 const& hash,
115  std::uint32_t ledgerSeq) = 0;
116 
130  uint256 const& hash,
131  std::uint32_t ledgerSeq = 0,
132  FetchType fetchType = FetchType::synchronous);
133 
147  virtual bool
148  asyncFetch(
149  uint256 const& hash,
150  std::uint32_t ledgerSeq,
151  std::shared_ptr<NodeObject>& nodeObject) = 0;
152 
158  virtual bool
159  storeLedger(std::shared_ptr<Ledger const> const& srcLedger) = 0;
160 
163  void
164  waitReads();
165 
172  virtual int
174 
176  virtual float
177  getCacheHitRate() = 0;
178 
184  virtual void
185  tune(int size, std::chrono::seconds age) = 0;
186 
188  virtual void
189  sweep() = 0;
190 
197  {
198  return storeCount_;
199  }
200 
203  {
204  return fetchTotalCount_;
205  }
206 
209  {
210  return fetchHitCount_;
211  }
212 
214  getStoreSize() const
215  {
216  return storeSz_;
217  }
218 
220  getFetchSize() const
221  {
222  return fetchSz_;
223  }
224 
226  int
227  fdRequired() const
228  {
229  return fdRequired_;
230  }
231 
232  void
233  onStop() override;
234 
235  void
236  onChildrenStopped() override;
237 
242  {
243  return earliestLedgerSeq_;
244  }
245 
246 protected:
249  int fdRequired_{0};
250 
253 
254  void
255  stopReadThreads();
256 
257  void
259  {
260  assert(count <= sz);
261  storeCount_ += count;
262  storeSz_ += sz;
263  }
264 
265  // Called by the public asyncFetch function
266  void
267  asyncFetch(uint256 const& hash, std::uint32_t ledgerSeq);
268 
269  // Called by the public import function
270  void
271  importInternal(Backend& dstBackend, Database& srcDB);
272 
273  // Called by the public storeLedger function
274  bool
275  storeLedger(
276  Ledger const& srcLedger,
277  std::shared_ptr<Backend> dstBackend,
280 
281 private:
285 
289 
290  // reads to do
292 
293  // last read
295 
297  bool readShut_{false};
298 
299  // current read generation
300  uint64_t readGen_{0};
301 
302  // The default is 32570 to match the XRP ledger network's earliest
303  // allowed sequence. Alternate networks may set this value.
305 
308  uint256 const& hash,
309  std::uint32_t ledgerSeq,
310  FetchReport& fetchReport) = 0;
311 
319  virtual void
321 
322  void
323  threadEntry();
324 };
325 
326 } // namespace NodeStore
327 } // namespace ripple
328 
329 #endif
ripple::NodeStore::Database::getFetchSize
std::uint32_t getFetchSize() const
Definition: Database.h:220
ripple::Section
Holds a collection of configuration values.
Definition: BasicConfig.h:43
ripple::NodeStore::Database::readLastHash_
uint256 readLastHash_
Definition: Database.h:294
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:196
ripple::NodeStore::Database
Persistency layer for NodeObject.
Definition: Database.h:53
std::string
STL class.
ripple::NodeStore::Database::readShut_
bool readShut_
Definition: Database.h:297
std::shared_ptr< NodeObject >
ripple::TaggedCache
Map/cache combination.
Definition: TaggedCache.h:52
ripple::NodeStore::Database::fdRequired_
int fdRequired_
Definition: Database.h:249
std::vector< unsigned char >
ripple::NodeObjectType
NodeObjectType
The types of node objects.
Definition: NodeObject.h:32
std::chrono::seconds
ripple::NodeStore::Database::fetchTotalCount_
std::atomic< std::uint64_t > fetchTotalCount_
Definition: Database.h:284
ripple::NodeStore::Database::fetchSz_
std::atomic< std::uint32_t > fetchSz_
Definition: Database.h:252
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::getCacheHitRate
virtual float getCacheHitRate()=0
Get the positive cache hits to total attempts ratio.
ripple::NodeStore::Database::read_
std::map< uint256, std::uint32_t > read_
Definition: Database.h:291
ripple::NodeStore::Database::fetchHitCount_
std::atomic< std::uint32_t > fetchHitCount_
Definition: Database.h:251
ripple::NodeStore::Database::storeCount_
std::atomic< std::uint64_t > storeCount_
Definition: Database.h:282
ripple::NodeStore::Database::readLock_
std::mutex readLock_
Definition: Database.h:286
ripple::NodeStore::Database::onChildrenStopped
void onChildrenStopped() override
Override called when all children have stopped.
Definition: Database.cpp:87
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:214
ripple::NodeStore::Database::storeSz_
std::atomic< std::uint64_t > storeSz_
Definition: Database.h:283
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:119
thread
ripple::Ledger
Holds a ledger.
Definition: Ledger.h:77
ripple::NodeStore::Database::readCondVar_
std::condition_variable readCondVar_
Definition: Database.h:287
ripple::NodeStore::Database::waitReads
void waitReads()
Wait for all currently pending async reads to complete.
Definition: Database.cpp:61
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:241
std::atomic< std::uint32_t >
std::map
STL class.
ripple::NodeStore::Scheduler
Scheduling for asynchronous backend activity.
Definition: ripple/nodestore/Scheduler.h:61
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:49
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:258
ripple::NodeStore::FetchType
FetchType
Definition: ripple/nodestore/Scheduler.h:29
ripple::NodeStore::Database::threadEntry
void threadEntry()
Definition: Database.cpp:293
ripple::NodeStore::Database::stopReadThreads
void stopReadThreads()
Definition: Database.cpp:93
ripple::NodeStore::Database::getDesiredAsyncReadCount
virtual int getDesiredAsyncReadCount(std::uint32_t ledgerSeq)=0
Get the maximum number of async reads the node store prefers.
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:79
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::readGenCondVar_
std::condition_variable readGenCondVar_
Definition: Database.h:288
ripple::NodeStore::Database::asyncFetch
virtual bool asyncFetch(uint256 const &hash, std::uint32_t ledgerSeq, std::shared_ptr< NodeObject > &nodeObject)=0
Fetch an object without waiting.
ripple::NodeStore::Database::j_
const beast::Journal j_
Definition: Database.h:247
ripple::NodeStore::Database::readThreads_
std::vector< std::thread > readThreads_
Definition: Database.h:296
std::condition_variable
ripple::NodeStore::Database::earliestLedgerSeq_
const std::uint32_t earliestLedgerSeq_
Definition: Database.h:304
ripple::NodeStore::Database::getFetchHitCount
std::uint32_t getFetchHitCount() const
Definition: Database.h:208
std::mutex
STL class.
ripple::NodeStore::Database::readGen_
uint64_t readGen_
Definition: Database.h:300
ripple::NodeStore::Database::scheduler_
Scheduler & scheduler_
Definition: Database.h:248
ripple::NodeStore::Database::getFetchTotalCount
std::uint32_t getFetchTotalCount() const
Definition: Database.h:202
ripple::KeyCache
Maintains a cache of keys with no associated data.
Definition: KeyCache.h:43
ripple::NodeStore::Database::tune
virtual void tune(int size, std::chrono::seconds age)=0
Set the maximum number of entries and maximum cache age for both caches.
ripple::NodeStore::FetchType::synchronous
@ synchronous
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:227
ripple::NodeStore::Backend
A backend used for the NodeStore.
Definition: Backend.h:37