rippled
Loading...
Searching...
No Matches
DatabaseRotatingImp.cpp
1#include <xrpl/nodestore/detail/DatabaseRotatingImp.h>
2
3namespace ripple {
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(
27 std::string const& writableName,
28 std::string const& archiveName)> const& f)
29{
30 // Pass these two names to the callback function
31 std::string const newWritableBackendName = newBackend->getName();
32 std::string newArchiveBackendName;
33 // Hold on to current archive backend pointer until after the
34 // callback finishes. Only then will the archive directory be
35 // deleted.
37 {
39
40 archiveBackend_->setDeletePath();
41 oldArchiveBackend = std::move(archiveBackend_);
42
44 newArchiveBackendName = archiveBackend_->getName();
45
46 writableBackend_ = std::move(newBackend);
47 }
48
49 f(newWritableBackendName, newArchiveBackendName);
50}
51
54{
56 return writableBackend_->getName();
57}
58
61{
63 return writableBackend_->getWriteLoad();
64}
65
66void
68{
69 auto const backend = [&] {
71 return writableBackend_;
72 }();
73
74 importInternal(*backend, source);
75}
76
77void
83
84void
86 NodeObjectType type,
87 Blob&& data,
88 uint256 const& hash,
90{
91 auto nObj = NodeObject::createObject(type, std::move(data), hash);
92
93 auto const backend = [&] {
95 return writableBackend_;
96 }();
97
98 backend->store(nObj);
99 storeStats(1, nObj->getData().size());
100}
101
102void
104{
105 // nothing to do
106}
107
110 uint256 const& hash,
112 FetchReport& fetchReport,
113 bool duplicate)
114{
115 auto fetch = [&](std::shared_ptr<Backend> const& backend) {
116 Status status;
118 try
119 {
120 status = backend->fetch(hash.data(), &nodeObject);
121 }
122 catch (std::exception const& e)
123 {
124 JLOG(j_.fatal()) << "Exception, " << e.what();
125 Rethrow();
126 }
127
128 switch (status)
129 {
130 case ok:
131 case notFound:
132 break;
133 case dataCorrupt:
134 JLOG(j_.fatal()) << "Corrupt NodeObject #" << hash;
135 break;
136 default:
137 JLOG(j_.warn()) << "Unknown status=" << status;
138 break;
139 }
140
141 return nodeObject;
142 };
143
144 // See if the node object exists in the cache
146
147 auto [writable, archive] = [&] {
150 }();
151
152 // Try to fetch from the writable backend
153 nodeObject = fetch(writable);
154 if (!nodeObject)
155 {
156 // Otherwise try to fetch from the archive backend
157 nodeObject = fetch(archive);
158 if (nodeObject)
159 {
160 {
161 // Refresh the writable backend pointer
163 writable = writableBackend_;
164 }
165
166 // Update writable backend with data from the archive backend
167 if (duplicate)
168 writable->store(nodeObject);
169 }
170 }
171
172 if (nodeObject)
173 fetchReport.wasFound = true;
174
175 return nodeObject;
176}
177
178void
181{
182 auto [writable, archive] = [&] {
185 }();
186
187 // Iterate the writable backend
188 writable->for_each(f);
189
190 // Iterate the archive backend
191 archive->for_each(f);
192}
193
194} // namespace NodeStore
195} // namespace ripple
A generic endpoint for log messages.
Definition Journal.h:41
Stream fatal() const
Definition Journal.h:333
Stream warn() const
Definition Journal.h:321
static std::shared_ptr< NodeObject > createObject(NodeObjectType type, Blob &&data, uint256 const &hash)
Create an object from fields.
std::int32_t getWriteLoad() const override
Retrieve the estimated number of pending write operations.
std::shared_ptr< Backend > archiveBackend_
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.
void importDatabase(Database &source) override
Import objects from another database.
std::string getName() const override
Retrieve the name associated with this backend.
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.
void for_each(std::function< void(std::shared_ptr< NodeObject >)> f) override
Visit every object in the database This is usually called during import.
Persistency layer for NodeObject.
Definition Database.h:32
void storeStats(std::uint64_t count, std::uint64_t sz)
Definition Database.h:229
beast::Journal const j_
Definition Database.h:208
int fdRequired() const
Returns the number of file descriptors the database expects to need.
Definition Database.h:188
void importInternal(Backend &dstBackend, Database &srcDB)
Definition Database.cpp:180
Scheduling for asynchronous backend activity.
Holds a collection of configuration values.
Definition BasicConfig.h:26
T make_pair(T... args)
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
STL namespace.
Contains information about a fetch operation.
T what(T... args)