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
142 {
143 return storeCount_;
144 }
145
148 {
149 return fetchTotalCount_;
150 }
151
154 {
155 return fetchHitCount_;
156 }
157
160 {
161 return storeSz_;
162 }
163
166 {
167 return fetchSz_;
168 }
169
170 void
172
174 int
176 {
177 return fdRequired_;
178 }
179
180 virtual void
181 stop();
182
183 bool
184 isStopping() const;
185
188 [[nodiscard]] std::uint32_t
189 earliestLedgerSeq() const noexcept
190 {
191 return earliestLedgerSeq_;
192 }
193
194protected:
198
201
202 // The default is XRP_LEDGER_EARLIEST_SEQ (32570) to match the XRP ledger
203 // network's earliest allowed ledger sequence. Can be set through the
204 // configuration file using the 'earliest_seq' field under the 'node_db'
205 // stanza. If specified, the value must be greater than zero.
206 // Only unit tests or alternate
207 // networks should change this value.
209
210 // The maximum number of requests a thread extracts from the queue in an
211 // attempt to minimize the overhead of mutex acquisition. This is an
212 // advanced tunable, via the config file. The default value is 4.
213 int const requestBundle_;
214
215 void
217 {
218 XRPL_ASSERT(count <= sz, "xrpl::NodeStore::Database::storeStats : valid inputs");
219 storeCount_ += count;
220 storeSz_ += sz;
221 }
222
223 // Called by the public import function
224 void
225 importInternal(Backend& dstBackend, Database& srcDB);
226
227 void
228 updateFetchMetrics(uint64_t fetches, uint64_t hits, uint64_t duration)
229 {
230 fetchTotalCount_ += fetches;
231 fetchHitCount_ += hits;
232 fetchDurationUs_ += duration;
233 }
234
235private:
241
244
245 // reads to do
248
252
254 fetchNodeObject(uint256 const& hash, std::uint32_t ledgerSeq, FetchReport& fetchReport, bool duplicate) = 0;
255
263 virtual void
265
266 void
268};
269
270} // namespace NodeStore
271} // 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:153
std::uint32_t earliestLedgerSeq() const noexcept
Definition Database.h:189
virtual void importDatabase(Database &source)=0
Import objects from another database.
void storeStats(std::uint64_t count, std::uint64_t sz)
Definition Database.h:216
std::condition_variable readCondVar_
Definition Database.h:243
std::atomic< std::uint64_t > fetchTotalCount_
Definition Database.h:238
std::atomic< int > readThreads_
Definition Database.h:250
void updateFetchMetrics(uint64_t fetches, uint64_t hits, uint64_t duration)
Definition Database.h:228
std::atomic< std::uint64_t > storeDurationUs_
Definition Database.h:240
int fdRequired() const
Returns the number of file descriptors the database expects to need.
Definition Database.h:175
std::atomic< int > runningThreads_
Definition Database.h:251
beast::Journal const j_
Definition Database.h:195
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:249
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:247
std::atomic< std::uint64_t > fetchDurationUs_
Definition Database.h:239
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:237
std::uint32_t const earliestLedgerSeq_
Definition Database.h:208
std::atomic< std::uint32_t > fetchHitCount_
Definition Database.h:199
std::atomic< std::uint64_t > storeCount_
Definition Database.h:236
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:141
std::uint64_t getStoreSize() const
Definition Database.h:159
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:147
std::uint32_t getFetchSize() const
Definition Database.h:165
virtual std::int32_t getWriteLoad() const =0
Retrieve the estimated number of pending write operations.
std::atomic< std::uint32_t > fetchSz_
Definition Database.h:200
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.