rippled
Loading...
Searching...
No Matches
DatabaseRotatingImp.cpp
1#include <xrpl/nodestore/detail/DatabaseRotatingImp.h>
2
3namespace xrpl {
4namespace NodeStore {
5
7 Scheduler& scheduler,
8 int readThreads,
9 std::shared_ptr<Backend> writableBackend,
10 std::shared_ptr<Backend> archiveBackend,
11 Section const& config,
13 : DatabaseRotating(scheduler, readThreads, config, j)
14 , writableBackend_(std::move(writableBackend))
15 , archiveBackend_(std::move(archiveBackend))
16{
20 fdRequired_ += archiveBackend_->fdRequired();
21}
22
23void
26 std::function<void(std::string const& writableName, std::string const& archiveName)> const& f)
27{
28 // Pass these two names to the callback function
29 std::string const newWritableBackendName = newBackend->getName();
30 std::string newArchiveBackendName;
31 // Hold on to current archive backend pointer until after the
32 // callback finishes. Only then will the archive directory be
33 // deleted.
35 {
37
38 archiveBackend_->setDeletePath();
39 oldArchiveBackend = std::move(archiveBackend_);
40
42 newArchiveBackendName = archiveBackend_->getName();
43
44 writableBackend_ = std::move(newBackend);
45 }
46
47 f(newWritableBackendName, newArchiveBackendName);
48}
49
52{
54 return writableBackend_->getName();
55}
56
59{
61 return writableBackend_->getWriteLoad();
62}
63
64void
66{
67 auto const backend = [&] {
69 return writableBackend_;
70 }();
71
72 importInternal(*backend, source);
73}
74
75void
81
82void
84{
85 auto nObj = NodeObject::createObject(type, std::move(data), hash);
86
87 auto const backend = [&] {
89 return writableBackend_;
90 }();
91
92 backend->store(nObj);
93 storeStats(1, nObj->getData().size());
94}
95
96void
98{
99 // nothing to do
100}
101
104{
105 auto fetch = [&](std::shared_ptr<Backend> const& backend) {
106 Status status;
108 try
109 {
110 status = backend->fetch(hash.data(), &nodeObject);
111 }
112 catch (std::exception const& e)
113 {
114 JLOG(j_.fatal()) << "Exception, " << e.what();
115 Rethrow();
116 }
117
118 switch (status)
119 {
120 case ok:
121 case notFound:
122 break;
123 case dataCorrupt:
124 JLOG(j_.fatal()) << "Corrupt NodeObject #" << hash;
125 break;
126 default:
127 JLOG(j_.warn()) << "Unknown status=" << status;
128 break;
129 }
130
131 return nodeObject;
132 };
133
134 // See if the node object exists in the cache
136
137 auto [writable, archive] = [&] {
140 }();
141
142 // Try to fetch from the writable backend
143 nodeObject = fetch(writable);
144 if (!nodeObject)
145 {
146 // Otherwise try to fetch from the archive backend
147 nodeObject = fetch(archive);
148 if (nodeObject)
149 {
150 {
151 // Refresh the writable backend pointer
153 writable = writableBackend_;
154 }
155
156 // Update writable backend with data from the archive backend
157 if (duplicate)
158 writable->store(nodeObject);
159 }
160 }
161
162 if (nodeObject)
163 fetchReport.wasFound = true;
164
165 return nodeObject;
166}
167
168void
170{
171 auto [writable, archive] = [&] {
174 }();
175
176 // Iterate the writable backend
177 writable->for_each(f);
178
179 // Iterate the archive backend
180 archive->for_each(f);
181}
182
183} // namespace NodeStore
184} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:41
Stream fatal() const
Definition Journal.h:325
Stream warn() const
Definition Journal.h:313
static std::shared_ptr< NodeObject > createObject(NodeObjectType type, Blob &&data, uint256 const &hash)
Create an object from fields.
std::shared_ptr< Backend > archiveBackend_
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.
void for_each(std::function< void(std::shared_ptr< NodeObject >)> f) override
Visit every object in the database This is usually called during import.
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.
Persistency layer for NodeObject.
Definition Database.h:32
void storeStats(std::uint64_t count, std::uint64_t sz)
Definition Database.h:221
int fdRequired() const
Returns the number of file descriptors the database expects to need.
Definition Database.h:180
beast::Journal const j_
Definition Database.h:200
void importInternal(Backend &dstBackend, Database &srcDB)
Definition Database.cpp:164
Scheduling for asynchronous backend activity.
Holds a collection of configuration values.
Definition BasicConfig.h:25
pointer data()
Definition base_uint.h:102
T make_pair(T... args)
STL namespace.
Status
Return codes from Backend operations.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
NodeObjectType
The types of node objects.
Definition NodeObject.h:13
void Rethrow()
Rethrow the exception currently being handled.
Definition contract.h:29
Contains information about a fetch operation.
T what(T... args)