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 "etlng/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 <unordered_set>
41#include <vector>
42
43namespace data {
44
49 struct CacheEntry {
50 uint32_t seq = 0;
51 Blob blob;
52 };
53
54 // counters for fetchLedgerObject(s) hit rate
55 std::reference_wrapper<util::prometheus::CounterInt> objectReqCounter_{PrometheusService::counterInt(
56 "ledger_cache_counter_total_number",
57 util::prometheus::Labels({{"type", "request"}, {"fetch", "ledger_objects"}}),
58 "LedgerCache statistics"
59 )};
60 std::reference_wrapper<util::prometheus::CounterInt> objectHitCounter_{PrometheusService::counterInt(
61 "ledger_cache_counter_total_number",
62 util::prometheus::Labels({{"type", "cache_hit"}, {"fetch", "ledger_objects"}})
63 )};
64
65 // counters for fetchSuccessorKey hit rate
66 std::reference_wrapper<util::prometheus::CounterInt> successorReqCounter_{PrometheusService::counterInt(
67 "ledger_cache_counter_total_number",
68 util::prometheus::Labels({{"type", "request"}, {"fetch", "successor_key"}}),
69 "ledgerCache"
70 )};
71 std::reference_wrapper<util::prometheus::CounterInt> successorHitCounter_{PrometheusService::counterInt(
72 "ledger_cache_counter_total_number",
73 util::prometheus::Labels({{"type", "cache_hit"}, {"fetch", "successor_key"}})
74 )};
75
76 std::map<ripple::uint256, CacheEntry> map_;
77 std::map<ripple::uint256, CacheEntry> deleted_;
78
79 mutable std::shared_mutex mtx_;
80 std::condition_variable_any cv_;
81 uint32_t latestSeq_ = 0;
83 "ledger_cache_full",
85 "Whether ledger cache full or not"
86 )};
88 "ledger_cache_disabled",
90 "Whether ledger cache is disabled or not"
91 )};
92
93 // temporary set to prevent background thread from writing already deleted data. not used when cache is full
94 std::unordered_set<ripple::uint256, ripple::hardened_hash<>> deletes_;
95
96public:
97 void
98 update(std::vector<LedgerObject> const& objs, uint32_t seq, bool isBackground) override;
99
100 void
101 update(std::vector<etlng::model::Object> const& objs, uint32_t seq) override;
102
103 std::optional<Blob>
104 get(ripple::uint256 const& key, uint32_t seq) const override;
105
106 std::optional<Blob>
107 getDeleted(ripple::uint256 const& key, uint32_t seq) const override;
108
109 std::optional<LedgerObject>
110 getSuccessor(ripple::uint256 const& key, uint32_t seq) const override;
111
112 std::optional<LedgerObject>
113 getPredecessor(ripple::uint256 const& key, uint32_t seq) const override;
114
115 void
116 setDisabled() override;
117
118 bool
119 isDisabled() const override;
120
121 void
122 setFull() override;
123
124 uint32_t
125 latestLedgerSequence() const override;
126
127 bool
128 isFull() const override;
129
130 size_t
131 size() const override;
132
133 float
134 getObjectHitRate() const override;
135
136 float
137 getSuccessorHitRate() const override;
138
139 void
140 waitUntilCacheContainsSeq(uint32_t seq) override;
141};
142
143} // 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
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:188
Cache for an entire ledger.
Definition LedgerCacheInterface.hpp:38
Cache for an entire ledger.
Definition LedgerCache.hpp:48
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:161
bool isDisabled() const override
Definition LedgerCache.cpp:209
void setDisabled() override
Disables the cache.
Definition LedgerCache.cpp:203
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:180
uint32_t latestLedgerSequence() const override
Definition LedgerCache.cpp:38
std::optional< LedgerObject > getPredecessor(ripple::uint256 const &key, uint32_t seq) const override
Gets a cached predcessor.
Definition LedgerCache.cpp:145
std::optional< LedgerObject > getSuccessor(ripple::uint256 const &key, uint32_t seq) const override
Gets a cached successor.
Definition LedgerCache.cpp:128
float getSuccessorHitRate() const override
Definition LedgerCache.cpp:247
void update(std::vector< LedgerObject > const &objs, uint32_t seq, bool isBackground) override
Update the cache with new ledger objects.
Definition LedgerCache.cpp:56
void waitUntilCacheContainsSeq(uint32_t seq) override
Waits until the cache contains a specific sequence.
Definition LedgerCache.cpp:45
bool isFull() const override
Definition LedgerCache.cpp:226
float getObjectHitRate() const override
Definition LedgerCache.cpp:239
void setFull() override
Sets the full flag to true.
Definition LedgerCache.cpp:215
size_t size() const override
Definition LedgerCache.cpp:232
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