1 /* Copyright (c) 2014, 2021, Oracle and/or its affiliates.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software Foundation,
21    51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
22 
23 #ifndef REPLICATION_THREADS_API_INCLUDE
24 #define REPLICATION_THREADS_API_INCLUDE
25 
26 #include <string>
27 #include <mysql/group_replication_priv.h>
28 
29 #define DEFAULT_THREAD_PRIORITY 0
30 //Applier thread InnoDB priority
31 #define GROUP_REPLICATION_APPLIER_THREAD_PRIORITY 1
32 
33 class Replication_thread_api
34 {
35 
36 public:
Replication_thread_api(const char * channel_interface)37   Replication_thread_api(const char *channel_interface)
38     :stop_wait_timeout(LONG_TIMEOUT),
39     interface_channel(channel_interface)
40     {};
41 
Replication_thread_api()42   Replication_thread_api()
43     :stop_wait_timeout(LONG_TIMEOUT),
44     interface_channel(NULL)
45     {};
46 
~Replication_thread_api()47   ~Replication_thread_api(){}
48 
49   /**
50     Set the channel name to be used on the interface method invocation.
51 
52     @param channel_name the name to be used.
53   */
set_channel_name(const char * channel_name)54   void set_channel_name(const char *channel_name)
55   {
56     interface_channel= channel_name;
57   }
58 
59   /**
60     Initializes a channel connection in a similar way to a change master command.
61 
62     @param hostname      The channel hostname
63     @param port          The channel port
64     @param user          The user used in the receiver connection
65     @param password      The password used in the receiver connection
66     @param use_ssl       Force the use of SSL on recovery connections
67     @param ssl_ca        SSL trusted certificate authorities file
68     @param ssl_capath    A directory with trusted CA files
69     @param ssl_cert      The certificate file for secure connections
70     @param ssl_cipher    The list of ciphers to use
71     @param ssl_key       The SSL key file
72     @param ssl_crl       SSL revocation list file
73     @param ssl_crlpath   Path with revocation list files
74     @param ssl_verify_server_cert  verify the hostname against the certificate
75     @param priority      The channel priority on event application
76     @param retry_count   The number of retries when connecting
77     @param preserve_logs If logs should be always preserved
78     @param ignore_ws_mem_limit Shall ignore write set mem limits
79     @param allow_drop_write_set Shall not require write set to be preserved
80 
81     @return the operation status
82       @retval 0      OK
83       @retval !=0    Error on channel creation
84   */
85   int initialize_channel(char* hostname, uint port,
86                          char* user, char* password,
87                          bool use_ssl,
88                          char *ssl_ca,
89                          char *ssl_capath,
90                          char *ssl_cert,
91                          char *ssl_cipher,
92                          char *ssl_key,
93                          char *ssl_crl,
94                          char *ssl_crlpath,
95                          bool ssl_verify_server_cert,
96                          int priority,
97                          int retry_count,
98                          bool preserve_logs,
99                          bool ignore_ws_mem_limit,
100                          bool allow_drop_write_set);
101 
102   /**
103     Start the Applier/Receiver threads according to the given options.
104     If the receiver thread is to be started, connection credential must be
105     supported.
106 
107     @param start_receiver       Is the receiver thread to be started
108     @param start_applier        Is the applier thread to be started
109     @param view_id              The view id, that can be used to activate the
110                                 until view id clause.
111     @param wait_for_connection  If when starting the receiver, the method should
112                                 wait for the connection to succeed
113 
114     @return the operation status
115       @retval 0      OK
116       @retval REPLICATION_THREAD_START_ERROR
117         Error when launching on of the threads
118       @retval REPLICATION_THREAD_START_IO_NOT_CONNECTED
119         Error when the threads start, but the IO thread cannot connect
120    */
121   int start_threads(bool start_receiver, bool start_applier,
122                     std::string* view_id, bool wait_for_connection);
123 
124   /**
125     Stops the channel threads according to the given options.
126 
127     @param stop_receiver if the receiver thread should be stopped
128     @param stop_applier  if the applier thread should be stopped
129 
130     @return the operation status
131       @retval 0      OK
132       @retval !=0    Error stopping channel thread
133   */
134   int stop_threads(bool stop_receiver, bool stop_applier);
135 
136   /**
137     Purges the relay logs.
138 
139     @param reset_all  If true, the method will purge logs and remove the channel
140                       If false, the channel logs will be deleted and recreated
141                                 but the channel info will be preserved.
142 
143     @return the operation status
144       @retval 0      OK
145       @retval !=0    Error purging channel logs
146   */
147   int purge_logs(bool reset_all);
148 
149   /**
150      Checks if the receiver thread is running.
151 
152      @return the thread status
153       @retval true      the thread is running
154       @retval false     the thread is stopped
155   */
156   bool is_receiver_thread_running();
157 
158   /**
159      Checks if the receiver thread is stopping.
160 
161      @return the thread status
162       @retval true      the thread is stopping
163       @retval false     the thread is not stopping
164   */
165   bool is_receiver_thread_stopping();
166 
167   /**
168      Checks if the applier thread is running.
169 
170      @return the thread status
171       @retval true      the thread is running
172       @retval false     the thread is stopped
173   */
174   bool is_applier_thread_running();
175 
176   /**
177      Checks if the applier thread is stopping.
178 
179      @return the thread status
180       @retval true      the thread is stopping
181       @retval false     the thread is not stopping
182   */
183   bool is_applier_thread_stopping();
184 
185   /**
186     Queues a event packet into the current active channel relay log.
187 
188     @param buf         the event buffer
189     @param event_len  the event buffer length
190 
191     @return the operation status
192       @retval 0      OK
193       @retval != 0   Error on queue
194   */
195   int queue_packet(const char* buf, ulong event_len);
196 
197   /**
198     Checks if the applier, and its workers when parallel applier is
199     enabled, has already consumed all relay log, that is, applier is
200     waiting for transactions to be queued.
201 
202     @return the applier status
203       @retval true      the applier is waiting
204       @retval false     otherwise
205   */
206   bool is_applier_thread_waiting();
207 
208   /**
209     Checks if all the queued transactions were executed.
210 
211     @param timeout  the time (seconds) after which the method returns if the
212                     above condition was not satisfied
213 
214     @return the operation status
215       @retval 0   All transactions were executed
216       @retval REPLICATION_THREAD_WAIT_TIMEOUT_ERROR     A timeout occurred
217       @retval REPLICATION_THREAD_WAIT_NO_INFO_ERROR     An error occurred
218   */
219   int wait_for_gtid_execution(double timeout);
220 
221   /**
222     Method to get applier ids from the configured channel
223 
224     @param[out] thread_ids The retrieved thread ids.
225 
226     @return the number of appliers
227       @retval <= 0  Some error occurred or the applier is not present
228       @retval >  0  Number of appliers
229   */
230   int get_applier_thread_ids(unsigned long** thread_ids);
231 
232   /**
233      Checks if the given id matches any of the event applying threads for
234      the configured channel.
235 
236      @param id  the thread id
237      @param channel_name  the channel name which needs to be checked. It is
238                           an optional parameter.
239 
240      @return if it belongs to a thread
241        @retval true   the id matches a SQL or worker thread
242        @retval false  the id doesn't match any thread
243    */
244   bool is_own_event_applier(my_thread_id id, const char* channel_name= NULL);
245 
246   /**
247      Checks if the given id matches the receiver thread for
248      the configured channel.
249 
250      @param id  the thread id
251 
252      @return if it belongs to a thread
253        @retval true   the id matches an IO thread
254        @retval false  the id doesn't match any thread
255    */
256   bool is_own_event_receiver(my_thread_id id);
257 
258   /**
259     Returns last GNO from the applier for a given UUID.
260 
261     @param sidno    the SIDNO of the group UUID, so that we get the
262                     last GNO of group's already certified transactions
263                     on relay log.
264 
265     @return
266       @retval       GNO value
267   */
268   rpl_gno get_last_delivered_gno(rpl_sidno sidno);
269 
270   /**
271     Sets the threads shutdown timeout.
272 
273     @param[in]  timeout      the timeout
274   */
set_stop_wait_timeout(ulong timeout)275   void set_stop_wait_timeout (ulong timeout){
276     stop_wait_timeout= timeout;
277   }
278 
279   /**
280     Returns the retrieved gtid set from the receiver thread.
281 
282     @param[out] retrieved_set the set in string format.
283     @param channel_name the name of the channel to get the information.
284 
285     @return
286       @retval true there was an error.
287       @retval false the operation has succeeded.
288   */
289   bool get_retrieved_gtid_set(std::string& retrieved_set,
290                               const char* channel_name= NULL);
291 
292   /**
293     Checks if the channel's relay log contains partial transaction.
294     @return
295       @retval true  If relaylog contains partial transaction.
296       @retval false If relaylog does not contain partial transaction.
297   */
298   bool is_partial_transaction_on_relay_log();
299 
300 private:
301   ulong stop_wait_timeout;
302   const char* interface_channel;
303 };
304 
305 #endif /* REPLICATION_THREADS_API_INCLUDE */
306