22#include "data/LedgerCacheInterface.hpp"
23#include "data/Types.hpp"
24#include "etl/Models.hpp"
25#include "util/prometheus/Bool.hpp"
26#include "util/prometheus/Counter.hpp"
27#include "util/prometheus/Label.hpp"
28#include "util/prometheus/Prometheus.hpp"
30#include <xrpl/basics/base_uint.h>
31#include <xrpl/basics/hardened_hash.h>
33#include <condition_variable>
39#include <shared_mutex>
41#include <unordered_set>
57 using CacheMap = std::map<ripple::uint256, CacheEntry>;
61 std::reference_wrapper<util::prometheus::CounterInt> objectReqCounter_{
63 "ledger_cache_counter_total_number",
65 "LedgerCache statistics"
68 std::reference_wrapper<util::prometheus::CounterInt> objectHitCounter_{
70 "ledger_cache_counter_total_number",
71 util::prometheus::Labels({{
"type",
"cache_hit"}, {
"fetch",
"ledger_objects"}})
76 std::reference_wrapper<util::prometheus::CounterInt> successorReqCounter_{
78 "ledger_cache_counter_total_number",
79 util::prometheus::Labels({{
"type",
"request"}, {
"fetch",
"successor_key"}}),
83 std::reference_wrapper<util::prometheus::CounterInt> successorHitCounter_{
85 "ledger_cache_counter_total_number",
86 util::prometheus::Labels({{
"type",
"cache_hit"}, {
"fetch",
"successor_key"}})
93 mutable std::shared_mutex mtx_;
94 std::condition_variable_any cv_;
95 uint32_t latestSeq_ = 0;
98 util::prometheus::Labels{},
99 "Whether ledger cache full or not"
102 "ledger_cache_disabled",
103 util::prometheus::Labels{},
104 "Whether ledger cache is disabled or not"
109 std::unordered_set<ripple::uint256, ripple::hardened_hash<>> deletes_;
113 update(std::vector<LedgerObject>
const& objs, uint32_t seq,
bool isBackground)
override;
116 update(std::vector<etl::model::Object>
const& objs, uint32_t seq)
override;
119 get(ripple::uint256
const& key, uint32_t seq)
const override;
122 getDeleted(ripple::uint256
const& key, uint32_t seq)
const override;
124 std::optional<LedgerObject>
125 getSuccessor(ripple::uint256
const& key, uint32_t seq)
const override;
127 std::optional<LedgerObject>
128 getPredecessor(ripple::uint256
const& key, uint32_t seq)
const override;
146 size()
const override;
157 std::expected<void, std::string>
158 saveToFile(std::string
const& path)
const override;
160 std::expected<void, std::string>
161 loadFromFile(std::string
const& path, uint32_t minLatestSequence)
override;
static util::prometheus::CounterInt & counterInt(std::string name, util::prometheus::Labels labels, std::optional< std::string > description=std::nullopt)
Get an integer based counter metric. It will be created if it doesn't exist.
Definition Prometheus.cpp:230
static util::prometheus::Bool boolMetric(std::string name, util::prometheus::Labels labels, std::optional< std::string > description=std::nullopt)
Get a bool based metric. It will be created if it doesn't exist.
Definition Prometheus.cpp:220
Cache for an entire ledger.
Definition LedgerCache.hpp:49
std::optional< Blob > get(ripple::uint256 const &key, uint32_t seq) const override
Fetch a cached object by its key and sequence number.
Definition LedgerCache.cpp:167
bool isDisabled() const override
Definition LedgerCache.cpp:215
void setDisabled() override
Disables the cache.
Definition LedgerCache.cpp:209
std::optional< Blob > getDeleted(ripple::uint256 const &key, uint32_t seq) const override
Fetch a recently deleted object by its key and sequence number.
Definition LedgerCache.cpp:186
uint32_t latestLedgerSequence() const override
Definition LedgerCache.cpp:44
std::optional< LedgerObject > getPredecessor(ripple::uint256 const &key, uint32_t seq) const override
Gets a cached predcessor.
Definition LedgerCache.cpp:151
std::expected< void, std::string > saveToFile(std::string const &path) const override
Save the cache to file.
Definition LedgerCache.cpp:262
std::optional< LedgerObject > getSuccessor(ripple::uint256 const &key, uint32_t seq) const override
Gets a cached successor.
Definition LedgerCache.cpp:134
float getSuccessorHitRate() const override
Definition LedgerCache.cpp:253
void update(std::vector< LedgerObject > const &objs, uint32_t seq, bool isBackground) override
Update the cache with new ledger objects.
Definition LedgerCache.cpp:62
void waitUntilCacheContainsSeq(uint32_t seq) override
Waits until the cache contains a specific sequence.
Definition LedgerCache.cpp:51
std::expected< void, std::string > loadFromFile(std::string const &path, uint32_t minLatestSequence) override
Load the cache from file.
Definition LedgerCache.cpp:277
bool isFull() const override
Definition LedgerCache.cpp:232
float getObjectHitRate() const override
Definition LedgerCache.cpp:245
void setFull() override
Sets the full flag to true.
Definition LedgerCache.cpp:221
size_t size() const override
Definition LedgerCache.cpp:238
Class representing a collection of Prometheus labels.
Definition Label.hpp:60
This namespace implements the data access layer and related components.
Definition AmendmentCenter.cpp:75
An entry of the cache.
Definition LedgerCache.hpp:52