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
97DatabaseRotatingImp::fetchNodeObject(uint256 const& hash, std::uint32_t, FetchReport& fetchReport, bool duplicate)
98{
99 auto fetch = [&](std::shared_ptr<Backend> const& backend) {
100 Status status;
102 try
103 {
104 status = backend->fetch(hash.data(), &nodeObject);
105 }
106 catch (std::exception const& e)
107 {
108 JLOG(j_.fatal()) << "Exception, " << e.what();
109 Rethrow();
110 }
111
112 switch (status)
113 {
114 case ok:
115 case notFound:
116 break;
117 case dataCorrupt:
118 JLOG(j_.fatal()) << "Corrupt NodeObject #" << hash;
119 break;
120 default:
121 JLOG(j_.warn()) << "Unknown status=" << status;
122 break;
123 }
124
125 return nodeObject;
126 };
127
128 // See if the node object exists in the cache
130
131 auto [writable, archive] = [&] {
134 }();
135
136 // Try to fetch from the writable backend
137 nodeObject = fetch(writable);
138 if (!nodeObject)
139 {
140 // Otherwise try to fetch from the archive backend
141 nodeObject = fetch(archive);
142 if (nodeObject)
143 {
144 {
145 // Refresh the writable backend pointer
147 writable = writableBackend_;
148 }
149
150 // Update writable backend with data from the archive backend
151 if (duplicate)
152 writable->store(nodeObject);
153 }
154 }
155
156 if (nodeObject)
157 fetchReport.wasFound = true;
158
159 return nodeObject;
160}
161
162void
164{
165 auto [writable, archive] = [&] {
168 }();
169
170 // Iterate the writable backend
171 writable->for_each(f);
172
173 // Iterate the archive backend
174 archive->for_each(f);
175}
176
177} // namespace NodeStore
178} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:40
Stream fatal() const
Definition Journal.h:324
Stream warn() const
Definition Journal.h:312
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.
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:31
void storeStats(std::uint64_t count, std::uint64_t sz)
Definition Database.h:216
int fdRequired() const
Returns the number of file descriptors the database expects to need.
Definition Database.h:175
beast::Journal const j_
Definition Database.h:195
void importInternal(Backend &dstBackend, Database &srcDB)
Definition Database.cpp:164
Scheduling for asynchronous backend activity.
Holds a collection of configuration values.
Definition BasicConfig.h:24
pointer data()
Definition base_uint.h:101
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:5
NodeObjectType
The types of node objects.
Definition NodeObject.h:12
void Rethrow()
Rethrow the exception currently being handled.
Definition contract.h:28
Contains information about a fetch operation.
T what(T... args)