xrpld
Loading...
Searching...
No Matches
Database.h
1#pragma once
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/TaggedCache.ipp>
5#include <xrpl/nodestore/Backend.h>
6#include <xrpl/nodestore/NodeObject.h>
7#include <xrpl/nodestore/Scheduler.h>
8#include <xrpl/protocol/SystemParameters.h>
9
10#include <condition_variable>
11
12namespace xrpl {
13class Section;
14} // namespace xrpl
15
16namespace xrpl::NodeStore {
17
32{
33public:
34 Database() = delete;
35
43 Database(Scheduler& scheduler, int readThreads, Section const& config, beast::Journal j);
44
49 virtual ~Database();
50
55 virtual std::string
56 getName() const = 0;
57
59 virtual void
61
65 virtual std::int32_t
66 getWriteLoad() const = 0;
67
80 virtual void
81 store(NodeObjectType type, Blob&& data, uint256 const& hash, std::uint32_t ledgerSeq) = 0;
82
83 /* Check if two ledgers are in the same database
84
85 If these two sequence numbers map to the same database,
86 the result of a fetch with either sequence number would
87 be identical.
88
89 @param s1 The first sequence number
90 @param s2 The second sequence number
91
92 @return 'true' if both ledgers would be in the same DB
93
94 */
95 virtual bool
97
98 virtual void
99 sync() = 0;
100
114 uint256 const& hash,
115 std::uint32_t ledgerSeq = 0,
117 bool duplicate = false);
118
131 virtual void
133 uint256 const& hash,
134 std::uint32_t ledgerSeq,
135 std::function<void(std::shared_ptr<NodeObject> const&)>&& callback);
136
138 virtual void
139 sweep() = 0;
140
147 {
148 return storeCount_;
149 }
150
153 {
154 return fetchTotalCount_;
155 }
156
159 {
160 return fetchHitCount_;
161 }
162
165 {
166 return storeSz_;
167 }
168
171 {
172 return fetchSz_;
173 }
174
175 void
177
179 int
181 {
182 return fdRequired_;
183 }
184
185 virtual void
186 stop();
187
188 bool
189 isStopping() const;
190
193 [[nodiscard]] std::uint32_t
194 earliestLedgerSeq() const noexcept
195 {
196 return earliestLedgerSeq_;
197 }
198
199protected:
203
206
207 // The default is XRP_LEDGER_EARLIEST_SEQ (32570) to match the XRP ledger
208 // network's earliest allowed ledger sequence. Can be set through the
209 // configuration file using the 'earliest_seq' field under the 'node_db'
210 // stanza. If specified, the value must be greater than zero.
211 // Only unit tests or alternate
212 // networks should change this value.
214
215 // The maximum number of requests a thread extracts from the queue in an
216 // attempt to minimize the overhead of mutex acquisition. This is an
217 // advanced tunable, via the config file. The default value is 4.
218 int const requestBundle_;
219
220 void
222 {
223 XRPL_ASSERT(count <= sz, "xrpl::NodeStore::Database::storeStats : valid inputs");
224 storeCount_ += count;
225 storeSz_ += sz;
226 }
227
228 // Called by the public import function
229 void
230 importInternal(Backend& dstBackend, Database& srcDB);
231
232 void
233 updateFetchMetrics(uint64_t fetches, uint64_t hits, uint64_t duration)
234 {
235 fetchTotalCount_ += fetches;
236 fetchHitCount_ += hits;
237 fetchDurationUs_ += duration;
238 }
239
240private:
246
249
250 // reads to do
251 std::map<
252 uint256,
256
260
263 uint256 const& hash,
264 std::uint32_t ledgerSeq,
265 FetchReport& fetchReport,
266 bool duplicate) = 0;
267
275 virtual void
277
278 void
280};
281
282} // namespace xrpl::NodeStore
A generic endpoint for log messages.
Definition Journal.h:38
Represents a JSON value.
Definition json_value.h:130
A backend used for the NodeStore.
Definition Backend.h:19
Persistency layer for NodeObject.
Definition Database.h:32
std::uint32_t getFetchHitCount() const
Definition Database.h:158
virtual void sweep()=0
Remove expired entries from the positive and negative caches.
std::uint32_t earliestLedgerSeq() const noexcept
Definition Database.h:194
virtual void importDatabase(Database &source)=0
Import objects from another database.
void storeStats(std::uint64_t count, std::uint64_t sz)
Definition Database.h:221
std::condition_variable readCondVar_
Definition Database.h:248
std::atomic< std::uint64_t > fetchTotalCount_
Definition Database.h:243
std::atomic< int > readThreads_
Definition Database.h:258
void updateFetchMetrics(uint64_t fetches, uint64_t hits, uint64_t duration)
Definition Database.h:233
std::atomic< std::uint64_t > storeDurationUs_
Definition Database.h:245
int fdRequired() const
Returns the number of file descriptors the database expects to need.
Definition Database.h:180
std::atomic< int > runningThreads_
Definition Database.h:259
beast::Journal const j_
Definition Database.h:200
std::map< uint256, std::vector< std::pair< std::uint32_t, std::function< void(std::shared_ptr< NodeObject > const &)> > > > read_
Definition Database.h:255
std::atomic< bool > readStopping_
Definition Database.h:257
virtual void asyncFetch(uint256 const &hash, std::uint32_t ledgerSeq, std::function< void(std::shared_ptr< NodeObject > const &)> &&callback)
Fetch an object without waiting.
Definition Database.cpp:178
std::atomic< std::uint64_t > fetchDurationUs_
Definition Database.h:244
virtual bool isSameDB(std::uint32_t s1, std::uint32_t s2)=0
virtual ~Database()
Destroy the node store.
Definition Database.cpp:126
std::shared_ptr< NodeObject > fetchNodeObject(uint256 const &hash, std::uint32_t ledgerSeq=0, FetchType fetchType=FetchType::Synchronous, bool duplicate=false)
Fetch a node object.
Definition Database.cpp:231
virtual void sync()=0
std::atomic< std::uint64_t > storeSz_
Definition Database.h:242
std::uint32_t const earliestLedgerSeq_
Definition Database.h:213
std::atomic< std::uint32_t > fetchHitCount_
Definition Database.h:204
std::atomic< std::uint64_t > storeCount_
Definition Database.h:241
void getCountsJson(json::Value &obj)
Definition Database.cpp:258
virtual std::string getName() const =0
Retrieve the name associated with this backend.
virtual void forEach(std::function< void(std::shared_ptr< NodeObject >)> f)=0
Visit every object in the database This is usually called during import.
std::uint64_t getStoreCount() const
Gather statistics pertaining to read and write activities.
Definition Database.h:146
std::uint64_t getStoreSize() const
Definition Database.h:164
virtual void store(NodeObjectType type, Blob &&data, uint256 const &hash, std::uint32_t ledgerSeq)=0
Store the object.
virtual std::shared_ptr< NodeObject > fetchNodeObject(uint256 const &hash, std::uint32_t ledgerSeq, FetchReport &fetchReport, bool duplicate)=0
std::uint32_t getFetchTotalCount() const
Definition Database.h:152
std::uint32_t getFetchSize() const
Definition Database.h:170
virtual std::int32_t getWriteLoad() const =0
Retrieve the estimated number of pending write operations.
std::atomic< std::uint32_t > fetchSz_
Definition Database.h:205
void importInternal(Backend &dstBackend, Database &srcDB)
Definition Database.cpp:193
Scheduling for asynchronous backend activity.
Holds a collection of configuration values.
Definition BasicConfig.h:24
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
NodeObjectType
The types of node objects.
Definition NodeObject.h:12
std::vector< unsigned char > Blob
Storage for linear binary data.
Definition Blob.h:10
BaseUInt< 256 > uint256
Definition base_uint.h:562
Contains information about a fetch operation.