~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/slave.cc

  • Committer: Lee
  • Date: 2009-01-06 20:24:58 UTC
  • mfrom: (759 drizzle)
  • mto: This revision was merged to the branch mainline in revision 763.
  • Revision ID: lbieber@lbieber-desktop-20090106202458-82n4kyftrnakvl4r
merge with latest from the trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* Copyright (C) 2000-2003 DRIZZLE AB
2
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 */
 
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
15
 
16
16
 
17
17
/**
103
103
  {
104
104
    N_("Waiting to reconnect after a failed registration on master"),
105
105
    N_("Slave I/O thread killed while waiting to reconnect after a "
106
 
                 "failed registration on master"),
 
106
       "failed registration on master"),
107
107
    N_("Reconnecting after a failed registration on master"),
108
108
    N_("failed registering on master, reconnecting to try again, "
109
 
                 "log '%s' at position %s"),
 
109
       "log '%s' at position %s"),
110
110
    "COM_REGISTER_SLAVE",
111
111
    N_("Slave I/O thread killed during or after reconnect")
112
112
  },
115
115
    N_("Slave I/O thread killed while retrying master dump"),
116
116
    N_("Reconnecting after a failed binlog dump request"),
117
117
    N_("failed dump request, reconnecting to try again, "
118
 
                 "log '%s' at position %s"),
 
118
       "log '%s' at position %s"),
119
119
    "COM_BINLOG_DUMP",
120
120
    N_("Slave I/O thread killed during or after reconnect")
121
121
  },
122
122
  {
123
123
    N_("Waiting to reconnect after a failed master event read"),
124
124
    N_("Slave I/O thread killed while waiting to reconnect "
125
 
                 "after a failed read"),
 
125
       "after a failed read"),
126
126
    N_("Reconnecting after a failed master event read"),
127
127
    N_("Slave I/O thread: Failed reading log event, "
128
 
                 "reconnecting to retry, log '%s' at position %s"),
 
128
       "reconnecting to retry, log '%s' at position %s"),
129
129
    "",
130
130
    N_("Slave I/O thread killed during or after a "
131
 
                 "reconnect done to recover from failed read")
 
131
       "reconnect done to recover from failed read")
132
132
  }
133
133
};
134
134
 
143
143
static int32_t init_slave_thread(Session* session, SLAVE_Session_TYPE session_type);
144
144
static int32_t safe_connect(Session* session, DRIZZLE *drizzle, Master_info* mi);
145
145
static int32_t safe_reconnect(Session* session, DRIZZLE *drizzle, Master_info* mi,
146
 
                          bool suppress_warnings);
 
146
                              bool suppress_warnings);
147
147
static int32_t connect_to_master(Session* session, DRIZZLE *drizzle, Master_info* mi,
148
 
                             bool reconnect, bool suppress_warnings);
 
148
                                 bool reconnect, bool suppress_warnings);
149
149
static int32_t safe_sleep(Session* session, int32_t sec, CHECK_KILLED_FUNC thread_killed,
150
 
                      void* thread_killed_arg);
 
150
                          void* thread_killed_arg);
151
151
static int32_t get_master_version_and_clock(DRIZZLE *drizzle, Master_info* mi);
152
152
static Log_event* next_event(Relay_log_info* rli);
153
153
static int32_t queue_event(Master_info* mi,const char* buf,uint32_t event_len);
154
154
static int32_t terminate_slave_thread(Session *session,
155
 
                                  pthread_mutex_t* term_lock,
156
 
                                  pthread_cond_t* term_cond,
157
 
                                  volatile uint32_t *slave_running,
158
 
                                  bool skip_lock);
 
155
                                      pthread_mutex_t* term_lock,
 
156
                                      pthread_cond_t* term_cond,
 
157
                                      volatile uint32_t *slave_running,
 
158
                                      bool skip_lock);
159
159
static bool check_io_slave_killed(Session *session, Master_info *mi, const char *info);
160
160
 
161
161
/*
162
162
  Find out which replications threads are running
163
163
 
164
164
  SYNOPSIS
165
 
    init_thread_mask()
166
 
    mask                Return value here
167
 
    mi                  master_info for slave
168
 
    inverse             If set, returns which threads are not running
 
165
  init_thread_mask()
 
166
  mask                Return value here
 
167
  mi                  master_info for slave
 
168
  inverse             If set, returns which threads are not running
169
169
 
170
170
  IMPLEMENTATION
171
 
    Get a bit mask for which threads are running so that we can later restart
172
 
    these threads.
 
171
  Get a bit mask for which threads are running so that we can later restart
 
172
  these threads.
173
173
 
174
174
  RETURN
175
 
    mask        If inverse == 0, running threads
176
 
                If inverse == 1, stopped threads
 
175
  mask        If inverse == 0, running threads
 
176
  If inverse == 1, stopped threads
177
177
*/
178
178
 
179
179
void init_thread_mask(int32_t* mask,Master_info* mi,bool inverse)
229
229
  */
230
230
  pthread_mutex_lock(&LOCK_active_mi);
231
231
  /*
232
 
    TODO: re-write this to interate through the list of files
233
 
    for multi-master
 
232
    TODO: re-write this to interate through the list of files for multi-master
234
233
  */
235
234
  active_mi= new Master_info;
236
235
 
241
240
  */
242
241
  if (!active_mi)
243
242
  {
244
 
    sql_print_error(_("Failed to allocate memory for the master info structure"));
 
243
    errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to allocate memory for the master info structure"));
245
244
    goto err;
246
245
  }
247
246
 
248
247
  if (active_mi->init_master_info(master_info_file, relay_log_info_file, (SLAVE_IO | SLAVE_SQL)))
249
248
  {
250
 
    sql_print_error(_("Failed to initialize the master info structure"));
 
249
    errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to initialize the master info structure"));
251
250
    goto err;
252
251
  }
253
252
 
262
261
                            relay_log_info_file,
263
262
                            SLAVE_IO | SLAVE_SQL))
264
263
    {
265
 
      sql_print_error(_("Failed to create slave threads"));
 
264
      errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to create slave threads"));
266
265
      goto err;
267
266
    }
268
267
  }
279
278
  Init function to set up array for errors that should be skipped for slave
280
279
 
281
280
  SYNOPSIS
282
 
    init_slave_skip_errors()
283
 
    arg         List of errors numbers to skip, separated with ','
 
281
  init_slave_skip_errors()
 
282
  arg         List of errors numbers to skip, separated with ','
284
283
 
285
284
  NOTES
286
 
    Called from get_options() in mysqld.cc on start-up
 
285
  Called from get_options() in mysqld.cc on start-up
287
286
*/
288
287
 
289
288
void init_slave_skip_errors(const char* arg)
309
308
    if (!(p= str2int(p, 10, 0, LONG_MAX, &err_code)))
310
309
      break;
311
310
    if (err_code < MAX_SLAVE_ERROR)
312
 
       bitmap_set_bit(&slave_error_mask,(uint32_t)err_code);
 
311
      bitmap_set_bit(&slave_error_mask,(uint32_t)err_code);
313
312
    while (!my_isdigit(system_charset_info,*p) && *p)
314
313
      p++;
315
314
  }
349
348
 
350
349
 
351
350
/**
352
 
   Wait for a slave thread to terminate.
353
 
 
354
 
   This function is called after requesting the thread to terminate
355
 
   (by setting @c abort_slave member of @c Relay_log_info or @c
356
 
   Master_info structure to 1). Termination of the thread is
357
 
   controlled with the the predicate <code>*slave_running</code>.
358
 
 
359
 
   Function will acquire @c term_lock before waiting on the condition
360
 
   unless @c skip_lock is true in which case the mutex should be owned
361
 
   by the caller of this function and will remain acquired after
362
 
   return from the function.
363
 
 
364
 
   @param term_lock
365
 
          Associated lock to use when waiting for @c term_cond
366
 
 
367
 
   @param term_cond
368
 
          Condition that is signalled when the thread has terminated
369
 
 
370
 
   @param slave_running
371
 
          Pointer to predicate to check for slave thread termination
372
 
 
373
 
   @param skip_lock
374
 
          If @c true the lock will not be acquired before waiting on
375
 
          the condition. In this case, it is assumed that the calling
376
 
          function acquires the lock before calling this function.
377
 
 
378
 
   @retval 0 All OK
379
 
 */
 
351
  Wait for a slave thread to terminate.
 
352
 
 
353
  This function is called after requesting the thread to terminate
 
354
  (by setting @c abort_slave member of @c Relay_log_info or @c
 
355
  Master_info structure to 1). Termination of the thread is
 
356
  controlled with the the predicate <code>*slave_running</code>.
 
357
 
 
358
  Function will acquire @c term_lock before waiting on the condition
 
359
  unless @c skip_lock is true in which case the mutex should be owned
 
360
  by the caller of this function and will remain acquired after
 
361
  return from the function.
 
362
 
 
363
  @param term_lock
 
364
  Associated lock to use when waiting for @c term_cond
 
365
 
 
366
  @param term_cond
 
367
  Condition that is signalled when the thread has terminated
 
368
 
 
369
  @param slave_running
 
370
  Pointer to predicate to check for slave thread termination
 
371
 
 
372
  @param skip_lock
 
373
  If @c true the lock will not be acquired before waiting on
 
374
  the condition. In this case, it is assumed that the calling
 
375
  function acquires the lock before calling this function.
 
376
 
 
377
  @retval 0 All OK
 
378
*/
380
379
static int32_t
381
380
terminate_slave_thread(Session *session,
382
381
                       pthread_mutex_t* term_lock,
459
458
      pthread_cond_broadcast(start_cond);
460
459
    if (start_lock)
461
460
      pthread_mutex_unlock(start_lock);
462
 
    sql_print_error(_("Server id not set, will not start slave"));
 
461
    errmsg_printf(ERRMSG_LVL_ERROR, _("Server id not set, will not start slave"));
463
462
    return(ER_BAD_SLAVE);
464
463
  }
465
464
 
492
491
    while (start_id == *slave_run_id)
493
492
    {
494
493
      const char* old_msg = session->enter_cond(start_cond,cond_lock,
495
 
                                            "Waiting for slave thread to start");
 
494
                                                "Waiting for slave thread to start");
496
495
      pthread_cond_wait(start_cond,cond_lock);
497
496
      session->exit_cond(old_msg);
498
497
      pthread_mutex_lock(cond_lock); // re-acquire it as exit_cond() released
510
509
  start_slave_threads()
511
510
 
512
511
  NOTES
513
 
    SLAVE_FORCE_ALL is not implemented here on purpose since it does not make
514
 
    sense to do that for starting a slave--we always care if it actually
515
 
    started the threads that were not previously running
 
512
  SLAVE_FORCE_ALL is not implemented here on purpose since it does not make
 
513
  sense to do that for starting a slave--we always care if it actually
 
514
  started the threads that were not previously running
516
515
*/
517
516
 
518
517
int32_t start_slave_threads(bool need_slave_mutex, bool wait_for_start,
519
 
                        Master_info* mi, const char*, const char*,
520
 
                        int32_t thread_mask)
 
518
                            Master_info* mi, const char*, const char*,
 
519
                            int32_t thread_mask)
521
520
{
522
521
  pthread_mutex_t *lock_io=0,*lock_sql=0,*lock_cond_io=0,*lock_cond_sql=0;
523
522
  pthread_cond_t* cond_io=0,*cond_sql=0;
567
566
  Free all resources used by slave
568
567
 
569
568
  SYNOPSIS
570
 
    end_slave()
 
569
  end_slave()
571
570
*/
572
571
 
573
572
void end_slave()
626
625
    {
627
626
      rli->report(ERROR_LEVEL, 0,
628
627
                  _("SQL thread had to stop in an unsafe situation, in "
629
 
                  "the middle of applying updates to a "
630
 
                  "non-transactional table without any primary key. "
631
 
                  "There is a risk of duplicate updates when the slave "
632
 
                  "SQL thread is restarted. Please check your tables' "
633
 
                  "contents after restart."));
 
628
                    "the middle of applying updates to a "
 
629
                    "non-transactional table without any primary key. "
 
630
                    "There is a risk of duplicate updates when the slave "
 
631
                    "SQL thread is restarted. Please check your tables' "
 
632
                    "contents after restart."));
634
633
      return(1);
635
634
    }
636
635
  }
642
641
  skip_load_data_infile()
643
642
 
644
643
  NOTES
645
 
    This is used to tell a 3.23 master to break send_file()
 
644
  This is used to tell a 3.23 master to break send_file()
646
645
*/
647
646
 
648
647
void skip_load_data_infile(NET *net)
657
656
bool net_request_file(NET* net, const char* fname)
658
657
{
659
658
  return(net_write_command(net, 251, (unsigned char*) fname, strlen(fname),
660
 
                                (unsigned char*) "", 0));
 
659
                           (unsigned char*) "", 0));
661
660
}
662
661
 
663
662
/*
673
672
}
674
673
 
675
674
int32_t init_strvar_from_file(char *var, int32_t max_size, IO_CACHE *f,
676
 
                                 const char *default_val)
 
675
                              const char *default_val)
677
676
{
678
677
  uint32_t length;
679
678
 
745
744
  if (io_slave_killed(session, mi))
746
745
  {
747
746
    if (info && global_system_variables.log_warnings)
748
 
      sql_print_information("%s",info);
 
747
      errmsg_printf(ERRMSG_LVL_INFO, "%s",info);
749
748
    return true;
750
749
  }
751
750
  return false;
830
829
  }
831
830
 
832
831
  /*
833
 
     This does not mean that a 5.0 slave will be able to read a 6.0 master; but
834
 
     as we don't know yet, we don't want to forbid this for now. If a 5.0 slave
835
 
     can't read a 6.0 master, this will show up when the slave can't read some
836
 
     events sent by the master, and there will be error messages.
 
832
    This does not mean that a 5.0 slave will be able to read a 6.0 master; but
 
833
    as we don't know yet, we don't want to forbid this for now. If a 5.0 slave
 
834
    can't read a 6.0 master, this will show up when the slave can't read some
 
835
    events sent by the master, and there will be error messages.
837
836
  */
838
837
 
839
838
  if (err_msg.length() != 0)
864
863
  else if (!check_io_slave_killed(mi->io_session, mi, NULL))
865
864
  {
866
865
    mi->clock_diff_with_master= 0; /* The "most sensible" value */
867
 
    sql_print_warning(_("\"SELECT UNIX_TIMESTAMP()\" failed on master, "
868
 
                        "do not trust column Seconds_Behind_Master of SHOW "
869
 
                        "SLAVE STATUS. Error: %s (%d)"),
870
 
                      drizzle_error(drizzle), drizzle_errno(drizzle));
 
866
    errmsg_printf(ERRMSG_LVL_WARN, _("\"SELECT UNIX_TIMESTAMP()\" failed on master, "
 
867
                                     "do not trust column Seconds_Behind_Master of SHOW "
 
868
                                     "SLAVE STATUS. Error: %s (%d)"),
 
869
                  drizzle_error(drizzle), drizzle_errno(drizzle));
871
870
  }
872
871
  if (master_res)
873
872
    drizzle_free_result(master_res);
883
882
    UNIX_TIMESTAMP() instead, but this would not have worked on 3.23 masters.
884
883
  */
885
884
  if (!drizzle_real_query(drizzle,
886
 
                        STRING_WITH_LEN("SHOW VARIABLES LIKE 'SERVER_ID'")) &&
 
885
                          STRING_WITH_LEN("SHOW VARIABLES LIKE 'SERVER_ID'")) &&
887
886
      (master_res= drizzle_store_result(drizzle)))
888
887
  {
889
888
    if ((master_row= drizzle_fetch_row(master_res)) &&
929
928
 
930
929
  if ((*drizzle->server_version == '4') &&
931
930
      !drizzle_real_query(drizzle,
932
 
                        STRING_WITH_LEN("SELECT @@GLOBAL.COLLATION_SERVER")) &&
 
931
                          STRING_WITH_LEN("SELECT @@GLOBAL.COLLATION_SERVER")) &&
933
932
      (master_res= drizzle_store_result(drizzle)))
934
933
  {
935
934
    if ((master_row= drizzle_fetch_row(master_res)) &&
991
990
    const char query_format[]= "SET @master_heartbeat_period= %s";
992
991
    char query[sizeof(query_format) - 2 + sizeof(llbuf)];
993
992
    /*
994
 
       the period is an uint64_t of nano-secs.
 
993
      the period is an uint64_t of nano-secs.
995
994
    */
996
995
    llstr((uint64_t) (mi->heartbeat_period*1000000000UL), llbuf);
997
996
    sprintf(query, query_format, llbuf);
1017
1016
err:
1018
1017
  if (err_msg.length() != 0)
1019
1018
  {
1020
 
    sql_print_error("%s",err_msg.ptr());
 
1019
    errmsg_printf(ERRMSG_LVL_ERROR, "%s",err_msg.ptr());
1021
1020
    assert(err_code != 0);
1022
1021
    mi->report(ERROR_LEVEL, err_code, "%s",err_msg.ptr());
1023
1022
    return(1);
1036
1035
 
1037
1036
  pthread_mutex_lock(&rli->log_space_lock);
1038
1037
  save_proc_info= session->enter_cond(&rli->log_space_cond,
1039
 
                                  &rli->log_space_lock,
1040
 
                                  _("Waiting for the slave SQL thread "
1041
 
                                    "to free enough relay log space"));
 
1038
                                      &rli->log_space_lock,
 
1039
                                      _("Waiting for the slave SQL thread "
 
1040
                                        "to free enough relay log space"));
1042
1041
  while (rli->log_space_limit < rli->log_space_total &&
1043
1042
         !(slave_killed=io_slave_killed(session,mi)) &&
1044
1043
         !rli->ignore_log_space_limit)
1053
1052
 
1054
1053
  SYNOPSIS
1055
1054
  write_ignored_events_info_to_relay_log()
1056
 
    session             pointer to I/O thread's session
1057
 
    mi
 
1055
  session             pointer to I/O thread's session
 
1056
  mi
1058
1057
 
1059
1058
  DESCRIPTION
1060
 
    Slave I/O thread, going to die, must leave a durable trace of the
1061
 
    ignored events' end position for the use of the slave SQL thread, by
1062
 
    calling this function. Only that thread can call it (see assertion).
1063
 
 */
 
1059
  Slave I/O thread, going to die, must leave a durable trace of the
 
1060
  ignored events' end position for the use of the slave SQL thread, by
 
1061
  calling this function. Only that thread can call it (see assertion).
 
1062
*/
1064
1063
static void write_ignored_events_info_to_relay_log(Session *session,
1065
1064
                                                   Master_info *mi)
1066
1065
{
1088
1087
                     " inaccurate"));
1089
1088
      rli->relay_log.harvest_bytes_written(&rli->log_space_total);
1090
1089
      if (mi->flush())
1091
 
        sql_print_error(_("Failed to flush master info file"));
 
1090
        errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to flush master info file"));
1092
1091
      delete ev;
1093
1092
    }
1094
1093
    else
1104
1103
 
1105
1104
 
1106
1105
int32_t register_slave_on_master(DRIZZLE *drizzle, Master_info *mi,
1107
 
                             bool *suppress_warnings)
 
1106
                                 bool *suppress_warnings)
1108
1107
{
1109
1108
  unsigned char buf[1024], *pos= buf;
1110
1109
  uint32_t report_host_len, report_user_len=0, report_password_len=0;
1154
1153
  Protocol *protocol= session->protocol;
1155
1154
 
1156
1155
  field_list.push_back(new Item_empty_string("Slave_IO_State",
1157
 
                                                     14));
 
1156
                                             14));
1158
1157
  field_list.push_back(new Item_empty_string("Master_Host",
1159
 
                                                     sizeof(mi->host)));
 
1158
                                             sizeof(mi->host)));
1160
1159
  field_list.push_back(new Item_empty_string("Master_User",
1161
 
                                                     sizeof(mi->user)));
 
1160
                                             sizeof(mi->user)));
1162
1161
  field_list.push_back(new Item_return_int("Master_Port", 7,
1163
1162
                                           DRIZZLE_TYPE_LONG));
1164
1163
  field_list.push_back(new Item_return_int("Connect_Retry", 10,
1235
1234
    protocol->store((uint64_t) mi->rli.log_space_total);
1236
1235
 
1237
1236
    protocol->store(
1238
 
      mi->rli.until_condition==Relay_log_info::UNTIL_NONE ? "None":
1239
 
        ( mi->rli.until_condition==Relay_log_info::UNTIL_MASTER_POS? "Master":
1240
 
          "Relay"), &my_charset_bin);
 
1237
                    mi->rli.until_condition==Relay_log_info::UNTIL_NONE ? "None":
 
1238
                    ( mi->rli.until_condition==Relay_log_info::UNTIL_MASTER_POS? "Master":
 
1239
                      "Relay"), &my_charset_bin);
1241
1240
    protocol->store(mi->rli.until_log_name, &my_charset_bin);
1242
1241
    protocol->store((uint64_t) mi->rli.until_log_pos);
1243
1242
 
1271
1270
        special marker to say "consider we have caught up".
1272
1271
      */
1273
1272
      protocol->store((int64_t)(mi->rli.last_master_timestamp ?
1274
 
                                 cmax((long)0, time_diff) : 0));
 
1273
                                cmax((long)0, time_diff) : 0));
1275
1274
    }
1276
1275
    else
1277
1276
    {
1301
1300
void set_slave_thread_options(Session* session)
1302
1301
{
1303
1302
  /*
1304
 
     It's nonsense to constrain the slave threads with max_join_size; if a
1305
 
     query succeeded on master, we HAVE to execute it. So set
1306
 
     OPTION_BIG_SELECTS. Setting max_join_size to HA_POS_ERROR is not enough
1307
 
     (and it's not needed if we have OPTION_BIG_SELECTS) because an INSERT
1308
 
     SELECT examining more than 4 billion rows would still fail (yes, because
1309
 
     when max_join_size is 4G, OPTION_BIG_SELECTS is automatically set, but
1310
 
     only for client threads.
 
1303
    It's nonsense to constrain the slave threads with max_join_size; if a
 
1304
    query succeeded on master, we HAVE to execute it. So set
 
1305
    OPTION_BIG_SELECTS. Setting max_join_size to HA_POS_ERROR is not enough
 
1306
    (and it's not needed if we have OPTION_BIG_SELECTS) because an INSERT
 
1307
    SELECT examining more than 4 billion rows would still fail (yes, because
 
1308
    when max_join_size is 4G, OPTION_BIG_SELECTS is automatically set, but
 
1309
    only for client threads.
1311
1310
  */
1312
1311
  uint64_t options= session->options | OPTION_BIG_SELECTS;
1313
1312
  if (opt_log_slave_updates)
1330
1329
    SYSTEM_THREAD_SLAVE_SQL : SYSTEM_THREAD_SLAVE_IO;
1331
1330
  session->security_ctx.skip_grants();
1332
1331
  my_net_init(&session->net, 0);
1333
 
/*
1334
 
  Adding MAX_LOG_EVENT_HEADER_LEN to the max_allowed_packet on all
1335
 
  slave threads, since a replication event can become this much larger
1336
 
  than the corresponding packet (query) sent from client to master.
1337
 
*/
 
1332
  /*
 
1333
    Adding MAX_LOG_EVENT_HEADER_LEN to the max_allowed_packet on all
 
1334
    slave threads, since a replication event can become this much larger
 
1335
    than the corresponding packet (query) sent from client to master.
 
1336
  */
1338
1337
  session->variables.max_allowed_packet= global_system_variables.max_allowed_packet
1339
1338
    + MAX_LOG_EVENT_HEADER;  /* note, incr over the global not session var */
1340
1339
  session->slave_thread = 1;
1344
1343
  session->thread_id= session->variables.pseudo_thread_id= thread_id++;
1345
1344
  pthread_mutex_unlock(&LOCK_thread_count);
1346
1345
 
1347
 
 simulate_error|= (1 << SLAVE_Session_IO);
1348
 
 simulate_error|= (1 << SLAVE_Session_SQL);
 
1346
  simulate_error|= (1 << SLAVE_Session_IO);
 
1347
  simulate_error|= (1 << SLAVE_Session_SQL);
1349
1348
  if (init_thr_lock() || session->store_globals() || simulate_error & (1<< session_type))
1350
1349
  {
1351
1350
    session->cleanup();
1401
1400
 
1402
1401
 
1403
1402
static int32_t request_dump(DRIZZLE *drizzle, Master_info* mi,
1404
 
                        bool *suppress_warnings)
 
1403
                            bool *suppress_warnings)
1405
1404
{
1406
1405
  unsigned char buf[FN_REFLEN + 10];
1407
1406
  int32_t len;
1426
1425
    if (drizzle_errno(drizzle) == ER_NET_READ_INTERRUPTED)
1427
1426
      *suppress_warnings= true;                 // Suppress reconnect warning
1428
1427
    else
1429
 
      sql_print_error(_("Error on COM_BINLOG_DUMP: %d  %s, will retry in %d secs"),
1430
 
                      drizzle_errno(drizzle), drizzle_error(drizzle),
1431
 
                      mi->connect_retry);
 
1428
      errmsg_printf(ERRMSG_LVL_ERROR, _("Error on COM_BINLOG_DUMP: %d  %s, will retry in %d secs"),
 
1429
                    drizzle_errno(drizzle), drizzle_error(drizzle),
 
1430
                    mi->connect_retry);
1432
1431
    return(1);
1433
1432
  }
1434
1433
 
1439
1438
  Read one event from the master
1440
1439
 
1441
1440
  SYNOPSIS
1442
 
    read_event()
1443
 
    DRIZZLE               DRIZZLE connection
1444
 
    mi                  Master connection information
1445
 
    suppress_warnings   TRUE when a normal net read timeout has caused us to
1446
 
                        try a reconnect.  We do not want to print anything to
1447
 
                        the error log in this case because this a anormal
1448
 
                        event in an idle server.
 
1441
  read_event()
 
1442
  DRIZZLE               DRIZZLE connection
 
1443
  mi                  Master connection information
 
1444
  suppress_warnings   TRUE when a normal net read timeout has caused us to
 
1445
  try a reconnect.  We do not want to print anything to
 
1446
  the error log in this case because this a anormal
 
1447
  event in an idle server.
1449
1448
 
1450
 
    RETURN VALUES
1451
 
    'packet_error'      Error
1452
 
    number              Length of packet
 
1449
  RETURN VALUES
 
1450
  'packet_error'      Error
 
1451
  number              Length of packet
1453
1452
*/
1454
1453
 
1455
1454
static uint32_t read_event(DRIZZLE *drizzle,
1479
1478
      *suppress_warnings= true;
1480
1479
    }
1481
1480
    else
1482
 
      sql_print_error(_("Error reading packet from server: %s ( server_errno=%d)"),
1483
 
                      drizzle_error(drizzle), drizzle_errno(drizzle));
 
1481
      errmsg_printf(ERRMSG_LVL_ERROR, _("Error reading packet from server: %s ( server_errno=%d)"),
 
1482
                    drizzle_error(drizzle), drizzle_errno(drizzle));
1484
1483
    return(packet_error);
1485
1484
  }
1486
1485
 
1487
1486
  /* Check if eof packet */
1488
1487
  if (len < 8 && drizzle->net.read_pos[0] == 254)
1489
1488
  {
1490
 
    sql_print_information(_("Slave: received end packet from server, apparent "
1491
 
                            "master shutdown: %s"),
1492
 
                     drizzle_error(drizzle));
1493
 
     return(packet_error);
 
1489
    errmsg_printf(ERRMSG_LVL_INFO, _("Slave: received end packet from server, apparent "
 
1490
                                     "master shutdown: %s"),
 
1491
                  drizzle_error(drizzle));
 
1492
    return(packet_error);
1494
1493
  }
1495
1494
 
1496
1495
  return(len - 1);
1558
1557
  In essence, this function does:
1559
1558
 
1560
1559
  @code
1561
 
    ev->apply_event(rli);
1562
 
    ev->update_pos(rli);
 
1560
  ev->apply_event(rli);
 
1561
  ev->update_pos(rli);
1563
1562
  @endcode
1564
1563
 
1565
1564
  But it also does some maintainance, such as skipping events if
1579
1578
  ev->update_pos().
1580
1579
*/
1581
1580
int32_t apply_event_and_update_pos(Log_event* ev, Session* session, Relay_log_info* rli,
1582
 
                               bool skip)
 
1581
                                   bool skip)
1583
1582
{
1584
1583
  int32_t exec_res= 0;
1585
1584
 
1646
1645
      char buf[22];
1647
1646
      rli->report(ERROR_LEVEL, ER_UNKNOWN_ERROR,
1648
1647
                  _("It was not possible to update the positions"
1649
 
                  " of the relay log information: the slave may"
1650
 
                  " be in an inconsistent state."
1651
 
                  " Stopped in %s position %s"),
 
1648
                    " of the relay log information: the slave may"
 
1649
                    " be in an inconsistent state."
 
1650
                    " Stopped in %s position %s"),
1652
1651
                  rli->group_relay_log_name.c_str(),
1653
1652
                  llstr(rli->group_relay_log_pos, buf));
1654
1653
      return(2);
1667
1666
 
1668
1667
  This function may fail to apply the event for the following reasons:
1669
1668
 
1670
 
   - The position specfied by the UNTIL condition of the START SLAVE
1671
 
     command is reached.
1672
 
 
1673
 
   - It was not possible to read the event from the log.
1674
 
 
1675
 
   - The slave is killed.
1676
 
 
1677
 
   - An error occurred when applying the event, and the event has been
1678
 
     tried slave_trans_retries times.  If the event has been retried
1679
 
     fewer times, 0 is returned.
1680
 
 
1681
 
   - init_master_info or init_relay_log_pos failed. (These are called
1682
 
     if a failure occurs when applying the event.)</li>
1683
 
 
1684
 
   - An error occurred when updating the binlog position.
 
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.
1685
1684
 
1686
1685
  @retval 0 The event was applied.
1687
1686
 
1690
1689
static int32_t exec_relay_log_event(Session* session, Relay_log_info* rli)
1691
1690
{
1692
1691
  /*
1693
 
     We acquire this mutex since we need it for all operations except
1694
 
     event execution. But we will release it in places where we will
1695
 
     wait for something for example inside of next_event().
1696
 
   */
 
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
  */
1697
1696
  pthread_mutex_lock(&rli->data_lock);
1698
1697
 
1699
1698
  Log_event * ev = next_event(rli);
1720
1719
                                ev->log_pos - ev->data_written))
1721
1720
    {
1722
1721
      char buf[22];
1723
 
      sql_print_information(_("Slave SQL thread stopped because it reached its"
1724
 
                              " UNTIL position %s"),
1725
 
                            llstr(rli->until_pos(), buf));
 
1722
      errmsg_printf(ERRMSG_LVL_INFO, _("Slave SQL thread stopped because it reached its"
 
1723
                                       " UNTIL position %s"),
 
1724
                    llstr(rli->until_pos(), buf));
1726
1725
      /*
1727
1726
        Setting abort_slave flag because we do not want additional message about
1728
1727
        error in query execution to be printed.
1761
1760
          We were in a transaction which has been rolled back because of a
1762
1761
          temporary error;
1763
1762
          let's seek back to BEGIN log event and retry it all again.
1764
 
          Note, if lock wait timeout (innodb_lock_wait_timeout exceeded)
1765
 
          there is no rollback since 5.0.13 (ref: manual).
 
1763
          Note, if lock wait timeout (innodb_lock_wait_timeout exceeded)
 
1764
          there is no rollback since 5.0.13 (ref: manual).
1766
1765
          We have to not only seek but also
1767
1766
          a) init_master_info(), to seek back to hot relay log's start for later
1768
1767
          (for when we will come back to this hot log after re-processing the
1774
1773
        if (rli->trans_retries < slave_trans_retries)
1775
1774
        {
1776
1775
          if (rli->mi->init_master_info(0, 0, SLAVE_SQL))
1777
 
            sql_print_error(_("Failed to initialize the master info structure"));
 
1776
            errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to initialize the master info structure"));
1778
1777
          else if (init_relay_log_pos(rli,
1779
1778
                                      rli->group_relay_log_name.c_str(),
1780
1779
                                      rli->group_relay_log_pos,
1781
1780
                                      1, &errmsg, 1))
1782
 
            sql_print_error(_("Error initializing relay log position: %s"),
1783
 
                            errmsg);
 
1781
            errmsg_printf(ERRMSG_LVL_ERROR, _("Error initializing relay log position: %s"),
 
1782
                          errmsg);
1784
1783
          else
1785
1784
          {
1786
1785
            exec_res= 0;
1795
1794
          }
1796
1795
        }
1797
1796
        else
1798
 
          sql_print_error(_("Slave SQL thread retried transaction %"PRIu64" time(s) "
1799
 
                            "in vain, giving up. Consider raising the value of "
1800
 
                            "the slave_transaction_retries variable."),
1801
 
                          slave_trans_retries);
 
1797
          errmsg_printf(ERRMSG_LVL_ERROR, _("Slave SQL thread retried transaction %"PRIu64" time(s) "
 
1798
                                            "in vain, giving up. Consider raising the value of "
 
1799
                                            "the slave_transaction_retries variable."),
 
1800
                        slave_trans_retries);
1802
1801
      }
1803
1802
      else if ((exec_res && !temp_err) ||
1804
1803
               (opt_using_transactions &&
1809
1808
          or failed with a non-transient error.  On a successful
1810
1809
          event, the execution will proceed as usual; in the case of a
1811
1810
          non-transient error, the slave will stop with an error.
1812
 
         */
 
1811
        */
1813
1812
        rli->trans_retries= 0; // restart from fresh
1814
1813
      }
1815
1814
    }
1849
1848
  @param[in]     mi                  Master connection information.
1850
1849
  @param[in,out] retry_count         Number of attempts to reconnect.
1851
1850
  @param[in]     suppress_warnings   TRUE when a normal net read timeout
1852
 
                                     has caused to reconnecting.
 
1851
  has caused to reconnecting.
1853
1852
  @param[in]     messages            Messages to print/log, see
1854
 
                                     reconnect_messages[] array.
 
1853
  reconnect_messages[] array.
1855
1854
 
1856
1855
  @retval        0                   OK.
1857
1856
  @retval        1                   There was an error.
1892
1891
    }
1893
1892
    else
1894
1893
    {
1895
 
      sql_print_information("%s",buf);
 
1894
      errmsg_printf(ERRMSG_LVL_INFO, "%s",buf);
1896
1895
    }
1897
1896
  }
1898
1897
  if (safe_reconnect(session, drizzle, mi, 1) || io_slave_killed(session, mi))
1899
1898
  {
1900
1899
    if (global_system_variables.log_warnings)
1901
 
      sql_print_information("%s",_(messages[SLAVE_RECON_MSG_KILLED_AFTER]));
 
1900
      errmsg_printf(ERRMSG_LVL_INFO, "%s",_(messages[SLAVE_RECON_MSG_KILLED_AFTER]));
1902
1901
    return 1;
1903
1902
  }
1904
1903
  return 0;
1939
1938
  {
1940
1939
    pthread_cond_broadcast(&mi->start_cond);
1941
1940
    pthread_mutex_unlock(&mi->run_lock);
1942
 
    sql_print_error(_("Failed during slave I/O thread initialization"));
 
1941
    errmsg_printf(ERRMSG_LVL_ERROR, _("Failed during slave I/O thread initialization"));
1943
1942
    goto err;
1944
1943
  }
1945
1944
  pthread_mutex_lock(&LOCK_thread_count);
1961
1960
  // we can get killed during safe_connect
1962
1961
  if (!safe_connect(session, drizzle, mi))
1963
1962
  {
1964
 
    sql_print_information(_("Slave I/O thread: connected to master '%s@%s:%d',"
1965
 
                            "replication started in log '%s' at position %s"),
1966
 
                          mi->getUsername(), mi->getHostname(), mi->getPort(),
1967
 
                          IO_RPL_LOG_NAME,
1968
 
                          llstr(mi->getLogPosition(), llbuff));
1969
 
  /*
1970
 
    Adding MAX_LOG_EVENT_HEADER_LEN to the max_packet_size on the I/O
1971
 
    thread, since a replication event can become this much larger than
1972
 
    the corresponding packet (query) sent from client to master.
1973
 
  */
 
1963
    errmsg_printf(ERRMSG_LVL_INFO, _("Slave I/O thread: connected to master '%s@%s:%d',"
 
1964
                                     "replication started in log '%s' at position %s"),
 
1965
                  mi->getUsername(), mi->getHostname(), mi->getPort(),
 
1966
                  IO_RPL_LOG_NAME,
 
1967
                  llstr(mi->getLogPosition(), 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
    */
1974
1973
    drizzle->net.max_packet_size= session->net.max_packet_size+= MAX_LOG_EVENT_HEADER;
1975
1974
  }
1976
1975
  else
1977
1976
  {
1978
 
    sql_print_information(_("Slave I/O thread killed while connecting to master"));
 
1977
    errmsg_printf(ERRMSG_LVL_INFO, _("Slave I/O thread killed while connecting to master"));
1979
1978
    goto err;
1980
1979
  }
1981
1980
 
1999
1998
      if (!check_io_slave_killed(session, mi, "Slave I/O thread killed "
2000
1999
                                 "while registering slave on master"))
2001
2000
      {
2002
 
        sql_print_error(_("Slave I/O thread couldn't register on master"));
 
2001
        errmsg_printf(ERRMSG_LVL_ERROR, _("Slave I/O thread couldn't register on master"));
2003
2002
        if (try_to_reconnect(session, drizzle, mi, &retry_count, suppress_warnings,
2004
2003
                             reconnect_messages[SLAVE_RECON_ACT_REG]))
2005
2004
          goto err;
2011
2010
    if (!retry_count_reg)
2012
2011
    {
2013
2012
      retry_count_reg++;
2014
 
      sql_print_information(_("Forcing to reconnect slave I/O thread"));
 
2013
      errmsg_printf(ERRMSG_LVL_INFO, _("Forcing to reconnect slave I/O thread"));
2015
2014
      if (try_to_reconnect(session, drizzle, mi, &retry_count, suppress_warnings,
2016
2015
                           reconnect_messages[SLAVE_RECON_ACT_REG]))
2017
2016
        goto err;
2024
2023
    session->set_proc_info("Requesting binlog dump");
2025
2024
    if (request_dump(drizzle, mi, &suppress_warnings))
2026
2025
    {
2027
 
      sql_print_error(_("Failed on request_dump()"));
 
2026
      errmsg_printf(ERRMSG_LVL_ERROR, _("Failed on request_dump()"));
2028
2027
      if (check_io_slave_killed(session, mi, _("Slave I/O thread killed while \
2029
 
requesting master dump")) ||
 
2028
                                               requesting master dump")) ||
2030
2029
          try_to_reconnect(session, drizzle, mi, &retry_count, suppress_warnings,
2031
2030
                           reconnect_messages[SLAVE_RECON_ACT_DUMP]))
2032
2031
        goto err;
2035
2034
    if (!retry_count_dump)
2036
2035
    {
2037
2036
      retry_count_dump++;
2038
 
      sql_print_information(_("Forcing to reconnect slave I/O thread"));
 
2037
      errmsg_printf(ERRMSG_LVL_INFO, _("Forcing to reconnect slave I/O thread"));
2039
2038
      if (try_to_reconnect(session, drizzle, mi, &retry_count, suppress_warnings,
2040
2039
                           reconnect_messages[SLAVE_RECON_ACT_DUMP]))
2041
2040
        goto err;
2054
2053
      session->set_proc_info(_("Waiting for master to send event"));
2055
2054
      event_len= read_event(drizzle, mi, &suppress_warnings);
2056
2055
      if (check_io_slave_killed(session, mi, _("Slave I/O thread killed while "
2057
 
                                           "reading event")))
 
2056
                                               "reading event")))
2058
2057
        goto err;
2059
2058
      if (!retry_count_event)
2060
2059
      {
2061
2060
        retry_count_event++;
2062
 
        sql_print_information(_("Forcing to reconnect slave I/O thread"));
 
2061
        errmsg_printf(ERRMSG_LVL_INFO, _("Forcing to reconnect slave I/O thread"));
2063
2062
        if (try_to_reconnect(session, drizzle, mi, &retry_count, suppress_warnings,
2064
2063
                             reconnect_messages[SLAVE_RECON_ACT_EVENT]))
2065
2064
          goto err;
2071
2070
        uint32_t drizzle_error_number= drizzle_errno(drizzle);
2072
2071
        switch (drizzle_error_number) {
2073
2072
        case CR_NET_PACKET_TOO_LARGE:
2074
 
          sql_print_error(_("Log entry on master is longer than "
2075
 
                            "max_allowed_packet (%u) on "
2076
 
                            "slave. If the entry is correct, restart the "
2077
 
                            "server with a higher value of "
2078
 
                            "max_allowed_packet"),
2079
 
                          session->variables.max_allowed_packet);
 
2073
          errmsg_printf(ERRMSG_LVL_ERROR, _("Log entry on master is longer than "
 
2074
                                            "max_allowed_packet (%u) on "
 
2075
                                            "slave. If the entry is correct, restart the "
 
2076
                                            "server with a higher value of "
 
2077
                                            "max_allowed_packet"),
 
2078
                        session->variables.max_allowed_packet);
2080
2079
          goto err;
2081
2080
        case ER_MASTER_FATAL_ERROR_READING_BINLOG:
2082
 
          sql_print_error(ER(drizzle_error_number), drizzle_error_number,
2083
 
                          drizzle_error(drizzle));
 
2081
          errmsg_printf(ERRMSG_LVL_ERROR, ER(drizzle_error_number), drizzle_error_number,
 
2082
                        drizzle_error(drizzle));
2084
2083
          goto err;
2085
2084
        case EE_OUTOFMEMORY:
2086
2085
        case ER_OUTOFMEMORY:
2087
 
          sql_print_error(
2088
 
       _("Stopping slave I/O thread due to out-of-memory error from master"));
 
2086
          errmsg_printf(ERRMSG_LVL_ERROR, 
 
2087
                        _("Stopping slave I/O thread due to out-of-memory error from master"));
2089
2088
          goto err;
2090
2089
        }
2091
2090
        if (try_to_reconnect(session, drizzle, mi, &retry_count, suppress_warnings,
2102
2101
      }
2103
2102
      if (mi->flush())
2104
2103
      {
2105
 
        sql_print_error(_("Failed to flush master info file"));
 
2104
        errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to flush master info file"));
2106
2105
        goto err;
2107
2106
      }
2108
2107
      /*
2122
2121
          !rli->ignore_log_space_limit)
2123
2122
        if (wait_for_relay_log_space(rli))
2124
2123
        {
2125
 
          sql_print_error(_("Slave I/O thread aborted while waiting for "
2126
 
                            "relay log space"));
 
2124
          errmsg_printf(ERRMSG_LVL_ERROR, _("Slave I/O thread aborted while waiting for "
 
2125
                                            "relay log space"));
2127
2126
          goto err;
2128
2127
        }
2129
2128
    }
2130
2129
  }
2131
2130
 
2132
 
// error = 0;
 
2131
  // error = 0;
2133
2132
err:
2134
 
// print the current replication position
2135
 
  sql_print_information(_("Slave I/O thread exiting, read up to log '%s', "
2136
 
                          "position %s"),
2137
 
                        IO_RPL_LOG_NAME, llstr(mi->getLogPosition(), llbuff));
 
2133
  // print the current replication position
 
2134
  errmsg_printf(ERRMSG_LVL_INFO, _("Slave I/O thread exiting, read up to log '%s', "
 
2135
                                   "position %s"),
 
2136
                IO_RPL_LOG_NAME, llstr(mi->getLogPosition(), llbuff));
2138
2137
  pthread_mutex_lock(&LOCK_thread_count);
2139
2138
  session->query = session->db = 0; // extra safety
2140
2139
  session->query_length= session->db_length= 0;
2217
2216
    */
2218
2217
    pthread_cond_broadcast(&rli->start_cond);
2219
2218
    pthread_mutex_unlock(&rli->run_lock);
2220
 
    sql_print_error(_("Failed during slave thread initialization"));
 
2219
    errmsg_printf(ERRMSG_LVL_ERROR, _("Failed during slave thread initialization"));
2221
2220
    goto err;
2222
2221
  }
2223
2222
  session->init_for_queries();
2261
2260
                         1 /*need data lock*/, &errmsg,
2262
2261
                         1 /*look for a description_event*/))
2263
2262
  {
2264
 
    sql_print_error(_("Error initializing relay log position: %s"),
2265
 
                    errmsg);
 
2263
    errmsg_printf(ERRMSG_LVL_ERROR, _("Error initializing relay log position: %s"),
 
2264
                  errmsg);
2266
2265
    goto err;
2267
2266
  }
2268
2267
  Session_CHECK_SENTRY(session);
2283
2282
  assert(rli->sql_session == session);
2284
2283
 
2285
2284
  if (global_system_variables.log_warnings)
2286
 
    sql_print_information(_("Slave SQL thread initialized, "
2287
 
                            "starting replication in log '%s' at "
2288
 
                            "position %s, relay log '%s' position: %s"),
2289
 
                            RPL_LOG_NAME,
2290
 
                          llstr(rli->group_master_log_pos,llbuff),
2291
 
                          rli->group_relay_log_name.c_str(),
2292
 
                          llstr(rli->group_relay_log_pos,llbuff1));
 
2285
    errmsg_printf(ERRMSG_LVL_INFO, _("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.c_str(),
 
2291
                  llstr(rli->group_relay_log_pos,llbuff1));
2293
2292
 
2294
2293
  /* execute init_slave variable */
2295
2294
  if (sys_init_slave.value_length)
2297
2296
    execute_init_command(session, &sys_init_slave, &LOCK_sys_init_slave);
2298
2297
    if (session->is_slave_error)
2299
2298
    {
2300
 
      sql_print_error(_("Slave SQL thread aborted. "
2301
 
                        "Can't execute init_slave query"));
 
2299
      errmsg_printf(ERRMSG_LVL_ERROR, _("Slave SQL thread aborted. "
 
2300
                                        "Can't execute init_slave query"));
2302
2301
      goto err;
2303
2302
    }
2304
2303
  }
2312
2311
      rli->is_until_satisfied(rli->group_master_log_pos))
2313
2312
  {
2314
2313
    char buf[22];
2315
 
    sql_print_information(_("Slave SQL thread stopped because it reached its"
2316
 
                            " UNTIL position %s"), llstr(rli->until_pos(), buf));
 
2314
    errmsg_printf(ERRMSG_LVL_INFO, _("Slave SQL thread stopped because it reached its"
 
2315
                                     " UNTIL position %s"), llstr(rli->until_pos(), buf));
2317
2316
    pthread_mutex_unlock(&rli->data_lock);
2318
2317
    goto err;
2319
2318
  }
2348
2347
          }
2349
2348
          else if (last_errno != session->main_da.sql_errno())
2350
2349
          {
2351
 
            sql_print_error(_("Slave (additional info): %s Error_code: %d"),
2352
 
                            errmsg, session->main_da.sql_errno());
 
2350
            errmsg_printf(ERRMSG_LVL_ERROR, _("Slave (additional info): %s Error_code: %d"),
 
2351
                          errmsg, session->main_da.sql_errno());
2353
2352
          }
2354
2353
        }
2355
2354
 
2365
2364
        {
2366
2365
          if (err->code == ER_CANT_OPEN_LIBRARY)
2367
2366
            udf_error = true;
2368
 
          sql_print_warning(_("Slave: %s Error_code: %d"),err->msg, err->code);
 
2367
          errmsg_printf(ERRMSG_LVL_WARN, _("Slave: %s Error_code: %d"),err->msg, err->code);
2369
2368
        }
2370
2369
        if (udf_error)
2371
 
          sql_print_error(_("Error loading user-defined library, slave SQL "
2372
 
                            "thread aborted. Install the missing library, "
2373
 
                            "and restart the slave SQL thread with "
2374
 
                            "\"SLAVE START\". We stopped at log '%s' "
2375
 
                            "position %s"),
2376
 
                          RPL_LOG_NAME, llstr(rli->group_master_log_pos,
2377
 
            llbuff));
 
2370
          errmsg_printf(ERRMSG_LVL_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,
 
2376
                                            llbuff));
2378
2377
        else
2379
 
          sql_print_error(_("Error running query, slave SQL thread aborted. "
2380
 
                            "Fix the problem, and restart "
2381
 
                            "the slave SQL thread with \"SLAVE START\". "
2382
 
                            "We stopped at log '%s' position %s"),
2383
 
                          RPL_LOG_NAME,
2384
 
                          llstr(rli->group_master_log_pos, llbuff));
 
2378
          errmsg_printf(ERRMSG_LVL_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));
2385
2384
      }
2386
2385
      goto err;
2387
2386
    }
2388
2387
  }
2389
2388
 
2390
2389
  /* Thread stopped. Print the current replication position to the log */
2391
 
  sql_print_information(_("Slave SQL thread exiting, replication stopped in "
2392
 
                          "log '%s' at position %s"),
2393
 
                        RPL_LOG_NAME,
2394
 
                        llstr(rli->group_master_log_pos,llbuff));
 
2390
  errmsg_printf(ERRMSG_LVL_INFO, _("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));
2395
2394
 
2396
 
 err:
 
2395
err:
2397
2396
 
2398
2397
  /*
2399
2398
    Some events set some playgrounds, which won't be cleared because thread
2441
2440
  Session_CHECK_SENTRY(session);
2442
2441
  delete session;
2443
2442
  pthread_mutex_unlock(&LOCK_thread_count);
2444
 
 /*
2445
 
  Note: the order of the broadcast and unlock calls below (first broadcast, then unlock)
2446
 
  is important. Otherwise a killer_thread can execute between the calls and
2447
 
  delete the mi structure leading to a crash! (see BUG#25306 for details)
 
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)
2448
2447
 */
2449
2448
  pthread_cond_broadcast(&rli->stop_cond);
2450
2449
  pthread_mutex_unlock(&rli->run_lock);  // tell the world we are done
2477
2476
 
2478
2477
  if (unlikely(net_request_file(net,cev->fname)))
2479
2478
  {
2480
 
    sql_print_error(_("Slave I/O: failed requesting download of '%s'"),
2481
 
                    cev->fname);
 
2479
    errmsg_printf(ERRMSG_LVL_ERROR, _("Slave I/O: failed requesting download of '%s'"),
 
2480
                  cev->fname);
2482
2481
    goto err;
2483
2482
  }
2484
2483
 
2494
2493
    {
2495
2494
      if (unlikely((num_bytes=my_net_read(net)) == packet_error))
2496
2495
      {
2497
 
        sql_print_error(_("Network read error downloading '%s' from master"),
2498
 
                        cev->fname);
 
2496
        errmsg_printf(ERRMSG_LVL_ERROR, _("Network read error downloading '%s' from master"),
 
2497
                      cev->fname);
2499
2498
        goto err;
2500
2499
      }
2501
2500
      if (unlikely(!num_bytes)) /* eof */
2502
2501
      {
2503
 
        /* 3.23 master wants it */
 
2502
        /* 3.23 master wants it */
2504
2503
        net_write_command(net, 0, (unsigned char*) "", 0, (unsigned char*) "", 0);
2505
2504
        /*
2506
2505
          If we wrote Create_file_log_event, then we need to write
2562
2561
  Start using a new binary log on the master
2563
2562
 
2564
2563
  SYNOPSIS
2565
 
    process_io_rotate()
2566
 
    mi                  master_info for the slave
2567
 
    rev                 The rotate log event read from the binary log
 
2564
  process_io_rotate()
 
2565
  mi                  master_info for the slave
 
2566
  rev                 The rotate log event read from the binary log
2568
2567
 
2569
2568
  DESCRIPTION
2570
 
    Updates the master info with the place in the next binary
2571
 
    log where we should start reading.
2572
 
    Rotate the relay log to avoid mixed-format relay logs.
 
2569
  Updates the master info with the place in the next binary
 
2570
  log where we should start reading.
 
2571
  Rotate the relay log to avoid mixed-format relay logs.
2573
2572
 
2574
2573
  NOTES
2575
 
    We assume we already locked mi->data_lock
 
2574
  We assume we already locked mi->data_lock
2576
2575
 
2577
2576
  RETURN VALUES
2578
 
    0           ok
2579
 
    1           Log event is illegal
 
2577
  0           ok
 
2578
  1           Log event is illegal
2580
2579
 
2581
2580
*/
2582
2581
 
2624
2623
  copied from DRIZZLE 4.0.
2625
2624
*/
2626
2625
static int32_t queue_binlog_ver_1_event(Master_info *mi, const char *buf,
2627
 
                           uint32_t event_len)
 
2626
                                        uint32_t event_len)
2628
2627
{
2629
2628
  const char *errmsg = 0;
2630
2629
  uint32_t inc_pos;
2666
2665
                                            mi->rli.relay_log.description_event_for_queue);
2667
2666
  if (unlikely(!ev))
2668
2667
  {
2669
 
    sql_print_error(_("Read invalid event from master: '%s', "
2670
 
                      "master could be corrupt but a more likely cause "
2671
 
                      "of this is a bug"),
2672
 
                    errmsg);
 
2668
    errmsg_printf(ERRMSG_LVL_ERROR, _("Read invalid event from master: '%s', "
 
2669
                                      "master could be corrupt but a more likely cause "
 
2670
                                      "of this is a bug"),
 
2671
                  errmsg);
2673
2672
    free((char*) tmp_buf);
2674
2673
    return(1);
2675
2674
  }
2697
2696
      CREATE_FILE_EVENT. This is because read_log_event() above has just
2698
2697
      transformed LOAD_EVENT into CREATE_FILE_EVENT.
2699
2698
    */
2700
 
  {
2701
 
    /* We come here when and only when tmp_buf != 0 */
2702
 
    assert(tmp_buf != 0);
2703
 
    inc_pos=event_len;
2704
 
    ev->log_pos+= inc_pos;
2705
 
    int32_t error = process_io_create_file(mi,(Create_file_log_event*)ev);
2706
 
    delete ev;
2707
 
    mi->incrementLogPosition(inc_pos);
2708
 
    pthread_mutex_unlock(&mi->data_lock);
2709
 
    free((char*)tmp_buf);
2710
 
    return(error);
2711
 
  }
 
2699
    {
 
2700
      /* We come here when and only when tmp_buf != 0 */
 
2701
      assert(tmp_buf != 0);
 
2702
      inc_pos=event_len;
 
2703
      ev->log_pos+= inc_pos;
 
2704
      int32_t error = process_io_create_file(mi,(Create_file_log_event*)ev);
 
2705
      delete ev;
 
2706
      mi->incrementLogPosition(inc_pos);
 
2707
      pthread_mutex_unlock(&mi->data_lock);
 
2708
      free((char*)tmp_buf);
 
2709
      return(error);
 
2710
    }
2712
2711
  default:
2713
2712
    inc_pos= event_len;
2714
2713
    break;
2717
2716
  {
2718
2717
    if (ev->log_pos)
2719
2718
      /*
2720
 
         Don't do it for fake Rotate events (see comment in
2721
 
      Log_event::Log_event(const char* buf...) in log_event.cc).
 
2719
        Don't do it for fake Rotate events (see comment in
 
2720
        Log_event::Log_event(const char* buf...) in log_event.cc).
2722
2721
      */
2723
2722
      ev->log_pos+= event_len; /* make log_pos be the pos of the end of the event */
2724
2723
    if (unlikely(rli->relay_log.append(ev)))
2740
2739
  from queue_binlog_ver_1_event(), with some affordable simplifications.
2741
2740
*/
2742
2741
static int32_t queue_binlog_ver_3_event(Master_info *mi, const char *buf,
2743
 
                           uint32_t event_len)
 
2742
                                        uint32_t event_len)
2744
2743
{
2745
2744
  const char *errmsg = 0;
2746
2745
  uint32_t inc_pos;
2752
2751
                                            mi->rli.relay_log.description_event_for_queue);
2753
2752
  if (unlikely(!ev))
2754
2753
  {
2755
 
    sql_print_error(_("Read invalid event from master: '%s', "
2756
 
                      "master could be corrupt but a more likely cause of "
2757
 
                      "this is a bug"),
2758
 
                    errmsg);
 
2754
    errmsg_printf(ERRMSG_LVL_ERROR, _("Read invalid event from master: '%s', "
 
2755
                                      "master could be corrupt but a more likely cause of "
 
2756
                                      "this is a bug"),
 
2757
                  errmsg);
2759
2758
    free((char*) tmp_buf);
2760
2759
    return(1);
2761
2760
  }
2797
2796
  (exactly, slave's) format. To do the conversion, we create a 5.0 event from
2798
2797
  the 3.23/4.0 bytes, then write this event to the relay log.
2799
2798
 
2800
 
  TODO:
2801
 
    Test this code before release - it has to be tested on a separate
2802
 
    setup with 3.23 master or 4.0 master
 
2799
TODO:
 
2800
Test this code before release - it has to be tested on a separate
 
2801
setup with 3.23 master or 4.0 master
2803
2802
*/
2804
2803
 
2805
2804
static int32_t queue_old_event(Master_info *mi, const char *buf,
2806
 
                           uint32_t event_len)
 
2805
                               uint32_t event_len)
2807
2806
{
2808
2807
  switch (mi->rli.relay_log.description_event_for_queue->binlog_version)
2809
2808
  {
2810
2809
  case 1:
2811
 
      return(queue_binlog_ver_1_event(mi,buf,event_len));
 
2810
    return(queue_binlog_ver_1_event(mi,buf,event_len));
2812
2811
  case 3:
2813
 
      return(queue_binlog_ver_3_event(mi,buf,event_len));
 
2812
    return(queue_binlog_ver_3_event(mi,buf,event_len));
2814
2813
  default: /* unsupported format; eg version 2 */
2815
2814
    return(1);
2816
2815
  }
2857
2856
    */
2858
2857
    goto err;
2859
2858
  case ROTATE_EVENT:
2860
 
  {
2861
 
    Rotate_log_event rev(buf,event_len,mi->rli.relay_log.description_event_for_queue);
2862
 
    if (unlikely(process_io_rotate(mi,&rev)))
2863
2859
    {
2864
 
      error= ER_SLAVE_RELAY_LOG_WRITE_FAILURE;
2865
 
      goto err;
 
2860
      Rotate_log_event rev(buf,event_len,mi->rli.relay_log.description_event_for_queue);
 
2861
      if (unlikely(process_io_rotate(mi,&rev)))
 
2862
      {
 
2863
        error= ER_SLAVE_RELAY_LOG_WRITE_FAILURE;
 
2864
        goto err;
 
2865
      }
 
2866
      /*
 
2867
        Now the I/O thread has just changed its mi->master_log_name, so
 
2868
        incrementing mi->master_log_pos is nonsense.
 
2869
      */
 
2870
      inc_pos= 0;
 
2871
      break;
2866
2872
    }
2867
 
    /*
2868
 
      Now the I/O thread has just changed its mi->master_log_name, so
2869
 
      incrementing mi->master_log_pos is nonsense.
2870
 
    */
2871
 
    inc_pos= 0;
2872
 
    break;
2873
 
  }
2874
2873
  case FORMAT_DESCRIPTION_EVENT:
2875
 
  {
2876
 
    /*
2877
 
      Create an event, and save it (when we rotate the relay log, we will have
2878
 
      to write this event again).
2879
 
    */
2880
 
    /*
2881
 
      We are the only thread which reads/writes description_event_for_queue.
2882
 
      The relay_log struct does not move (though some members of it can
2883
 
      change), so we needn't any lock (no rli->data_lock, no log lock).
2884
 
    */
2885
 
    Format_description_log_event* tmp;
2886
 
    const char* errmsg;
2887
 
    if (!(tmp= (Format_description_log_event*)
2888
 
          Log_event::read_log_event(buf, event_len, &errmsg,
2889
 
                                    mi->rli.relay_log.description_event_for_queue)))
2890
2874
    {
2891
 
      error= ER_SLAVE_RELAY_LOG_WRITE_FAILURE;
2892
 
      goto err;
 
2875
      /*
 
2876
        Create an event, and save it (when we rotate the relay log, we will have
 
2877
        to write this event again).
 
2878
      */
 
2879
      /*
 
2880
        We are the only thread which reads/writes description_event_for_queue.
 
2881
        The relay_log struct does not move (though some members of it can
 
2882
        change), so we needn't any lock (no rli->data_lock, no log lock).
 
2883
      */
 
2884
      Format_description_log_event* tmp;
 
2885
      const char* errmsg;
 
2886
      if (!(tmp= (Format_description_log_event*)
 
2887
            Log_event::read_log_event(buf, event_len, &errmsg,
 
2888
                                      mi->rli.relay_log.description_event_for_queue)))
 
2889
      {
 
2890
        error= ER_SLAVE_RELAY_LOG_WRITE_FAILURE;
 
2891
        goto err;
 
2892
      }
 
2893
      delete mi->rli.relay_log.description_event_for_queue;
 
2894
      mi->rli.relay_log.description_event_for_queue= tmp;
 
2895
      /*
 
2896
        Though this does some conversion to the slave's format, this will
 
2897
        preserve the master's binlog format version, and number of event types.
 
2898
      */
 
2899
      /*
 
2900
        If the event was not requested by the slave (the slave did not ask for
 
2901
        it), i.e. has end_log_pos=0, we do not increment mi->master_log_pos
 
2902
      */
 
2903
      inc_pos= uint4korr(buf+LOG_POS_OFFSET) ? event_len : 0;
2893
2904
    }
2894
 
    delete mi->rli.relay_log.description_event_for_queue;
2895
 
    mi->rli.relay_log.description_event_for_queue= tmp;
2896
 
    /*
2897
 
       Though this does some conversion to the slave's format, this will
2898
 
       preserve the master's binlog format version, and number of event types.
2899
 
    */
2900
 
    /*
2901
 
       If the event was not requested by the slave (the slave did not ask for
2902
 
       it), i.e. has end_log_pos=0, we do not increment mi->master_log_pos
2903
 
    */
2904
 
    inc_pos= uint4korr(buf+LOG_POS_OFFSET) ? event_len : 0;
2905
 
  }
2906
 
  break;
 
2905
    break;
2907
2906
 
2908
2907
  case HEARTBEAT_LOG_EVENT:
2909
 
  {
2910
 
    /*
2911
 
      HB (heartbeat) cannot come before RL (Relay)
2912
 
    */
2913
 
    char  llbuf[22];
2914
 
    Heartbeat_log_event hb(buf, event_len, mi->rli.relay_log.description_event_for_queue);
2915
 
    if (!hb.is_valid())
2916
 
    {
2917
 
      error= ER_SLAVE_HEARTBEAT_FAILURE;
2918
 
      error_msg.append(STRING_WITH_LEN("inconsistent heartbeat event content;"));
2919
 
      error_msg.append(STRING_WITH_LEN("the event's data: log_file_name "));
2920
 
      error_msg.append(hb.get_log_ident(), (uint32_t) strlen(hb.get_log_ident()));
2921
 
      error_msg.append(STRING_WITH_LEN(" log_pos "));
2922
 
      llstr(hb.log_pos, llbuf);
2923
 
      error_msg.append(llbuf, strlen(llbuf));
2924
 
      goto err;
2925
 
    }
2926
 
    mi->received_heartbeats++;
2927
 
    /*
2928
 
       compare local and event's versions of log_file, log_pos.
2929
 
 
2930
 
       Heartbeat is sent only after an event corresponding to the corrdinates
2931
 
       the heartbeat carries.
2932
 
       Slave can not have a difference in coordinates except in the only
2933
 
       special case when mi->master_log_name, master_log_pos have never
2934
 
       been updated by Rotate event i.e when slave does not have any history
2935
 
       with the master (and thereafter mi->master_log_pos is NULL).
2936
 
 
2937
 
       TODO: handling `when' for SHOW SLAVE STATUS' snds behind
2938
 
    */
2939
 
    if ((mi->setLogName(hb.get_log_ident()) && mi->getLogName() != NULL)
2940
 
        || mi->getLogPosition() != hb.log_pos)
2941
 
    {
2942
 
      /* missed events of heartbeat from the past */
2943
 
      error= ER_SLAVE_HEARTBEAT_FAILURE;
2944
 
      error_msg.append(STRING_WITH_LEN("heartbeat is not compatible with local info;"));
2945
 
      error_msg.append(STRING_WITH_LEN("the event's data: log_file_name "));
2946
 
      error_msg.append(hb.get_log_ident(), (uint32_t) strlen(hb.get_log_ident()));
2947
 
      error_msg.append(STRING_WITH_LEN(" log_pos "));
2948
 
      llstr(hb.log_pos, llbuf);
2949
 
      error_msg.append(llbuf, strlen(llbuf));
2950
 
      goto err;
2951
 
    }
2952
 
    goto skip_relay_logging;
2953
 
  }
2954
 
  break;
 
2908
    {
 
2909
      /*
 
2910
        HB (heartbeat) cannot come before RL (Relay)
 
2911
      */
 
2912
      char  llbuf[22];
 
2913
      Heartbeat_log_event hb(buf, event_len, mi->rli.relay_log.description_event_for_queue);
 
2914
      if (!hb.is_valid())
 
2915
      {
 
2916
        error= ER_SLAVE_HEARTBEAT_FAILURE;
 
2917
        error_msg.append(STRING_WITH_LEN("inconsistent heartbeat event content;"));
 
2918
        error_msg.append(STRING_WITH_LEN("the event's data: log_file_name "));
 
2919
        error_msg.append(hb.get_log_ident(), (uint32_t) strlen(hb.get_log_ident()));
 
2920
        error_msg.append(STRING_WITH_LEN(" log_pos "));
 
2921
        llstr(hb.log_pos, llbuf);
 
2922
        error_msg.append(llbuf, strlen(llbuf));
 
2923
        goto err;
 
2924
      }
 
2925
      mi->received_heartbeats++;
 
2926
      /*
 
2927
        compare local and event's versions of log_file, log_pos.
 
2928
 
 
2929
        Heartbeat is sent only after an event corresponding to the corrdinates
 
2930
        the heartbeat carries.
 
2931
        Slave can not have a difference in coordinates except in the only
 
2932
        special case when mi->master_log_name, master_log_pos have never
 
2933
        been updated by Rotate event i.e when slave does not have any history
 
2934
        with the master (and thereafter mi->master_log_pos is NULL).
 
2935
 
 
2936
TODO: handling `when' for SHOW SLAVE STATUS' snds behind
 
2937
    */
 
2938
      if ((mi->setLogName(hb.get_log_ident()) && mi->getLogName() != NULL)
 
2939
          || mi->getLogPosition() != hb.log_pos)
 
2940
      {
 
2941
        /* missed events of heartbeat from the past */
 
2942
        error= ER_SLAVE_HEARTBEAT_FAILURE;
 
2943
        error_msg.append(STRING_WITH_LEN("heartbeat is not compatible with local info;"));
 
2944
        error_msg.append(STRING_WITH_LEN("the event's data: log_file_name "));
 
2945
        error_msg.append(hb.get_log_ident(), (uint32_t) strlen(hb.get_log_ident()));
 
2946
        error_msg.append(STRING_WITH_LEN(" log_pos "));
 
2947
        llstr(hb.log_pos, llbuf);
 
2948
        error_msg.append(llbuf, strlen(llbuf));
 
2949
        goto err;
 
2950
      }
 
2951
      goto skip_relay_logging;
 
2952
    }
 
2953
    break;
2955
2954
 
2956
2955
  default:
2957
2956
    inc_pos= event_len;
2959
2958
  }
2960
2959
 
2961
2960
  /*
2962
 
     If this event is originating from this server, don't queue it.
2963
 
     We don't check this for 3.23 events because it's simpler like this; 3.23
2964
 
     will be filtered anyway by the SQL slave thread which also tests the
2965
 
     server id (we must also keep this test in the SQL thread, in case somebody
2966
 
     upgrades a 4.0 slave which has a not-filtered relay log).
 
2961
    If this event is originating from this server, don't queue it.
 
2962
    We don't check this for 3.23 events because it's simpler like this; 3.23
 
2963
    will be filtered anyway by the SQL slave thread which also tests the
 
2964
    server id (we must also keep this test in the SQL thread, in case somebody
 
2965
    upgrades a 4.0 slave which has a not-filtered relay log).
2967
2966
 
2968
 
     ANY event coming from ourselves can be ignored: it is obvious for queries;
2969
 
     for STOP_EVENT/ROTATE_EVENT/START_EVENT: these cannot come from ourselves
2970
 
     (--log-slave-updates would not log that) unless this slave is also its
2971
 
     direct master (an unsupported, useless setup!).
 
2967
    ANY event coming from ourselves can be ignored: it is obvious for queries;
 
2968
    for STOP_EVENT/ROTATE_EVENT/START_EVENT: these cannot come from ourselves
 
2969
    (--log-slave-updates would not log that) unless this slave is also its
 
2970
    direct master (an unsupported, useless setup!).
2972
2971
  */
2973
2972
 
2974
2973
  pthread_mutex_lock(log_lock);
3062
3061
  Try to connect until successful or slave killed
3063
3062
 
3064
3063
  SYNPOSIS
3065
 
    safe_connect()
3066
 
    session                 Thread handler for slave
3067
 
    DRIZZLE               DRIZZLE connection handle
3068
 
    mi                  Replication handle
 
3064
  safe_connect()
 
3065
  session                 Thread handler for slave
 
3066
  DRIZZLE               DRIZZLE connection handle
 
3067
  mi                  Replication handle
3069
3068
 
3070
3069
  RETURN
3071
 
    0   ok
3072
 
    #   Error
 
3070
  0   ok
 
3071
#   Error
3073
3072
*/
3074
3073
 
3075
3074
static int32_t safe_connect(Session* session, DRIZZLE *drizzle, Master_info* mi)
3080
3079
 
3081
3080
/*
3082
3081
  SYNPOSIS
3083
 
    connect_to_master()
 
3082
  connect_to_master()
3084
3083
 
3085
3084
  IMPLEMENTATION
3086
 
    Try to connect until successful or slave killed or we have retried
3087
 
    master_retry_count times
 
3085
  Try to connect until successful or slave killed or we have retried
 
3086
  master_retry_count times
3088
3087
*/
3089
3088
 
3090
3089
static int32_t connect_to_master(Session* session, DRIZZLE *drizzle, Master_info* mi,
3106
3105
  while (!(slave_was_killed = io_slave_killed(session,mi)) &&
3107
3106
         (reconnect ? drizzle_reconnect(drizzle) != 0 :
3108
3107
          drizzle_connect(drizzle, mi->getHostname(), mi->getUsername(), mi->getPassword(), 0,
3109
 
                             mi->getPort(), 0, client_flag) == 0))
 
3108
                          mi->getPort(), 0, client_flag) == 0))
3110
3109
  {
3111
3110
    /* Don't repeat last error */
3112
3111
    if ((int32_t)drizzle_errno(drizzle) != last_errno)
3140
3139
    if (reconnect)
3141
3140
    {
3142
3141
      if (!suppress_warnings && global_system_variables.log_warnings)
3143
 
        sql_print_information(_("Slave: connected to master '%s@%s:%d', "
3144
 
                                "replication resumed in log '%s' at "
3145
 
                                "position %s"), mi->getUsername(),
3146
 
                                mi->getHostname(), mi->getPort(),
3147
 
                                IO_RPL_LOG_NAME,
3148
 
                                llstr(mi->getLogPosition(),llbuff));
 
3142
        errmsg_printf(ERRMSG_LVL_INFO, _("Slave: connected to master '%s@%s:%d', "
 
3143
                                         "replication resumed in log '%s' at "
 
3144
                                         "position %s"), mi->getUsername(),
 
3145
                      mi->getHostname(), mi->getPort(),
 
3146
                      IO_RPL_LOG_NAME,
 
3147
                      llstr(mi->getLogPosition(),llbuff));
3149
3148
    }
3150
3149
  }
3151
3150
  drizzle->reconnect= 1;
3157
3156
  safe_reconnect()
3158
3157
 
3159
3158
  IMPLEMENTATION
3160
 
    Try to connect until successful or slave killed or we have retried
3161
 
    master_retry_count times
 
3159
  Try to connect until successful or slave killed or we have retried
 
3160
  master_retry_count times
3162
3161
*/
3163
3162
 
3164
3163
static int32_t safe_reconnect(Session* session, DRIZZLE *drizzle, Master_info* mi,
3165
 
                          bool suppress_warnings)
 
3164
                              bool suppress_warnings)
3166
3165
{
3167
3166
  return(connect_to_master(session, drizzle, mi, 1, suppress_warnings));
3168
3167
}
3173
3172
  relay log.
3174
3173
 
3175
3174
  SYNOPSIS
3176
 
    flush_relay_log_info()
3177
 
    rli                 Relay log information
 
3175
  flush_relay_log_info()
 
3176
  rli                 Relay log information
3178
3177
 
3179
3178
  NOTES
3180
 
    - As this is only called by the slave thread, we don't need to
3181
 
      have a lock on this.
3182
 
    - If there is an active transaction, then we don't update the position
3183
 
      in the relay log.  This is to ensure that we re-execute statements
3184
 
      if we die in the middle of an transaction that was rolled back.
3185
 
    - As a transaction never spans binary logs, we don't have to handle the
3186
 
      case where we do a relay-log-rotation in the middle of the transaction.
3187
 
      If this would not be the case, we would have to ensure that we
3188
 
      don't delete the relay log file where the transaction started when
3189
 
      we switch to a new relay log file.
 
3179
  - As this is only called by the slave thread, we don't need to
 
3180
  have a lock on this.
 
3181
  - If there is an active transaction, then we don't update the position
 
3182
  in the relay log.  This is to ensure that we re-execute statements
 
3183
  if we die in the middle of an transaction that was rolled back.
 
3184
  - As a transaction never spans binary logs, we don't have to handle the
 
3185
  case where we do a relay-log-rotation in the middle of the transaction.
 
3186
  If this would not be the case, we would have to ensure that we
 
3187
  don't delete the relay log file where the transaction started when
 
3188
  we switch to a new relay log file.
3190
3189
 
3191
3190
  TODO
3192
 
    - Change the log file information to a binary format to avoid calling
3193
 
      int64_t2str.
 
3191
  - Change the log file information to a binary format to avoid calling
 
3192
  int64_t2str.
3194
3193
 
3195
3194
  RETURN VALUES
3196
 
    0   ok
3197
 
    1   write error
 
3195
  0   ok
 
3196
  1   write error
3198
3197
*/
3199
3198
 
3200
3199
bool flush_relay_log_info(Relay_log_info* rli)
3257
3256
  {
3258
3257
    /*
3259
3258
      We can have two kinds of log reading:
3260
 
      hot_log:
3261
 
        rli->cur_log points at the IO_CACHE of relay_log, which
3262
 
        is actively being updated by the I/O thread. We need to be careful
3263
 
        in this case and make sure that we are not looking at a stale log that
3264
 
        has already been rotated. If it has been, we reopen the log.
 
3259
hot_log:
 
3260
rli->cur_log points at the IO_CACHE of relay_log, which
 
3261
is actively being updated by the I/O thread. We need to be careful
 
3262
in this case and make sure that we are not looking at a stale log that
 
3263
has already been rotated. If it has been, we reopen the log.
3265
3264
 
3266
 
      The other case is much simpler:
3267
 
        We just have a read only log that nobody else will be updating.
 
3265
The other case is much simpler:
 
3266
We just have a read only log that nobody else will be updating.
3268
3267
    */
3269
3268
    bool hot_log;
3270
3269
    if ((hot_log = (cur_log != &rli->cache_buf)))
3366
3365
        rli->last_master_timestamp= 0;
3367
3366
 
3368
3367
        assert(rli->relay_log.get_open_count() ==
3369
 
                    rli->cur_log_old_open_count);
 
3368
               rli->cur_log_old_open_count);
3370
3369
 
3371
3370
        if (rli->ign_master_log_name_end[0])
3372
3371
        {
3396
3395
          Possible deadlock :
3397
3396
          - the I/O thread has reached log_space_limit
3398
3397
          - the SQL thread has read all relay logs, but cannot purge for some
3399
 
          reason:
3400
 
            * it has already purged all logs except the current one
3401
 
            * there are other logs than the current one but they're involved in
3402
 
            a transaction that finishes in the current one (or is not finished)
3403
 
          Solution :
3404
 
          Wake up the possibly waiting I/O thread, and set a boolean asking
3405
 
          the I/O thread to temporarily ignore the log_space_limit
3406
 
          constraint, because we do not want the I/O thread to block because of
3407
 
          space (it's ok if it blocks for any other reason (e.g. because the
3408
 
          master does not send anything). Then the I/O thread stops waiting
3409
 
          and reads more events.
3410
 
          The SQL thread decides when the I/O thread should take log_space_limit
3411
 
          into account again : ignore_log_space_limit is reset to 0
3412
 
          in purge_first_log (when the SQL thread purges the just-read relay
3413
 
          log), and also when the SQL thread starts. We should also reset
3414
 
          ignore_log_space_limit to 0 when the user does RESET SLAVE, but in
3415
 
          fact, no need as RESET SLAVE requires that the slave
3416
 
          be stopped, and the SQL thread sets ignore_log_space_limit to 0 when
3417
 
          it stops.
 
3398
reason:
 
3399
         * it has already purged all logs except the current one
 
3400
         * there are other logs than the current one but they're involved in
 
3401
         a transaction that finishes in the current one (or is not finished)
 
3402
Solution :
 
3403
Wake up the possibly waiting I/O thread, and set a boolean asking
 
3404
the I/O thread to temporarily ignore the log_space_limit
 
3405
constraint, because we do not want the I/O thread to block because of
 
3406
space (it's ok if it blocks for any other reason (e.g. because the
 
3407
master does not send anything). Then the I/O thread stops waiting
 
3408
and reads more events.
 
3409
The SQL thread decides when the I/O thread should take log_space_limit
 
3410
into account again : ignore_log_space_limit is reset to 0
 
3411
in purge_first_log (when the SQL thread purges the just-read relay
 
3412
log), and also when the SQL thread starts. We should also reset
 
3413
ignore_log_space_limit to 0 when the user does RESET SLAVE, but in
 
3414
fact, no need as RESET SLAVE requires that the slave
 
3415
be stopped, and the SQL thread sets ignore_log_space_limit to 0 when
 
3416
it stops.
3418
3417
        */
3419
3418
        pthread_mutex_lock(&rli->log_space_lock);
3420
3419
        // prevent the I/O thread from blocking next times
3501
3500
      {
3502
3501
#ifdef EXTRA_DEBUG
3503
3502
        if (global_system_variables.log_warnings)
3504
 
          sql_print_information(_("next log '%s' is currently active"),
3505
 
                                rli->linfo.log_file_name);
 
3503
          errmsg_printf(ERRMSG_LVL_INFO, _("next log '%s' is currently active"),
 
3504
                        rli->linfo.log_file_name);
3506
3505
#endif
3507
3506
        rli->cur_log= cur_log= rli->relay_log.get_log_file();
3508
3507
        rli->cur_log_old_open_count= rli->relay_log.get_open_count();
3531
3530
      */
3532
3531
#ifdef EXTRA_DEBUG
3533
3532
      if (global_system_variables.log_warnings)
3534
 
        sql_print_information(_("next log '%s' is not active"),
3535
 
                              rli->linfo.log_file_name);
 
3533
        errmsg_printf(ERRMSG_LVL_INFO, _("next log '%s' is not active"),
 
3534
                      rli->linfo.log_file_name);
3536
3535
#endif
3537
3536
      // open_binlog() will check the magic header
3538
3537
      if ((rli->cur_log_fd=open_binlog(cur_log,rli->linfo.log_file_name,
3547
3546
      */
3548
3547
      if (hot_log)
3549
3548
        pthread_mutex_unlock(log_lock);
3550
 
      sql_print_error(_("Slave SQL thread: I/O error reading "
3551
 
                        "event(errno: %d  cur_log->error: %d)"),
3552
 
                      my_errno,cur_log->error);
 
3549
      errmsg_printf(ERRMSG_LVL_ERROR, _("Slave SQL thread: I/O error reading "
 
3550
                                        "event(errno: %d  cur_log->error: %d)"),
 
3551
                    my_errno,cur_log->error);
3553
3552
      // set read position to the beginning of the event
3554
3553
      my_b_seek(cur_log,rli->event_relay_log_pos);
3555
3554
      /* otherwise, we have had a partial read */
3559
3558
  }
3560
3559
  if (!errmsg && global_system_variables.log_warnings)
3561
3560
  {
3562
 
    sql_print_information(_("Error reading relay log event: %s"),
3563
 
                          _("slave SQL thread was killed"));
 
3561
    errmsg_printf(ERRMSG_LVL_INFO, _("Error reading relay log event: %s"),
 
3562
                  _("slave SQL thread was killed"));
3564
3563
    return(0);
3565
3564
  }
3566
3565
 
3567
3566
err:
3568
3567
  if (errmsg)
3569
 
    sql_print_error(_("Error reading relay log event: %s"), errmsg);
 
3568
    errmsg_printf(ERRMSG_LVL_ERROR, _("Error reading relay log event: %s"), errmsg);
3570
3569
  return(0);
3571
3570
}
3572
3571
 
3586
3585
  pthread_mutex_lock(&mi->run_lock);
3587
3586
 
3588
3587
  /*
3589
 
     We need to test inited because otherwise, new_file() will attempt to lock
3590
 
     LOCK_log, which may not be inited (if we're not a slave).
 
3588
    We need to test inited because otherwise, new_file() will attempt to lock
 
3589
    LOCK_log, which may not be inited (if we're not a slave).
3591
3590
  */
3592
3591
  if (!rli->inited)
3593
3592
  {
3618
3617
 
3619
3618
 
3620
3619
/**
3621
 
   Detects, based on master's version (as found in the relay log), if master
3622
 
   has a certain bug.
3623
 
   @param rli Relay_log_info which tells the master's version
3624
 
   @param bug_id Number of the bug as found in bugs.mysql.com
3625
 
   @param report bool report error message, default TRUE
3626
 
   @return true if master has the bug, FALSE if it does not.
 
3620
  Detects, based on master's version (as found in the relay log), if master
 
3621
  has a certain bug.
 
3622
  @param rli Relay_log_info which tells the master's version
 
3623
  @param bug_id Number of the bug as found in bugs.mysql.com
 
3624
  @param report bool report error message, default TRUE
 
3625
  @return true if master has the bug, FALSE if it does not.
3627
3626
*/
3628
3627
bool rpl_master_has_bug(Relay_log_info *rli, uint32_t bug_id, bool report)
3629
3628
{
3648
3647
       i < sizeof(versions_for_all_bugs)/sizeof(*versions_for_all_bugs);i++)
3649
3648
  {
3650
3649
    const unsigned char *introduced_in= versions_for_all_bugs[i].introduced_in,
3651
 
      *fixed_in= versions_for_all_bugs[i].fixed_in;
 
3650
          *fixed_in= versions_for_all_bugs[i].fixed_in;
3652
3651
    if ((versions_for_all_bugs[i].bug_id == bug_id) &&
3653
3652
        (memcmp(introduced_in, master_ver, 3) <= 0) &&
3654
3653
        (memcmp(fixed_in,      master_ver, 3) >  0))
3689
3688
}
3690
3689
 
3691
3690
/**
3692
 
   BUG#33029, For all 5.0 up to 5.0.58 exclusive, and 5.1 up to 5.1.12
3693
 
   exclusive, if one statement in a SP generated AUTO_INCREMENT value
3694
 
   by the top statement, all statements after it would be considered
3695
 
   generated AUTO_INCREMENT value by the top statement, and a
3696
 
   erroneous INSERT_ID value might be associated with these statement,
3697
 
   which could cause duplicate entry error and stop the slave.
 
3691
  BUG#33029, For all 5.0 up to 5.0.58 exclusive, and 5.1 up to 5.1.12
 
3692
  exclusive, if one statement in a SP generated AUTO_INCREMENT value
 
3693
  by the top statement, all statements after it would be considered
 
3694
  generated AUTO_INCREMENT value by the top statement, and a
 
3695
  erroneous INSERT_ID value might be associated with these statement,
 
3696
  which could cause duplicate entry error and stop the slave.
3698
3697
 
3699
 
   Detect buggy master to work around.
3700
 
 */
 
3698
  Detect buggy master to work around.
 
3699
*/
3701
3700
bool rpl_master_erroneous_autoinc(Session *session)
3702
3701
{
3703
3702
  if (active_mi && active_mi->rli.sql_session == session)