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  , writableBackend_(std::move(writableBackend))
38  , archiveBackend_(std::move(archiveBackend))
39 {
40  if (writableBackend_)
41  fdRequired_ += writableBackend_->fdRequired();
42  if (archiveBackend_)
43  fdRequired_ += archiveBackend_->fdRequired();
44  setParent(parent);
45 }
46 
47 void
50  std::string const& writableBackendName)> const& f)
51 {
52  std::lock_guard lock(mutex_);
53 
54  auto newBackend = f(writableBackend_->getName());
55  archiveBackend_->setDeletePath();
56  archiveBackend_ = std::move(writableBackend_);
57  writableBackend_ = std::move(newBackend);
58 }
59 
62 {
63  std::lock_guard lock(mutex_);
64  return writableBackend_->getName();
65 }
66 
69 {
70  std::lock_guard lock(mutex_);
71  return writableBackend_->getWriteLoad();
72 }
73 
74 void
76 {
77  auto const backend = [&] {
78  std::lock_guard lock(mutex_);
79  return writableBackend_;
80  }();
81 
82  importInternal(*backend, source);
83 }
84 
85 bool
87 {
88  auto const backend = [&] {
89  std::lock_guard lock(mutex_);
90  return writableBackend_;
91  }();
92 
93  return Database::storeLedger(*srcLedger, backend);
94 }
95 
96 void
98  NodeObjectType type,
99  Blob&& data,
100  uint256 const& hash,
102 {
103  auto nObj = NodeObject::createObject(type, std::move(data), hash);
104 
105  auto const backend = [&] {
106  std::lock_guard lock(mutex_);
107  return writableBackend_;
108  }();
109 
110  backend->store(nObj);
111  storeStats(1, nObj->getData().size());
112 }
113 
114 void
116 {
117  // nothing to do
118 }
119 
122  uint256 const& hash,
124  FetchReport& fetchReport)
125 {
126  auto fetch = [&](std::shared_ptr<Backend> const& backend) {
127  Status status;
128  std::shared_ptr<NodeObject> nodeObject;
129  try
130  {
131  status = backend->fetch(hash.data(), &nodeObject);
132  }
133  catch (std::exception const& e)
134  {
135  JLOG(j_.fatal()) << "Exception, " << e.what();
136  Rethrow();
137  }
138 
139  switch (status)
140  {
141  case ok:
142  ++fetchHitCount_;
143  if (nodeObject)
144  fetchSz_ += nodeObject->getData().size();
145  break;
146  case notFound:
147  break;
148  case dataCorrupt:
149  JLOG(j_.fatal()) << "Corrupt NodeObject #" << hash;
150  break;
151  default:
152  JLOG(j_.warn()) << "Unknown status=" << status;
153  break;
154  }
155 
156  return nodeObject;
157  };
158 
159  // See if the node object exists in the cache
160  std::shared_ptr<NodeObject> nodeObject;
161 
162  auto [writable, archive] = [&] {
163  std::lock_guard lock(mutex_);
165  }();
166 
167  // Try to fetch from the writable backend
168  nodeObject = fetch(writable);
169  if (!nodeObject)
170  {
171  // Otherwise try to fetch from the archive backend
172  nodeObject = fetch(archive);
173  if (nodeObject)
174  {
175  {
176  // Refresh the writable backend pointer
177  std::lock_guard lock(mutex_);
178  writable = writableBackend_;
179  }
180 
181  // Update writable backend with data from the archive backend
182  writable->store(nodeObject);
183  }
184  }
185 
186  if (nodeObject)
187  fetchReport.wasFound = true;
188 
189  return nodeObject;
190 }
191 
192 void
195 {
196  auto [writable, archive] = [&] {
197  std::lock_guard lock(mutex_);
199  }();
200 
201  // Iterate the writable backend
202  writable->for_each(f);
203 
204  // Iterate the archive backend
205  archive->for_each(f);
206 }
207 
208 } // namespace NodeStore
209 } // namespace ripple
beast::Journal::fatal
Stream fatal() const
Definition: Journal.h:339
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:48
ripple::NodeStore::DatabaseRotatingImp::archiveBackend_
std::shared_ptr< Backend > archiveBackend_
Definition: DatabaseRotatingImp.h:96
ripple::NodeStore::Database
Persistency layer for NodeObject.
Definition: Database.h:52
std::string
STL class.
std::shared_ptr
STL class.
std::exception
STL class.
ripple::NodeStore::ok
@ ok
Definition: nodestore/Types.h:45
ripple::NodeStore::DatabaseRotatingImp::sweep
void sweep() override
Remove expired entries from the positive and negative caches.
Definition: DatabaseRotatingImp.cpp:115
ripple::NodeStore::Database::fdRequired_
int fdRequired_
Definition: Database.h:245
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::DatabaseRotatingImp
DatabaseRotatingImp()=delete
ripple::NodeStore::DatabaseRotatingImp::fetchNodeObject
std::shared_ptr< NodeObject > fetchNodeObject(uint256 const &hash, std::uint32_t, FetchReport &fetchReport) override
Definition: DatabaseRotatingImp.cpp:121
beast::Journal::warn
Stream warn() const
Definition: Journal.h:327
ripple::NodeStore::Database::fetchSz_
std::atomic< std::uint32_t > fetchSz_
Definition: Database.h:248
std::lock_guard
STL class.
ripple::NodeStore::FetchReport
Contains information about a fetch operation.
Definition: ripple/nodestore/Scheduler.h:32
std::function
ripple::NodeStore::DatabaseRotatingImp::getName
std::string getName() const override
Retrieve the name associated with this backend.
Definition: DatabaseRotatingImp.cpp:61
ripple::NodeStore::Database::fetchHitCount_
std::atomic< std::uint32_t > fetchHitCount_
Definition: Database.h:247
ripple::base_uint::data
pointer data()
Definition: base_uint.h:113
ripple::Stoppable::setParent
void setParent(Stoppable &parent)
Set the parent of this Stoppable.
Definition: Stoppable.cpp:43
ripple::NodeStore::notFound
@ notFound
Definition: nodestore/Types.h:46
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:68
ripple::Stoppable
Provides an interface for starting and stopping.
Definition: Stoppable.h:201
ripple::NodeStore::Database::importInternal
void importInternal(Backend &dstBackend, Database &srcDB)
Definition: Database.cpp:106
ripple::Rethrow
void Rethrow()
Rethrow the exception currently being handled.
Definition: contract.h:48
ripple::NodeStore::DatabaseRotatingImp::mutex_
std::mutex mutex_
Definition: DatabaseRotatingImp.h:97
ripple::NodeStore::DatabaseRotatingImp::storeLedger
bool storeLedger(std::shared_ptr< Ledger const > const &srcLedger) override
Store a ledger from a different database.
Definition: DatabaseRotatingImp.cpp:86
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::NodeStore::dataCorrupt
@ dataCorrupt
Definition: nodestore/Types.h:47
std::int32_t
ripple::NodeStore::DatabaseRotating
Definition: DatabaseRotating.h:33
ripple::NodeStore::Scheduler
Scheduling for asynchronous backend activity.
Definition: ripple/nodestore/Scheduler.h:60
ripple::NodeStore::DatabaseRotatingImp::import
void import(Database &source) override
Import objects from another database.
Definition: DatabaseRotatingImp.cpp:75
ripple::NodeStore::Database::storeStats
void storeStats(std::uint64_t count, std::uint64_t sz)
Definition: Database.h:254
ripple::NodeStore::Status
Status
Return codes from Backend operations.
Definition: nodestore/Types.h:44
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
Store a ledger from a different database.
ripple::NodeStore::DatabaseRotatingImp::store
void store(NodeObjectType type, Blob &&data, uint256 const &hash, std::uint32_t) override
Store the object.
Definition: DatabaseRotatingImp.cpp:97
ripple::NodeStore::Database::j_
const beast::Journal j_
Definition: Database.h:243
ripple::NodeStore::FetchReport::wasFound
bool wasFound
Definition: ripple/nodestore/Scheduler.h:40
std
STL namespace.
std::make_pair
T make_pair(T... args)
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:193
ripple::NodeStore::DatabaseRotatingImp::writableBackend_
std::shared_ptr< Backend > writableBackend_
Definition: DatabaseRotatingImp.h:95
std::exception::what
T what(T... args)