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#include <xrpl/protocol/HashPrefix.h>
22
23namespace ripple {
24namespace 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(
47 std::string const& writableName,
48 std::string const& archiveName)> const& f)
49{
50 // Pass these two names to the callback function
51 std::string const newWritableBackendName = newBackend->getName();
52 std::string newArchiveBackendName;
53 // Hold on to current archive backend pointer until after the
54 // callback finishes. Only then will the archive directory be
55 // deleted.
57 {
59
60 archiveBackend_->setDeletePath();
61 oldArchiveBackend = std::move(archiveBackend_);
62
64 newArchiveBackendName = archiveBackend_->getName();
65
66 writableBackend_ = std::move(newBackend);
67 }
68
69 f(newWritableBackendName, newArchiveBackendName);
70}
71
74{
76 return writableBackend_->getName();
77}
78
81{
83 return writableBackend_->getWriteLoad();
84}
85
86void
88{
89 auto const backend = [&] {
91 return writableBackend_;
92 }();
93
94 importInternal(*backend, source);
95}
96
97void
99{
101 writableBackend_->sync();
102}
103
104void
106 NodeObjectType type,
107 Blob&& data,
108 uint256 const& hash,
110{
111 auto nObj = NodeObject::createObject(type, std::move(data), hash);
112
113 auto const backend = [&] {
115 return writableBackend_;
116 }();
117
118 backend->store(nObj);
119 storeStats(1, nObj->getData().size());
120}
121
122void
124{
125 // nothing to do
126}
127
130 uint256 const& hash,
132 FetchReport& fetchReport,
133 bool duplicate)
134{
135 auto fetch = [&](std::shared_ptr<Backend> const& backend) {
136 Status status;
138 try
139 {
140 status = backend->fetch(hash.data(), &nodeObject);
141 }
142 catch (std::exception const& e)
143 {
144 JLOG(j_.fatal()) << "Exception, " << e.what();
145 Rethrow();
146 }
147
148 switch (status)
149 {
150 case ok:
151 case notFound:
152 break;
153 case dataCorrupt:
154 JLOG(j_.fatal()) << "Corrupt NodeObject #" << hash;
155 break;
156 default:
157 JLOG(j_.warn()) << "Unknown status=" << status;
158 break;
159 }
160
161 return nodeObject;
162 };
163
164 // See if the node object exists in the cache
166
167 auto [writable, archive] = [&] {
170 }();
171
172 // Try to fetch from the writable backend
173 nodeObject = fetch(writable);
174 if (!nodeObject)
175 {
176 // Otherwise try to fetch from the archive backend
177 nodeObject = fetch(archive);
178 if (nodeObject)
179 {
180 {
181 // Refresh the writable backend pointer
183 writable = writableBackend_;
184 }
185
186 // Update writable backend with data from the archive backend
187 if (duplicate)
188 writable->store(nodeObject);
189 }
190 }
191
192 if (nodeObject)
193 fetchReport.wasFound = true;
194
195 return nodeObject;
196}
197
198void
201{
202 auto [writable, archive] = [&] {
205 }();
206
207 // Iterate the writable backend
208 writable->for_each(f);
209
210 // Iterate the archive backend
211 archive->for_each(f);
212}
213
214} // namespace NodeStore
215} // namespace ripple
A generic endpoint for log messages.
Definition: Journal.h:59
Stream fatal() const
Definition: Journal.h:351
Stream warn() const
Definition: Journal.h:339
static std::shared_ptr< NodeObject > createObject(NodeObjectType type, Blob &&data, uint256 const &hash)
Create an object from fields.
Definition: NodeObject.cpp:37
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:50
void storeStats(std::uint64_t count, std::uint64_t sz)
Definition: Database.h:247
beast::Journal const j_
Definition: Database.h:226
void importInternal(Backend &dstBackend, Database &srcDB)
Definition: Database.cpp:198
Scheduling for asynchronous backend activity.
Holds a collection of configuration values.
Definition: BasicConfig.h:46
pointer data()
Definition: base_uint.h:124
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)