forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 8
25.3 Antalya port of #743 - Object storage list objects cache #805
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 all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
62f5344
Merge pull request #743 from Altinity/list_objects_cache
Enmk fd2d4bc
Merge branch 'antalya-25.3' into list_objects_object_storage_cache_25.3
arthurpassos d485e04
Merge branch 'antalya-25.3' into list_objects_object_storage_cache_25.3
arthurpassos 5c06781
Fixed merge hiccup
Enmk 5cd76fc
Fixed another merge hiccup
Enmk 84e3805
Merge branch 'antalya-25.3' into list_objects_object_storage_cache_25.3
arthurpassos 6ca0071
Merge branch 'antalya-25.3' into list_objects_object_storage_cache_25.3
arthurpassos 53e50ee
Update Settings.cpp
ianton-ru e1e8671
Fixed stateless/01271_show_privileges
Enmk 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(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.
Wrong merge here, as result
object_storage_remote_initiator
goes into description.