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 
36  std::string const& name,
37  Scheduler& scheduler,
38  int readThreads,
39  Stoppable& parent,
40  std::shared_ptr<Backend> writableBackend,
41  std::shared_ptr<Backend> archiveBackend,
42  Section const& config,
43  beast::Journal j);
44 
46  {
47  // Stop threads before data members are destroyed.
48  stopThreads();
49  }
50 
52  getWritableBackend() const override
53  {
55  return writableBackend_;
56  }
57 
60  std::shared_ptr<Backend> newBackend,
61  std::lock_guard<std::mutex> const&) override;
62 
63  std::mutex& peekMutex() const override
64  {
65  return rotateMutex_;
66  }
67 
68  std::string getName() const override
69  {
70  return getWritableBackend()->getName();
71  }
72 
73  std::int32_t getWriteLoad() const override
74  {
75  return getWritableBackend()->getWriteLoad();
76  }
77 
78  void import (Database& source) override
79  {
81  }
82 
83  void store(NodeObjectType type, Blob&& data,
84  uint256 const& hash, std::uint32_t seq) override;
85 
87  fetch(uint256 const& hash, std::uint32_t seq) override
88  {
89  return doFetch(hash, seq, *pCache_, *nCache_, false);
90  }
91 
92  bool
93  asyncFetch(uint256 const& hash, std::uint32_t seq,
94  std::shared_ptr<NodeObject>& object) override;
95 
96  bool
97  storeLedger(std::shared_ptr<Ledger const> const& srcLedger) override
98  {
99  return Database::storeLedger(
100  *srcLedger, getWritableBackend(), pCache_, nCache_, nullptr);
101  }
102 
103  int
105  {
106  // We prefer a client not fill our cache
107  // We don't want to push data out of the cache
108  // before it's retrieved
109  return pCache_->getTargetSize() / asyncDivider;
110  }
111 
112  float
113  getCacheHitRate() override {return pCache_->getHitRate();}
114 
115  void
116  tune(int size, std::chrono::seconds age) override;
117 
118  void
119  sweep() override;
120 
122  getPositiveCache() override {return *pCache_;}
123 
124 private:
125  // Positive cache
127 
128  // Negative cache
130 
134 
135  struct Backends {
138  };
139 
141  {
144  }
145 
147  uint256 const& hash, std::uint32_t seq) override;
148 
149  void
151  {
152  Backends b = getBackends();
153  b.archiveBackend->for_each(f);
154  b.writableBackend->for_each(f);
155  }
156 };
157 
158 }
159 }
160 
161 #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:113
ripple::NodeStore::DatabaseRotatingImp::archiveBackend_
std::shared_ptr< Backend > archiveBackend_
Definition: DatabaseRotatingImp.h:132
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: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::DatabaseRotatingImp::fetch
std::shared_ptr< NodeObject > fetch(uint256 const &hash, std::uint32_t seq) override
Fetch an object.
Definition: DatabaseRotatingImp.h:87
ripple::NodeStore::DatabaseRotatingImp::sweep
void sweep() override
Remove expired entries from the positive and negative caches.
Definition: DatabaseRotatingImp.cpp:96
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:87
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:137
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:63
std::lock_guard
STL class.
ripple::NodeStore::DatabaseRotatingImp::getWritableBackend
std::shared_ptr< Backend > const & getWritableBackend() const override
Definition: DatabaseRotatingImp.h:52
std::function
ripple::NodeStore::DatabaseRotatingImp::getName
std::string getName() const override
Retrieve the name associated with this backend.
Definition: DatabaseRotatingImp.h:68
ripple::NodeStore::DatabaseRotatingImp::getPositiveCache
TaggedCache< uint256, NodeObject > const & getPositiveCache() override
Definition: DatabaseRotatingImp.h:122
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:73
ripple::NodeStore::DatabaseRotatingImp
Definition: DatabaseRotatingImp.h:28
ripple::NodeStore::DatabaseRotatingImp::peekMutex
std::mutex & peekMutex() const override
Definition: DatabaseRotatingImp.h:63
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::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:74
ripple::NodeStore::DatabaseRotatingImp::getBackends
Backends getBackends() const
Definition: DatabaseRotatingImp.h:140
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:97
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:60
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:45
ripple::NodeStore::DatabaseRotatingImp::rotateMutex_
std::mutex rotateMutex_
Definition: DatabaseRotatingImp.h:133
ripple::NodeStore::DatabaseRotatingImp::fetchFrom
std::shared_ptr< NodeObject > fetchFrom(uint256 const &hash, std::uint32_t seq) override
Definition: DatabaseRotatingImp.cpp:103
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:104
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:95
ripple::NodeStore::DatabaseRotatingImp::Backends::writableBackend
std::shared_ptr< Backend > const & writableBackend
Definition: DatabaseRotatingImp.h:136
ripple::NodeStore::DatabaseRotatingImp::operator=
DatabaseRotatingImp & operator=(DatabaseRotatingImp const &)=delete
ripple::NodeStore::DatabaseRotatingImp::Backends
Definition: DatabaseRotatingImp.h:135
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:52
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:150
ripple::NodeStore::DatabaseRotatingImp::writableBackend_
std::shared_ptr< Backend > writableBackend_
Definition: DatabaseRotatingImp.h:131
ripple::NodeStore::asyncDivider
@ asyncDivider
Definition: nodestore/impl/Tuning.h:32
ripple::NodeStore::DatabaseRotatingImp::nCache_
std::shared_ptr< KeyCache< uint256 > > nCache_
Definition: DatabaseRotatingImp.h:129
ripple::NodeStore::DatabaseRotatingImp::pCache_
std::shared_ptr< TaggedCache< uint256, NodeObject > > pCache_
Definition: DatabaseRotatingImp.h:126