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 
131  virtual void
132  sync() = 0;
133 
147  uint256 const& hash,
148  std::uint32_t ledgerSeq = 0,
149  FetchType fetchType = FetchType::synchronous);
150 
163  void
164  asyncFetch(
165  uint256 const& hash,
166  std::uint32_t ledgerSeq,
167  std::function<void(std::shared_ptr<NodeObject> const&)>&& callback);
168 
174  virtual bool
175  storeLedger(std::shared_ptr<Ledger const> const& srcLedger) = 0;
176 
178  virtual void
179  sweep() = 0;
180 
181  virtual Backend&
182  getBackend() = 0;
183 
190  {
191  return storeCount_;
192  }
193 
196  {
197  return fetchTotalCount_;
198  }
199 
202  {
203  return fetchHitCount_;
204  }
205 
207  getStoreSize() const
208  {
209  return storeSz_;
210  }
211 
213  getFetchSize() const
214  {
215  return fetchSz_;
216  }
217 
218  void
220 
222  int
223  fdRequired() const
224  {
225  return fdRequired_;
226  }
227 
228  void
229  onStop() override;
230 
231  void
232  onChildrenStopped() override;
233 
238  {
239  return earliestLedgerSeq_;
240  }
241 
242 protected:
245  int fdRequired_{0};
246 
249 
250  void
251  stopReadThreads();
252 
253  void
255  {
256  assert(count <= sz);
257  storeCount_ += count;
258  storeSz_ += sz;
259  }
260 
261  // Called by the public asyncFetch function
262  void
263  asyncFetch(uint256 const& hash, std::uint32_t ledgerSeq);
264 
265  // Called by the public import function
266  void
267  importInternal(Backend& dstBackend, Database& srcDB);
268 
269  // Called by the public storeLedger function
270  bool
271  storeLedger(Ledger const& srcLedger, std::shared_ptr<Backend> dstBackend);
272 
273  void
274  updateFetchMetrics(uint64_t fetches, uint64_t hits, uint64_t duration)
275  {
276  fetchTotalCount_ += fetches;
277  fetchHitCount_ += hits;
278  fetchDurationUs_ += duration;
279  }
280 
281 private:
287 
290 
291  // reads to do
292  std::map<
293  uint256,
298 
299  // last read
301 
303  bool readShut_{false};
304 
305  // The default is 32570 to match the XRP ledger network's earliest
306  // allowed sequence. Alternate networks may set this value.
308 
311  uint256 const& hash,
312  std::uint32_t ledgerSeq,
313  FetchReport& fetchReport) = 0;
314 
322  virtual void
324 
325  void
326  threadEntry();
327 };
328 
329 } // namespace NodeStore
330 } // namespace ripple
331 
332 #endif
ripple::NodeStore::Database::getFetchSize
std::uint32_t getFetchSize() const
Definition: Database.h:213
ripple::Section
Holds a collection of configuration values.
Definition: BasicConfig.h:43
ripple::NodeStore::Database::readLastHash_
uint256 readLastHash_
Definition: Database.h:300
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:189
ripple::NodeStore::Database::fetchDurationUs_
std::atomic< std::uint64_t > fetchDurationUs_
Definition: Database.h:285
ripple::NodeStore::Database
Persistency layer for NodeObject.
Definition: Database.h:52
std::string
STL class.
ripple::NodeStore::Database::readShut_
bool readShut_
Definition: Database.h:303
std::shared_ptr< NodeObject >
std::pair
ripple::NodeStore::Database::fdRequired_
int fdRequired_
Definition: Database.h:245
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:297
ripple::NodeStore::Database::getBackend
virtual Backend & getBackend()=0
ripple::NodeStore::Database::fetchTotalCount_
std::atomic< std::uint64_t > fetchTotalCount_
Definition: Database.h:284
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:248
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:247
ripple::NodeStore::Database::storeCount_
std::atomic< std::uint64_t > storeCount_
Definition: Database.h:282
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:274
ripple::NodeStore::Database::readLock_
std::mutex readLock_
Definition: Database.h:288
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:207
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:106
thread
ripple::Ledger
Holds a ledger.
Definition: Ledger.h:77
ripple::NodeStore::Database::readCondVar_
std::condition_variable readCondVar_
Definition: Database.h:289
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:237
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:254
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
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:243
ripple::NodeStore::Database::readThreads_
std::vector< std::thread > readThreads_
Definition: Database.h:302
ripple::NodeStore::Database::storeDurationUs_
std::atomic< std::uint64_t > storeDurationUs_
Definition: Database.h:286
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:307
ripple::NodeStore::Database::getFetchHitCount
std::uint32_t getFetchHitCount() const
Definition: Database.h:201
std::mutex
STL class.
ripple::NodeStore::Database::scheduler_
Scheduler & scheduler_
Definition: Database.h:244
ripple::NodeStore::Database::getFetchTotalCount
std::uint32_t getFetchTotalCount() const
Definition: Database.h:195
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:223
ripple::NodeStore::Backend
A backend used for the NodeStore.
Definition: Backend.h:39