rippled
Loading...
Searching...
No Matches
Database.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2017 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_NODESTORE_DATABASE_H_INCLUDED
21#define RIPPLE_NODESTORE_DATABASE_H_INCLUDED
22
23#include <xrpld/nodestore/Backend.h>
24#include <xrpld/nodestore/NodeObject.h>
25#include <xrpld/nodestore/Scheduler.h>
26#include <xrpl/basics/TaggedCache.h>
27#include <xrpl/protocol/SystemParameters.h>
28
29#include <condition_variable>
30#include <thread>
31
32namespace ripple {
33
34namespace NodeStore {
35
50{
51public:
52 Database() = delete;
53
62 Scheduler& scheduler,
63 int readThreads,
64 Section const& config,
66
71 virtual ~Database();
72
77 virtual std::string
78 getName() const = 0;
79
81 virtual void
83
87 virtual std::int32_t
88 getWriteLoad() const = 0;
89
102 virtual void
104 NodeObjectType type,
105 Blob&& data,
106 uint256 const& hash,
107 std::uint32_t ledgerSeq) = 0;
108
109 /* Check if two ledgers are in the same database
110
111 If these two sequence numbers map to the same database,
112 the result of a fetch with either sequence number would
113 be identical.
114
115 @param s1 The first sequence number
116 @param s2 The second sequence number
117
118 @return 'true' if both ledgers would be in the same DB
119
120 */
121 virtual bool
123
124 virtual void
125 sync() = 0;
126
140 uint256 const& hash,
141 std::uint32_t ledgerSeq = 0,
143 bool duplicate = false);
144
157 virtual void
159 uint256 const& hash,
160 std::uint32_t ledgerSeq,
161 std::function<void(std::shared_ptr<NodeObject> const&)>&& callback);
162
164 virtual void
165 sweep() = 0;
166
173 {
174 return storeCount_;
175 }
176
179 {
180 return fetchTotalCount_;
181 }
182
185 {
186 return fetchHitCount_;
187 }
188
191 {
192 return storeSz_;
193 }
194
197 {
198 return fetchSz_;
199 }
200
201 void
203
205 int
207 {
208 return fdRequired_;
209 }
210
211 virtual void
212 stop();
213
214 bool
215 isStopping() const;
216
219 [[nodiscard]] std::uint32_t
220 earliestLedgerSeq() const noexcept
221 {
222 return earliestLedgerSeq_;
223 }
224
225protected:
229
232
233 // The default is XRP_LEDGER_EARLIEST_SEQ (32570) to match the XRP ledger
234 // network's earliest allowed ledger sequence. Can be set through the
235 // configuration file using the 'earliest_seq' field under the 'node_db'
236 // stanza. If specified, the value must be greater than zero.
237 // Only unit tests or alternate
238 // networks should change this value.
240
241 // The maximum number of requests a thread extracts from the queue in an
242 // attempt to minimize the overhead of mutex acquisition. This is an
243 // advanced tunable, via the config file. The default value is 4.
244 int const requestBundle_;
245
246 void
248 {
249 XRPL_ASSERT(
250 count <= sz,
251 "ripple::NodeStore::Database::storeStats : valid inputs");
252 storeCount_ += count;
253 storeSz_ += sz;
254 }
255
256 // Called by the public import function
257 void
258 importInternal(Backend& dstBackend, Database& srcDB);
259
260 void
261 updateFetchMetrics(uint64_t fetches, uint64_t hits, uint64_t duration)
262 {
263 fetchTotalCount_ += fetches;
264 fetchHitCount_ += hits;
265 fetchDurationUs_ += duration;
266 }
267
268private:
274
277
278 // reads to do
279 std::map<
280 uint256,
285
289
292 uint256 const& hash,
293 std::uint32_t ledgerSeq,
294 FetchReport& fetchReport,
295 bool duplicate) = 0;
296
304 virtual void
306
307 void
309};
310
311} // namespace NodeStore
312} // namespace ripple
313
314#endif
Represents a JSON value.
Definition: json_value.h:147
A generic endpoint for log messages.
Definition: Journal.h:59
A backend used for the NodeStore.
Definition: Backend.h:40
Persistency layer for NodeObject.
Definition: Database.h:50
void getCountsJson(Json::Value &obj)
Definition: Database.cpp:266
std::atomic< std::uint32_t > fetchSz_
Definition: Database.h:231
void storeStats(std::uint64_t count, std::uint64_t sz)
Definition: Database.h:247
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:183
std::uint64_t getStoreSize() const
Definition: Database.h:190
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:130
virtual void sweep()=0
Remove expired entries from the positive and negative caches.
std::condition_variable readCondVar_
Definition: Database.h:276
std::uint64_t getStoreCount() const
Gather statistics pertaining to read and write activities.
Definition: Database.h:172
std::atomic< std::uint64_t > storeCount_
Definition: Database.h:269
std::uint32_t const earliestLedgerSeq_
Definition: Database.h:239
std::map< uint256, std::vector< std::pair< std::uint32_t, std::function< void(std::shared_ptr< NodeObject > const &)> > > > read_
Definition: Database.h:284
std::atomic< std::uint64_t > storeSz_
Definition: Database.h:270
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:239
std::atomic< bool > readStopping_
Definition: Database.h:286
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:273
std::atomic< std::uint32_t > fetchHitCount_
Definition: Database.h:230
std::uint32_t getFetchSize() const
Definition: Database.h:196
beast::Journal const j_
Definition: Database.h:226
virtual std::int32_t getWriteLoad() const =0
Retrieve the estimated number of pending write operations.
std::atomic< std::uint64_t > fetchDurationUs_
Definition: Database.h:272
void updateFetchMetrics(uint64_t fetches, uint64_t hits, uint64_t duration)
Definition: Database.h:261
std::uint32_t getFetchHitCount() const
Definition: Database.h:184
std::atomic< int > runningThreads_
Definition: Database.h:288
std::uint32_t earliestLedgerSeq() const noexcept
Definition: Database.h:220
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:206
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:178
std::atomic< std::uint64_t > fetchTotalCount_
Definition: Database.h:271
std::atomic< int > readThreads_
Definition: Database.h:287
void importInternal(Backend &dstBackend, Database &srcDB)
Definition: Database.cpp:198
Scheduling for asynchronous backend activity.
Holds a collection of configuration values.
Definition: BasicConfig.h:43
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
base_uint< 256 > uint256
Definition: base_uint.h:557
NodeObjectType
The types of node objects.
Definition: NodeObject.h:32
Contains information about a fetch operation.