rippled
Loading...
Searching...
No Matches
TaggedCache.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or 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#ifndef RIPPLE_BASICS_TAGGEDCACHE_H_INCLUDED
21#define RIPPLE_BASICS_TAGGEDCACHE_H_INCLUDED
22
23#include <xrpl/basics/IntrusivePointer.h>
24#include <xrpl/basics/Log.h>
25#include <xrpl/basics/SharedWeakCachePointer.ipp>
26#include <xrpl/basics/UnorderedContainers.h>
27#include <xrpl/basics/hardened_hash.h>
28#include <xrpl/beast/clock/abstract_clock.h>
29#include <xrpl/beast/insight/Insight.h>
30
31#include <atomic>
32#include <functional>
33#include <mutex>
34#include <thread>
35#include <type_traits>
36#include <vector>
37
38namespace ripple {
39
52template <
53 class Key,
54 class T,
55 bool IsKeyCache = false,
56 class SharedWeakUnionPointerType = SharedWeakCachePointer<T>,
57 class SharedPointerType = std::shared_ptr<T>,
58 class Hash = hardened_hash<>,
59 class KeyEqual = std::equal_to<Key>,
60 class Mutex = std::recursive_mutex>
62{
63public:
64 using mutex_type = Mutex;
65 using key_type = Key;
66 using mapped_type = T;
68 using shared_weak_combo_pointer_type = SharedWeakUnionPointerType;
69 using shared_pointer_type = SharedPointerType;
70
71public:
73 std::string const& name,
74 int size,
75 clock_type::duration expiration,
77 beast::Journal journal,
78 beast::insight::Collector::ptr const& collector =
80
81public:
85
88 size() const;
89
90 int
91 getCacheSize() const;
92
93 int
94 getTrackSize() const;
95
96 float
98
99 void
101
102 void
104
108 template <class KeyComparable>
109 bool
110 touch_if_exists(KeyComparable const& key);
111
113
114 void
116
117 bool
118 del(key_type const& key, bool valid);
119
120public:
134 template <class R>
135 bool
137 key_type const& key,
138 SharedPointerType& data,
139 R&& replaceCallback);
140
141 bool
143 key_type const& key,
144 SharedPointerType const& data);
145
146 bool
147 canonicalize_replace_client(key_type const& key, SharedPointerType& data);
148
149 SharedPointerType
150 fetch(key_type const& key);
151
156 template <class ReturnType = bool>
157 auto
158 insert(key_type const& key, T const& value)
160
161 template <class ReturnType = bool>
162 auto
164
165 // VFALCO NOTE It looks like this returns a copy of the data in
166 // the output parameter 'data'. This could be expensive.
167 // Perhaps it should work like standard containers, which
168 // simply return an iterator.
169 //
170 bool
171 retrieve(key_type const& key, T& data);
172
175
177 getKeys() const;
178
179 // CachedSLEs functions.
181 double
182 rate() const;
183
189 template <class Handler>
190 SharedPointerType
191 fetch(key_type const& digest, Handler const& h);
192 // End CachedSLEs functions.
193
194private:
195 SharedPointerType
197
198 void
200
201private:
202 struct Stats
203 {
204 template <class Handler>
206 std::string const& prefix,
207 Handler const& handler,
208 beast::insight::Collector::ptr const& collector)
209 : hook(collector->make_hook(handler))
210 , size(collector->make_gauge(prefix, "size"))
211 , hit_rate(collector->make_gauge(prefix, "hit_rate"))
212 , hits(0)
213 , misses(0)
214 {
215 }
216
220
223 };
224
226 {
227 public:
229
230 explicit KeyOnlyEntry(clock_type::time_point const& last_access_)
231 : last_access(last_access_)
232 {
233 }
234
235 void
237 {
238 last_access = now;
239 }
240 };
241
243 {
244 public:
247
249 clock_type::time_point const& last_access_,
250 shared_pointer_type const& ptr_)
251 : ptr(ptr_), last_access(last_access_)
252 {
253 }
254
255 bool
256 isWeak() const
257 {
258 if (!ptr)
259 return true;
260 return ptr.isWeak();
261 }
262 bool
263 isCached() const
264 {
265 return ptr && ptr.isStrong();
266 }
267 bool
268 isExpired() const
269 {
270 return ptr.expired();
271 }
272 SharedPointerType
274 {
275 return ptr.lock();
276 }
277 void
279 {
280 last_access = now;
281 }
282 };
283
284 typedef
287
290
293
296
297 [[nodiscard]] std::thread
299 clock_type::time_point const& when_expire,
300 [[maybe_unused]] clock_type::time_point const& now,
301 typename KeyValueCacheType::map_type& partition,
302 SweptPointersVector& stuffToSweep,
303 std::atomic<int>& allRemovals,
305
306 [[nodiscard]] std::thread
308 clock_type::time_point const& when_expire,
309 clock_type::time_point const& now,
310 typename KeyOnlyCacheType::map_type& partition,
312 std::atomic<int>& allRemovals,
314
318
320
321 // Used for logging
323
324 // Desired number of cache entries (0 = ignore)
325 int const m_target_size;
326
327 // Desired maximum cache age
329
330 // Number of items cached
332 cache_type m_cache; // Hold strong reference to recent objects
335};
336
337} // namespace ripple
338
339#endif
A generic endpoint for log messages.
Definition Journal.h:60
typename Clock::time_point time_point
typename Clock::duration duration
A metric for measuring an integral value.
Definition Gauge.h:40
A reference to a handler for performing polled collection.
Definition Hook.h:32
static std::shared_ptr< Collector > New()
void touch(clock_type::time_point const &now)
KeyOnlyEntry(clock_type::time_point const &last_access_)
clock_type::time_point last_access
ValueEntry(clock_type::time_point const &last_access_, shared_pointer_type const &ptr_)
shared_weak_combo_pointer_type ptr
clock_type::time_point last_access
void touch(clock_type::time_point const &now)
Map/cache combination.
Definition TaggedCache.h:62
bool canonicalize(key_type const &key, SharedPointerType &data, R &&replaceCallback)
Replace aliased objects with originals.
bool touch_if_exists(KeyComparable const &key)
Refresh the last access time on a key if present.
bool canonicalize_replace_client(key_type const &key, SharedPointerType &data)
bool retrieve(key_type const &key, T &data)
bool del(key_type const &key, bool valid)
SharedPointerType fetch(key_type const &key)
beast::Journal m_journal
clock_type & clock()
Return the clock associated with the cache.
clock_type & m_clock
SharedWeakUnionPointerType shared_weak_combo_pointer_type
Definition TaggedCache.h:68
mutex_type & peekMutex()
SharedPointerType fetch(key_type const &digest, Handler const &h)
Fetch an item from the cache.
auto insert(key_type const &key, T const &value) -> std::enable_if_t<!IsKeyCache, ReturnType >
Insert the element into the container.
std::vector< key_type > getKeys() const
bool canonicalize_replace_cache(key_type const &key, SharedPointerType const &data)
std::size_t size() const
Returns the number of items in the container.
int getCacheSize() const
SharedPointerType shared_pointer_type
Definition TaggedCache.h:69
std::thread sweepHelper(clock_type::time_point const &when_expire, clock_type::time_point const &now, typename KeyOnlyCacheType::map_type &partition, SweptPointersVector &, std::atomic< int > &allRemovals, std::lock_guard< std::recursive_mutex > const &)
SharedPointerType initialFetch(key_type const &key, std::lock_guard< mutex_type > const &l)
std::thread sweepHelper(clock_type::time_point const &when_expire, clock_type::time_point const &now, typename KeyValueCacheType::map_type &partition, SweptPointersVector &stuffToSweep, std::atomic< int > &allRemovals, std::lock_guard< std::recursive_mutex > const &)
std::uint64_t m_misses
std::conditional< IsKeyCache, KeyOnlyEntry, ValueEntry >::type Entry
double rate() const
Returns the fraction of cache hits.
auto insert(key_type const &key) -> std::enable_if_t< IsKeyCache, ReturnType >
clock_type::duration const m_target_age
TaggedCache(std::string const &name, int size, clock_type::duration expiration, clock_type &clock, beast::Journal journal, beast::insight::Collector::ptr const &collector=beast::insight::NullCollector::New())
std::uint64_t m_hits
int const m_target_size
int getTrackSize() const
TER valid(STTx const &tx, ReadView const &view, AccountID const &src, beast::Journal j)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition tokens.cpp:156
beast::insight::Gauge size
beast::insight::Hook hook
beast::insight::Gauge hit_rate
Stats(std::string const &prefix, Handler const &handler, beast::insight::Collector::ptr const &collector)