|
| 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 | + size_t number_of_replicas_) |
| 12 | + : iterator(std::move(iterator_)) |
| 13 | + , connection_to_files(number_of_replicas_) |
| 14 | + , iterator_exhausted(false) |
| 15 | +{ |
| 16 | +} |
| 17 | + |
| 18 | +std::optional<String> StorageObjectStorageStableTaskDistributor::getNextTask(size_t number_of_current_replica) |
| 19 | +{ |
| 20 | + LOG_TRACE( |
| 21 | + log, |
| 22 | + "Received a new connection from replica {} looking for a file", |
| 23 | + number_of_current_replica |
| 24 | + ); |
| 25 | + |
| 26 | + // 1. Check pre-queued files first |
| 27 | + if (auto file = getPreQueuedFile(number_of_current_replica)) |
| 28 | + return file; |
| 29 | + |
| 30 | + // 2. Try to find a matching file from the iterator |
| 31 | + if (auto file = getMatchingFileFromIterator(number_of_current_replica)) |
| 32 | + return file; |
| 33 | + |
| 34 | + // 3. Process unprocessed files if iterator is exhausted |
| 35 | + return getAnyUnprocessedFile(number_of_current_replica); |
| 36 | +} |
| 37 | + |
| 38 | +size_t StorageObjectStorageStableTaskDistributor::getReplicaForFile(const String & file_path) |
| 39 | +{ |
| 40 | + return ConsistentHashing(sipHash64(file_path), connection_to_files.size()); |
| 41 | +} |
| 42 | + |
| 43 | +std::optional<String> StorageObjectStorageStableTaskDistributor::getPreQueuedFile(size_t number_of_current_replica) |
| 44 | +{ |
| 45 | + std::lock_guard lock(mutex); |
| 46 | + |
| 47 | + auto & files = connection_to_files[number_of_current_replica]; |
| 48 | + |
| 49 | + while (!files.empty()) |
| 50 | + { |
| 51 | + String next_file = files.back(); |
| 52 | + files.pop_back(); |
| 53 | + |
| 54 | + auto it = unprocessed_files.find(next_file); |
| 55 | + if (it == unprocessed_files.end()) |
| 56 | + continue; |
| 57 | + |
| 58 | + unprocessed_files.erase(it); |
| 59 | + |
| 60 | + LOG_TRACE( |
| 61 | + log, |
| 62 | + "Assigning pre-queued file {} to replica {}", |
| 63 | + next_file, |
| 64 | + number_of_current_replica |
| 65 | + ); |
| 66 | + |
| 67 | + return next_file; |
| 68 | + } |
| 69 | + |
| 70 | + return std::nullopt; |
| 71 | +} |
| 72 | + |
| 73 | +std::optional<String> StorageObjectStorageStableTaskDistributor::getMatchingFileFromIterator(size_t number_of_current_replica) |
| 74 | +{ |
| 75 | + { |
| 76 | + std::lock_guard lock(mutex); |
| 77 | + if (iterator_exhausted) |
| 78 | + return std::nullopt; |
| 79 | + } |
| 80 | + |
| 81 | + while (true) |
| 82 | + { |
| 83 | + ObjectInfoPtr object_info; |
| 84 | + |
| 85 | + { |
| 86 | + std::lock_guard lock(mutex); |
| 87 | + object_info = iterator->next(0); |
| 88 | + |
| 89 | + if (!object_info) |
| 90 | + { |
| 91 | + iterator_exhausted = true; |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + String file_path; |
| 97 | + |
| 98 | + auto archive_object_info = std::dynamic_pointer_cast<StorageObjectStorageSource::ArchiveIterator::ObjectInfoInArchive>(object_info); |
| 99 | + if (archive_object_info) |
| 100 | + { |
| 101 | + file_path = archive_object_info->getPathToArchive(); |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + file_path = object_info->getPath(); |
| 106 | + } |
| 107 | + |
| 108 | + size_t file_replica_idx = getReplicaForFile(file_path); |
| 109 | + if (file_replica_idx == number_of_current_replica) |
| 110 | + { |
| 111 | + LOG_TRACE( |
| 112 | + log, |
| 113 | + "Found file {} for replica {}", |
| 114 | + file_path, |
| 115 | + number_of_current_replica |
| 116 | + ); |
| 117 | + |
| 118 | + return file_path; |
| 119 | + } |
| 120 | + |
| 121 | + // Queue file for its assigned replica |
| 122 | + { |
| 123 | + std::lock_guard lock(mutex); |
| 124 | + unprocessed_files.insert(file_path); |
| 125 | + connection_to_files[file_replica_idx].push_back(file_path); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return std::nullopt; |
| 130 | +} |
| 131 | + |
| 132 | +std::optional<String> StorageObjectStorageStableTaskDistributor::getAnyUnprocessedFile(size_t number_of_current_replica) |
| 133 | +{ |
| 134 | + std::lock_guard lock(mutex); |
| 135 | + |
| 136 | + if (!unprocessed_files.empty()) |
| 137 | + { |
| 138 | + auto it = unprocessed_files.begin(); |
| 139 | + String next_file = *it; |
| 140 | + unprocessed_files.erase(it); |
| 141 | + |
| 142 | + LOG_TRACE( |
| 143 | + log, |
| 144 | + "Iterator exhausted. Assigning unprocessed file {} to replica {}", |
| 145 | + next_file, |
| 146 | + number_of_current_replica |
| 147 | + ); |
| 148 | + |
| 149 | + return next_file; |
| 150 | + } |
| 151 | + |
| 152 | + return std::nullopt; |
| 153 | +} |
| 154 | + |
| 155 | +} |
0 commit comments