xrpld
Loading...
Searching...
No Matches
tests/libxrpl/nodestore/Database.cpp
1#include <xrpl/nodestore/Database.h>
2
3#include <xrpl/basics/ByteUtilities.h>
4#include <xrpl/basics/FileUtilities.h>
5#include <xrpl/basics/scope.h>
6#include <xrpl/beast/utility/Journal.h>
7#include <xrpl/beast/xor_shift_engine.h>
8#include <xrpl/config/BasicConfig.h>
9#include <xrpl/nodestore/DummyScheduler.h>
10#include <xrpl/nodestore/Manager.h>
11#include <xrpl/nodestore/NodeObject.h>
12#include <xrpl/nodestore/Types.h>
13#include <xrpl/protocol/SystemParameters.h>
14
15#include <gtest/gtest.h>
16#include <helpers/TestSink.h>
17#include <nodestore/TestBase.h>
18
19#include <algorithm>
20#include <atomic>
21#include <chrono>
22#include <functional>
23#include <memory>
24#include <stdexcept>
25#include <string>
26#include <thread>
27#include <vector>
28
29namespace xrpl::node_store {
30
31namespace {
32
33std::vector<std::string>
34allBackends()
35{
36#if XRPL_ROCKSDB_AVAILABLE
37 return {"memory", "nudb", "rocksdb"};
38#else
39 return {"memory", "nudb"};
40#endif
41}
42
43std::vector<std::string>
44persistentBackends()
45{
46 std::vector<std::string> types{"nudb"};
47#if XRPL_ROCKSDB_AVAILABLE
48 types.emplace_back("rocksdb");
49#endif
50 return types;
51}
52
53std::vector<std::string>
54importBackends()
55{
56 std::vector<std::string> types{"nudb"};
57#if XRPL_ROCKSDB_AVAILABLE
58 types.emplace_back("rocksdb");
59#endif
60#ifdef XRPL_ENABLE_SQLITE_BACKEND_TESTS
61 types.emplace_back("sqlite");
62#endif
63 return types;
64}
65
66} // namespace
67
68// Shared setup for the parameterized Database tests: builds the node params,
69// journal and a predictable batch per test, mirroring Backend.cpp's fixture.
70class NodeStoreDatabaseTestBase : public ::testing::TestWithParam<std::string>
71{
72protected:
73 void
74 SetUp() override
75 {
76 nodeParams_.set("type", GetParam());
77 nodeParams_.set("path", nodeDb_.path());
78
81 }
82
88
94};
95
99
103
105{
106 auto db = makeDatabase();
107
108 storeBatch(*db, batch_);
109
110 {
111 SCOPED_TRACE("read in original order");
112 auto const copy = fetchCopyOfBatch(*db, batch_);
113 EXPECT_EQ(batch_, copy);
114 }
115
116 {
117 SCOPED_TRACE("read in shuffled order");
119 std::shuffle(batch_.begin(), batch_.end(), rng);
120 auto const copy = fetchCopyOfBatch(*db, batch_);
121 EXPECT_EQ(batch_, copy);
122 }
123}
124
126{
127 {
128 auto db = makeDatabase();
129 storeBatch(*db, batch_);
130 }
131
132 // re-open without the ephemeral db
133 auto db = makeDatabase();
134
135 auto copy = fetchCopyOfBatch(*db, batch_);
136 std::ranges::sort(batch_, LessThan{});
138 EXPECT_EQ(batch_, copy);
139}
140
141// missing-key path at the Database layer. Mirrors Backend's fetch_missing —
142// fetching keys that were never stored must return nullptr (NotFound).
144{
145 auto db = makeDatabase();
146
147 // never store: every key must be absent
148 fetchMissing(*db, batch_);
149}
150
152 NodeStoreBackends,
154 ::testing::ValuesIn(allBackends()),
155 [](::testing::TestParamInfo<std::string> const& info) { return info.param; });
156
158 PersistentBackends,
160 ::testing::ValuesIn(persistentBackends()),
161 [](::testing::TestParamInfo<std::string> const& info) { return info.param; });
162
163TEST(NodeStoreDatabase, memory_earliest_seq)
164{
165 DummyScheduler scheduler;
166 TempDir const nodeDb;
167 Section nodeParams;
168 nodeParams.set("type", "memory");
169 nodeParams.set("path", nodeDb.path());
170
171 beast::Journal const journal(TestSink::instance());
172
173 // default earliest ledger sequence
174 {
175 auto db = Manager::instance().makeDatabase(megabytes(4), scheduler, 2, nodeParams, journal);
176 EXPECT_EQ(db->earliestLedgerSeq(), kXrpLedgerEarliestSeq);
177 }
178
179 // invalid earliest_seq value
180 {
181 nodeParams.set("earliest_seq", "0");
182 try
183 {
184 auto const db =
185 Manager::instance().makeDatabase(megabytes(4), scheduler, 2, nodeParams, journal);
186 FAIL() << "expected runtime_error for earliest_seq=0";
187 }
188 catch (std::runtime_error const& e)
189 {
190 EXPECT_STREQ(e.what(), "Invalid earliest_seq");
191 }
192 }
193
194 // valid earliest_seq value
195 {
196 nodeParams.set("earliest_seq", "1");
197 auto db = Manager::instance().makeDatabase(megabytes(4), scheduler, 2, nodeParams, journal);
198 EXPECT_EQ(db->earliestLedgerSeq(), 1u);
199 }
200}
201
202namespace {
203
212[[nodiscard]] bool
213waitFor(
214 std::function<bool()> const& condition,
215 std::chrono::steady_clock::duration deadline = std::chrono::seconds{5})
216{
217 auto const giveUp = std::chrono::steady_clock::now() + deadline;
218 while (std::chrono::steady_clock::now() < giveUp)
219 {
220 if (condition())
221 return true;
223 }
224 return condition();
225}
226
227} // namespace
228
229// Every read posted to the database completes exactly once, including reads still queued when
230// stop() closes the queue and reads posted after it.
231TEST(NodeStoreDatabase, stop_completes_every_read_once)
232{
233 DummyScheduler scheduler;
234 TempDir const nodeDb;
235 Section nodeParams;
236 nodeParams.set("type", "memory");
237 nodeParams.set("path", nodeDb.path());
238 beast::Journal const journal(TestSink::instance());
239
240 // One read thread, so a callback that blocks parks the whole queue.
241 auto db = Manager::instance().makeDatabase(megabytes(4), scheduler, 1, nodeParams, journal);
242
243 struct Outcome
244 {
245 std::atomic<int> calls{0};
246 std::atomic<bool> null{false};
247 };
248 Outcome queued;
249 Outcome blocked;
250 Outcome late;
251 std::atomic<bool> entered{false};
252 std::atomic<bool> released{false};
253
254 // ASSERT_TRUE below returns early. Release the parked callback and join the read thread
255 // before the locals the callbacks capture go out of scope.
256 ScopeExit const finish{[&] {
257 released = true;
258 db->stop();
259 }};
260
261 // The first read parks the read thread inside its callback until the test releases it.
262 db->asyncFetch(UInt256{1}, 1, [&](std::shared_ptr<NodeObject> const& object) {
263 entered = true;
264 while (!released)
266 // The result is published before the count, so a reader that sees the count sees it.
267 blocked.null = !object;
268 ++blocked.calls;
269 });
270 ASSERT_TRUE(waitFor([&] { return entered.load(); }));
271
272 // With the thread parked, this read stays in the queue.
273 db->asyncFetch(UInt256{2}, 1, [&](std::shared_ptr<NodeObject> const& object) {
274 queued.null = !object;
275 ++queued.calls;
276 });
277
278 // stop() closes the queue and completes the queued read with no object, then waits for the
279 // read thread, which is still parked. So it runs on its own thread.
280 std::thread stopper{[&] { db->stop(); }};
281 EXPECT_TRUE(waitFor([&] { return queued.calls == 1; }));
282 EXPECT_TRUE(queued.null);
283
284 // A read posted after the queue closed completes at once, on the caller's thread.
285 db->asyncFetch(UInt256{3}, 1, [&](std::shared_ptr<NodeObject> const& object) {
286 late.null = !object;
287 ++late.calls;
288 });
289 EXPECT_EQ(late.calls, 1);
290 EXPECT_TRUE(late.null);
291
292 released = true;
293 stopper.join();
294
295 // The parked read completed on the read thread, and nothing ran twice.
296 EXPECT_EQ(blocked.calls, 1);
297 EXPECT_TRUE(blocked.null);
298 EXPECT_EQ(queued.calls, 1);
299 EXPECT_EQ(late.calls, 1);
300}
301
302class DatabaseImportTest : public ::testing::TestWithParam<std::string>
303{
304};
305
307{
308 auto const type = GetParam();
309
310 DummyScheduler scheduler;
311 beast::Journal const journal(TestSink::instance());
312
313 TempDir const srcDir;
314 Section srcParams;
315 srcParams.set("type", type);
316 srcParams.set("path", srcDir.path());
317
319
320 // write to source db
321 {
322 auto src = Manager::instance().makeDatabase(megabytes(4), scheduler, 2, srcParams, journal);
323 storeBatch(*src, batch);
324 }
325
326 Batch copy;
327 {
328 // re-open source and import into a fresh destination
329 auto src = Manager::instance().makeDatabase(megabytes(4), scheduler, 2, srcParams, journal);
330
331 TempDir const destDir;
332 Section destParams;
333 destParams.set("type", type);
334 destParams.set("path", destDir.path());
335
336 auto dest =
337 Manager::instance().makeDatabase(megabytes(4), scheduler, 2, destParams, journal);
338
339 dest->importDatabase(*src);
340 copy = fetchCopyOfBatch(*dest, batch);
341 }
342
343 std::ranges::sort(batch, LessThan{});
345 EXPECT_EQ(batch, copy);
346}
347
349 ImportBackends,
351 ::testing::ValuesIn(importBackends()),
352 [](::testing::TestParamInfo<std::string> const& info) { return info.param; });
353
354} // namespace xrpl::node_store
A generic endpoint for log messages.
Definition Journal.h:44
Holds a collection of configuration values.
Definition BasicConfig.h:28
void set(std::string const &key, std::string const &value)
Set a key/value pair.
RAII temporary directory.
std::string path() const
Get the native path for the temporary directory.
static TestSink & instance()
Definition TestSink.h:12
Simple NodeStore Scheduler that just performs the tasks synchronously.
virtual std::unique_ptr< Database > makeDatabase(std::size_t burstSize, Scheduler &scheduler, int readThreads, Section const &backendParameters, beast::Journal journal)=0
Construct a NodeStore database.
static Manager & instance()
Returns the instance of the manager singleton.
T emplace_back(T... args)
T join(T... args)
T load(T... args)
detail::XorShiftEngine<> XorShiftEngine
XOR-shift Generator.
constexpr std::uint64_t kSeedValue
Definition TestBase.h:28
TEST_P(BackendTypeTest, store_and_fetch)
INSTANTIATE_TEST_SUITE_P(BackendTypes, BackendTypeTest, ::testing::ValuesIn(backendTypes()), [](::testing::TestParamInfo< std::string > const &info) { return info.param;})
TEST(NodeStoreBasics, predictable_batches)
Definition Basics.cpp:14
std::vector< std::shared_ptr< NodeObject > > Batch
A batch of NodeObjects to write at once.
Batch createPredictableBatch(std::size_t numObjects, std::uint64_t seed)
Definition TestBase.h:48
constexpr int kNumObjects
Definition TestBase.h:27
void storeBatch(Backend &backend, Batch const &batch)
Definition TestBase.h:85
void fetchMissing(Backend &backend, Batch const &batch)
Definition TestBase.h:113
Batch fetchCopyOfBatch(Backend &backend, Batch const &batch)
Definition TestBase.h:92
BaseUInt< 256 > UInt256
Definition base_uint.h:581
constexpr auto megabytes(T value) noexcept
static constexpr std::uint32_t kXrpLedgerEarliestSeq
The XRP ledger network's earliest allowed sequence.
T shuffle(T... args)
T sleep_for(T... args)
T sort(T... args)
T what(T... args)