forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 18
Antalya 26.1 - Forward port of list objects cache #1040 #1405
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5bb0d48
Merge pull request #1040 from Altinity/fp_antalya_25_8_list_objects_c…
Enmk fbe10c0
settings history
arthurpassos 127db00
fix header
arthurpassos 79d5de4
build fixes with some nasty workarounds
arthurpassos 16031ce
fix list objects cache
arthurpassos 988de0d
thx ai
arthurpassos 2f7ed9f
Merge branch 'antalya-26.1' into fp_antalya_26_1_list_objects_cache
CarlosFelipeOR 7776586
Merge branch 'antalya-26.1' into fp_antalya_26_1_list_objects_cache
zvonand 8b171dc
Merge branch 'antalya-26.1' into fp_antalya_26_1_list_objects_cache
zvonand d0f05e4
integrate list objects cache with with_tags
arthurpassos 4440dd6
Merge branch 'antalya-26.1' into fp_antalya_26_1_list_objects_cache
CarlosFelipeOR e2f1b6d
Merge branch 'antalya-26.1' into fp_antalya_26_1_list_objects_cache
arthurpassos b94329e
possibly fix build
arthurpassos 6ff8782
possibly fix build
arthurpassos 68ba3dd
Merge branch 'antalya-26.1' into fp_antalya_26_1_list_objects_cache
arthurpassos 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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| #include <Storages/Cache/ObjectStorageListObjectsCache.h> | ||
| #include <Common/TTLCachePolicy.h> | ||
| #include <Common/ProfileEvents.h> | ||
| #include <boost/functional/hash.hpp> | ||
|
|
||
| namespace ProfileEvents | ||
| { | ||
| extern const Event ObjectStorageListObjectsCacheHits; | ||
| extern const Event ObjectStorageListObjectsCacheMisses; | ||
| extern const Event ObjectStorageListObjectsCacheExactMatchHits; | ||
| extern const Event ObjectStorageListObjectsCachePrefixMatchHits; | ||
| } | ||
|
|
||
| namespace DB | ||
| { | ||
|
|
||
| template <typename Key, typename Mapped, typename HashFunction, typename WeightFunction, typename IsStaleFunction> | ||
| class ObjectStorageListObjectsCachePolicy : public TTLCachePolicy<Key, Mapped, HashFunction, WeightFunction, IsStaleFunction> | ||
| { | ||
| public: | ||
| using BasePolicy = TTLCachePolicy<Key, Mapped, HashFunction, WeightFunction, IsStaleFunction>; | ||
| using typename BasePolicy::MappedPtr; | ||
| using typename BasePolicy::KeyMapped; | ||
| using BasePolicy::cache; | ||
|
|
||
| ObjectStorageListObjectsCachePolicy() | ||
| : BasePolicy(CurrentMetrics::end(), CurrentMetrics::end(), std::make_unique<NoCachePolicyUserQuota>()) | ||
| { | ||
| } | ||
|
|
||
| std::optional<KeyMapped> getWithKey(const Key & key) override | ||
| { | ||
| if (const auto it = cache.find(key); it != cache.end()) | ||
| { | ||
| if (!IsStaleFunction()(it->first)) | ||
| { | ||
| return std::make_optional<KeyMapped>({it->first, it->second}); | ||
| } | ||
| // found a stale entry, remove it but don't return. We still want to perform the prefix matching search | ||
| BasePolicy::remove(it->first); | ||
| } | ||
|
|
||
| if (const auto it = findBestMatchingPrefixAndRemoveExpiredEntries(key); it != cache.end()) | ||
| { | ||
| return std::make_optional<KeyMapped>({it->first, it->second}); | ||
| } | ||
|
|
||
| return std::nullopt; | ||
| } | ||
|
|
||
| private: | ||
| auto findBestMatchingPrefixAndRemoveExpiredEntries(Key key) | ||
| { | ||
| while (!key.prefix.empty()) | ||
| { | ||
| if (const auto it = cache.find(key); it != cache.end()) | ||
| { | ||
| if (IsStaleFunction()(it->first)) | ||
| { | ||
| BasePolicy::remove(it->first); | ||
| } | ||
| else | ||
| { | ||
| return it; | ||
| } | ||
| } | ||
|
|
||
| key.prefix.pop_back(); | ||
| } | ||
|
|
||
| return cache.end(); | ||
| } | ||
| }; | ||
|
|
||
| ObjectStorageListObjectsCache::Key::Key( | ||
| const String & storage_description_, | ||
| const String & bucket_, | ||
| const String & prefix_, | ||
| const std::chrono::steady_clock::time_point & expires_at_, | ||
| std::optional<UUID> user_id_) | ||
| : storage_description(storage_description_), bucket(bucket_), prefix(prefix_), expires_at(expires_at_), user_id(user_id_) {} | ||
|
|
||
| bool ObjectStorageListObjectsCache::Key::operator==(const Key & other) const | ||
| { | ||
| return storage_description == other.storage_description && bucket == other.bucket && prefix == other.prefix; | ||
| } | ||
|
|
||
| size_t ObjectStorageListObjectsCache::KeyHasher::operator()(const Key & key) const | ||
| { | ||
| std::size_t seed = 0; | ||
|
|
||
| boost::hash_combine(seed, key.storage_description); | ||
| boost::hash_combine(seed, key.bucket); | ||
| boost::hash_combine(seed, key.prefix); | ||
|
|
||
| return seed; | ||
| } | ||
|
|
||
| bool ObjectStorageListObjectsCache::IsStale::operator()(const Key & key) const | ||
| { | ||
| return key.expires_at < std::chrono::steady_clock::now(); | ||
| } | ||
|
|
||
| size_t ObjectStorageListObjectsCache::WeightFunction::operator()(const Value & value) const | ||
| { | ||
| std::size_t weight = 0; | ||
|
|
||
| for (const auto & object : value) | ||
| { | ||
| const auto object_metadata = object->metadata; | ||
| weight += object->relative_path.capacity() + sizeof(object_metadata); | ||
|
|
||
| // variable size | ||
| if (object_metadata) | ||
| { | ||
| weight += object_metadata->etag.capacity(); | ||
| weight += object_metadata->attributes.size() * (sizeof(std::string) * 2); | ||
|
|
||
| for (const auto & [k, v] : object_metadata->attributes) | ||
| { | ||
| weight += k.capacity() + v.capacity(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return weight; | ||
| } | ||
|
|
||
| ObjectStorageListObjectsCache::ObjectStorageListObjectsCache() | ||
| : cache(std::make_unique<ObjectStorageListObjectsCachePolicy<Key, Value, KeyHasher, WeightFunction, IsStale>>()) | ||
| { | ||
| } | ||
|
|
||
| void ObjectStorageListObjectsCache::set( | ||
| const Key & key, | ||
| const std::shared_ptr<Value> & value) | ||
| { | ||
| auto key_with_ttl = key; | ||
| key_with_ttl.expires_at = std::chrono::steady_clock::now() + std::chrono::seconds(ttl_in_seconds); | ||
|
|
||
| cache.set(key_with_ttl, value); | ||
| } | ||
|
|
||
| void ObjectStorageListObjectsCache::clear() | ||
| { | ||
| cache.clear(); | ||
| } | ||
|
|
||
| std::optional<ObjectStorageListObjectsCache::Value> ObjectStorageListObjectsCache::get(const Key & key, bool filter_by_prefix) | ||
| { | ||
| const auto pair = cache.getWithKey(key); | ||
|
|
||
| if (!pair) | ||
| { | ||
| ProfileEvents::increment(ProfileEvents::ObjectStorageListObjectsCacheMisses); | ||
| return {}; | ||
| } | ||
|
|
||
| ProfileEvents::increment(ProfileEvents::ObjectStorageListObjectsCacheHits); | ||
|
|
||
| if (pair->key == key) | ||
| { | ||
| ProfileEvents::increment(ProfileEvents::ObjectStorageListObjectsCacheExactMatchHits); | ||
| return *pair->mapped; | ||
| } | ||
|
|
||
| ProfileEvents::increment(ProfileEvents::ObjectStorageListObjectsCachePrefixMatchHits); | ||
|
|
||
| if (!filter_by_prefix) | ||
| { | ||
| return *pair->mapped; | ||
| } | ||
|
|
||
| Value filtered_objects; | ||
|
|
||
| filtered_objects.reserve(pair->mapped->size()); | ||
|
|
||
| for (const auto & object : *pair->mapped) | ||
| { | ||
| if (object->relative_path.starts_with(key.prefix)) | ||
| { | ||
| filtered_objects.push_back(object); | ||
| } | ||
| } | ||
|
|
||
| return filtered_objects; | ||
| } | ||
|
|
||
| void ObjectStorageListObjectsCache::setMaxSizeInBytes(std::size_t size_in_bytes_) | ||
| { | ||
| cache.setMaxSizeInBytes(size_in_bytes_); | ||
| } | ||
|
|
||
| void ObjectStorageListObjectsCache::setMaxCount(std::size_t count) | ||
| { | ||
| cache.setMaxCount(count); | ||
| } | ||
|
|
||
| void ObjectStorageListObjectsCache::setTTL(std::size_t ttl_in_seconds_) | ||
| { | ||
| ttl_in_seconds = ttl_in_seconds_; | ||
| } | ||
|
|
||
| ObjectStorageListObjectsCache & ObjectStorageListObjectsCache::instance() | ||
| { | ||
| static ObjectStorageListObjectsCache instance; | ||
| return instance; | ||
| } | ||
|
|
||
| } | ||
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.
The server setting says TTL
0means unlimited, butObjectStorageListObjectsCache::setalways writesexpires_at = now + seconds(ttl_in_seconds). Withttl_in_seconds == 0, entries expire immediately and are treated as stale on subsequent lookups, effectively disabling caching instead of making entries non-expiring. Handle0as a special case (e.g., max time point) to match the documented setting behavior.Useful? React with 👍 / 👎.
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.
Fixed - I am ashamed, thanks machine