1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
/*
Copyright (c) 2015, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "client/dump/object_queue.h"
#include <stddef.h>
#include <chrono>
#include <functional>
#include <thread>
using namespace Mysql::Tools::Dump;
using std::placeholders::_1;
void Object_queue::add_ready_items_to_queue(
std::map<const I_dump_task *,
std::vector<Item_processing_data *> *>::iterator it) {
for (std::vector<Item_processing_data *>::iterator item_iterator =
it->second->begin();
item_iterator != it->second->end(); ++item_iterator) {
m_items_ready_for_processing.push(*item_iterator);
}
delete it->second;
m_tasks_map.erase(it);
}
void Object_queue::task_availability_callback(
const Abstract_dump_task *available_task) {
std::lock_guard<std::mutex> lock(m_queue_mutex);
std::map<const I_dump_task *, std::vector<Item_processing_data *> *>::iterator
it = m_tasks_map.find(available_task);
if (it != m_tasks_map.end()) {
this->add_ready_items_to_queue(it);
}
}
void Object_queue::queue_thread() {
(*this->m_thread_callback)(true);
while (true) {
/* check if any errors are generated by main or child threads */
if (m_program->get_error_code()) stop_queue();
if (m_is_queue_running.load() == false) break;
Item_processing_data *item_to_process = nullptr;
{
std::lock_guard<std::mutex> lock(m_queue_mutex);
if (m_items_ready_for_processing.size() > 0) {
item_to_process = m_items_ready_for_processing.front();
m_items_ready_for_processing.pop();
}
}
if (item_to_process != nullptr) {
this->format_object(item_to_process);
this->object_processing_ends(item_to_process);
}
/**
In case there are no items to be processed from the queue then
in such a scenario the current thread will be infinitely hung in
this loop without allowing context switch to happen, so that other
executing threads can further progress. Thus we need this thread
to sleep so that other threads can proceed to complete its execution
or allow main thread to insert new items into the queue.
*/
{ std::this_thread::sleep_for(std::chrono::milliseconds(1)); }
}
(*this->m_thread_callback)(false);
}
void Object_queue::read_object(Item_processing_data *item_to_process) {
this->object_processing_starts(item_to_process);
Abstract_dump_task *dump_task = dynamic_cast<Abstract_dump_task *>(
item_to_process->get_process_task_object());
if (dump_task == nullptr) {
(*this->get_message_handler())(Mysql::Tools::Base::Message_data(
0, "Not supported operation called.",
Mysql::Tools::Base::Message_type_error));
}
std::lock_guard<std::mutex> lock(m_queue_mutex);
/*
Check if all dependencies are already met, if so, we can directly add
this processing item to queue. If no, we will create completion callback
to handle addition to queue when ready, but this may be called in the
meantime, so we need to check this statement again at the end.
*/
if (dump_task->can_be_executed()) {
m_items_ready_for_processing.push(item_to_process);
return;
}
std::map<const I_dump_task *, std::vector<Item_processing_data *> *>::iterator
it = m_tasks_map.find(dump_task);
std::vector<Item_processing_data *> *list;
if (it == m_tasks_map.end()) {
list = new std::vector<Item_processing_data *>();
m_tasks_map.insert(make_pair(dump_task, list));
dump_task->register_execution_availability_callback(
&m_task_availability_callback);
} else
list = it->second;
list->push_back(item_to_process);
/*
Check if executing threads haven't completed this task dependencies in
meantime, if so, we must revert what we have done with waiting task list.
*/
if (dump_task->can_be_executed())
this->add_ready_items_to_queue(m_tasks_map.find(dump_task));
}
void Object_queue::stop_queue() {
/*
In case of error we stop all the running queues. Make sure the
cleanup of the items is done properly.
*/
if (m_is_queue_running) {
Item_processing_data *item_to_process = nullptr;
do {
{
std::lock_guard<std::mutex> lock(m_queue_mutex);
if (m_items_ready_for_processing.size() == 0) break;
item_to_process = m_items_ready_for_processing.front();
m_items_ready_for_processing.pop();
}
this->object_processing_ends(item_to_process);
} while (item_to_process != nullptr);
m_is_queue_running = false;
}
}
Object_queue::~Object_queue() {
m_is_queue_running = false;
m_thread_group.join_all();
delete m_thread_callback;
std::map<const I_dump_task *, std::vector<Item_processing_data *> *>::iterator
it = m_tasks_map.begin();
for (; it != m_tasks_map.end(); ++it) delete it->second;
}
Object_queue::Object_queue(
std::function<bool(const Mysql::Tools::Base::Message_data &)>
*message_handler,
Simple_id_generator *object_id_generator, uint threads_count,
std::function<void(bool)> *thread_callback,
Mysql::Tools::Base::Abstract_program *program)
: Abstract_object_reader_wrapper(message_handler, object_id_generator),
m_task_availability_callback(
std::bind(&Object_queue::task_availability_callback, this, _1)),
m_is_queue_running(true),
m_thread_callback(thread_callback),
m_program(program) {
for (int thread = threads_count; thread-- > 0;) {
m_thread_group.create_thread(std::bind(&Object_queue::queue_thread, this));
}
}
|