control_events.h 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  1. /* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License, version 2.0,
  4. as published by the Free Software Foundation.
  5. This program is also distributed with certain software (including
  6. but not limited to OpenSSL) that is licensed under separate terms,
  7. as designated in a particular file or component or in included license
  8. documentation. The authors of MySQL hereby grant you an additional
  9. permission to link the program and your derivative works with the
  10. separately licensed software that they have included with MySQL.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License, version 2.0, for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  18. /**
  19. @addtogroup Replication
  20. @{
  21. @file control_events.h
  22. @brief Contains the classes representing events operating in the replication
  23. stream properties. Each event is represented as a byte sequence with logical
  24. divisions as event header, event specific data and event footer. The header
  25. and footer are common to all the events and are represented as two different
  26. subclasses.
  27. */
  28. #ifndef CONTROL_EVENT_INCLUDED
  29. #define CONTROL_EVENT_INCLUDED
  30. #include <sys/types.h>
  31. #include <time.h>
  32. #include <list>
  33. #include <map>
  34. #include <vector>
  35. #include "binlog_event.h"
  36. #include "template_utils.h"
  37. #include "uuid.h"
  38. namespace binary_log {
  39. /**
  40. @class Rotate_event
  41. When a binary log file exceeds a size limit, a ROTATE_EVENT is written
  42. at the end of the file that points to the next file in the squence.
  43. This event is information for the slave to know the name of the next
  44. binary log it is going to receive.
  45. ROTATE_EVENT is generated locally and written to the binary log
  46. on the master. It is written to the relay log on the slave when FLUSH LOGS
  47. occurs, and when receiving a ROTATE_EVENT from the master.
  48. In the latter case, there will be two rotate events in total originating
  49. on different servers.
  50. @section Rotate_event_binary_format Binary Format
  51. <table>
  52. <caption>Post-Header for Rotate_event</caption>
  53. <tr>
  54. <th>Name</th>
  55. <th>Format</th>
  56. <th>Description</th>
  57. </tr>
  58. <tr>
  59. <td>position</td>
  60. <td>8 byte integer</td>
  61. <td>The position within the binary log to rotate to.</td>
  62. </tr>
  63. </table>
  64. The Body has one component:
  65. <table>
  66. <caption>Body for Rotate_event</caption>
  67. <tr>
  68. <th>Name</th>
  69. <th>Format</th>
  70. <th>Description</th>
  71. </tr>
  72. <tr>
  73. <td>new_log_ident</td>
  74. <td>variable length string without trailing zero, extending to the
  75. end of the event (determined by the length field of the
  76. Common-Header)
  77. </td>
  78. <td>Name of the binlog to rotate to.</td>
  79. </tr>
  80. </table>
  81. */
  82. class Rotate_event : public Binary_log_event {
  83. public:
  84. const char *new_log_ident;
  85. size_t ident_len;
  86. unsigned int flags;
  87. uint64_t pos;
  88. enum {
  89. /* Values taken by the flag member variable */
  90. DUP_NAME = 2, // if constructor should dup the string argument
  91. RELAY_LOG = 4 // rotate event for the relay log
  92. };
  93. enum {
  94. /* Rotate event post_header */
  95. R_POS_OFFSET = 0,
  96. R_IDENT_OFFSET = 8
  97. };
  98. /**
  99. This is the minimal constructor, it will set the type code as ROTATE_EVENT.
  100. */
  101. Rotate_event(const char *new_log_ident_arg, size_t ident_len_arg,
  102. unsigned int flags_arg, uint64_t pos_arg)
  103. : Binary_log_event(ROTATE_EVENT),
  104. new_log_ident(new_log_ident_arg),
  105. ident_len(ident_len_arg ? ident_len_arg : strlen(new_log_ident_arg)),
  106. flags(flags_arg),
  107. pos(pos_arg) {}
  108. /**
  109. The layout of Rotate_event data part is as follows:
  110. <pre>
  111. +-----------------------------------------------------------------------+
  112. | common_header | post_header | position of the first event | file name |
  113. +-----------------------------------------------------------------------+
  114. </pre>
  115. @param buf Contains the serialized event.
  116. @param fde An FDE event, used to get the following information:
  117. -binlog_version
  118. -server_version
  119. -post_header_len
  120. -common_header_len
  121. The content of this object depends on the binlog-version
  122. currently in use.
  123. */
  124. Rotate_event(const char *buf, const Format_description_event *fde);
  125. #ifndef HAVE_MYSYS
  126. void print_event_info(std::ostream &);
  127. void print_long_info(std::ostream &);
  128. #endif
  129. ~Rotate_event() {
  130. if (flags & DUP_NAME) bapi_free(const_cast<char *>(new_log_ident));
  131. }
  132. };
  133. /**
  134. @class Format_description_event
  135. For binlog version 4.
  136. This event is saved by threads which read it, as they need it for future
  137. use (to decode the ordinary events).
  138. @section Format_description_event_binary_format Binary Format
  139. The Post-Header has six components:
  140. <table>
  141. <caption>Post-Header for Format_description_event</caption>
  142. <tr>
  143. <th>Name</th>
  144. <th>Format</th>
  145. <th>Description</th>
  146. </tr>
  147. <tr>
  148. <td>created</td>
  149. <td>4 byte unsigned integer</td>
  150. <td>The creation timestamp, if non-zero,
  151. is the time in seconds when this event was created</td>
  152. </tr>
  153. <tr>
  154. <td>binlog_version</td>
  155. <td>2 byte unsigned integer</td>
  156. <td>This is 1 in MySQL 3.23 and 3 in MySQL 4.0 and 4.1
  157. (In MySQL 5.0 and up, FORMAT_DESCRIPTION_EVENT is
  158. used instead of START_EVENT_V3 and for them its 4).</td>
  159. </tr>
  160. <tr>
  161. <td>server_version</td>
  162. <td>char array of 50 bytes</td>
  163. <td>The MySQL server's version (example: 4.0.14-debug-log),
  164. padded with 0x00 bytes on the right</td>
  165. </tr>
  166. <tr>
  167. <td>common_header_len</td>
  168. <td>1 byte unsigned integer</td>
  169. <td>The length of the event header. This value includes the extra_headers
  170. field, so this header length - 19 yields the size
  171. of the extra_headers field.</td>
  172. </tr>
  173. <tr>
  174. <td>post_header_len</td>
  175. <td>array of type 1 byte unsigned integer</td>
  176. <td>The lengths for the fixed data part of each event</td>
  177. </tr>
  178. <tr>
  179. <td>server_version_split</td>
  180. <td>unsigned char array</td>
  181. <td>Stores the server version of the server
  182. and splits them in three parts</td>
  183. </tr>
  184. <tr>
  185. <td>number_of_event_types</td>
  186. <td>1 byte unsigned integer</td>
  187. <td>number of event types present in the server</td>
  188. </tr>
  189. </table>
  190. */
  191. class Format_description_event : public Binary_log_event {
  192. public:
  193. /**
  194. If this event is at the start of the first binary log since server
  195. startup 'created' should be the timestamp when the event (and the
  196. binary log) was created. In the other case (i.e. this event is at
  197. the start of a binary log created by FLUSH LOGS or automatic
  198. rotation), 'created' should be 0. This "trick" is used by MySQL
  199. >=4.0.14 slaves to know whether they must drop stale temporary
  200. tables and whether they should abort unfinished transaction.
  201. Note that when 'created'!=0, it is always equal to the event's
  202. timestamp; indeed Format_description_event is written only in binlog.cc
  203. where the first constructor below is called, in which 'created' is set to
  204. 'when'. So in fact 'created' is a useless variable. When it is 0 we can
  205. read the actual value from timestamp ('when') and when it is non-zero we
  206. can read the same value from timestamp
  207. ('when'). Conclusion:
  208. - we use timestamp to print when the binlog was created.
  209. - we use 'created' only to know if this is a first binlog or not.
  210. */
  211. time_t created;
  212. uint16_t binlog_version;
  213. char server_version[ST_SERVER_VER_LEN];
  214. /*
  215. We set this to 1 if we don't want to have the created time in the log,
  216. which is the case when we rollover to a new log.
  217. */
  218. bool dont_set_created;
  219. /**
  220. The size of the fixed header which _all_ events have
  221. (for binlogs written by this version, this is equal to
  222. LOG_EVENT_HEADER_LEN), except FORMAT_DESCRIPTION_EVENT and ROTATE_EVENT
  223. (those have a header of size LOG_EVENT_MINIMAL_HEADER_LEN).
  224. */
  225. uint8_t common_header_len;
  226. /*
  227. The list of post-headers' lengths followed
  228. by the checksum alg decription byte
  229. */
  230. std::vector<uint8_t> post_header_len;
  231. unsigned char server_version_split[ST_SERVER_VER_SPLIT_LEN];
  232. /**
  233. Format_description_event 1st constructor.
  234. This constructor can be used to create the event to write to the binary log
  235. (when the server starts or when FLUSH LOGS)
  236. @param binlog_ver the binlog version for which we want to build
  237. an event. It should only be 4, old versions are not compatible anymore
  238. since 8.0.2.
  239. @param server_ver The MySQL server's version.
  240. */
  241. Format_description_event(uint8_t binlog_ver, const char *server_ver);
  242. /**
  243. The layout of Format_description_event data part is as follows:
  244. <pre>
  245. +=====================================+
  246. | event | binlog_version 19 : 2 | = 4
  247. | data +----------------------------+
  248. | | server_version 21 : 50 |
  249. | +----------------------------+
  250. | | create_timestamp 71 : 4 |
  251. | +----------------------------+
  252. | | header_length 75 : 1 |
  253. | +----------------------------+
  254. | | post-header 76 : n | = array of n bytes, one byte
  255. | | lengths for all | per event type that the
  256. | | event types | server knows about
  257. +=====================================+
  258. </pre>
  259. @param buf Contains the serialized event.
  260. @param fde An FDE event (see Rotate_event constructor for more info).
  261. @note The fde passed to this constructor was created through another
  262. constructor of FDE class.
  263. */
  264. Format_description_event(const char *buf,
  265. const Format_description_event *fde);
  266. Format_description_event(const Format_description_event &) = default;
  267. Format_description_event &operator=(const Format_description_event &) =
  268. default;
  269. uint8_t number_of_event_types;
  270. /**
  271. This method is used to find out the version of server that originated
  272. the current FD instance.
  273. @return the version of server.
  274. */
  275. unsigned long get_product_version() const;
  276. /**
  277. This method checks the MySQL version to determine whether checksums may be
  278. present in the events contained in the binary log.
  279. @retval true if the event's version is earlier than one that introduced
  280. the replication event checksum.
  281. @retval false otherwise.
  282. */
  283. bool is_version_before_checksum() const;
  284. /**
  285. This method populates the array server_version_split which is then used for
  286. lookups to find if the server which created this event has some known bug.
  287. */
  288. void calc_server_version_split();
  289. #ifndef HAVE_MYSYS
  290. void print_event_info(std::ostream &info);
  291. void print_long_info(std::ostream &info);
  292. #endif
  293. ~Format_description_event();
  294. bool header_is_valid() const {
  295. return ((common_header_len >= LOG_EVENT_MINIMAL_HEADER_LEN) &&
  296. (!post_header_len.empty()));
  297. }
  298. bool version_is_valid() const {
  299. /* It is invalid only when all version numbers are 0 */
  300. return server_version_split[0] != 0 || server_version_split[1] != 0 ||
  301. server_version_split[2] != 0;
  302. }
  303. };
  304. /**
  305. @class Stop_event
  306. A stop event is written to the log files under these circumstances:
  307. - A master writes the event to the binary log when it shuts down.
  308. - A slave writes the event to the relay log when it shuts down or
  309. when a RESET SLAVE statement is executed.
  310. @section Stop_event_binary_format Binary Format
  311. The Post-Header and Body for this event type are empty; it only has
  312. the Common-Header.
  313. */
  314. class Stop_event : public Binary_log_event {
  315. public:
  316. /**
  317. It is the minimal constructor, and all it will do is set the type_code as
  318. STOP_EVENT in the header object in Binary_log_event.
  319. */
  320. Stop_event() : Binary_log_event(STOP_EVENT) {}
  321. /**
  322. A Stop_event is occurs under these circumstances:
  323. - A master writes the event to the binary log when it shuts down
  324. - A slave writes the event to the relay log when it shuts down or when a
  325. RESET SLAVE statement is executed
  326. @param buf Contains the serialized event.
  327. @param fde An FDE event (see Rotate_event constructor for more info).
  328. */
  329. Stop_event(const char *buf, const Format_description_event *fde);
  330. #ifndef HAVE_MYSYS
  331. void print_event_info(std::ostream &) {}
  332. void print_long_info(std::ostream &info);
  333. #endif
  334. };
  335. /**
  336. @class Incident_event
  337. Class representing an incident, an occurance out of the ordinary,
  338. that happened on the master.
  339. The event is used to inform the slave that something out of the
  340. ordinary happened on the master that might cause the database to be
  341. in an inconsistent state.
  342. @section Incident_event_binary_format Binary Format
  343. <table id="IncidentFormat">
  344. <caption>Incident event format</caption>
  345. <tr>
  346. <th>Symbol</th>
  347. <th>Format</th>
  348. <th>Description</th>
  349. </tr>
  350. <tr>
  351. <td>INCIDENT</td>
  352. <td align="right">2</td>
  353. <td>Incident number as an unsigned integer</td>
  354. </tr>
  355. <tr>
  356. <td>MSGLEN</td>
  357. <td align="right">1</td>
  358. <td>Message length as an unsigned integer</td>
  359. </tr>
  360. <tr>
  361. <td>MESSAGE</td>
  362. <td align="right">MSGLEN</td>
  363. <td>The message, if present. Not null terminated.</td>
  364. </tr>
  365. </table>
  366. */
  367. class Incident_event : public Binary_log_event {
  368. public:
  369. /**
  370. Enumeration of the incidents that can occur for the server.
  371. */
  372. enum enum_incident {
  373. /** No incident */
  374. INCIDENT_NONE = 0,
  375. /** There are possibly lost events in the replication stream */
  376. INCIDENT_LOST_EVENTS = 1,
  377. /** Shall be last event of the enumeration */
  378. INCIDENT_COUNT
  379. };
  380. enum_incident get_incident_type() { return incident; }
  381. char *get_message() { return message; }
  382. /**
  383. This will create an Incident_event with an empty message and set the
  384. type_code as INCIDENT_EVENT in the header object in Binary_log_event.
  385. */
  386. explicit Incident_event(enum_incident incident_arg)
  387. : Binary_log_event(INCIDENT_EVENT),
  388. incident(incident_arg),
  389. message(nullptr),
  390. message_length(0) {}
  391. /**
  392. Constructor of Incident_event
  393. The buffer layout is as follows:
  394. <pre>
  395. +-----------------------------------------------------+
  396. | Incident_number | message_length | Incident_message |
  397. +-----------------------------------------------------+
  398. </pre>
  399. Incident number codes are listed in binlog_evnet.h.
  400. The only code currently used is INCIDENT_LOST_EVENTS, which indicates that
  401. there may be lost events (a "gap") in the replication stream that requires
  402. databases to be resynchronized.
  403. @param buf Contains the serialized event.
  404. @param fde An FDE event (see Rotate_event constructor for more info).
  405. */
  406. Incident_event(const char *buf, const Format_description_event *fde);
  407. #ifndef HAVE_MYSYS
  408. void print_event_info(std::ostream &info);
  409. void print_long_info(std::ostream &info);
  410. #endif
  411. protected:
  412. enum_incident incident;
  413. char *message;
  414. size_t message_length;
  415. };
  416. /**
  417. @class Xid_event
  418. An XID event is generated for a commit of a transaction that modifies one or
  419. more tables of an XA-capable storage engine.
  420. @section Xid_event_binary_format Binary Format
  421. The Body has the following component:
  422. <table>
  423. <caption>Body for Xid_event</caption>
  424. <tr>
  425. <th>Name</th>
  426. <th>Format</th>
  427. <th>Description</th>
  428. </tr>
  429. <tr>
  430. <td>xid</td>
  431. <td>8 byte unsigned integer</td>
  432. <td>The XID transaction number.</td>
  433. </tr>
  434. </table>
  435. The Post-Header and Body for this event type are empty; it only has
  436. the common header.
  437. */
  438. class Xid_event : public Binary_log_event {
  439. public:
  440. /**
  441. The minimal constructor of Xid_event, it initializes the instance variable
  442. xid and set the type_code as XID_EVENT in the header object in
  443. Binary_log_event
  444. */
  445. explicit Xid_event(uint64_t xid_arg)
  446. : Binary_log_event(XID_EVENT), xid(xid_arg) {}
  447. /**
  448. An XID event is generated for a commit of a transaction that modifies one or
  449. more tables of an XA-capable storage engine
  450. @param buf Contains the serialized event.
  451. @param fde An FDE event (see Rotate_event constructor for more info).
  452. */
  453. Xid_event(const char *buf, const Format_description_event *fde);
  454. uint64_t xid;
  455. #ifndef HAVE_MYSYS
  456. void print_event_info(std::ostream &info);
  457. void print_long_info(std::ostream &info);
  458. #endif
  459. };
  460. /**
  461. @class XA_prepare_event
  462. An XA_prepare event is generated for a XA prepared transaction.
  463. Like Xid_event it contans XID of the *prepared* transaction.
  464. @section XA_prepare_event_binary_format Binary Format
  465. The Body has the following component:
  466. <table>
  467. <caption>Body for XA_prepare_event</caption>
  468. <tr>
  469. <th>Name</th>
  470. <th>Format</th>
  471. <th>Description</th>
  472. </tr>
  473. <tr>
  474. <td>my_xid</td>
  475. <td>a struct similar to mysql/plugin.h containing three members.</td>
  476. <td>serialized XID representation of XA transaction.</td>
  477. </tr>
  478. <tr>
  479. <td>xid</td>
  480. <td>a pointer to XID object.</td>
  481. <td>a reference to an object for mysql logger.</td>
  482. </tr>
  483. <tr>
  484. <td>one_phase</td>
  485. <td>a bool</td>
  486. <td>the value specifies the current XA transaction commit method.</td>
  487. </tr>
  488. </table>
  489. The Post-Header and Body for this event type are empty; it only has
  490. the common header.
  491. */
  492. class XA_prepare_event : public Binary_log_event {
  493. /*
  494. Struct def is copied from $MYSQL/include/mysql/plugin.h,
  495. consult there about fine details.
  496. */
  497. static const int MY_XIDDATASIZE = 128;
  498. struct MY_XID {
  499. long formatID;
  500. long gtrid_length;
  501. long bqual_length;
  502. char data[MY_XIDDATASIZE]; /* Not \0-terminated */
  503. };
  504. protected:
  505. /* size of serialization buffer is explained in $MYSQL/sql/xa.h. */
  506. static const uint16_t ser_buf_size =
  507. 8 + 2 * MY_XIDDATASIZE + 4 * sizeof(long) + 1;
  508. MY_XID my_xid;
  509. void *xid; /* Master side only */
  510. bool one_phase;
  511. public:
  512. /**
  513. The minimal constructor of XA_prepare_event, it initializes the
  514. instance variable xid and set the type_code as XID_EVENT in the
  515. header object in Binary_log_event
  516. */
  517. XA_prepare_event(void *xid_arg, bool oph_arg)
  518. : Binary_log_event(XA_PREPARE_LOG_EVENT),
  519. xid(xid_arg),
  520. one_phase(oph_arg) {}
  521. /**
  522. An XID event is generated for a commit of a transaction that modifies one or
  523. more tables of an XA-capable storage engine
  524. @param buf Contains the serialized event.
  525. @param fde An FDE event (see Rotate_event constructor for more info).
  526. */
  527. XA_prepare_event(const char *buf, const Format_description_event *fde);
  528. #ifndef HAVE_MYSYS
  529. /*
  530. todo: we need to find way how to exploit server's code of
  531. serialize_xid()
  532. */
  533. void print_event_info(std::ostream &) {}
  534. void print_long_info(std::ostream &) {}
  535. #endif
  536. };
  537. /**
  538. @class Ignorable_event
  539. Base class for ignorable log events. Events deriving from
  540. this class can be safely ignored by slaves that cannot
  541. recognize them. Newer slaves, will be able to read and
  542. handle them. This has been designed to be an open-ended
  543. architecture, so adding new derived events shall not harm
  544. the old slaves that support ignorable log event mechanism
  545. (they will just ignore unrecognized ignorable events).
  546. @note The only thing that makes an event ignorable is that it has
  547. the LOG_EVENT_IGNORABLE_F flag set. It is not strictly necessary
  548. that ignorable event types derive from Ignorable_event; they may
  549. just as well derive from Binary_log_event and Log_event and pass
  550. LOG_EVENT_IGNORABLE_F as argument to the Log_event constructor.
  551. @section Ignoarble_event_binary_format Binary format
  552. The Post-Header and Body for this event type are empty; it only has
  553. the Common-Header.
  554. */
  555. class Ignorable_event : public Binary_log_event {
  556. public:
  557. // buf is advanced in Binary_log_event constructor to point to beginning of
  558. // post-header
  559. /**
  560. The minimal constructor and all it will do is set the type_code as
  561. IGNORABLE_LOG_EVENT in the header object in Binary_log_event.
  562. */
  563. explicit Ignorable_event(Log_event_type type_arg = IGNORABLE_LOG_EVENT)
  564. : Binary_log_event(type_arg) {}
  565. /**
  566. @param buf Contains the serialized event.
  567. @param fde An FDE event (see Rotate_event constructor for more info).
  568. */
  569. Ignorable_event(const char *buf, const Format_description_event *fde);
  570. #ifndef HAVE_MYSYS
  571. void print_event_info(std::ostream &) {}
  572. void print_long_info(std::ostream &) {}
  573. #endif
  574. };
  575. /**
  576. @struct gtid_info
  577. Structure to hold the members declared in the class Gtid_log_event those
  578. member are objects of classes defined in server(rpl_gtid.h). As we can not
  579. move all the classes defined there(in rpl_gtid.h) in libbinlogevents so this
  580. structure was created, to provide a way to map the decoded value in Gtid_event
  581. ctor and the class members defined in rpl_gtid.h, these classes are also the
  582. members of Gtid_log_event(subclass of this in server code)
  583. The structure contains the following components.
  584. <table>
  585. <caption>Structure gtid_info</caption>
  586. <tr>
  587. <th>Name</th>
  588. <th>Format</th>
  589. <th>Description</th>
  590. </tr>
  591. <tr>
  592. <td>rpl_gtid_sidno</td>
  593. <td>4 bytes integer</td>
  594. <td>SIDNO (source ID number, first component of GTID)</td>
  595. </tr>
  596. <tr>
  597. <td>rpl_gtid_gno</td>
  598. <td>8 bytes integer</td>
  599. <td>GNO (group number, second component of GTID)</td>
  600. </tr>
  601. </table>
  602. */
  603. struct gtid_info {
  604. int32_t rpl_gtid_sidno;
  605. int64_t rpl_gtid_gno;
  606. };
  607. /**
  608. @class Gtid_event
  609. GTID stands for Global Transaction IDentifier
  610. It is composed of two parts:
  611. - SID for Source Identifier, and
  612. - GNO for Group Number.
  613. The basic idea is to
  614. - Associate an identifier, the Global Transaction IDentifier or GTID,
  615. to every transaction.
  616. - When a transaction is copied to a slave, re-executed on the slave,
  617. and written to the slave's binary log, the GTID is preserved.
  618. - When a slave connects to a master, the slave uses GTIDs instead of
  619. (file, offset)
  620. @section Gtid_event_binary_format Binary Format
  621. The Body can have up to nine components:
  622. <table>
  623. <caption>Body for Gtid_event</caption>
  624. <tr>
  625. <th>Name</th>
  626. <th>Format</th>
  627. <th>Description</th>
  628. </tr>
  629. <tr>
  630. <td>GTID_FLAGS</td>
  631. <td>1 byte</td>
  632. <td>00000001 = Transaction may have changes logged with SBR.
  633. In 5.6, 5.7.0-5.7.18, and 8.0.0-8.0.1, this flag is always set.
  634. Starting in 5.7.19 and 8.0.2, this flag is cleared if the transaction
  635. only contains row events. It is set if any part of the transaction is
  636. written in statement format.</td>
  637. </tr>
  638. <tr>
  639. <td>SID</td>
  640. <td>16 byte sequence</td>
  641. <td>UUID representing the SID</td>
  642. </tr>
  643. <tr>
  644. <td>GNO</td>
  645. <td>8 byte integer</td>
  646. <td>Group number, second component of GTID.</td>
  647. </tr>
  648. <tr>
  649. <td>logical clock timestamp typecode</td>
  650. <td>1 byte integer</td>
  651. <td>The type of logical timestamp used in the logical clock fields.</td>
  652. </tr>
  653. <tr>
  654. <td>last_committed</td>
  655. <td>8 byte integer</td>
  656. <td>Store the transaction's commit parent sequence_number</td>
  657. </tr>
  658. <tr>
  659. <td>sequence_number</td>
  660. <td>8 byte integer</td>
  661. <td>The transaction's logical timestamp assigned at prepare phase</td>
  662. </tr>
  663. <tr>
  664. <td>immediate_commit_timestamp</td>
  665. <td>7 byte integer</td>
  666. <td>Timestamp of commit on the immediate master</td>
  667. </tr>
  668. <tr>
  669. <td>original_commit_timestamp</td>
  670. <td>7 byte integer</td>
  671. <td>Timestamp of commit on the originating master</td>
  672. </tr>
  673. <tr>
  674. <td>transaction_length</td>
  675. <td>1 to 9 byte integer // See net_length_size(ulonglong num)</td>
  676. <td>The packed transaction's length in bytes, including the Gtid</td>
  677. </tr>
  678. <tr>
  679. <td>immediate_server_version</td>
  680. <td>4 byte integer</td>
  681. <td>Server version of the immediate server</td>
  682. </tr>
  683. <tr>
  684. <td>original_server_version</td>
  685. <td>4 byte integer</td>
  686. <td>Version of the server where the transaction was originally executed</td>
  687. </tr>
  688. </table>
  689. */
  690. class Gtid_event : public Binary_log_event {
  691. public:
  692. /*
  693. The transaction's logical timestamps used for MTS: see
  694. Transaction_ctx::last_committed and
  695. Transaction_ctx::sequence_number for details.
  696. Note: Transaction_ctx is in the MySQL server code.
  697. */
  698. long long int last_committed;
  699. long long int sequence_number;
  700. /** GTID flags constants */
  701. unsigned const char FLAG_MAY_HAVE_SBR = 1;
  702. /** Transaction might have changes logged with SBR */
  703. bool may_have_sbr_stmts;
  704. /** Timestamp when the transaction was committed on the originating master. */
  705. unsigned long long int original_commit_timestamp;
  706. /** Timestamp when the transaction was committed on the nearest master. */
  707. unsigned long long int immediate_commit_timestamp;
  708. bool has_commit_timestamps;
  709. /** The length of the transaction in bytes. */
  710. unsigned long long int transaction_length;
  711. public:
  712. /**
  713. Ctor of Gtid_event
  714. The layout of the buffer is as follows
  715. <pre>
  716. +----------+---+---+-------+--------------+---------+----------+
  717. |gtid flags|SID|GNO|TS_TYPE|logical ts(:s)|commit ts|trx length|
  718. +----------+---+---+-------+------------------------+----------+
  719. </pre>
  720. TS_TYPE is from {G_COMMIT_TS2} singleton set of values
  721. Details on commit timestamps in Gtid_event(const char*...)
  722. @param buf Contains the serialized event.
  723. @param fde An FDE event (see Rotate_event constructor for more info).
  724. */
  725. Gtid_event(const char *buf, const Format_description_event *fde);
  726. /**
  727. Constructor.
  728. */
  729. explicit Gtid_event(long long int last_committed_arg,
  730. long long int sequence_number_arg,
  731. bool may_have_sbr_stmts_arg,
  732. unsigned long long int original_commit_timestamp_arg,
  733. unsigned long long int immediate_commit_timestamp_arg,
  734. uint32_t original_server_version_arg,
  735. uint32_t immediate_server_version_arg)
  736. : Binary_log_event(GTID_LOG_EVENT),
  737. last_committed(last_committed_arg),
  738. sequence_number(sequence_number_arg),
  739. may_have_sbr_stmts(may_have_sbr_stmts_arg),
  740. original_commit_timestamp(original_commit_timestamp_arg),
  741. immediate_commit_timestamp(immediate_commit_timestamp_arg),
  742. transaction_length(0),
  743. original_server_version(original_server_version_arg),
  744. immediate_server_version(immediate_server_version_arg) {}
  745. #ifndef HAVE_MYSYS
  746. // TODO(WL#7684): Implement the method print_event_info and print_long_info
  747. // for all the events supported in MySQL Binlog
  748. void print_event_info(std::ostream &) {}
  749. void print_long_info(std::ostream &) {}
  750. #endif
  751. protected:
  752. static const int ENCODED_FLAG_LENGTH = 1;
  753. static const int ENCODED_SID_LENGTH = 16; // Uuid::BYTE_LENGTH;
  754. static const int ENCODED_GNO_LENGTH = 8;
  755. /// Length of typecode for logical timestamps.
  756. static const int LOGICAL_TIMESTAMP_TYPECODE_LENGTH = 1;
  757. /// Length of two logical timestamps.
  758. static const int LOGICAL_TIMESTAMP_LENGTH = 16;
  759. // Type code used before the logical timestamps.
  760. static const int LOGICAL_TIMESTAMP_TYPECODE = 2;
  761. static const int IMMEDIATE_COMMIT_TIMESTAMP_LENGTH = 7;
  762. static const int ORIGINAL_COMMIT_TIMESTAMP_LENGTH = 7;
  763. // Length of two timestamps (from original/immediate masters)
  764. static const int FULL_COMMIT_TIMESTAMP_LENGTH =
  765. IMMEDIATE_COMMIT_TIMESTAMP_LENGTH + ORIGINAL_COMMIT_TIMESTAMP_LENGTH;
  766. // We use 7 bytes out of which 1 bit is used as a flag.
  767. static const int ENCODED_COMMIT_TIMESTAMP_LENGTH = 55;
  768. // Minimum and maximum lengths of transaction length field.
  769. static const int TRANSACTION_LENGTH_MIN_LENGTH = 1;
  770. static const int TRANSACTION_LENGTH_MAX_LENGTH = 9;
  771. /// Length of original_server_version
  772. static const int ORIGINAL_SERVER_VERSION_LENGTH = 4;
  773. /// Length of immediate_server_version
  774. static const int IMMEDIATE_SERVER_VERSION_LENGTH = 4;
  775. /// Length of original and immediate server version
  776. static const int FULL_SERVER_VERSION_LENGTH =
  777. ORIGINAL_SERVER_VERSION_LENGTH + IMMEDIATE_SERVER_VERSION_LENGTH;
  778. // We use 4 bytes out of which 1 bit is used as a flag.
  779. static const int ENCODED_SERVER_VERSION_LENGTH = 31;
  780. /* We have only original commit timestamp if both timestamps are equal. */
  781. int get_commit_timestamp_length() const {
  782. if (original_commit_timestamp != immediate_commit_timestamp)
  783. return FULL_COMMIT_TIMESTAMP_LENGTH;
  784. return ORIGINAL_COMMIT_TIMESTAMP_LENGTH;
  785. }
  786. /**
  787. We only store the immediate_server_version if both server versions are the
  788. same.
  789. */
  790. int get_server_version_length() const {
  791. if (original_server_version != immediate_server_version)
  792. return FULL_SERVER_VERSION_LENGTH;
  793. return IMMEDIATE_SERVER_VERSION_LENGTH;
  794. }
  795. gtid_info gtid_info_struct;
  796. Uuid Uuid_parent_struct;
  797. /* Minimum GNO expected in a serialized GTID event */
  798. static const int64_t MIN_GNO = 1;
  799. /* Maximum GNO expected in a serialized GTID event */
  800. static const int64_t MAX_GNO = LLONG_MAX;
  801. public:
  802. /// Total length of post header
  803. static const int POST_HEADER_LENGTH =
  804. ENCODED_FLAG_LENGTH + /* flags */
  805. ENCODED_SID_LENGTH + /* SID length */
  806. ENCODED_GNO_LENGTH + /* GNO length */
  807. LOGICAL_TIMESTAMP_TYPECODE_LENGTH + /* length of typecode */
  808. LOGICAL_TIMESTAMP_LENGTH; /* length of two logical timestamps */
  809. /*
  810. We keep the commit timestamps in the body section because they can be of
  811. variable length.
  812. On the originating master, the event has only one timestamp as the two
  813. timestamps are equal. On every other server we have two timestamps.
  814. */
  815. static const int MAX_DATA_LENGTH = FULL_COMMIT_TIMESTAMP_LENGTH +
  816. TRANSACTION_LENGTH_MAX_LENGTH +
  817. FULL_SERVER_VERSION_LENGTH;
  818. static const int MAX_EVENT_LENGTH =
  819. LOG_EVENT_HEADER_LEN + POST_HEADER_LENGTH + MAX_DATA_LENGTH;
  820. /**
  821. Set the transaction length information.
  822. This function should be used when the full transaction length (including
  823. the Gtid event length) is known.
  824. @param transaction_length_arg The transaction length.
  825. */
  826. void set_trx_length(unsigned long long int transaction_length_arg) {
  827. transaction_length = transaction_length_arg;
  828. }
  829. /** The version of the server where the transaction was originally executed */
  830. uint32_t original_server_version;
  831. /** The version of the immediate server */
  832. uint32_t immediate_server_version;
  833. };
  834. /**
  835. @class Previous_gtids_event
  836. @section Previous_gtids_event_binary_format Binary Format
  837. The Post-Header for this event type is empty. The Body has two
  838. components:
  839. <table>
  840. <caption>Body for Previous_gtids_event</caption>
  841. <tr>
  842. <th>Name</th>
  843. <th>Format</th>
  844. <th>Description</th>
  845. </tr>
  846. <tr>
  847. <td>buf</td>
  848. <td>unsigned char array</td>
  849. <td>It contains the Gtids executed in the
  850. last binary log file.</td>
  851. </tr>
  852. <tr>
  853. <td>buf_size</td>
  854. <td>4 byte integer</td>
  855. <td>Size of the above buffer</td>
  856. </tr>
  857. </table>
  858. */
  859. class Previous_gtids_event : public Binary_log_event {
  860. public:
  861. /**
  862. Decodes the gtid_executed in the last binlog file
  863. <pre>
  864. The buffer layout is as follows
  865. +--------------------------------------------+
  866. | Gtids executed in the last binary log file |
  867. +--------------------------------------------+
  868. </pre>
  869. @param buf Contains the serialized event.
  870. @param fde An FDE event (see Rotate_event constructor for more info).
  871. */
  872. Previous_gtids_event(const char *buf, const Format_description_event *fde);
  873. /**
  874. This is the minimal constructor, and set the
  875. type_code as PREVIOUS_GTIDS_LOG_EVENT in the header object in
  876. Binary_log_event
  877. */
  878. Previous_gtids_event() : Binary_log_event(PREVIOUS_GTIDS_LOG_EVENT) {}
  879. #ifndef HAVE_MYSYS
  880. // TODO(WL#7684): Implement the method print_event_info and print_long_info
  881. // for all the events supported in MySQL Binlog
  882. void print_event_info(std::ostream &) {}
  883. void print_long_info(std::ostream &) {}
  884. #endif
  885. protected:
  886. size_t buf_size;
  887. const unsigned char *buf;
  888. };
  889. /**
  890. @class Transaction_context_event
  891. This class is used to combine the information of the ongoing transaction
  892. including the write set and other information of the thread executing the
  893. transaction.
  894. <tr>
  895. <th>Name</th>
  896. <th>Format</th>
  897. <th>Description</th>
  898. </tr>
  899. <tr>
  900. <td>thread_id</td>
  901. <td>4 byte integer</td>
  902. <td>The identifier for the thread executing the transaction.</td>
  903. </tr>
  904. <tr>
  905. <td>gtid_specified</td>
  906. <td>bool type variable</td>
  907. <td>Variable to identify whether the Gtid have been specified for the
  908. ongoing transaction or not.
  909. </td>
  910. </tr>
  911. <tr>
  912. <td>encoded_snapshot_version</td>
  913. <td>unsigned char array</td>
  914. <td>A gtid_set which is used to store the transaction set used for
  915. conflict detection.</td>
  916. </tr>
  917. <tr>
  918. <td>encoded_snapshot_version_length</td>
  919. <td>4 byte integer</td>
  920. <td>Length of the above char array.</td>
  921. </tr>
  922. <tr>
  923. <td>write_set</td>
  924. <td>variable length list to store the hash values. </td>
  925. <td>Used to store the hash values of the rows identifier for the rows
  926. which have changed in the ongoing transaction.
  927. </td>
  928. </tr>
  929. <tr>
  930. <td>read_set</td>
  931. <td>variable length list to store the read set values. Currently empty.
  932. </td> <td>Will be used to store the read set values of the current
  933. transaction.</td>
  934. </tr>
  935. */
  936. class Transaction_context_event : public Binary_log_event {
  937. public:
  938. /**
  939. Decodes the transaction_context_log_event of the ongoing transaction.
  940. <pre>
  941. The buffer layout is as follows
  942. </pre>
  943. @param buf Contains the serialized event.
  944. @param fde An FDE event (see Rotate_event constructor for more info).
  945. */
  946. Transaction_context_event(const char *buf,
  947. const Format_description_event *fde);
  948. Transaction_context_event(unsigned int thread_id_arg,
  949. bool is_gtid_specified_arg)
  950. : Binary_log_event(TRANSACTION_CONTEXT_EVENT),
  951. thread_id(thread_id_arg),
  952. gtid_specified(is_gtid_specified_arg) {}
  953. virtual ~Transaction_context_event();
  954. static const char *read_data_set(const char *pos, uint32_t set_len,
  955. std::list<const char *> *set,
  956. uint32_t remaining_buffer);
  957. static void clear_set(std::list<const char *> *set);
  958. #ifndef HAVE_MYSYS
  959. void print_event_info(std::ostream &) {}
  960. void print_long_info(std::ostream &) {}
  961. #endif
  962. protected:
  963. const char *server_uuid;
  964. uint32_t thread_id;
  965. bool gtid_specified;
  966. const unsigned char *encoded_snapshot_version;
  967. uint32_t encoded_snapshot_version_length;
  968. std::list<const char *> write_set;
  969. std::list<const char *> read_set;
  970. // The values mentioned on the next class constants is the offset where the
  971. // data that will be copied in the buffer.
  972. // 1 byte length.
  973. static const int ENCODED_SERVER_UUID_LEN_OFFSET = 0;
  974. // 4 bytes length.
  975. static const int ENCODED_THREAD_ID_OFFSET = 1;
  976. // 1 byte length.
  977. static const int ENCODED_GTID_SPECIFIED_OFFSET = 5;
  978. // 4 bytes length
  979. static const int ENCODED_SNAPSHOT_VERSION_LEN_OFFSET = 6;
  980. // 4 bytes length.
  981. static const int ENCODED_WRITE_SET_ITEMS_OFFSET = 10;
  982. // 4 bytes length.
  983. static const int ENCODED_READ_SET_ITEMS_OFFSET = 14;
  984. // The values mentioned on the next class's constants is the length of the
  985. // data that will be copied in the buffer.
  986. static const int ENCODED_READ_WRITE_SET_ITEM_LEN = 2;
  987. static const int ENCODED_SNAPSHOT_VERSION_LEN = 2;
  988. };
  989. /**
  990. @class View_change_event
  991. This class is used to add view change markers in the binary log when a
  992. member of the group enters or leaves the group.
  993. <tr>
  994. <th>Name</th>
  995. <th>Format</th>
  996. <th>Description</th>
  997. </tr>
  998. <tr>
  999. <td>view_id</td>
  1000. <td>40 length character array</td>
  1001. <td>This is used to store the view id value of the new view change when a
  1002. node add or leaves the group.
  1003. </td>
  1004. </tr>
  1005. <tr>
  1006. <td>seq_number</td>
  1007. <td>8 bytes integer</td>
  1008. <td>Variable to identify the next sequence number to be alloted to the
  1009. certified transaction.</td>
  1010. </tr>
  1011. <tr>
  1012. <td>certification_info</td>
  1013. <td>variable length map to store the certification data.</td>
  1014. <td>Map to store the certification info ie. the hash of write_set and the
  1015. snapshot sequence value.
  1016. </td>
  1017. </tr>
  1018. */
  1019. class View_change_event : public Binary_log_event {
  1020. public:
  1021. /**
  1022. Decodes the view_change_log_event generated incase a server enters or
  1023. leaves the group.
  1024. <pre>
  1025. The buffer layout is as follows
  1026. </pre>
  1027. @param buf Contains the serialized event.
  1028. @param fde An FDE event (see Rotate_event constructor for more info).
  1029. */
  1030. View_change_event(const char *buf, const Format_description_event *fde);
  1031. explicit View_change_event(const char *raw_view_id);
  1032. virtual ~View_change_event();
  1033. #ifndef HAVE_MYSYS
  1034. void print_event_info(std::ostream &) {}
  1035. void print_long_info(std::ostream &) {}
  1036. #endif
  1037. protected:
  1038. // The values mentioned on the next class constants is the offset where the
  1039. // data that will be copied in the buffer.
  1040. // 40 bytes length.
  1041. static const int ENCODED_VIEW_ID_OFFSET = 0;
  1042. // 8 bytes length.
  1043. static const int ENCODED_SEQ_NUMBER_OFFSET = 40;
  1044. // 4 bytes length.
  1045. static const int ENCODED_CERT_INFO_SIZE_OFFSET = 48;
  1046. /*
  1047. The layout of the buffer is as follows
  1048. +--------------------- -+-------------+----------+
  1049. | View Id | seq number | map size |
  1050. +-----------------------+-------------+----------+
  1051. view id (40 bytes) + seq number (8 bytes) + map size (4 bytes)
  1052. Sum of the length of the values at the above OFFSETS.
  1053. */
  1054. // The values mentioned on the next class constants is the length of the data
  1055. // that will be copied in the buffer.
  1056. // Field sizes on serialization
  1057. static const int ENCODED_VIEW_ID_MAX_LEN = 40;
  1058. static const int ENCODED_CERT_INFO_KEY_SIZE_LEN = 2;
  1059. static const int ENCODED_CERT_INFO_VALUE_LEN = 4;
  1060. char view_id[ENCODED_VIEW_ID_MAX_LEN];
  1061. long long int seq_number;
  1062. std::map<std::string, std::string> certification_info;
  1063. };
  1064. /**
  1065. @class Heartbeat_event
  1066. Replication event to ensure to slave that master is alive.
  1067. The event is originated by master's dump thread and sent straight to
  1068. slave without being logged. Slave itself does not store it in relay log
  1069. but rather uses a data for immediate checks and throws away the event.
  1070. Two members of the class log_ident and Binary_log_event::log_pos comprise
  1071. @see the rpl_event_coordinates instance. The coordinates that a heartbeat
  1072. instance carries correspond to the last event master has sent from
  1073. its binlog.
  1074. @section Heartbeat_event_binary_format Binary Format
  1075. The Body has one component:
  1076. <table>
  1077. <caption>Body for Heartbeat_event</caption>
  1078. <tr>
  1079. <th>Name</th>
  1080. <th>Format</th>
  1081. <th>Description</th>
  1082. </tr>
  1083. <tr>
  1084. <td>log_ident</td>
  1085. <td>variable length string without trailing zero, extending to the
  1086. end of the event</td>
  1087. <td>Name of the current binlog being written to.</td>
  1088. </tr>
  1089. </table>
  1090. */
  1091. class Heartbeat_event : public Binary_log_event {
  1092. public:
  1093. /**
  1094. Sent by a master to a slave to let the slave know that the master is
  1095. still alive. Events of this type do not appear in the binary or relay logs.
  1096. They are generated on a master server by the thread that dumps events and
  1097. sent straight to the slave without ever being written to the binary log.
  1098. @param buf Contains the serialized event.
  1099. @param fde An FDE event (see Rotate_event constructor for more info).
  1100. */
  1101. Heartbeat_event(const char *buf, const Format_description_event *fde);
  1102. const char *get_log_ident() { return log_ident; }
  1103. unsigned int get_ident_len() { return ident_len; }
  1104. protected:
  1105. const char *log_ident;
  1106. unsigned int ident_len; /** filename length */
  1107. };
  1108. } // end namespace binary_log
  1109. /**
  1110. @} (end of group Replication)
  1111. */
  1112. #endif /* CONTROL_EVENTS_INCLUDED */