|
| 1 | +/* |
| 2 | + * Copyright (c) 2008-2023, Hazelcast, Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#pragma once |
| 17 | + |
| 18 | +#include <queue> |
| 19 | +#include "hazelcast/util/SynchronizedMap.h" |
| 20 | +#include "hazelcast/util/export.h" |
| 21 | + |
| 22 | +namespace hazelcast { |
| 23 | +namespace client { |
| 24 | +namespace sql { |
| 25 | +namespace impl { |
| 26 | + |
| 27 | +/** |
| 28 | + * Implementation of an LRU cache optimized for read-heavy use cases. |
| 29 | + * <p> |
| 30 | + * It stores the entries in a {SynchronizedMap}, along with the last |
| 31 | + * access time. It allows the size to grow beyond the capacity, up to |
| 32 | + * `cleanup_threshold_`, at which point the inserting thread will remove a batch |
| 33 | + * of the eldest items in two passes. |
| 34 | + * <p> |
| 35 | + * The cleanup process isn't synchronized to guarantee that the capacity is not |
| 36 | + * exceeded. The cache is available during the cleanup for reads and writes. If |
| 37 | + * there's a large number of writes by many threads, the one thread doing the |
| 38 | + * cleanup might not be quick enough and there's no upper bound on the actual |
| 39 | + * size of the cache. This is done to optimize the happy path when the keys fit |
| 40 | + * into the cache. |
| 41 | + */ |
| 42 | +template<typename K, typename V> |
| 43 | +class read_optimized_lru_cache |
| 44 | +{ |
| 45 | +public: |
| 46 | + /** |
| 47 | + * @param capacity Capacity of the cache |
| 48 | + * @param cleanup_threshold The size at which the cache will clean up oldest |
| 49 | + * entries in batch. `cleanup_threshold - capacity` entries will be |
| 50 | + * removed |
| 51 | + * @throws exception::illegal_argument if capacity is smaller or equal to 0, |
| 52 | + * or if the cleanup_threshold is smaller than capacity |
| 53 | + */ |
| 54 | + explicit read_optimized_lru_cache(const uint32_t capacity, |
| 55 | + const uint32_t cleanup_threshold) |
| 56 | + { |
| 57 | + if (capacity == 0) { |
| 58 | + BOOST_THROW_EXCEPTION( |
| 59 | + client::exception::illegal_argument("capacity == 0")); |
| 60 | + } |
| 61 | + if (cleanup_threshold <= capacity) { |
| 62 | + BOOST_THROW_EXCEPTION(client::exception::illegal_argument( |
| 63 | + "cleanupThreshold <= capacity")); |
| 64 | + } |
| 65 | + |
| 66 | + capacity_ = capacity; |
| 67 | + cleanup_threshold_ = cleanup_threshold; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @param key the key of the cache entry |
| 72 | + * @param default_value the default value if the key is not cached. |
| 73 | + * @returns Returns the value to which the specified key is cached, |
| 74 | + * or default value if this cache contains no mapping for the key. |
| 75 | + */ |
| 76 | + std::shared_ptr<V> get_or_default(const K& key, |
| 77 | + const std::shared_ptr<V>& default_value) |
| 78 | + { |
| 79 | + const auto existing_value = get(key); |
| 80 | + return (existing_value != nullptr) ? existing_value : default_value; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @param key the key of the cache entry |
| 85 | + * Returns the value to which the specified key is cached, |
| 86 | + * or {@code null} if this cache contains no mapping for the key. |
| 87 | + * @returns Returns the value to which the specified key is cached |
| 88 | + */ |
| 89 | + std::shared_ptr<V> get(const K& key) |
| 90 | + { |
| 91 | + auto value_from_cache = cache_.get(key); |
| 92 | + if (value_from_cache == nullptr) { |
| 93 | + return nullptr; |
| 94 | + } |
| 95 | + value_from_cache->touch(); |
| 96 | + return std::make_shared<int32_t>(value_from_cache->value_); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @param key the key of the cache entry |
| 101 | + * @param value the value of the cache entry |
| 102 | + * @throws exception::illegal_argument if the value equals to nullptr |
| 103 | + */ |
| 104 | + void put(const K& key, const std::shared_ptr<V>& value) |
| 105 | + { |
| 106 | + if (value == nullptr) { |
| 107 | + BOOST_THROW_EXCEPTION(client::exception::illegal_argument( |
| 108 | + "Null values are disallowed")); |
| 109 | + } |
| 110 | + |
| 111 | + auto old_value = |
| 112 | + cache_.put(key, std::make_shared<value_and_timestamp<V>>(*value)); |
| 113 | + if (old_value == nullptr && cache_.size() > cleanup_threshold_) { |
| 114 | + do_cleanup(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * @param key the key of the cache entry |
| 120 | + * Removes the cached value for the given key |
| 121 | + */ |
| 122 | + void remove(const K& key) { cache_.remove(key); } |
| 123 | + |
| 124 | +protected: |
| 125 | + /** |
| 126 | + * Helper class to hold the value with timestamp. |
| 127 | + */ |
| 128 | + template<typename T> |
| 129 | + class value_and_timestamp |
| 130 | + { |
| 131 | + public: |
| 132 | + const T value_; |
| 133 | + int64_t timestamp_; |
| 134 | + |
| 135 | + value_and_timestamp(T value) |
| 136 | + : value_(value) |
| 137 | + { |
| 138 | + touch(); |
| 139 | + } |
| 140 | + |
| 141 | + void touch() { timestamp_ = util::current_time_nanos(); } |
| 142 | + }; |
| 143 | + |
| 144 | + util::SynchronizedMap<K, value_and_timestamp<V>> cache_; |
| 145 | + |
| 146 | +private: |
| 147 | + /** |
| 148 | + * Cleans the cache |
| 149 | + */ |
| 150 | + void do_cleanup() |
| 151 | + { |
| 152 | + bool expected = false; |
| 153 | + // if no thread is cleaning up, we'll do it |
| 154 | + if (!cleanup_lock_.compare_exchange_strong(expected, true)) { |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + util::finally release_lock( |
| 159 | + [this]() { this->cleanup_lock_.store(false); }); |
| 160 | + |
| 161 | + if (capacity_ >= cache_.size()) { |
| 162 | + // this can happen if the cache is concurrently modified |
| 163 | + return; |
| 164 | + } |
| 165 | + auto entries_to_remove = cache_.size() - capacity_; |
| 166 | + |
| 167 | + /*max heap*/ |
| 168 | + std::priority_queue<int64_t> oldest_timestamps; |
| 169 | + |
| 170 | + // 1st pass |
| 171 | + const auto values = cache_.values(); |
| 172 | + for (const auto& value_and_timestamp : values) { |
| 173 | + oldest_timestamps.push(value_and_timestamp->timestamp_); |
| 174 | + if (oldest_timestamps.size() > entries_to_remove) { |
| 175 | + oldest_timestamps.pop(); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + // find out the highest value in the queue - the value, below which |
| 180 | + // entries will be removed |
| 181 | + if (oldest_timestamps.empty()) { |
| 182 | + // this can happen if the cache is concurrently modified |
| 183 | + return; |
| 184 | + } |
| 185 | + int64_t remove_threshold = oldest_timestamps.top(); |
| 186 | + oldest_timestamps.pop(); |
| 187 | + |
| 188 | + // 2nd pass |
| 189 | + cache_.remove_values_if( |
| 190 | + [remove_threshold](const value_and_timestamp<V>& v) -> bool { |
| 191 | + return (v.timestamp_ <= remove_threshold); |
| 192 | + }); |
| 193 | + } |
| 194 | + |
| 195 | + std::atomic<bool> cleanup_lock_{ false }; |
| 196 | + uint32_t capacity_; |
| 197 | + uint32_t cleanup_threshold_; |
| 198 | +}; |
| 199 | + |
| 200 | +} // namespace impl |
| 201 | +} // namespace sql |
| 202 | +} // namespace client |
| 203 | +} // namespace hazelcast |
0 commit comments