Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
LedgerCache.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2022, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
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"
29
30#include <xrpl/basics/base_uint.h>
31#include <xrpl/basics/hardened_hash.h>
32
33#include <condition_variable>
34#include <cstddef>
35#include <cstdint>
36#include <functional>
37#include <map>
38#include <optional>
39#include <shared_mutex>
40#include <string>
41#include <unordered_set>
42#include <vector>
43
44namespace data {
45
49class LedgerCache : public LedgerCacheInterface {
50public:
52 struct CacheEntry {
53 uint32_t seq = 0;
54 Blob blob;
55 };
56
57 using CacheMap = std::map<ripple::uint256, CacheEntry>;
58
59private:
60 // counters for fetchLedgerObject(s) hit rate
61 std::reference_wrapper<util::prometheus::CounterInt> objectReqCounter_{
63 "ledger_cache_counter_total_number",
64 util::prometheus::Labels({{"type", "request"}, {"fetch", "ledger_objects"}}),
65 "LedgerCache statistics"
66 )
67 };
68 std::reference_wrapper<util::prometheus::CounterInt> objectHitCounter_{
70 "ledger_cache_counter_total_number",
71 util::prometheus::Labels({{"type", "cache_hit"}, {"fetch", "ledger_objects"}})
72 )
73 };
74
75 // counters for fetchSuccessorKey hit rate
76 std::reference_wrapper<util::prometheus::CounterInt> successorReqCounter_{
78 "ledger_cache_counter_total_number",
79 util::prometheus::Labels({{"type", "request"}, {"fetch", "successor_key"}}),
80 "ledgerCache"
81 )
82 };
83 std::reference_wrapper<util::prometheus::CounterInt> successorHitCounter_{
85 "ledger_cache_counter_total_number",
86 util::prometheus::Labels({{"type", "cache_hit"}, {"fetch", "successor_key"}})
87 )
88 };
89
90 CacheMap map_;
91 CacheMap deleted_;
92
93 mutable std::shared_mutex mtx_;
94 std::condition_variable_any cv_;
95 uint32_t latestSeq_ = 0;
96 util::prometheus::Bool full_{PrometheusService::boolMetric(
97 "ledger_cache_full",
98 util::prometheus::Labels{},
99 "Whether ledger cache full or not"
100 )};
101 util::prometheus::Bool disabled_{PrometheusService::boolMetric(
102 "ledger_cache_disabled",
103 util::prometheus::Labels{},
104 "Whether ledger cache is disabled or not"
105 )};
106
107 // temporary set to prevent background thread from writing already deleted data. not used when
108 // cache is full
109 std::unordered_set<ripple::uint256, ripple::hardened_hash<>> deletes_;
110
111public:
112 void
113 update(std::vector<LedgerObject> const& objs, uint32_t seq, bool isBackground) override;
114
115 void
116 update(std::vector<etl::model::Object> const& objs, uint32_t seq) override;
117
118 std::optional<Blob>
119 get(ripple::uint256 const& key, uint32_t seq) const override;
120
121 std::optional<Blob>
122 getDeleted(ripple::uint256 const& key, uint32_t seq) const override;
123
124 std::optional<LedgerObject>
125 getSuccessor(ripple::uint256 const& key, uint32_t seq) const override;
126
127 std::optional<LedgerObject>
128 getPredecessor(ripple::uint256 const& key, uint32_t seq) const override;
129
130 void
131 setDisabled() override;
132
133 bool
134 isDisabled() const override;
135
136 void
137 setFull() override;
138
139 uint32_t
140 latestLedgerSequence() const override;
141
142 bool
143 isFull() const override;
144
145 size_t
146 size() const override;
147
148 float
149 getObjectHitRate() const override;
150
151 float
152 getSuccessorHitRate() const override;
153
154 void
155 waitUntilCacheContainsSeq(uint32_t seq) override;
156
157 std::expected<void, std::string>
158 saveToFile(std::string const& path) const override;
159
160 std::expected<void, std::string>
161 loadFromFile(std::string const& path, uint32_t minLatestSequence) override;
162};
163
164} // 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: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