xrpld
Loading...
Searching...
No Matches
libxrpl/nodestore/Database.cpp
1#include <xrpl/nodestore/Database.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/basics/contract.h>
6#include <xrpl/beast/core/CurrentThreadName.h>
7#include <xrpl/beast/utility/Journal.h>
8#include <xrpl/beast/utility/instrumentation.h>
9#include <xrpl/config/BasicConfig.h>
10#include <xrpl/config/Constants.h>
11#include <xrpl/json/json_forwards.h>
12#include <xrpl/json/json_value.h>
13#include <xrpl/nodestore/Backend.h>
14#include <xrpl/nodestore/NodeObject.h>
15#include <xrpl/nodestore/Scheduler.h>
16#include <xrpl/nodestore/Types.h>
17#include <xrpl/protocol/SystemParameters.h>
18#include <xrpl/protocol/jss.h>
19
20#include <algorithm>
21#include <atomic>
22#include <chrono>
23#include <cstdint>
24#include <exception>
25#include <functional>
26#include <memory>
27#include <mutex>
28#include <stdexcept>
29#include <string>
30#include <thread>
31#include <utility>
32
33namespace xrpl::node_store {
34
36 Scheduler& scheduler,
37 int readThreads,
38 Section const& config,
39 beast::Journal journal)
40 : j_(journal)
41 , scheduler_(scheduler)
42 , earliestLedgerSeq_(get<std::uint32_t>(config, Keys::kEarliestSeq, kXrpLedgerEarliestSeq))
43 , requestBundle_(get<int>(config, Keys::kRqBundle, 4))
44 , readThreads_(std::max(1, readThreads))
45{
46 XRPL_ASSERT(readThreads, "xrpl::node_store::Database::Database : nonzero threads input");
47
48 if (earliestLedgerSeq_ < 1)
49 Throw<std::runtime_error>("Invalid earliest_seq");
50
52 Throw<std::runtime_error>("Invalid rq_bundle");
53
54 for (int i = readThreads_.load(); i != 0; --i)
55 {
57 [this](int i) {
59
60 beast::setCurrentThreadName("db prefetch #" + std::to_string(i));
61
62 decltype(read_) read;
63
64 while (true)
65 {
66 {
68
69 if (isStopping())
70 break;
71
72 if (read_.empty())
73 {
75 readCondVar_.wait(lock);
77 }
78
79 if (isStopping())
80 break;
81
82 // extract multiple object at a time to minimize the
83 // overhead of acquiring the mutex.
84 for (int cnt = 0; !read_.empty() && cnt != requestBundle_; ++cnt)
85 read.insert(read_.extract(read_.begin()));
86 }
87
88 for (auto it = read.begin(); it != read.end(); ++it)
89 {
90 XRPL_ASSERT(
91 !it->second.empty(),
92 "xrpl::node_store::Database::Database : non-empty "
93 "data");
94
95 auto const& hash = it->first;
96 auto const& data = it->second;
97 auto const seqn = data[0].first;
98
99 auto const obj = fetchNodeObject(hash, seqn, FetchType::Async);
100
101 // This could be further optimized: if there are
102 // multiple requests for sequence numbers mapping to
103 // multiple databases by sorting requests such that all
104 // indices mapping to the same database are grouped
105 // together and serviced by a single read.
106 for (auto const& req : data)
107 {
108 req.second(
109 (seqn == req.first) || isSameDB(req.first, seqn)
110 ? obj
111 : fetchNodeObject(hash, req.first, FetchType::Async));
112 }
113 }
114
115 read.clear();
116 }
117
119 --readThreads_;
120 },
121 i);
122 t.detach();
123 }
124}
125
127{
128 // NOTE!
129 // Any derived class should call the stop() method in its
130 // destructor. Otherwise, occasionally, the derived class may
131 // crash during shutdown when its members are accessed by one of
132 // these threads after the derived class is destroyed but before
133 // this base class is destroyed.
134 stop();
135}
136
137bool
139{
140 return readStopping_.load(std::memory_order_relaxed);
141}
142
153void
155{
156 decltype(read_) pending;
157 {
158 std::scoped_lock const lock(readLock_);
159
160 if (!readStopping_.exchange(true, std::memory_order_relaxed))
161 {
162 JLOG(j_.debug()) << "Clearing read queue because of stop request";
163 pending.swap(read_);
164 readCondVar_.notify_all();
165 }
166 }
167
168 for (auto const& [hash, requests] : pending)
169 {
170 for (auto const& request : requests)
171 request.second(nullptr);
172 }
173
174 JLOG(j_.debug()) << "Waiting for stop request to complete...";
175
176 using namespace std::chrono;
177
178 auto const start = steady_clock::now();
179
180 while (readThreads_.load() != 0)
181 {
182 XRPL_ASSERT(
183 steady_clock::now() - start < 30s,
184 "xrpl::node_store::Database::stop : maximum stop duration");
186 }
187
188 JLOG(j_.debug())
189 << "Stop request completed in "
191 << " milliseconds";
192}
193
203void
205 UInt256 const& hash,
206 std::uint32_t ledgerSeq,
208{
209 {
211
212 if (!isStopping())
213 {
214 read_[hash].emplace_back(ledgerSeq, std::move(cb));
215 readCondVar_.notify_one();
216 return;
217 }
218 }
219
220 // The queue is closed. Complete the read here, with the lock released.
221 cb(nullptr);
222}
223
224void
226{
227 Batch batch;
229 auto storeBatch = [&, fname = __func__] {
230 try
231 {
232 dstBackend.storeBatch(batch);
233 }
234 catch (std::exception const& e)
235 {
236 JLOG(j_.error()) << "Exception caught in function " << fname << ". Error: " << e.what();
237 return;
238 }
239
240 std::uint64_t sz{0};
241 for (auto const& nodeObject : batch)
242 sz += nodeObject->getData().size();
243 storeStats(batch.size(), sz);
244 batch.clear();
245 };
246
247 srcDB.forEach([&](std::shared_ptr<NodeObject> nodeObject) {
248 XRPL_ASSERT(nodeObject, "xrpl::node_store::Database::importInternal : non-null node");
249 if (!nodeObject) // This should never happen
250 return;
251
252 batch.emplace_back(std::move(nodeObject));
253 if (batch.size() >= kBatchWritePreallocationSize)
254 storeBatch();
255 });
256
257 if (!batch.empty())
258 storeBatch();
259}
260
261// Perform a fetch and report the time it took
264 UInt256 const& hash,
265 std::uint32_t ledgerSeq,
266 FetchType fetchType,
267 bool duplicate)
268{
269 FetchReport fetchReport(fetchType);
270
271 using namespace std::chrono;
272 auto const begin{steady_clock::now()};
273
274 auto nodeObject{fetchNodeObject(hash, ledgerSeq, fetchReport, duplicate)};
275 auto const dur = steady_clock::now() - begin;
277 if (nodeObject)
278 {
280 fetchSz_ += nodeObject->getData().size();
281 }
283
284 fetchReport.elapsed = duration_cast<milliseconds>(dur);
285 scheduler_.onFetch(fetchReport);
286 return nodeObject;
287}
288
289void
291{
292 XRPL_ASSERT(obj.isObject(), "xrpl::node_store::Database::getCountsJson : valid input type");
293
294 {
296 obj["read_queue"] = static_cast<json::UInt>(read_.size());
297 }
298
299 obj["read_threads_total"] = readThreads_.load();
300 obj["read_threads_running"] = runningThreads_.load();
301 obj["read_request_bundle"] = requestBundle_;
302
303 obj[jss::node_writes] = std::to_string(storeCount_);
304 obj[jss::node_reads_total] = std::to_string(fetchTotalCount_);
305 obj[jss::node_reads_hit] = std::to_string(fetchHitCount_);
306 obj[jss::node_written_bytes] = std::to_string(storeSz_);
307 obj[jss::node_read_bytes] = std::to_string(fetchSz_);
308 obj[jss::node_reads_duration_us] = std::to_string(fetchDurationUs_);
309}
310
311} // namespace xrpl::node_store
T begin(T... args)
A generic endpoint for log messages.
Definition Journal.h:44
Represents a JSON value.
Definition json_value.h:121
bool isObject() const
Holds a collection of configuration values.
Definition BasicConfig.h:28
A backend used for the NodeStore.
Definition Backend.h:29
virtual void storeBatch(Batch const &batch)=0
Store a group of objects.
std::atomic< std::uint64_t > storeSz_
Definition Database.h:273
std::atomic< int > readThreads_
Definition Database.h:289
virtual void asyncFetch(UInt256 const &hash, std::uint32_t ledgerSeq, std::function< void(std::shared_ptr< NodeObject > const &)> &&callback)
Fetch an object without waiting.
std::atomic< bool > readStopping_
Definition Database.h:288
std::atomic< std::uint64_t > fetchDurationUs_
Definition Database.h:275
std::atomic< std::uint64_t > storeCount_
Definition Database.h:272
virtual ~Database()
Destroy the node store.
std::uint32_t const earliestLedgerSeq_
Definition Database.h:244
std::atomic< int > runningThreads_
Definition Database.h:290
virtual void forEach(std::function< void(std::shared_ptr< NodeObject >)> f)=0
Visit every object in the database This is usually called during import.
void storeStats(std::uint64_t count, std::uint64_t sz)
Definition Database.h:252
void importInternal(Backend &dstBackend, Database &srcDB)
std::atomic< std::uint64_t > fetchTotalCount_
Definition Database.h:274
std::shared_ptr< NodeObject > fetchNodeObject(UInt256 const &hash, std::uint32_t ledgerSeq=0, FetchType fetchType=FetchType::Synchronous, bool duplicate=false)
Fetch a node object.
std::map< UInt256, std::vector< std::pair< std::uint32_t, std::function< void(std::shared_ptr< NodeObject > const &)> > > > read_
Definition Database.h:286
virtual bool isSameDB(std::uint32_t s1, std::uint32_t s2)=0
Check if two ledgers are in the same database.
beast::Journal const j_
Definition Database.h:231
std::condition_variable readCondVar_
Definition Database.h:279
std::atomic< std::uint32_t > fetchSz_
Definition Database.h:236
virtual void stop()
Close the read queue and wait for the read threads to exit.
std::atomic< std::uint32_t > fetchHitCount_
Definition Database.h:235
Scheduling for asynchronous backend activity.
T detach(T... args)
T duration_cast(T... args)
T emplace_back(T... args)
T empty(T... args)
T lock(T... args)
void setCurrentThreadName(std::string_view newThreadName)
Changes the name of the caller thread.
unsigned int UInt
STL namespace.
std::vector< std::shared_ptr< NodeObject > > Batch
A batch of NodeObjects to write at once.
static constexpr auto kBatchWritePreallocationSize
void read(nudb::detail::istream &is, std::size_t &u)
Definition Varint.h:106
void storeBatch(Backend &backend, Batch const &batch)
Definition TestBase.h:85
T get(Section const &section, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
BaseUInt< 256 > UInt256
Definition base_uint.h:581
static constexpr std::uint32_t kXrpLedgerEarliestSeq
The XRP ledger network's earliest allowed sequence.
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
T reserve(T... args)
T size(T... args)
Contains information about a fetch operation.
T to_string(T... args)
T what(T... args)
T yield(T... args)