|
| 1 | +#include "StorageObjectStorageStableTaskDistributor.h" |
| 2 | +#include <Common/SipHash.h> |
| 3 | +#include <consistent_hashing.h> |
| 4 | +#include <optional> |
| 5 | + |
| 6 | +namespace DB |
| 7 | +{ |
| 8 | + |
| 9 | +StorageObjectStorageStableTaskDistributor::StorageObjectStorageStableTaskDistributor( |
| 10 | + std::shared_ptr<IObjectIterator> iterator_, |
| 11 | + std::optional<std::vector<std::string>> ids_of_nodes_) |
| 12 | + : iterator(std::move(iterator_)) |
| 13 | + , connection_to_files(ids_of_nodes_.has_value() ? ids_of_nodes_.value().size() : 1) |
| 14 | + , ids_of_nodes(ids_of_nodes_) |
| 15 | + , iterator_exhausted(false) |
| 16 | +{ |
| 17 | +} |
| 18 | + |
| 19 | +std::optional<String> StorageObjectStorageStableTaskDistributor::getNextTask(size_t number_of_current_replica) |
| 20 | +{ |
| 21 | + LOG_TRACE( |
| 22 | + log, |
| 23 | + "Received a new connection from replica {} looking for a file", |
| 24 | + number_of_current_replica |
| 25 | + ); |
| 26 | + |
| 27 | + // 1. Check pre-queued files first |
| 28 | + if (auto file = getPreQueuedFile(number_of_current_replica)) |
| 29 | + return file; |
| 30 | + |
| 31 | + // 2. Try to find a matching file from the iterator |
| 32 | + if (auto file = getMatchingFileFromIterator(number_of_current_replica)) |
| 33 | + return file; |
| 34 | + |
| 35 | + // 3. Process unprocessed files if iterator is exhausted |
| 36 | + return getAnyUnprocessedFile(number_of_current_replica); |
| 37 | +} |
| 38 | + |
| 39 | +size_t StorageObjectStorageStableTaskDistributor::getReplicaForFile(const String & file_path) |
| 40 | +{ |
| 41 | + if (!ids_of_nodes.has_value()) |
| 42 | + throw Exception(ErrorCodes::LOGICAL_ERROR, "No list of nodes inside Task Distributer."); |
| 43 | + |
| 44 | + const auto & ids_of_nodes_value = ids_of_nodes.value(); |
| 45 | + size_t nodes_count = ids_of_nodes_value.size(); |
| 46 | + |
| 47 | + /// Trivial case |
| 48 | + if (nodes_count < 2) |
| 49 | + return 0; |
| 50 | + |
| 51 | + /// Rendezvous hashing |
| 52 | + size_t best_id = 0; |
| 53 | + UInt64 best_weight = sipHash64(ids_of_nodes_value[0] + file_path); |
| 54 | + for (size_t id = 1; id < nodes_count; ++id) |
| 55 | + { |
| 56 | + UInt64 weight = sipHash64(ids_of_nodes_value[id] + file_path); |
| 57 | + if (weight > best_weight) |
| 58 | + { |
| 59 | + best_weight = weight; |
| 60 | + best_id = id; |
| 61 | + } |
| 62 | + } |
| 63 | + return best_id; |
| 64 | +} |
| 65 | + |
| 66 | +std::optional<String> StorageObjectStorageStableTaskDistributor::getPreQueuedFile(size_t number_of_current_replica) |
| 67 | +{ |
| 68 | + std::lock_guard lock(mutex); |
| 69 | + |
| 70 | + auto & files = connection_to_files[number_of_current_replica]; |
| 71 | + |
| 72 | + while (!files.empty()) |
| 73 | + { |
| 74 | + String next_file = files.back(); |
| 75 | + files.pop_back(); |
| 76 | + |
| 77 | + auto it = unprocessed_files.find(next_file); |
| 78 | + if (it == unprocessed_files.end()) |
| 79 | + continue; |
| 80 | + |
| 81 | + unprocessed_files.erase(it); |
| 82 | + |
| 83 | + LOG_TRACE( |
| 84 | + log, |
| 85 | + "Assigning pre-queued file {} to replica {}", |
| 86 | + next_file, |
| 87 | + number_of_current_replica |
| 88 | + ); |
| 89 | + |
| 90 | + return next_file; |
| 91 | + } |
| 92 | + |
| 93 | + return std::nullopt; |
| 94 | +} |
| 95 | + |
| 96 | +std::optional<String> StorageObjectStorageStableTaskDistributor::getMatchingFileFromIterator(size_t number_of_current_replica) |
| 97 | +{ |
| 98 | + { |
| 99 | + std::lock_guard lock(mutex); |
| 100 | + if (iterator_exhausted) |
| 101 | + return std::nullopt; |
| 102 | + } |
| 103 | + |
| 104 | + while (true) |
| 105 | + { |
| 106 | + ObjectInfoPtr object_info; |
| 107 | + |
| 108 | + { |
| 109 | + std::lock_guard lock(mutex); |
| 110 | + object_info = iterator->next(0); |
| 111 | + |
| 112 | + if (!object_info) |
| 113 | + { |
| 114 | + iterator_exhausted = true; |
| 115 | + break; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + String file_path; |
| 120 | + |
| 121 | + auto archive_object_info = std::dynamic_pointer_cast<StorageObjectStorageSource::ArchiveIterator::ObjectInfoInArchive>(object_info); |
| 122 | + if (archive_object_info) |
| 123 | + { |
| 124 | + file_path = archive_object_info->getPathToArchive(); |
| 125 | + } |
| 126 | + else |
| 127 | + { |
| 128 | + file_path = object_info->getPath(); |
| 129 | + } |
| 130 | + |
| 131 | + size_t file_replica_idx = getReplicaForFile(file_path); |
| 132 | + if (file_replica_idx == number_of_current_replica) |
| 133 | + { |
| 134 | + LOG_TRACE( |
| 135 | + log, |
| 136 | + "Found file {} for replica {}", |
| 137 | + file_path, |
| 138 | + number_of_current_replica |
| 139 | + ); |
| 140 | + |
| 141 | + return file_path; |
| 142 | + } |
| 143 | + |
| 144 | + // Queue file for its assigned replica |
| 145 | + { |
| 146 | + std::lock_guard lock(mutex); |
| 147 | + unprocessed_files.insert(file_path); |
| 148 | + connection_to_files[file_replica_idx].push_back(file_path); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return std::nullopt; |
| 153 | +} |
| 154 | + |
| 155 | +std::optional<String> StorageObjectStorageStableTaskDistributor::getAnyUnprocessedFile(size_t number_of_current_replica) |
| 156 | +{ |
| 157 | + std::lock_guard lock(mutex); |
| 158 | + |
| 159 | + if (!unprocessed_files.empty()) |
| 160 | + { |
| 161 | + auto it = unprocessed_files.begin(); |
| 162 | + String next_file = *it; |
| 163 | + unprocessed_files.erase(it); |
| 164 | + |
| 165 | + LOG_TRACE( |
| 166 | + log, |
| 167 | + "Iterator exhausted. Assigning unprocessed file {} to replica {}", |
| 168 | + next_file, |
| 169 | + number_of_current_replica |
| 170 | + ); |
| 171 | + |
| 172 | + return next_file; |
| 173 | + } |
| 174 | + |
| 175 | + return std::nullopt; |
| 176 | +} |
| 177 | + |
| 178 | +} |
0 commit comments