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/protocol/SystemParameters.h>
30 
31 #include <thread>
32 
33 namespace ripple {
34 
35 class Ledger;
36 
37 namespace NodeStore {
38 
52 class Database : public Stoppable
53 {
54 public:
55  Database() = delete;
56 
66  Database(
67  std::string name,
68  Stoppable& parent,
69  Scheduler& scheduler,
70  int readThreads,
71  Section const& config,
72  beast::Journal j);
73 
78  virtual ~Database();
79 
84  virtual std::string
85  getName() const = 0;
86 
88  virtual void
89  import(Database& source) = 0;
90 
94  virtual std::int32_t
95  getWriteLoad() const = 0;
96 
109  virtual void
110  store(
111  NodeObjectType type,
112  Blob&& data,
113  uint256 const& hash,
114  std::uint32_t ledgerSeq) = 0;
115 
116  /* Check if two ledgers are in the same database
117 
118  If these two sequence numbers map to the same database,
119  the result of a fetch with either sequence number would
120  be identical.
121 
122  @param s1 The first sequence number
123  @param s2 The second sequence number
124 
125  @return 'true' if both ledgers would be in the same DB
126 
127  */
128  virtual bool
130 
144  uint256 const& hash,
145  std::uint32_t ledgerSeq = 0,
146  FetchType fetchType = FetchType::synchronous);
147 
160  void
161  asyncFetch(
162  uint256 const& hash,
163  std::uint32_t ledgerSeq,
164  std::function<void(std::shared_ptr<NodeObject> const&)>&& callback);
165 
171  virtual bool
172  storeLedger(std::shared_ptr<Ledger const> const& srcLedger) = 0;
173 
175  virtual void
176  sweep() = 0;
177 
184  {
185  return storeCount_;
186  }
187 
190  {
191  return fetchTotalCount_;
192  }
193 
196  {
197  return fetchHitCount_;
198  }
199 
201  getStoreSize() const
202  {
203  return storeSz_;
204  }
205 
207  getFetchSize() const
208  {
209  return fetchSz_;
210  }
211 
213  int
214  fdRequired() const
215  {
216  return fdRequired_;
217  }
218 
219  void
220  onStop() override;
221 
222  void
223  onChildrenStopped() override;
224 
229  {
230  return earliestLedgerSeq_;
231  }
232 
233 protected:
236  int fdRequired_{0};
237 
240 
241  void
242  stopReadThreads();
243 
244  void
246  {
247  assert(count <= sz);
248  storeCount_ += count;
249  storeSz_ += sz;
250  }
251 
252  // Called by the public asyncFetch function
253  void
254  asyncFetch(uint256 const& hash, std::uint32_t ledgerSeq);
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 private:
268 
271 
272  // reads to do
273  std::map<
274  uint256,
279 
280  // last read
282 
284  bool readShut_{false};
285 
286  // The default is 32570 to match the XRP ledger network's earliest
287  // allowed sequence. Alternate networks may set this value.
289 
292  uint256 const& hash,
293  std::uint32_t ledgerSeq,
294  FetchReport& fetchReport) = 0;
295 
303  virtual void
305 
306  void
307  threadEntry();
308 };
309 
310 } // namespace NodeStore
311 } // namespace ripple
312 
313 #endif
ripple::NodeStore::Database::getFetchSize
std::uint32_t getFetchSize() const
Definition: Database.h:207
ripple::Section
Holds a collection of configuration values.
Definition: BasicConfig.h:43
ripple::NodeStore::Database::readLastHash_
uint256 readLastHash_
Definition: Database.h:281
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:183
ripple::NodeStore::Database
Persistency layer for NodeObject.
Definition: Database.h:52
std::string
STL class.
ripple::NodeStore::Database::readShut_
bool readShut_
Definition: Database.h:284
std::shared_ptr< NodeObject >
std::pair
ripple::NodeStore::Database::fdRequired_
int fdRequired_
Definition: Database.h:236
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:278
ripple::NodeStore::Database::fetchTotalCount_
std::atomic< std::uint64_t > fetchTotalCount_
Definition: Database.h:267
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:91
ripple::NodeStore::Database::fetchSz_
std::atomic< std::uint32_t > fetchSz_
Definition: Database.h:239
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:238
ripple::NodeStore::Database::storeCount_
std::atomic< std::uint64_t > storeCount_
Definition: Database.h:265
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:457
ripple::NodeStore::Database::readLock_
std::mutex readLock_
Definition: Database.h:269
ripple::NodeStore::Database::onChildrenStopped
void onChildrenStopped() override
Override called when all children have stopped.
Definition: Database.cpp:69
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:201
ripple::NodeStore::Database::storeSz_
std::atomic< std::uint64_t > storeSz_
Definition: Database.h:266
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:103
thread
ripple::Ledger
Holds a ledger.
Definition: Ledger.h:77
ripple::NodeStore::Database::readCondVar_
std::condition_variable readCondVar_
Definition: Database.h:270
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:228
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:142
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:245
ripple::NodeStore::FetchType
FetchType
Definition: ripple/nodestore/Scheduler.h:29
ripple::NodeStore::Database::threadEntry
void threadEntry()
Definition: Database.cpp:267
ripple::NodeStore::Database::stopReadThreads
void stopReadThreads()
Definition: Database.cpp:75
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:61
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:234
ripple::NodeStore::Database::readThreads_
std::vector< std::thread > readThreads_
Definition: Database.h:283
std::condition_variable
ripple::NodeStore::Database::earliestLedgerSeq_
const std::uint32_t earliestLedgerSeq_
Definition: Database.h:288
ripple::NodeStore::Database::getFetchHitCount
std::uint32_t getFetchHitCount() const
Definition: Database.h:195
std::mutex
STL class.
ripple::NodeStore::Database::scheduler_
Scheduler & scheduler_
Definition: Database.h:235
ripple::NodeStore::Database::getFetchTotalCount
std::uint32_t getFetchTotalCount() const
Definition: Database.h:189
ripple::NodeStore::Database::isSameDB
virtual bool isSameDB(std::uint32_t s1, std::uint32_t s2)=0
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:214
ripple::NodeStore::Backend
A backend used for the NodeStore.
Definition: Backend.h:37