forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 8
Rendezvous hashing filesystem cache #709
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
8 commits
Select commit
Hold shift + click to select a range
b5b7126
Change object storage cluster table functions to prefer specific repl…
adikus 7d96b64
Improve object storage cache locality with rendezvous hashing
ianton-ru 0faded9
Properly initialize replica info wherever we use task iterator
adikus 763c860
Fixes after review
ianton-ru cdfdd26
Fix after rebase
ianton-ru 420b584
Merge branch 'antalya' into feature/rendezvous-hashing-filesystem-cache
MyroTk d6198a4
Add test for cache locality
ianton-ru e4b51d1
Merge branch 'antalya' into feature/rendezvous-hashing-filesystem-cache
ianton-ru 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
178 changes: 178 additions & 0 deletions
178
src/Storages/ObjectStorage/StorageObjectStorageStableTaskDistributor.cpp
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,178 @@ | ||
#include "StorageObjectStorageStableTaskDistributor.h" | ||
#include <Common/SipHash.h> | ||
#include <consistent_hashing.h> | ||
#include <optional> | ||
|
||
namespace DB | ||
{ | ||
|
||
StorageObjectStorageStableTaskDistributor::StorageObjectStorageStableTaskDistributor( | ||
std::shared_ptr<IObjectIterator> iterator_, | ||
std::optional<std::vector<std::string>> ids_of_nodes_) | ||
: iterator(std::move(iterator_)) | ||
, connection_to_files(ids_of_nodes_.has_value() ? ids_of_nodes_.value().size() : 1) | ||
, ids_of_nodes(ids_of_nodes_) | ||
, iterator_exhausted(false) | ||
{ | ||
} | ||
|
||
std::optional<String> StorageObjectStorageStableTaskDistributor::getNextTask(size_t number_of_current_replica) | ||
{ | ||
LOG_TRACE( | ||
log, | ||
"Received a new connection from replica {} looking for a file", | ||
number_of_current_replica | ||
); | ||
|
||
// 1. Check pre-queued files first | ||
if (auto file = getPreQueuedFile(number_of_current_replica)) | ||
return file; | ||
|
||
// 2. Try to find a matching file from the iterator | ||
if (auto file = getMatchingFileFromIterator(number_of_current_replica)) | ||
return file; | ||
|
||
// 3. Process unprocessed files if iterator is exhausted | ||
return getAnyUnprocessedFile(number_of_current_replica); | ||
} | ||
|
||
size_t StorageObjectStorageStableTaskDistributor::getReplicaForFile(const String & file_path) | ||
{ | ||
if (!ids_of_nodes.has_value()) | ||
throw Exception(ErrorCodes::LOGICAL_ERROR, "No list of nodes inside Task Distributer."); | ||
|
||
const auto & ids_of_nodes_value = ids_of_nodes.value(); | ||
size_t nodes_count = ids_of_nodes_value.size(); | ||
|
||
/// Trivial case | ||
if (nodes_count < 2) | ||
return 0; | ||
|
||
/// Rendezvous hashing | ||
size_t best_id = 0; | ||
UInt64 best_weight = sipHash64(ids_of_nodes_value[0] + file_path); | ||
for (size_t id = 1; id < nodes_count; ++id) | ||
{ | ||
UInt64 weight = sipHash64(ids_of_nodes_value[id] + file_path); | ||
if (weight > best_weight) | ||
{ | ||
best_weight = weight; | ||
best_id = id; | ||
} | ||
} | ||
return best_id; | ||
} | ||
|
||
std::optional<String> StorageObjectStorageStableTaskDistributor::getPreQueuedFile(size_t number_of_current_replica) | ||
{ | ||
std::lock_guard lock(mutex); | ||
|
||
auto & files = connection_to_files[number_of_current_replica]; | ||
|
||
while (!files.empty()) | ||
{ | ||
String next_file = files.back(); | ||
files.pop_back(); | ||
|
||
auto it = unprocessed_files.find(next_file); | ||
if (it == unprocessed_files.end()) | ||
continue; | ||
|
||
unprocessed_files.erase(it); | ||
|
||
LOG_TRACE( | ||
log, | ||
"Assigning pre-queued file {} to replica {}", | ||
next_file, | ||
number_of_current_replica | ||
); | ||
|
||
return next_file; | ||
} | ||
|
||
return std::nullopt; | ||
} | ||
|
||
std::optional<String> StorageObjectStorageStableTaskDistributor::getMatchingFileFromIterator(size_t number_of_current_replica) | ||
{ | ||
{ | ||
std::lock_guard lock(mutex); | ||
if (iterator_exhausted) | ||
return std::nullopt; | ||
} | ||
|
||
while (true) | ||
{ | ||
ObjectInfoPtr object_info; | ||
|
||
{ | ||
std::lock_guard lock(mutex); | ||
object_info = iterator->next(0); | ||
|
||
if (!object_info) | ||
{ | ||
iterator_exhausted = true; | ||
break; | ||
} | ||
} | ||
|
||
String file_path; | ||
|
||
auto archive_object_info = std::dynamic_pointer_cast<StorageObjectStorageSource::ArchiveIterator::ObjectInfoInArchive>(object_info); | ||
if (archive_object_info) | ||
{ | ||
file_path = archive_object_info->getPathToArchive(); | ||
} | ||
else | ||
{ | ||
file_path = object_info->getPath(); | ||
} | ||
|
||
size_t file_replica_idx = getReplicaForFile(file_path); | ||
if (file_replica_idx == number_of_current_replica) | ||
{ | ||
LOG_TRACE( | ||
log, | ||
"Found file {} for replica {}", | ||
file_path, | ||
number_of_current_replica | ||
); | ||
|
||
return file_path; | ||
} | ||
|
||
// Queue file for its assigned replica | ||
{ | ||
std::lock_guard lock(mutex); | ||
unprocessed_files.insert(file_path); | ||
connection_to_files[file_replica_idx].push_back(file_path); | ||
} | ||
} | ||
|
||
return std::nullopt; | ||
} | ||
|
||
std::optional<String> StorageObjectStorageStableTaskDistributor::getAnyUnprocessedFile(size_t number_of_current_replica) | ||
{ | ||
std::lock_guard lock(mutex); | ||
|
||
if (!unprocessed_files.empty()) | ||
{ | ||
auto it = unprocessed_files.begin(); | ||
String next_file = *it; | ||
unprocessed_files.erase(it); | ||
|
||
LOG_TRACE( | ||
log, | ||
"Iterator exhausted. Assigning unprocessed file {} to replica {}", | ||
next_file, | ||
number_of_current_replica | ||
); | ||
|
||
return next_file; | ||
} | ||
|
||
return std::nullopt; | ||
} | ||
|
||
} |
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.
In which case would it not have a value? And why is
empty != nullopt
?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.
It must always have value, in
ReadFromCluster::createExtension
it is called withids_of_nodes
always.About nullopt - it is the same:
First and second are synonyms - https://en.cppreference.com/w/cpp/utility/optional/operator_bool
Third - because comparation with nullopt calls bool operator:
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.
Sorry, I didn't explain my question correctly. I was asking why not just have a vector and check if it is empty instead of an optional of vector
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.
What I mean is that instead of doing:
you simply do:
I mean, it is ok to keep it as optional, just thought it could be simpler