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>
40 using CacheMap = std::map<xrpl::uint256, CacheEntry>;
44 std::reference_wrapper<util::prometheus::CounterInt> objectReqCounter_{
46 "ledger_cache_counter_total_number",
48 "LedgerCache statistics"
51 std::reference_wrapper<util::prometheus::CounterInt> objectHitCounter_{
53 "ledger_cache_counter_total_number",
54 util::prometheus::Labels({{
"type",
"cache_hit"}, {
"fetch",
"ledger_objects"}})
59 std::reference_wrapper<util::prometheus::CounterInt> successorReqCounter_{
61 "ledger_cache_counter_total_number",
62 util::prometheus::Labels({{
"type",
"request"}, {
"fetch",
"successor_key"}}),
66 std::reference_wrapper<util::prometheus::CounterInt> successorHitCounter_{
68 "ledger_cache_counter_total_number",
69 util::prometheus::Labels({{
"type",
"cache_hit"}, {
"fetch",
"successor_key"}})
76 mutable std::shared_mutex mtx_;
77 std::condition_variable_any cv_;
78 uint32_t latestSeq_ = 0;
81 util::prometheus::Labels{},
82 "Whether ledger cache full or not"
85 "ledger_cache_disabled",
86 util::prometheus::Labels{},
87 "Whether ledger cache is disabled or not"
89 util::prometheus::Bool isCurrentlyLoading_{
91 "ledger_cache_is_currently_loading",
92 util::prometheus::Labels{},
93 "Whether ledger cache is currently loading or not"
100 std::unordered_set<xrpl::uint256, xrpl::HardenedHash<>> deletes_;
104 update(std::vector<LedgerObject>
const& objs, uint32_t seq,
bool isBackground)
override;
107 update(std::vector<etl::model::Object>
const& objs, uint32_t seq)
override;
110 get(xrpl::uint256
const& key, uint32_t seq)
const override;
113 getDeleted(xrpl::uint256
const& key, uint32_t seq)
const override;
115 std::optional<LedgerObject>
116 getSuccessor(xrpl::uint256
const& key, uint32_t seq)
const override;
118 std::optional<LedgerObject>
119 getPredecessor(xrpl::uint256
const& key, uint32_t seq)
const override;
137 size()
const override;
148 std::expected<void, std::string>
149 saveToFile(std::string
const& path)
const override;
151 std::expected<void, std::string>
152 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
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 > get(xrpl::uint256 const &key, uint32_t seq) const override
Fetch a cached object by its key and sequence number.
Definition LedgerCache.cpp:148
uint32_t latestLedgerSequence() const override
Definition LedgerCache.cpp:25
std::optional< Blob > getDeleted(xrpl::uint256 const &key, uint32_t seq) const override
Fetch a recently deleted object by its key and sequence number.
Definition LedgerCache.cpp:167
std::expected< void, std::string > saveToFile(std::string const &path) const override
Save the cache to file.
Definition LedgerCache.cpp:244
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
std::optional< LedgerObject > getSuccessor(xrpl::uint256 const &key, uint32_t seq) const override
Gets a cached successor.
Definition LedgerCache.cpp:115
bool isFull() const override
Definition LedgerCache.cpp:214
void startLoading() override
Mark the cache as currently loading from the backend.
Definition LedgerCache.cpp:276
std::optional< LedgerObject > getPredecessor(xrpl::uint256 const &key, uint32_t seq) const override
Gets a cached predcessor.
Definition LedgerCache.cpp:132
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:35