~drizzle-trunk/drizzle/development

206.3.1 by Patrick Galbraith
Most everything working with client rename
1
/* Copyright (C) 2000-2003 DRIZZLE AB
1 by brian
clean slate
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
17
/**
18
  @addtogroup Replication
19
  @{
20
21
  @file
22
23
  @brief Code to run the io thread and the sql thread on the
24
  replication slave.
25
*/
243.1.17 by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.)
26
#include <drizzled/server_includes.h>
1 by brian
clean slate
27
212.4.2 by Monty Taylor
Fixed the includes in places to make the myisam header file move work.
28
#include <storage/myisam/myisam.h>
1 by brian
clean slate
29
#include "rpl_mi.h"
30
#include "rpl_rli.h"
31
#include "sql_repl.h"
32
#include "rpl_filter.h"
212.5.7 by Monty Taylor
Move thr_*h to mysys.
33
#include <mysys/thr_alarm.h>
212.5.31 by Monty Taylor
Moved sql_common.h and my_time.h to libdrizzle.
34
#include <libdrizzle/sql_common.h>
212.5.41 by Monty Taylor
Moved errmsg.h.
35
#include <libdrizzle/errmsg.h>
212.5.13 by Monty Taylor
Moved my_sys/my_pthread/my_nosys and mysys_err to mysys.
36
#include <mysys/mysys_err.h>
202.3.6 by Monty Taylor
First pass at gettexizing the error messages.
37
#include <drizzled/drizzled_error_messages.h>
1 by brian
clean slate
38
39
#include "rpl_tblmap.h"
40
41
#define FLAGSTR(V,F) ((V)&(F)?#F" ":"")
42
43
#define MAX_SLAVE_RETRY_PAUSE 5
44
bool use_slave_mask = 0;
45
MY_BITMAP slave_error_mask;
46
47
typedef bool (*CHECK_KILLED_FUNC)(THD*,void*);
48
49
char* slave_load_tmpdir = 0;
50
Master_info *active_mi= 0;
197 by Brian Aker
More my_bool cleanup.
51
bool replicate_same_server_id;
130 by Brian Aker
ulong cleanup
52
uint64_t relay_log_space_limit = 0;
1 by brian
clean slate
53
54
/*
55
  When slave thread exits, we need to remember the temporary tables so we
56
  can re-use them on slave start.
57
58
  TODO: move the vars below under Master_info
59
*/
60
130 by Brian Aker
ulong cleanup
61
int32_t disconnect_slave_event_count = 0, abort_slave_event_count = 0;
62
int32_t events_till_abort = -1;
1 by brian
clean slate
63
64
enum enum_slave_reconnect_actions
65
{
66
  SLAVE_RECON_ACT_REG= 0,
67
  SLAVE_RECON_ACT_DUMP= 1,
68
  SLAVE_RECON_ACT_EVENT= 2,
69
  SLAVE_RECON_ACT_MAX
70
};
71
72
enum enum_slave_reconnect_messages
73
{
74
  SLAVE_RECON_MSG_WAIT= 0,
75
  SLAVE_RECON_MSG_KILLED_WAITING= 1,
76
  SLAVE_RECON_MSG_AFTER= 2,
77
  SLAVE_RECON_MSG_FAILED= 3,
78
  SLAVE_RECON_MSG_COMMAND= 4,
79
  SLAVE_RECON_MSG_KILLED_AFTER= 5,
80
  SLAVE_RECON_MSG_MAX
81
};
82
83
static const char *reconnect_messages[SLAVE_RECON_ACT_MAX][SLAVE_RECON_MSG_MAX]=
84
{
85
  {
261.3.5 by Monty Taylor
Changed gettext_noop() to N_()
86
    N_("Waiting to reconnect after a failed registration on master"),
87
    N_("Slave I/O thread killed while waitnig to reconnect after a "
236.1.44 by Monty Taylor
Added en_US translation file.
88
                 "failed registration on master"),
261.3.5 by Monty Taylor
Changed gettext_noop() to N_()
89
    N_("Reconnecting after a failed registration on master"),
90
    N_("failed registering on master, reconnecting to try again, "
236.1.44 by Monty Taylor
Added en_US translation file.
91
                 "log '%s' at postion %s"),
1 by brian
clean slate
92
    "COM_REGISTER_SLAVE",
261.3.5 by Monty Taylor
Changed gettext_noop() to N_()
93
    N_("Slave I/O thread killed during or after reconnect")
1 by brian
clean slate
94
  },
95
  {
261.3.5 by Monty Taylor
Changed gettext_noop() to N_()
96
    N_("Waiting to reconnect after a failed binlog dump request"),
97
    N_("Slave I/O thread killed while retrying master dump"),
98
    N_("Reconnecting after a failed binlog dump request"),
99
    N_("failed dump request, reconnecting to try again, "
236.1.44 by Monty Taylor
Added en_US translation file.
100
                 "log '%s' at postion %s"),
1 by brian
clean slate
101
    "COM_BINLOG_DUMP",
261.3.5 by Monty Taylor
Changed gettext_noop() to N_()
102
    N_("Slave I/O thread killed during or after reconnect")
1 by brian
clean slate
103
  },
104
  {
261.3.5 by Monty Taylor
Changed gettext_noop() to N_()
105
    N_("Waiting to reconnect after a failed master event read"),
106
    N_("Slave I/O thread killed while waiting to reconnect "
236.1.44 by Monty Taylor
Added en_US translation file.
107
                 "after a failed read"),
261.3.5 by Monty Taylor
Changed gettext_noop() to N_()
108
    N_("Reconnecting after a failed master event read"),
109
    N_("Slave I/O thread: Failed reading log event, "
236.1.44 by Monty Taylor
Added en_US translation file.
110
                 "reconnecting to retry, log '%s' at postion %s"),
1 by brian
clean slate
111
    "",
261.3.5 by Monty Taylor
Changed gettext_noop() to N_()
112
    N_("Slave I/O thread killed during or after a "
236.1.44 by Monty Taylor
Added en_US translation file.
113
                 "reconnect done to recover from failed read")
1 by brian
clean slate
114
  }
115
};
116
 
117
118
typedef enum { SLAVE_THD_IO, SLAVE_THD_SQL} SLAVE_THD_TYPE;
119
130 by Brian Aker
ulong cleanup
120
static int32_t process_io_rotate(Master_info* mi, Rotate_log_event* rev);
121
static int32_t process_io_create_file(Master_info* mi, Create_file_log_event* cev);
1 by brian
clean slate
122
static bool wait_for_relay_log_space(Relay_log_info* rli);
123
static inline bool io_slave_killed(THD* thd,Master_info* mi);
124
static inline bool sql_slave_killed(THD* thd,Relay_log_info* rli);
130 by Brian Aker
ulong cleanup
125
static int32_t init_slave_thread(THD* thd, SLAVE_THD_TYPE thd_type);
206.3.1 by Patrick Galbraith
Most everything working with client rename
126
static int32_t safe_connect(THD* thd, DRIZZLE *drizzle, Master_info* mi);
127
static int32_t safe_reconnect(THD* thd, DRIZZLE *drizzle, Master_info* mi,
1 by brian
clean slate
128
                          bool suppress_warnings);
206.3.1 by Patrick Galbraith
Most everything working with client rename
129
static int32_t connect_to_master(THD* thd, DRIZZLE *drizzle, Master_info* mi,
1 by brian
clean slate
130
                             bool reconnect, bool suppress_warnings);
130 by Brian Aker
ulong cleanup
131
static int32_t safe_sleep(THD* thd, int32_t sec, CHECK_KILLED_FUNC thread_killed,
1 by brian
clean slate
132
                      void* thread_killed_arg);
206.3.1 by Patrick Galbraith
Most everything working with client rename
133
static int32_t get_master_version_and_clock(DRIZZLE *drizzle, Master_info* mi);
1 by brian
clean slate
134
static Log_event* next_event(Relay_log_info* rli);
130 by Brian Aker
ulong cleanup
135
static int32_t queue_event(Master_info* mi,const char* buf,uint32_t event_len);
136
static int32_t terminate_slave_thread(THD *thd,
1 by brian
clean slate
137
                                  pthread_mutex_t* term_lock,
138
                                  pthread_cond_t* term_cond,
130 by Brian Aker
ulong cleanup
139
                                  volatile uint32_t *slave_running,
1 by brian
clean slate
140
                                  bool skip_lock);
141
static bool check_io_slave_killed(THD *thd, Master_info *mi, const char *info);
142
143
/*
144
  Find out which replications threads are running
145
146
  SYNOPSIS
147
    init_thread_mask()
148
    mask                Return value here
149
    mi                  master_info for slave
150
    inverse             If set, returns which threads are not running
151
152
  IMPLEMENTATION
153
    Get a bit mask for which threads are running so that we can later restart
154
    these threads.
155
156
  RETURN
157
    mask        If inverse == 0, running threads
158
                If inverse == 1, stopped threads
159
*/
160
130 by Brian Aker
ulong cleanup
161
void init_thread_mask(int32_t* mask,Master_info* mi,bool inverse)
1 by brian
clean slate
162
{
163
  bool set_io = mi->slave_running, set_sql = mi->rli.slave_running;
130 by Brian Aker
ulong cleanup
164
  register int32_t tmp_mask=0;
1 by brian
clean slate
165
166
  if (set_io)
167
    tmp_mask |= SLAVE_IO;
168
  if (set_sql)
169
    tmp_mask |= SLAVE_SQL;
170
  if (inverse)
171
    tmp_mask^= (SLAVE_IO | SLAVE_SQL);
172
  *mask = tmp_mask;
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
173
  return;
1 by brian
clean slate
174
}
175
176
177
/*
178
  lock_slave_threads()
179
*/
180
181
void lock_slave_threads(Master_info* mi)
182
{
183
  //TODO: see if we can do this without dual mutex
184
  pthread_mutex_lock(&mi->run_lock);
185
  pthread_mutex_lock(&mi->rli.run_lock);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
186
  return;
1 by brian
clean slate
187
}
188
189
190
/*
191
  unlock_slave_threads()
192
*/
193
194
void unlock_slave_threads(Master_info* mi)
195
{
196
  //TODO: see if we can do this without dual mutex
197
  pthread_mutex_unlock(&mi->rli.run_lock);
198
  pthread_mutex_unlock(&mi->run_lock);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
199
  return;
1 by brian
clean slate
200
}
201
202
203
/* Initialize slave structures */
204
130 by Brian Aker
ulong cleanup
205
int32_t init_slave()
1 by brian
clean slate
206
{
207
  /*
208
    This is called when mysqld starts. Before client connections are
209
    accepted. However bootstrap may conflict with us if it does START SLAVE.
210
    So it's safer to take the lock.
211
  */
212
  pthread_mutex_lock(&LOCK_active_mi);
213
  /*
214
    TODO: re-write this to interate through the list of files
215
    for multi-master
216
  */
217
  active_mi= new Master_info;
218
219
  /*
220
    If master_host is not specified, try to read it from the master_info file.
221
    If master_host is specified, create the master_info file if it doesn't
222
    exists.
223
  */
224
  if (!active_mi)
225
  {
236.1.44 by Monty Taylor
Added en_US translation file.
226
    sql_print_error(_("Failed to allocate memory for the master info structure"));
1 by brian
clean slate
227
    goto err;
228
  }
229
230
  if (init_master_info(active_mi,master_info_file,relay_log_info_file,
231
                       1, (SLAVE_IO | SLAVE_SQL)))
232
  {
236.1.44 by Monty Taylor
Added en_US translation file.
233
    sql_print_error(_("Failed to initialize the master info structure"));
1 by brian
clean slate
234
    goto err;
235
  }
236
237
  /* If server id is not set, start_slave_thread() will say it */
238
239
  if (active_mi->host[0] && !opt_skip_slave_start)
240
  {
241
    if (start_slave_threads(1 /* need mutex */,
242
                            0 /* no wait for start*/,
243
                            active_mi,
244
                            master_info_file,
245
                            relay_log_info_file,
246
                            SLAVE_IO | SLAVE_SQL))
247
    {
236.1.44 by Monty Taylor
Added en_US translation file.
248
      sql_print_error(_("Failed to create slave threads"));
1 by brian
clean slate
249
      goto err;
250
    }
251
  }
252
  pthread_mutex_unlock(&LOCK_active_mi);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
253
  return(0);
1 by brian
clean slate
254
255
err:
256
  pthread_mutex_unlock(&LOCK_active_mi);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
257
  return(1);
1 by brian
clean slate
258
}
259
260
261
/*
262
  Init function to set up array for errors that should be skipped for slave
263
264
  SYNOPSIS
265
    init_slave_skip_errors()
266
    arg         List of errors numbers to skip, separated with ','
267
268
  NOTES
269
    Called from get_options() in mysqld.cc on start-up
270
*/
271
272
void init_slave_skip_errors(const char* arg)
273
{
274
  const char *p;
275
276
  if (bitmap_init(&slave_error_mask,0,MAX_SLAVE_ERROR,0))
277
  {
278
    fprintf(stderr, "Badly out of memory, please check your system status\n");
279
    exit(1);
280
  }
281
  use_slave_mask = 1;
282
  for (;my_isspace(system_charset_info,*arg);++arg)
283
    /* empty */;
481 by Brian Aker
Remove all of uchar.
284
  if (!my_strnncoll(system_charset_info,(unsigned char*)arg,4,(const unsigned char*)"all",4))
1 by brian
clean slate
285
  {
286
    bitmap_set_all(&slave_error_mask);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
287
    return;
1 by brian
clean slate
288
  }
289
  for (p= arg ; *p; )
290
  {
291
    long err_code;
292
    if (!(p= str2int(p, 10, 0, LONG_MAX, &err_code)))
293
      break;
294
    if (err_code < MAX_SLAVE_ERROR)
130 by Brian Aker
ulong cleanup
295
       bitmap_set_bit(&slave_error_mask,(uint32_t)err_code);
1 by brian
clean slate
296
    while (!my_isdigit(system_charset_info,*p) && *p)
297
      p++;
298
  }
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
299
  return;
1 by brian
clean slate
300
}
301
302
130 by Brian Aker
ulong cleanup
303
int32_t terminate_slave_threads(Master_info* mi,int32_t thread_mask,bool skip_lock)
1 by brian
clean slate
304
{
305
  if (!mi->inited)
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
306
    return(0); /* successfully do nothing */
130 by Brian Aker
ulong cleanup
307
  int32_t error,force_all = (thread_mask & SLAVE_FORCE_ALL);
1 by brian
clean slate
308
  pthread_mutex_t *sql_lock = &mi->rli.run_lock, *io_lock = &mi->run_lock;
309
310
  if ((thread_mask & (SLAVE_IO|SLAVE_FORCE_ALL)))
311
  {
312
    mi->abort_slave=1;
313
    if ((error=terminate_slave_thread(mi->io_thd,io_lock,
314
                                      &mi->stop_cond,
315
                                      &mi->slave_running,
316
                                      skip_lock)) &&
317
        !force_all)
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
318
      return(error);
1 by brian
clean slate
319
  }
320
  if ((thread_mask & (SLAVE_SQL|SLAVE_FORCE_ALL)))
321
  {
322
    mi->rli.abort_slave=1;
323
    if ((error=terminate_slave_thread(mi->rli.sql_thd,sql_lock,
324
                                      &mi->rli.stop_cond,
325
                                      &mi->rli.slave_running,
326
                                      skip_lock)) &&
327
        !force_all)
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
328
      return(error);
1 by brian
clean slate
329
  }
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
330
  return(0);
1 by brian
clean slate
331
}
332
333
334
/**
335
   Wait for a slave thread to terminate.
336
337
   This function is called after requesting the thread to terminate
338
   (by setting @c abort_slave member of @c Relay_log_info or @c
339
   Master_info structure to 1). Termination of the thread is
340
   controlled with the the predicate <code>*slave_running</code>.
341
342
   Function will acquire @c term_lock before waiting on the condition
343
   unless @c skip_lock is true in which case the mutex should be owned
344
   by the caller of this function and will remain acquired after
345
   return from the function.
346
347
   @param term_lock
348
          Associated lock to use when waiting for @c term_cond
349
350
   @param term_cond
351
          Condition that is signalled when the thread has terminated
352
353
   @param slave_running
354
          Pointer to predicate to check for slave thread termination
355
356
   @param skip_lock
357
          If @c true the lock will not be acquired before waiting on
358
          the condition. In this case, it is assumed that the calling
359
          function acquires the lock before calling this function.
360
361
   @retval 0 All OK
362
 */
130 by Brian Aker
ulong cleanup
363
static int32_t
1 by brian
clean slate
364
terminate_slave_thread(THD *thd,
365
                       pthread_mutex_t* term_lock,
366
                       pthread_cond_t* term_cond,
130 by Brian Aker
ulong cleanup
367
                       volatile uint32_t *slave_running,
1 by brian
clean slate
368
                       bool skip_lock)
369
{
130 by Brian Aker
ulong cleanup
370
  int32_t error;
1 by brian
clean slate
371
372
  if (!skip_lock)
373
    pthread_mutex_lock(term_lock);
374
375
  safe_mutex_assert_owner(term_lock);
376
377
  if (!*slave_running)
378
  {
379
    if (!skip_lock)
380
      pthread_mutex_unlock(term_lock);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
381
    return(ER_SLAVE_NOT_RUNNING);
1 by brian
clean slate
382
  }
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
383
  assert(thd != 0);
1 by brian
clean slate
384
  THD_CHECK_SENTRY(thd);
385
386
  /*
387
    Is is critical to test if the slave is running. Otherwise, we might
388
    be referening freed memory trying to kick it
389
  */
390
391
  while (*slave_running)                        // Should always be true
392
  {
393
    pthread_mutex_lock(&thd->LOCK_delete);
394
#ifndef DONT_USE_THR_ALARM
395
    /*
396
      Error codes from pthread_kill are:
397
      EINVAL: invalid signal number (can't happen)
398
      ESRCH: thread already killed (can happen, should be ignored)
399
    */
130 by Brian Aker
ulong cleanup
400
    int32_t err= pthread_kill(thd->real_id, thr_client_alarm);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
401
    assert(err != EINVAL);
1 by brian
clean slate
402
#endif
403
    thd->awake(THD::NOT_KILLED);
404
    pthread_mutex_unlock(&thd->LOCK_delete);
405
406
    /*
407
      There is a small chance that slave thread might miss the first
408
      alarm. To protect againts it, resend the signal until it reacts
409
    */
410
    struct timespec abstime;
411
    set_timespec(abstime,2);
412
    error= pthread_cond_timedwait(term_cond, term_lock, &abstime);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
413
    assert(error == ETIMEDOUT || error == 0);
1 by brian
clean slate
414
  }
415
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
416
  assert(*slave_running == 0);
1 by brian
clean slate
417
418
  if (!skip_lock)
419
    pthread_mutex_unlock(term_lock);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
420
  return(0);
1 by brian
clean slate
421
}
422
423
130 by Brian Aker
ulong cleanup
424
int32_t start_slave_thread(pthread_handler h_func, pthread_mutex_t *start_lock,
425
                           pthread_mutex_t *cond_lock,
426
                           pthread_cond_t *start_cond,
427
                           volatile uint32_t *slave_running,
428
                           volatile uint32_t *slave_run_id,
429
                           Master_info* mi,
430
                           bool high_priority)
1 by brian
clean slate
431
{
432
  pthread_t th;
130 by Brian Aker
ulong cleanup
433
  uint32_t start_id;
1 by brian
clean slate
434
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
435
  assert(mi->inited);
1 by brian
clean slate
436
437
  if (start_lock)
438
    pthread_mutex_lock(start_lock);
439
  if (!server_id)
440
  {
441
    if (start_cond)
442
      pthread_cond_broadcast(start_cond);
443
    if (start_lock)
444
      pthread_mutex_unlock(start_lock);
236.1.44 by Monty Taylor
Added en_US translation file.
445
    sql_print_error(_("Server id not set, will not start slave"));
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
446
    return(ER_BAD_SLAVE);
1 by brian
clean slate
447
  }
448
449
  if (*slave_running)
450
  {
451
    if (start_cond)
452
      pthread_cond_broadcast(start_cond);
453
    if (start_lock)
454
      pthread_mutex_unlock(start_lock);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
455
    return(ER_SLAVE_MUST_STOP);
1 by brian
clean slate
456
  }
457
  start_id= *slave_run_id;
458
  if (high_priority)
6 by Brian Aker
Second pass on pthread cleanup
459
  {
460
    struct sched_param tmp_sched_param;
461
462
    memset(&tmp_sched_param, 0, sizeof(tmp_sched_param));
463
    tmp_sched_param.sched_priority= CONNECT_PRIOR;
464
    (void)pthread_attr_setschedparam(&connection_attrib, &tmp_sched_param);
465
  }
1 by brian
clean slate
466
  if (pthread_create(&th, &connection_attrib, h_func, (void*)mi))
467
  {
468
    if (start_lock)
469
      pthread_mutex_unlock(start_lock);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
470
    return(ER_SLAVE_THREAD);
1 by brian
clean slate
471
  }
472
  if (start_cond && cond_lock) // caller has cond_lock
473
  {
474
    THD* thd = current_thd;
475
    while (start_id == *slave_run_id)
476
    {
477
      const char* old_msg = thd->enter_cond(start_cond,cond_lock,
478
                                            "Waiting for slave thread to start");
479
      pthread_cond_wait(start_cond,cond_lock);
480
      thd->exit_cond(old_msg);
481
      pthread_mutex_lock(cond_lock); // re-acquire it as exit_cond() released
482
      if (thd->killed)
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
483
        return(thd->killed_errno());
1 by brian
clean slate
484
    }
485
  }
486
  if (start_lock)
487
    pthread_mutex_unlock(start_lock);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
488
  return(0);
1 by brian
clean slate
489
}
490
491
492
/*
493
  start_slave_threads()
494
495
  NOTES
496
    SLAVE_FORCE_ALL is not implemented here on purpose since it does not make
497
    sense to do that for starting a slave--we always care if it actually
498
    started the threads that were not previously running
499
*/
500
130 by Brian Aker
ulong cleanup
501
int32_t start_slave_threads(bool need_slave_mutex, bool wait_for_start,
77.1.46 by Monty Taylor
Finished the warnings work!
502
                        Master_info* mi,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
503
                        const char* master_info_fname __attribute__((unused)),
504
                        const char* slave_info_fname __attribute__((unused)),
130 by Brian Aker
ulong cleanup
505
                        int32_t thread_mask)
1 by brian
clean slate
506
{
507
  pthread_mutex_t *lock_io=0,*lock_sql=0,*lock_cond_io=0,*lock_cond_sql=0;
508
  pthread_cond_t* cond_io=0,*cond_sql=0;
130 by Brian Aker
ulong cleanup
509
  int32_t error=0;
1 by brian
clean slate
510
511
  if (need_slave_mutex)
512
  {
513
    lock_io = &mi->run_lock;
514
    lock_sql = &mi->rli.run_lock;
515
  }
516
  if (wait_for_start)
517
  {
518
    cond_io = &mi->start_cond;
519
    cond_sql = &mi->rli.start_cond;
520
    lock_cond_io = &mi->run_lock;
521
    lock_cond_sql = &mi->rli.run_lock;
522
  }
523
524
  if (thread_mask & SLAVE_IO)
130 by Brian Aker
ulong cleanup
525
    error= start_slave_thread(handle_slave_io,lock_io,lock_cond_io,
526
                              cond_io,
527
                              &mi->slave_running, &mi->slave_run_id,
528
                              mi, 1); //high priority, to read the most possible
1 by brian
clean slate
529
  if (!error && (thread_mask & SLAVE_SQL))
530
  {
130 by Brian Aker
ulong cleanup
531
    error= start_slave_thread(handle_slave_sql,lock_sql,lock_cond_sql,
532
                              cond_sql,
533
                              &mi->rli.slave_running, &mi->rli.slave_run_id,
534
                              mi, 0);
1 by brian
clean slate
535
    if (error)
536
      terminate_slave_threads(mi, thread_mask & SLAVE_IO, 0);
537
  }
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
538
  return(error);
1 by brian
clean slate
539
}
540
541
542
#ifdef NOT_USED_YET
481 by Brian Aker
Remove all of uchar.
543
static int32_t end_slave_on_walk(Master_info* mi, unsigned char* /*unused*/)
1 by brian
clean slate
544
{
545
  end_master_info(mi);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
546
  return(0);
1 by brian
clean slate
547
}
548
#endif
549
550
551
/*
552
  Free all resources used by slave
553
554
  SYNOPSIS
555
    end_slave()
556
*/
557
558
void end_slave()
559
{
560
  /*
561
    This is called when the server terminates, in close_connections().
562
    It terminates slave threads. However, some CHANGE MASTER etc may still be
563
    running presently. If a START SLAVE was in progress, the mutex lock below
564
    will make us wait until slave threads have started, and START SLAVE
565
    returns, then we terminate them here.
566
  */
567
  pthread_mutex_lock(&LOCK_active_mi);
568
  if (active_mi)
569
  {
570
    /*
571
      TODO: replace the line below with
572
      list_walk(&master_list, (list_walk_action)end_slave_on_walk,0);
573
      once multi-master code is ready.
574
    */
575
    terminate_slave_threads(active_mi,SLAVE_FORCE_ALL);
576
    end_master_info(active_mi);
577
    delete active_mi;
578
    active_mi= 0;
579
  }
580
  pthread_mutex_unlock(&LOCK_active_mi);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
581
  return;
1 by brian
clean slate
582
}
583
584
585
static bool io_slave_killed(THD* thd, Master_info* mi)
586
{
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
587
  assert(mi->io_thd == thd);
588
  assert(mi->slave_running); // tracking buffer overrun
589
  return(mi->abort_slave || abort_loop || thd->killed);
1 by brian
clean slate
590
}
591
592
593
static bool sql_slave_killed(THD* thd, Relay_log_info* rli)
594
{
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
595
  assert(rli->sql_thd == thd);
596
  assert(rli->slave_running == 1);// tracking buffer overrun
1 by brian
clean slate
597
  if (abort_loop || thd->killed || rli->abort_slave)
598
  {
599
    /*
600
      If we are in an unsafe situation (stopping could corrupt replication),
601
      we give one minute to the slave SQL thread of grace before really
602
      terminating, in the hope that it will be able to read more events and
603
      the unsafe situation will soon be left. Note that this one minute starts
604
      from the last time anything happened in the slave SQL thread. So it's
605
      really one minute of idleness, we don't timeout if the slave SQL thread
606
      is actively working.
607
    */
608
    if (rli->last_event_start_time == 0)
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
609
      return(1);
1 by brian
clean slate
610
    if (difftime(time(0), rli->last_event_start_time) > 60)
611
    {
612
      rli->report(ERROR_LEVEL, 0,
261.3.4 by Monty Taylor
Added three more files worth of stuff.
613
                  _("SQL thread had to stop in an unsafe situation, in "
1 by brian
clean slate
614
                  "the middle of applying updates to a "
615
                  "non-transactional table without any primary key. "
616
                  "There is a risk of duplicate updates when the slave "
617
                  "SQL thread is restarted. Please check your tables' "
261.3.4 by Monty Taylor
Added three more files worth of stuff.
618
                  "contents after restart."));
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
619
      return(1);
1 by brian
clean slate
620
    }
621
  }
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
622
  return(0);
1 by brian
clean slate
623
}
624
625
626
/*
627
  skip_load_data_infile()
628
629
  NOTES
630
    This is used to tell a 3.23 master to break send_file()
631
*/
632
633
void skip_load_data_infile(NET *net)
634
{
635
  (void)net_request_file(net, "/dev/null");
636
  (void)my_net_read(net);                               // discard response
481 by Brian Aker
Remove all of uchar.
637
  (void)net_write_command(net, 0, (unsigned char*) "", 0, (unsigned char*) "", 0); // ok
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
638
  return;
1 by brian
clean slate
639
}
640
641
642
bool net_request_file(NET* net, const char* fname)
643
{
481 by Brian Aker
Remove all of uchar.
644
  return(net_write_command(net, 251, (unsigned char*) fname, strlen(fname),
645
                                (unsigned char*) "", 0));
1 by brian
clean slate
646
}
647
648
/*
649
  From other comments and tests in code, it looks like
650
  sometimes Query_log_event and Load_log_event can have db == 0
651
  (see rewrite_db() above for example)
652
  (cases where this happens are unclear; it may be when the master is 3.23).
653
*/
654
655
const char *print_slave_db_safe(const char* db)
656
{
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
657
  return((db ? db : ""));
1 by brian
clean slate
658
}
659
130 by Brian Aker
ulong cleanup
660
int32_t init_strvar_from_file(char *var, int32_t max_size, IO_CACHE *f,
1 by brian
clean slate
661
                                 const char *default_val)
662
{
130 by Brian Aker
ulong cleanup
663
  uint32_t length;
1 by brian
clean slate
664
665
  if ((length=my_b_gets(f,var, max_size)))
666
  {
667
    char* last_p = var + length -1;
668
    if (*last_p == '\n')
669
      *last_p = 0; // if we stopped on newline, kill it
670
    else
671
    {
672
      /*
673
        If we truncated a line or stopped on last char, remove all chars
674
        up to and including newline.
675
      */
130 by Brian Aker
ulong cleanup
676
      int32_t c;
677
      while (((c=my_b_get(f)) != '\n' && c != my_b_EOF)) {};
1 by brian
clean slate
678
    }
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
679
    return(0);
1 by brian
clean slate
680
  }
681
  else if (default_val)
682
  {
683
    strmake(var,  default_val, max_size-1);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
684
    return(0);
1 by brian
clean slate
685
  }
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
686
  return(1);
1 by brian
clean slate
687
}
688
689
130 by Brian Aker
ulong cleanup
690
int32_t init_intvar_from_file(int32_t* var, IO_CACHE* f, int32_t default_val)
1 by brian
clean slate
691
{
692
  char buf[32];
693
694
695
  if (my_b_gets(f, buf, sizeof(buf)))
696
  {
697
    *var = atoi(buf);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
698
    return(0);
1 by brian
clean slate
699
  }
700
  else if (default_val)
701
  {
702
    *var = default_val;
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
703
    return(0);
1 by brian
clean slate
704
  }
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
705
  return(1);
1 by brian
clean slate
706
}
707
130 by Brian Aker
ulong cleanup
708
int32_t init_floatvar_from_file(float* var, IO_CACHE* f, float default_val)
1 by brian
clean slate
709
{
710
  char buf[16];
711
712
713
  if (my_b_gets(f, buf, sizeof(buf)))
714
  {
715
    if (sscanf(buf, "%f", var) != 1)
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
716
      return(1);
1 by brian
clean slate
717
    else
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
718
      return(0);
1 by brian
clean slate
719
  }
720
  else if (default_val != 0.0)
721
  {
722
    *var = default_val;
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
723
    return(0);
1 by brian
clean slate
724
  }
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
725
  return(1);
1 by brian
clean slate
726
}
727
728
static bool check_io_slave_killed(THD *thd, Master_info *mi, const char *info)
729
{
730
  if (io_slave_killed(thd, mi))
731
  {
732
    if (info && global_system_variables.log_warnings)
733
      sql_print_information(info);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
734
    return true;
1 by brian
clean slate
735
  }
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
736
  return false;
1 by brian
clean slate
737
}
738
739
740
/*
741
  Note that we rely on the master's version (3.23, 4.0.14 etc) instead of
742
  relying on the binlog's version. This is not perfect: imagine an upgrade
743
  of the master without waiting that all slaves are in sync with the master;
744
  then a slave could be fooled about the binlog's format. This is what happens
745
  when people upgrade a 3.23 master to 4.0 without doing RESET MASTER: 4.0
746
  slaves are fooled. So we do this only to distinguish between 3.23 and more
747
  recent masters (it's too late to change things for 3.23).
748
749
  RETURNS
750
  0       ok
751
  1       error
752
*/
753
206.3.1 by Patrick Galbraith
Most everything working with client rename
754
static int32_t get_master_version_and_clock(DRIZZLE *drizzle, Master_info* mi)
1 by brian
clean slate
755
{
756
  char error_buf[512];
757
  String err_msg(error_buf, sizeof(error_buf), &my_charset_bin);
758
  char err_buff[MAX_SLAVE_ERRMSG];
759
  const char* errmsg= 0;
130 by Brian Aker
ulong cleanup
760
  int32_t err_code= 0;
206.3.1 by Patrick Galbraith
Most everything working with client rename
761
  DRIZZLE_RES *master_res= 0;
762
  DRIZZLE_ROW master_row;
1 by brian
clean slate
763
764
  err_msg.length(0);
765
  /*
766
    Free old description_event_for_queue (that is needed if we are in
767
    a reconnection).
768
  */
769
  delete mi->rli.relay_log.description_event_for_queue;
770
  mi->rli.relay_log.description_event_for_queue= 0;
771
206.3.1 by Patrick Galbraith
Most everything working with client rename
772
  if (!my_isdigit(&my_charset_bin,*drizzle->server_version))
1 by brian
clean slate
773
  {
236.1.44 by Monty Taylor
Added en_US translation file.
774
    errmsg = _("Master reported unrecognized DRIZZLE version");
1 by brian
clean slate
775
    err_code= ER_SLAVE_FATAL_ERROR;
776
    sprintf(err_buff, ER(err_code), errmsg);
777
    err_msg.append(err_buff);
778
  }
779
  else
780
  {
781
    /*
206.3.1 by Patrick Galbraith
Most everything working with client rename
782
      Note the following switch will bug when we have DRIZZLE branch 30 ;)
1 by brian
clean slate
783
    */
206.3.1 by Patrick Galbraith
Most everything working with client rename
784
    switch (*drizzle->server_version)
1 by brian
clean slate
785
    {
786
    case '0':
787
    case '1':
788
    case '2':
236.1.44 by Monty Taylor
Added en_US translation file.
789
      errmsg = _("Master reported unrecognized DRIZZLE version");
1 by brian
clean slate
790
      err_code= ER_SLAVE_FATAL_ERROR;
791
      sprintf(err_buff, ER(err_code), errmsg);
792
      err_msg.append(err_buff);
793
      break;
794
    case '3':
795
      mi->rli.relay_log.description_event_for_queue= new
206.3.1 by Patrick Galbraith
Most everything working with client rename
796
        Format_description_log_event(1, drizzle->server_version);
1 by brian
clean slate
797
      break;
798
    case '4':
799
      mi->rli.relay_log.description_event_for_queue= new
206.3.1 by Patrick Galbraith
Most everything working with client rename
800
        Format_description_log_event(3, drizzle->server_version);
1 by brian
clean slate
801
      break;
802
    default:
803
      /*
206.3.1 by Patrick Galbraith
Most everything working with client rename
804
        Master is DRIZZLE >=5.0. Give a default Format_desc event, so that we can
1 by brian
clean slate
805
        take the early steps (like tests for "is this a 3.23 master") which we
806
        have to take before we receive the real master's Format_desc which will
807
        override this one. Note that the Format_desc we create below is garbage
808
        (it has the format of the *slave*); it's only good to help know if the
809
        master is 3.23, 4.0, etc.
810
      */
811
      mi->rli.relay_log.description_event_for_queue= new
206.3.1 by Patrick Galbraith
Most everything working with client rename
812
        Format_description_log_event(4, drizzle->server_version);
1 by brian
clean slate
813
      break;
814
    }
815
  }
816
817
  /*
818
     This does not mean that a 5.0 slave will be able to read a 6.0 master; but
819
     as we don't know yet, we don't want to forbid this for now. If a 5.0 slave
820
     can't read a 6.0 master, this will show up when the slave can't read some
821
     events sent by the master, and there will be error messages.
822
  */
823
824
  if (err_msg.length() != 0)
825
    goto err;
826
827
  /* as we are here, we tried to allocate the event */
828
  if (!mi->rli.relay_log.description_event_for_queue)
829
  {
236.1.44 by Monty Taylor
Added en_US translation file.
830
    errmsg= _("default Format_description_log_event");
1 by brian
clean slate
831
    err_code= ER_SLAVE_CREATE_EVENT_FAILURE;
832
    sprintf(err_buff, ER(err_code), errmsg);
833
    err_msg.append(err_buff);
834
    goto err;
835
  }
836
837
  /*
838
    Compare the master and slave's clock. Do not die if master's clock is
839
    unavailable (very old master not supporting UNIX_TIMESTAMP()?).
840
  */
841
206.3.1 by Patrick Galbraith
Most everything working with client rename
842
  if (!drizzle_real_query(drizzle, STRING_WITH_LEN("SELECT UNIX_TIMESTAMP()")) &&
843
      (master_res= drizzle_store_result(drizzle)) &&
844
      (master_row= drizzle_fetch_row(master_res)))
1 by brian
clean slate
845
  {
846
    mi->clock_diff_with_master=
847
      (long) (time((time_t*) 0) - strtoul(master_row[0], 0, 10));
848
  }
849
  else if (!check_io_slave_killed(mi->io_thd, mi, NULL))
850
  {
851
    mi->clock_diff_with_master= 0; /* The "most sensible" value */
236.1.44 by Monty Taylor
Added en_US translation file.
852
    sql_print_warning(_("\"SELECT UNIX_TIMESTAMP()\" failed on master, "
853
                        "do not trust column Seconds_Behind_Master of SHOW "
854
                        "SLAVE STATUS. Error: %s (%d)"),
206.3.1 by Patrick Galbraith
Most everything working with client rename
855
                      drizzle_error(drizzle), drizzle_errno(drizzle));
1 by brian
clean slate
856
  }
857
  if (master_res)
206.3.1 by Patrick Galbraith
Most everything working with client rename
858
    drizzle_free_result(master_res);
1 by brian
clean slate
859
860
  /*
861
    Check that the master's server id and ours are different. Because if they
862
    are equal (which can result from a simple copy of master's datadir to slave,
863
    thus copying some my.cnf), replication will work but all events will be
864
    skipped.
865
    Do not die if SHOW VARIABLES LIKE 'SERVER_ID' fails on master (very old
866
    master?).
867
    Note: we could have put a @@SERVER_ID in the previous SELECT
868
    UNIX_TIMESTAMP() instead, but this would not have worked on 3.23 masters.
869
  */
206.3.1 by Patrick Galbraith
Most everything working with client rename
870
  if (!drizzle_real_query(drizzle,
1 by brian
clean slate
871
                        STRING_WITH_LEN("SHOW VARIABLES LIKE 'SERVER_ID'")) &&
206.3.1 by Patrick Galbraith
Most everything working with client rename
872
      (master_res= drizzle_store_result(drizzle)))
1 by brian
clean slate
873
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
874
    if ((master_row= drizzle_fetch_row(master_res)) &&
1 by brian
clean slate
875
        (::server_id == strtoul(master_row[1], 0, 10)) &&
876
        !mi->rli.replicate_same_server_id)
877
    {
878
      errmsg=
236.1.44 by Monty Taylor
Added en_US translation file.
879
        _("The slave I/O thread stops because master and slave have equal "
880
          "DRIZZLE server ids; these ids must be different "
881
          "for replication to work (or "
882
          "the --replicate-same-server-id option must be used "
883
          "on slave but this does"
884
          "not always make sense; please check the manual before using it).");
1 by brian
clean slate
885
      err_code= ER_SLAVE_FATAL_ERROR;
886
      sprintf(err_buff, ER(err_code), errmsg);
887
      err_msg.append(err_buff);
888
    }
206.3.1 by Patrick Galbraith
Most everything working with client rename
889
    drizzle_free_result(master_res);
1 by brian
clean slate
890
    if (errmsg)
891
      goto err;
892
  }
893
894
  /*
895
    Check that the master's global character_set_server and ours are the same.
896
    Not fatal if query fails (old master?).
897
    Note that we don't check for equality of global character_set_client and
898
    collation_connection (neither do we prevent their setting in
899
    set_var.cc). That's because from what I (Guilhem) have tested, the global
900
    values of these 2 are never used (new connections don't use them).
901
    We don't test equality of global collation_database either as it's is
902
    going to be deprecated (made read-only) in 4.1 very soon.
903
    The test is only relevant if master < 5.0.3 (we'll test only if it's older
904
    than the 5 branch; < 5.0.3 was alpha...), as >= 5.0.3 master stores
905
    charset info in each binlog event.
906
    We don't do it for 3.23 because masters <3.23.50 hang on
907
    SELECT @@unknown_var (BUG#7965 - see changelog of 3.23.50). So finally we
908
    test only if master is 4.x.
909
  */
910
911
  /* redundant with rest of code but safer against later additions */
206.3.1 by Patrick Galbraith
Most everything working with client rename
912
  if (*drizzle->server_version == '3')
1 by brian
clean slate
913
    goto err;
914
206.3.1 by Patrick Galbraith
Most everything working with client rename
915
  if ((*drizzle->server_version == '4') &&
916
      !drizzle_real_query(drizzle,
1 by brian
clean slate
917
                        STRING_WITH_LEN("SELECT @@GLOBAL.COLLATION_SERVER")) &&
206.3.1 by Patrick Galbraith
Most everything working with client rename
918
      (master_res= drizzle_store_result(drizzle)))
1 by brian
clean slate
919
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
920
    if ((master_row= drizzle_fetch_row(master_res)) &&
1 by brian
clean slate
921
        strcmp(master_row[0], global_system_variables.collation_server->name))
922
    {
923
      errmsg=
236.1.44 by Monty Taylor
Added en_US translation file.
924
        _("The slave I/O thread stops because master and slave have"
925
          " different values for the COLLATION_SERVER global variable."
926
          " The values must be equal for replication to work");
1 by brian
clean slate
927
      err_code= ER_SLAVE_FATAL_ERROR;
928
      sprintf(err_buff, ER(err_code), errmsg);
929
      err_msg.append(err_buff);
930
    }
206.3.1 by Patrick Galbraith
Most everything working with client rename
931
    drizzle_free_result(master_res);
1 by brian
clean slate
932
    if (errmsg)
933
      goto err;
934
  }
935
936
  /*
937
    Perform analogous check for time zone. Theoretically we also should
938
    perform check here to verify that SYSTEM time zones are the same on
939
    slave and master, but we can't rely on value of @@system_time_zone
940
    variable (it is time zone abbreviation) since it determined at start
941
    time and so could differ for slave and master even if they are really
942
    in the same system time zone. So we are omiting this check and just
943
    relying on documentation. Also according to Monty there are many users
944
    who are using replication between servers in various time zones. Hence
945
    such check will broke everything for them. (And now everything will
946
    work for them because by default both their master and slave will have
947
    'SYSTEM' time zone).
948
    This check is only necessary for 4.x masters (and < 5.0.4 masters but
949
    those were alpha).
950
  */
206.3.1 by Patrick Galbraith
Most everything working with client rename
951
  if ((*drizzle->server_version == '4') &&
952
      !drizzle_real_query(drizzle, STRING_WITH_LEN("SELECT @@GLOBAL.TIME_ZONE")) &&
953
      (master_res= drizzle_store_result(drizzle)))
1 by brian
clean slate
954
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
955
    if ((master_row= drizzle_fetch_row(master_res)) &&
1 by brian
clean slate
956
        strcmp(master_row[0],
957
               global_system_variables.time_zone->get_name()->ptr()))
958
    {
959
      errmsg=
236.1.44 by Monty Taylor
Added en_US translation file.
960
        _("The slave I/O thread stops because master and slave have"
961
          " different values for the TIME_ZONE global variable."
962
          " The values must be equal for replication to work");
1 by brian
clean slate
963
      err_code= ER_SLAVE_FATAL_ERROR;
964
      sprintf(err_buff, ER(err_code), errmsg);
965
      err_msg.append(err_buff);
966
    }
206.3.1 by Patrick Galbraith
Most everything working with client rename
967
    drizzle_free_result(master_res);
1 by brian
clean slate
968
969
    if (errmsg)
970
      goto err;
971
  }
972
973
  if (mi->heartbeat_period != 0.0)
974
  {
975
    char llbuf[22];
976
    const char query_format[]= "SET @master_heartbeat_period= %s";
977
    char query[sizeof(query_format) - 2 + sizeof(llbuf)];
978
    /* 
130 by Brian Aker
ulong cleanup
979
       the period is an uint64_t of nano-secs. 
1 by brian
clean slate
980
    */
130 by Brian Aker
ulong cleanup
981
    llstr((uint64_t) (mi->heartbeat_period*1000000000UL), llbuf);
171.1.1 by Patrick Galbraith
Dar, I forgot to commit this earlier.
982
    sprintf(query, query_format, llbuf);
1 by brian
clean slate
983
206.3.1 by Patrick Galbraith
Most everything working with client rename
984
    if (drizzle_real_query(drizzle, query, strlen(query))
1 by brian
clean slate
985
        && !check_io_slave_killed(mi->io_thd, mi, NULL))
986
    {
987
      err_msg.append("The slave I/O thread stops because querying master with '");
988
      err_msg.append(query);
989
      err_msg.append("' failed;");
990
      err_msg.append(" error: ");
206.3.1 by Patrick Galbraith
Most everything working with client rename
991
      err_code= drizzle_errno(drizzle);
1 by brian
clean slate
992
      err_msg.qs_append(err_code);
993
      err_msg.append("  '");
206.3.1 by Patrick Galbraith
Most everything working with client rename
994
      err_msg.append(drizzle_error(drizzle));
1 by brian
clean slate
995
      err_msg.append("'");
206.3.1 by Patrick Galbraith
Most everything working with client rename
996
      drizzle_free_result(drizzle_store_result(drizzle));
1 by brian
clean slate
997
      goto err;
998
    }
206.3.1 by Patrick Galbraith
Most everything working with client rename
999
    drizzle_free_result(drizzle_store_result(drizzle));
1 by brian
clean slate
1000
  }
1001
  
1002
err:
1003
  if (err_msg.length() != 0)
1004
  {
1005
    sql_print_error(err_msg.ptr());
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1006
    assert(err_code != 0);
1 by brian
clean slate
1007
    mi->report(ERROR_LEVEL, err_code, err_msg.ptr());
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1008
    return(1);
1 by brian
clean slate
1009
  }
1010
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1011
  return(0);
1 by brian
clean slate
1012
}
1013
1014
1015
static bool wait_for_relay_log_space(Relay_log_info* rli)
1016
{
1017
  bool slave_killed=0;
1018
  Master_info* mi = rli->mi;
1019
  const char *save_proc_info;
1020
  THD* thd = mi->io_thd;
1021
1022
  pthread_mutex_lock(&rli->log_space_lock);
1023
  save_proc_info= thd->enter_cond(&rli->log_space_cond,
1024
                                  &rli->log_space_lock,
236.1.44 by Monty Taylor
Added en_US translation file.
1025
                                  _("Waiting for the slave SQL thread "
1026
                                    "to free enough relay log space"));
1 by brian
clean slate
1027
  while (rli->log_space_limit < rli->log_space_total &&
1028
         !(slave_killed=io_slave_killed(thd,mi)) &&
1029
         !rli->ignore_log_space_limit)
1030
    pthread_cond_wait(&rli->log_space_cond, &rli->log_space_lock);
1031
  thd->exit_cond(save_proc_info);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1032
  return(slave_killed);
1 by brian
clean slate
1033
}
1034
1035
1036
/*
1037
  Builds a Rotate from the ignored events' info and writes it to relay log.
1038
1039
  SYNOPSIS
1040
  write_ignored_events_info_to_relay_log()
1041
    thd             pointer to I/O thread's thd
1042
    mi
1043
1044
  DESCRIPTION
1045
    Slave I/O thread, going to die, must leave a durable trace of the
1046
    ignored events' end position for the use of the slave SQL thread, by
1047
    calling this function. Only that thread can call it (see assertion).
1048
 */
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1049
static void write_ignored_events_info_to_relay_log(THD *thd __attribute__((unused)),
77.1.46 by Monty Taylor
Finished the warnings work!
1050
                                                   Master_info *mi)
1 by brian
clean slate
1051
{
1052
  Relay_log_info *rli= &mi->rli;
1053
  pthread_mutex_t *log_lock= rli->relay_log.get_log_lock();
1054
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1055
  assert(thd == mi->io_thd);
1 by brian
clean slate
1056
  pthread_mutex_lock(log_lock);
1057
  if (rli->ign_master_log_name_end[0])
1058
  {
1059
    Rotate_log_event *ev= new Rotate_log_event(rli->ign_master_log_name_end,
1060
                                               0, rli->ign_master_log_pos_end,
1061
                                               Rotate_log_event::DUP_NAME);
1062
    rli->ign_master_log_name_end[0]= 0;
1063
    /* can unlock before writing as slave SQL thd will soon see our Rotate */
1064
    pthread_mutex_unlock(log_lock);
1065
    if (likely((bool)ev))
1066
    {
1067
      ev->server_id= 0; // don't be ignored by slave SQL thread
1068
      if (unlikely(rli->relay_log.append(ev)))
1069
        mi->report(ERROR_LEVEL, ER_SLAVE_RELAY_LOG_WRITE_FAILURE,
1070
                   ER(ER_SLAVE_RELAY_LOG_WRITE_FAILURE),
236.1.44 by Monty Taylor
Added en_US translation file.
1071
                   _("failed to write a Rotate event"
1072
                     " to the relay log, SHOW SLAVE STATUS may be"
1073
                     " inaccurate"));
1 by brian
clean slate
1074
      rli->relay_log.harvest_bytes_written(&rli->log_space_total);
1075
      if (flush_master_info(mi, 1))
236.1.44 by Monty Taylor
Added en_US translation file.
1076
        sql_print_error(_("Failed to flush master info file"));
1 by brian
clean slate
1077
      delete ev;
1078
    }
1079
    else
1080
      mi->report(ERROR_LEVEL, ER_SLAVE_CREATE_EVENT_FAILURE,
1081
                 ER(ER_SLAVE_CREATE_EVENT_FAILURE),
236.1.44 by Monty Taylor
Added en_US translation file.
1082
                 _("Rotate_event (out of memory?),"
1083
                   " SHOW SLAVE STATUS may be inaccurate"));
1 by brian
clean slate
1084
  }
1085
  else
1086
    pthread_mutex_unlock(log_lock);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1087
  return;
1 by brian
clean slate
1088
}
1089
1090
206.3.1 by Patrick Galbraith
Most everything working with client rename
1091
int32_t register_slave_on_master(DRIZZLE *drizzle, Master_info *mi,
1 by brian
clean slate
1092
                             bool *suppress_warnings)
1093
{
481 by Brian Aker
Remove all of uchar.
1094
  unsigned char buf[1024], *pos= buf;
130 by Brian Aker
ulong cleanup
1095
  uint32_t report_host_len, report_user_len=0, report_password_len=0;
1 by brian
clean slate
1096
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1097
  *suppress_warnings= false;
1 by brian
clean slate
1098
  if (!report_host)
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1099
    return(0);
1 by brian
clean slate
1100
  report_host_len= strlen(report_host);
1101
  if (report_user)
1102
    report_user_len= strlen(report_user);
1103
  if (report_password)
1104
    report_password_len= strlen(report_password);
1105
  /* 30 is a good safety margin */
1106
  if (report_host_len + report_user_len + report_password_len + 30 >
1107
      sizeof(buf))
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1108
    return(0);                                     // safety
1 by brian
clean slate
1109
1110
  int4store(pos, server_id); pos+= 4;
481 by Brian Aker
Remove all of uchar.
1111
  pos= net_store_data(pos, (unsigned char*) report_host, report_host_len);
1112
  pos= net_store_data(pos, (unsigned char*) report_user, report_user_len);
1113
  pos= net_store_data(pos, (unsigned char*) report_password, report_password_len);
206 by Brian Aker
Removed final uint dead types.
1114
  int2store(pos, (uint16_t) report_port); pos+= 2;
1 by brian
clean slate
1115
  int4store(pos, rpl_recovery_rank);    pos+= 4;
1116
  /* The master will fill in master_id */
1117
  int4store(pos, 0);                    pos+= 4;
1118
206.3.1 by Patrick Galbraith
Most everything working with client rename
1119
  if (simple_command(drizzle, COM_REGISTER_SLAVE, buf, (size_t) (pos- buf), 0))
1 by brian
clean slate
1120
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
1121
    if (drizzle_errno(drizzle) == ER_NET_READ_INTERRUPTED)
1 by brian
clean slate
1122
    {
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1123
      *suppress_warnings= true;                 // Suppress reconnect warning
1 by brian
clean slate
1124
    }
1125
    else if (!check_io_slave_killed(mi->io_thd, mi, NULL))
1126
    {
1127
      char buf[256];
206.3.1 by Patrick Galbraith
Most everything working with client rename
1128
      snprintf(buf, sizeof(buf), "%s (Errno: %d)", drizzle_error(drizzle), 
1129
               drizzle_errno(drizzle));
1 by brian
clean slate
1130
      mi->report(ERROR_LEVEL, ER_SLAVE_MASTER_COM_FAILURE,
1131
                 ER(ER_SLAVE_MASTER_COM_FAILURE), "COM_REGISTER_SLAVE", buf);
1132
    }
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1133
    return(1);
1 by brian
clean slate
1134
  }
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1135
  return(0);
1 by brian
clean slate
1136
}
1137
1138
1139
bool show_master_info(THD* thd, Master_info* mi)
1140
{
1141
  // TODO: fix this for multi-master
1142
  List<Item> field_list;
1143
  Protocol *protocol= thd->protocol;
1144
1145
  field_list.push_back(new Item_empty_string("Slave_IO_State",
1146
                                                     14));
1147
  field_list.push_back(new Item_empty_string("Master_Host",
1148
                                                     sizeof(mi->host)));
1149
  field_list.push_back(new Item_empty_string("Master_User",
1150
                                                     sizeof(mi->user)));
1151
  field_list.push_back(new Item_return_int("Master_Port", 7,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1152
                                           DRIZZLE_TYPE_LONG));
1 by brian
clean slate
1153
  field_list.push_back(new Item_return_int("Connect_Retry", 10,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1154
                                           DRIZZLE_TYPE_LONG));
1 by brian
clean slate
1155
  field_list.push_back(new Item_empty_string("Master_Log_File",
1156
                                             FN_REFLEN));
1157
  field_list.push_back(new Item_return_int("Read_Master_Log_Pos", 10,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1158
                                           DRIZZLE_TYPE_LONGLONG));
1 by brian
clean slate
1159
  field_list.push_back(new Item_empty_string("Relay_Log_File",
1160
                                             FN_REFLEN));
1161
  field_list.push_back(new Item_return_int("Relay_Log_Pos", 10,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1162
                                           DRIZZLE_TYPE_LONGLONG));
1 by brian
clean slate
1163
  field_list.push_back(new Item_empty_string("Relay_Master_Log_File",
1164
                                             FN_REFLEN));
1165
  field_list.push_back(new Item_empty_string("Slave_IO_Running", 3));
1166
  field_list.push_back(new Item_empty_string("Slave_SQL_Running", 3));
1167
  field_list.push_back(new Item_empty_string("Replicate_Do_DB", 20));
1168
  field_list.push_back(new Item_empty_string("Replicate_Ignore_DB", 20));
1169
  field_list.push_back(new Item_empty_string("Replicate_Do_Table", 20));
1170
  field_list.push_back(new Item_empty_string("Replicate_Ignore_Table", 23));
1171
  field_list.push_back(new Item_empty_string("Replicate_Wild_Do_Table", 24));
1172
  field_list.push_back(new Item_empty_string("Replicate_Wild_Ignore_Table",
1173
                                             28));
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1174
  field_list.push_back(new Item_return_int("Last_Errno", 4, DRIZZLE_TYPE_LONG));
1 by brian
clean slate
1175
  field_list.push_back(new Item_empty_string("Last_Error", 20));
1176
  field_list.push_back(new Item_return_int("Skip_Counter", 10,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1177
                                           DRIZZLE_TYPE_LONG));
1 by brian
clean slate
1178
  field_list.push_back(new Item_return_int("Exec_Master_Log_Pos", 10,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1179
                                           DRIZZLE_TYPE_LONGLONG));
1 by brian
clean slate
1180
  field_list.push_back(new Item_return_int("Relay_Log_Space", 10,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1181
                                           DRIZZLE_TYPE_LONGLONG));
1 by brian
clean slate
1182
  field_list.push_back(new Item_empty_string("Until_Condition", 6));
1183
  field_list.push_back(new Item_empty_string("Until_Log_File", FN_REFLEN));
1184
  field_list.push_back(new Item_return_int("Until_Log_Pos", 10,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1185
                                           DRIZZLE_TYPE_LONGLONG));
1 by brian
clean slate
1186
  field_list.push_back(new Item_return_int("Seconds_Behind_Master", 10,
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1187
                                           DRIZZLE_TYPE_LONGLONG));
1188
  field_list.push_back(new Item_return_int("Last_IO_Errno", 4, DRIZZLE_TYPE_LONG));
1 by brian
clean slate
1189
  field_list.push_back(new Item_empty_string("Last_IO_Error", 20));
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1190
  field_list.push_back(new Item_return_int("Last_SQL_Errno", 4, DRIZZLE_TYPE_LONG));
1 by brian
clean slate
1191
  field_list.push_back(new Item_empty_string("Last_SQL_Error", 20));
1192
1193
  if (protocol->send_fields(&field_list,
1194
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1195
    return(true);
1 by brian
clean slate
1196
1197
  if (mi->host[0])
1198
  {
1199
    String *packet= &thd->packet;
1200
    protocol->prepare_for_resend();
1201
1202
    /*
1203
      slave_running can be accessed without run_lock but not other
1204
      non-volotile members like mi->io_thd, which is guarded by the mutex.
1205
    */
1206
    pthread_mutex_lock(&mi->run_lock);
322.2.2 by Mats Kindahl
Hiding THD::proc_info field and providing a setter and getter.
1207
    protocol->store(mi->io_thd ? mi->io_thd->get_proc_info() : "", &my_charset_bin);
1 by brian
clean slate
1208
    pthread_mutex_unlock(&mi->run_lock);
1209
1210
    pthread_mutex_lock(&mi->data_lock);
1211
    pthread_mutex_lock(&mi->rli.data_lock);
1212
    protocol->store(mi->host, &my_charset_bin);
1213
    protocol->store(mi->user, &my_charset_bin);
205 by Brian Aker
uint32 -> uin32_t
1214
    protocol->store((uint32_t) mi->port);
1215
    protocol->store((uint32_t) mi->connect_retry);
1 by brian
clean slate
1216
    protocol->store(mi->master_log_name, &my_charset_bin);
130 by Brian Aker
ulong cleanup
1217
    protocol->store((uint64_t) mi->master_log_pos);
1 by brian
clean slate
1218
    protocol->store(mi->rli.group_relay_log_name +
1219
                    dirname_length(mi->rli.group_relay_log_name),
1220
                    &my_charset_bin);
130 by Brian Aker
ulong cleanup
1221
    protocol->store((uint64_t) mi->rli.group_relay_log_pos);
1 by brian
clean slate
1222
    protocol->store(mi->rli.group_master_log_name, &my_charset_bin);
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
1223
    protocol->store(mi->slave_running == DRIZZLE_SLAVE_RUN_CONNECT ?
1 by brian
clean slate
1224
                    "Yes" : "No", &my_charset_bin);
1225
    protocol->store(mi->rli.slave_running ? "Yes":"No", &my_charset_bin);
1226
    protocol->store(rpl_filter->get_do_db());
1227
    protocol->store(rpl_filter->get_ignore_db());
1228
1229
    char buf[256];
1230
    String tmp(buf, sizeof(buf), &my_charset_bin);
1231
    rpl_filter->get_do_table(&tmp);
1232
    protocol->store(&tmp);
1233
    rpl_filter->get_ignore_table(&tmp);
1234
    protocol->store(&tmp);
1235
    rpl_filter->get_wild_do_table(&tmp);
1236
    protocol->store(&tmp);
1237
    rpl_filter->get_wild_ignore_table(&tmp);
1238
    protocol->store(&tmp);
1239
1240
    protocol->store(mi->rli.last_error().number);
1241
    protocol->store(mi->rli.last_error().message, &my_charset_bin);
205 by Brian Aker
uint32 -> uin32_t
1242
    protocol->store((uint32_t) mi->rli.slave_skip_counter);
130 by Brian Aker
ulong cleanup
1243
    protocol->store((uint64_t) mi->rli.group_master_log_pos);
1244
    protocol->store((uint64_t) mi->rli.log_space_total);
1 by brian
clean slate
1245
1246
    protocol->store(
1247
      mi->rli.until_condition==Relay_log_info::UNTIL_NONE ? "None":
1248
        ( mi->rli.until_condition==Relay_log_info::UNTIL_MASTER_POS? "Master":
1249
          "Relay"), &my_charset_bin);
1250
    protocol->store(mi->rli.until_log_name, &my_charset_bin);
130 by Brian Aker
ulong cleanup
1251
    protocol->store((uint64_t) mi->rli.until_log_pos);
1 by brian
clean slate
1252
1253
    /*
1254
      Seconds_Behind_Master: if SQL thread is running and I/O thread is
1255
      connected, we can compute it otherwise show NULL (i.e. unknown).
1256
    */
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
1257
    if ((mi->slave_running == DRIZZLE_SLAVE_RUN_CONNECT) &&
1 by brian
clean slate
1258
        mi->rli.slave_running)
1259
    {
1260
      long time_diff= ((long)(time(0) - mi->rli.last_master_timestamp)
1261
                       - mi->clock_diff_with_master);
1262
      /*
1263
        Apparently on some systems time_diff can be <0. Here are possible
1264
        reasons related to MySQL:
1265
        - the master is itself a slave of another master whose time is ahead.
1266
        - somebody used an explicit SET TIMESTAMP on the master.
1267
        Possible reason related to granularity-to-second of time functions
1268
        (nothing to do with MySQL), which can explain a value of -1:
1269
        assume the master's and slave's time are perfectly synchronized, and
1270
        that at slave's connection time, when the master's timestamp is read,
1271
        it is at the very end of second 1, and (a very short time later) when
1272
        the slave's timestamp is read it is at the very beginning of second
1273
        2. Then the recorded value for master is 1 and the recorded value for
1274
        slave is 2. At SHOW SLAVE STATUS time, assume that the difference
1275
        between timestamp of slave and rli->last_master_timestamp is 0
1276
        (i.e. they are in the same second), then we get 0-(2-1)=-1 as a result.
398.1.4 by Monty Taylor
Renamed max/min.
1277
        This confuses users, so we don't go below 0: hence the cmax().
1 by brian
clean slate
1278
1279
        last_master_timestamp == 0 (an "impossible" timestamp 1970) is a
1280
        special marker to say "consider we have caught up".
1281
      */
152 by Brian Aker
longlong replacement
1282
      protocol->store((int64_t)(mi->rli.last_master_timestamp ?
398.1.4 by Monty Taylor
Renamed max/min.
1283
                                 cmax((long)0, time_diff) : 0));
1 by brian
clean slate
1284
    }
1285
    else
1286
    {
1287
      protocol->store_null();
1288
    }
1289
1290
    // Last_IO_Errno
1291
    protocol->store(mi->last_error().number);
1292
    // Last_IO_Error
1293
    protocol->store(mi->last_error().message, &my_charset_bin);
1294
    // Last_SQL_Errno
1295
    protocol->store(mi->rli.last_error().number);
1296
    // Last_SQL_Error
1297
    protocol->store(mi->rli.last_error().message, &my_charset_bin);
1298
1299
    pthread_mutex_unlock(&mi->rli.data_lock);
1300
    pthread_mutex_unlock(&mi->data_lock);
1301
481 by Brian Aker
Remove all of uchar.
1302
    if (my_net_write(&thd->net, (unsigned char*) thd->packet.ptr(), packet->length()))
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1303
      return(true);
1 by brian
clean slate
1304
  }
1305
  my_eof(thd);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1306
  return(false);
1 by brian
clean slate
1307
}
1308
1309
1310
void set_slave_thread_options(THD* thd)
1311
{
1312
  /*
1313
     It's nonsense to constrain the slave threads with max_join_size; if a
1314
     query succeeded on master, we HAVE to execute it. So set
1315
     OPTION_BIG_SELECTS. Setting max_join_size to HA_POS_ERROR is not enough
1316
     (and it's not needed if we have OPTION_BIG_SELECTS) because an INSERT
1317
     SELECT examining more than 4 billion rows would still fail (yes, because
1318
     when max_join_size is 4G, OPTION_BIG_SELECTS is automatically set, but
1319
     only for client threads.
1320
  */
130 by Brian Aker
ulong cleanup
1321
  uint64_t options= thd->options | OPTION_BIG_SELECTS;
1 by brian
clean slate
1322
  if (opt_log_slave_updates)
1323
    options|= OPTION_BIN_LOG;
1324
  else
1325
    options&= ~OPTION_BIN_LOG;
1326
  thd->options= options;
1327
  thd->variables.completion_type= 0;
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1328
  return;
1 by brian
clean slate
1329
}
1330
1331
/*
1332
  init_slave_thread()
1333
*/
1334
130 by Brian Aker
ulong cleanup
1335
static int32_t init_slave_thread(THD* thd, SLAVE_THD_TYPE thd_type)
1 by brian
clean slate
1336
{
130 by Brian Aker
ulong cleanup
1337
  int32_t simulate_error= 0;
1 by brian
clean slate
1338
  thd->system_thread = (thd_type == SLAVE_THD_SQL) ?
1339
    SYSTEM_THREAD_SLAVE_SQL : SYSTEM_THREAD_SLAVE_IO;
1340
  thd->security_ctx->skip_grants();
1341
  my_net_init(&thd->net, 0);
1342
/*
1343
  Adding MAX_LOG_EVENT_HEADER_LEN to the max_allowed_packet on all
1344
  slave threads, since a replication event can become this much larger
1345
  than the corresponding packet (query) sent from client to master.
1346
*/
1347
  thd->variables.max_allowed_packet= global_system_variables.max_allowed_packet
1348
    + MAX_LOG_EVENT_HEADER;  /* note, incr over the global not session var */
1349
  thd->slave_thread = 1;
1350
  thd->enable_slow_log= opt_log_slow_slave_statements;
1351
  set_slave_thread_options(thd);
1352
  thd->client_capabilities = CLIENT_LOCAL_FILES;
1353
  pthread_mutex_lock(&LOCK_thread_count);
1354
  thd->thread_id= thd->variables.pseudo_thread_id= thread_id++;
1355
  pthread_mutex_unlock(&LOCK_thread_count);
1356
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1357
 simulate_error|= (1 << SLAVE_THD_IO);
1358
 simulate_error|= (1 << SLAVE_THD_SQL);
1 by brian
clean slate
1359
  if (init_thr_lock() || thd->store_globals() || simulate_error & (1<< thd_type))
1360
  {
1361
    thd->cleanup();
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1362
    return(-1);
1 by brian
clean slate
1363
  }
1364
  lex_start(thd);
1365
1366
  if (thd_type == SLAVE_THD_SQL)
1367
    thd_proc_info(thd, "Waiting for the next event in relay log");
1368
  else
1369
    thd_proc_info(thd, "Waiting for master update");
1370
  thd->version=refresh_version;
1371
  thd->set_time();
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1372
  return(0);
1 by brian
clean slate
1373
}
1374
1375
130 by Brian Aker
ulong cleanup
1376
static int32_t safe_sleep(THD* thd, int32_t sec, CHECK_KILLED_FUNC thread_killed,
1 by brian
clean slate
1377
                      void* thread_killed_arg)
1378
{
130 by Brian Aker
ulong cleanup
1379
  int32_t nap_time;
1 by brian
clean slate
1380
  thr_alarm_t alarmed;
1381
1382
  thr_alarm_init(&alarmed);
1383
  time_t start_time= my_time(0);
1384
  time_t end_time= start_time+sec;
1385
130 by Brian Aker
ulong cleanup
1386
  while ((nap_time= (int32_t) (end_time - start_time)) > 0)
1 by brian
clean slate
1387
  {
1388
    ALARM alarm_buff;
1389
    /*
1390
      The only reason we are asking for alarm is so that
1391
      we will be woken up in case of murder, so if we do not get killed,
1392
      set the alarm so it goes off after we wake up naturally
1393
    */
1394
    thr_alarm(&alarmed, 2 * nap_time, &alarm_buff);
1395
    sleep(nap_time);
1396
    thr_end_alarm(&alarmed);
1397
1398
    if ((*thread_killed)(thd,thread_killed_arg))
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1399
      return(1);
1 by brian
clean slate
1400
    start_time= my_time(0);
1401
  }
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1402
  return(0);
1 by brian
clean slate
1403
}
1404
1405
206.3.1 by Patrick Galbraith
Most everything working with client rename
1406
static int32_t request_dump(DRIZZLE *drizzle, Master_info* mi,
1 by brian
clean slate
1407
                        bool *suppress_warnings)
1408
{
481 by Brian Aker
Remove all of uchar.
1409
  unsigned char buf[FN_REFLEN + 10];
130 by Brian Aker
ulong cleanup
1410
  int32_t len;
1411
  int32_t binlog_flags = 0; // for now
1 by brian
clean slate
1412
  char* logname = mi->master_log_name;
1413
  
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1414
  *suppress_warnings= false;
1 by brian
clean slate
1415
1416
  // TODO if big log files: Change next to int8store()
130 by Brian Aker
ulong cleanup
1417
  int4store(buf, (uint32_t) mi->master_log_pos);
1 by brian
clean slate
1418
  int2store(buf + 4, binlog_flags);
1419
  int4store(buf + 6, server_id);
130 by Brian Aker
ulong cleanup
1420
  len = (uint32_t) strlen(logname);
1 by brian
clean slate
1421
  memcpy(buf + 10, logname,len);
206.3.1 by Patrick Galbraith
Most everything working with client rename
1422
  if (simple_command(drizzle, COM_BINLOG_DUMP, buf, len + 10, 1))
1 by brian
clean slate
1423
  {
1424
    /*
1425
      Something went wrong, so we will just reconnect and retry later
1426
      in the future, we should do a better error analysis, but for
1427
      now we just fill up the error log :-)
1428
    */
206.3.1 by Patrick Galbraith
Most everything working with client rename
1429
    if (drizzle_errno(drizzle) == ER_NET_READ_INTERRUPTED)
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1430
      *suppress_warnings= true;                 // Suppress reconnect warning
1 by brian
clean slate
1431
    else
236.1.44 by Monty Taylor
Added en_US translation file.
1432
      sql_print_error(_("Error on COM_BINLOG_DUMP: %d  %s, will retry in %d secs"),
206.3.1 by Patrick Galbraith
Most everything working with client rename
1433
                      drizzle_errno(drizzle), drizzle_error(drizzle),
1 by brian
clean slate
1434
                      mi->connect_retry);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1435
    return(1);
1 by brian
clean slate
1436
  }
1437
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1438
  return(0);
1 by brian
clean slate
1439
}
1440
1441
/*
1442
  Read one event from the master
1443
1444
  SYNOPSIS
1445
    read_event()
206.3.1 by Patrick Galbraith
Most everything working with client rename
1446
    DRIZZLE               DRIZZLE connection
1 by brian
clean slate
1447
    mi                  Master connection information
1448
    suppress_warnings   TRUE when a normal net read timeout has caused us to
1449
                        try a reconnect.  We do not want to print anything to
1450
                        the error log in this case because this a anormal
1451
                        event in an idle server.
1452
1453
    RETURN VALUES
1454
    'packet_error'      Error
1455
    number              Length of packet
1456
*/
1457
206.3.1 by Patrick Galbraith
Most everything working with client rename
1458
static uint32_t read_event(DRIZZLE *drizzle,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1459
                        Master_info *mi __attribute__((unused)),
77.1.46 by Monty Taylor
Finished the warnings work!
1460
                        bool* suppress_warnings)
1 by brian
clean slate
1461
{
130 by Brian Aker
ulong cleanup
1462
  uint32_t len;
1 by brian
clean slate
1463
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1464
  *suppress_warnings= false;
1 by brian
clean slate
1465
  /*
1466
    my_real_read() will time us out
1467
    We check if we were told to die, and if not, try reading again
1468
  */
1469
  if (disconnect_slave_event_count && !(mi->events_till_disconnect--))
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1470
    return(packet_error);
1 by brian
clean slate
1471
206.3.1 by Patrick Galbraith
Most everything working with client rename
1472
  len = cli_safe_read(drizzle);
77.1.78 by Monty Taylor
One last bunch of warnings edits.
1473
  if (len == packet_error || (int32_t) len < 1)
1 by brian
clean slate
1474
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
1475
    if (drizzle_errno(drizzle) == ER_NET_READ_INTERRUPTED)
1 by brian
clean slate
1476
    {
1477
      /*
1478
        We are trying a normal reconnect after a read timeout;
1479
        we suppress prints to .err file as long as the reconnect
1480
        happens without problems
1481
      */
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1482
      *suppress_warnings= true;
1 by brian
clean slate
1483
    }
1484
    else
236.1.44 by Monty Taylor
Added en_US translation file.
1485
      sql_print_error(_("Error reading packet from server: %s ( server_errno=%d)"),
206.3.1 by Patrick Galbraith
Most everything working with client rename
1486
                      drizzle_error(drizzle), drizzle_errno(drizzle));
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1487
    return(packet_error);
1 by brian
clean slate
1488
  }
1489
1490
  /* Check if eof packet */
206.3.1 by Patrick Galbraith
Most everything working with client rename
1491
  if (len < 8 && drizzle->net.read_pos[0] == 254)
1 by brian
clean slate
1492
  {
236.1.44 by Monty Taylor
Added en_US translation file.
1493
    sql_print_information(_("Slave: received end packet from server, apparent "
1494
                            "master shutdown: %s"),
206.3.1 by Patrick Galbraith
Most everything working with client rename
1495
                     drizzle_error(drizzle));
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1496
     return(packet_error);
1 by brian
clean slate
1497
  }
1498
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1499
  return(len - 1);
1 by brian
clean slate
1500
}
1501
1502
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1503
int32_t check_expected_error(THD* thd __attribute__((unused)),
1504
                         Relay_log_info const *rli __attribute__((unused)),
130 by Brian Aker
ulong cleanup
1505
                         int32_t expected_error)
1 by brian
clean slate
1506
{
1507
  switch (expected_error) {
1508
  case ER_NET_READ_ERROR:
1509
  case ER_NET_ERROR_ON_WRITE:
1510
  case ER_QUERY_INTERRUPTED:
1511
  case ER_SERVER_SHUTDOWN:
1512
  case ER_NEW_ABORTING_CONNECTION:
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1513
    return(1);
1 by brian
clean slate
1514
  default:
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1515
    return(0);
1 by brian
clean slate
1516
  }
1517
}
1518
1519
1520
/*
1521
  Check if the current error is of temporary nature of not.
1522
  Some errors are temporary in nature, such as
1523
  ER_LOCK_DEADLOCK and ER_LOCK_WAIT_TIMEOUT.  Ndb also signals
1524
  that the error is temporary by pushing a warning with the error code
1525
  ER_GET_TEMPORARY_ERRMSG, if the originating error is temporary.
1526
*/
130 by Brian Aker
ulong cleanup
1527
static int32_t has_temporary_error(THD *thd)
1 by brian
clean slate
1528
{
1529
  if (thd->is_fatal_error)
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1530
    return(0);
1 by brian
clean slate
1531
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1532
  if (thd->main_da.is_error())
1533
  {
1534
    thd->clear_error();
1535
    my_error(ER_LOCK_DEADLOCK, MYF(0));
1536
  }
1 by brian
clean slate
1537
1538
  /*
1539
    If there is no message in THD, we can't say if it's a temporary
1540
    error or not. This is currently the case for Incident_log_event,
1541
    which sets no message. Return FALSE.
1542
  */
1543
  if (!thd->is_error())
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1544
    return(0);
1 by brian
clean slate
1545
1546
  /*
1547
    Temporary error codes:
1548
    currently, InnoDB deadlock detected by InnoDB or lock
1549
    wait timeout (innodb_lock_wait_timeout exceeded
1550
  */
1551
  if (thd->main_da.sql_errno() == ER_LOCK_DEADLOCK ||
1552
      thd->main_da.sql_errno() == ER_LOCK_WAIT_TIMEOUT)
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1553
    return(1);
1 by brian
clean slate
1554
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1555
  return(0);
1 by brian
clean slate
1556
}
1557
1558
1559
/**
1560
  Applies the given event and advances the relay log position.
1561
1562
  In essence, this function does:
1563
1564
  @code
1565
    ev->apply_event(rli);
1566
    ev->update_pos(rli);
1567
  @endcode
1568
1569
  But it also does some maintainance, such as skipping events if
1570
  needed and reporting errors.
1571
1572
  If the @c skip flag is set, then it is tested whether the event
1573
  should be skipped, by looking at the slave_skip_counter and the
1574
  server id.  The skip flag should be set when calling this from a
1575
  replication thread but not set when executing an explicit BINLOG
1576
  statement.
1577
1578
  @retval 0 OK.
1579
1580
  @retval 1 Error calling ev->apply_event().
1581
1582
  @retval 2 No error calling ev->apply_event(), but error calling
1583
  ev->update_pos().
1584
*/
130 by Brian Aker
ulong cleanup
1585
int32_t apply_event_and_update_pos(Log_event* ev, THD* thd, Relay_log_info* rli,
1 by brian
clean slate
1586
                               bool skip)
1587
{
130 by Brian Aker
ulong cleanup
1588
  int32_t exec_res= 0;
1 by brian
clean slate
1589
1590
  /*
1591
    Execute the event to change the database and update the binary
1592
    log coordinates, but first we set some data that is needed for
1593
    the thread.
1594
1595
    The event will be executed unless it is supposed to be skipped.
1596
1597
    Queries originating from this server must be skipped.  Low-level
1598
    events (Format_description_log_event, Rotate_log_event,
1599
    Stop_log_event) from this server must also be skipped. But for
1600
    those we don't want to modify 'group_master_log_pos', because
1601
    these events did not exist on the master.
1602
    Format_description_log_event is not completely skipped.
1603
1604
    Skip queries specified by the user in 'slave_skip_counter'.  We
1605
    can't however skip events that has something to do with the log
1606
    files themselves.
1607
1608
    Filtering on own server id is extremely important, to ignore
1609
    execution of events created by the creation/rotation of the relay
1610
    log (remember that now the relay log starts with its Format_desc,
1611
    has a Rotate etc).
1612
  */
1613
1614
  thd->server_id = ev->server_id; // use the original server id for logging
1615
  thd->set_time();                            // time the query
1616
  thd->lex->current_select= 0;
1617
  if (!ev->when)
1618
    ev->when= my_time(0);
1619
  ev->thd = thd; // because up to this point, ev->thd == 0
1620
1621
  if (skip)
1622
  {
130 by Brian Aker
ulong cleanup
1623
    int32_t reason= ev->shall_skip(rli);
1 by brian
clean slate
1624
    if (reason == Log_event::EVENT_SKIP_COUNT)
1625
      --rli->slave_skip_counter;
1626
    pthread_mutex_unlock(&rli->data_lock);
1627
    if (reason == Log_event::EVENT_SKIP_NOT)
1628
      exec_res= ev->apply_event(rli);
1629
  }
1630
  else
1631
    exec_res= ev->apply_event(rli);
1632
1633
  if (exec_res == 0)
1634
  {
130 by Brian Aker
ulong cleanup
1635
    int32_t error= ev->update_pos(rli);
1 by brian
clean slate
1636
    /*
1637
      The update should not fail, so print an error message and
1638
      return an error code.
1639
1640
      TODO: Replace this with a decent error message when merged
1641
      with BUG#24954 (which adds several new error message).
1642
    */
1643
    if (error)
1644
    {
1645
      char buf[22];
1646
      rli->report(ERROR_LEVEL, ER_UNKNOWN_ERROR,
261.3.4 by Monty Taylor
Added three more files worth of stuff.
1647
                  _("It was not possible to update the positions"
1 by brian
clean slate
1648
                  " of the relay log information: the slave may"
1649
                  " be in an inconsistent state."
261.3.4 by Monty Taylor
Added three more files worth of stuff.
1650
                  " Stopped in %s position %s"),
1 by brian
clean slate
1651
                  rli->group_relay_log_name,
1652
                  llstr(rli->group_relay_log_pos, buf));
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1653
      return(2);
1 by brian
clean slate
1654
    }
1655
  }
1656
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1657
  return(exec_res ? 1 : 0);
1 by brian
clean slate
1658
}
1659
1660
1661
/**
1662
  Top-level function for executing the next event from the relay log.
1663
1664
  This function reads the event from the relay log, executes it, and
1665
  advances the relay log position.  It also handles errors, etc.
1666
1667
  This function may fail to apply the event for the following reasons:
1668
1669
   - The position specfied by the UNTIL condition of the START SLAVE
1670
     command is reached.
1671
1672
   - It was not possible to read the event from the log.
1673
1674
   - The slave is killed.
1675
1676
   - An error occurred when applying the event, and the event has been
1677
     tried slave_trans_retries times.  If the event has been retried
1678
     fewer times, 0 is returned.
1679
1680
   - init_master_info or init_relay_log_pos failed. (These are called
1681
     if a failure occurs when applying the event.)</li>
1682
1683
   - An error occurred when updating the binlog position.
1684
1685
  @retval 0 The event was applied.
1686
1687
  @retval 1 The event was not applied.
1688
*/
130 by Brian Aker
ulong cleanup
1689
static int32_t exec_relay_log_event(THD* thd, Relay_log_info* rli)
1 by brian
clean slate
1690
{
1691
  /*
1692
     We acquire this mutex since we need it for all operations except
1693
     event execution. But we will release it in places where we will
1694
     wait for something for example inside of next_event().
1695
   */
1696
  pthread_mutex_lock(&rli->data_lock);
1697
1698
  Log_event * ev = next_event(rli);
1699
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1700
  assert(rli->sql_thd==thd);
1 by brian
clean slate
1701
1702
  if (sql_slave_killed(thd,rli))
1703
  {
1704
    pthread_mutex_unlock(&rli->data_lock);
1705
    delete ev;
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1706
    return(1);
1 by brian
clean slate
1707
  }
1708
  if (ev)
1709
  {
130 by Brian Aker
ulong cleanup
1710
    int32_t exec_res;
1 by brian
clean slate
1711
1712
    /*
1713
      This tests if the position of the beginning of the current event
1714
      hits the UNTIL barrier.
1715
    */
1716
    if (rli->until_condition != Relay_log_info::UNTIL_NONE &&
1717
        rli->is_until_satisfied((rli->is_in_group() || !ev->log_pos) ?
1718
                                rli->group_master_log_pos :
1719
                                ev->log_pos - ev->data_written))
1720
    {
1721
      char buf[22];
236.1.44 by Monty Taylor
Added en_US translation file.
1722
      sql_print_information(_("Slave SQL thread stopped because it reached its"
1723
                              " UNTIL position %s"),
1724
                            llstr(rli->until_pos(), buf));
1 by brian
clean slate
1725
      /*
1726
        Setting abort_slave flag because we do not want additional message about
1727
        error in query execution to be printed.
1728
      */
1729
      rli->abort_slave= 1;
1730
      pthread_mutex_unlock(&rli->data_lock);
1731
      delete ev;
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1732
      return(1);
1 by brian
clean slate
1733
    }
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1734
    exec_res= apply_event_and_update_pos(ev, thd, rli, true);
1 by brian
clean slate
1735
1736
    /*
1737
      Format_description_log_event should not be deleted because it will be
1738
      used to read info about the relay log's format; it will be deleted when
1739
      the SQL thread does not need it, i.e. when this thread terminates.
1740
    */
1741
    if (ev->get_type_code() != FORMAT_DESCRIPTION_EVENT)
1742
    {
1743
      delete ev;
1744
    }
1745
1746
    /*
1747
      update_log_pos failed: this should not happen, so we don't
1748
      retry.
1749
    */
1750
    if (exec_res == 2)
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1751
      return(1);
1 by brian
clean slate
1752
1753
    if (slave_trans_retries)
1754
    {
130 by Brian Aker
ulong cleanup
1755
      int32_t temp_err= 0;
1 by brian
clean slate
1756
      if (exec_res && (temp_err= has_temporary_error(thd)))
1757
      {
1758
        const char *errmsg;
1759
        /*
1760
          We were in a transaction which has been rolled back because of a
1761
          temporary error;
1762
          let's seek back to BEGIN log event and retry it all again.
1763
	  Note, if lock wait timeout (innodb_lock_wait_timeout exceeded)
1764
	  there is no rollback since 5.0.13 (ref: manual).
1765
          We have to not only seek but also
1766
          a) init_master_info(), to seek back to hot relay log's start for later
1767
          (for when we will come back to this hot log after re-processing the
1768
          possibly existing old logs where BEGIN is: check_binlog_magic() will
1769
          then need the cache to be at position 0 (see comments at beginning of
1770
          init_master_info()).
1771
          b) init_relay_log_pos(), because the BEGIN may be an older relay log.
1772
        */
1773
        if (rli->trans_retries < slave_trans_retries)
1774
        {
1775
          if (init_master_info(rli->mi, 0, 0, 0, SLAVE_SQL))
236.1.44 by Monty Taylor
Added en_US translation file.
1776
            sql_print_error(_("Failed to initialize the master info structure"));
1 by brian
clean slate
1777
          else if (init_relay_log_pos(rli,
1778
                                      rli->group_relay_log_name,
1779
                                      rli->group_relay_log_pos,
1780
                                      1, &errmsg, 1))
236.1.44 by Monty Taylor
Added en_US translation file.
1781
            sql_print_error(_("Error initializing relay log position: %s"),
1 by brian
clean slate
1782
                            errmsg);
1783
          else
1784
          {
1785
            exec_res= 0;
1786
            end_trans(thd, ROLLBACK);
1787
            /* chance for concurrent connection to get more locks */
398.1.4 by Monty Taylor
Renamed max/min.
1788
            safe_sleep(thd, cmin(rli->trans_retries, (uint32_t)MAX_SLAVE_RETRY_PAUSE),
1 by brian
clean slate
1789
                       (CHECK_KILLED_FUNC)sql_slave_killed, (void*)rli);
1790
            pthread_mutex_lock(&rli->data_lock); // because of SHOW STATUS
1791
            rli->trans_retries++;
1792
            rli->retried_trans++;
1793
            pthread_mutex_unlock(&rli->data_lock);
1794
          }
1795
        }
1796
        else
236.1.44 by Monty Taylor
Added en_US translation file.
1797
          sql_print_error(_("Slave SQL thread retried transaction %lu time(s) "
1798
                            "in vain, giving up. Consider raising the value of "
1799
                            "the slave_transaction_retries variable."),
1 by brian
clean slate
1800
                          slave_trans_retries);
1801
      }
143 by Brian Aker
Bool cleanup.
1802
      else if ((exec_res && !temp_err) ||
1 by brian
clean slate
1803
               (opt_using_transactions &&
1804
                rli->group_relay_log_pos == rli->event_relay_log_pos))
1805
      {
1806
        /*
1807
          Only reset the retry counter if the entire group succeeded
1808
          or failed with a non-transient error.  On a successful
1809
          event, the execution will proceed as usual; in the case of a
1810
          non-transient error, the slave will stop with an error.
1811
         */
1812
        rli->trans_retries= 0; // restart from fresh
1813
      }
1814
    }
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1815
    return(exec_res);
1 by brian
clean slate
1816
  }
1817
  pthread_mutex_unlock(&rli->data_lock);
1818
  rli->report(ERROR_LEVEL, ER_SLAVE_RELAY_LOG_READ_FAILURE,
261.3.4 by Monty Taylor
Added three more files worth of stuff.
1819
              ER(ER_SLAVE_RELAY_LOG_READ_FAILURE),
1820
              _("Could not parse relay log event entry. The possible reasons "
1821
                "are: the master's binary log is corrupted (you can check this "
1822
                "by running 'mysqlbinlog' on the binary log), the slave's "
1823
                "relay log is corrupted (you can check this by running "
1824
                "'mysqlbinlog' on the relay log), a network problem, or a bug "
1825
                "in the master's or slave's DRIZZLE code. If you want to check "
1826
                "the master's binary log or slave's relay log, you will be "
1827
                "able to know their names by issuing 'SHOW SLAVE STATUS' "
1828
                "on this slave."));
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1829
  return(1);
1 by brian
clean slate
1830
}
1831
1832
1833
/**
1834
  @brief Try to reconnect slave IO thread.
1835
1836
  @details Terminates current connection to master, sleeps for
1837
  @c mi->connect_retry msecs and initiates new connection with
1838
  @c safe_reconnect(). Variable pointed by @c retry_count is increased -
1839
  if it exceeds @c master_retry_count then connection is not re-established
1840
  and function signals error.
1841
  Unless @c suppres_warnings is TRUE, a warning is put in the server error log
1842
  when reconnecting. The warning message and messages used to report errors
1843
  are taken from @c messages array. In case @c master_retry_count is exceeded,
1844
  no messages are added to the log.
1845
1846
  @param[in]     thd                 Thread context.
206.3.1 by Patrick Galbraith
Most everything working with client rename
1847
  @param[in]     DRIZZLE               DRIZZLE connection.
1 by brian
clean slate
1848
  @param[in]     mi                  Master connection information.
1849
  @param[in,out] retry_count         Number of attempts to reconnect.
1850
  @param[in]     suppress_warnings   TRUE when a normal net read timeout 
1851
                                     has caused to reconnecting.
1852
  @param[in]     messages            Messages to print/log, see 
1853
                                     reconnect_messages[] array.
1854
1855
  @retval        0                   OK.
1856
  @retval        1                   There was an error.
1857
*/
1858
206.3.1 by Patrick Galbraith
Most everything working with client rename
1859
static int32_t try_to_reconnect(THD *thd, DRIZZLE *drizzle, Master_info *mi,
390.1.5 by Monty Taylor
Moved more functions into drizzle.c as part of the split of code.
1860
                                uint32_t *retry_count, bool suppress_warnings,
1861
                                const char *messages[SLAVE_RECON_MSG_MAX])
1 by brian
clean slate
1862
{
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
1863
  mi->slave_running= DRIZZLE_SLAVE_RUN_NOT_CONNECT;
322.2.2 by Mats Kindahl
Hiding THD::proc_info field and providing a setter and getter.
1864
  thd->set_proc_info(_(messages[SLAVE_RECON_MSG_WAIT]));
390.1.5 by Monty Taylor
Moved more functions into drizzle.c as part of the split of code.
1865
  drizzle_disconnect(drizzle);
1 by brian
clean slate
1866
  if ((*retry_count)++)
1867
  {
1868
    if (*retry_count > master_retry_count)
1869
      return 1;                             // Don't retry forever
1870
    safe_sleep(thd, mi->connect_retry, (CHECK_KILLED_FUNC) io_slave_killed,
1871
               (void *) mi);
1872
  }
236.1.45 by Monty Taylor
Fixed a few gettext bugs.
1873
  if (check_io_slave_killed(thd, mi,
1874
                            _(messages[SLAVE_RECON_MSG_KILLED_WAITING])))
1 by brian
clean slate
1875
    return 1;
322.2.2 by Mats Kindahl
Hiding THD::proc_info field and providing a setter and getter.
1876
  thd->set_proc_info(_(messages[SLAVE_RECON_MSG_AFTER]));
236.1.45 by Monty Taylor
Fixed a few gettext bugs.
1877
  if (!suppress_warnings)
1 by brian
clean slate
1878
  {
1879
    char buf[256], llbuff[22];
236.1.45 by Monty Taylor
Fixed a few gettext bugs.
1880
    snprintf(buf, sizeof(buf), _(messages[SLAVE_RECON_MSG_FAILED]),
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
1881
             IO_RPL_LOG_NAME, llstr(mi->master_log_pos, llbuff));
236.1.45 by Monty Taylor
Fixed a few gettext bugs.
1882
    /*
1 by brian
clean slate
1883
      Raise a warining during registering on master/requesting dump.
1884
      Log a message reading event.
1885
    */
236.1.45 by Monty Taylor
Fixed a few gettext bugs.
1886
    if (_(messages[SLAVE_RECON_MSG_COMMAND])[0])
1 by brian
clean slate
1887
    {
1888
      mi->report(WARNING_LEVEL, ER_SLAVE_MASTER_COM_FAILURE,
236.1.45 by Monty Taylor
Fixed a few gettext bugs.
1889
                 ER(ER_SLAVE_MASTER_COM_FAILURE),
1890
                 _(messages[SLAVE_RECON_MSG_COMMAND]), buf);
1 by brian
clean slate
1891
    }
1892
    else
1893
    {
1894
      sql_print_information(buf);
1895
    }
1896
  }
206.3.1 by Patrick Galbraith
Most everything working with client rename
1897
  if (safe_reconnect(thd, drizzle, mi, 1) || io_slave_killed(thd, mi))
1 by brian
clean slate
1898
  {
1899
    if (global_system_variables.log_warnings)
236.1.45 by Monty Taylor
Fixed a few gettext bugs.
1900
      sql_print_information(_(messages[SLAVE_RECON_MSG_KILLED_AFTER]));
1 by brian
clean slate
1901
    return 1;
1902
  }
1903
  return 0;
1904
}
1905
1906
1907
/* Slave I/O Thread entry point */
1908
1909
pthread_handler_t handle_slave_io(void *arg)
1910
{
1911
  THD *thd; // needs to be first for thread_stack
206.3.1 by Patrick Galbraith
Most everything working with client rename
1912
  DRIZZLE *drizzle;
1 by brian
clean slate
1913
  Master_info *mi = (Master_info*)arg;
1914
  Relay_log_info *rli= &mi->rli;
1915
  char llbuff[22];
130 by Brian Aker
ulong cleanup
1916
  uint32_t retry_count;
1 by brian
clean slate
1917
  bool suppress_warnings;
130 by Brian Aker
ulong cleanup
1918
  uint32_t retry_count_reg= 0, retry_count_dump= 0, retry_count_event= 0;
1 by brian
clean slate
1919
  my_thread_init();
1920
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1921
  assert(mi->inited);
206.3.1 by Patrick Galbraith
Most everything working with client rename
1922
  drizzle= NULL ;
1 by brian
clean slate
1923
  retry_count= 0;
1924
1925
  pthread_mutex_lock(&mi->run_lock);
1926
  /* Inform waiting threads that slave has started */
1927
  mi->slave_run_id++;
1928
1929
  mi->events_till_disconnect = disconnect_slave_event_count;
1930
51.1.75 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1931
  thd= new THD;
1 by brian
clean slate
1932
  THD_CHECK_SENTRY(thd);
1933
  mi->io_thd = thd;
1934
1935
  pthread_detach_this_thread();
1936
  thd->thread_stack= (char*) &thd; // remember where our stack is
1937
  if (init_slave_thread(thd, SLAVE_THD_IO))
1938
  {
1939
    pthread_cond_broadcast(&mi->start_cond);
1940
    pthread_mutex_unlock(&mi->run_lock);
236.1.44 by Monty Taylor
Added en_US translation file.
1941
    sql_print_error(_("Failed during slave I/O thread initialization"));
1 by brian
clean slate
1942
    goto err;
1943
  }
1944
  pthread_mutex_lock(&LOCK_thread_count);
1945
  threads.append(thd);
1946
  pthread_mutex_unlock(&LOCK_thread_count);
1947
  mi->slave_running = 1;
1948
  mi->abort_slave = 0;
1949
  pthread_mutex_unlock(&mi->run_lock);
1950
  pthread_cond_broadcast(&mi->start_cond);
1951
202.2.4 by Monty Taylor
Merged from Patrick.
1952
  if (!(mi->drizzle= drizzle = drizzle_create(NULL)))
1 by brian
clean slate
1953
  {
1954
    mi->report(ERROR_LEVEL, ER_SLAVE_FATAL_ERROR,
261.3.4 by Monty Taylor
Added three more files worth of stuff.
1955
               ER(ER_SLAVE_FATAL_ERROR), _("error in drizzle_create()"));
1 by brian
clean slate
1956
    goto err;
1957
  }
1958
1959
  thd_proc_info(thd, "Connecting to master");
1960
  // we can get killed during safe_connect
206.3.1 by Patrick Galbraith
Most everything working with client rename
1961
  if (!safe_connect(thd, drizzle, mi))
1 by brian
clean slate
1962
  {
236.1.44 by Monty Taylor
Added en_US translation file.
1963
    sql_print_information(_("Slave I/O thread: connected to master '%s@%s:%d',"
1964
                            "replication started in log '%s' at position %s"),
1 by brian
clean slate
1965
                          mi->user, mi->host, mi->port,
1966
			  IO_RPL_LOG_NAME,
1967
			  llstr(mi->master_log_pos,llbuff));
1968
  /*
1969
    Adding MAX_LOG_EVENT_HEADER_LEN to the max_packet_size on the I/O
1970
    thread, since a replication event can become this much larger than
1971
    the corresponding packet (query) sent from client to master.
1972
  */
206.3.1 by Patrick Galbraith
Most everything working with client rename
1973
    drizzle->net.max_packet_size= thd->net.max_packet_size+= MAX_LOG_EVENT_HEADER;
1 by brian
clean slate
1974
  }
1975
  else
1976
  {
236.1.44 by Monty Taylor
Added en_US translation file.
1977
    sql_print_information(_("Slave I/O thread killed while connecting to master"));
1 by brian
clean slate
1978
    goto err;
1979
  }
1980
1981
connected:
1982
1983
  // TODO: the assignment below should be under mutex (5.0)
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
1984
  mi->slave_running= DRIZZLE_SLAVE_RUN_CONNECT;
206.3.1 by Patrick Galbraith
Most everything working with client rename
1985
  thd->slave_net = &drizzle->net;
1 by brian
clean slate
1986
  thd_proc_info(thd, "Checking master version");
206.3.1 by Patrick Galbraith
Most everything working with client rename
1987
  if (get_master_version_and_clock(drizzle, mi))
1 by brian
clean slate
1988
    goto err;
236.1.44 by Monty Taylor
Added en_US translation file.
1989
  
1 by brian
clean slate
1990
  if (mi->rli.relay_log.description_event_for_queue->binlog_version > 1)
1991
  {
1992
    /*
1993
      Register ourselves with the master.
1994
    */
1995
    thd_proc_info(thd, "Registering slave on master");
206.3.1 by Patrick Galbraith
Most everything working with client rename
1996
    if (register_slave_on_master(drizzle, mi, &suppress_warnings))
1 by brian
clean slate
1997
    {
1998
      if (!check_io_slave_killed(thd, mi, "Slave I/O thread killed "
236.1.44 by Monty Taylor
Added en_US translation file.
1999
                                 "while registering slave on master"))
1 by brian
clean slate
2000
      {
236.1.44 by Monty Taylor
Added en_US translation file.
2001
        sql_print_error(_("Slave I/O thread couldn't register on master"));
206.3.1 by Patrick Galbraith
Most everything working with client rename
2002
        if (try_to_reconnect(thd, drizzle, mi, &retry_count, suppress_warnings,
236.1.45 by Monty Taylor
Fixed a few gettext bugs.
2003
                             reconnect_messages[SLAVE_RECON_ACT_REG]))
1 by brian
clean slate
2004
          goto err;
2005
      }
2006
      else
2007
        goto err;
2008
      goto connected;
2009
    }
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2010
    if (!retry_count_reg)
2011
    {
2012
      retry_count_reg++;
236.1.44 by Monty Taylor
Added en_US translation file.
2013
      sql_print_information(_("Forcing to reconnect slave I/O thread"));
206.3.1 by Patrick Galbraith
Most everything working with client rename
2014
      if (try_to_reconnect(thd, drizzle, mi, &retry_count, suppress_warnings,
236.1.45 by Monty Taylor
Fixed a few gettext bugs.
2015
                           reconnect_messages[SLAVE_RECON_ACT_REG]))
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2016
        goto err;
2017
      goto connected;
2018
    }
1 by brian
clean slate
2019
  }
2020
2021
  while (!io_slave_killed(thd,mi))
2022
  {
2023
    thd_proc_info(thd, "Requesting binlog dump");
206.3.1 by Patrick Galbraith
Most everything working with client rename
2024
    if (request_dump(drizzle, mi, &suppress_warnings))
1 by brian
clean slate
2025
    {
236.1.44 by Monty Taylor
Added en_US translation file.
2026
      sql_print_error(_("Failed on request_dump()"));
2027
      if (check_io_slave_killed(thd, mi, _("Slave I/O thread killed while \
2028
requesting master dump")) ||
206.3.1 by Patrick Galbraith
Most everything working with client rename
2029
          try_to_reconnect(thd, drizzle, mi, &retry_count, suppress_warnings,
236.1.45 by Monty Taylor
Fixed a few gettext bugs.
2030
                           reconnect_messages[SLAVE_RECON_ACT_DUMP]))
1 by brian
clean slate
2031
        goto err;
2032
      goto connected;
2033
    }
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2034
    if (!retry_count_dump)
2035
    {
2036
      retry_count_dump++;
236.1.44 by Monty Taylor
Added en_US translation file.
2037
      sql_print_information(_("Forcing to reconnect slave I/O thread"));
206.3.1 by Patrick Galbraith
Most everything working with client rename
2038
      if (try_to_reconnect(thd, drizzle, mi, &retry_count, suppress_warnings,
236.1.45 by Monty Taylor
Fixed a few gettext bugs.
2039
                           reconnect_messages[SLAVE_RECON_ACT_DUMP]))
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2040
        goto err;
2041
      goto connected;
2042
    }
1 by brian
clean slate
2043
2044
    while (!io_slave_killed(thd,mi))
2045
    {
130 by Brian Aker
ulong cleanup
2046
      uint32_t event_len;
1 by brian
clean slate
2047
      /*
236.1.44 by Monty Taylor
Added en_US translation file.
2048
        We say "waiting" because read_event() will wait if there's nothing to
2049
        read. But if there's something to read, it will not wait. The
2050
        important thing is to not confuse users by saying "reading" whereas
2051
        we're in fact receiving nothing.
1 by brian
clean slate
2052
      */
236.1.44 by Monty Taylor
Added en_US translation file.
2053
      thd_proc_info(thd, _("Waiting for master to send event"));
206.3.1 by Patrick Galbraith
Most everything working with client rename
2054
      event_len= read_event(drizzle, mi, &suppress_warnings);
236.1.44 by Monty Taylor
Added en_US translation file.
2055
      if (check_io_slave_killed(thd, mi, _("Slave I/O thread killed while "
2056
                                           "reading event")))
1 by brian
clean slate
2057
        goto err;
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2058
      if (!retry_count_event)
2059
      {
2060
        retry_count_event++;
236.1.44 by Monty Taylor
Added en_US translation file.
2061
        sql_print_information(_("Forcing to reconnect slave I/O thread"));
206.3.1 by Patrick Galbraith
Most everything working with client rename
2062
        if (try_to_reconnect(thd, drizzle, mi, &retry_count, suppress_warnings,
236.1.45 by Monty Taylor
Fixed a few gettext bugs.
2063
                             reconnect_messages[SLAVE_RECON_ACT_EVENT]))
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2064
          goto err;
2065
        goto connected;
2066
      }
1 by brian
clean slate
2067
2068
      if (event_len == packet_error)
2069
      {
206.3.1 by Patrick Galbraith
Most everything working with client rename
2070
        uint32_t drizzle_error_number= drizzle_errno(drizzle);
2071
        switch (drizzle_error_number) {
1 by brian
clean slate
2072
        case CR_NET_PACKET_TOO_LARGE:
236.1.44 by Monty Taylor
Added en_US translation file.
2073
          sql_print_error(_("Log entry on master is longer than "
2074
                            "max_allowed_packet (%ld) on "
2075
                            "slave. If the entry is correct, restart the "
2076
                            "server with a higher value of "
2077
                            "max_allowed_packet"),
1 by brian
clean slate
2078
                          thd->variables.max_allowed_packet);
2079
          goto err;
2080
        case ER_MASTER_FATAL_ERROR_READING_BINLOG:
206.3.1 by Patrick Galbraith
Most everything working with client rename
2081
          sql_print_error(ER(drizzle_error_number), drizzle_error_number,
2082
                          drizzle_error(drizzle));
1 by brian
clean slate
2083
          goto err;
2084
        case EE_OUTOFMEMORY:
2085
        case ER_OUTOFMEMORY:
236.1.44 by Monty Taylor
Added en_US translation file.
2086
          sql_print_error(
2087
       _("Stopping slave I/O thread due to out-of-memory error from master"));
1 by brian
clean slate
2088
          goto err;
2089
        }
206.3.1 by Patrick Galbraith
Most everything working with client rename
2090
        if (try_to_reconnect(thd, drizzle, mi, &retry_count, suppress_warnings,
236.1.45 by Monty Taylor
Fixed a few gettext bugs.
2091
                             reconnect_messages[SLAVE_RECON_ACT_EVENT]))
1 by brian
clean slate
2092
          goto err;
2093
        goto connected;
2094
      } // if (event_len == packet_error)
2095
2096
      retry_count=0;                    // ok event, reset retry counter
236.1.44 by Monty Taylor
Added en_US translation file.
2097
      thd_proc_info(thd, _("Queueing master event to the relay log"));
206.3.1 by Patrick Galbraith
Most everything working with client rename
2098
      if (queue_event(mi,(const char*)drizzle->net.read_pos + 1, event_len))
1 by brian
clean slate
2099
      {
2100
        goto err;
2101
      }
2102
      if (flush_master_info(mi, 1))
2103
      {
236.1.44 by Monty Taylor
Added en_US translation file.
2104
        sql_print_error(_("Failed to flush master info file"));
1 by brian
clean slate
2105
        goto err;
2106
      }
2107
      /*
2108
        See if the relay logs take too much space.
2109
        We don't lock mi->rli.log_space_lock here; this dirty read saves time
2110
        and does not introduce any problem:
2111
        - if mi->rli.ignore_log_space_limit is 1 but becomes 0 just after (so
2112
        the clean value is 0), then we are reading only one more event as we
2113
        should, and we'll block only at the next event. No big deal.
2114
        - if mi->rli.ignore_log_space_limit is 0 but becomes 1 just after (so
2115
        the clean value is 1), then we are going into wait_for_relay_log_space()
2116
        for no reason, but this function will do a clean read, notice the clean
2117
        value and exit immediately.
2118
      */
2119
      if (rli->log_space_limit && rli->log_space_limit <
2120
          rli->log_space_total &&
2121
          !rli->ignore_log_space_limit)
2122
        if (wait_for_relay_log_space(rli))
2123
        {
236.1.44 by Monty Taylor
Added en_US translation file.
2124
          sql_print_error(_("Slave I/O thread aborted while waiting for "
2125
                            "relay log space"));
1 by brian
clean slate
2126
          goto err;
2127
        }
2128
    }
2129
  }
2130
236.1.44 by Monty Taylor
Added en_US translation file.
2131
// error = 0;
1 by brian
clean slate
2132
err:
236.1.44 by Monty Taylor
Added en_US translation file.
2133
// print the current replication position
2134
  sql_print_information(_("Slave I/O thread exiting, read up to log '%s', "
2135
                          "position %s"),
2136
                        IO_RPL_LOG_NAME, llstr(mi->master_log_pos,llbuff));
398.1.6 by Monty Taylor
Removed __alpha__ references.
2137
  pthread_mutex_lock(&LOCK_thread_count);
1 by brian
clean slate
2138
  thd->query = thd->db = 0; // extra safety
2139
  thd->query_length= thd->db_length= 0;
398.1.6 by Monty Taylor
Removed __alpha__ references.
2140
  pthread_mutex_unlock(&LOCK_thread_count);
206.3.1 by Patrick Galbraith
Most everything working with client rename
2141
  if (drizzle)
1 by brian
clean slate
2142
  {
2143
    /*
2144
      Here we need to clear the active VIO before closing the
2145
      connection with the master.  The reason is that THD::awake()
2146
      might be called from terminate_slave_thread() because somebody
2147
      issued a STOP SLAVE.  If that happends, the close_active_vio()
2148
      can be called in the middle of closing the VIO associated with
2149
      the 'mysql' object, causing a crash.
2150
    */
206.3.1 by Patrick Galbraith
Most everything working with client rename
2151
    drizzle_close(drizzle);
2152
    mi->drizzle=0;
1 by brian
clean slate
2153
  }
2154
  write_ignored_events_info_to_relay_log(thd, mi);
236.1.44 by Monty Taylor
Added en_US translation file.
2155
  thd_proc_info(thd, _("Waiting for slave mutex on exit"));
1 by brian
clean slate
2156
  pthread_mutex_lock(&mi->run_lock);
2157
2158
  /* Forget the relay log's format */
2159
  delete mi->rli.relay_log.description_event_for_queue;
2160
  mi->rli.relay_log.description_event_for_queue= 0;
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2161
  assert(thd->net.buff != 0);
1 by brian
clean slate
2162
  net_end(&thd->net); // destructor will not free it, because net.vio is 0
2163
  close_thread_tables(thd);
2164
  pthread_mutex_lock(&LOCK_thread_count);
2165
  THD_CHECK_SENTRY(thd);
2166
  delete thd;
2167
  pthread_mutex_unlock(&LOCK_thread_count);
2168
  mi->abort_slave= 0;
2169
  mi->slave_running= 0;
2170
  mi->io_thd= 0;
2171
  /*
2172
    Note: the order of the two following calls (first broadcast, then unlock)
2173
    is important. Otherwise a killer_thread can execute between the calls and
2174
    delete the mi structure leading to a crash! (see BUG#25306 for details)
2175
   */ 
2176
  pthread_cond_broadcast(&mi->stop_cond);       // tell the world we are done
2177
  pthread_mutex_unlock(&mi->run_lock);
2178
  my_thread_end();
2179
  pthread_exit(0);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2180
  return(0);                               // Can't return anything here
1 by brian
clean slate
2181
}
2182
2183
2184
/* Slave SQL Thread entry point */
2185
2186
pthread_handler_t handle_slave_sql(void *arg)
2187
{
2188
  THD *thd;                     /* needs to be first for thread_stack */
2189
  char llbuff[22],llbuff1[22];
2190
2191
  Relay_log_info* rli = &((Master_info*)arg)->rli;
2192
  const char *errmsg;
2193
2194
  my_thread_init();
2195
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2196
  assert(rli->inited);
1 by brian
clean slate
2197
  pthread_mutex_lock(&rli->run_lock);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2198
  assert(!rli->slave_running);
1 by brian
clean slate
2199
  errmsg= 0;
2200
  rli->events_till_abort = abort_slave_event_count;
2201
51.1.75 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
2202
  thd = new THD;
1 by brian
clean slate
2203
  thd->thread_stack = (char*)&thd; // remember where our stack is
2204
  rli->sql_thd= thd;
2205
  
2206
  /* Inform waiting threads that slave has started */
2207
  rli->slave_run_id++;
2208
  rli->slave_running = 1;
2209
2210
  pthread_detach_this_thread();
2211
  if (init_slave_thread(thd, SLAVE_THD_SQL))
2212
  {
2213
    /*
2214
      TODO: this is currently broken - slave start and change master
2215
      will be stuck if we fail here
2216
    */
2217
    pthread_cond_broadcast(&rli->start_cond);
2218
    pthread_mutex_unlock(&rli->run_lock);
236.1.44 by Monty Taylor
Added en_US translation file.
2219
    sql_print_error(_("Failed during slave thread initialization"));
1 by brian
clean slate
2220
    goto err;
2221
  }
2222
  thd->init_for_queries();
2223
  thd->temporary_tables = rli->save_temporary_tables; // restore temp tables
2224
  pthread_mutex_lock(&LOCK_thread_count);
2225
  threads.append(thd);
2226
  pthread_mutex_unlock(&LOCK_thread_count);
2227
  /*
2228
    We are going to set slave_running to 1. Assuming slave I/O thread is
2229
    alive and connected, this is going to make Seconds_Behind_Master be 0
2230
    i.e. "caught up". Even if we're just at start of thread. Well it's ok, at
2231
    the moment we start we can think we are caught up, and the next second we
2232
    start receiving data so we realize we are not caught up and
2233
    Seconds_Behind_Master grows. No big deal.
2234
  */
2235
  rli->abort_slave = 0;
2236
  pthread_mutex_unlock(&rli->run_lock);
2237
  pthread_cond_broadcast(&rli->start_cond);
2238
2239
  /*
2240
    Reset errors for a clean start (otherwise, if the master is idle, the SQL
2241
    thread may execute no Query_log_event, so the error will remain even
2242
    though there's no problem anymore). Do not reset the master timestamp
2243
    (imagine the slave has caught everything, the STOP SLAVE and START SLAVE:
2244
    as we are not sure that we are going to receive a query, we want to
2245
    remember the last master timestamp (to say how many seconds behind we are
2246
    now.
2247
    But the master timestamp is reset by RESET SLAVE & CHANGE MASTER.
2248
  */
2249
  rli->clear_error();
2250
2251
  //tell the I/O thread to take relay_log_space_limit into account from now on
2252
  pthread_mutex_lock(&rli->log_space_lock);
2253
  rli->ignore_log_space_limit= 0;
2254
  pthread_mutex_unlock(&rli->log_space_lock);
2255
  rli->trans_retries= 0; // start from "no error"
2256
2257
  if (init_relay_log_pos(rli,
2258
                         rli->group_relay_log_name,
2259
                         rli->group_relay_log_pos,
2260
                         1 /*need data lock*/, &errmsg,
2261
                         1 /*look for a description_event*/))
2262
  {
236.1.44 by Monty Taylor
Added en_US translation file.
2263
    sql_print_error(_("Error initializing relay log position: %s"),
1 by brian
clean slate
2264
                    errmsg);
2265
    goto err;
2266
  }
2267
  THD_CHECK_SENTRY(thd);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2268
  assert(rli->event_relay_log_pos >= BIN_LOG_HEADER_SIZE);
2269
  /*
2270
    Wonder if this is correct. I (Guilhem) wonder if my_b_tell() returns the
2271
    correct position when it's called just after my_b_seek() (the questionable
2272
    stuff is those "seek is done on next read" comments in the my_b_seek()
2273
    source code).
2274
    The crude reality is that this assertion randomly fails whereas
2275
    replication seems to work fine. And there is no easy explanation why it
2276
    fails (as we my_b_seek(rli->event_relay_log_pos) at the very end of
2277
    init_relay_log_pos() called above). Maybe the assertion would be
2278
    meaningful if we held rli->data_lock between the my_b_seek() and the
2279
    assert().
2280
  */
2281
  assert(my_b_tell(rli->cur_log) == rli->event_relay_log_pos);
2282
  assert(rli->sql_thd == thd);
1 by brian
clean slate
2283
2284
  if (global_system_variables.log_warnings)
236.1.44 by Monty Taylor
Added en_US translation file.
2285
    sql_print_information(_("Slave SQL thread initialized, "
2286
                            "starting replication in log '%s' at "
2287
                            "position %s, relay log '%s' position: %s"),
2288
                            RPL_LOG_NAME,
2289
                          llstr(rli->group_master_log_pos,llbuff),
2290
                          rli->group_relay_log_name,
2291
                          llstr(rli->group_relay_log_pos,llbuff1));
1 by brian
clean slate
2292
2293
  /* execute init_slave variable */
2294
  if (sys_init_slave.value_length)
2295
  {
2296
    execute_init_command(thd, &sys_init_slave, &LOCK_sys_init_slave);
2297
    if (thd->is_slave_error)
2298
    {
236.1.44 by Monty Taylor
Added en_US translation file.
2299
      sql_print_error(_("Slave SQL thread aborted. "
2300
                        "Can't execute init_slave query"));
1 by brian
clean slate
2301
      goto err;
2302
    }
2303
  }
2304
2305
  /*
2306
    First check until condition - probably there is nothing to execute. We
2307
    do not want to wait for next event in this case.
2308
  */
2309
  pthread_mutex_lock(&rli->data_lock);
2310
  if (rli->until_condition != Relay_log_info::UNTIL_NONE &&
2311
      rli->is_until_satisfied(rli->group_master_log_pos))
2312
  {
2313
    char buf[22];
236.1.44 by Monty Taylor
Added en_US translation file.
2314
    sql_print_information(_("Slave SQL thread stopped because it reached its"
2315
                            " UNTIL position %s"), llstr(rli->until_pos(), buf));
1 by brian
clean slate
2316
    pthread_mutex_unlock(&rli->data_lock);
2317
    goto err;
2318
  }
2319
  pthread_mutex_unlock(&rli->data_lock);
2320
2321
  /* Read queries from the IO/THREAD until this thread is killed */
2322
2323
  while (!sql_slave_killed(thd,rli))
2324
  {
236.1.44 by Monty Taylor
Added en_US translation file.
2325
    thd_proc_info(thd, _("Reading event from the relay log"));
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2326
    assert(rli->sql_thd == thd);
1 by brian
clean slate
2327
    THD_CHECK_SENTRY(thd);
2328
    if (exec_relay_log_event(thd,rli))
2329
    {
2330
      // do not scare the user if SQL thread was simply killed or stopped
2331
      if (!sql_slave_killed(thd,rli))
2332
      {
2333
        /*
2334
          retrieve as much info as possible from the thd and, error
2335
          codes and warnings and print this to the error log as to
2336
          allow the user to locate the error
2337
        */
205 by Brian Aker
uint32 -> uin32_t
2338
        uint32_t const last_errno= rli->last_error().number;
1 by brian
clean slate
2339
2340
        if (thd->is_error())
2341
        {
2342
          char const *const errmsg= thd->main_da.message();
2343
2344
          if (last_errno == 0)
2345
          {
2346
            rli->report(ERROR_LEVEL, thd->main_da.sql_errno(), errmsg);
2347
          }
2348
          else if (last_errno != thd->main_da.sql_errno())
2349
          {
236.1.44 by Monty Taylor
Added en_US translation file.
2350
            sql_print_error(_("Slave (additional info): %s Error_code: %d"),
1 by brian
clean slate
2351
                            errmsg, thd->main_da.sql_errno());
2352
          }
2353
        }
2354
2355
        /* Print any warnings issued */
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
2356
        List_iterator_fast<DRIZZLE_ERROR> it(thd->warn_list);
2357
        DRIZZLE_ERROR *err;
1 by brian
clean slate
2358
        /*
2359
          Added controlled slave thread cancel for replication
2360
          of user-defined variables.
2361
        */
2362
        bool udf_error = false;
2363
        while ((err= it++))
2364
        {
2365
          if (err->code == ER_CANT_OPEN_LIBRARY)
2366
            udf_error = true;
236.1.44 by Monty Taylor
Added en_US translation file.
2367
          sql_print_warning(_("Slave: %s Error_code: %d"),err->msg, err->code);
1 by brian
clean slate
2368
        }
2369
        if (udf_error)
236.1.44 by Monty Taylor
Added en_US translation file.
2370
          sql_print_error(_("Error loading user-defined library, slave SQL "
2371
                            "thread aborted. Install the missing library, "
2372
                            "and restart the slave SQL thread with "
2373
                            "\"SLAVE START\". We stopped at log '%s' "
2374
                            "position %s"),
2375
                          RPL_LOG_NAME, llstr(rli->group_master_log_pos,
1 by brian
clean slate
2376
            llbuff));
2377
        else
236.1.44 by Monty Taylor
Added en_US translation file.
2378
          sql_print_error(_("Error running query, slave SQL thread aborted. "
2379
                            "Fix the problem, and restart "
2380
                            "the slave SQL thread with \"SLAVE START\". "
2381
                            "We stopped at log '%s' position %s"),
2382
                          RPL_LOG_NAME,
2383
                          llstr(rli->group_master_log_pos, llbuff));
1 by brian
clean slate
2384
      }
2385
      goto err;
2386
    }
2387
  }
2388
2389
  /* Thread stopped. Print the current replication position to the log */
236.1.44 by Monty Taylor
Added en_US translation file.
2390
  sql_print_information(_("Slave SQL thread exiting, replication stopped in "
2391
                          "log '%s' at position %s"),
2392
                        RPL_LOG_NAME,
2393
                        llstr(rli->group_master_log_pos,llbuff));
1 by brian
clean slate
2394
2395
 err:
2396
2397
  /*
2398
    Some events set some playgrounds, which won't be cleared because thread
2399
    stops. Stopping of this thread may not be known to these events ("stop"
2400
    request is detected only by the present function, not by events), so we
2401
    must "proactively" clear playgrounds:
2402
  */
2403
  rli->cleanup_context(thd, 1);
398.1.6 by Monty Taylor
Removed __alpha__ references.
2404
  pthread_mutex_lock(&LOCK_thread_count);
1 by brian
clean slate
2405
  /*
2406
    Some extra safety, which should not been needed (normally, event deletion
2407
    should already have done these assignments (each event which sets these
2408
    variables is supposed to set them to 0 before terminating)).
2409
  */
2410
  thd->query= thd->db= thd->catalog= 0;
2411
  thd->query_length= thd->db_length= 0;
398.1.6 by Monty Taylor
Removed __alpha__ references.
2412
  pthread_mutex_unlock(&LOCK_thread_count);
1 by brian
clean slate
2413
  thd_proc_info(thd, "Waiting for slave mutex on exit");
2414
  pthread_mutex_lock(&rli->run_lock);
2415
  /* We need data_lock, at least to wake up any waiting master_pos_wait() */
2416
  pthread_mutex_lock(&rli->data_lock);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2417
  assert(rli->slave_running == 1); // tracking buffer overrun
1 by brian
clean slate
2418
  /* When master_pos_wait() wakes up it will check this and terminate */
2419
  rli->slave_running= 0;
2420
  /* Forget the relay log's format */
2421
  delete rli->relay_log.description_event_for_exec;
2422
  rli->relay_log.description_event_for_exec= 0;
2423
  /* Wake up master_pos_wait() */
2424
  pthread_mutex_unlock(&rli->data_lock);
2425
  pthread_cond_broadcast(&rli->data_cond);
2426
  rli->ignore_log_space_limit= 0; /* don't need any lock */
2427
  rli->save_temporary_tables = thd->temporary_tables;
2428
2429
  /*
2430
    TODO: see if we can do this conditionally in next_event() instead
2431
    to avoid unneeded position re-init
2432
  */
2433
  thd->temporary_tables = 0; // remove tempation from destructor to close them
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2434
  assert(thd->net.buff != 0);
1 by brian
clean slate
2435
  net_end(&thd->net); // destructor will not free it, because we are weird
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2436
  assert(rli->sql_thd == thd);
1 by brian
clean slate
2437
  THD_CHECK_SENTRY(thd);
2438
  rli->sql_thd= 0;
2439
  pthread_mutex_lock(&LOCK_thread_count);
2440
  THD_CHECK_SENTRY(thd);
2441
  delete thd;
2442
  pthread_mutex_unlock(&LOCK_thread_count);
2443
 /*
2444
  Note: the order of the broadcast and unlock calls below (first broadcast, then unlock)
2445
  is important. Otherwise a killer_thread can execute between the calls and
2446
  delete the mi structure leading to a crash! (see BUG#25306 for details)
2447
 */ 
2448
  pthread_cond_broadcast(&rli->stop_cond);
2449
  pthread_mutex_unlock(&rli->run_lock);  // tell the world we are done
2450
  
2451
  my_thread_end();
2452
  pthread_exit(0);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2453
  return(0);                               // Can't return anything here
1 by brian
clean slate
2454
}
2455
2456
2457
/*
2458
  process_io_create_file()
2459
*/
2460
130 by Brian Aker
ulong cleanup
2461
static int32_t process_io_create_file(Master_info* mi, Create_file_log_event* cev)
1 by brian
clean slate
2462
{
130 by Brian Aker
ulong cleanup
2463
  int32_t error = 1;
2464
  uint32_t num_bytes;
1 by brian
clean slate
2465
  bool cev_not_written;
2466
  THD *thd = mi->io_thd;
206.3.1 by Patrick Galbraith
Most everything working with client rename
2467
  NET *net = &mi->drizzle->net;
1 by brian
clean slate
2468
2469
  if (unlikely(!cev->is_valid()))
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2470
    return(1);
1 by brian
clean slate
2471
2472
  if (!rpl_filter->db_ok(cev->db))
2473
  {
2474
    skip_load_data_infile(net);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2475
    return(0);
1 by brian
clean slate
2476
  }
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2477
  assert(cev->inited_from_old);
1 by brian
clean slate
2478
  thd->file_id = cev->file_id = mi->file_id++;
2479
  thd->server_id = cev->server_id;
2480
  cev_not_written = 1;
2481
2482
  if (unlikely(net_request_file(net,cev->fname)))
2483
  {
236.1.44 by Monty Taylor
Added en_US translation file.
2484
    sql_print_error(_("Slave I/O: failed requesting download of '%s'"),
1 by brian
clean slate
2485
                    cev->fname);
2486
    goto err;
2487
  }
2488
2489
  /*
2490
    This dummy block is so we could instantiate Append_block_log_event
2491
    once and then modify it slightly instead of doing it multiple times
2492
    in the loop
2493
  */
2494
  {
2495
    Append_block_log_event aev(thd,0,0,0,0);
2496
2497
    for (;;)
2498
    {
2499
      if (unlikely((num_bytes=my_net_read(net)) == packet_error))
2500
      {
236.1.44 by Monty Taylor
Added en_US translation file.
2501
        sql_print_error(_("Network read error downloading '%s' from master"),
1 by brian
clean slate
2502
                        cev->fname);
2503
        goto err;
2504
      }
2505
      if (unlikely(!num_bytes)) /* eof */
2506
      {
2507
	/* 3.23 master wants it */
481 by Brian Aker
Remove all of uchar.
2508
        net_write_command(net, 0, (unsigned char*) "", 0, (unsigned char*) "", 0);
1 by brian
clean slate
2509
        /*
2510
          If we wrote Create_file_log_event, then we need to write
2511
          Execute_load_log_event. If we did not write Create_file_log_event,
2512
          then this is an empty file and we can just do as if the LOAD DATA
2513
          INFILE had not existed, i.e. write nothing.
2514
        */
2515
        if (unlikely(cev_not_written))
2516
          break;
2517
        Execute_load_log_event xev(thd,0,0);
2518
        xev.log_pos = cev->log_pos;
2519
        if (unlikely(mi->rli.relay_log.append(&xev)))
2520
        {
2521
          mi->report(ERROR_LEVEL, ER_SLAVE_RELAY_LOG_WRITE_FAILURE,
2522
                     ER(ER_SLAVE_RELAY_LOG_WRITE_FAILURE),
261.3.4 by Monty Taylor
Added three more files worth of stuff.
2523
                     _("error writing Exec_load event to relay log"));
1 by brian
clean slate
2524
          goto err;
2525
        }
2526
        mi->rli.relay_log.harvest_bytes_written(&mi->rli.log_space_total);
2527
        break;
2528
      }
2529
      if (unlikely(cev_not_written))
2530
      {
2531
        cev->block = net->read_pos;
2532
        cev->block_len = num_bytes;
2533
        if (unlikely(mi->rli.relay_log.append(cev)))
2534
        {
2535
          mi->report(ERROR_LEVEL, ER_SLAVE_RELAY_LOG_WRITE_FAILURE,
2536
                     ER(ER_SLAVE_RELAY_LOG_WRITE_FAILURE),
261.3.4 by Monty Taylor
Added three more files worth of stuff.
2537
                     _("error writing Create_file event to relay log"));
1 by brian
clean slate
2538
          goto err;
2539
        }
2540
        cev_not_written=0;
2541
        mi->rli.relay_log.harvest_bytes_written(&mi->rli.log_space_total);
2542
      }
2543
      else
2544
      {
2545
        aev.block = net->read_pos;
2546
        aev.block_len = num_bytes;
2547
        aev.log_pos = cev->log_pos;
2548
        if (unlikely(mi->rli.relay_log.append(&aev)))
2549
        {
2550
          mi->report(ERROR_LEVEL, ER_SLAVE_RELAY_LOG_WRITE_FAILURE,
2551
                     ER(ER_SLAVE_RELAY_LOG_WRITE_FAILURE),
261.3.4 by Monty Taylor
Added three more files worth of stuff.
2552
                     _("error writing Append_block event to relay log"));
1 by brian
clean slate
2553
          goto err;
2554
        }
2555
        mi->rli.relay_log.harvest_bytes_written(&mi->rli.log_space_total) ;
2556
      }
2557
    }
2558
  }
2559
  error=0;
2560
err:
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2561
  return(error);
1 by brian
clean slate
2562
}
2563
2564
2565
/*
2566
  Start using a new binary log on the master
2567
2568
  SYNOPSIS
2569
    process_io_rotate()
2570
    mi                  master_info for the slave
2571
    rev                 The rotate log event read from the binary log
2572
2573
  DESCRIPTION
2574
    Updates the master info with the place in the next binary
2575
    log where we should start reading.
2576
    Rotate the relay log to avoid mixed-format relay logs.
2577
2578
  NOTES
2579
    We assume we already locked mi->data_lock
2580
2581
  RETURN VALUES
2582
    0           ok
2583
    1           Log event is illegal
2584
2585
*/
2586
130 by Brian Aker
ulong cleanup
2587
static int32_t process_io_rotate(Master_info *mi, Rotate_log_event *rev)
1 by brian
clean slate
2588
{
2589
  safe_mutex_assert_owner(&mi->data_lock);
2590
2591
  if (unlikely(!rev->is_valid()))
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2592
    return(1);
1 by brian
clean slate
2593
2594
  /* Safe copy as 'rev' has been "sanitized" in Rotate_log_event's ctor */
2595
  memcpy(mi->master_log_name, rev->new_log_ident, rev->ident_len+1);
2596
  mi->master_log_pos= rev->pos;
2597
  /*
2598
    If we do not do this, we will be getting the first
2599
    rotate event forever, so we need to not disconnect after one.
2600
  */
2601
  if (disconnect_slave_event_count)
2602
    mi->events_till_disconnect++;
2603
2604
  /*
2605
    If description_event_for_queue is format <4, there is conversion in the
2606
    relay log to the slave's format (4). And Rotate can mean upgrade or
2607
    nothing. If upgrade, it's to 5.0 or newer, so we will get a Format_desc, so
2608
    no need to reset description_event_for_queue now. And if it's nothing (same
2609
    master version as before), no need (still using the slave's format).
2610
  */
2611
  if (mi->rli.relay_log.description_event_for_queue->binlog_version >= 4)
2612
  {
2613
    delete mi->rli.relay_log.description_event_for_queue;
206.3.1 by Patrick Galbraith
Most everything working with client rename
2614
    /* start from format 3 (DRIZZLE 4.0) again */
1 by brian
clean slate
2615
    mi->rli.relay_log.description_event_for_queue= new
2616
      Format_description_log_event(3);
2617
  }
2618
  /*
2619
    Rotate the relay log makes binlog format detection easier (at next slave
2620
    start or mysqlbinlog)
2621
  */
2622
  rotate_relay_log(mi); /* will take the right mutexes */
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2623
  return(0);
1 by brian
clean slate
2624
}
2625
2626
/*
2627
  Reads a 3.23 event and converts it to the slave's format. This code was
206.3.1 by Patrick Galbraith
Most everything working with client rename
2628
  copied from DRIZZLE 4.0.
1 by brian
clean slate
2629
*/
130 by Brian Aker
ulong cleanup
2630
static int32_t queue_binlog_ver_1_event(Master_info *mi, const char *buf,
2631
                           uint32_t event_len)
1 by brian
clean slate
2632
{
2633
  const char *errmsg = 0;
130 by Brian Aker
ulong cleanup
2634
  uint32_t inc_pos;
1 by brian
clean slate
2635
  bool ignore_event= 0;
2636
  char *tmp_buf = 0;
2637
  Relay_log_info *rli= &mi->rli;
2638
2639
  /*
2640
    If we get Load event, we need to pass a non-reusable buffer
2641
    to read_log_event, so we do a trick
2642
  */
2643
  if (buf[EVENT_TYPE_OFFSET] == LOAD_EVENT)
2644
  {
2645
    if (unlikely(!(tmp_buf=(char*)my_malloc(event_len+1,MYF(MY_WME)))))
2646
    {
2647
      mi->report(ERROR_LEVEL, ER_SLAVE_FATAL_ERROR,
261.3.4 by Monty Taylor
Added three more files worth of stuff.
2648
                 ER(ER_SLAVE_FATAL_ERROR), _("Memory allocation failed"));
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2649
      return(1);
1 by brian
clean slate
2650
    }
2651
    memcpy(tmp_buf,buf,event_len);
2652
    /*
2653
      Create_file constructor wants a 0 as last char of buffer, this 0 will
2654
      serve as the string-termination char for the file's name (which is at the
2655
      end of the buffer)
2656
      We must increment event_len, otherwise the event constructor will not see
2657
      this end 0, which leads to segfault.
2658
    */
2659
    tmp_buf[event_len++]=0;
2660
    int4store(tmp_buf+EVENT_LEN_OFFSET, event_len);
2661
    buf = (const char*)tmp_buf;
2662
  }
2663
  /*
2664
    This will transform LOAD_EVENT into CREATE_FILE_EVENT, ask the master to
2665
    send the loaded file, and write it to the relay log in the form of
2666
    Append_block/Exec_load (the SQL thread needs the data, as that thread is not
2667
    connected to the master).
2668
  */
2669
  Log_event *ev = Log_event::read_log_event(buf,event_len, &errmsg,
2670
                                            mi->rli.relay_log.description_event_for_queue);
2671
  if (unlikely(!ev))
2672
  {
236.1.44 by Monty Taylor
Added en_US translation file.
2673
    sql_print_error(_("Read invalid event from master: '%s', "
2674
                      "master could be corrupt but a more likely cause "
2675
                      "of this is a bug"),
1 by brian
clean slate
2676
                    errmsg);
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
2677
    free((char*) tmp_buf);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2678
    return(1);
1 by brian
clean slate
2679
  }
2680
2681
  pthread_mutex_lock(&mi->data_lock);
2682
  ev->log_pos= mi->master_log_pos; /* 3.23 events don't contain log_pos */
2683
  switch (ev->get_type_code()) {
2684
  case STOP_EVENT:
2685
    ignore_event= 1;
2686
    inc_pos= event_len;
2687
    break;
2688
  case ROTATE_EVENT:
2689
    if (unlikely(process_io_rotate(mi,(Rotate_log_event*)ev)))
2690
    {
2691
      delete ev;
2692
      pthread_mutex_unlock(&mi->data_lock);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2693
      return(1);
1 by brian
clean slate
2694
    }
2695
    inc_pos= 0;
2696
    break;
2697
  case CREATE_FILE_EVENT:
2698
    /*
2699
      Yes it's possible to have CREATE_FILE_EVENT here, even if we're in
2700
      queue_old_event() which is for 3.23 events which don't comprise
2701
      CREATE_FILE_EVENT. This is because read_log_event() above has just
2702
      transformed LOAD_EVENT into CREATE_FILE_EVENT.
2703
    */
2704
  {
2705
    /* We come here when and only when tmp_buf != 0 */
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2706
    assert(tmp_buf != 0);
1 by brian
clean slate
2707
    inc_pos=event_len;
2708
    ev->log_pos+= inc_pos;
130 by Brian Aker
ulong cleanup
2709
    int32_t error = process_io_create_file(mi,(Create_file_log_event*)ev);
1 by brian
clean slate
2710
    delete ev;
2711
    mi->master_log_pos += inc_pos;
2712
    pthread_mutex_unlock(&mi->data_lock);
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
2713
    free((char*)tmp_buf);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2714
    return(error);
1 by brian
clean slate
2715
  }
2716
  default:
2717
    inc_pos= event_len;
2718
    break;
2719
  }
2720
  if (likely(!ignore_event))
2721
  {
2722
    if (ev->log_pos)
2723
      /*
2724
         Don't do it for fake Rotate events (see comment in
2725
      Log_event::Log_event(const char* buf...) in log_event.cc).
2726
      */
2727
      ev->log_pos+= event_len; /* make log_pos be the pos of the end of the event */
2728
    if (unlikely(rli->relay_log.append(ev)))
2729
    {
2730
      delete ev;
2731
      pthread_mutex_unlock(&mi->data_lock);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2732
      return(1);
1 by brian
clean slate
2733
    }
2734
    rli->relay_log.harvest_bytes_written(&rli->log_space_total);
2735
  }
2736
  delete ev;
2737
  mi->master_log_pos+= inc_pos;
2738
  pthread_mutex_unlock(&mi->data_lock);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2739
  return(0);
1 by brian
clean slate
2740
}
2741
2742
/*
2743
  Reads a 4.0 event and converts it to the slave's format. This code was copied
2744
  from queue_binlog_ver_1_event(), with some affordable simplifications.
2745
*/
130 by Brian Aker
ulong cleanup
2746
static int32_t queue_binlog_ver_3_event(Master_info *mi, const char *buf,
2747
                           uint32_t event_len)
1 by brian
clean slate
2748
{
2749
  const char *errmsg = 0;
130 by Brian Aker
ulong cleanup
2750
  uint32_t inc_pos;
1 by brian
clean slate
2751
  char *tmp_buf = 0;
2752
  Relay_log_info *rli= &mi->rli;
2753
2754
  /* read_log_event() will adjust log_pos to be end_log_pos */
2755
  Log_event *ev = Log_event::read_log_event(buf,event_len, &errmsg,
2756
                                            mi->rli.relay_log.description_event_for_queue);
2757
  if (unlikely(!ev))
2758
  {
236.1.44 by Monty Taylor
Added en_US translation file.
2759
    sql_print_error(_("Read invalid event from master: '%s', "
2760
                      "master could be corrupt but a more likely cause of "
2761
                      "this is a bug"),
1 by brian
clean slate
2762
                    errmsg);
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
2763
    free((char*) tmp_buf);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2764
    return(1);
1 by brian
clean slate
2765
  }
2766
  pthread_mutex_lock(&mi->data_lock);
2767
  switch (ev->get_type_code()) {
2768
  case STOP_EVENT:
2769
    goto err;
2770
  case ROTATE_EVENT:
2771
    if (unlikely(process_io_rotate(mi,(Rotate_log_event*)ev)))
2772
    {
2773
      delete ev;
2774
      pthread_mutex_unlock(&mi->data_lock);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2775
      return(1);
1 by brian
clean slate
2776
    }
2777
    inc_pos= 0;
2778
    break;
2779
  default:
2780
    inc_pos= event_len;
2781
    break;
2782
  }
2783
  if (unlikely(rli->relay_log.append(ev)))
2784
  {
2785
    delete ev;
2786
    pthread_mutex_unlock(&mi->data_lock);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2787
    return(1);
1 by brian
clean slate
2788
  }
2789
  rli->relay_log.harvest_bytes_written(&rli->log_space_total);
2790
  delete ev;
2791
  mi->master_log_pos+= inc_pos;
2792
err:
2793
  pthread_mutex_unlock(&mi->data_lock);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2794
  return(0);
1 by brian
clean slate
2795
}
2796
2797
/*
2798
  queue_old_event()
2799
2800
  Writes a 3.23 or 4.0 event to the relay log, after converting it to the 5.0
2801
  (exactly, slave's) format. To do the conversion, we create a 5.0 event from
2802
  the 3.23/4.0 bytes, then write this event to the relay log.
2803
2804
  TODO:
2805
    Test this code before release - it has to be tested on a separate
2806
    setup with 3.23 master or 4.0 master
2807
*/
2808
130 by Brian Aker
ulong cleanup
2809
static int32_t queue_old_event(Master_info *mi, const char *buf,
2810
                           uint32_t event_len)
1 by brian
clean slate
2811
{
2812
  switch (mi->rli.relay_log.description_event_for_queue->binlog_version)
2813
  {
2814
  case 1:
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2815
      return(queue_binlog_ver_1_event(mi,buf,event_len));
1 by brian
clean slate
2816
  case 3:
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2817
      return(queue_binlog_ver_3_event(mi,buf,event_len));
1 by brian
clean slate
2818
  default: /* unsupported format; eg version 2 */
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2819
    return(1);
1 by brian
clean slate
2820
  }
2821
}
2822
2823
/*
2824
  queue_event()
2825
2826
  If the event is 3.23/4.0, passes it to queue_old_event() which will convert
2827
  it. Otherwise, writes a 5.0 (or newer) event to the relay log. Then there is
2828
  no format conversion, it's pure read/write of bytes.
2829
  So a 5.0.0 slave's relay log can contain events in the slave's format or in
2830
  any >=5.0.0 format.
2831
*/
2832
130 by Brian Aker
ulong cleanup
2833
static int32_t queue_event(Master_info* mi,const char* buf, uint32_t event_len)
1 by brian
clean slate
2834
{
130 by Brian Aker
ulong cleanup
2835
  int32_t error= 0;
1 by brian
clean slate
2836
  String error_msg;
236.2.2 by rbradfor
Using correct coding standards for variable initialization
2837
  uint32_t inc_pos= 0;
1 by brian
clean slate
2838
  Relay_log_info *rli= &mi->rli;
2839
  pthread_mutex_t *log_lock= rli->relay_log.get_log_lock();
2840
2841
2842
  if (mi->rli.relay_log.description_event_for_queue->binlog_version<4 &&
2843
      buf[EVENT_TYPE_OFFSET] != FORMAT_DESCRIPTION_EVENT /* a way to escape */)
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2844
    return(queue_old_event(mi,buf,event_len));
1 by brian
clean slate
2845
2846
  pthread_mutex_lock(&mi->data_lock);
2847
2848
  switch (buf[EVENT_TYPE_OFFSET]) {
2849
  case STOP_EVENT:
2850
    /*
2851
      We needn't write this event to the relay log. Indeed, it just indicates a
2852
      master server shutdown. The only thing this does is cleaning. But
2853
      cleaning is already done on a per-master-thread basis (as the master
2854
      server is shutting down cleanly, it has written all DROP TEMPORARY TABLE
2855
      prepared statements' deletion are TODO only when we binlog prep stmts).
2856
2857
      We don't even increment mi->master_log_pos, because we may be just after
2858
      a Rotate event. Btw, in a few milliseconds we are going to have a Start
2859
      event from the next binlog (unless the master is presently running
2860
      without --log-bin).
2861
    */
2862
    goto err;
2863
  case ROTATE_EVENT:
2864
  {
2865
    Rotate_log_event rev(buf,event_len,mi->rli.relay_log.description_event_for_queue);
2866
    if (unlikely(process_io_rotate(mi,&rev)))
2867
    {
2868
      error= ER_SLAVE_RELAY_LOG_WRITE_FAILURE;
2869
      goto err;
2870
    }
2871
    /*
2872
      Now the I/O thread has just changed its mi->master_log_name, so
2873
      incrementing mi->master_log_pos is nonsense.
2874
    */
2875
    inc_pos= 0;
2876
    break;
2877
  }
2878
  case FORMAT_DESCRIPTION_EVENT:
2879
  {
2880
    /*
2881
      Create an event, and save it (when we rotate the relay log, we will have
2882
      to write this event again).
2883
    */
2884
    /*
2885
      We are the only thread which reads/writes description_event_for_queue.
2886
      The relay_log struct does not move (though some members of it can
2887
      change), so we needn't any lock (no rli->data_lock, no log lock).
2888
    */
2889
    Format_description_log_event* tmp;
2890
    const char* errmsg;
2891
    if (!(tmp= (Format_description_log_event*)
2892
          Log_event::read_log_event(buf, event_len, &errmsg,
2893
                                    mi->rli.relay_log.description_event_for_queue)))
2894
    {
2895
      error= ER_SLAVE_RELAY_LOG_WRITE_FAILURE;
2896
      goto err;
2897
    }
2898
    delete mi->rli.relay_log.description_event_for_queue;
2899
    mi->rli.relay_log.description_event_for_queue= tmp;
2900
    /*
2901
       Though this does some conversion to the slave's format, this will
2902
       preserve the master's binlog format version, and number of event types.
2903
    */
2904
    /*
2905
       If the event was not requested by the slave (the slave did not ask for
2906
       it), i.e. has end_log_pos=0, we do not increment mi->master_log_pos
2907
    */
2908
    inc_pos= uint4korr(buf+LOG_POS_OFFSET) ? event_len : 0;
2909
  }
2910
  break;
2911
2912
  case HEARTBEAT_LOG_EVENT:
2913
  {
2914
    /*
2915
      HB (heartbeat) cannot come before RL (Relay)
2916
    */
2917
    char  llbuf[22];
2918
    Heartbeat_log_event hb(buf, event_len, mi->rli.relay_log.description_event_for_queue);
2919
    if (!hb.is_valid())
2920
    {
2921
      error= ER_SLAVE_HEARTBEAT_FAILURE;
2922
      error_msg.append(STRING_WITH_LEN("inconsistent heartbeat event content;"));
2923
      error_msg.append(STRING_WITH_LEN("the event's data: log_file_name "));
130 by Brian Aker
ulong cleanup
2924
      error_msg.append(hb.get_log_ident(), (uint32_t) strlen(hb.get_log_ident()));
1 by brian
clean slate
2925
      error_msg.append(STRING_WITH_LEN(" log_pos "));
2926
      llstr(hb.log_pos, llbuf);
2927
      error_msg.append(llbuf, strlen(llbuf));
2928
      goto err;
2929
    }
2930
    mi->received_heartbeats++;
2931
    /* 
2932
       compare local and event's versions of log_file, log_pos.
2933
       
2934
       Heartbeat is sent only after an event corresponding to the corrdinates
2935
       the heartbeat carries.
2936
       Slave can not have a difference in coordinates except in the only
2937
       special case when mi->master_log_name, master_log_pos have never
2938
       been updated by Rotate event i.e when slave does not have any history
2939
       with the master (and thereafter mi->master_log_pos is NULL).
2940
2941
       TODO: handling `when' for SHOW SLAVE STATUS' snds behind
2942
    */
2943
    if ((memcmp(mi->master_log_name, hb.get_log_ident(), hb.get_ident_len())
2944
         && mi->master_log_name != NULL)
2945
        || mi->master_log_pos != hb.log_pos)
2946
    {
2947
      /* missed events of heartbeat from the past */
2948
      error= ER_SLAVE_HEARTBEAT_FAILURE;
2949
      error_msg.append(STRING_WITH_LEN("heartbeat is not compatible with local info;"));
2950
      error_msg.append(STRING_WITH_LEN("the event's data: log_file_name "));
130 by Brian Aker
ulong cleanup
2951
      error_msg.append(hb.get_log_ident(), (uint32_t) strlen(hb.get_log_ident()));
1 by brian
clean slate
2952
      error_msg.append(STRING_WITH_LEN(" log_pos "));
2953
      llstr(hb.log_pos, llbuf);
2954
      error_msg.append(llbuf, strlen(llbuf));
2955
      goto err;
2956
    }
2957
    goto skip_relay_logging;
2958
  }
2959
  break;
2960
    
2961
  default:
2962
    inc_pos= event_len;
2963
    break;
2964
  }
2965
2966
  /*
2967
     If this event is originating from this server, don't queue it.
2968
     We don't check this for 3.23 events because it's simpler like this; 3.23
2969
     will be filtered anyway by the SQL slave thread which also tests the
2970
     server id (we must also keep this test in the SQL thread, in case somebody
2971
     upgrades a 4.0 slave which has a not-filtered relay log).
2972
2973
     ANY event coming from ourselves can be ignored: it is obvious for queries;
2974
     for STOP_EVENT/ROTATE_EVENT/START_EVENT: these cannot come from ourselves
2975
     (--log-slave-updates would not log that) unless this slave is also its
2976
     direct master (an unsupported, useless setup!).
2977
  */
2978
2979
  pthread_mutex_lock(log_lock);
2980
2981
  if ((uint4korr(buf + SERVER_ID_OFFSET) == ::server_id) &&
2982
      !mi->rli.replicate_same_server_id)
2983
  {
2984
    /*
2985
      Do not write it to the relay log.
2986
      a) We still want to increment mi->master_log_pos, so that we won't
2987
      re-read this event from the master if the slave IO thread is now
2988
      stopped/restarted (more efficient if the events we are ignoring are big
2989
      LOAD DATA INFILE).
2990
      b) We want to record that we are skipping events, for the information of
2991
      the slave SQL thread, otherwise that thread may let
2992
      rli->group_relay_log_pos stay too small if the last binlog's event is
2993
      ignored.
2994
      But events which were generated by this slave and which do not exist in
2995
      the master's binlog (i.e. Format_desc, Rotate & Stop) should not increment
2996
      mi->master_log_pos.
2997
    */
2998
    if (buf[EVENT_TYPE_OFFSET]!=FORMAT_DESCRIPTION_EVENT &&
2999
        buf[EVENT_TYPE_OFFSET]!=ROTATE_EVENT &&
3000
        buf[EVENT_TYPE_OFFSET]!=STOP_EVENT)
3001
    {
3002
      mi->master_log_pos+= inc_pos;
3003
      memcpy(rli->ign_master_log_name_end, mi->master_log_name, FN_REFLEN);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3004
      assert(rli->ign_master_log_name_end[0]);
1 by brian
clean slate
3005
      rli->ign_master_log_pos_end= mi->master_log_pos;
3006
    }
3007
    rli->relay_log.signal_update(); // the slave SQL thread needs to re-check
3008
  }
3009
  else
3010
  {
3011
    /* write the event to the relay log */
3012
    if (likely(!(rli->relay_log.appendv(buf,event_len,0))))
3013
    {
3014
      mi->master_log_pos+= inc_pos;
3015
      rli->relay_log.harvest_bytes_written(&rli->log_space_total);
3016
    }
3017
    else
3018
    {
3019
      error= ER_SLAVE_RELAY_LOG_WRITE_FAILURE;
3020
    }
3021
    rli->ign_master_log_name_end[0]= 0; // last event is not ignored
3022
  }
3023
  pthread_mutex_unlock(log_lock);
3024
3025
skip_relay_logging:
3026
  
3027
err:
3028
  pthread_mutex_unlock(&mi->data_lock);
3029
  if (error)
261.3.4 by Monty Taylor
Added three more files worth of stuff.
3030
    mi->report(ERROR_LEVEL, error, ER(error),
1 by brian
clean slate
3031
               (error == ER_SLAVE_RELAY_LOG_WRITE_FAILURE)?
261.3.4 by Monty Taylor
Added three more files worth of stuff.
3032
               _("could not queue event from master") :
1 by brian
clean slate
3033
               error_msg.ptr());
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3034
  return(error);
1 by brian
clean slate
3035
}
3036
3037
3038
void end_relay_log_info(Relay_log_info* rli)
3039
{
3040
  if (!rli->inited)
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3041
    return;
1 by brian
clean slate
3042
  if (rli->info_fd >= 0)
3043
  {
3044
    end_io_cache(&rli->info_file);
3045
    (void) my_close(rli->info_fd, MYF(MY_WME));
3046
    rli->info_fd = -1;
3047
  }
3048
  if (rli->cur_log_fd >= 0)
3049
  {
3050
    end_io_cache(&rli->cache_buf);
3051
    (void)my_close(rli->cur_log_fd, MYF(MY_WME));
3052
    rli->cur_log_fd = -1;
3053
  }
3054
  rli->inited = 0;
3055
  rli->relay_log.close(LOG_CLOSE_INDEX | LOG_CLOSE_STOP_EVENT);
3056
  rli->relay_log.harvest_bytes_written(&rli->log_space_total);
3057
  /*
3058
    Delete the slave's temporary tables from memory.
3059
    In the future there will be other actions than this, to ensure persistance
3060
    of slave's temp tables after shutdown.
3061
  */
3062
  rli->close_temporary_tables();
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3063
  return;
1 by brian
clean slate
3064
}
3065
3066
/*
3067
  Try to connect until successful or slave killed
3068
3069
  SYNPOSIS
3070
    safe_connect()
3071
    thd                 Thread handler for slave
206.3.1 by Patrick Galbraith
Most everything working with client rename
3072
    DRIZZLE               DRIZZLE connection handle
1 by brian
clean slate
3073
    mi                  Replication handle
3074
3075
  RETURN
3076
    0   ok
3077
    #   Error
3078
*/
3079
206.3.1 by Patrick Galbraith
Most everything working with client rename
3080
static int32_t safe_connect(THD* thd, DRIZZLE *drizzle, Master_info* mi)
1 by brian
clean slate
3081
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
3082
  return(connect_to_master(thd, drizzle, mi, 0, 0));
1 by brian
clean slate
3083
}
3084
3085
3086
/*
3087
  SYNPOSIS
3088
    connect_to_master()
3089
3090
  IMPLEMENTATION
3091
    Try to connect until successful or slave killed or we have retried
3092
    master_retry_count times
3093
*/
3094
206.3.1 by Patrick Galbraith
Most everything working with client rename
3095
static int32_t connect_to_master(THD* thd, DRIZZLE *drizzle, Master_info* mi,
390.1.5 by Monty Taylor
Moved more functions into drizzle.c as part of the split of code.
3096
                                 bool reconnect, bool suppress_warnings)
1 by brian
clean slate
3097
{
130 by Brian Aker
ulong cleanup
3098
  int32_t slave_was_killed;
3099
  int32_t last_errno= -2;                           // impossible error
3100
  uint32_t err_count=0;
1 by brian
clean slate
3101
  char llbuff[22];
3102
3103
  mi->events_till_disconnect = disconnect_slave_event_count;
130 by Brian Aker
ulong cleanup
3104
  uint32_t client_flag= CLIENT_REMEMBER_OPTIONS;
1 by brian
clean slate
3105
  if (opt_slave_compressed_protocol)
3106
    client_flag=CLIENT_COMPRESS;                /* We will use compression */
3107
206.3.1 by Patrick Galbraith
Most everything working with client rename
3108
  drizzle_options(drizzle, DRIZZLE_OPT_CONNECT_TIMEOUT, (char *) &slave_net_timeout);
3109
  drizzle_options(drizzle, DRIZZLE_OPT_READ_TIMEOUT, (char *) &slave_net_timeout);
1 by brian
clean slate
3110
3111
  while (!(slave_was_killed = io_slave_killed(thd,mi)) &&
206.3.1 by Patrick Galbraith
Most everything working with client rename
3112
         (reconnect ? drizzle_reconnect(drizzle) != 0 :
3113
          drizzle_connect(drizzle, mi->host, mi->user, mi->password, 0,
1 by brian
clean slate
3114
                             mi->port, 0, client_flag) == 0))
3115
  {
3116
    /* Don't repeat last error */
206.3.1 by Patrick Galbraith
Most everything working with client rename
3117
    if ((int32_t)drizzle_errno(drizzle) != last_errno)
1 by brian
clean slate
3118
    {
206.3.1 by Patrick Galbraith
Most everything working with client rename
3119
      last_errno=drizzle_errno(drizzle);
1 by brian
clean slate
3120
      suppress_warnings= 0;
3121
      mi->report(ERROR_LEVEL, last_errno,
261.3.4 by Monty Taylor
Added three more files worth of stuff.
3122
                 _("error %s to master '%s@%s:%d'"
3123
                   " - retry-time: %d  retries: %u"),
3124
                 (reconnect ? _("reconnecting") : _("connecting")),
1 by brian
clean slate
3125
                 mi->user, mi->host, mi->port,
3126
                 mi->connect_retry, master_retry_count);
3127
    }
3128
    /*
3129
      By default we try forever. The reason is that failure will trigger
3130
      master election, so if the user did not set master_retry_count we
3131
      do not want to have election triggered on the first failure to
3132
      connect
3133
    */
3134
    if (++err_count == master_retry_count)
3135
    {
3136
      slave_was_killed=1;
3137
      break;
3138
    }
3139
    safe_sleep(thd,mi->connect_retry,(CHECK_KILLED_FUNC)io_slave_killed,
3140
               (void*)mi);
3141
  }
3142
3143
  if (!slave_was_killed)
3144
  {
3145
    if (reconnect)
3146
    {
3147
      if (!suppress_warnings && global_system_variables.log_warnings)
236.1.44 by Monty Taylor
Added en_US translation file.
3148
        sql_print_information(_("Slave: connected to master '%s@%s:%d', "
3149
                                "replication resumed in log '%s' at "
3150
                                "position %s"), mi->user,
3151
                                mi->host, mi->port,
3152
                                IO_RPL_LOG_NAME,
3153
                                llstr(mi->master_log_pos,llbuff));
1 by brian
clean slate
3154
    }
3155
  }
206.3.1 by Patrick Galbraith
Most everything working with client rename
3156
  drizzle->reconnect= 1;
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3157
  return(slave_was_killed);
1 by brian
clean slate
3158
}
3159
3160
3161
/*
3162
  safe_reconnect()
3163
3164
  IMPLEMENTATION
3165
    Try to connect until successful or slave killed or we have retried
3166
    master_retry_count times
3167
*/
3168
206.3.1 by Patrick Galbraith
Most everything working with client rename
3169
static int32_t safe_reconnect(THD* thd, DRIZZLE *drizzle, Master_info* mi,
1 by brian
clean slate
3170
                          bool suppress_warnings)
3171
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
3172
  return(connect_to_master(thd, drizzle, mi, 1, suppress_warnings));
1 by brian
clean slate
3173
}
3174
3175
3176
/*
3177
  Store the file and position where the execute-slave thread are in the
3178
  relay log.
3179
3180
  SYNOPSIS
3181
    flush_relay_log_info()
3182
    rli                 Relay log information
3183
3184
  NOTES
3185
    - As this is only called by the slave thread, we don't need to
3186
      have a lock on this.
3187
    - If there is an active transaction, then we don't update the position
3188
      in the relay log.  This is to ensure that we re-execute statements
3189
      if we die in the middle of an transaction that was rolled back.
3190
    - As a transaction never spans binary logs, we don't have to handle the
3191
      case where we do a relay-log-rotation in the middle of the transaction.
3192
      If this would not be the case, we would have to ensure that we
3193
      don't delete the relay log file where the transaction started when
3194
      we switch to a new relay log file.
3195
3196
  TODO
3197
    - Change the log file information to a binary format to avoid calling
152 by Brian Aker
longlong replacement
3198
      int64_t2str.
1 by brian
clean slate
3199
3200
  RETURN VALUES
3201
    0   ok
3202
    1   write error
3203
*/
3204
3205
bool flush_relay_log_info(Relay_log_info* rli)
3206
{
3207
  bool error=0;
3208
3209
  if (unlikely(rli->no_storage))
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3210
    return(0);
1 by brian
clean slate
3211
3212
  IO_CACHE *file = &rli->info_file;
3213
  char buff[FN_REFLEN*2+22*2+4], *pos;
3214
3215
  my_b_seek(file, 0L);
411.1.1 by Brian Aker
Work on removing GNU specific calls.
3216
  pos=my_stpcpy(buff, rli->group_relay_log_name);
1 by brian
clean slate
3217
  *pos++='\n';
152 by Brian Aker
longlong replacement
3218
  pos=int64_t2str(rli->group_relay_log_pos, pos, 10);
1 by brian
clean slate
3219
  *pos++='\n';
411.1.1 by Brian Aker
Work on removing GNU specific calls.
3220
  pos=my_stpcpy(pos, rli->group_master_log_name);
1 by brian
clean slate
3221
  *pos++='\n';
152 by Brian Aker
longlong replacement
3222
  pos=int64_t2str(rli->group_master_log_pos, pos, 10);
1 by brian
clean slate
3223
  *pos='\n';
481 by Brian Aker
Remove all of uchar.
3224
  if (my_b_write(file, (unsigned char*) buff, (size_t) (pos-buff)+1))
1 by brian
clean slate
3225
    error=1;
3226
  if (flush_io_cache(file))
3227
    error=1;
3228
3229
  /* Flushing the relay log is done by the slave I/O thread */
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3230
  return(error);
1 by brian
clean slate
3231
}
3232
3233
3234
/*
3235
  Called when we notice that the current "hot" log got rotated under our feet.
3236
*/
3237
3238
static IO_CACHE *reopen_relay_log(Relay_log_info *rli, const char **errmsg)
3239
{
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3240
  assert(rli->cur_log != &rli->cache_buf);
3241
  assert(rli->cur_log_fd == -1);
1 by brian
clean slate
3242
3243
  IO_CACHE *cur_log = rli->cur_log=&rli->cache_buf;
3244
  if ((rli->cur_log_fd=open_binlog(cur_log,rli->event_relay_log_name,
3245
                                   errmsg)) <0)
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3246
    return(0);
1 by brian
clean slate
3247
  /*
3248
    We want to start exactly where we was before:
3249
    relay_log_pos       Current log pos
3250
    pending             Number of bytes already processed from the event
3251
  */
398.1.4 by Monty Taylor
Renamed max/min.
3252
  rli->event_relay_log_pos= cmax(rli->event_relay_log_pos, (uint64_t)BIN_LOG_HEADER_SIZE);
1 by brian
clean slate
3253
  my_b_seek(cur_log,rli->event_relay_log_pos);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3254
  return(cur_log);
1 by brian
clean slate
3255
}
3256
3257
3258
static Log_event* next_event(Relay_log_info* rli)
3259
{
3260
  Log_event* ev;
3261
  IO_CACHE* cur_log = rli->cur_log;
3262
  pthread_mutex_t *log_lock = rli->relay_log.get_log_lock();
3263
  const char* errmsg=0;
3264
  THD* thd = rli->sql_thd;
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3265
3266
  assert(thd != 0);
3267
1 by brian
clean slate
3268
  if (abort_slave_event_count && !rli->events_till_abort--)
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3269
    return(0);
1 by brian
clean slate
3270
3271
  /*
3272
    For most operations we need to protect rli members with data_lock,
3273
    so we assume calling function acquired this mutex for us and we will
3274
    hold it for the most of the loop below However, we will release it
3275
    whenever it is worth the hassle,  and in the cases when we go into a
3276
    pthread_cond_wait() with the non-data_lock mutex
3277
  */
3278
  safe_mutex_assert_owner(&rli->data_lock);
3279
3280
  while (!sql_slave_killed(thd,rli))
3281
  {
3282
    /*
3283
      We can have two kinds of log reading:
3284
      hot_log:
3285
        rli->cur_log points at the IO_CACHE of relay_log, which
3286
        is actively being updated by the I/O thread. We need to be careful
3287
        in this case and make sure that we are not looking at a stale log that
3288
        has already been rotated. If it has been, we reopen the log.
3289
3290
      The other case is much simpler:
3291
        We just have a read only log that nobody else will be updating.
3292
    */
3293
    bool hot_log;
3294
    if ((hot_log = (cur_log != &rli->cache_buf)))
3295
    {
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3296
      assert(rli->cur_log_fd == -1); // foreign descriptor
1 by brian
clean slate
3297
      pthread_mutex_lock(log_lock);
3298
3299
      /*
3300
        Reading xxx_file_id is safe because the log will only
3301
        be rotated when we hold relay_log.LOCK_log
3302
      */
3303
      if (rli->relay_log.get_open_count() != rli->cur_log_old_open_count)
3304
      {
3305
        // The master has switched to a new log file; Reopen the old log file
3306
        cur_log=reopen_relay_log(rli, &errmsg);
3307
        pthread_mutex_unlock(log_lock);
3308
        if (!cur_log)                           // No more log files
3309
          goto err;
3310
        hot_log=0;                              // Using old binary log
3311
      }
3312
    }
3313
    /* 
3314
      As there is no guarantee that the relay is open (for example, an I/O
3315
      error during a write by the slave I/O thread may have closed it), we
3316
      have to test it.
3317
    */
3318
    if (!my_b_inited(cur_log))
3319
      goto err;
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3320
    assert(my_b_tell(cur_log) >= BIN_LOG_HEADER_SIZE);
3321
    assert(my_b_tell(cur_log) == rli->event_relay_log_pos);
3322
1 by brian
clean slate
3323
    /*
3324
      Relay log is always in new format - if the master is 3.23, the
3325
      I/O thread will convert the format for us.
3326
      A problem: the description event may be in a previous relay log. So if
3327
      the slave has been shutdown meanwhile, we would have to look in old relay
3328
      logs, which may even have been deleted. So we need to write this
3329
      description event at the beginning of the relay log.
3330
      When the relay log is created when the I/O thread starts, easy: the
3331
      master will send the description event and we will queue it.
3332
      But if the relay log is created by new_file(): then the solution is:
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
3333
      DRIZZLE_BIN_LOG::open() will write the buffered description event.
1 by brian
clean slate
3334
    */
3335
    if ((ev=Log_event::read_log_event(cur_log,0,
3336
                                      rli->relay_log.description_event_for_exec)))
3337
3338
    {
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3339
      assert(thd==rli->sql_thd);
1 by brian
clean slate
3340
      /*
3341
        read it while we have a lock, to avoid a mutex lock in
3342
        inc_event_relay_log_pos()
3343
      */
3344
      rli->future_event_relay_log_pos= my_b_tell(cur_log);
3345
      if (hot_log)
3346
        pthread_mutex_unlock(log_lock);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3347
      return(ev);
1 by brian
clean slate
3348
    }
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3349
    assert(thd==rli->sql_thd);
1 by brian
clean slate
3350
    if (opt_reckless_slave)                     // For mysql-test
3351
      cur_log->error = 0;
3352
    if (cur_log->error < 0)
3353
    {
3354
      errmsg = "slave SQL thread aborted because of I/O error";
3355
      if (hot_log)
3356
        pthread_mutex_unlock(log_lock);
3357
      goto err;
3358
    }
3359
    if (!cur_log->error) /* EOF */
3360
    {
3361
      /*
3362
        On a hot log, EOF means that there are no more updates to
3363
        process and we must block until I/O thread adds some and
3364
        signals us to continue
3365
      */
3366
      if (hot_log)
3367
      {
3368
        /*
3369
          We say in Seconds_Behind_Master that we have "caught up". Note that
3370
          for example if network link is broken but I/O slave thread hasn't
3371
          noticed it (slave_net_timeout not elapsed), then we'll say "caught
3372
          up" whereas we're not really caught up. Fixing that would require
3373
          internally cutting timeout in smaller pieces in network read, no
3374
          thanks. Another example: SQL has caught up on I/O, now I/O has read
3375
          a new event and is queuing it; the false "0" will exist until SQL
3376
          finishes executing the new event; it will be look abnormal only if
3377
          the events have old timestamps (then you get "many", 0, "many").
3378
3379
          Transient phases like this can be fixed with implemeting
3380
          Heartbeat event which provides the slave the status of the
3381
          master at time the master does not have any new update to send.
3382
          Seconds_Behind_Master would be zero only when master has no
3383
          more updates in binlog for slave. The heartbeat can be sent
3384
          in a (small) fraction of slave_net_timeout. Until it's done
3385
          rli->last_master_timestamp is temporarely (for time of
3386
          waiting for the following event) reset whenever EOF is
3387
          reached.
3388
        */
3389
        time_t save_timestamp= rli->last_master_timestamp;
3390
        rli->last_master_timestamp= 0;
3391
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3392
        assert(rli->relay_log.get_open_count() ==
1 by brian
clean slate
3393
                    rli->cur_log_old_open_count);
3394
3395
        if (rli->ign_master_log_name_end[0])
3396
        {
3397
          /* We generate and return a Rotate, to make our positions advance */
3398
          ev= new Rotate_log_event(rli->ign_master_log_name_end,
3399
                                   0, rli->ign_master_log_pos_end,
3400
                                   Rotate_log_event::DUP_NAME);
3401
          rli->ign_master_log_name_end[0]= 0;
3402
          pthread_mutex_unlock(log_lock);
3403
          if (unlikely(!ev))
3404
          {
3405
            errmsg= "Slave SQL thread failed to create a Rotate event "
3406
              "(out of memory?), SHOW SLAVE STATUS may be inaccurate";
3407
            goto err;
3408
          }
3409
          ev->server_id= 0; // don't be ignored by slave SQL thread
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3410
          return(ev);
1 by brian
clean slate
3411
        }
3412
3413
        /*
3414
          We can, and should release data_lock while we are waiting for
3415
          update. If we do not, show slave status will block
3416
        */
3417
        pthread_mutex_unlock(&rli->data_lock);
3418
3419
        /*
3420
          Possible deadlock :
3421
          - the I/O thread has reached log_space_limit
3422
          - the SQL thread has read all relay logs, but cannot purge for some
3423
          reason:
3424
            * it has already purged all logs except the current one
3425
            * there are other logs than the current one but they're involved in
3426
            a transaction that finishes in the current one (or is not finished)
3427
          Solution :
3428
          Wake up the possibly waiting I/O thread, and set a boolean asking
3429
          the I/O thread to temporarily ignore the log_space_limit
3430
          constraint, because we do not want the I/O thread to block because of
3431
          space (it's ok if it blocks for any other reason (e.g. because the
3432
          master does not send anything). Then the I/O thread stops waiting
3433
          and reads more events.
3434
          The SQL thread decides when the I/O thread should take log_space_limit
3435
          into account again : ignore_log_space_limit is reset to 0
3436
          in purge_first_log (when the SQL thread purges the just-read relay
3437
          log), and also when the SQL thread starts. We should also reset
3438
          ignore_log_space_limit to 0 when the user does RESET SLAVE, but in
3439
          fact, no need as RESET SLAVE requires that the slave
3440
          be stopped, and the SQL thread sets ignore_log_space_limit to 0 when
3441
          it stops.
3442
        */
3443
        pthread_mutex_lock(&rli->log_space_lock);
3444
        // prevent the I/O thread from blocking next times
3445
        rli->ignore_log_space_limit= 1;
3446
        /*
3447
          If the I/O thread is blocked, unblock it.  Ok to broadcast
3448
          after unlock, because the mutex is only destroyed in
3449
          ~Relay_log_info(), i.e. when rli is destroyed, and rli will
3450
          not be destroyed before we exit the present function.
3451
        */
3452
        pthread_mutex_unlock(&rli->log_space_lock);
3453
        pthread_cond_broadcast(&rli->log_space_cond);
3454
        // Note that wait_for_update_relay_log unlocks lock_log !
3455
        rli->relay_log.wait_for_update_relay_log(rli->sql_thd);
3456
        // re-acquire data lock since we released it earlier
3457
        pthread_mutex_lock(&rli->data_lock);
3458
        rli->last_master_timestamp= save_timestamp;
3459
        continue;
3460
      }
3461
      /*
3462
        If the log was not hot, we need to move to the next log in
3463
        sequence. The next log could be hot or cold, we deal with both
3464
        cases separately after doing some common initialization
3465
      */
3466
      end_io_cache(cur_log);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3467
      assert(rli->cur_log_fd >= 0);
1 by brian
clean slate
3468
      my_close(rli->cur_log_fd, MYF(MY_WME));
3469
      rli->cur_log_fd = -1;
3470
3471
      if (relay_log_purge)
3472
      {
3473
        /*
3474
          purge_first_log will properly set up relay log coordinates in rli.
3475
          If the group's coordinates are equal to the event's coordinates
3476
          (i.e. the relay log was not rotated in the middle of a group),
3477
          we can purge this relay log too.
130 by Brian Aker
ulong cleanup
3478
          We do uint64_t and string comparisons, this may be slow but
1 by brian
clean slate
3479
          - purging the last relay log is nice (it can save 1GB of disk), so we
3480
          like to detect the case where we can do it, and given this,
3481
          - I see no better detection method
3482
          - purge_first_log is not called that often
3483
        */
3484
        if (rli->relay_log.purge_first_log
3485
            (rli,
3486
             rli->group_relay_log_pos == rli->event_relay_log_pos
3487
             && !strcmp(rli->group_relay_log_name,rli->event_relay_log_name)))
3488
        {
3489
          errmsg = "Error purging processed logs";
3490
          goto err;
3491
        }
3492
      }
3493
      else
3494
      {
3495
        /*
3496
          If hot_log is set, then we already have a lock on
3497
          LOCK_log.  If not, we have to get the lock.
3498
3499
          According to Sasha, the only time this code will ever be executed
3500
          is if we are recovering from a bug.
3501
        */
3502
        if (rli->relay_log.find_next_log(&rli->linfo, !hot_log))
3503
        {
3504
          errmsg = "error switching to the next log";
3505
          goto err;
3506
        }
3507
        rli->event_relay_log_pos = BIN_LOG_HEADER_SIZE;
3508
        strmake(rli->event_relay_log_name,rli->linfo.log_file_name,
3509
                sizeof(rli->event_relay_log_name)-1);
3510
        flush_relay_log_info(rli);
3511
      }
3512
3513
      /*
3514
        Now we want to open this next log. To know if it's a hot log (the one
3515
        being written by the I/O thread now) or a cold log, we can use
3516
        is_active(); if it is hot, we use the I/O cache; if it's cold we open
3517
        the file normally. But if is_active() reports that the log is hot, this
3518
        may change between the test and the consequence of the test. So we may
3519
        open the I/O cache whereas the log is now cold, which is nonsense.
3520
        To guard against this, we need to have LOCK_log.
3521
      */
3522
3523
      if (!hot_log) /* if hot_log, we already have this mutex */
3524
        pthread_mutex_lock(log_lock);
3525
      if (rli->relay_log.is_active(rli->linfo.log_file_name))
3526
      {
3527
#ifdef EXTRA_DEBUG
3528
        if (global_system_variables.log_warnings)
236.1.44 by Monty Taylor
Added en_US translation file.
3529
          sql_print_information(_("next log '%s' is currently active"),
1 by brian
clean slate
3530
                                rli->linfo.log_file_name);
3531
#endif
3532
        rli->cur_log= cur_log= rli->relay_log.get_log_file();
3533
        rli->cur_log_old_open_count= rli->relay_log.get_open_count();
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3534
        assert(rli->cur_log_fd == -1);
1 by brian
clean slate
3535
3536
        /*
3537
          Read pointer has to be at the start since we are the only
3538
          reader.
3539
          We must keep the LOCK_log to read the 4 first bytes, as this is a hot
3540
          log (same as when we call read_log_event() above: for a hot log we
3541
          take the mutex).
3542
        */
3543
        if (check_binlog_magic(cur_log,&errmsg))
3544
        {
3545
          if (!hot_log) pthread_mutex_unlock(log_lock);
3546
          goto err;
3547
        }
3548
        if (!hot_log) pthread_mutex_unlock(log_lock);
3549
        continue;
3550
      }
3551
      if (!hot_log) pthread_mutex_unlock(log_lock);
3552
      /*
3553
        if we get here, the log was not hot, so we will have to open it
3554
        ourselves. We are sure that the log is still not hot now (a log can get
3555
        from hot to cold, but not from cold to hot). No need for LOCK_log.
3556
      */
3557
#ifdef EXTRA_DEBUG
3558
      if (global_system_variables.log_warnings)
236.1.44 by Monty Taylor
Added en_US translation file.
3559
        sql_print_information(_("next log '%s' is not active"),
1 by brian
clean slate
3560
                              rli->linfo.log_file_name);
3561
#endif
3562
      // open_binlog() will check the magic header
3563
      if ((rli->cur_log_fd=open_binlog(cur_log,rli->linfo.log_file_name,
3564
                                       &errmsg)) <0)
3565
        goto err;
3566
    }
3567
    else
3568
    {
3569
      /*
3570
        Read failed with a non-EOF error.
3571
        TODO: come up with something better to handle this error
3572
      */
3573
      if (hot_log)
3574
        pthread_mutex_unlock(log_lock);
236.1.44 by Monty Taylor
Added en_US translation file.
3575
      sql_print_error(_("Slave SQL thread: I/O error reading "
3576
                        "event(errno: %d  cur_log->error: %d)"),
1 by brian
clean slate
3577
                      my_errno,cur_log->error);
3578
      // set read position to the beginning of the event
3579
      my_b_seek(cur_log,rli->event_relay_log_pos);
3580
      /* otherwise, we have had a partial read */
236.1.44 by Monty Taylor
Added en_US translation file.
3581
      errmsg = _("Aborting slave SQL thread because of partial event read");
1 by brian
clean slate
3582
      break;                                    // To end of function
3583
    }
3584
  }
3585
  if (!errmsg && global_system_variables.log_warnings)
3586
  {
236.1.46 by Monty Taylor
Fixed a few final gettext things in sql_slave.cc.
3587
    sql_print_information(_("Error reading relay log event: %s"),
3588
                          _("slave SQL thread was killed"));
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3589
    return(0);
1 by brian
clean slate
3590
  }
3591
3592
err:
3593
  if (errmsg)
236.1.44 by Monty Taylor
Added en_US translation file.
3594
    sql_print_error(_("Error reading relay log event: %s"), errmsg);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3595
  return(0);
1 by brian
clean slate
3596
}
3597
3598
/*
3599
  Rotate a relay log (this is used only by FLUSH LOGS; the automatic rotation
3600
  because of size is simpler because when we do it we already have all relevant
3601
  locks; here we don't, so this function is mainly taking locks).
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
3602
  Returns nothing as we cannot catch any error (DRIZZLE_BIN_LOG::new_file()
1 by brian
clean slate
3603
  is void).
3604
*/
3605
3606
void rotate_relay_log(Master_info* mi)
3607
{
3608
  Relay_log_info* rli= &mi->rli;
3609
3610
  /* We don't lock rli->run_lock. This would lead to deadlocks. */
3611
  pthread_mutex_lock(&mi->run_lock);
3612
3613
  /*
3614
     We need to test inited because otherwise, new_file() will attempt to lock
3615
     LOCK_log, which may not be inited (if we're not a slave).
3616
  */
3617
  if (!rli->inited)
3618
  {
3619
    goto end;
3620
  }
3621
3622
  /* If the relay log is closed, new_file() will do nothing. */
3623
  rli->relay_log.new_file();
3624
3625
  /*
3626
    We harvest now, because otherwise BIN_LOG_HEADER_SIZE will not immediately
3627
    be counted, so imagine a succession of FLUSH LOGS  and assume the slave
3628
    threads are started:
3629
    relay_log_space decreases by the size of the deleted relay log, but does
3630
    not increase, so flush-after-flush we may become negative, which is wrong.
3631
    Even if this will be corrected as soon as a query is replicated on the
3632
    slave (because the I/O thread will then call harvest_bytes_written() which
3633
    will harvest all these BIN_LOG_HEADER_SIZE we forgot), it may give strange
3634
    output in SHOW SLAVE STATUS meanwhile. So we harvest now.
3635
    If the log is closed, then this will just harvest the last writes, probably
3636
    0 as they probably have been harvested.
3637
  */
3638
  rli->relay_log.harvest_bytes_written(&rli->log_space_total);
3639
end:
3640
  pthread_mutex_unlock(&mi->run_lock);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3641
  return;
1 by brian
clean slate
3642
}
3643
3644
3645
/**
3646
   Detects, based on master's version (as found in the relay log), if master
3647
   has a certain bug.
3648
   @param rli Relay_log_info which tells the master's version
3649
   @param bug_id Number of the bug as found in bugs.mysql.com
3650
   @param report bool report error message, default TRUE
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3651
   @return true if master has the bug, FALSE if it does not.
1 by brian
clean slate
3652
*/
130 by Brian Aker
ulong cleanup
3653
bool rpl_master_has_bug(Relay_log_info *rli, uint32_t bug_id, bool report)
1 by brian
clean slate
3654
{
3655
  struct st_version_range_for_one_bug {
130 by Brian Aker
ulong cleanup
3656
    uint32_t        bug_id;
481 by Brian Aker
Remove all of uchar.
3657
    const unsigned char introduced_in[3]; // first version with bug
3658
    const unsigned char fixed_in[3];      // first version with fix
1 by brian
clean slate
3659
  };
3660
  static struct st_version_range_for_one_bug versions_for_all_bugs[]=
3661
  {
3662
    {24432, { 5, 0, 24 }, { 5, 0, 38 } },
3663
    {24432, { 5, 1, 12 }, { 5, 1, 17 } },
3664
    {33029, { 5, 0,  0 }, { 5, 0, 58 } },
3665
    {33029, { 5, 1,  0 }, { 5, 1, 12 } },
3666
  };
481 by Brian Aker
Remove all of uchar.
3667
  const unsigned char *master_ver=
1 by brian
clean slate
3668
    rli->relay_log.description_event_for_exec->server_version_split;
3669
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3670
  assert(sizeof(rli->relay_log.description_event_for_exec->server_version_split) == 3);
1 by brian
clean slate
3671
130 by Brian Aker
ulong cleanup
3672
  for (uint32_t i= 0;
1 by brian
clean slate
3673
       i < sizeof(versions_for_all_bugs)/sizeof(*versions_for_all_bugs);i++)
3674
  {
481 by Brian Aker
Remove all of uchar.
3675
    const unsigned char *introduced_in= versions_for_all_bugs[i].introduced_in,
1 by brian
clean slate
3676
      *fixed_in= versions_for_all_bugs[i].fixed_in;
3677
    if ((versions_for_all_bugs[i].bug_id == bug_id) &&
3678
        (memcmp(introduced_in, master_ver, 3) <= 0) &&
3679
        (memcmp(fixed_in,      master_ver, 3) >  0))
3680
    {
3681
      if (!report)
236.1.45 by Monty Taylor
Fixed a few gettext bugs.
3682
        return true;
3683
1 by brian
clean slate
3684
      // a short message for SHOW SLAVE STATUS (message length constraints)
236.1.45 by Monty Taylor
Fixed a few gettext bugs.
3685
      my_printf_error(ER_UNKNOWN_ERROR,
3686
                      _("master may suffer from"
3687
                        " http://bugs.mysql.com/bug.php?id=%u"
3688
                        " so slave stops; check error log on slave"
236.1.46 by Monty Taylor
Fixed a few final gettext things in sql_slave.cc.
3689
                        " for more info"), MYF(0), bug_id);
1 by brian
clean slate
3690
      // a verbose message for the error log
3691
      rli->report(ERROR_LEVEL, ER_UNKNOWN_ERROR,
236.1.45 by Monty Taylor
Fixed a few gettext bugs.
3692
                  _("According to the master's version ('%s'),"
3693
                    " it is probable that master suffers from this bug:"
3694
                    " http://bugs.mysql.com/bug.php?id=%u"
3695
                    " and thus replicating the current binary log event"
3696
                    " may make the slave's data become different from the"
3697
                    " master's data."
3698
                    " To take no risk, slave refuses to replicate"
3699
                    " this event and stops."
3700
                    " We recommend that all updates be stopped on the"
3701
                    " master and slave, that the data of both be"
3702
                    " manually synchronized,"
3703
                    " that master's binary logs be deleted,"
3704
                    " that master be upgraded to a version at least"
3705
                    " equal to '%d.%d.%d'. Then replication can be"
3706
                    " restarted."),
3707
                  rli->relay_log.description_event_for_exec->server_version,
3708
                  bug_id,
3709
                  fixed_in[0], fixed_in[1], fixed_in[2]);
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3710
      return true;
1 by brian
clean slate
3711
    }
3712
  }
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3713
  return false;
1 by brian
clean slate
3714
}
3715
3716
/**
3717
   BUG#33029, For all 5.0 up to 5.0.58 exclusive, and 5.1 up to 5.1.12
3718
   exclusive, if one statement in a SP generated AUTO_INCREMENT value
3719
   by the top statement, all statements after it would be considered
3720
   generated AUTO_INCREMENT value by the top statement, and a
3721
   erroneous INSERT_ID value might be associated with these statement,
3722
   which could cause duplicate entry error and stop the slave.
3723
3724
   Detect buggy master to work around.
3725
 */
3726
bool rpl_master_erroneous_autoinc(THD *thd)
3727
{
3728
  if (active_mi && active_mi->rli.sql_thd == thd)
3729
  {
3730
    Relay_log_info *rli= &active_mi->rli;
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3731
    return rpl_master_has_bug(rli, 33029, false);
1 by brian
clean slate
3732
  }
51.1.47 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3733
  return false;
1 by brian
clean slate
3734
}
3735
3736
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
3737
template class I_List_iterator<i_string>;
3738
template class I_List_iterator<i_string_pair>;
3739
#endif
3740
3741
/**
3742
  @} (end of group Replication)
3743
*/