-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[Enhancement](ms) Add sharded LRU cache for tablet index metadata to reduce FDB IO #61666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
wyxxxcat
wants to merge
1
commit into
apache:master
Choose a base branch
from
wyxxxcat:ms_lru_cache
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+426
−0
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <array> | ||
| #include <chrono> | ||
| #include <list> | ||
| #include <memory> | ||
| #include <mutex> | ||
| #include <tuple> | ||
| #include <unordered_map> | ||
|
|
||
| namespace doris::cloud { | ||
|
|
||
| // Sharded LRU Cache to reduce lock contention | ||
| // KeyTuple: std::tuple type, corresponding to BasicKeyInfo::base_type in keys.h | ||
| // ValuePB: protobuf message type | ||
| template <typename KeyTuple, typename ValuePB, size_t NumShards = 16> | ||
| class KvCache { | ||
| public: | ||
| explicit KvCache(size_t capacity, int64_t ttl_seconds = 0) | ||
| : shard_capacity_(capacity / NumShards + 1), ttl_seconds_(ttl_seconds) { | ||
| for (auto& shard : shards_) { | ||
| shard = std::make_unique<Shard>(shard_capacity_, ttl_seconds); | ||
| } | ||
| } | ||
|
|
||
| // Query cache, returns true and fills value if hit | ||
| bool get(const KeyTuple& key, ValuePB* value) { return get_shard(key)->get(key, value); } | ||
|
|
||
| // Write to cache | ||
| void put(const KeyTuple& key, const ValuePB& value) { get_shard(key)->put(key, value); } | ||
|
|
||
| // Invalidate single entry | ||
| void invalidate(const KeyTuple& key) { get_shard(key)->invalidate(key); } | ||
|
|
||
| void clear() { | ||
| for (auto& shard : shards_) { | ||
| shard->clear(); | ||
| } | ||
| } | ||
|
|
||
| size_t size() const { | ||
| size_t total = 0; | ||
| for (const auto& shard : shards_) { | ||
| total += shard->size(); | ||
| } | ||
| return total; | ||
| } | ||
|
|
||
| private: | ||
| struct Entry { | ||
| KeyTuple key; | ||
| ValuePB value; | ||
| int64_t expire_time; | ||
| }; | ||
|
|
||
| struct KeyHash { | ||
| size_t operator()(const KeyTuple& k) const { | ||
| return std::apply( | ||
| [](const auto&... args) { | ||
| size_t seed = 0; | ||
| ((seed ^= std::hash<std::decay_t<decltype(args)>> {}(args) + 0x9e3779b9 + | ||
| (seed << 6) + (seed >> 2)), | ||
| ...); | ||
| return seed; | ||
| }, | ||
| k); | ||
| } | ||
| }; | ||
|
|
||
| class Shard { | ||
| public: | ||
| explicit Shard(size_t capacity, int64_t ttl_seconds) | ||
| : capacity_(capacity), ttl_seconds_(ttl_seconds) {} | ||
|
|
||
| bool get(const KeyTuple& key, ValuePB* value) { | ||
| std::lock_guard lock(mu_); | ||
| auto it = map_.find(key); | ||
| if (it == map_.end()) { | ||
| return false; | ||
| } | ||
| // Check TTL expiration | ||
| if (ttl_seconds_ > 0 && it->second->expire_time < now_seconds()) { | ||
| list_.erase(it->second); | ||
| map_.erase(it); | ||
| return false; | ||
| } | ||
| list_.splice(list_.begin(), list_, it->second); | ||
| *value = it->second->value; | ||
| return true; | ||
| } | ||
|
|
||
| void put(const KeyTuple& key, const ValuePB& value) { | ||
| std::lock_guard lock(mu_); | ||
| int64_t expire_time = ttl_seconds_ > 0 ? now_seconds() + ttl_seconds_ : 0; | ||
| auto it = map_.find(key); | ||
| if (it != map_.end()) { | ||
| it->second->value = value; | ||
| it->second->expire_time = expire_time; | ||
| list_.splice(list_.begin(), list_, it->second); | ||
| return; | ||
| } | ||
| if (map_.size() >= capacity_) { | ||
| map_.erase(list_.back().key); | ||
| list_.pop_back(); | ||
| } | ||
| list_.push_front({key, value, expire_time}); | ||
| map_[key] = list_.begin(); | ||
| } | ||
|
|
||
| void invalidate(const KeyTuple& key) { | ||
| std::lock_guard lock(mu_); | ||
| auto it = map_.find(key); | ||
| if (it != map_.end()) { | ||
| list_.erase(it->second); | ||
| map_.erase(it); | ||
| } | ||
| } | ||
|
|
||
| void clear() { | ||
| std::lock_guard lock(mu_); | ||
| map_.clear(); | ||
| list_.clear(); | ||
| } | ||
|
|
||
| size_t size() const { | ||
| std::lock_guard lock(mu_); | ||
| return map_.size(); | ||
| } | ||
|
|
||
| private: | ||
| static int64_t now_seconds() { | ||
| return std::chrono::duration_cast<std::chrono::seconds>( | ||
| std::chrono::steady_clock::now().time_since_epoch()) | ||
| .count(); | ||
| } | ||
|
|
||
| size_t capacity_; | ||
| int64_t ttl_seconds_; | ||
| mutable std::mutex mu_; | ||
| std::list<Entry> list_; | ||
| std::unordered_map<KeyTuple, typename std::list<Entry>::iterator, KeyHash> map_; | ||
| }; | ||
|
|
||
| Shard* get_shard(const KeyTuple& key) { | ||
| size_t hash = KeyHash {}(key); | ||
| return shards_[hash % NumShards].get(); | ||
| } | ||
|
|
||
| size_t shard_capacity_; | ||
| int64_t ttl_seconds_; | ||
| std::array<std::unique_ptr<Shard>, NumShards> shards_; | ||
| }; | ||
|
|
||
| } // namespace doris::cloud |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "common/kv_cache.h" | ||
| #include "gen_cpp/cloud.pb.h" | ||
|
|
||
| namespace doris::cloud { | ||
|
|
||
| struct CacheConfig { | ||
| size_t tablet_index_capacity = 10000; | ||
| int64_t tablet_index_ttl_seconds = 0; | ||
| }; | ||
|
|
||
| class KvCacheManager { | ||
| public: | ||
| using TabletIndexCache = KvCache<std::tuple<std::string, int64_t>, TabletIndexPB>; | ||
|
|
||
| explicit KvCacheManager(const CacheConfig& config) | ||
| : tablet_index_cache_(std::make_unique<TabletIndexCache>( | ||
| config.tablet_index_capacity, config.tablet_index_ttl_seconds)) {} | ||
|
|
||
| TabletIndexCache* tablet_index_cache() { return tablet_index_cache_.get(); } | ||
|
|
||
| private: | ||
| std::unique_ptr<TabletIndexCache> tablet_index_cache_; | ||
| }; | ||
|
|
||
| } // namespace doris::cloud |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is already a lru cache in be, we should use it.