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
27#include <xrpl/basics/BasicConfig.h>
28#include <xrpl/basics/Log.h>
29#include <xrpl/protocol/SystemParameters.h>
30
31#include <condition_variable>
32
33namespace ripple {
34
35namespace NodeStore {
36
51{
52public:
53 Database() = delete;
54
63 Scheduler& scheduler,
64 int readThreads,
65 Section const& config,
67
72 virtual ~Database();
73
78 virtual std::string
79 getName() const = 0;
80
82 virtual void
84
88 virtual std::int32_t
89 getWriteLoad() const = 0;
90
103 virtual void
105 NodeObjectType type,
106 Blob&& data,
107 uint256 const& hash,
108 std::uint32_t ledgerSeq) = 0;
109
110 /* Check if two ledgers are in the same database
111
112 If these two sequence numbers map to the same database,
113 the result of a fetch with either sequence number would
114 be identical.
115
116 @param s1 The first sequence number
117 @param s2 The second sequence number
118
119 @return 'true' if both ledgers would be in the same DB
120
121 */
122 virtual bool
124
125 virtual void
126 sync() = 0;
127
141 uint256 const& hash,
142 std::uint32_t ledgerSeq = 0,
144 bool duplicate = false);
145
158 virtual void
160 uint256 const& hash,
161 std::uint32_t ledgerSeq,
162 std::function<void(std::shared_ptr<NodeObject> const&)>&& callback);
163
165 virtual void
166 sweep() = 0;
167
174 {
175 return storeCount_;
176 }
177
180 {
181 return fetchTotalCount_;
182 }
183
186 {
187 return fetchHitCount_;
188 }
189
192 {
193 return storeSz_;
194 }
195
198 {
199 return fetchSz_;
200 }
201
202 void
204
206 int
208 {
209 return fdRequired_;
210 }
211
212 virtual void
213 stop();
214
215 bool
216 isStopping() const;
217
220 [[nodiscard]] std::uint32_t
221 earliestLedgerSeq() const noexcept
222 {
223 return earliestLedgerSeq_;
224 }
225
226protected:
230
233
234 // The default is XRP_LEDGER_EARLIEST_SEQ (32570) to match the XRP ledger
235 // network's earliest allowed ledger sequence. Can be set through the
236 // configuration file using the 'earliest_seq' field under the 'node_db'
237 // stanza. If specified, the value must be greater than zero.
238 // Only unit tests or alternate
239 // networks should change this value.
241
242 // The maximum number of requests a thread extracts from the queue in an
243 // attempt to minimize the overhead of mutex acquisition. This is an
244 // advanced tunable, via the config file. The default value is 4.
245 int const requestBundle_;
246
247 void
249 {
250 XRPL_ASSERT(
251 count <= sz,
252 "ripple::NodeStore::Database::storeStats : valid inputs");
253 storeCount_ += count;
254 storeSz_ += sz;
255 }
256
257 // Called by the public import function
258 void
259 importInternal(Backend& dstBackend, Database& srcDB);
260
261 void
262 updateFetchMetrics(uint64_t fetches, uint64_t hits, uint64_t duration)
263 {
264 fetchTotalCount_ += fetches;
265 fetchHitCount_ += hits;
266 fetchDurationUs_ += duration;
267 }
268
269private:
275
278
279 // reads to do
280 std::map<
281 uint256,
286
290
293 uint256 const& hash,
294 std::uint32_t ledgerSeq,
295 FetchReport& fetchReport,
296 bool duplicate) = 0;
297
305 virtual void
307
308 void
310};
311
312} // namespace NodeStore
313} // namespace ripple
314
315#endif
Represents a JSON value.
Definition: json_value.h:148
A generic endpoint for log messages.
Definition: Journal.h:60
A backend used for the NodeStore.
Definition: Backend.h:40
Persistency layer for NodeObject.
Definition: Database.h:51
void getCountsJson(Json::Value &obj)
Definition: Database.cpp:268
std::atomic< std::uint32_t > fetchSz_
Definition: Database.h:232
void storeStats(std::uint64_t count, std::uint64_t sz)
Definition: Database.h:248
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:185
std::uint64_t getStoreSize() const
Definition: Database.h:191
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:132
virtual void sweep()=0
Remove expired entries from the positive and negative caches.
std::condition_variable readCondVar_
Definition: Database.h:277
std::uint64_t getStoreCount() const
Gather statistics pertaining to read and write activities.
Definition: Database.h:173
std::atomic< std::uint64_t > storeCount_
Definition: Database.h:270
std::uint32_t const earliestLedgerSeq_
Definition: Database.h:240
std::map< uint256, std::vector< std::pair< std::uint32_t, std::function< void(std::shared_ptr< NodeObject > const &)> > > > read_
Definition: Database.h:285
std::atomic< std::uint64_t > storeSz_
Definition: Database.h:271
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:241
std::atomic< bool > readStopping_
Definition: Database.h:287
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:274
std::atomic< std::uint32_t > fetchHitCount_
Definition: Database.h:231
std::uint32_t getFetchSize() const
Definition: Database.h:197
beast::Journal const j_
Definition: Database.h:227
virtual std::int32_t getWriteLoad() const =0
Retrieve the estimated number of pending write operations.
std::atomic< std::uint64_t > fetchDurationUs_
Definition: Database.h:273
void updateFetchMetrics(uint64_t fetches, uint64_t hits, uint64_t duration)
Definition: Database.h:262
std::uint32_t getFetchHitCount() const
Definition: Database.h:185
std::atomic< int > runningThreads_
Definition: Database.h:289
std::uint32_t earliestLedgerSeq() const noexcept
Definition: Database.h:221
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:207
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:179
std::atomic< std::uint64_t > fetchTotalCount_
Definition: Database.h:272
std::atomic< int > readThreads_
Definition: Database.h:288
void importInternal(Backend &dstBackend, Database &srcDB)
Definition: Database.cpp:200
Scheduling for asynchronous backend activity.
Holds a collection of configuration values.
Definition: BasicConfig.h:45
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:558
NodeObjectType
The types of node objects.
Definition: NodeObject.h:32
Contains information about a fetch operation.