1 /*
2   Copyright (c) 2015, 2021, Oracle and/or its affiliates.
3 
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2.0,
6   as published by the Free Software Foundation.
7 
8   This program is also distributed with certain software (including
9   but not limited to OpenSSL) that is licensed under separate terms,
10   as designated in a particular file or component or in included license
11   documentation.  The authors of MySQL hereby grant you an additional
12   permission to link the program and your derivative works with the
13   separately licensed software that they have included with MySQL.
14 
15   This program is distributed in the hope that it will be useful,
16   but WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   GNU General Public License, version 2.0, for more details.
19 
20   You should have received a copy of the GNU General Public License
21   along with this program; if not, write to the Free Software
22   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 #include "abstract_crawler.h"
26 #include "dump_end_dump_task.h"
27 #include "this_thread.h"
28 #include <boost/date_time.hpp>
29 
30 using namespace Mysql::Tools::Dump;
31 
32 my_boost::atomic_uint64_t Abstract_crawler::next_chain_id;
33 
Abstract_crawler(Mysql::I_callable<bool,const Mysql::Tools::Base::Message_data &> * message_handler,Simple_id_generator * object_id_generator,Mysql::Tools::Base::Abstract_program * program)34 Abstract_crawler::Abstract_crawler(
35   Mysql::I_callable<bool, const Mysql::Tools::Base::Message_data&>*
36     message_handler, Simple_id_generator* object_id_generator,
37     Mysql::Tools::Base::Abstract_program* program)
38   : Abstract_chain_element(message_handler, object_id_generator),
39     m_program(program)
40 {}
41 
~Abstract_crawler()42 Abstract_crawler::~Abstract_crawler()
43 {
44   for (std::vector<I_dump_task*>::iterator it= m_dump_tasks_created.begin();
45     it != m_dump_tasks_created.end(); ++it)
46   {
47     delete *it;
48   }
49 }
50 
register_chain_maker(I_chain_maker * new_chain_maker)51 void Abstract_crawler::register_chain_maker(I_chain_maker* new_chain_maker)
52 {
53   m_chain_makers.push_back(new_chain_maker);
54 }
55 
get_program()56 Mysql::Tools::Base::Abstract_program* Abstract_crawler::get_program()
57 {
58   return m_program;
59 }
60 
process_dump_task(I_dump_task * new_dump_task)61 void Abstract_crawler::process_dump_task(I_dump_task* new_dump_task)
62 {
63   /*
64    Add the tasks to this list so that even if we error out,
65    cleanup is done properly.
66   */
67   m_dump_tasks_created.push_back(new_dump_task);
68 
69   /* in case of error stop all further processing */
70   if (get_program()->get_error_code())
71     return;
72 
73   Item_processing_data* main_item_processing_data=
74     this->new_task_created(new_dump_task);
75 
76   this->object_processing_starts(main_item_processing_data);
77 
78   for (std::vector<I_chain_maker*>::iterator it= m_chain_makers.begin();
79     it != m_chain_makers.end(); ++it)
80   {
81     uint64 new_chain_id= next_chain_id++;
82     Chain_data* chain_data= new Chain_data(new_chain_id);
83 
84     I_object_reader* chain= (*it)->create_chain(chain_data, new_dump_task);
85     if (chain != NULL)
86     {
87       main_item_processing_data->set_chain(chain_data);
88       chain->read_object(
89         this->new_chain_created(
90         chain_data, main_item_processing_data, chain));
91     }
92     else
93     {
94       delete chain_data;
95     }
96   }
97   this->object_processing_ends(main_item_processing_data);
98 }
99 
wait_for_tasks_completion()100 void Abstract_crawler::wait_for_tasks_completion()
101 {
102   for (std::vector<I_dump_task*>::iterator it= m_dump_tasks_created.begin();
103     it != m_dump_tasks_created.end(); ++it)
104   {
105     while ((*it)->is_completed() == false)
106     {
107       /* in case of error stop all running queues */
108       if (get_program()->get_error_code())
109       {
110         for (std::vector<I_chain_maker*>::iterator it= m_chain_makers.begin();
111           it != m_chain_makers.end(); ++it)
112         {
113           (*it)->stop_queues();
114         }
115         return;
116       }
117       my_boost::this_thread::sleep(boost::posix_time::milliseconds(1));
118     }
119   }
120 }
121 
need_callbacks_in_child()122 bool Abstract_crawler::need_callbacks_in_child()
123 {
124   return true;
125 }
126