MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sql_binlog.cc
1 /*
2  Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program 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
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16 
17 #include "sql_priv.h"
18 #include "sql_binlog.h"
19 #include "sql_parse.h"
20 #include "sql_acl.h"
21 #include "rpl_info.h"
22 #include "rpl_info_factory.h"
23 #include "base64.h"
24 #include "rpl_slave.h" // apply_event_and_update_pos
25 #include "log_event.h" // Format_description_log_event,
26  // EVENT_LEN_OFFSET,
27  // EVENT_TYPE_OFFSET,
28  // FORMAT_DESCRIPTION_LOG_EVENT,
29  // START_EVENT_V3,
30  // Log_event_type,
31  // Log_event
32 
33 
40 static int check_event_type(int type, Relay_log_info *rli)
41 {
43 
44  /*
45  Convert event type id of certain old versions (see comment in
46  Format_description_log_event::Format_description_log_event(char*,...)).
47  */
48  if (fd_event && fd_event->event_type_permutation)
49  {
50 #ifndef DBUG_OFF
51  Log_event_type new_type;
52  new_type= (Log_event_type) fd_event->event_type_permutation[type];
53  DBUG_PRINT("info", ("converting event type %d to %d (%s)",
54  type, new_type, Log_event::get_type_str(new_type)));
55 #endif
56  type= fd_event->event_type_permutation[type];
57  }
58 
59  switch (type)
60  {
61  case START_EVENT_V3:
62  case FORMAT_DESCRIPTION_EVENT:
63  /*
64  We need a preliminary FD event in order to parse the FD event,
65  if we don't already have one.
66  */
67  if (!fd_event)
69 
70  /* It is always allowed to execute FD events. */
71  return 0;
72 
73  case ROWS_QUERY_LOG_EVENT:
74  case TABLE_MAP_EVENT:
75  case WRITE_ROWS_EVENT:
76  case UPDATE_ROWS_EVENT:
77  case DELETE_ROWS_EVENT:
78  case WRITE_ROWS_EVENT_V1:
79  case UPDATE_ROWS_EVENT_V1:
80  case DELETE_ROWS_EVENT_V1:
81  case PRE_GA_WRITE_ROWS_EVENT:
82  case PRE_GA_UPDATE_ROWS_EVENT:
83  case PRE_GA_DELETE_ROWS_EVENT:
84  /*
85  Row events are only allowed if a Format_description_event has
86  already been seen.
87  */
88  if (fd_event)
89  return 0;
90  else
91  {
92  my_error(ER_NO_FORMAT_DESCRIPTION_EVENT_BEFORE_BINLOG_STATEMENT,
94  return 1;
95  }
96  break;
97 
98  default:
99  /*
100  It is not meaningful to execute other events than row-events and
101  FD events. It would even be dangerous to execute Stop_log_event
102  and Rotate_log_event since they call flush_relay_log_info, which
103  is not allowed to call by other threads than the slave SQL
104  thread when the slave SQL thread is running.
105  */
106  my_error(ER_ONLY_FD_AND_RBR_EVENTS_ALLOWED_IN_BINLOG_STATEMENT,
107  MYF(0), Log_event::get_type_str((Log_event_type)type));
108  return 1;
109  }
110 }
111 
126 void mysql_client_binlog_statement(THD* thd)
127 {
128  DBUG_ENTER("mysql_client_binlog_statement");
129  DBUG_PRINT("info",("binlog base64: '%*s'",
130  (int) (thd->lex->comment.length < 2048 ?
131  thd->lex->comment.length : 2048),
132  thd->lex->comment.str));
133 
134  if (check_global_access(thd, SUPER_ACL))
135  DBUG_VOID_RETURN;
136 
137  size_t coded_len= thd->lex->comment.length;
138  if (!coded_len)
139  {
140  my_error(ER_SYNTAX_ERROR, MYF(0));
141  DBUG_VOID_RETURN;
142  }
143  size_t decoded_len= base64_needed_decoded_length(coded_len);
144 
145  /*
146  option_bits will be changed when applying the event. But we don't expect
147  it be changed permanently after BINLOG statement, so backup it first.
148  It will be restored at the end of this function.
149  */
150  ulonglong thd_options= thd->variables.option_bits;
151 
152  /*
153  Allocation
154  */
155  int err= 0;
156  Relay_log_info *rli= thd->rli_fake;
157  if (!rli)
158  {
159  /*
160  We create a Relay_log_info object with a INFO_REPOSITORY_DUMMY because
161  to process a BINLOG command a real repository is not necessary. In the
162  future, we need to improve the code around the BINLOG command as only a
163  small part of the object is required to execute it. / Alfranio
164  */
165  if ((rli= Rpl_info_factory::create_rli(INFO_REPOSITORY_DUMMY, FALSE)))
166  {
167  thd->rli_fake= rli;
168  rli->info_thd= thd;
169  }
170  }
171 
172  const char *error= 0;
173  char *buf= (char *) my_malloc(decoded_len, MYF(MY_WME));
174  Log_event *ev = 0;
175 
176  /*
177  Out of memory check
178  */
179  if (!(rli && buf))
180  {
181  my_error(ER_OUTOFMEMORY, MYF(ME_FATALERROR), 1); /* needed 1 bytes */
182  goto end;
183  }
184 
185  DBUG_ASSERT(rli->belongs_to_client());
186 
187  for (char const *strptr= thd->lex->comment.str ;
188  strptr < thd->lex->comment.str + thd->lex->comment.length ; )
189  {
190  char const *endptr= 0;
191  int bytes_decoded= base64_decode(strptr, coded_len, buf, &endptr,
192  MY_BASE64_DECODE_ALLOW_MULTIPLE_CHUNKS);
193 
194 #ifndef HAVE_purify
195  /*
196  This debug printout should not be used for valgrind builds
197  since it will read from unassigned memory.
198  */
199  DBUG_PRINT("info",
200  ("bytes_decoded: %d strptr: 0x%lx endptr: 0x%lx ('%c':%d)",
201  bytes_decoded, (long) strptr, (long) endptr, *endptr,
202  *endptr));
203 #endif
204 
205  if (bytes_decoded < 0)
206  {
207  my_error(ER_BASE64_DECODE_ERROR, MYF(0));
208  goto end;
209  }
210  else if (bytes_decoded == 0)
211  break; // If no bytes where read, the string contained only whitespace
212 
213  DBUG_ASSERT(bytes_decoded > 0);
214  DBUG_ASSERT(endptr > strptr);
215  coded_len-= endptr - strptr;
216  strptr= endptr;
217 
218  /*
219  Now we have one or more events stored in the buffer. The size of
220  the buffer is computed based on how much base64-encoded data
221  there were, so there should be ample space for the data (maybe
222  even too much, since a statement can consist of a considerable
223  number of events).
224 
225  TODO: Switch to use a stream-based base64 encoder/decoder in
226  order to be able to read exactly what is necessary.
227  */
228 
229  DBUG_PRINT("info",("binlog base64 decoded_len: %lu bytes_decoded: %d",
230  (ulong) decoded_len, bytes_decoded));
231 
232  /*
233  Now we start to read events of the buffer, until there are no
234  more.
235  */
236  for (char *bufptr= buf ; bytes_decoded > 0 ; )
237  {
238  /*
239  Checking that the first event in the buffer is not truncated.
240  */
241  ulong event_len;
242  if (bytes_decoded < EVENT_LEN_OFFSET + 4 ||
243  (event_len= uint4korr(bufptr + EVENT_LEN_OFFSET)) >
244  (uint) bytes_decoded)
245  {
246  my_error(ER_SYNTAX_ERROR, MYF(0));
247  goto end;
248  }
249  DBUG_PRINT("info", ("event_len=%lu, bytes_decoded=%d",
250  event_len, bytes_decoded));
251 
252  if (check_event_type(bufptr[EVENT_TYPE_OFFSET], rli))
253  goto end;
254 
255  ev= Log_event::read_log_event(bufptr, event_len, &error,
257  0);
258 
259  DBUG_PRINT("info",("binlog base64 err=%s", error));
260  if (!ev)
261  {
262  /*
263  This could actually be an out-of-memory, but it is more likely
264  caused by a bad statement
265  */
266  my_error(ER_SYNTAX_ERROR, MYF(0));
267  goto end;
268  }
269 
270  bytes_decoded -= event_len;
271  bufptr += event_len;
272 
273  DBUG_PRINT("info",("ev->get_type_code()=%d", ev->get_type_code()));
274  ev->thd= thd;
275  /*
276  We go directly to the application phase, since we don't need
277  to check if the event shall be skipped or not.
278 
279  Neither do we have to update the log positions, since that is
280  not used at all: the rli_fake instance is used only for error
281  reporting.
282  */
283 #if !defined(MYSQL_CLIENT) && defined(HAVE_REPLICATION)
284  err= ev->apply_event(rli);
285 #endif
286  /*
287  Format_description_log_event should not be deleted because it
288  will be used to read info about the relay log's format; it
289  will be deleted when the SQL thread does not need it,
290  i.e. when this thread terminates.
291  ROWS_QUERY_LOG_EVENT if present in rli is deleted at the end
292  of the event.
293  */
294  if (ev->get_type_code() != FORMAT_DESCRIPTION_EVENT &&
295  ev->get_type_code() != ROWS_QUERY_LOG_EVENT)
296  {
297  delete ev;
298  ev= NULL;
299  }
300  if (err)
301  {
302  /*
303  TODO: Maybe a better error message since the BINLOG statement
304  now contains several events.
305  */
306  my_error(ER_UNKNOWN_ERROR, MYF(0));
307  goto end;
308  }
309  }
310  }
311 
312  DBUG_PRINT("info",("binlog base64 execution finished successfully"));
313  my_ok(thd);
314 
315 end:
316  if (rli)
317  {
318  if ((error || err) && rli->rows_query_ev)
319  {
320  delete rli->rows_query_ev;
321  rli->rows_query_ev= NULL;
322  }
323  rli->slave_close_thread_tables(thd);
324  }
325  thd->variables.option_bits= thd_options;
326  my_free(buf);
327  DBUG_VOID_RETURN;
328 }