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