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/basics/TaggedCache.ipp>
30#include <xrpl/protocol/SystemParameters.h>
31
32#include <condition_variable>
33
34namespace ripple {
35
36namespace NodeStore {
37
52{
53public:
54 Database() = delete;
55
64 Scheduler& scheduler,
65 int readThreads,
66 Section const& config,
68
73 virtual ~Database();
74
79 virtual std::string
80 getName() const = 0;
81
83 virtual void
85
89 virtual std::int32_t
90 getWriteLoad() const = 0;
91
104 virtual void
106 NodeObjectType type,
107 Blob&& data,
108 uint256 const& hash,
109 std::uint32_t ledgerSeq) = 0;
110
111 /* Check if two ledgers are in the same database
112
113 If these two sequence numbers map to the same database,
114 the result of a fetch with either sequence number would
115 be identical.
116
117 @param s1 The first sequence number
118 @param s2 The second sequence number
119
120 @return 'true' if both ledgers would be in the same DB
121
122 */
123 virtual bool
125
126 virtual void
127 sync() = 0;
128
142 uint256 const& hash,
143 std::uint32_t ledgerSeq = 0,
145 bool duplicate = false);
146
159 virtual void
161 uint256 const& hash,
162 std::uint32_t ledgerSeq,
163 std::function<void(std::shared_ptr<NodeObject> const&)>&& callback);
164
166 virtual void
167 sweep() = 0;
168
175 {
176 return storeCount_;
177 }
178
181 {
182 return fetchTotalCount_;
183 }
184
187 {
188 return fetchHitCount_;
189 }
190
193 {
194 return storeSz_;
195 }
196
199 {
200 return fetchSz_;
201 }
202
203 void
205
207 int
209 {
210 return fdRequired_;
211 }
212
213 virtual void
214 stop();
215
216 bool
217 isStopping() const;
218
221 [[nodiscard]] std::uint32_t
222 earliestLedgerSeq() const noexcept
223 {
224 return earliestLedgerSeq_;
225 }
226
227protected:
231
234
235 // The default is XRP_LEDGER_EARLIEST_SEQ (32570) to match the XRP ledger
236 // network's earliest allowed ledger sequence. Can be set through the
237 // configuration file using the 'earliest_seq' field under the 'node_db'
238 // stanza. If specified, the value must be greater than zero.
239 // Only unit tests or alternate
240 // networks should change this value.
242
243 // The maximum number of requests a thread extracts from the queue in an
244 // attempt to minimize the overhead of mutex acquisition. This is an
245 // advanced tunable, via the config file. The default value is 4.
246 int const requestBundle_;
247
248 void
250 {
251 XRPL_ASSERT(
252 count <= sz,
253 "ripple::NodeStore::Database::storeStats : valid inputs");
254 storeCount_ += count;
255 storeSz_ += sz;
256 }
257
258 // Called by the public import function
259 void
260 importInternal(Backend& dstBackend, Database& srcDB);
261
262 void
263 updateFetchMetrics(uint64_t fetches, uint64_t hits, uint64_t duration)
264 {
265 fetchTotalCount_ += fetches;
266 fetchHitCount_ += hits;
267 fetchDurationUs_ += duration;
268 }
269
270private:
276
279
280 // reads to do
281 std::map<
282 uint256,
287
291
294 uint256 const& hash,
295 std::uint32_t ledgerSeq,
296 FetchReport& fetchReport,
297 bool duplicate) = 0;
298
306 virtual void
308
309 void
311};
312
313} // namespace NodeStore
314} // namespace ripple
315
316#endif
Represents a JSON value.
Definition: json_value.h:150
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:52
void getCountsJson(Json::Value &obj)
Definition: Database.cpp:268
std::atomic< std::uint32_t > fetchSz_
Definition: Database.h:233
void storeStats(std::uint64_t count, std::uint64_t sz)
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:185
std::uint64_t getStoreSize() const
Definition: Database.h:192
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:278
std::uint64_t getStoreCount() const
Gather statistics pertaining to read and write activities.
Definition: Database.h:174
std::atomic< std::uint64_t > storeCount_
Definition: Database.h:271
std::uint32_t const earliestLedgerSeq_
Definition: Database.h:241
std::map< uint256, std::vector< std::pair< std::uint32_t, std::function< void(std::shared_ptr< NodeObject > const &)> > > > read_
Definition: Database.h:286
std::atomic< std::uint64_t > storeSz_
Definition: Database.h:272
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:288
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:275
std::atomic< std::uint32_t > fetchHitCount_
Definition: Database.h:232
std::uint32_t getFetchSize() const
Definition: Database.h:198
beast::Journal const j_
Definition: Database.h:228
virtual std::int32_t getWriteLoad() const =0
Retrieve the estimated number of pending write operations.
std::atomic< std::uint64_t > fetchDurationUs_
Definition: Database.h:274
void updateFetchMetrics(uint64_t fetches, uint64_t hits, uint64_t duration)
Definition: Database.h:263
std::uint32_t getFetchHitCount() const
Definition: Database.h:186
std::atomic< int > runningThreads_
Definition: Database.h:290
std::uint32_t earliestLedgerSeq() const noexcept
Definition: Database.h:222
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:208
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:180
std::atomic< std::uint64_t > fetchTotalCount_
Definition: Database.h:273
std::atomic< int > readThreads_
Definition: Database.h:289
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.