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 
53  getWritableBackend() const override
54  {
56  return writableBackend_;
57  }
58 
61  std::shared_ptr<Backend> newBackend,
62  std::lock_guard<std::mutex> const&) override;
63 
64  std::mutex&
65  peekMutex() const override
66  {
67  return rotateMutex_;
68  }
69 
71  getName() const override
72  {
73  return getWritableBackend()->getName();
74  }
75 
77  getWriteLoad() const override
78  {
79  return getWritableBackend()->getWriteLoad();
80  }
81 
82  void
83  import(Database& source) override
84  {
86  }
87 
88  void
89  store(
90  NodeObjectType type,
91  Blob&& data,
92  uint256 const& hash,
93  std::uint32_t seq) override;
94 
96  fetch(uint256 const& hash, std::uint32_t seq) override
97  {
98  return doFetch(hash, seq, *pCache_, *nCache_, false);
99  }
100 
101  bool
102  asyncFetch(
103  uint256 const& hash,
104  std::uint32_t seq,
105  std::shared_ptr<NodeObject>& object) override;
106 
107  bool
108  storeLedger(std::shared_ptr<Ledger const> const& srcLedger) override
109  {
110  return Database::storeLedger(
111  *srcLedger, getWritableBackend(), pCache_, nCache_, nullptr);
112  }
113 
114  int
116  {
117  // We prefer a client not fill our cache
118  // We don't want to push data out of the cache
119  // before it's retrieved
120  return pCache_->getTargetSize() / asyncDivider;
121  }
122 
123  float
124  getCacheHitRate() override
125  {
126  return pCache_->getHitRate();
127  }
128 
129  void
130  tune(int size, std::chrono::seconds age) override;
131 
132  void
133  sweep() override;
134 
136  getPositiveCache() override
137  {
138  return *pCache_;
139  }
140 
141 private:
142  // Positive cache
144 
145  // Negative cache
147 
151 
152  struct Backends
153  {
156  };
157 
158  Backends
159  getBackends() const
160  {
163  }
164 
166  fetchFrom(uint256 const& hash, std::uint32_t seq) override;
167 
168  void
170  {
171  Backends b = getBackends();
172  b.archiveBackend->for_each(f);
173  b.writableBackend->for_each(f);
174  }
175 };
176 
177 } // namespace NodeStore
178 } // namespace ripple
179 
180 #endif
ripple::Section
Holds a collection of configuration values.
Definition: BasicConfig.h:43
ripple::NodeStore::DatabaseRotatingImp::getCacheHitRate
float getCacheHitRate() override
Get the positive cache hits to total attempts ratio.
Definition: DatabaseRotatingImp.h:124
ripple::NodeStore::DatabaseRotatingImp::archiveBackend_
std::shared_ptr< Backend > archiveBackend_
Definition: DatabaseRotatingImp.h:149
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: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::DatabaseRotatingImp::fetch
std::shared_ptr< NodeObject > fetch(uint256 const &hash, std::uint32_t seq) override
Fetch an object.
Definition: DatabaseRotatingImp.h:96
ripple::NodeStore::DatabaseRotatingImp::sweep
void sweep() override
Remove expired entries from the positive and negative caches.
Definition: DatabaseRotatingImp.cpp:108
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:99
std::vector< unsigned char >
ripple::NodeObjectType
NodeObjectType
The types of node objects.
Definition: NodeObject.h:32
ripple::NodeStore::DatabaseRotatingImp::Backends::archiveBackend
std::shared_ptr< Backend > const & archiveBackend
Definition: DatabaseRotatingImp.h:155
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:70
std::lock_guard
STL class.
ripple::NodeStore::DatabaseRotatingImp::getWritableBackend
std::shared_ptr< Backend > const & getWritableBackend() const override
Definition: DatabaseRotatingImp.h:53
std::function
ripple::NodeStore::DatabaseRotatingImp::getName
std::string getName() const override
Retrieve the name associated with this backend.
Definition: DatabaseRotatingImp.h:71
ripple::NodeStore::DatabaseRotatingImp::getPositiveCache
TaggedCache< uint256, NodeObject > const & getPositiveCache() override
Definition: DatabaseRotatingImp.h:136
ripple::base_uint< 256 >
ripple::NodeStore::DatabaseRotatingImp::getWriteLoad
std::int32_t getWriteLoad() const override
Retrieve the estimated number of pending write operations.
Definition: DatabaseRotatingImp.h:77
ripple::NodeStore::DatabaseRotatingImp
Definition: DatabaseRotatingImp.h:28
ripple::NodeStore::DatabaseRotatingImp::peekMutex
std::mutex & peekMutex() const override
Definition: DatabaseRotatingImp.h:65
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::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:84
ripple::NodeStore::DatabaseRotatingImp::getBackends
Backends getBackends() const
Definition: DatabaseRotatingImp.h:159
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.h:108
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::rotateMutex_
std::mutex rotateMutex_
Definition: DatabaseRotatingImp.h:150
ripple::NodeStore::DatabaseRotatingImp::fetchFrom
std::shared_ptr< NodeObject > fetchFrom(uint256 const &hash, std::uint32_t seq) override
Definition: DatabaseRotatingImp.cpp:115
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:115
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
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::stopThreads
void stopThreads()
Definition: Database.cpp:93
ripple::NodeStore::DatabaseRotatingImp::Backends::writableBackend
std::shared_ptr< Backend > const & writableBackend
Definition: DatabaseRotatingImp.h:154
ripple::NodeStore::DatabaseRotatingImp::operator=
DatabaseRotatingImp & operator=(DatabaseRotatingImp const &)=delete
ripple::NodeStore::DatabaseRotatingImp::Backends
Definition: DatabaseRotatingImp.h:152
std::mutex
STL class.
ripple::NodeStore::DatabaseRotatingImp::rotateBackends
std::shared_ptr< Backend > rotateBackends(std::shared_ptr< Backend > newBackend, std::lock_guard< std::mutex > const &) override
Definition: DatabaseRotatingImp.cpp:59
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.h:169
ripple::NodeStore::DatabaseRotatingImp::writableBackend_
std::shared_ptr< Backend > writableBackend_
Definition: DatabaseRotatingImp.h:148
ripple::NodeStore::asyncDivider
@ asyncDivider
Definition: nodestore/impl/Tuning.h:32
ripple::NodeStore::DatabaseRotatingImp::nCache_
std::shared_ptr< KeyCache< uint256 > > nCache_
Definition: DatabaseRotatingImp.h:146
ripple::NodeStore::DatabaseRotatingImp::pCache_
std::shared_ptr< TaggedCache< uint256, NodeObject > > pCache_
Definition: DatabaseRotatingImp.h:143