rippled
DatabaseRotatingImp.cpp
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 #include <ripple/app/ledger/Ledger.h>
21 #include <ripple/nodestore/impl/DatabaseRotatingImp.h>
22 #include <ripple/protocol/HashPrefix.h>
23 
24 namespace ripple {
25 namespace NodeStore {
26 
28  std::string const& name,
29  Scheduler& scheduler,
30  int readThreads,
31  Stoppable& parent,
32  std::shared_ptr<Backend> writableBackend,
33  std::shared_ptr<Backend> archiveBackend,
34  Section const& config,
36  : DatabaseRotating(name, parent, scheduler, readThreads, config, j)
37  , pCache_(std::make_shared<TaggedCache<uint256, NodeObject>>(
38  name,
41  stopwatch(),
42  j))
43  , nCache_(std::make_shared<KeyCache<uint256>>(
44  name,
45  stopwatch(),
48  , writableBackend_(std::move(writableBackend))
49  , archiveBackend_(std::move(archiveBackend))
50 {
51  if (writableBackend_)
52  fdRequired_ += writableBackend_->fdRequired();
53  if (archiveBackend_)
54  fdRequired_ += archiveBackend_->fdRequired();
55  setParent(parent);
56 }
57 
60  std::shared_ptr<Backend> newBackend,
62 {
63  auto oldBackend{std::move(archiveBackend_)};
64  archiveBackend_ = std::move(writableBackend_);
65  writableBackend_ = std::move(newBackend);
66  return oldBackend;
67 }
68 
69 void
71  NodeObjectType type,
72  Blob&& data,
73  uint256 const& hash,
74  std::uint32_t seq)
75 {
76  auto nObj = NodeObject::createObject(type, std::move(data), hash);
77  pCache_->canonicalize_replace_cache(hash, nObj);
78  getWritableBackend()->store(nObj);
79  nCache_->erase(hash);
80  storeStats(nObj->getData().size());
81 }
82 
83 bool
85  uint256 const& hash,
86  std::uint32_t seq,
88 {
89  // See if the object is in cache
90  object = pCache_->fetch(hash);
91  if (object || nCache_->touch_if_exists(hash))
92  return true;
93  // Otherwise post a read
95  return false;
96 }
97 
98 void
100 {
101  pCache_->setTargetSize(size);
102  pCache_->setTargetAge(age);
103  nCache_->setTargetSize(size);
104  nCache_->setTargetAge(age);
105 }
106 
107 void
109 {
110  pCache_->sweep();
111  nCache_->sweep();
112 }
113 
116 {
117  Backends b = getBackends();
118  auto nObj = fetchInternal(hash, b.writableBackend);
119  if (!nObj)
120  {
121  nObj = fetchInternal(hash, b.archiveBackend);
122  if (nObj)
123  {
124  getWritableBackend()->store(nObj);
125  nCache_->erase(hash);
126  }
127  }
128  return nObj;
129 }
130 
131 } // namespace NodeStore
132 } // namespace ripple
ripple::Section
Holds a collection of configuration values.
Definition: BasicConfig.h:43
ripple::NodeStore::cacheTargetSize
@ cacheTargetSize
Definition: nodestore/impl/Tuning.h:28
ripple::NodeStore::DatabaseRotatingImp::archiveBackend_
std::shared_ptr< Backend > archiveBackend_
Definition: DatabaseRotatingImp.h:149
std::string
STL class.
std::shared_ptr
STL class.
ripple::TaggedCache
Map/cache combination.
Definition: TaggedCache.h:54
ripple::KeyCache::erase
bool erase(key_type const &key)
Remove the specified cache entry.
Definition: KeyCache.h:253
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
ripple::NodeStore::Database::fdRequired_
int fdRequired_
Definition: Database.h:244
std::vector< unsigned char >
ripple::NodeObjectType
NodeObjectType
The types of node objects.
Definition: NodeObject.h:32
ripple::NodeObject::createObject
static std::shared_ptr< NodeObject > createObject(NodeObjectType type, Blob &&data, uint256 const &hash)
Create an object from fields.
Definition: NodeObject.cpp:37
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
ripple::stopwatch
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition: chrono.h:86
ripple::Stoppable::setParent
void setParent(Stoppable &parent)
Set the parent of this Stoppable.
Definition: Stoppable.cpp:43
ripple::KeyCache::touch_if_exists
bool touch_if_exists(KeyComparable const &key)
Refresh the last access time on a key if present.
Definition: KeyCache.h:234
ripple::base_uint< 256 >
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:84
ripple::NodeStore::DatabaseRotatingImp::getBackends
Backends getBackends() const
Definition: DatabaseRotatingImp.h:159
ripple::NodeStore::cacheTargetAge
constexpr std::chrono::seconds cacheTargetAge
Definition: nodestore/impl/Tuning.h:36
ripple::NodeStore::Database::storeStats
void storeStats(size_t sz)
Definition: Database.h:250
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::uint32_t
ripple::NodeStore::DatabaseRotating
Definition: DatabaseRotating.h:33
ripple::NodeStore::Scheduler
Scheduling for asynchronous backend activity.
Definition: ripple/nodestore/Scheduler.h:57
ripple::KeyCache::sweep
void sweep()
Remove stale entries from the cache.
Definition: KeyCache.h:267
ripple::NodeStore::Database::fetchInternal
std::shared_ptr< NodeObject > fetchInternal(uint256 const &hash, std::shared_ptr< Backend > backend)
Definition: Database.cpp:123
ripple::NodeStore::DatabaseRotatingImp::fetchFrom
std::shared_ptr< NodeObject > fetchFrom(uint256 const &hash, std::uint32_t seq) override
Definition: DatabaseRotatingImp.cpp:115
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::KeyCache::setTargetSize
void setTargetSize(size_type s)
Definition: KeyCache.h:177
std
STL namespace.
ripple::NodeStore::DatabaseRotatingImp::Backends::writableBackend
std::shared_ptr< Backend > const & writableBackend
Definition: DatabaseRotatingImp.h:154
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.
ripple::KeyCache::setTargetAge
void setTargetAge(std::chrono::seconds s)
Definition: KeyCache.h:184
ripple::NodeStore::DatabaseRotatingImp::Backends
Definition: DatabaseRotatingImp.h:152
ripple::NodeObject
A simple object that the Ledger uses to store entries.
Definition: NodeObject.h:48
ripple::KeyCache
Maintains a cache of keys with no associated data.
Definition: KeyCache.h:43
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::writableBackend_
std::shared_ptr< Backend > writableBackend_
Definition: DatabaseRotatingImp.h:148
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