rippled
DatabaseRotatingImp.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 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_DATABASEROTATINGIMP_H_INCLUDED
21 #define RIPPLE_NODESTORE_DATABASEROTATINGIMP_H_INCLUDED
22 
23 #include <ripple/nodestore/DatabaseRotating.h>
24 
25 namespace ripple {
26 namespace NodeStore {
27 
29 {
30 public:
31  DatabaseRotatingImp() = delete;
34  operator=(DatabaseRotatingImp const&) = delete;
35 
37  std::string const& name,
38  Scheduler& scheduler,
39  int readThreads,
40  Stoppable& parent,
41  std::shared_ptr<Backend> writableBackend,
42  std::shared_ptr<Backend> archiveBackend,
43  Section const& config,
44  beast::Journal j);
45 
47  {
48  // Stop threads before data members are destroyed.
49  stopThreads();
50  }
51 
52  void
55  std::string const& writableBackendName)> const& f) override;
56 
58  getName() const override;
59 
61  getWriteLoad() const override;
62 
63  void
64  import(Database& source) override;
65 
66  void
67  store(
68  NodeObjectType type,
69  Blob&& data,
70  uint256 const& hash,
71  std::uint32_t seq) override;
72 
74  fetch(uint256 const& hash, std::uint32_t seq) override
75  {
76  return doFetch(hash, seq, *pCache_, *nCache_, false);
77  }
78 
79  bool
80  asyncFetch(
81  uint256 const& hash,
82  std::uint32_t seq,
83  std::shared_ptr<NodeObject>& object) override;
84 
85  bool
86  storeLedger(std::shared_ptr<Ledger const> const& srcLedger) override;
87 
88  int
90  {
91  // We prefer a client not fill our cache
92  // We don't want to push data out of the cache
93  // before it's retrieved
94  return pCache_->getTargetSize() / asyncDivider;
95  }
96 
97  float
98  getCacheHitRate() override
99  {
100  return pCache_->getHitRate();
101  }
102 
103  void
104  tune(int size, std::chrono::seconds age) override;
105 
106  void
107  sweep() override;
108 
110  getPositiveCache() override
111  {
112  return *pCache_;
113  }
114 
115 private:
116  // Positive cache
118 
119  // Negative cache
121 
125 
127  fetchFrom(uint256 const& hash, std::uint32_t seq) override;
128 
129  void
131 };
132 
133 } // namespace NodeStore
134 } // namespace ripple
135 
136 #endif
ripple::Section
Holds a collection of configuration values.
Definition: BasicConfig.h:43
ripple::NodeStore::DatabaseRotatingImp::rotateWithLock
void rotateWithLock(std::function< std::unique_ptr< NodeStore::Backend >(std::string const &writableBackendName)> const &f) override
Rotates the backends.
Definition: DatabaseRotatingImp.cpp:59
ripple::NodeStore::DatabaseRotatingImp::getCacheHitRate
float getCacheHitRate() override
Get the positive cache hits to total attempts ratio.
Definition: DatabaseRotatingImp.h:98
ripple::NodeStore::DatabaseRotatingImp::archiveBackend_
std::shared_ptr< Backend > archiveBackend_
Definition: DatabaseRotatingImp.h:123
ripple::NodeStore::Database
Persistency layer for NodeObject.
Definition: Database.h:53
std::string
STL class.
std::shared_ptr
STL class.
ripple::TaggedCache
Map/cache combination.
Definition: TaggedCache.h:52
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::DatabaseRotatingImp::fetch
std::shared_ptr< NodeObject > fetch(uint256 const &hash, std::uint32_t seq) override
Fetch an object.
Definition: DatabaseRotatingImp.h:74
ripple::NodeStore::DatabaseRotatingImp::sweep
void sweep() override
Remove expired entries from the positive and negative caches.
Definition: DatabaseRotatingImp.cpp:154
ripple::NodeStore::DatabaseRotatingImp::tune
void tune(int size, std::chrono::seconds age) override
Set the maximum number of entries and maximum cache age for both caches.
Definition: DatabaseRotatingImp.cpp:145
std::vector< unsigned char >
ripple::NodeObjectType
NodeObjectType
The types of node objects.
Definition: NodeObject.h:32
std::chrono::seconds
ripple::NodeStore::DatabaseRotatingImp::DatabaseRotatingImp
DatabaseRotatingImp()=delete
ripple::NodeStore::DatabaseRotatingImp::store
void store(NodeObjectType type, Blob &&data, uint256 const &hash, std::uint32_t seq) override
Store the object.
Definition: DatabaseRotatingImp.cpp:109
std::function
ripple::NodeStore::DatabaseRotatingImp::getName
std::string getName() const override
Retrieve the name associated with this backend.
Definition: DatabaseRotatingImp.cpp:72
ripple::NodeStore::DatabaseRotatingImp::getPositiveCache
TaggedCache< uint256, NodeObject > const & getPositiveCache() override
Definition: DatabaseRotatingImp.h:110
ripple::base_uint< 256 >
ripple::NodeStore::DatabaseRotatingImp::getWriteLoad
std::int32_t getWriteLoad() const override
Retrieve the estimated number of pending write operations.
Definition: DatabaseRotatingImp.cpp:79
ripple::NodeStore::DatabaseRotatingImp
Definition: DatabaseRotatingImp.h:28
ripple::Stoppable
Provides an interface for starting and stopping.
Definition: Stoppable.h:200
ripple::NodeStore::DatabaseRotatingImp::asyncFetch
bool asyncFetch(uint256 const &hash, std::uint32_t seq, std::shared_ptr< NodeObject > &object) override
Fetch an object without waiting.
Definition: DatabaseRotatingImp.cpp:129
ripple::NodeStore::DatabaseRotatingImp::mutex_
std::mutex mutex_
Definition: DatabaseRotatingImp.h:124
ripple::NodeStore::DatabaseRotatingImp::storeLedger
bool storeLedger(std::shared_ptr< Ledger const > const &srcLedger) override
Copies a ledger stored in a different database to this one.
Definition: DatabaseRotatingImp.cpp:97
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::int32_t
ripple::NodeStore::DatabaseRotating
Definition: DatabaseRotating.h:33
ripple::NodeStore::Scheduler
Scheduling for asynchronous backend activity.
Definition: ripple/nodestore/Scheduler.h:57
ripple::NodeStore::DatabaseRotatingImp::~DatabaseRotatingImp
~DatabaseRotatingImp() override
Definition: DatabaseRotatingImp.h:46
ripple::NodeStore::DatabaseRotatingImp::fetchFrom
std::shared_ptr< NodeObject > fetchFrom(uint256 const &hash, std::uint32_t seq) override
Definition: DatabaseRotatingImp.cpp:161
ripple::NodeStore::DatabaseRotatingImp::getDesiredAsyncReadCount
int getDesiredAsyncReadCount(std::uint32_t seq) override
Get the maximum number of async reads the node store prefers.
Definition: DatabaseRotatingImp.h:89
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::NodeStore::Database::stopThreads
void stopThreads()
Definition: Database.cpp:93
ripple::NodeStore::DatabaseRotatingImp::operator=
DatabaseRotatingImp & operator=(DatabaseRotatingImp const &)=delete
std::mutex
STL class.
std::unique_ptr
STL class.
ripple::NodeStore::DatabaseRotatingImp::for_each
void for_each(std::function< void(std::shared_ptr< NodeObject >)> f) override
Visit every object in the database This is usually called during import.
Definition: DatabaseRotatingImp.cpp:191
ripple::NodeStore::DatabaseRotatingImp::writableBackend_
std::shared_ptr< Backend > writableBackend_
Definition: DatabaseRotatingImp.h:122
ripple::NodeStore::asyncDivider
@ asyncDivider
Definition: nodestore/impl/Tuning.h:32
ripple::NodeStore::DatabaseRotatingImp::nCache_
std::shared_ptr< KeyCache< uint256 > > nCache_
Definition: DatabaseRotatingImp.h:120
ripple::NodeStore::DatabaseRotatingImp::pCache_
std::shared_ptr< TaggedCache< uint256, NodeObject > > pCache_
Definition: DatabaseRotatingImp.h:117