3#include "data/LedgerCacheInterface.hpp"
4#include "data/Types.hpp"
5#include "etl/Models.hpp"
6#include "util/prometheus/Bool.hpp"
7#include "util/prometheus/Counter.hpp"
8#include "util/prometheus/Label.hpp"
9#include "util/prometheus/Prometheus.hpp"
11#include <xrpl/basics/base_uint.h>
12#include <xrpl/basics/hardened_hash.h>
14#include <condition_variable>
20#include <shared_mutex>
22#include <unordered_set>
38 using CacheMap = std::map<ripple::uint256, CacheEntry>;
42 std::reference_wrapper<util::prometheus::CounterInt> objectReqCounter_{
44 "ledger_cache_counter_total_number",
46 "LedgerCache statistics"
49 std::reference_wrapper<util::prometheus::CounterInt> objectHitCounter_{
51 "ledger_cache_counter_total_number",
52 util::prometheus::Labels({{
"type",
"cache_hit"}, {
"fetch",
"ledger_objects"}})
57 std::reference_wrapper<util::prometheus::CounterInt> successorReqCounter_{
59 "ledger_cache_counter_total_number",
60 util::prometheus::Labels({{
"type",
"request"}, {
"fetch",
"successor_key"}}),
64 std::reference_wrapper<util::prometheus::CounterInt> successorHitCounter_{
66 "ledger_cache_counter_total_number",
67 util::prometheus::Labels({{
"type",
"cache_hit"}, {
"fetch",
"successor_key"}})
74 mutable std::shared_mutex mtx_;
75 std::condition_variable_any cv_;
76 uint32_t latestSeq_ = 0;
79 util::prometheus::Labels{},
80 "Whether ledger cache full or not"
83 "ledger_cache_disabled",
84 util::prometheus::Labels{},
85 "Whether ledger cache is disabled or not"
87 util::prometheus::Bool isCurrentlyLoading_{
89 "ledger_cache_is_currently_loading",
90 util::prometheus::Labels{},
91 "Whether ledger cache is currently loading or not"
98 std::unordered_set<ripple::uint256, ripple::hardened_hash<>> deletes_;
102 update(std::vector<LedgerObject>
const& objs, uint32_t seq,
bool isBackground)
override;
105 update(std::vector<etl::model::Object>
const& objs, uint32_t seq)
override;
108 get(ripple::uint256
const& key, uint32_t seq)
const override;
111 getDeleted(ripple::uint256
const& key, uint32_t seq)
const override;
113 std::optional<LedgerObject>
114 getSuccessor(ripple::uint256
const& key, uint32_t seq)
const override;
116 std::optional<LedgerObject>
117 getPredecessor(ripple::uint256
const& key, uint32_t seq)
const override;
135 size()
const override;
146 std::expected<void, std::string>
147 saveToFile(std::string
const& path)
const override;
149 std::expected<void, std::string>
150 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:211
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:201
Cache for an entire ledger.
Definition LedgerCache.hpp:30
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:148
bool isDisabled() const override
Definition LedgerCache.cpp:196
void setDisabled() override
Disables the cache.
Definition LedgerCache.cpp:190
bool isCurrentlyLoading() const override
Check whether the cache is currently being loaded from the backend.
Definition LedgerCache.cpp:282
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:167
uint32_t latestLedgerSequence() const override
Definition LedgerCache.cpp:25
std::optional< LedgerObject > getPredecessor(ripple::uint256 const &key, uint32_t seq) const override
Gets a cached predcessor.
Definition LedgerCache.cpp:132
std::expected< void, std::string > saveToFile(std::string const &path) const override
Save the cache to file.
Definition LedgerCache.cpp:244
std::optional< LedgerObject > getSuccessor(ripple::uint256 const &key, uint32_t seq) const override
Gets a cached successor.
Definition LedgerCache.cpp:115
float getSuccessorHitRate() const override
Definition LedgerCache.cpp:235
void update(std::vector< LedgerObject > const &objs, uint32_t seq, bool isBackground) override
Update the cache with new ledger objects.
Definition LedgerCache.cpp:43
void waitUntilCacheContainsSeq(uint32_t seq) override
Waits until the cache contains a specific sequence.
Definition LedgerCache.cpp:32
std::expected< void, std::string > loadFromFile(std::string const &path, uint32_t minLatestSequence) override
Load the cache from file.
Definition LedgerCache.cpp:259
bool isFull() const override
Definition LedgerCache.cpp:214
void startLoading() override
Mark the cache as currently loading from the backend.
Definition LedgerCache.cpp:276
float getObjectHitRate() const override
Definition LedgerCache.cpp:227
void setFull() override
Sets the full flag to true.
Definition LedgerCache.cpp:202
size_t size() const override
Definition LedgerCache.cpp:220
Class representing a collection of Prometheus labels.
Definition Label.hpp:41
This namespace implements the data access layer and related components.
Definition AmendmentCenter.cpp:56
An entry of the cache.
Definition LedgerCache.hpp:33