Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
LedgerCache.hpp
1#pragma once
2
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"
10
11#include <xrpl/basics/base_uint.h>
12#include <xrpl/basics/hardened_hash.h>
13
14#include <condition_variable>
15#include <cstddef>
16#include <cstdint>
17#include <functional>
18#include <map>
19#include <optional>
20#include <shared_mutex>
21#include <string>
22#include <unordered_set>
23#include <vector>
24
25namespace data {
26
30class LedgerCache : public LedgerCacheInterface {
31public:
33 struct CacheEntry {
34 uint32_t seq = 0;
35 Blob blob;
36 };
37
38 using CacheMap = std::map<ripple::uint256, CacheEntry>;
39
40private:
41 // counters for fetchLedgerObject(s) hit rate
42 std::reference_wrapper<util::prometheus::CounterInt> objectReqCounter_{
44 "ledger_cache_counter_total_number",
45 util::prometheus::Labels({{"type", "request"}, {"fetch", "ledger_objects"}}),
46 "LedgerCache statistics"
47 )
48 };
49 std::reference_wrapper<util::prometheus::CounterInt> objectHitCounter_{
51 "ledger_cache_counter_total_number",
52 util::prometheus::Labels({{"type", "cache_hit"}, {"fetch", "ledger_objects"}})
53 )
54 };
55
56 // counters for fetchSuccessorKey hit rate
57 std::reference_wrapper<util::prometheus::CounterInt> successorReqCounter_{
59 "ledger_cache_counter_total_number",
60 util::prometheus::Labels({{"type", "request"}, {"fetch", "successor_key"}}),
61 "ledgerCache"
62 )
63 };
64 std::reference_wrapper<util::prometheus::CounterInt> successorHitCounter_{
66 "ledger_cache_counter_total_number",
67 util::prometheus::Labels({{"type", "cache_hit"}, {"fetch", "successor_key"}})
68 )
69 };
70
71 CacheMap map_;
72 CacheMap deleted_;
73
74 mutable std::shared_mutex mtx_;
75 std::condition_variable_any cv_;
76 uint32_t latestSeq_ = 0;
77 util::prometheus::Bool full_{PrometheusService::boolMetric(
78 "ledger_cache_full",
79 util::prometheus::Labels{},
80 "Whether ledger cache full or not"
81 )};
82 util::prometheus::Bool disabled_{PrometheusService::boolMetric(
83 "ledger_cache_disabled",
84 util::prometheus::Labels{},
85 "Whether ledger cache is disabled or not"
86 )};
87 util::prometheus::Bool isCurrentlyLoading_{
89 "ledger_cache_is_currently_loading",
90 util::prometheus::Labels{},
91 "Whether ledger cache is currently loading or not"
92 )
93
94 };
95
96 // temporary set to prevent background thread from writing already deleted data. not used when
97 // cache is full
98 std::unordered_set<ripple::uint256, ripple::hardened_hash<>> deletes_;
99
100public:
101 void
102 update(std::vector<LedgerObject> const& objs, uint32_t seq, bool isBackground) override;
103
104 void
105 update(std::vector<etl::model::Object> const& objs, uint32_t seq) override;
106
107 std::optional<Blob>
108 get(ripple::uint256 const& key, uint32_t seq) const override;
109
110 std::optional<Blob>
111 getDeleted(ripple::uint256 const& key, uint32_t seq) const override;
112
113 std::optional<LedgerObject>
114 getSuccessor(ripple::uint256 const& key, uint32_t seq) const override;
115
116 std::optional<LedgerObject>
117 getPredecessor(ripple::uint256 const& key, uint32_t seq) const override;
118
119 void
120 setDisabled() override;
121
122 bool
123 isDisabled() const override;
124
125 void
126 setFull() override;
127
128 uint32_t
129 latestLedgerSequence() const override;
130
131 bool
132 isFull() const override;
133
134 size_t
135 size() const override;
136
137 float
138 getObjectHitRate() const override;
139
140 float
141 getSuccessorHitRate() const override;
142
143 void
144 waitUntilCacheContainsSeq(uint32_t seq) override;
145
146 std::expected<void, std::string>
147 saveToFile(std::string const& path) const override;
148
149 std::expected<void, std::string>
150 loadFromFile(std::string const& path, uint32_t minLatestSequence) override;
151
152 void
153 startLoading() override;
154
155 [[nodiscard]] bool
156 isCurrentlyLoading() const override;
157};
158
159} // namespace data
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