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/nodestore/impl/DatabaseRotatingImp.h>
21 #include <ripple/app/ledger/Ledger.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>>(
39  , nCache_(std::make_shared<KeyCache<uint256>>(
41  , writableBackend_(std::move(writableBackend))
42  , archiveBackend_(std::move(archiveBackend))
43 {
44  if (writableBackend_)
45  fdRequired_ += writableBackend_->fdRequired();
46  if (archiveBackend_)
47  fdRequired_ += archiveBackend_->fdRequired();
48  setParent(parent);
49 }
50 
53  std::shared_ptr<Backend> newBackend,
55 {
56  auto oldBackend {std::move(archiveBackend_)};
57  archiveBackend_ = std::move(writableBackend_);
58  writableBackend_ = std::move(newBackend);
59  return oldBackend;
60 }
61 
62 void
64  uint256 const& hash, std::uint32_t seq)
65 {
66  auto nObj = NodeObject::createObject(type, std::move(data), hash);
67  pCache_->canonicalize_replace_cache(hash, nObj);
68  getWritableBackend()->store(nObj);
69  nCache_->erase(hash);
70  storeStats(nObj->getData().size());
71 }
72 
73 bool
76 {
77  // See if the object is in cache
78  object = pCache_->fetch(hash);
79  if (object || nCache_->touch_if_exists(hash))
80  return true;
81  // Otherwise post a read
83  return false;
84 }
85 
86 void
88 {
89  pCache_->setTargetSize(size);
90  pCache_->setTargetAge(age);
91  nCache_->setTargetSize(size);
92  nCache_->setTargetAge(age);
93 }
94 
95 void
97 {
98  pCache_->sweep();
99  nCache_->sweep();
100 }
101 
104 {
105  Backends b = getBackends();
106  auto nObj = fetchInternal(hash, b.writableBackend);
107  if (! nObj)
108  {
109  nObj = fetchInternal(hash, b.archiveBackend);
110  if (nObj)
111  {
112  getWritableBackend()->store(nObj);
113  nCache_->erase(hash);
114  }
115  }
116  return nObj;
117 }
118 
119 } // NodeStore
120 } // ripple
ripple::Section
Holds a collection of configuration values.
Definition: BasicConfig.h:43
ripple::NodeStore::cacheTargetSize
@ cacheTargetSize
Definition: nodestore/impl/Tuning.h:29
ripple::NodeStore::DatabaseRotatingImp::archiveBackend_
std::shared_ptr< Backend > archiveBackend_
Definition: DatabaseRotatingImp.h:132
std::string
STL class.
std::shared_ptr
STL class.
ripple::TaggedCache
Map/cache combination.
Definition: TaggedCache.h:55
ripple::KeyCache::erase
bool erase(key_type const &key)
Remove the specified cache entry.
Definition: KeyCache.h:235
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
ripple::NodeStore::Database::fdRequired_
int fdRequired_
Definition: Database.h:228
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:39
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
ripple::stopwatch
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition: chrono.h:87
ripple::Stoppable::setParent
void setParent(Stoppable &parent)
Set the parent of this Stoppable.
Definition: Stoppable.cpp:46
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:217
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:74
ripple::NodeStore::DatabaseRotatingImp::getBackends
Backends getBackends() const
Definition: DatabaseRotatingImp.h:140
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:234
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:60
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:248
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:103
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:165
std
STL namespace.
ripple::NodeStore::DatabaseRotatingImp::Backends::writableBackend
std::shared_ptr< Backend > const & writableBackend
Definition: DatabaseRotatingImp.h:136
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:171
ripple::NodeStore::DatabaseRotatingImp::Backends
Definition: DatabaseRotatingImp.h:135
ripple::NodeObject
A simple object that the Ledger uses to store entries.
Definition: NodeObject.h:50
ripple::KeyCache
Maintains a cache of keys with no associated data.
Definition: KeyCache.h:44
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::writableBackend_
std::shared_ptr< Backend > writableBackend_
Definition: DatabaseRotatingImp.h:131
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