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/TaggedCache.h>
24 #include <ripple/basics/KeyCache.h>
25 #include <ripple/core/Stoppable.h>
26 #include <ripple/nodestore/Backend.h>
27 #include <ripple/nodestore/impl/Tuning.h>
28 #include <ripple/nodestore/Scheduler.h>
29 #include <ripple/nodestore/NodeObject.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(std::string name, Stoppable& parent, Scheduler& scheduler,
68  int readThreads, Section const& config, beast::Journal j);
69 
74  virtual
75  ~Database();
76 
81  virtual
83  getName() const = 0;
84 
86  virtual
87  void
88  import(Database& source) = 0;
89 
93  virtual
95  getWriteLoad() const = 0;
96 
109  virtual
110  void
111  store(NodeObjectType type, Blob&& data,
112  uint256 const& hash, std::uint32_t seq) = 0;
113 
124  virtual
126  fetch(uint256 const& hash, std::uint32_t seq) = 0;
127 
140  virtual
141  bool
142  asyncFetch(uint256 const& hash, std::uint32_t seq,
143  std::shared_ptr<NodeObject>& object) = 0;
144 
150  virtual
151  bool
152  storeLedger(std::shared_ptr<Ledger const> const& srcLedger) = 0;
153 
156  void
157  waitReads();
158 
165  virtual
166  int
168 
170  virtual
171  float
172  getCacheHitRate() = 0;
173 
179  virtual
180  void
181  tune(int size, std::chrono::seconds age) = 0;
182 
184  virtual
185  void
186  sweep() = 0;
187 
193  getStoreCount() const { return storeCount_; }
194 
197 
199  getFetchHitCount() const { return fetchHitCount_; }
200 
202  getStoreSize() const { return storeSz_; }
203 
205  getFetchSize() const { return fetchSz_; }
206 
208  int
209  fdRequired() const { return fdRequired_; }
210 
211  void
212  onStop() override;
213 
214  void
215  onChildrenStopped() override;
216 
221  {
222  return earliestLedgerSeq_;
223  }
224 
225 protected:
228  int fdRequired_ {0};
229 
230  void
231  stopThreads();
232 
233  void
234  storeStats(size_t sz)
235  {
236  ++storeCount_;
237  storeSz_ += sz;
238  }
239 
240  // Called by the public asyncFetch function
241  void
242  asyncFetch(uint256 const& hash, std::uint32_t seq,
244  std::shared_ptr<KeyCache<uint256>> const& nCache);
245 
246  // Called by the public fetch function
248  fetchInternal(uint256 const& hash, std::shared_ptr<Backend> backend);
249 
250  // Called by the public import function
251  void
252  importInternal(Backend& dstBackend, Database& srcDB);
253 
255  doFetch(uint256 const& hash, std::uint32_t seq,
257  KeyCache<uint256>& nCache, bool isAsync);
258 
259  // Called by the public storeLedger function
260  bool
261  storeLedger(
262  Ledger const& srcLedger,
263  std::shared_ptr<Backend> dstBackend,
267 
268 private:
274 
278 
279  // reads to do
283 
284  // last read
286 
288  bool readShut_ {false};
289 
290  // current read generation
291  uint64_t readGen_ {0};
292 
293  // The default is 32570 to match the XRP ledger network's earliest
294  // allowed sequence. Alternate networks may set this value.
296 
297  virtual
299  fetchFrom(uint256 const& hash, std::uint32_t seq) = 0;
300 
308  virtual
309  void
311 
312  void
313  threadEntry();
314 };
315 
316 }
317 }
318 
319 #endif
ripple::NodeStore::Database::getFetchSize
std::uint32_t getFetchSize() const
Definition: Database.h:205
ripple::Section
Holds a collection of configuration values.
Definition: BasicConfig.h:43
ripple::NodeStore::Database::readLastHash_
uint256 readLastHash_
Definition: Database.h:285
ripple::NodeStore::Database::getWriteLoad
virtual std::int32_t getWriteLoad() const =0
Retrieve the estimated number of pending write operations.
ripple::NodeStore::Database
Persistency layer for NodeObject.
Definition: Database.h:53
std::string
STL class.
ripple::NodeStore::Database::readShut_
bool readShut_
Definition: Database.h:288
std::shared_ptr< NodeObject >
ripple::TaggedCache
Map/cache combination.
Definition: TaggedCache.h:55
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:189
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:228
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:273
std::tuple
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:282
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:271
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:436
ripple::NodeStore::Database::readLock_
std::mutex readLock_
Definition: Database.h:275
ripple::NodeStore::Database::onChildrenStopped
void onChildrenStopped() override
Override called when all children have stopped.
Definition: Database.cpp:89
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:161
ripple::NodeStore::Database::getStoreSize
std::uint32_t getStoreSize() const
Definition: Database.h:202
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:276
ripple::NodeStore::Database::waitReads
void waitReads()
Wait for all currently pending async reads to complete.
Definition: Database.cpp:63
ripple::NodeStore::Database::getStoreCount
std::uint32_t getStoreCount() const
Gather statistics pertaining to read and write activities.
Definition: Database.h:193
ripple::NodeStore::Database::storeStats
void storeStats(size_t sz)
Definition: Database.h:234
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:60
std::int32_t
ripple::NodeStore::Database::earliestLedgerSeq
std::uint32_t earliestLedgerSeq() const
Definition: Database.h:220
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:51
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:351
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:81
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:277
ripple::NodeStore::Database::storeCount_
std::atomic< std::uint32_t > storeCount_
Definition: Database.h:269
ripple::NodeStore::Database::j_
const beast::Journal j_
Definition: Database.h:226
ripple::NodeStore::Database::readThreads_
std::vector< std::thread > readThreads_
Definition: Database.h:287
ripple::NodeStore::Database::stopThreads
void stopThreads()
Definition: Database.cpp:95
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:295
ripple::NodeStore::Database::fetchTotalCount_
std::atomic< std::uint32_t > fetchTotalCount_
Definition: Database.h:270
ripple::NodeStore::Database::getFetchHitCount
std::uint32_t getFetchHitCount() const
Definition: Database.h:199
std::mutex
STL class.
ripple::NodeStore::Database::storeSz_
std::atomic< std::uint32_t > storeSz_
Definition: Database.h:272
ripple::NodeStore::Database::readGen_
uint64_t readGen_
Definition: Database.h:291
ripple::NodeStore::Database::scheduler_
Scheduler & scheduler_
Definition: Database.h:227
ripple::NodeStore::Database::getFetchTotalCount
std::uint32_t getFetchTotalCount() const
Definition: Database.h:196
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:209
ripple::NodeStore::Backend
A backend used for the NodeStore.
Definition: Backend.h:37