20 #include <ripple/app/ledger/Ledger.h>
21 #include <ripple/basics/chrono.h>
22 #include <ripple/beast/core/CurrentThreadName.h>
23 #include <ripple/json/json_value.h>
24 #include <ripple/nodestore/Database.h>
25 #include <ripple/protocol/HashPrefix.h>
26 #include <ripple/protocol/jss.h>
38 , scheduler_(scheduler)
39 , ledgersPerShard_(
get<
std::uint32_t>(
45 , earliestShardIndex_((earliestLedgerSeq_ - 1) / ledgersPerShard_)
48 Throw<std::runtime_error>(
"Invalid ledgers_per_shard");
51 Throw<std::runtime_error>(
"Invalid earliest_seq");
53 while (readThreads-- > 0)
78 if (shardIndex > earliestShardIndex_)
79 return ledgersPerShard_;
81 if (shardIndex == earliestShardIndex_)
82 return lastLedgerSeq(shardIndex) - firstLedgerSeq(shardIndex) + 1;
84 assert(!
"Invalid shard index");
114 read_[hash].emplace_back(ledgerSeq, std::move(cb));
123 auto storeBatch = [&, fname = __func__]() {
130 JLOG(
j_.
error()) <<
"Exception caught in function " << fname
131 <<
". Error: " << e.
what();
136 for (
auto const& nodeObject : batch)
137 sz += nodeObject->getData().size();
167 auto const begin{steady_clock::now()};
169 auto nodeObject{
fetchNodeObject(hash, ledgerSeq, fetchReport, duplicate)};
170 auto dur = steady_clock::now() - begin;
175 fetchSz_ += nodeObject->getData().size();
179 fetchReport.
elapsed = duration_cast<milliseconds>(dur);
190 JLOG(
j_.
error()) <<
"Source ledger sequence " << srcLedger.
info().
seq
196 return fail(
"Invalid hash");
198 return fail(
"Invalid account hash");
202 return fail(
"Source and destination databases are the same");
206 auto storeBatch = [&, fname = __func__]() {
208 for (
auto const& nodeObject : batch)
209 sz += nodeObject->getData().size();
213 dstBackend->storeBatch(batch);
218 std::string(
"Exception caught in function ") + fname +
219 ". Error: " + e.
what());
242 if (
auto nodeObject = srcDB.fetchNodeObject(
243 node.getHash().as_uint256(), srcLedger.
info().
seq))
259 return fail(
"Invalid state map");
263 return fail(
"Failed to store state map");
270 return fail(
"Invalid transaction map");
274 return fail(
"Failed to store transaction map");
277 if (!batch.
empty() && !storeBatch())
278 return fail(
"Failed to store");
305 if (it ==
read_.end())
310 lastHash = it->first;
311 entry = std::move(it->second);
316 auto seq = entry[0].first;
319 for (
auto const& req : entry)
321 if ((seq == req.first) ||
isSameDB(req.first, seq))
347 obj[jss::node_writes_duration_us] =
std::to_string(c->writeDurationUs);
Holds a collection of configuration values.
@ ledgerMaster
ledger master data for signing
static constexpr std::uint32_t DEFAULT_LEDGERS_PER_SHARD
The number of ledgers in a shard.
std::atomic< std::uint64_t > fetchDurationUs_
Persistency layer for NodeObject.
SHAMapHash getHash() const
Family const & family() const
const std::uint32_t ledgersPerShard_
void addRaw(LedgerInfo const &info, Serializer &s, bool includeHash)
std::map< uint256, std::vector< std::pair< std::uint32_t, std::function< void(std::shared_ptr< NodeObject > const &)> > > > read_
static std::shared_ptr< NodeObject > createObject(NodeObjectType type, Blob &&data, uint256 const &hash)
Create an object from fields.
std::atomic< std::uint64_t > fetchTotalCount_
void asyncFetch(uint256 const &hash, std::uint32_t ledgerSeq, std::function< void(std::shared_ptr< NodeObject > const &)> &&callback)
Fetch an object without waiting.
std::atomic< std::uint32_t > fetchSz_
Contains information about a fetch operation.
std::shared_ptr< SHAMap > snapShot(bool isMutable) const
std::atomic< std::uint32_t > fetchHitCount_
virtual NodeStore::Database & db()=0
std::atomic< std::uint64_t > storeCount_
@ batchWritePreallocationSize
Integers of any length that is a multiple of 32-bits.
std::atomic< std::uint64_t > storeSz_
LedgerInfo const & info() const override
Returns information about the ledger.
void importInternal(Backend &dstBackend, Database &srcDB)
SHAMap const & stateMap() const
std::condition_variable readCondVar_
virtual void onFetch(FetchReport const &report)=0
Reports completion of a fetch Allows the scheduler to monitor the node store's performance.
A generic endpoint for log messages.
Scheduling for asynchronous backend activity.
virtual ~Database()
Destroy the node store.
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.
void storeStats(std::uint64_t count, std::uint64_t sz)
SHAMap const & txMap() const
virtual std::optional< Backend::Counters< std::uint64_t > > getCounters() const
Retrieve backend read and write stats.
void setCurrentThreadName(std::string_view name)
Changes the name of the caller thread.
T emplace_back(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
virtual bool storeLedger(std::shared_ptr< Ledger const > const &srcLedger)=0
Store a ledger from a different database.
std::vector< std::thread > readThreads_
static constexpr std::uint32_t XRP_LEDGER_EARLIEST_SEQ
The XRP ledger network's earliest allowed sequence.
void getCountsJson(Json::Value &obj)
const std::uint32_t earliestLedgerSeq_
int add32(std::uint32_t i)
Information about the notional ledger backing the view.
std::shared_ptr< NodeObject > fetchNodeObject(uint256 const &hash, std::uint32_t ledgerSeq=0, FetchType fetchType=FetchType::synchronous, bool duplicate=false)
Fetch a node object.
virtual bool isSameDB(std::uint32_t s1, std::uint32_t s2)=0
std::uint32_t maxLedgers(std::uint32_t shardIndex) const noexcept
Calculates the maximum ledgers for a given shard index.
virtual void storeBatch(Batch const &batch)=0
Store a group of objects.
std::chrono::milliseconds elapsed
T & get(EitherAmount &amt)
A backend used for the NodeStore.