|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#pragma once |
| 19 | + |
| 20 | +#include <array> |
| 21 | +#include <chrono> |
| 22 | +#include <list> |
| 23 | +#include <memory> |
| 24 | +#include <mutex> |
| 25 | +#include <tuple> |
| 26 | +#include <unordered_map> |
| 27 | + |
| 28 | +namespace doris::cloud { |
| 29 | + |
| 30 | +// Sharded LRU Cache to reduce lock contention |
| 31 | +// KeyTuple: std::tuple type, corresponding to BasicKeyInfo::base_type in keys.h |
| 32 | +// ValuePB: protobuf message type |
| 33 | +template <typename KeyTuple, typename ValuePB, size_t NumShards = 16> |
| 34 | +class KvCache { |
| 35 | +public: |
| 36 | + explicit KvCache(size_t capacity, int64_t ttl_seconds = 0) |
| 37 | + : shard_capacity_(capacity / NumShards + 1), ttl_seconds_(ttl_seconds) { |
| 38 | + for (auto& shard : shards_) { |
| 39 | + shard = std::make_unique<Shard>(shard_capacity_, ttl_seconds); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // Query cache, returns true and fills value if hit |
| 44 | + bool get(const KeyTuple& key, ValuePB* value) { return get_shard(key)->get(key, value); } |
| 45 | + |
| 46 | + // Write to cache |
| 47 | + void put(const KeyTuple& key, const ValuePB& value) { get_shard(key)->put(key, value); } |
| 48 | + |
| 49 | + // Invalidate single entry |
| 50 | + void invalidate(const KeyTuple& key) { get_shard(key)->invalidate(key); } |
| 51 | + |
| 52 | + void clear() { |
| 53 | + for (auto& shard : shards_) { |
| 54 | + shard->clear(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + size_t size() const { |
| 59 | + size_t total = 0; |
| 60 | + for (const auto& shard : shards_) { |
| 61 | + total += shard->size(); |
| 62 | + } |
| 63 | + return total; |
| 64 | + } |
| 65 | + |
| 66 | +private: |
| 67 | + struct Entry { |
| 68 | + KeyTuple key; |
| 69 | + ValuePB value; |
| 70 | + int64_t expire_time; |
| 71 | + }; |
| 72 | + |
| 73 | + struct KeyHash { |
| 74 | + size_t operator()(const KeyTuple& k) const { |
| 75 | + return std::apply( |
| 76 | + [](const auto&... args) { |
| 77 | + size_t seed = 0; |
| 78 | + ((seed ^= std::hash<std::decay_t<decltype(args)>> {}(args) + 0x9e3779b9 + |
| 79 | + (seed << 6) + (seed >> 2)), |
| 80 | + ...); |
| 81 | + return seed; |
| 82 | + }, |
| 83 | + k); |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + class Shard { |
| 88 | + public: |
| 89 | + explicit Shard(size_t capacity, int64_t ttl_seconds) |
| 90 | + : capacity_(capacity), ttl_seconds_(ttl_seconds) {} |
| 91 | + |
| 92 | + bool get(const KeyTuple& key, ValuePB* value) { |
| 93 | + std::lock_guard lock(mu_); |
| 94 | + auto it = map_.find(key); |
| 95 | + if (it == map_.end()) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + // Check TTL expiration |
| 99 | + if (ttl_seconds_ > 0 && it->second->expire_time < now_seconds()) { |
| 100 | + list_.erase(it->second); |
| 101 | + map_.erase(it); |
| 102 | + return false; |
| 103 | + } |
| 104 | + list_.splice(list_.begin(), list_, it->second); |
| 105 | + *value = it->second->value; |
| 106 | + return true; |
| 107 | + } |
| 108 | + |
| 109 | + void put(const KeyTuple& key, const ValuePB& value) { |
| 110 | + std::lock_guard lock(mu_); |
| 111 | + int64_t expire_time = ttl_seconds_ > 0 ? now_seconds() + ttl_seconds_ : 0; |
| 112 | + auto it = map_.find(key); |
| 113 | + if (it != map_.end()) { |
| 114 | + it->second->value = value; |
| 115 | + it->second->expire_time = expire_time; |
| 116 | + list_.splice(list_.begin(), list_, it->second); |
| 117 | + return; |
| 118 | + } |
| 119 | + if (map_.size() >= capacity_) { |
| 120 | + map_.erase(list_.back().key); |
| 121 | + list_.pop_back(); |
| 122 | + } |
| 123 | + list_.push_front({key, value, expire_time}); |
| 124 | + map_[key] = list_.begin(); |
| 125 | + } |
| 126 | + |
| 127 | + void invalidate(const KeyTuple& key) { |
| 128 | + std::lock_guard lock(mu_); |
| 129 | + auto it = map_.find(key); |
| 130 | + if (it != map_.end()) { |
| 131 | + list_.erase(it->second); |
| 132 | + map_.erase(it); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + void clear() { |
| 137 | + std::lock_guard lock(mu_); |
| 138 | + map_.clear(); |
| 139 | + list_.clear(); |
| 140 | + } |
| 141 | + |
| 142 | + size_t size() const { |
| 143 | + std::lock_guard lock(mu_); |
| 144 | + return map_.size(); |
| 145 | + } |
| 146 | + |
| 147 | + private: |
| 148 | + static int64_t now_seconds() { |
| 149 | + return std::chrono::duration_cast<std::chrono::seconds>( |
| 150 | + std::chrono::steady_clock::now().time_since_epoch()) |
| 151 | + .count(); |
| 152 | + } |
| 153 | + |
| 154 | + size_t capacity_; |
| 155 | + int64_t ttl_seconds_; |
| 156 | + mutable std::mutex mu_; |
| 157 | + std::list<Entry> list_; |
| 158 | + std::unordered_map<KeyTuple, typename std::list<Entry>::iterator, KeyHash> map_; |
| 159 | + }; |
| 160 | + |
| 161 | + Shard* get_shard(const KeyTuple& key) { |
| 162 | + size_t hash = KeyHash {}(key); |
| 163 | + return shards_[hash % NumShards].get(); |
| 164 | + } |
| 165 | + |
| 166 | + size_t shard_capacity_; |
| 167 | + int64_t ttl_seconds_; |
| 168 | + std::array<std::unique_ptr<Shard>, NumShards> shards_; |
| 169 | +}; |
| 170 | + |
| 171 | +} // namespace doris::cloud |
0 commit comments