1 /* Copyright (c) 2015, 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 RPL_SERVICE_INTERFACE_INCLUDE
24 #define RPL_SERVICE_INTERFACE_INCLUDE
25 
26 //Channel errors
27 
28 #define RPL_CHANNEL_SERVICE_RECEIVER_CONNECTION_ERROR      -1
29 #define RPL_CHANNEL_SERVICE_DEFAULT_CHANNEL_CREATION_ERROR -2
30 #define RPL_CHANNEL_SERVICE_SLAVE_SKIP_COUNTER_ACTIVE      -3
31 #define RPL_CHANNEL_SERVICE_CHANNEL_DOES_NOT_EXISTS_ERROR  -4
32 //Error for the wait event consumption, equal to the server wait for GTID method
33 #define REPLICATION_THREAD_WAIT_TIMEOUT_ERROR -1
34 #define REPLICATION_THREAD_WAIT_NO_INFO_ERROR -2
35 
36 //Settings
37 
38 //Used whenever a parameter should take the server default value
39 #define RPL_SERVICE_SERVER_DEFAULT -1
40 
41 //Channel creation settings
42 
43 /**
44   Types of channels
45 */
46 enum enum_channel_type
47 {
48   SLAVE_REPLICATION_CHANNEL,  //Master slave replication channels
49   GROUP_REPLICATION_CHANNEL   //Group replication channels
50 };
51 
52 /**
53   Know parallelization options that can be applied to channel appliers
54 */
55 enum enum_multi_threaded_workers_type
56 {
57   CHANNEL_MTS_PARALLEL_TYPE_DB_NAME,
58   CHANNEL_MTS_PARALLEL_TYPE_LOGICAL_CLOCK
59 };
60 
61 /**
62  SSL information to be used when creating a channel.
63  It maps the SSL options present in a CHANGE MASTER.
64 */
65 struct st_ssl_info
66 {
67   int   use_ssl;                //use SSL
68   char* ssl_ca_file_name;       //SSL list of trusted certificate authorities
69   char* ssl_ca_directory;       //SSL certificate authorities directory
70   char* ssl_cert_file_name;     //SSL connection certificate
71   char* ssl_crl_file_name;      //SSL certificate revocation list
72   char* ssl_crl_directory;      //SSL certificate revocation list file directory
73   char* ssl_key;                //SSL key file for connections
74   char* ssl_cipher;             //list of permissible ciphers to use for SSL
75   int   ssl_verify_server_cert; //check the server's Common Name value
76   char* tls_version;            //TLS version to use for SSL
77 };
78 typedef struct st_ssl_info Channel_ssl_info;
79 
80 void initialize_channel_ssl_info(Channel_ssl_info* channel_ssl_info);
81 
82 /**
83  Creation information for a channel.
84  It includes the data that is usually associated to a change master command
85 */
86 struct st_channel_info
87 {
88   enum_channel_type type;
89   char* hostname;
90   int port;
91   char* user;
92   char* password;
93   Channel_ssl_info* ssl_info;
94   int auto_position;
95   int channel_mts_parallel_type;
96   int channel_mts_parallel_workers;
97   int channel_mts_checkpoint_group;
98   int replicate_same_server_id;
99   int thd_tx_priority;           //The applier thread priority
100   int sql_delay;
101   int connect_retry;             //How many seconds to wait between retries.
102   int retry_count;               //Limits the number of reconnection attempts
103   bool preserve_relay_logs;      //If the logs should be preserved on creation
104   bool m_ignore_write_set_memory_limit; //Shall ignore write set mem limits
105   bool m_allow_drop_write_set; //Shall not require write set to be preserved
106 };
107 typedef struct st_channel_info Channel_creation_info;
108 
109 void initialize_channel_creation_info(Channel_creation_info* channel_info);
110 
111 //Start settings
112 
113 /**
114   The known types of channel threads.
115   All new types should be power of 2
116 */
117 enum enum_channel_thread_types
118 {
119   CHANNEL_NO_THD=0,
120   CHANNEL_RECEIVER_THREAD=1,
121   CHANNEL_APPLIER_THREAD=2
122 };
123 
124 /**
125   The known until conditions that can be applied to channels
126 */
127 enum enum_channel_until_condition
128 {
129   CHANNEL_NO_UNTIL_CONDITION,
130   CHANNEL_UNTIL_APPLIER_BEFORE_GTIDS,
131   CHANNEL_UNTIL_APPLIER_AFTER_GTIDS,
132   CHANNEL_UNTIL_APPLIER_AFTER_GAPS,
133   CHANNEL_UNTIL_VIEW_ID
134 };
135 
136 /**
137   Channel information to connect to a receiver
138 */
139 struct st_channel_connection_info
140 {
141   int until_condition; //base on enum_channel_until_condition
142   char* gtid;          //Gtids to wait on a until condition
143   char* view_id;       //The view id to wait on a until condition
144 };
145 
146 typedef struct st_channel_connection_info Channel_connection_info;
147 
148 void
149 initialize_channel_connection_info(Channel_connection_info* channel_info);
150 
151 /**
152   Initializes a channel connection in a similar way to a change master command.
153 
154   @note If the channel exists, it is reconfigured with the new options.
155         About the logs, the preserve_relay_logs option allows the user to
156         maintain them untouched.
157 
158   @param channel              The channel name
159   @param channel_information  Channel creation information.
160 
161   @return the operation status
162     @retval 0      OK
163     @retval !=0    Error on channel creation
164 */
165 int channel_create(const char* channel,
166                    Channel_creation_info* channel_information);
167 
168 /**
169   Start the Applier/Receiver threads according to the given options.
170   If the receiver thread is to be started, connection credential must be
171   supported.
172 
173   @param channel              The channel name
174   @param connection_info      Channel connection information
175   @param threads_to_start     The types of threads to be started
176   @param wait_for_connection  If when starting the receiver, the method should
177                               wait for the connection to succeed
178 
179   @return the operation status
180     @retval 0      OK
181     @retval !=0    Error
182  */
183 int channel_start(const char* channel,
184                   Channel_connection_info* connection_info,
185                   int threads_to_start,
186                   int wait_for_connection);
187 
188 /**
189   Stops the channel threads according to the given options.
190 
191   @param channel              The channel name
192   @param threads_to_stop      The types of threads to be stopped
193   @param timeout              The expected time in which the thread should stop
194 
195   @return the operation status
196     @retval 0      OK
197     @retval !=0    Error
198 */
199 int channel_stop(const char* channel,
200                  int threads_to_stop,
201                  long timeout);
202 
203 /**
204   Stops all the running channel threads according to the given options.
205 
206   @param threads_to_stop      The types of threads to be stopped
207   @param timeout              The expected time in which the thread should stop
208 
209   @return the operation status
210     @retval 0      OK
211     @retval !=0    Error
212 */
213 int channel_stop_all(int threads_to_stop,
214                      long timeout);
215 
216 /**
217   Purges the channel logs
218 
219   @param reset_all  If true, the method will purge logs and remove the channel
220                     If false, only the channel information will be reset.
221 
222   @return the operation status
223     @retval 0      OK
224     @retval !=0    Error
225 */
226 int channel_purge_queue(const char* channel, bool reset_all);
227 
228 /**
229   Tells if the selected component of the channel is active or not.
230   If no component is passed, this method returns if the channel exists or not
231 
232   @param channel  The channel name
233   @param type     The thread that should be checked.
234                   If 0, this method applies to the channel existence.
235 
236   @return is the channel (component) active
237     @retval true    Yes
238     @retval false   No
239 */
240 bool channel_is_active(const char* channel, enum_channel_thread_types type);
241 
242 /**
243   Returns the id(s) of the channel threads: receiver or applier.
244   If more than one applier exists, an array is returned, on which first
245   index is coordinator thread id.
246 
247   @param[in]  channel      The channel name
248   @param[in]  thread_type  The thread type (receiver or applier)
249   @param[out] thread_id    The array of id(s)
250 
251   @return the number of returned ids
252     @retval -1  the channel does no exists, or the thread is not present
253     @retval >0 the number of thread ids returned.
254 */
255 int channel_get_thread_id(const char* channel,
256                           enum_channel_thread_types thread_type,
257                           unsigned long** thread_id);
258 
259 /**
260   Returns last GNO from applier from a given UUID.
261 
262   @param channel the channel name
263   @param sidno   the uuid associated to the desired gno
264 
265   @return the last applier gno
266     @retval <0 the channel does no exists, or the applier is not present
267     @retval >0 the gno
268 */
269 long long channel_get_last_delivered_gno(const char* channel, int sidno);
270 
271 /**
272   Adds server executed GTID set to channel received GTID set.
273 
274   @param channel the channel name
275 
276   @return the operation status
277     @retval 0      OK
278     @retval != 0   Error
279 */
280 int channel_add_executed_gtids_to_received_gtids(const char* channel);
281 
282 /**
283   Queues a event packet into the current active channel.
284 
285   @param buf         the event buffer
286   @param event_len  the event buffer length
287 
288   @return the operation status
289     @retval 0      OK
290     @retval != 0   Error on queue
291 */
292 int channel_queue_packet(const char* channel, const char* buf, unsigned long len);
293 
294 /**
295   Checks if all the queued transactions were executed.
296 
297   @note This method assumes that the channel is not receiving any more events.
298         If it is still receiving, then the method should wait for execution of
299         transactions that were present when this method was invoked.
300 
301   @param timeout  the time (seconds) after which the method returns if the
302                   above condition was not satisfied
303 
304   @return the operation status
305     @retval 0   All transactions were executed
306     @retval REPLICATION_THREAD_WAIT_TIMEOUT_ERROR     A timeout occurred
307     @retval REPLICATION_THREAD_WAIT_NO_INFO_ERROR     An error occurred
308 */
309 int channel_wait_until_apply_queue_applied(const char* channel,
310                                            double timeout);
311 
312 /**
313   Checks if the applier, and its workers when parallel applier is
314   enabled, has already consumed all relay log, that is, applier is
315   waiting for transactions to be queued.
316 
317   @param channel  The channel name
318 
319   @return the operation status
320     @retval <0  Error
321     @retval  0  Applier is not waiting
322     @retval  1  Applier is waiting
323 */
324 int channel_is_applier_waiting(const char* channel);
325 
326 /**
327   Checks if the applier thread, and its workers when parallel applier is
328   enabled, has already consumed all relay log, that is, applier thread
329   is waiting for transactions to be queued.
330 
331   @param thread_id  the applier thread id to check
332   @param worker     flag to indicate if thread is a parallel worker
333 
334   @return the operation status
335     @retval -1  Unable to find applier thread
336     @retval  0  Applier thread is not waiting
337     @retval  1  Applier thread is waiting
338 */
339 int channel_is_applier_thread_waiting(unsigned long thread_id,
340                                       bool worker= false);
341 
342 /**
343   Flush the channel.
344 
345   @return the operation status
346     @retval 0      OK
347     @retval != 0   Error on flush
348 */
349 int channel_flush(const char* channel);
350 
351 /**
352   Initializes channel structures if needed.
353 
354   @return the operation status
355     @retval 0      OK
356     @retval != 0   Error on queue
357 */
358 int initialize_channel_service_interface();
359 
360 /**
361   Returns the receiver thread retrieved GTID set in string format.
362 
363   @param      channel        The channel name.
364   @param[out] retrieved_set  Pointer to pointer to string. The function will
365                              set it to point to a newly allocated buffer, or
366                              NULL on out of memory.
367 
368   @return the operation status
369     @retval 0    OK
370     @retval !=0  Error on retrieval
371 */
372 int channel_get_retrieved_gtid_set(const char* channel,
373                                    char** retrieved_set);
374 
375 /**
376   Tells if the selected component of the channel is stopping or not.
377 
378   @param channel  The channel name
379   @param type     The thread that should be checked.
380 
381   @return is the channel (component) stopping
382     @retval true    Yes
383     @retval false   No, no type was specified or the channel does not exist.
384 */
385 bool channel_is_stopping(const char* channel, enum_channel_thread_types type);
386 
387 /**
388   Checks if the given channel's relaylog contains a partial transaction.
389 
390   @param channel  The channel name
391 
392   @return
393     @retval true    If relaylog contains partial transcation.
394     @retval false   If relaylog does not contain partial transaction.
395 */
396 bool is_partial_transaction_on_channel_relay_log(const char* channel);
397 
398 /**
399   Checks if any slave threads of any channel is running
400 
401   @param[in]        thread_mask       type of slave thread- IO/SQL or any
402 
403   @return
404     @retval          true               atleast one channel threads are running.
405     @retval          false              none of the the channels are running.
406 */
407 bool is_any_slave_channel_running(int thread_mask);
408 
409 #endif //RPL_SERVICE_INTERFACE_INCLUDE
410