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/Types.hpp"
23#include "util/prometheus/Counter.hpp"
24#include "util/prometheus/Label.hpp"
25#include "util/prometheus/Prometheus.hpp"
26
27#include <xrpl/basics/base_uint.h>
28#include <xrpl/basics/hardened_hash.h>
29
30#include <atomic>
31#include <condition_variable>
32#include <cstddef>
33#include <cstdint>
34#include <functional>
35#include <map>
36#include <optional>
37#include <shared_mutex>
38#include <unordered_set>
39#include <vector>
40
41namespace data {
42
47 struct CacheEntry {
48 uint32_t seq = 0;
49 Blob blob;
50 };
51
52 // counters for fetchLedgerObject(s) hit rate
53 std::reference_wrapper<util::prometheus::CounterInt> objectReqCounter_{PrometheusService::counterInt(
54 "ledger_cache_counter_total_number",
55 util::prometheus::Labels({{"type", "request"}, {"fetch", "ledger_objects"}}),
56 "LedgerCache statistics"
57 )};
58 std::reference_wrapper<util::prometheus::CounterInt> objectHitCounter_{PrometheusService::counterInt(
59 "ledger_cache_counter_total_number",
60 util::prometheus::Labels({{"type", "cache_hit"}, {"fetch", "ledger_objects"}})
61 )};
62
63 // counters for fetchSuccessorKey hit rate
64 std::reference_wrapper<util::prometheus::CounterInt> successorReqCounter_{PrometheusService::counterInt(
65 "ledger_cache_counter_total_number",
66 util::prometheus::Labels({{"type", "request"}, {"fetch", "successor_key"}}),
67 "ledgerCache"
68 )};
69 std::reference_wrapper<util::prometheus::CounterInt> successorHitCounter_{PrometheusService::counterInt(
70 "ledger_cache_counter_total_number",
71 util::prometheus::Labels({{"type", "cache_hit"}, {"fetch", "successor_key"}})
72 )};
73
74 std::map<ripple::uint256, CacheEntry> map_;
75
76 mutable std::shared_mutex mtx_;
77 std::condition_variable_any cv_;
78 uint32_t latestSeq_ = 0;
79 std::atomic_bool full_ = false;
80 std::atomic_bool disabled_ = false;
81
82 // temporary set to prevent background thread from writing already deleted data. not used when cache is full
83 std::unordered_set<ripple::uint256, ripple::hardened_hash<>> deletes_;
84
85public:
93 void
94 update(std::vector<LedgerObject> const& objs, uint32_t seq, bool isBackground = false);
95
103 std::optional<Blob>
104 get(ripple::uint256 const& key, uint32_t seq) const;
105
115 std::optional<LedgerObject>
116 getSuccessor(ripple::uint256 const& key, uint32_t seq) const;
117
127 std::optional<LedgerObject>
128 getPredecessor(ripple::uint256 const& key, uint32_t seq) const;
129
133 void
134 setDisabled();
135
139 bool
140 isDisabled() const;
141
149 void
150 setFull();
151
155 uint32_t
156 latestLedgerSequence() const;
157
161 bool
162 isFull() const;
163
167 size_t
168 size() const;
169
173 float
174 getObjectHitRate() const;
175
179 float
180 getSuccessorHitRate() const;
181
187 void
188 waitUntilCacheContainsSeq(uint32_t seq);
189};
190
191} // 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:194
Cache for an entire ledger.
Definition LedgerCache.hpp:46
float getObjectHitRate() const
Definition LedgerCache.cpp:179
bool isDisabled() const
Definition LedgerCache.cpp:149
bool isFull() const
Definition LedgerCache.cpp:166
void update(std::vector< LedgerObject > const &objs, uint32_t seq, bool isBackground=false)
Update the cache with new ledger objects.
Definition LedgerCache.cpp:55
std::optional< LedgerObject > getSuccessor(ripple::uint256 const &key, uint32_t seq) const
Gets a cached successor.
Definition LedgerCache.cpp:91
void setDisabled()
Disables the cache.
Definition LedgerCache.cpp:143
std::optional< Blob > get(ripple::uint256 const &key, uint32_t seq) const
Fetch a cached object by its key and sequence number.
Definition LedgerCache.cpp:124
uint32_t latestLedgerSequence() const
Definition LedgerCache.cpp:37
size_t size() const
Definition LedgerCache.cpp:172
float getSuccessorHitRate() const
Definition LedgerCache.cpp:187
void waitUntilCacheContainsSeq(uint32_t seq)
Waits until the cache contains a specific sequence.
Definition LedgerCache.cpp:44
void setFull()
Sets the full flag to true.
Definition LedgerCache.cpp:155
std::optional< LedgerObject > getPredecessor(ripple::uint256 const &key, uint32_t seq) const
Gets a cached predcessor.
Definition LedgerCache.cpp:108
Class representing a collection of Prometheus labels.
Definition Label.hpp:59
This namespace implements the data access layer and related components.
Definition AmendmentCenter.cpp:70