rippled
Loading...
Searching...
No Matches
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 <xrpld/nodestore/detail/DatabaseRotatingImp.h>
21
22namespace ripple {
23namespace NodeStore {
24
26 Scheduler& scheduler,
27 int readThreads,
28 std::shared_ptr<Backend> writableBackend,
29 std::shared_ptr<Backend> archiveBackend,
30 Section const& config,
32 : DatabaseRotating(scheduler, readThreads, config, j)
33 , writableBackend_(std::move(writableBackend))
34 , archiveBackend_(std::move(archiveBackend))
35{
37 fdRequired_ += writableBackend_->fdRequired();
39 fdRequired_ += archiveBackend_->fdRequired();
40}
41
42void
45 std::function<void(
46 std::string const& writableName,
47 std::string const& archiveName)> const& f)
48{
49 // Pass these two names to the callback function
50 std::string const newWritableBackendName = newBackend->getName();
51 std::string newArchiveBackendName;
52 // Hold on to current archive backend pointer until after the
53 // callback finishes. Only then will the archive directory be
54 // deleted.
56 {
58
59 archiveBackend_->setDeletePath();
60 oldArchiveBackend = std::move(archiveBackend_);
61
63 newArchiveBackendName = archiveBackend_->getName();
64
65 writableBackend_ = std::move(newBackend);
66 }
67
68 f(newWritableBackendName, newArchiveBackendName);
69}
70
73{
75 return writableBackend_->getName();
76}
77
80{
82 return writableBackend_->getWriteLoad();
83}
84
85void
87{
88 auto const backend = [&] {
90 return writableBackend_;
91 }();
92
93 importInternal(*backend, source);
94}
95
96void
98{
100 writableBackend_->sync();
101}
102
103void
105 NodeObjectType type,
106 Blob&& data,
107 uint256 const& hash,
109{
110 auto nObj = NodeObject::createObject(type, std::move(data), hash);
111
112 auto const backend = [&] {
114 return writableBackend_;
115 }();
116
117 backend->store(nObj);
118 storeStats(1, nObj->getData().size());
119}
120
121void
123{
124 // nothing to do
125}
126
129 uint256 const& hash,
131 FetchReport& fetchReport,
132 bool duplicate)
133{
134 auto fetch = [&](std::shared_ptr<Backend> const& backend) {
135 Status status;
137 try
138 {
139 status = backend->fetch(hash.data(), &nodeObject);
140 }
141 catch (std::exception const& e)
142 {
143 JLOG(j_.fatal()) << "Exception, " << e.what();
144 Rethrow();
145 }
146
147 switch (status)
148 {
149 case ok:
150 case notFound:
151 break;
152 case dataCorrupt:
153 JLOG(j_.fatal()) << "Corrupt NodeObject #" << hash;
154 break;
155 default:
156 JLOG(j_.warn()) << "Unknown status=" << status;
157 break;
158 }
159
160 return nodeObject;
161 };
162
163 // See if the node object exists in the cache
165
166 auto [writable, archive] = [&] {
169 }();
170
171 // Try to fetch from the writable backend
172 nodeObject = fetch(writable);
173 if (!nodeObject)
174 {
175 // Otherwise try to fetch from the archive backend
176 nodeObject = fetch(archive);
177 if (nodeObject)
178 {
179 {
180 // Refresh the writable backend pointer
182 writable = writableBackend_;
183 }
184
185 // Update writable backend with data from the archive backend
186 if (duplicate)
187 writable->store(nodeObject);
188 }
189 }
190
191 if (nodeObject)
192 fetchReport.wasFound = true;
193
194 return nodeObject;
195}
196
197void
200{
201 auto [writable, archive] = [&] {
204 }();
205
206 // Iterate the writable backend
207 writable->for_each(f);
208
209 // Iterate the archive backend
210 archive->for_each(f);
211}
212
213} // namespace NodeStore
214} // namespace ripple
A generic endpoint for log messages.
Definition: Journal.h:60
Stream fatal() const
Definition: Journal.h:352
Stream warn() const
Definition: Journal.h:340
static std::shared_ptr< NodeObject > createObject(NodeObjectType type, Blob &&data, uint256 const &hash)
Create an object from fields.
Definition: NodeObject.cpp:38
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:52
void storeStats(std::uint64_t count, std::uint64_t sz)
Definition: Database.h:249
beast::Journal const j_
Definition: Database.h:228
void importInternal(Backend &dstBackend, Database &srcDB)
Definition: Database.cpp:200
Scheduling for asynchronous backend activity.
Holds a collection of configuration values.
Definition: BasicConfig.h:45
pointer data()
Definition: base_uint.h:125
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:26
NodeObjectType
The types of node objects.
Definition: NodeObject.h:32
void Rethrow()
Rethrow the exception currently being handled.
Definition: contract.h:48
STL namespace.
Contains information about a fetch operation.
T what(T... args)