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::string const& writableBackendName)> const& f)
47{
49
50 auto newBackend = f(writableBackend_->getName());
51 archiveBackend_->setDeletePath();
53 writableBackend_ = std::move(newBackend);
54}
55
58{
60 return writableBackend_->getName();
61}
62
65{
67 return writableBackend_->getWriteLoad();
68}
69
70void
72{
73 auto const backend = [&] {
75 return writableBackend_;
76 }();
77
78 importInternal(*backend, source);
79}
80
81void
83{
85 writableBackend_->sync();
86}
87
88void
90 NodeObjectType type,
91 Blob&& data,
92 uint256 const& hash,
94{
95 auto nObj = NodeObject::createObject(type, std::move(data), hash);
96
97 auto const backend = [&] {
99 return writableBackend_;
100 }();
101
102 backend->store(nObj);
103 storeStats(1, nObj->getData().size());
104}
105
106void
108{
109 // nothing to do
110}
111
114 uint256 const& hash,
116 FetchReport& fetchReport,
117 bool duplicate)
118{
119 auto fetch = [&](std::shared_ptr<Backend> const& backend) {
120 Status status;
122 try
123 {
124 status = backend->fetch(hash.data(), &nodeObject);
125 }
126 catch (std::exception const& e)
127 {
128 JLOG(j_.fatal()) << "Exception, " << e.what();
129 Rethrow();
130 }
131
132 switch (status)
133 {
134 case ok:
135 case notFound:
136 break;
137 case dataCorrupt:
138 JLOG(j_.fatal()) << "Corrupt NodeObject #" << hash;
139 break;
140 default:
141 JLOG(j_.warn()) << "Unknown status=" << status;
142 break;
143 }
144
145 return nodeObject;
146 };
147
148 // See if the node object exists in the cache
150
151 auto [writable, archive] = [&] {
154 }();
155
156 // Try to fetch from the writable backend
157 nodeObject = fetch(writable);
158 if (!nodeObject)
159 {
160 // Otherwise try to fetch from the archive backend
161 nodeObject = fetch(archive);
162 if (nodeObject)
163 {
164 {
165 // Refresh the writable backend pointer
167 writable = writableBackend_;
168 }
169
170 // Update writable backend with data from the archive backend
171 if (duplicate)
172 writable->store(nodeObject);
173 }
174 }
175
176 if (nodeObject)
177 fetchReport.wasFound = true;
178
179 return nodeObject;
180}
181
182void
185{
186 auto [writable, archive] = [&] {
189 }();
190
191 // Iterate the writable backend
192 writable->for_each(f);
193
194 // Iterate the archive backend
195 archive->for_each(f);
196}
197
198} // namespace NodeStore
199} // namespace ripple
A generic endpoint for log messages.
Definition: Journal.h:59
Stream fatal() const
Definition: Journal.h:341
Stream warn() const
Definition: Journal.h:329
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 rotateWithLock(std::function< std::unique_ptr< NodeStore::Backend >(std::string const &writableBackendName)> const &f) override
Rotates the backends.
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 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)