66namespace DB
77{
88
9+ namespace ErrorCodes
10+ {
11+ extern const int LOGICAL_ERROR;
12+ extern const int CANNOT_READ_ALL_DATA;
13+ };
14+
915StorageObjectStorageStableTaskDistributor::StorageObjectStorageStableTaskDistributor (
1016 std::shared_ptr<IObjectIterator> iterator_,
1117 std::vector<std::string> ids_of_nodes_)
@@ -14,6 +20,9 @@ StorageObjectStorageStableTaskDistributor::StorageObjectStorageStableTaskDistrib
1420 , ids_of_nodes(ids_of_nodes_)
1521 , iterator_exhausted(false )
1622{
23+ size_t nodes = ids_of_nodes.size ();
24+ for (size_t i = 0 ; i < nodes; ++i)
25+ replica_to_files_to_be_processed[i] = std::list<String>{};
1726}
1827
1928std::optional<String> StorageObjectStorageStableTaskDistributor::getNextTask (size_t number_of_current_replica)
@@ -24,16 +33,27 @@ std::optional<String> StorageObjectStorageStableTaskDistributor::getNextTask(siz
2433 number_of_current_replica
2534 );
2635
27- // 1. Check pre-queued files first
28- if (auto file = getPreQueuedFile (number_of_current_replica))
29- return file;
36+ auto processed_file_list_ptr = replica_to_files_to_be_processed.find (number_of_current_replica);
37+ if (processed_file_list_ptr == replica_to_files_to_be_processed.end ())
38+ throw Exception (
39+ ErrorCodes::LOGICAL_ERROR,
40+ " Replica number {} was marked as lost, can't set task for it anymore" ,
41+ number_of_current_replica
42+ );
3043
44+ // 1. Check pre-queued files first
45+ std::optional<String> file = getPreQueuedFile (number_of_current_replica);
3146 // 2. Try to find a matching file from the iterator
32- if (auto file = getMatchingFileFromIterator (number_of_current_replica))
33- return file;
34-
47+ if (!file.has_value ())
48+ file = getMatchingFileFromIterator (number_of_current_replica);
3549 // 3. Process unprocessed files if iterator is exhausted
36- return getAnyUnprocessedFile (number_of_current_replica);
50+ if (!file.has_value ())
51+ file = getAnyUnprocessedFile (number_of_current_replica);
52+
53+ if (file.has_value ())
54+ processed_file_list_ptr->second .push_back (*file);
55+
56+ return file;
3757}
3858
3959size_t StorageObjectStorageStableTaskDistributor::getReplicaForFile (const String & file_path)
@@ -179,4 +199,28 @@ std::optional<String> StorageObjectStorageStableTaskDistributor::getAnyUnprocess
179199 return std::nullopt ;
180200}
181201
202+ void StorageObjectStorageStableTaskDistributor::rescheduleTasksFromReplica (size_t number_of_current_replica)
203+ {
204+ LOG_INFO (log, " Replica {} is marked as lost, tasks are returned to queue" , number_of_current_replica);
205+ std::lock_guard lock (mutex);
206+
207+ auto processed_file_list_ptr = replica_to_files_to_be_processed.find (number_of_current_replica);
208+ if (processed_file_list_ptr == replica_to_files_to_be_processed.end ())
209+ throw Exception (
210+ ErrorCodes::LOGICAL_ERROR,
211+ " Replica number {} was marked as lost already" ,
212+ number_of_current_replica
213+ );
214+
215+ if (replica_to_files_to_be_processed.size () < 2 )
216+ throw Exception (
217+ ErrorCodes::CANNOT_READ_ALL_DATA,
218+ " All replicas were marked as lost"
219+ );
220+
221+ for (const auto & file_path : processed_file_list_ptr->second )
222+ unprocessed_files.insert (file_path);
223+ replica_to_files_to_be_processed.erase (number_of_current_replica);
224+ }
225+
182226}
0 commit comments