rippled
Loading...
Searching...
No Matches
Database.h
1#ifndef XRPL_NODESTORE_DATABASE_H_INCLUDED
2#define XRPL_NODESTORE_DATABASE_H_INCLUDED
3
4#include <xrpl/basics/BasicConfig.h>
5#include <xrpl/basics/Log.h>
6#include <xrpl/basics/TaggedCache.ipp>
7#include <xrpl/nodestore/Backend.h>
8#include <xrpl/nodestore/NodeObject.h>
9#include <xrpl/nodestore/Scheduler.h>
10#include <xrpl/protocol/SystemParameters.h>
11
12#include <condition_variable>
13
14namespace ripple {
15
16namespace NodeStore {
17
32{
33public:
34 Database() = delete;
35
44 Scheduler& scheduler,
45 int readThreads,
46 Section const& config,
48
53 virtual ~Database();
54
59 virtual std::string
60 getName() const = 0;
61
63 virtual void
65
69 virtual std::int32_t
70 getWriteLoad() const = 0;
71
84 virtual void
86 NodeObjectType type,
87 Blob&& data,
88 uint256 const& hash,
89 std::uint32_t ledgerSeq) = 0;
90
91 /* Check if two ledgers are in the same database
92
93 If these two sequence numbers map to the same database,
94 the result of a fetch with either sequence number would
95 be identical.
96
97 @param s1 The first sequence number
98 @param s2 The second sequence number
99
100 @return 'true' if both ledgers would be in the same DB
101
102 */
103 virtual bool
105
106 virtual void
107 sync() = 0;
108
122 uint256 const& hash,
123 std::uint32_t ledgerSeq = 0,
125 bool duplicate = false);
126
139 virtual void
141 uint256 const& hash,
142 std::uint32_t ledgerSeq,
143 std::function<void(std::shared_ptr<NodeObject> const&)>&& callback);
144
146 virtual void
147 sweep() = 0;
148
155 {
156 return storeCount_;
157 }
158
161 {
162 return fetchTotalCount_;
163 }
164
167 {
168 return fetchHitCount_;
169 }
170
173 {
174 return storeSz_;
175 }
176
179 {
180 return fetchSz_;
181 }
182
183 void
185
187 int
189 {
190 return fdRequired_;
191 }
192
193 virtual void
194 stop();
195
196 bool
197 isStopping() const;
198
201 [[nodiscard]] std::uint32_t
202 earliestLedgerSeq() const noexcept
203 {
204 return earliestLedgerSeq_;
205 }
206
207protected:
211
214
215 // The default is XRP_LEDGER_EARLIEST_SEQ (32570) to match the XRP ledger
216 // network's earliest allowed ledger sequence. Can be set through the
217 // configuration file using the 'earliest_seq' field under the 'node_db'
218 // stanza. If specified, the value must be greater than zero.
219 // Only unit tests or alternate
220 // networks should change this value.
222
223 // The maximum number of requests a thread extracts from the queue in an
224 // attempt to minimize the overhead of mutex acquisition. This is an
225 // advanced tunable, via the config file. The default value is 4.
226 int const requestBundle_;
227
228 void
230 {
231 XRPL_ASSERT(
232 count <= sz,
233 "ripple::NodeStore::Database::storeStats : valid inputs");
234 storeCount_ += count;
235 storeSz_ += sz;
236 }
237
238 // Called by the public import function
239 void
240 importInternal(Backend& dstBackend, Database& srcDB);
241
242 void
243 updateFetchMetrics(uint64_t fetches, uint64_t hits, uint64_t duration)
244 {
245 fetchTotalCount_ += fetches;
246 fetchHitCount_ += hits;
247 fetchDurationUs_ += duration;
248 }
249
250private:
256
259
260 // reads to do
261 std::map<
262 uint256,
267
271
274 uint256 const& hash,
275 std::uint32_t ledgerSeq,
276 FetchReport& fetchReport,
277 bool duplicate) = 0;
278
286 virtual void
288
289 void
291};
292
293} // namespace NodeStore
294} // namespace ripple
295
296#endif
Represents a JSON value.
Definition json_value.h:130
A generic endpoint for log messages.
Definition Journal.h:41
A backend used for the NodeStore.
Definition Backend.h:21
Persistency layer for NodeObject.
Definition Database.h:32
void getCountsJson(Json::Value &obj)
Definition Database.cpp:248
std::atomic< std::uint32_t > fetchSz_
Definition Database.h:213
void storeStats(std::uint64_t count, std::uint64_t sz)
Definition Database.h:229
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:165
std::uint64_t getStoreSize() const
Definition Database.h:172
virtual std::string getName() const =0
Retrieve the name associated with this backend.
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.
virtual ~Database()
Destroy the node store.
Definition Database.cpp:112
virtual void sweep()=0
Remove expired entries from the positive and negative caches.
std::condition_variable readCondVar_
Definition Database.h:258
std::uint64_t getStoreCount() const
Gather statistics pertaining to read and write activities.
Definition Database.h:154
std::atomic< std::uint64_t > storeCount_
Definition Database.h:251
std::uint32_t const earliestLedgerSeq_
Definition Database.h:221
std::map< uint256, std::vector< std::pair< std::uint32_t, std::function< void(std::shared_ptr< NodeObject > const &)> > > > read_
Definition Database.h:266
std::atomic< std::uint64_t > storeSz_
Definition Database.h:252
virtual void importDatabase(Database &source)=0
Import objects from another database.
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:221
std::atomic< bool > readStopping_
Definition Database.h:268
virtual void store(NodeObjectType type, Blob &&data, uint256 const &hash, std::uint32_t ledgerSeq)=0
Store the object.
std::atomic< std::uint64_t > storeDurationUs_
Definition Database.h:255
std::atomic< std::uint32_t > fetchHitCount_
Definition Database.h:212
std::uint32_t getFetchSize() const
Definition Database.h:178
beast::Journal const j_
Definition Database.h:208
virtual std::int32_t getWriteLoad() const =0
Retrieve the estimated number of pending write operations.
std::atomic< std::uint64_t > fetchDurationUs_
Definition Database.h:254
void updateFetchMetrics(uint64_t fetches, uint64_t hits, uint64_t duration)
Definition Database.h:243
std::uint32_t getFetchHitCount() const
Definition Database.h:166
std::atomic< int > runningThreads_
Definition Database.h:270
std::uint32_t earliestLedgerSeq() const noexcept
Definition Database.h:202
virtual bool isSameDB(std::uint32_t s1, std::uint32_t s2)=0
int fdRequired() const
Returns the number of file descriptors the database expects to need.
Definition Database.h:188
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:160
std::atomic< std::uint64_t > fetchTotalCount_
Definition Database.h:253
std::atomic< int > readThreads_
Definition Database.h:269
void importInternal(Backend &dstBackend, Database &srcDB)
Definition Database.cpp:180
Scheduling for asynchronous backend activity.
Holds a collection of configuration values.
Definition BasicConfig.h:26
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
base_uint< 256 > uint256
Definition base_uint.h:539
NodeObjectType
The types of node objects.
Definition NodeObject.h:13
Contains information about a fetch operation.