1 /* Copyright (C) 2018 MariaDB Corporation AB
2 
3    This library is free software; you can redistribute it and/or
4    modify it under the terms of the GNU Library General Public
5    License as published by the Free Software Foundation; either
6    version 2 of the License, or (at your option) any later version.
7 
8    This library is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11    Library General Public License for more details.
12 
13    You should have received a copy of the GNU Library General Public
14    License along with this library; if not, write to the Free
15    Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16    MA 02111-1301, USA */
17 #ifndef _mariadb_rpl_h_
18 #define _mariadb_rpl_h_
19 
20 #ifdef	__cplusplus
21 extern "C" {
22 #endif
23 
24 #include <stdint.h>
25 
26 #define MARIADB_RPL_VERSION 0x0001
27 #define MARIADB_RPL_REQUIRED_VERSION 0x0001
28 
29 /* Protocol flags */
30 #define MARIADB_RPL_BINLOG_DUMP_NON_BLOCK 1
31 #define MARIADB_RPL_BINLOG_SEND_ANNOTATE_ROWS 2
32 #define MARIADB_RPL_IGNORE_HEARTBEAT (1 << 17)
33 
34 #define EVENT_HEADER_OFS 20
35 
36 #define FL_GROUP_COMMIT_ID 2
37 #define FL_STMT_END 1
38 
39 #define LOG_EVENT_ARTIFICIAL_F 0x20
40 
41 
42 /* Options */
43 enum mariadb_rpl_option {
44   MARIADB_RPL_FILENAME,       /* Filename and length */
45   MARIADB_RPL_START,          /* Start position */
46   MARIADB_RPL_SERVER_ID,      /* Server ID */
47   MARIADB_RPL_FLAGS,          /* Protocol flags */
48   MARIADB_RPL_GTID_CALLBACK,  /* GTID callback function */
49   MARIADB_RPL_GTID_DATA,      /* GTID data */
50   MARIADB_RPL_BUFFER
51 };
52 
53 /* Event types: From MariaDB Server sql/log_event.h */
54 enum mariadb_rpl_event {
55   UNKNOWN_EVENT= 0,
56   START_EVENT_V3= 1,
57   QUERY_EVENT= 2,
58   STOP_EVENT= 3,
59   ROTATE_EVENT= 4,
60   INTVAR_EVENT= 5,
61   LOAD_EVENT= 6,
62   SLAVE_EVENT= 7,
63   CREATE_FILE_EVENT= 8,
64   APPEND_BLOCK_EVENT= 9,
65   EXEC_LOAD_EVENT= 10,
66   DELETE_FILE_EVENT= 11,
67   NEW_LOAD_EVENT= 12,
68   RAND_EVENT= 13,
69   USER_VAR_EVENT= 14,
70   FORMAT_DESCRIPTION_EVENT= 15,
71   XID_EVENT= 16,
72   BEGIN_LOAD_QUERY_EVENT= 17,
73   EXECUTE_LOAD_QUERY_EVENT= 18,
74   TABLE_MAP_EVENT = 19,
75 
76   PRE_GA_WRITE_ROWS_EVENT = 20, /* deprecated */
77   PRE_GA_UPDATE_ROWS_EVENT = 21, /* deprecated */
78   PRE_GA_DELETE_ROWS_EVENT = 22, /* deprecated */
79 
80   WRITE_ROWS_EVENT_V1 = 23,
81   UPDATE_ROWS_EVENT_V1 = 24,
82   DELETE_ROWS_EVENT_V1 = 25,
83   INCIDENT_EVENT= 26,
84   HEARTBEAT_LOG_EVENT= 27,
85   IGNORABLE_LOG_EVENT= 28,
86   ROWS_QUERY_LOG_EVENT= 29,
87   WRITE_ROWS_EVENT = 30,
88   UPDATE_ROWS_EVENT = 31,
89   DELETE_ROWS_EVENT = 32,
90   GTID_LOG_EVENT= 33,
91   ANONYMOUS_GTID_LOG_EVENT= 34,
92   PREVIOUS_GTIDS_LOG_EVENT= 35,
93   TRANSACTION_CONTEXT_EVENT= 36,
94   VIEW_CHANGE_EVENT= 37,
95   XA_PREPARE_LOG_EVENT= 38,
96 
97   /*
98     Add new events here - right above this comment!
99     Existing events (except ENUM_END_EVENT) should never change their numbers
100   */
101 
102   /* New MySQL/Sun events are to be added right above this comment */
103   MYSQL_EVENTS_END,
104 
105   MARIA_EVENTS_BEGIN= 160,
106   ANNOTATE_ROWS_EVENT= 160,
107   BINLOG_CHECKPOINT_EVENT= 161,
108   GTID_EVENT= 162,
109   GTID_LIST_EVENT= 163,
110   START_ENCRYPTION_EVENT= 164,
111   QUERY_COMPRESSED_EVENT = 165,
112   WRITE_ROWS_COMPRESSED_EVENT_V1 = 166,
113   UPDATE_ROWS_COMPRESSED_EVENT_V1 = 167,
114   DELETE_ROWS_COMPRESSED_EVENT_V1 = 168,
115   WRITE_ROWS_COMPRESSED_EVENT = 169,
116   UPDATE_ROWS_COMPRESSED_EVENT = 170,
117   DELETE_ROWS_COMPRESSED_EVENT = 171,
118 
119   /* Add new MariaDB events here - right above this comment!  */
120 
121   ENUM_END_EVENT /* end marker */
122 };
123 
124 typedef struct {
125   char *str;
126   size_t length;
127 } MARIADB_STRING;
128 
129 enum mariadb_row_event_type {
130   WRITE_ROWS= 0,
131   UPDATE_ROWS= 1,
132   DELETE_ROWS= 2
133 };
134 
135 /* Global transaction id */
136 typedef struct st_mariadb_gtid {
137   unsigned int domain_id;
138   unsigned int server_id;
139   unsigned long long sequence_nr;
140 } MARIADB_GTID;
141 
142 /* Generic replication handle */
143 typedef struct st_mariadb_rpl {
144   unsigned int version;
145   MYSQL *mysql;
146   char *filename;
147   uint32_t filename_length;
148   unsigned char *buffer;
149   unsigned long buffer_size;
150   uint32_t server_id;
151   unsigned long start_position;
152   uint32_t flags;
153   uint8_t fd_header_len; /* header len from last format description event */
154   uint8_t use_checksum;
155 } MARIADB_RPL;
156 
157 /* Event header */
158 struct st_mariadb_rpl_rotate_event {
159   unsigned long long position;
160   MARIADB_STRING filename;
161 };
162 
163 struct st_mariadb_rpl_query_event {
164   uint32_t thread_id;
165   uint32_t seconds;
166   MARIADB_STRING database;
167   uint32_t errornr;
168   MARIADB_STRING status;
169   MARIADB_STRING statement;
170 };
171 
172 struct st_mariadb_rpl_gtid_list_event {
173   uint32_t gtid_cnt;
174   MARIADB_GTID *gtid;
175 };
176 
177 struct st_mariadb_rpl_format_description_event
178 {
179   uint16_t format;
180   char *server_version;
181   uint32_t timestamp;
182   uint8_t header_len;
183 };
184 
185 struct st_mariadb_rpl_checkpoint_event {
186   MARIADB_STRING filename;
187 };
188 
189 struct st_mariadb_rpl_xid_event {
190   uint64_t transaction_nr;
191 };
192 
193 struct st_mariadb_rpl_gtid_event {
194   uint64_t sequence_nr;
195   uint32_t domain_id;
196   uint8_t flags;
197   uint64_t commit_id;
198 };
199 
200 struct st_mariadb_rpl_annotate_rows_event {
201   MARIADB_STRING statement;
202 };
203 
204 struct st_mariadb_rpl_table_map_event {
205   unsigned long long table_id;
206   MARIADB_STRING database;
207   MARIADB_STRING table;
208   unsigned int column_count;
209   MARIADB_STRING column_types;
210   MARIADB_STRING metadata;
211   char *null_indicator;
212 };
213 
214 struct st_mariadb_rpl_rand_event {
215   unsigned long long first_seed;
216   unsigned long long second_seed;
217 };
218 
219 struct st_mariadb_rpl_encryption_event {
220   char scheme;
221   unsigned int key_version;
222   char *nonce;
223 };
224 
225 struct st_mariadb_rpl_intvar_event {
226   char type;
227   unsigned long long value;
228 };
229 
230 struct st_mariadb_rpl_uservar_event {
231   MARIADB_STRING name;
232   uint8_t is_null;
233   uint8_t type;
234   uint32_t charset_nr;
235   MARIADB_STRING value;
236   uint8_t flags;
237 };
238 
239 struct st_mariadb_rpl_rows_event {
240   enum mariadb_row_event_type type;
241   uint64_t table_id;
242   uint16_t flags;
243   uint32_t column_count;
244   char *column_bitmap;
245   char *column_update_bitmap;
246   size_t row_data_size;
247   void *row_data;
248 };
249 
250 struct st_mariadb_rpl_heartbeat_event {
251   uint32_t timestamp;
252   uint32_t next_position;
253   uint8_t type;
254   uint16_t flags;
255 };
256 
257 typedef struct st_mariadb_rpl_event
258 {
259   /* common header */
260   MA_MEM_ROOT memroot;
261   unsigned int checksum;
262   char ok;
263   enum mariadb_rpl_event event_type;
264   unsigned int timestamp;
265   unsigned int server_id;
266   unsigned int event_length;
267   unsigned int next_event_pos;
268   unsigned short flags;
269   /****************/
270   union {
271     struct st_mariadb_rpl_rotate_event rotate;
272     struct st_mariadb_rpl_query_event query;
273     struct st_mariadb_rpl_format_description_event format_description;
274     struct st_mariadb_rpl_gtid_list_event gtid_list;
275     struct st_mariadb_rpl_checkpoint_event checkpoint;
276     struct st_mariadb_rpl_xid_event xid;
277     struct st_mariadb_rpl_gtid_event gtid;
278     struct st_mariadb_rpl_annotate_rows_event annotate_rows;
279     struct st_mariadb_rpl_table_map_event table_map;
280     struct st_mariadb_rpl_rand_event rand;
281     struct st_mariadb_rpl_encryption_event encryption;
282     struct st_mariadb_rpl_intvar_event intvar;
283     struct st_mariadb_rpl_uservar_event uservar;
284     struct st_mariadb_rpl_rows_event rows;
285     struct st_mariadb_rpl_heartbeat_event heartbeat;
286   } event;
287 } MARIADB_RPL_EVENT;
288 
289 #define mariadb_rpl_init(a) mariadb_rpl_init_ex((a), MARIADB_RPL_VERSION)
290 
291 /* Function prototypes */
292 MARIADB_RPL * STDCALL mariadb_rpl_init_ex(MYSQL *mysql, unsigned int version);
293 
294 int mariadb_rpl_optionsv(MARIADB_RPL *rpl, enum mariadb_rpl_option, ...);
295 int mariadb_rpl_get_optionsv(MARIADB_RPL *rpl, enum mariadb_rpl_option, ...);
296 
297 int STDCALL mariadb_rpl_open(MARIADB_RPL *rpl);
298 void STDCALL mariadb_rpl_close(MARIADB_RPL *rpl);
299 MARIADB_RPL_EVENT * STDCALL mariadb_rpl_fetch(MARIADB_RPL *rpl, MARIADB_RPL_EVENT *event);
300 void STDCALL mariadb_free_rpl_event(MARIADB_RPL_EVENT *event);
301 
302 #ifdef	__cplusplus
303 }
304 #endif
305 #endif
306