1 /* Copyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved.
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_H
24 #define REPLICATION_H
25 
26 #include <mysql.h>
27 
28 typedef struct st_mysql MYSQL;
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /**
35    Transaction observer flags.
36 */
37 enum Trans_flags {
38   /** Transaction is a real transaction */
39   TRANS_IS_REAL_TRANS = 1
40 };
41 
42 /**
43    Transaction observer parameter
44 */
45 typedef struct Trans_param {
46   uint32 server_id;
47   uint32 flags;
48 
49   /*
50     The latest binary log file name and position written by current
51     transaction, if binary log is disabled or no log event has been
52     written into binary log file by current transaction (events
53     written into transaction log cache are not counted), these two
54     member will be zero.
55   */
56   const char *log_file;
57   my_off_t log_pos;
58 } Trans_param;
59 
60 /**
61    Observes and extends transaction execution
62 */
63 typedef struct Trans_observer {
64   uint32 len;
65 
66   /**
67      This callback is called after transaction commit
68 
69      This callback is called right after commit to storage engines for
70      transactional tables.
71 
72      For non-transactional tables, this is called at the end of the
73      statement, before sending statement status, if the statement
74      succeeded.
75 
76      @note The return value is currently ignored by the server.
77 
78      @param param The parameter for transaction observers
79 
80      @retval 0 Sucess
81      @retval 1 Failure
82   */
83   int (*after_commit)(Trans_param *param);
84 
85   /**
86      This callback is called after transaction rollback
87 
88      This callback is called right after rollback to storage engines
89      for transactional tables.
90 
91      For non-transactional tables, this is called at the end of the
92      statement, before sending statement status, if the statement
93      failed.
94 
95      @note The return value is currently ignored by the server.
96 
97      @param param The parameter for transaction observers
98 
99      @retval 0 Sucess
100      @retval 1 Failure
101   */
102   int (*after_rollback)(Trans_param *param);
103 } Trans_observer;
104 
105 /**
106    Binlog storage flags
107 */
108 enum Binlog_storage_flags {
109   /** Binary log was sync:ed */
110   BINLOG_STORAGE_IS_SYNCED = 1
111 };
112 
113 /**
114    Binlog storage observer parameters
115  */
116 typedef struct Binlog_storage_param {
117   uint32 server_id;
118 } Binlog_storage_param;
119 
120 /**
121    Observe binlog logging storage
122 */
123 typedef struct Binlog_storage_observer {
124   uint32 len;
125 
126   /**
127      This callback is called after binlog has been flushed
128 
129      This callback is called after cached events have been flushed to
130      binary log file but not yet synced.
131 
132      @param param Observer common parameter
133      @param log_file Binlog file name been updated
134      @param log_pos Binlog position after update
135 
136      @retval 0 Sucess
137      @retval 1 Failure
138   */
139   int (*after_flush)(Binlog_storage_param *param,
140                      const char *log_file, my_off_t log_pos);
141 } Binlog_storage_observer;
142 
143 /**
144    Replication binlog transmitter (binlog dump) observer parameter.
145 */
146 typedef struct Binlog_transmit_param {
147   uint32 server_id;
148   uint32 flags;
149   /* Let us keep 1-16 as output flags and 17-32 as input flags */
150   static const uint32 F_OBSERVE= 1;
151   static const uint32 F_DONT_OBSERVE= 2;
152 
set_observe_flagBinlog_transmit_param153   void set_observe_flag() { flags|= F_OBSERVE; }
set_dont_observe_flagBinlog_transmit_param154   void set_dont_observe_flag() { flags|= F_DONT_OBSERVE; }
155   /**
156      If F_OBSERVE is set by any plugin, then it should observe binlog
157      transmission, even F_DONT_OBSERVE is set by some plugins.
158 
159      If both F_OBSERVE and F_DONT_OBSERVE are not set, then it is an old
160      plugin. In this case, it should always observe binlog transmission.
161    */
should_observeBinlog_transmit_param162   bool should_observe()
163   {
164     return (flags & F_OBSERVE) || !(flags & F_DONT_OBSERVE);
165   }
166 } Binlog_transmit_param;
167 
168 /**
169    Observe and extends the binlog dumping thread.
170 */
171 typedef struct Binlog_transmit_observer {
172   uint32 len;
173 
174   /**
175      This callback is called when binlog dumping starts
176 
177 
178      @param param Observer common parameter
179      @param log_file Binlog file name to transmit from
180      @param log_pos Binlog position to transmit from
181 
182      @retval 0 Sucess
183      @retval 1 Failure
184   */
185   int (*transmit_start)(Binlog_transmit_param *param,
186                         const char *log_file, my_off_t log_pos);
187 
188   /**
189      This callback is called when binlog dumping stops
190 
191      @param param Observer common parameter
192 
193      @retval 0 Sucess
194      @retval 1 Failure
195   */
196   int (*transmit_stop)(Binlog_transmit_param *param);
197 
198   /**
199      This callback is called to reserve bytes in packet header for event transmission
200 
201      This callback is called when resetting transmit packet header to
202      reserve bytes for this observer in packet header.
203 
204      The @a header buffer is allocated by the server code, and @a size
205      is the size of the header buffer. Each observer can only reserve
206      a maximum size of @a size in the header.
207 
208      @param param Observer common parameter
209      @param header Pointer of the header buffer
210      @param size Size of the header buffer
211      @param len Header length reserved by this observer
212 
213      @retval 0 Sucess
214      @retval 1 Failure
215   */
216   int (*reserve_header)(Binlog_transmit_param *param,
217                         unsigned char *header,
218                         unsigned long size,
219                         unsigned long *len);
220 
221   /**
222      This callback is called before sending an event packet to slave
223 
224      @param param Observer common parameter
225      @param packet Binlog event packet to send
226      @param len Length of the event packet
227      @param log_file Binlog file name of the event packet to send
228      @param log_pos Binlog position of the event packet to send
229 
230      @retval 0 Sucess
231      @retval 1 Failure
232   */
233   int (*before_send_event)(Binlog_transmit_param *param,
234                            unsigned char *packet, unsigned long len,
235                            const char *log_file, my_off_t log_pos );
236 
237   /**
238      This callback is called after an event packet is sent to the
239      slave or is skipped.
240 
241      @param param             Observer common parameter
242      @param event_buf         Binlog event packet buffer sent
243      @param len               length of the event packet buffer
244      @param skipped_log_file  Binlog file name of the event that
245                               was skipped in the master. This is
246                               null if the position was not skipped
247      @param skipped_log_pos   Binlog position of the event that
248                               was skipped in the master. 0 if not
249                               skipped
250      @retval 0 Sucess
251      @retval 1 Failure
252    */
253   int (*after_send_event)(Binlog_transmit_param *param,
254                           const char *event_buf, unsigned long len,
255                           const char *skipped_log_file, my_off_t skipped_log_pos);
256 
257   /**
258      This callback is called after resetting master status
259 
260      This is called when executing the command RESET MASTER, and is
261      used to reset status variables added by observers.
262 
263      @param param Observer common parameter
264 
265      @retval 0 Sucess
266      @retval 1 Failure
267   */
268   int (*after_reset_master)(Binlog_transmit_param *param);
269 } Binlog_transmit_observer;
270 
271 /**
272    Binlog relay IO flags
273 */
274 enum Binlog_relay_IO_flags {
275   /** Binary relay log was sync:ed */
276   BINLOG_RELAY_IS_SYNCED = 1
277 };
278 
279 
280 /**
281   Replication binlog relay IO observer parameter
282 */
283 typedef struct Binlog_relay_IO_param {
284   uint32 server_id;
285 
286   /* Master host, user and port */
287   char *host;
288   char *user;
289   unsigned int port;
290 
291   char *master_log_name;
292   my_off_t master_log_pos;
293 
294   MYSQL *mysql;                        /* the connection to master */
295 } Binlog_relay_IO_param;
296 
297 /**
298    Observes and extends the service of slave IO thread.
299 */
300 typedef struct Binlog_relay_IO_observer {
301   uint32 len;
302 
303   /**
304      This callback is called when slave IO thread starts
305 
306      @param param Observer common parameter
307 
308      @retval 0 Sucess
309      @retval 1 Failure
310   */
311   int (*thread_start)(Binlog_relay_IO_param *param);
312 
313   /**
314      This callback is called when slave IO thread stops
315 
316      @param param Observer common parameter
317 
318      @retval 0 Sucess
319      @retval 1 Failure
320   */
321   int (*thread_stop)(Binlog_relay_IO_param *param);
322 
323   /**
324      This callback is called before slave requesting binlog transmission from master
325 
326      This is called before slave issuing BINLOG_DUMP command to master
327      to request binlog.
328 
329      @param param Observer common parameter
330      @param flags binlog dump flags
331 
332      @retval 0 Sucess
333      @retval 1 Failure
334   */
335   int (*before_request_transmit)(Binlog_relay_IO_param *param, uint32 flags);
336 
337   /**
338      This callback is called after read an event packet from master
339 
340      @param param Observer common parameter
341      @param packet The event packet read from master
342      @param len Length of the event packet read from master
343      @param event_buf The event packet return after process
344      @param event_len The length of event packet return after process
345 
346      @retval 0 Sucess
347      @retval 1 Failure
348   */
349   int (*after_read_event)(Binlog_relay_IO_param *param,
350                           const char *packet, unsigned long len,
351                           const char **event_buf, unsigned long *event_len);
352 
353   /**
354      This callback is called after written an event packet to relay log
355 
356      @param param Observer common parameter
357      @param event_buf Event packet written to relay log
358      @param event_len Length of the event packet written to relay log
359      @param flags flags for relay log
360 
361      @retval 0 Sucess
362      @retval 1 Failure
363   */
364   int (*after_queue_event)(Binlog_relay_IO_param *param,
365                            const char *event_buf, unsigned long event_len,
366                            uint32 flags);
367 
368   /**
369      This callback is called after reset slave relay log IO status
370 
371      @param param Observer common parameter
372 
373      @retval 0 Sucess
374      @retval 1 Failure
375   */
376   int (*after_reset_slave)(Binlog_relay_IO_param *param);
377 } Binlog_relay_IO_observer;
378 
379 
380 /**
381    Register a transaction observer
382 
383    @param observer The transaction observer to register
384    @param p pointer to the internal plugin structure
385 
386    @retval 0 Sucess
387    @retval 1 Observer already exists
388 */
389 int register_trans_observer(Trans_observer *observer, void *p);
390 
391 /**
392    Unregister a transaction observer
393 
394    @param observer The transaction observer to unregister
395    @param p pointer to the internal plugin structure
396 
397    @retval 0 Sucess
398    @retval 1 Observer not exists
399 */
400 int unregister_trans_observer(Trans_observer *observer, void *p);
401 
402 /**
403    Register a binlog storage observer
404 
405    @param observer The binlog storage observer to register
406    @param p pointer to the internal plugin structure
407 
408    @retval 0 Sucess
409    @retval 1 Observer already exists
410 */
411 int register_binlog_storage_observer(Binlog_storage_observer *observer, void *p);
412 
413 /**
414    Unregister a binlog storage observer
415 
416    @param observer The binlog storage observer to unregister
417    @param p pointer to the internal plugin structure
418 
419    @retval 0 Sucess
420    @retval 1 Observer not exists
421 */
422 int unregister_binlog_storage_observer(Binlog_storage_observer *observer, void *p);
423 
424 /**
425    Register a binlog transmit observer
426 
427    @param observer The binlog transmit observer to register
428    @param p pointer to the internal plugin structure
429 
430    @retval 0 Sucess
431    @retval 1 Observer already exists
432 */
433 int register_binlog_transmit_observer(Binlog_transmit_observer *observer, void *p);
434 
435 /**
436    Unregister a binlog transmit observer
437 
438    @param observer The binlog transmit observer to unregister
439    @param p pointer to the internal plugin structure
440 
441    @retval 0 Sucess
442    @retval 1 Observer not exists
443 */
444 int unregister_binlog_transmit_observer(Binlog_transmit_observer *observer, void *p);
445 
446 /**
447    Register a binlog relay IO (slave IO thread) observer
448 
449    @param observer The binlog relay IO observer to register
450    @param p pointer to the internal plugin structure
451 
452    @retval 0 Sucess
453    @retval 1 Observer already exists
454 */
455 int register_binlog_relay_io_observer(Binlog_relay_IO_observer *observer, void *p);
456 
457 /**
458    Unregister a binlog relay IO (slave IO thread) observer
459 
460    @param observer The binlog relay IO observer to unregister
461    @param p pointer to the internal plugin structure
462 
463    @retval 0 Sucess
464    @retval 1 Observer not exists
465 */
466 int unregister_binlog_relay_io_observer(Binlog_relay_IO_observer *observer, void *p);
467 
468 /**
469    Set thread entering a condition
470 
471    This function should be called before putting a thread to wait for
472    a condition. @a mutex should be held before calling this
473    function. After being waken up, @f thd_exit_cond should be called.
474 
475    @param thd      The thread entering the condition, NULL means current thread
476    @param cond     The condition the thread is going to wait for
477    @param mutex    The mutex associated with the condition, this must be
478                    held before call this function
479    @param stage    The new process message for the thread
480    @param old_stage The old process message for the thread
481    @param src_function The caller source function name
482    @param src_file The caller source file name
483    @param src_line The caller source line number
484 */
485 void thd_enter_cond(MYSQL_THD thd, mysql_cond_t *cond, mysql_mutex_t *mutex,
486                     const PSI_stage_info *stage, PSI_stage_info *old_stage,
487                     const char *src_function, const char *src_file,
488                     int src_line);
489 
490 #define THD_ENTER_COND(P1, P2, P3, P4, P5) \
491   thd_enter_cond(P1, P2, P3, P4, P5, __func__, __FILE__, __LINE__)
492 
493 /**
494    Set thread leaving a condition
495 
496    This function should be called after a thread being waken up for a
497    condition.
498 
499    @param thd      The thread entering the condition, NULL means current thread
500    @param stage    The process message, ususally this should be the old process
501                    message before calling @f thd_enter_cond
502    @param src_function The caller source function name
503    @param src_file The caller source file name
504    @param src_line The caller source line number
505 */
506 void thd_exit_cond(MYSQL_THD thd, const PSI_stage_info *stage,
507                    const char *src_function, const char *src_file,
508                    int src_line);
509 
510 #define THD_EXIT_COND(P1, P2) \
511   thd_exit_cond(P1, P2, __func__, __FILE__, __LINE__)
512 
513 /**
514    Get the value of user variable as an integer.
515 
516    This function will return the value of variable @a name as an
517    integer. If the original value of the variable is not an integer,
518    the value will be converted into an integer.
519 
520    @param name     user variable name
521    @param value    pointer to return the value
522    @param null_value if not NULL, the function will set it to true if
523    the value of variable is null, set to false if not
524 
525    @retval 0 Success
526    @retval 1 Variable not found
527 */
528 int get_user_var_int(const char *name,
529                      long long int *value, int *null_value);
530 
531 /**
532    Get the value of user variable as a double precision float number.
533 
534    This function will return the value of variable @a name as real
535    number. If the original value of the variable is not a real number,
536    the value will be converted into a real number.
537 
538    @param name     user variable name
539    @param value    pointer to return the value
540    @param null_value if not NULL, the function will set it to true if
541    the value of variable is null, set to false if not
542 
543    @retval 0 Success
544    @retval 1 Variable not found
545 */
546 int get_user_var_real(const char *name,
547                       double *value, int *null_value);
548 
549 /**
550    Get the value of user variable as a string.
551 
552    This function will return the value of variable @a name as
553    string. If the original value of the variable is not a string,
554    the value will be converted into a string.
555 
556    @param name     user variable name
557    @param value    pointer to the value buffer
558    @param len      length of the value buffer
559    @param precision precision of the value if it is a float number
560    @param null_value if not NULL, the function will set it to true if
561    the value of variable is null, set to false if not
562 
563    @retval 0 Success
564    @retval 1 Variable not found
565 */
566 int get_user_var_str(const char *name,
567                      char *value, unsigned long len,
568                      unsigned int precision, int *null_value);
569 
570 
571 
572 #ifdef __cplusplus
573 }
574 #endif
575 #endif /* REPLICATION_H */
576