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 seq) = 0;
116 
128  fetch(uint256 const& hash, std::uint32_t seq) = 0;
129 
142  virtual bool
143  asyncFetch(
144  uint256 const& hash,
145  std::uint32_t seq,
146  std::shared_ptr<NodeObject>& object) = 0;
147 
153  virtual bool
154  storeLedger(std::shared_ptr<Ledger const> const& srcLedger) = 0;
155 
158  void
159  waitReads();
160 
167  virtual int
169 
171  virtual float
172  getCacheHitRate() = 0;
173 
179  virtual void
180  tune(int size, std::chrono::seconds age) = 0;
181 
183  virtual void
184  sweep() = 0;
185 
192  {
193  return storeCount_;
194  }
195 
198  {
199  return fetchTotalCount_;
200  }
201 
204  {
205  return fetchHitCount_;
206  }
207 
209  getStoreSize() const
210  {
211  return storeSz_;
212  }
213 
215  getFetchSize() const
216  {
217  return fetchSz_;
218  }
219 
221  int
222  fdRequired() const
223  {
224  return fdRequired_;
225  }
226 
227  void
228  onStop() override;
229 
230  void
231  onChildrenStopped() override;
232 
237  {
238  return earliestLedgerSeq_;
239  }
240 
241 protected:
244  int fdRequired_{0};
245 
246  void
247  stopThreads();
248 
249  void
250  storeStats(size_t sz)
251  {
252  ++storeCount_;
253  storeSz_ += sz;
254  }
255 
256  // Called by the public asyncFetch function
257  void
258  asyncFetch(
259  uint256 const& hash,
260  std::uint32_t seq,
262  std::shared_ptr<KeyCache<uint256>> const& nCache);
263 
264  // Called by the public fetch function
266  fetchInternal(uint256 const& hash, std::shared_ptr<Backend> backend);
267 
268  // Called by the public import function
269  void
270  importInternal(Backend& dstBackend, Database& srcDB);
271 
273  doFetch(
274  uint256 const& hash,
275  std::uint32_t seq,
277  KeyCache<uint256>& nCache,
278  bool isAsync);
279 
280  // Called by the public storeLedger function
281  bool
282  storeLedger(
283  Ledger const& srcLedger,
284  std::shared_ptr<Backend> dstBackend,
288 
289 private:
295 
299 
300  // reads to do
301  std::map<
302  uint256,
303  std::tuple<
308 
309  // last read
311 
313  bool readShut_{false};
314 
315  // current read generation
316  uint64_t readGen_{0};
317 
318  // The default is 32570 to match the XRP ledger network's earliest
319  // allowed sequence. Alternate networks may set this value.
321 
323  fetchFrom(uint256 const& hash, std::uint32_t seq) = 0;
324 
332  virtual void
334 
335  void
336  threadEntry();
337 };
338 
339 } // namespace NodeStore
340 } // namespace ripple
341 
342 #endif
ripple::NodeStore::Database::getFetchSize
std::uint32_t getFetchSize() const
Definition: Database.h:215
ripple::Section
Holds a collection of configuration values.
Definition: BasicConfig.h:43
ripple::NodeStore::Database::readLastHash_
uint256 readLastHash_
Definition: Database.h:310
ripple::NodeStore::Database::getWriteLoad
virtual std::int32_t getWriteLoad() const =0
Retrieve the estimated number of pending write operations.
ripple::NodeStore::Database::read_
std::map< uint256, std::tuple< std::uint32_t, std::weak_ptr< TaggedCache< uint256, NodeObject > >, std::weak_ptr< KeyCache< uint256 > > > > read_
Definition: Database.h:307
ripple::NodeStore::Database
Persistency layer for NodeObject.
Definition: Database.h:53
std::string
STL class.
ripple::NodeStore::Database::readShut_
bool readShut_
Definition: Database.h:313
std::shared_ptr< NodeObject >
ripple::TaggedCache
Map/cache combination.
Definition: TaggedCache.h:54
ripple::NodeStore::Database::doFetch
std::shared_ptr< NodeObject > doFetch(uint256 const &hash, std::uint32_t seq, TaggedCache< uint256, NodeObject > &pCache, KeyCache< uint256 > &nCache, bool isAsync)
Definition: Database.cpp:184
ripple::NodeStore::Database::fetchFrom
virtual std::shared_ptr< NodeObject > fetchFrom(uint256 const &hash, std::uint32_t seq)=0
ripple::NodeStore::Database::fdRequired_
int fdRequired_
Definition: Database.h:244
std::vector< unsigned char >
ripple::NodeObjectType
NodeObjectType
The types of node objects.
Definition: NodeObject.h:32
std::chrono::seconds
ripple::NodeStore::Database::fetchSz_
std::atomic< std::uint32_t > fetchSz_
Definition: Database.h:294
std::tuple
std::function
ripple::NodeStore::Database::getCacheHitRate
virtual float getCacheHitRate()=0
Get the positive cache hits to total attempts ratio.
ripple::NodeStore::Database::fetchHitCount_
std::atomic< std::uint32_t > fetchHitCount_
Definition: Database.h:292
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:493
ripple::NodeStore::Database::readLock_
std::mutex readLock_
Definition: Database.h:296
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::store
virtual void store(NodeObjectType type, Blob &&data, uint256 const &hash, std::uint32_t seq)=0
Store the object.
ripple::Stoppable
Provides an interface for starting and stopping.
Definition: Stoppable.h:200
ripple::NodeStore::Database::importInternal
void importInternal(Backend &dstBackend, Database &srcDB)
Definition: Database.cpp:158
ripple::NodeStore::Database::getStoreSize
std::uint32_t getStoreSize() const
Definition: Database.h:209
thread
ripple::Ledger
Holds a ledger.
Definition: Ledger.h:77
ripple::NodeStore::Database::fetch
virtual std::shared_ptr< NodeObject > fetch(uint256 const &hash, std::uint32_t seq)=0
Fetch an object.
ripple::NodeStore::Database::readCondVar_
std::condition_variable readCondVar_
Definition: Database.h:297
ripple::NodeStore::Database::waitReads
void waitReads()
Wait for all currently pending async reads to complete.
Definition: Database.cpp:61
ripple::NodeStore::Database::getStoreCount
std::uint32_t getStoreCount() const
Gather statistics pertaining to read and write activities.
Definition: Database.h:191
ripple::NodeStore::Database::storeStats
void storeStats(size_t sz)
Definition: Database.h:250
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:236
std::atomic< std::uint32_t >
std::map
STL class.
ripple::NodeStore::Scheduler
Scheduling for asynchronous backend activity.
Definition: ripple/nodestore/Scheduler.h:57
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::fetchInternal
std::shared_ptr< NodeObject > fetchInternal(uint256 const &hash, std::shared_ptr< Backend > backend)
Definition: Database.cpp:123
std::weak_ptr
STL class.
ripple::NodeStore::Database::threadEntry
void threadEntry()
Definition: Database.cpp:340
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
Copies a ledger stored in a different database to this one.
ripple::NodeStore::Database::readGenCondVar_
std::condition_variable readGenCondVar_
Definition: Database.h:298
ripple::NodeStore::Database::storeCount_
std::atomic< std::uint32_t > storeCount_
Definition: Database.h:290
ripple::NodeStore::Database::j_
const beast::Journal j_
Definition: Database.h:242
ripple::NodeStore::Database::readThreads_
std::vector< std::thread > readThreads_
Definition: Database.h:312
ripple::NodeStore::Database::stopThreads
void stopThreads()
Definition: Database.cpp:93
ripple::NodeStore::Database::asyncFetch
virtual bool asyncFetch(uint256 const &hash, std::uint32_t seq, std::shared_ptr< NodeObject > &object)=0
Fetch an object without waiting.
std::condition_variable
ripple::NodeStore::Database::earliestLedgerSeq_
const std::uint32_t earliestLedgerSeq_
Definition: Database.h:320
ripple::NodeStore::Database::fetchTotalCount_
std::atomic< std::uint32_t > fetchTotalCount_
Definition: Database.h:291
ripple::NodeStore::Database::getFetchHitCount
std::uint32_t getFetchHitCount() const
Definition: Database.h:203
std::mutex
STL class.
ripple::NodeStore::Database::storeSz_
std::atomic< std::uint32_t > storeSz_
Definition: Database.h:293
ripple::NodeStore::Database::readGen_
uint64_t readGen_
Definition: Database.h:316
ripple::NodeStore::Database::scheduler_
Scheduler & scheduler_
Definition: Database.h:243
ripple::NodeStore::Database::getFetchTotalCount
std::uint32_t getFetchTotalCount() const
Definition: Database.h:197
ripple::KeyCache< uint256 >
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::Database::getDesiredAsyncReadCount
virtual int getDesiredAsyncReadCount(std::uint32_t seq)=0
Get the maximum number of async reads the node store prefers.
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:222
ripple::NodeStore::Backend
A backend used for the NodeStore.
Definition: Backend.h:37