rippled
Loading...
Searching...
No Matches
Database.h
1#pragma once
2
3#include <xrpl/basics/BasicConfig.h>
4#include <xrpl/basics/Log.h>
5#include <xrpl/basics/TaggedCache.ipp>
6#include <xrpl/nodestore/Backend.h>
7#include <xrpl/nodestore/NodeObject.h>
8#include <xrpl/nodestore/Scheduler.h>
9#include <xrpl/protocol/SystemParameters.h>
10
11#include <condition_variable>
12
13namespace xrpl {
14
15namespace NodeStore {
16
31{
32public:
33 Database() = delete;
34
42 Database(Scheduler& scheduler, int readThreads, Section const& config, beast::Journal j);
43
48 virtual ~Database();
49
54 virtual std::string
55 getName() const = 0;
56
58 virtual void
60
64 virtual std::int32_t
65 getWriteLoad() const = 0;
66
79 virtual void
80 store(NodeObjectType type, Blob&& data, uint256 const& hash, std::uint32_t ledgerSeq) = 0;
81
82 /* Check if two ledgers are in the same database
83
84 If these two sequence numbers map to the same database,
85 the result of a fetch with either sequence number would
86 be identical.
87
88 @param s1 The first sequence number
89 @param s2 The second sequence number
90
91 @return 'true' if both ledgers would be in the same DB
92
93 */
94 virtual bool
96
97 virtual void
98 sync() = 0;
99
113 uint256 const& hash,
114 std::uint32_t ledgerSeq = 0,
116 bool duplicate = false);
117
130 virtual void
132 uint256 const& hash,
133 std::uint32_t ledgerSeq,
134 std::function<void(std::shared_ptr<NodeObject> const&)>&& callback);
135
137 virtual void
138 sweep() = 0;
139
146 {
147 return storeCount_;
148 }
149
152 {
153 return fetchTotalCount_;
154 }
155
158 {
159 return fetchHitCount_;
160 }
161
164 {
165 return storeSz_;
166 }
167
170 {
171 return fetchSz_;
172 }
173
174 void
176
178 int
180 {
181 return fdRequired_;
182 }
183
184 virtual void
185 stop();
186
187 bool
188 isStopping() const;
189
192 [[nodiscard]] std::uint32_t
193 earliestLedgerSeq() const noexcept
194 {
195 return earliestLedgerSeq_;
196 }
197
198protected:
202
205
206 // The default is XRP_LEDGER_EARLIEST_SEQ (32570) to match the XRP ledger
207 // network's earliest allowed ledger sequence. Can be set through the
208 // configuration file using the 'earliest_seq' field under the 'node_db'
209 // stanza. If specified, the value must be greater than zero.
210 // Only unit tests or alternate
211 // networks should change this value.
213
214 // The maximum number of requests a thread extracts from the queue in an
215 // attempt to minimize the overhead of mutex acquisition. This is an
216 // advanced tunable, via the config file. The default value is 4.
217 int const requestBundle_;
218
219 void
221 {
222 XRPL_ASSERT(count <= sz, "xrpl::NodeStore::Database::storeStats : valid inputs");
223 storeCount_ += count;
224 storeSz_ += sz;
225 }
226
227 // Called by the public import function
228 void
229 importInternal(Backend& dstBackend, Database& srcDB);
230
231 void
232 updateFetchMetrics(uint64_t fetches, uint64_t hits, uint64_t duration)
233 {
234 fetchTotalCount_ += fetches;
235 fetchHitCount_ += hits;
236 fetchDurationUs_ += duration;
237 }
238
239private:
245
248
249 // reads to do
252
256
258 fetchNodeObject(uint256 const& hash, std::uint32_t ledgerSeq, FetchReport& fetchReport, bool duplicate) = 0;
259
267 virtual void
269
270 void
272};
273
274} // namespace NodeStore
275} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
A generic endpoint for log messages.
Definition Journal.h:40
A backend used for the NodeStore.
Definition Backend.h:20
Persistency layer for NodeObject.
Definition Database.h:31
std::uint32_t getFetchHitCount() const
Definition Database.h:157
virtual void sweep()=0
Remove expired entries from the positive and negative caches.
std::uint32_t earliestLedgerSeq() const noexcept
Definition Database.h:193
virtual void importDatabase(Database &source)=0
Import objects from another database.
void storeStats(std::uint64_t count, std::uint64_t sz)
Definition Database.h:220
std::condition_variable readCondVar_
Definition Database.h:247
std::atomic< std::uint64_t > fetchTotalCount_
Definition Database.h:242
std::atomic< int > readThreads_
Definition Database.h:254
void updateFetchMetrics(uint64_t fetches, uint64_t hits, uint64_t duration)
Definition Database.h:232
std::atomic< std::uint64_t > storeDurationUs_
Definition Database.h:244
int fdRequired() const
Returns the number of file descriptors the database expects to need.
Definition Database.h:179
std::atomic< int > runningThreads_
Definition Database.h:255
beast::Journal const j_
Definition Database.h:199
virtual void for_each(std::function< void(std::shared_ptr< NodeObject >)> f)=0
Visit every object in the database This is usually called during import.
std::atomic< bool > readStopping_
Definition Database.h:253
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:149
std::map< uint256, std::vector< std::pair< std::uint32_t, std::function< void(std::shared_ptr< NodeObject > const &)> > > > read_
Definition Database.h:251
std::atomic< std::uint64_t > fetchDurationUs_
Definition Database.h:243
virtual bool isSameDB(std::uint32_t s1, std::uint32_t s2)=0
virtual ~Database()
Destroy the node store.
Definition Database.cpp:100
virtual void sync()=0
std::atomic< std::uint64_t > storeSz_
Definition Database.h:241
std::uint32_t const earliestLedgerSeq_
Definition Database.h:212
std::atomic< std::uint32_t > fetchHitCount_
Definition Database.h:203
std::atomic< std::uint64_t > storeCount_
Definition Database.h:240
virtual std::string getName() const =0
Retrieve the name associated with this backend.
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:202
std::uint64_t getStoreCount() const
Gather statistics pertaining to read and write activities.
Definition Database.h:145
std::uint64_t getStoreSize() const
Definition Database.h:163
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
void getCountsJson(Json::Value &obj)
Definition Database.cpp:225
std::uint32_t getFetchTotalCount() const
Definition Database.h:151
std::uint32_t getFetchSize() const
Definition Database.h:169
virtual std::int32_t getWriteLoad() const =0
Retrieve the estimated number of pending write operations.
std::atomic< std::uint32_t > fetchSz_
Definition Database.h:204
void importInternal(Backend &dstBackend, Database &srcDB)
Definition Database.cpp:164
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
base_uint< 256 > uint256
Definition base_uint.h:526
Contains information about a fetch operation.