xrpld
Loading...
Searching...
No Matches
DatabaseRotatingImp.cpp
1#include <xrpl/nodestore/detail/DatabaseRotatingImp.h>
2
3#include <xrpl/basics/Blob.h>
4#include <xrpl/basics/Log.h>
5#include <xrpl/basics/base_uint.h>
6#include <xrpl/basics/contract.h>
7#include <xrpl/beast/utility/Journal.h>
8#include <xrpl/config/BasicConfig.h>
9#include <xrpl/nodestore/Backend.h>
10#include <xrpl/nodestore/Database.h>
11#include <xrpl/nodestore/DatabaseRotating.h>
12#include <xrpl/nodestore/NodeObject.h>
13#include <xrpl/nodestore/Scheduler.h>
14#include <xrpl/nodestore/Types.h>
15
16#include <cstdint>
17#include <exception>
18#include <functional>
19#include <memory>
20#include <mutex>
21#include <string>
22#include <utility>
23
24namespace xrpl::NodeStore {
25
27 Scheduler& scheduler,
28 int readThreads,
29 std::shared_ptr<Backend> writableBackend,
30 std::shared_ptr<Backend> archiveBackend,
31 Section const& config,
33 : DatabaseRotating(scheduler, readThreads, config, j)
34 , writableBackend_(std::move(writableBackend))
35 , archiveBackend_(std::move(archiveBackend))
36{
38 fdRequired_ += writableBackend_->fdRequired();
40 fdRequired_ += archiveBackend_->fdRequired();
41}
42
43void
46 std::function<void(std::string const& writableName, std::string const& archiveName)> const& f)
47{
48 // Pass these two names to the callback function
49 std::string const newWritableBackendName = newBackend->getName();
50 std::string newArchiveBackendName;
51 // Hold on to current archive backend pointer until after the
52 // callback finishes. Only then will the archive directory be
53 // deleted.
55 {
56 std::scoped_lock const lock(mutex_);
57
58 archiveBackend_->setDeletePath();
59 oldArchiveBackend = std::move(archiveBackend_);
60
62 newArchiveBackendName = archiveBackend_->getName();
63
64 writableBackend_ = std::move(newBackend);
65 }
66
67 f(newWritableBackendName, newArchiveBackendName);
68}
69
72{
73 std::scoped_lock const lock(mutex_);
74 return writableBackend_->getName();
75}
76
79{
80 std::scoped_lock const lock(mutex_);
81 return writableBackend_->getWriteLoad();
82}
83
84void
86{
87 auto const backend = [&] {
88 std::scoped_lock const lock(mutex_);
89 return writableBackend_;
90 }();
91
92 importInternal(*backend, source);
93}
94
95void
101
102void
104{
105 auto nObj = NodeObject::createObject(type, std::move(data), hash);
106
107 auto const backend = [&] {
108 std::scoped_lock const lock(mutex_);
109 return writableBackend_;
110 }();
111
112 backend->store(nObj);
113 storeStats(1, nObj->getData().size());
114}
115
116void
118{
119 // Nothing to do.
120}
121
124 uint256 const& hash,
126 FetchReport& fetchReport,
127 bool duplicate)
128{
129 auto fetch = [&](std::shared_ptr<Backend> const& backend) {
130 Status status = Status::Ok;
132 try
133 {
134 status = backend->fetch(hash, &nodeObject);
135 }
136 catch (std::exception const& e)
137 {
138 JLOG(j_.fatal()) << "Exception, " << e.what();
139 rethrow();
140 }
141
142 switch (status)
143 {
144 case Status::Ok:
145 case Status::NotFound:
146 break;
148 JLOG(j_.fatal()) << "Corrupt NodeObject #" << hash;
149 break;
150 default:
151 JLOG(j_.warn()) << "Unknown status=" << static_cast<int>(status);
152 break;
153 }
154
155 return nodeObject;
156 };
157
158 // See if the node object exists in the cache
160
161 auto [writable, archive] = [&] {
162 std::scoped_lock const lock(mutex_);
164 }();
165
166 // Try to fetch from the writable backend
167 nodeObject = fetch(writable);
168 if (!nodeObject)
169 {
170 // Otherwise try to fetch from the archive backend
171 nodeObject = fetch(archive);
172 if (nodeObject)
173 {
174 {
175 // Refresh the writable backend pointer
176 std::scoped_lock const lock(mutex_);
177 writable = writableBackend_;
178 }
179
180 // Update writable backend with data from the archive backend
181 if (duplicate)
182 writable->store(nodeObject);
183 }
184 }
185
186 if (nodeObject)
187 fetchReport.wasFound = true;
188
189 return nodeObject;
190}
191
192void
194{
195 auto [writable, archive] = [&] {
196 std::scoped_lock const lock(mutex_);
198 }();
199
200 // Iterate the writable backend
201 writable->forEach(f);
202
203 // Iterate the archive backend
204 archive->forEach(f);
205}
206
207} // namespace xrpl::NodeStore
A generic endpoint for log messages.
Definition Journal.h:38
static std::shared_ptr< NodeObject > createObject(NodeObjectType type, Blob &&data, uint256 const &hash)
Create an object from fields.
std::shared_ptr< Backend > archiveBackend_
void forEach(std::function< void(std::shared_ptr< NodeObject >)> f) override
Visit every object in the database This is usually called during import.
std::int32_t getWriteLoad() const override
Retrieve the estimated number of pending write operations.
void importDatabase(Database &source) override
Import objects from another database.
void sweep() override
Remove expired entries from the positive and negative caches.
std::shared_ptr< Backend > writableBackend_
void rotate(std::unique_ptr< NodeStore::Backend > &&newBackend, std::function< void(std::string const &writableName, std::string const &archiveName)> const &f) override
Rotates the backends.
std::string getName() const override
Retrieve the name associated with this backend.
std::shared_ptr< NodeObject > fetchNodeObject(uint256 const &hash, std::uint32_t, FetchReport &fetchReport, bool duplicate) override
void store(NodeObjectType type, Blob &&data, uint256 const &hash, std::uint32_t) override
Store the object.
DatabaseRotating(Scheduler &scheduler, int readThreads, Section const &config, beast::Journal journal)
Persistency layer for NodeObject.
Definition Database.h:32
void storeStats(std::uint64_t count, std::uint64_t sz)
Definition Database.h:221
beast::Journal const j_
Definition Database.h:200
void importInternal(Backend &dstBackend, Database &srcDB)
Definition Database.cpp:193
Scheduling for asynchronous backend activity.
Holds a collection of configuration values.
Definition BasicConfig.h:24
T make_pair(T... args)
STL namespace.
Status
Return codes from Backend operations.
NodeObjectType
The types of node objects.
Definition NodeObject.h:12
XRPL_NO_SANITIZE_ADDRESS void rethrow()
Rethrow the exception currently being handled.
Definition contract.h:33
std::vector< unsigned char > Blob
Storage for linear binary data.
Definition Blob.h:10
BaseUInt< 256 > uint256
Definition base_uint.h:562
Contains information about a fetch operation.
T what(T... args)