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.

550 lines
15 KiB

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