-
Notifications
You must be signed in to change notification settings - Fork 14
Antalya: Cache the list objects operation on object storage using a TTL + prefix matching cache implementation #743
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
Changes from 22 commits
3dc33f3
7e182c7
ef985c9
43c1383
989cfe0
157450d
d4af4ae
727b64d
68cbad7
00f58b3
67ccaf0
2b37e0c
0d6e343
0f605c4
3ed2349
f345b33
9242843
4e19b09
7a6eaec
8c4ea48
74b980c
4f55a75
303ee27
e6b379e
d7b50f4
6cfa510
be8c6a1
b60cb95
d91bf00
c6e53a1
f1c3591
14973d2
55ac0bc
e0e19a2
45af8a5
7266d92
8e78b28
2ed102d
28bfcfb
aab089c
dd5934e
0f5057e
27c4dea
d789d1e
fef71c0
6bfcb86
f68725a
7597da0
e7940af
f863a6e
cbfe36d
9092aba
057b0b5
49748c9
96cf2d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,7 @@ | |
| #include <Access/Common/AllowedClientHosts.h> | ||
| #include <Databases/DatabaseReplicated.h> | ||
| #include <Disks/ObjectStorages/IMetadataStorage.h> | ||
| #include <Storages/Cache/ObjectStorageListObjectsCache.h> | ||
| #include <Storages/StorageDistributed.h> | ||
| #include <Storages/StorageReplicatedMergeTree.h> | ||
| #include <Storages/Freeze.h> | ||
|
|
@@ -437,6 +438,12 @@ BlockIO InterpreterSystemQuery::execute() | |
| throw Exception(ErrorCodes::SUPPORT_IS_DISABLED, "The server was compiled without the support for Parquet"); | ||
| #endif | ||
| } | ||
| case Type::DROP_OBJECT_STORAGE_LIST_OBJECTS_CACHE: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is caching works only on Parquet files or generally on any S3 ListObject requests?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, copy and paste issues. Should be any :D
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| { | ||
| getContext()->checkAccess(AccessType::SYSTEM_DROP_OBJECT_STORAGE_LIST_OBJECTS_CACHE); | ||
| ObjectStorageListObjectsCache::instance().clear(); | ||
| break; | ||
| } | ||
| case Type::DROP_COMPILED_EXPRESSION_CACHE: | ||
| #if USE_EMBEDDED_COMPILER | ||
| getContext()->checkAccess(AccessType::SYSTEM_DROP_COMPILED_EXPRESSION_CACHE); | ||
|
|
@@ -1469,6 +1476,7 @@ AccessRightsElements InterpreterSystemQuery::getRequiredAccessForDDLOnCluster() | |
| case Type::DROP_SCHEMA_CACHE: | ||
| case Type::DROP_FORMAT_SCHEMA_CACHE: | ||
| case Type::DROP_PARQUET_METADATA_CACHE: | ||
| case Type::DROP_OBJECT_STORAGE_LIST_OBJECTS_CACHE: | ||
| case Type::DROP_S3_CLIENT_CACHE: | ||
| { | ||
| required_access.emplace_back(AccessType::SYSTEM_DROP_CACHE); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| #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()) | ||
ianton-ru marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| 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(const Key & key) | ||
| { | ||
| const auto & prefix = key.prefix; | ||
|
|
||
| auto best_match = cache.end(); | ||
| size_t best_length = 0; | ||
|
|
||
| std::vector<Key> to_remove; | ||
|
|
||
| for (auto it = cache.begin(); it != cache.end(); ++it) | ||
|
||
| { | ||
| const auto & candidate_bucket = it->first.bucket; | ||
| const auto & candidate_prefix = it->first.prefix; | ||
|
|
||
| if (candidate_bucket == key.bucket && prefix.starts_with(candidate_prefix)) | ||
| { | ||
| if (IsStaleFunction()(it->first)) | ||
| { | ||
| to_remove.push_back(it->first); | ||
| continue; | ||
| } | ||
|
|
||
| if (candidate_prefix.size() > best_length) | ||
| { | ||
| best_match = it; | ||
| best_length = candidate_prefix.size(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (const auto & k : to_remove) | ||
| BasePolicy::remove(k); | ||
|
|
||
| return best_match; | ||
| } | ||
| }; | ||
|
|
||
| ObjectStorageListObjectsCache::Key::Key( | ||
| const String & bucket_, | ||
| const String & prefix_, | ||
| const std::chrono::steady_clock::time_point & expires_at_, | ||
| std::optional<UUID> user_id_) | ||
| : bucket(bucket_), prefix(prefix_), expires_at(expires_at_), user_id(user_id_) {} | ||
|
|
||
| bool ObjectStorageListObjectsCache::Key::operator==(const Key & other) const | ||
| { | ||
| return 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.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) | ||
| { | ||
| weight += object->relative_path.capacity() + sizeof(ObjectMetadata); | ||
| } | ||
|
|
||
| return weight; | ||
| } | ||
|
|
||
| ObjectStorageListObjectsCache::ObjectStorageListObjectsCache() | ||
| : cache(std::make_unique<ObjectStorageListObjectsCachePolicy<Key, Value, KeyHasher, WeightFunction, IsStale>>()) | ||
| { | ||
| } | ||
|
|
||
| void ObjectStorageListObjectsCache::set( | ||
| const std::string & bucket, | ||
| const std::string & prefix, | ||
| const std::shared_ptr<Value> & value) | ||
| { | ||
| const auto key = Key{bucket, prefix, std::chrono::steady_clock::now() + std::chrono::seconds(ttl_in_seconds)}; | ||
ianton-ru marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| cache.set(key, value); | ||
| } | ||
|
|
||
| void ObjectStorageListObjectsCache::clear() | ||
| { | ||
| cache.clear(); | ||
| } | ||
|
|
||
| ObjectStorageListObjectsCache::Cache::MappedPtr ObjectStorageListObjectsCache::get(const String & bucket, const String & prefix, bool filter_by_prefix) | ||
| { | ||
| const auto input_key = Key{bucket, prefix}; | ||
| auto pair = cache.getWithKey(input_key); | ||
|
|
||
| if (!pair) | ||
| { | ||
| ProfileEvents::increment(ProfileEvents::ObjectStorageListObjectsCacheMisses); | ||
| return {}; | ||
| } | ||
|
|
||
| ProfileEvents::increment(ProfileEvents::ObjectStorageListObjectsCacheHits); | ||
|
|
||
| if (pair->key == input_key) | ||
| { | ||
| ProfileEvents::increment(ProfileEvents::ObjectStorageListObjectsCacheExactMatchHits); | ||
| return pair->mapped; | ||
| } | ||
|
|
||
| ProfileEvents::increment(ProfileEvents::ObjectStorageListObjectsCachePrefixMatchHits); | ||
|
|
||
| if (!filter_by_prefix) | ||
| { | ||
| return pair->mapped; | ||
| } | ||
|
|
||
| auto filtered_objects = std::make_shared<std::vector<ObjectInfoPtr>>(); | ||
| filtered_objects->reserve(pair->mapped->size()); | ||
|
|
||
| for (const auto & object : *pair->mapped) | ||
| { | ||
| if (object->relative_path.starts_with(input_key.prefix)) | ||
ianton-ru marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| 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; | ||
| } | ||
|
|
||
| } | ||
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.
?