You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

552 lines
15 KiB

  1. /* Copyright (C) 2008 MySQL AB, 2008-2009 Sun Microsystems, Inc
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. #ifndef REPLICATION_H
  13. #define REPLICATION_H
  14. #include <mysql.h>
  15. typedef struct st_mysql MYSQL;
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. /**
  20. Transaction observer flags.
  21. */
  22. enum Trans_flags {
  23. /** Transaction is a real transaction */
  24. TRANS_IS_REAL_TRANS = 1
  25. };
  26. /**
  27. Transaction observer parameter
  28. */
  29. typedef struct Trans_param {
  30. uint32 server_id;
  31. uint32 flags;
  32. /*
  33. The latest binary log file name and position written by current
  34. transaction, if binary log is disabled or no log event has been
  35. written into binary log file by current transaction (events
  36. written into transaction log cache are not counted), these two
  37. member will be zero.
  38. */
  39. const char *log_file;
  40. my_off_t log_pos;
  41. } Trans_param;
  42. /**
  43. Observes and extends transaction execution
  44. */
  45. typedef struct Trans_observer {
  46. uint32 len;
  47. /**
  48. This callback is called after transaction commit
  49. This callback is called right after commit to storage engines for
  50. transactional tables.
  51. For non-transactional tables, this is called at the end of the
  52. statement, before sending statement status, if the statement
  53. succeeded.
  54. @note The return value is currently ignored by the server.
  55. @param param The parameter for transaction observers
  56. @retval 0 Sucess
  57. @retval 1 Failure
  58. */
  59. int (*after_commit)(Trans_param *param);
  60. /**
  61. This callback is called after transaction rollback
  62. This callback is called right after rollback to storage engines
  63. for transactional tables.
  64. For non-transactional tables, this is called at the end of the
  65. statement, before sending statement status, if the statement
  66. failed.
  67. @note The return value is currently ignored by the server.
  68. @param param The parameter for transaction observers
  69. @retval 0 Sucess
  70. @retval 1 Failure
  71. */
  72. int (*after_rollback)(Trans_param *param);
  73. } Trans_observer;
  74. /**
  75. Binlog storage flags
  76. */
  77. enum Binlog_storage_flags {
  78. /** Binary log was sync:ed */
  79. BINLOG_STORAGE_IS_SYNCED = 1
  80. };
  81. /**
  82. Binlog storage observer parameters
  83. */
  84. typedef struct Binlog_storage_param {
  85. uint32 server_id;
  86. } Binlog_storage_param;
  87. /**
  88. Observe binlog logging storage
  89. */
  90. typedef struct Binlog_storage_observer {
  91. uint32 len;
  92. /**
  93. This callback is called after binlog has been flushed
  94. This callback is called after cached events have been flushed to
  95. binary log file. Whether the binary log file is synchronized to
  96. disk is indicated by the bit BINLOG_STORAGE_IS_SYNCED in @a flags.
  97. @param param Observer common parameter
  98. @param log_file Binlog file name been updated
  99. @param log_pos Binlog position after update
  100. @param flags flags for binlog storage
  101. @retval 0 Sucess
  102. @retval 1 Failure
  103. */
  104. int (*after_flush)(Binlog_storage_param *param,
  105. const char *log_file, my_off_t log_pos,
  106. uint32 flags);
  107. } Binlog_storage_observer;
  108. /**
  109. Replication binlog transmitter (binlog dump) observer parameter.
  110. */
  111. typedef struct Binlog_transmit_param {
  112. uint32 server_id;
  113. uint32 flags;
  114. } Binlog_transmit_param;
  115. /**
  116. Observe and extends the binlog dumping thread.
  117. */
  118. typedef struct Binlog_transmit_observer {
  119. uint32 len;
  120. /**
  121. This callback is called when binlog dumping starts
  122. @param param Observer common parameter
  123. @param log_file Binlog file name to transmit from
  124. @param log_pos Binlog position to transmit from
  125. @retval 0 Sucess
  126. @retval 1 Failure
  127. */
  128. int (*transmit_start)(Binlog_transmit_param *param,
  129. const char *log_file, my_off_t log_pos);
  130. /**
  131. This callback is called when binlog dumping stops
  132. @param param Observer common parameter
  133. @retval 0 Sucess
  134. @retval 1 Failure
  135. */
  136. int (*transmit_stop)(Binlog_transmit_param *param);
  137. /**
  138. This callback is called to reserve bytes in packet header for event transmission
  139. This callback is called when resetting transmit packet header to
  140. reserve bytes for this observer in packet header.
  141. The @a header buffer is allocated by the server code, and @a size
  142. is the size of the header buffer. Each observer can only reserve
  143. a maximum size of @a size in the header.
  144. @param param Observer common parameter
  145. @param header Pointer of the header buffer
  146. @param size Size of the header buffer
  147. @param len Header length reserved by this observer
  148. @retval 0 Sucess
  149. @retval 1 Failure
  150. */
  151. int (*reserve_header)(Binlog_transmit_param *param,
  152. unsigned char *header,
  153. unsigned long size,
  154. unsigned long *len);
  155. /**
  156. This callback is called before sending an event packet to slave
  157. @param param Observer common parameter
  158. @param packet Binlog event packet to send
  159. @param len Length of the event packet
  160. @param log_file Binlog file name of the event packet to send
  161. @param log_pos Binlog position of the event packet to send
  162. @retval 0 Sucess
  163. @retval 1 Failure
  164. */
  165. int (*before_send_event)(Binlog_transmit_param *param,
  166. unsigned char *packet, unsigned long len,
  167. const char *log_file, my_off_t log_pos );
  168. /**
  169. This callback is called after sending an event packet to slave
  170. @param param Observer common parameter
  171. @param event_buf Binlog event packet buffer sent
  172. @param len length of the event packet buffer
  173. @retval 0 Sucess
  174. @retval 1 Failure
  175. */
  176. int (*after_send_event)(Binlog_transmit_param *param,
  177. const char *event_buf, unsigned long len);
  178. /**
  179. This callback is called after resetting master status
  180. This is called when executing the command RESET MASTER, and is
  181. used to reset status variables added by observers.
  182. @param param Observer common parameter
  183. @retval 0 Sucess
  184. @retval 1 Failure
  185. */
  186. int (*after_reset_master)(Binlog_transmit_param *param);
  187. } Binlog_transmit_observer;
  188. /**
  189. Binlog relay IO flags
  190. */
  191. enum Binlog_relay_IO_flags {
  192. /** Binary relay log was sync:ed */
  193. BINLOG_RELAY_IS_SYNCED = 1
  194. };
  195. /**
  196. Replication binlog relay IO observer parameter
  197. */
  198. typedef struct Binlog_relay_IO_param {
  199. uint32 server_id;
  200. /* Master host, user and port */
  201. char *host;
  202. char *user;
  203. unsigned int port;
  204. char *master_log_name;
  205. my_off_t master_log_pos;
  206. MYSQL *mysql; /* the connection to master */
  207. } Binlog_relay_IO_param;
  208. /**
  209. Observes and extends the service of slave IO thread.
  210. */
  211. typedef struct Binlog_relay_IO_observer {
  212. uint32 len;
  213. /**
  214. This callback is called when slave IO thread starts
  215. @param param Observer common parameter
  216. @retval 0 Sucess
  217. @retval 1 Failure
  218. */
  219. int (*thread_start)(Binlog_relay_IO_param *param);
  220. /**
  221. This callback is called when slave IO thread stops
  222. @param param Observer common parameter
  223. @retval 0 Sucess
  224. @retval 1 Failure
  225. */
  226. int (*thread_stop)(Binlog_relay_IO_param *param);
  227. /**
  228. This callback is called before slave requesting binlog transmission from master
  229. This is called before slave issuing BINLOG_DUMP command to master
  230. to request binlog.
  231. @param param Observer common parameter
  232. @param flags binlog dump flags
  233. @retval 0 Sucess
  234. @retval 1 Failure
  235. */
  236. int (*before_request_transmit)(Binlog_relay_IO_param *param, uint32 flags);
  237. /**
  238. This callback is called after read an event packet from master
  239. @param param Observer common parameter
  240. @param packet The event packet read from master
  241. @param len Length of the event packet read from master
  242. @param event_buf The event packet return after process
  243. @param event_len The length of event packet return after process
  244. @retval 0 Sucess
  245. @retval 1 Failure
  246. */
  247. int (*after_read_event)(Binlog_relay_IO_param *param,
  248. const char *packet, unsigned long len,
  249. const char **event_buf, unsigned long *event_len);
  250. /**
  251. This callback is called after written an event packet to relay log
  252. @param param Observer common parameter
  253. @param event_buf Event packet written to relay log
  254. @param event_len Length of the event packet written to relay log
  255. @param flags flags for relay log
  256. @retval 0 Sucess
  257. @retval 1 Failure
  258. */
  259. int (*after_queue_event)(Binlog_relay_IO_param *param,
  260. const char *event_buf, unsigned long event_len,
  261. uint32 flags);
  262. /**
  263. This callback is called after reset slave relay log IO status
  264. @param param Observer common parameter
  265. @retval 0 Sucess
  266. @retval 1 Failure
  267. */
  268. int (*after_reset_slave)(Binlog_relay_IO_param *param);
  269. } Binlog_relay_IO_observer;
  270. /**
  271. Register a transaction observer
  272. @param observer The transaction observer to register
  273. @param p pointer to the internal plugin structure
  274. @retval 0 Sucess
  275. @retval 1 Observer already exists
  276. */
  277. int register_trans_observer(Trans_observer *observer, void *p);
  278. /**
  279. Unregister a transaction observer
  280. @param observer The transaction observer to unregister
  281. @param p pointer to the internal plugin structure
  282. @retval 0 Sucess
  283. @retval 1 Observer not exists
  284. */
  285. int unregister_trans_observer(Trans_observer *observer, void *p);
  286. /**
  287. Register a binlog storage observer
  288. @param observer The binlog storage observer to register
  289. @param p pointer to the internal plugin structure
  290. @retval 0 Sucess
  291. @retval 1 Observer already exists
  292. */
  293. int register_binlog_storage_observer(Binlog_storage_observer *observer, void *p);
  294. /**
  295. Unregister a binlog storage observer
  296. @param observer The binlog storage observer to unregister
  297. @param p pointer to the internal plugin structure
  298. @retval 0 Sucess
  299. @retval 1 Observer not exists
  300. */
  301. int unregister_binlog_storage_observer(Binlog_storage_observer *observer, void *p);
  302. /**
  303. Register a binlog transmit observer
  304. @param observer The binlog transmit observer to register
  305. @param p pointer to the internal plugin structure
  306. @retval 0 Sucess
  307. @retval 1 Observer already exists
  308. */
  309. int register_binlog_transmit_observer(Binlog_transmit_observer *observer, void *p);
  310. /**
  311. Unregister a binlog transmit observer
  312. @param observer The binlog transmit observer to unregister
  313. @param p pointer to the internal plugin structure
  314. @retval 0 Sucess
  315. @retval 1 Observer not exists
  316. */
  317. int unregister_binlog_transmit_observer(Binlog_transmit_observer *observer, void *p);
  318. /**
  319. Register a binlog relay IO (slave IO thread) observer
  320. @param observer The binlog relay IO observer to register
  321. @param p pointer to the internal plugin structure
  322. @retval 0 Sucess
  323. @retval 1 Observer already exists
  324. */
  325. int register_binlog_relay_io_observer(Binlog_relay_IO_observer *observer, void *p);
  326. /**
  327. Unregister a binlog relay IO (slave IO thread) observer
  328. @param observer The binlog relay IO observer to unregister
  329. @param p pointer to the internal plugin structure
  330. @retval 0 Sucess
  331. @retval 1 Observer not exists
  332. */
  333. int unregister_binlog_relay_io_observer(Binlog_relay_IO_observer *observer, void *p);
  334. /**
  335. Connect to master
  336. This function can only used in the slave I/O thread context, and
  337. will use the same master information to do the connection.
  338. @code
  339. MYSQL *mysql = mysql_init(NULL);
  340. if (rpl_connect_master(mysql))
  341. {
  342. // do stuff with the connection
  343. }
  344. mysql_close(mysql); // close the connection
  345. @endcode
  346. @param mysql address of MYSQL structure to use, pass NULL will
  347. create a new one
  348. @return address of MYSQL structure on success, NULL on failure
  349. */
  350. MYSQL *rpl_connect_master(MYSQL *mysql);
  351. /**
  352. Set thread entering a condition
  353. This function should be called before putting a thread to wait for
  354. a condition. @a mutex should be held before calling this
  355. function. After being waken up, @f thd_exit_cond should be called.
  356. @param thd The thread entering the condition, NULL means current thread
  357. @param cond The condition the thread is going to wait for
  358. @param mutex The mutex associated with the condition, this must be
  359. held before call this function
  360. @param msg The new process message for the thread
  361. */
  362. const char* thd_enter_cond(MYSQL_THD thd, mysql_cond_t *cond,
  363. mysql_mutex_t *mutex, const char *msg);
  364. /**
  365. Set thread leaving a condition
  366. This function should be called after a thread being waken up for a
  367. condition.
  368. @param thd The thread entering the condition, NULL means current thread
  369. @param old_msg The process message, ususally this should be the old process
  370. message before calling @f thd_enter_cond
  371. */
  372. void thd_exit_cond(MYSQL_THD thd, const char *old_msg);
  373. /**
  374. Get the value of user variable as an integer.
  375. This function will return the value of variable @a name as an
  376. integer. If the original value of the variable is not an integer,
  377. the value will be converted into an integer.
  378. @param name user variable name
  379. @param value pointer to return the value
  380. @param null_value if not NULL, the function will set it to true if
  381. the value of variable is null, set to false if not
  382. @retval 0 Success
  383. @retval 1 Variable not found
  384. */
  385. int get_user_var_int(const char *name,
  386. long long int *value, int *null_value);
  387. /**
  388. Get the value of user variable as a double precision float number.
  389. This function will return the value of variable @a name as real
  390. number. If the original value of the variable is not a real number,
  391. the value will be converted into a real number.
  392. @param name user variable name
  393. @param value pointer to return the value
  394. @param null_value if not NULL, the function will set it to true if
  395. the value of variable is null, set to false if not
  396. @retval 0 Success
  397. @retval 1 Variable not found
  398. */
  399. int get_user_var_real(const char *name,
  400. double *value, int *null_value);
  401. /**
  402. Get the value of user variable as a string.
  403. This function will return the value of variable @a name as
  404. string. If the original value of the variable is not a string,
  405. the value will be converted into a string.
  406. @param name user variable name
  407. @param value pointer to the value buffer
  408. @param len length of the value buffer
  409. @param precision precision of the value if it is a float number
  410. @param null_value if not NULL, the function will set it to true if
  411. the value of variable is null, set to false if not
  412. @retval 0 Success
  413. @retval 1 Variable not found
  414. */
  415. int get_user_var_str(const char *name,
  416. char *value, unsigned long len,
  417. unsigned int precision, int *null_value);
  418. #ifdef __cplusplus
  419. }
  420. #endif
  421. #endif /* REPLICATION_H */