~ubuntu-branches/ubuntu/lucid/mysql-dfsg-5.1/lucid-security

« back to all changes in this revision

Viewing changes to sql/slave.cc

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2012-02-22 22:33:55 UTC
  • mto: (1.2.1) (37.1.1 lucid-security)
  • mto: This revision was merged to the branch mainline in revision 36.
  • Revision ID: package-import@ubuntu.com-20120222223355-ku1tb4r70osci6v2
Tags: upstream-5.1.61
ImportĀ upstreamĀ versionĀ 5.1.61

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000-2003 MySQL AB
 
1
/*
 
2
   Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2
3
 
3
4
   This program is free software; you can redistribute it and/or modify
4
5
   it under the terms of the GNU General Public License as published by
11
12
 
12
13
   You should have received a copy of the GNU General Public License
13
14
   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
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 
16
*/
15
17
 
16
18
 
17
19
/**
44
46
#ifdef HAVE_REPLICATION
45
47
 
46
48
#include "rpl_tblmap.h"
 
49
#include "debug_sync.h"
47
50
 
48
51
#define FLAGSTR(V,F) ((V)&(F)?#F" ":"")
49
52
 
96
99
registration on master",
97
100
    "Reconnecting after a failed registration on master",
98
101
    "failed registering on master, reconnecting to try again, \
99
 
log '%s' at postion %s",
 
102
log '%s' at position %s",
100
103
    "COM_REGISTER_SLAVE",
101
104
    "Slave I/O thread killed during or after reconnect"
102
105
  },
104
107
    "Waiting to reconnect after a failed binlog dump request",
105
108
    "Slave I/O thread killed while retrying master dump",
106
109
    "Reconnecting after a failed binlog dump request",
107
 
    "failed dump request, reconnecting to try again, log '%s' at postion %s",
 
110
    "failed dump request, reconnecting to try again, log '%s' at position %s",
108
111
    "COM_BINLOG_DUMP",
109
112
    "Slave I/O thread killed during or after reconnect"
110
113
  },
113
116
    "Slave I/O thread killed while waiting to reconnect after a failed read",
114
117
    "Reconnecting after a failed master event read",
115
118
    "Slave I/O thread: Failed reading log event, reconnecting to retry, \
116
 
log '%s' at postion %s",
 
119
log '%s' at position %s",
117
120
    "",
118
121
    "Slave I/O thread killed during or after a reconnect done to recover from \
119
122
failed read"
407
410
  int error,force_all = (thread_mask & SLAVE_FORCE_ALL);
408
411
  pthread_mutex_t *sql_lock = &mi->rli.run_lock, *io_lock = &mi->run_lock;
409
412
 
 
413
  if (thread_mask & (SLAVE_SQL|SLAVE_FORCE_ALL))
 
414
  {
 
415
    DBUG_PRINT("info",("Terminating SQL thread"));
 
416
    mi->rli.abort_slave=1;
 
417
    if ((error=terminate_slave_thread(mi->rli.sql_thd, sql_lock,
 
418
                                      &mi->rli.stop_cond,
 
419
                                      &mi->rli.slave_running,
 
420
                                      skip_lock)) &&
 
421
        !force_all)
 
422
      DBUG_RETURN(error);
 
423
  }
410
424
  if (thread_mask & (SLAVE_IO|SLAVE_FORCE_ALL))
411
425
  {
412
426
    DBUG_PRINT("info",("Terminating IO thread"));
418
432
        !force_all)
419
433
      DBUG_RETURN(error);
420
434
  }
421
 
  if (thread_mask & (SLAVE_SQL|SLAVE_FORCE_ALL))
422
 
  {
423
 
    DBUG_PRINT("info",("Terminating SQL thread"));
424
 
    mi->rli.abort_slave=1;
425
 
    if ((error=terminate_slave_thread(mi->rli.sql_thd, sql_lock,
426
 
                                      &mi->rli.stop_cond,
427
 
                                      &mi->rli.slave_running,
428
 
                                      skip_lock)) &&
429
 
        !force_all)
430
 
      DBUG_RETURN(error);
431
 
  }
432
435
  DBUG_RETURN(0);
433
436
}
434
437
 
595
598
      DBUG_PRINT("sleep",("Waiting for slave thread to start"));
596
599
      const char* old_msg = thd->enter_cond(start_cond,cond_lock,
597
600
                                            "Waiting for slave thread to start");
598
 
      pthread_cond_wait(start_cond,cond_lock);
 
601
      pthread_cond_wait(start_cond, cond_lock);
599
602
      thd->exit_cond(old_msg);
600
603
      pthread_mutex_lock(cond_lock); // re-acquire it as exit_cond() released
601
604
      if (thd->killed)
 
605
      {
 
606
        if (start_lock)
 
607
          pthread_mutex_unlock(start_lock);
602
608
        DBUG_RETURN(thd->killed_errno());
 
609
      }
603
610
    }
604
611
  }
605
612
  if (start_lock)
739
746
  DBUG_ASSERT(rli->slave_running == 1);// tracking buffer overrun
740
747
  if (abort_loop || thd->killed || rli->abort_slave)
741
748
  {
 
749
    /*
 
750
      The transaction should always be binlogged if OPTION_KEEP_LOG is set
 
751
      (it implies that something can not be rolled back). And such case
 
752
      should be regarded similarly as modifing a non-transactional table
 
753
      because retrying of the transaction will lead to an error or inconsistency
 
754
      as well.
 
755
      Example: OPTION_KEEP_LOG is set if a temporary table is created or dropped.
 
756
    */
742
757
    if (rli->abort_slave && rli->is_in_group() &&
743
 
        thd->transaction.all.modified_non_trans_table)
 
758
        (thd->transaction.all.modified_non_trans_table ||
 
759
         (thd->options & OPTION_KEEP_LOG)))
744
760
      DBUG_RETURN(0);
745
761
    /*
746
762
      If we are in an unsafe situation (stopping could corrupt replication),
981
997
    unavailable (very old master not supporting UNIX_TIMESTAMP()?).
982
998
  */
983
999
 
984
 
  DBUG_SYNC_POINT("debug_lock.before_get_UNIX_TIMESTAMP", 10);
 
1000
  DBUG_EXECUTE_IF("dbug.before_get_UNIX_TIMESTAMP",
 
1001
                  {
 
1002
                    const char act[]=
 
1003
                      "now "
 
1004
                      "wait_for signal.get_unix_timestamp";
 
1005
                    DBUG_ASSERT(opt_debug_sync_timeout > 0);
 
1006
                    DBUG_ASSERT(!debug_sync_set_action(current_thd,
 
1007
                                                       STRING_WITH_LEN(act)));
 
1008
                  };);
 
1009
 
985
1010
  master_res= NULL;
986
1011
  if (!mysql_real_query(mysql, STRING_WITH_LEN("SELECT UNIX_TIMESTAMP()")) &&
987
1012
      (master_res= mysql_store_result(mysql)) &&
1020
1045
    Note: we could have put a @@SERVER_ID in the previous SELECT
1021
1046
    UNIX_TIMESTAMP() instead, but this would not have worked on 3.23 masters.
1022
1047
  */
1023
 
  DBUG_SYNC_POINT("debug_lock.before_get_SERVER_ID", 10);
 
1048
  DBUG_EXECUTE_IF("dbug.before_get_SERVER_ID",
 
1049
                  {
 
1050
                    const char act[]=
 
1051
                      "now "
 
1052
                      "wait_for signal.get_server_id";
 
1053
                    DBUG_ASSERT(opt_debug_sync_timeout > 0);
 
1054
                    DBUG_ASSERT(!debug_sync_set_action(current_thd, 
 
1055
                                                       STRING_WITH_LEN(act)));
 
1056
                  };);
1024
1057
  master_res= NULL;
1025
1058
  master_row= NULL;
1026
1059
  if (!mysql_real_query(mysql,
1462
1495
                   " to the relay log, SHOW SLAVE STATUS may be"
1463
1496
                   " inaccurate");
1464
1497
      rli->relay_log.harvest_bytes_written(&rli->log_space_total);
1465
 
      if (flush_master_info(mi, 1))
 
1498
      if (flush_master_info(mi, TRUE, TRUE))
1466
1499
        sql_print_error("Failed to flush master info file");
1467
1500
      delete ev;
1468
1501
    }
2095
2128
  DBUG_PRINT("info", ("thd->options: %s%s; rli->last_event_start_time: %lu",
2096
2129
                      FLAGSTR(thd->options, OPTION_NOT_AUTOCOMMIT),
2097
2130
                      FLAGSTR(thd->options, OPTION_BEGIN),
2098
 
                      rli->last_event_start_time));
 
2131
                      (ulong) rli->last_event_start_time));
2099
2132
 
2100
2133
  /*
2101
2134
    Execute the event to change the database and update the binary
2258
2291
      hits the UNTIL barrier.
2259
2292
    */
2260
2293
    if (rli->until_condition != Relay_log_info::UNTIL_NONE &&
2261
 
        rli->is_until_satisfied((rli->is_in_group() || !ev->log_pos) ?
2262
 
                                rli->group_master_log_pos :
2263
 
                                ev->log_pos - ev->data_written))
 
2294
        rli->is_until_satisfied(thd, ev))
2264
2295
    {
2265
2296
      char buf[22];
2266
2297
      sql_print_information("Slave SQL thread stopped because it reached its"
2296
2327
 
2297
2328
    if (slave_trans_retries)
2298
2329
    {
2299
 
      int temp_err;
 
2330
      int UNINIT_VAR(temp_err);
2300
2331
      if (exec_res && (temp_err= has_temporary_error(thd)))
2301
2332
      {
2302
2333
        const char *errmsg;
2327
2358
          else
2328
2359
          {
2329
2360
            exec_res= 0;
2330
 
            end_trans(thd, ROLLBACK);
 
2361
            rli->cleanup_context(thd, 1);
2331
2362
            /* chance for concurrent connection to get more locks */
2332
2363
            safe_sleep(thd, min(rli->trans_retries, MAX_SLAVE_RETRY_PAUSE),
2333
2364
                       (CHECK_KILLED_FUNC)sql_slave_killed, (void*)rli);
2504
2535
 
2505
2536
  thd= new THD; // note that contructor of THD uses DBUG_ !
2506
2537
  THD_CHECK_SENTRY(thd);
 
2538
  DBUG_ASSERT(mi->io_thd == 0);
2507
2539
  mi->io_thd = thd;
2508
2540
 
2509
2541
  pthread_detach_this_thread();
2559
2591
 
2560
2592
connected:
2561
2593
 
 
2594
    DBUG_EXECUTE_IF("dbug.before_get_running_status_yes",
 
2595
                    {
 
2596
                      const char act[]=
 
2597
                        "now "
 
2598
                        "wait_for signal.io_thread_let_running";
 
2599
                      DBUG_ASSERT(opt_debug_sync_timeout > 0);
 
2600
                      DBUG_ASSERT(!debug_sync_set_action(thd, 
 
2601
                                                         STRING_WITH_LEN(act)));
 
2602
                    };);
 
2603
 
2562
2604
  // TODO: the assignment below should be under mutex (5.0)
2563
2605
  mi->slave_running= MYSQL_SLAVE_RUN_CONNECT;
2564
2606
  thd->slave_net = &mysql->net;
2705
2747
                   "could not queue event from master");
2706
2748
        goto err;
2707
2749
      }
2708
 
      if (flush_master_info(mi, 1))
 
2750
      if (flush_master_info(mi, TRUE, TRUE))
2709
2751
      {
2710
2752
        sql_print_error("Failed to flush master info file");
2711
2753
        goto err;
2857
2899
{
2858
2900
  THD *thd;                     /* needs to be first for thread_stack */
2859
2901
  char llbuff[22],llbuff1[22];
 
2902
  char saved_log_name[FN_REFLEN];
 
2903
  char saved_master_log_name[FN_REFLEN];
 
2904
  my_off_t UNINIT_VAR(saved_log_pos);
 
2905
  my_off_t UNINIT_VAR(saved_master_log_pos);
 
2906
  my_off_t saved_skip= 0;
2860
2907
 
2861
2908
  Relay_log_info* rli = &((Master_info*)arg)->rli;
2862
2909
  const char *errmsg;
3002
3049
    do not want to wait for next event in this case.
3003
3050
  */
3004
3051
  pthread_mutex_lock(&rli->data_lock);
 
3052
  if (rli->slave_skip_counter)
 
3053
  {
 
3054
    strmake(saved_log_name, rli->group_relay_log_name, FN_REFLEN - 1);
 
3055
    strmake(saved_master_log_name, rli->group_master_log_name, FN_REFLEN - 1);
 
3056
    saved_log_pos= rli->group_relay_log_pos;
 
3057
    saved_master_log_pos= rli->group_master_log_pos;
 
3058
    saved_skip= rli->slave_skip_counter;
 
3059
  }
3005
3060
  if (rli->until_condition != Relay_log_info::UNTIL_NONE &&
3006
 
      rli->is_until_satisfied(rli->group_master_log_pos))
 
3061
      rli->is_until_satisfied(thd, NULL))
3007
3062
  {
3008
3063
    char buf[22];
3009
3064
    sql_print_information("Slave SQL thread stopped because it reached its"
3020
3075
    thd_proc_info(thd, "Reading event from the relay log");
3021
3076
    DBUG_ASSERT(rli->sql_thd == thd);
3022
3077
    THD_CHECK_SENTRY(thd);
 
3078
 
 
3079
    if (saved_skip && rli->slave_skip_counter == 0)
 
3080
    {
 
3081
      sql_print_information("'SQL_SLAVE_SKIP_COUNTER=%ld' executed at "
 
3082
        "relay_log_file='%s', relay_log_pos='%ld', master_log_name='%s', "
 
3083
        "master_log_pos='%ld' and new position at "
 
3084
        "relay_log_file='%s', relay_log_pos='%ld', master_log_name='%s', "
 
3085
        "master_log_pos='%ld' ",
 
3086
        (ulong) saved_skip, saved_log_name, (ulong) saved_log_pos,
 
3087
        saved_master_log_name, (ulong) saved_master_log_pos,
 
3088
        rli->group_relay_log_name, (ulong) rli->group_relay_log_pos,
 
3089
        rli->group_master_log_name, (ulong) rli->group_master_log_pos);
 
3090
      saved_skip= 0;
 
3091
    }
 
3092
    
3023
3093
    if (exec_relay_log_event(thd,rli))
3024
3094
    {
3025
3095
      DBUG_PRINT("info", ("exec_relay_log_event() failed"));
3104
3174
    request is detected only by the present function, not by events), so we
3105
3175
    must "proactively" clear playgrounds:
3106
3176
  */
 
3177
  thd->clear_error();
3107
3178
  rli->cleanup_context(thd, 1);
3108
3179
  /*
3109
3180
    Some extra safety, which should not been needed (normally, event deletion
3334
3405
    Rotate the relay log makes binlog format detection easier (at next slave
3335
3406
    start or mysqlbinlog)
3336
3407
  */
3337
 
  rotate_relay_log(mi); /* will take the right mutexes */
3338
 
  DBUG_RETURN(0);
 
3408
  DBUG_RETURN(rotate_relay_log(mi) /* will take the right mutexes */);
3339
3409
}
3340
3410
 
3341
3411
/*
4295
4365
        DBUG_ASSERT(rli->cur_log_fd == -1);
4296
4366
 
4297
4367
        /*
4298
 
          Read pointer has to be at the start since we are the only
4299
 
          reader.
4300
 
          We must keep the LOCK_log to read the 4 first bytes, as this is a hot
4301
 
          log (same as when we call read_log_event() above: for a hot log we
4302
 
          take the mutex).
 
4368
           When the SQL thread is [stopped and] (re)started the
 
4369
           following may happen:
 
4370
 
 
4371
           1. Log was hot at stop time and remains hot at restart
 
4372
 
 
4373
              SQL thread reads again from hot_log (SQL thread was
 
4374
              reading from the active log when it was stopped and the
 
4375
              very same log is still active on SQL thread restart).
 
4376
 
 
4377
              In this case, my_b_seek is performed on cur_log, while
 
4378
              cur_log points to relay_log.get_log_file();
 
4379
 
 
4380
           2. Log was hot at stop time but got cold before restart
 
4381
 
 
4382
              The log was hot when SQL thread stopped, but it is not
 
4383
              anymore when the SQL thread restarts.
 
4384
 
 
4385
              In this case, the SQL thread reopens the log, using
 
4386
              cache_buf, ie, cur_log points to &cache_buf, and thence
 
4387
              its coordinates are reset.
 
4388
 
 
4389
           3. Log was already cold at stop time
 
4390
 
 
4391
              The log was not hot when the SQL thread stopped, and, of
 
4392
              course, it will not be hot when it restarts.
 
4393
 
 
4394
              In this case, the SQL thread opens the cold log again,
 
4395
              using cache_buf, ie, cur_log points to &cache_buf, and
 
4396
              thence its coordinates are reset.
 
4397
 
 
4398
           4. Log was hot at stop time, DBA changes to previous cold
 
4399
              log and restarts SQL thread
 
4400
 
 
4401
              The log was hot when the SQL thread was stopped, but the
 
4402
              user changed the coordinates of the SQL thread to
 
4403
              restart from a previous cold log.
 
4404
 
 
4405
              In this case, at start time, cur_log points to a cold
 
4406
              log, opened using &cache_buf as cache, and coordinates
 
4407
              are reset. However, as it moves on to the next logs, it
 
4408
              will eventually reach the hot log. If the hot log is the
 
4409
              same at the time the SQL thread was stopped, then
 
4410
              coordinates were not reset - the cur_log will point to
 
4411
              relay_log.get_log_file(), and not a freshly opened
 
4412
              IO_CACHE through cache_buf. For this reason we need to
 
4413
              deploy a my_b_seek before calling check_binlog_magic at
 
4414
              this point of the code (see: BUG#55263 for more
 
4415
              details).
 
4416
          
 
4417
          NOTES: 
 
4418
            - We must keep the LOCK_log to read the 4 first bytes, as
 
4419
              this is a hot log (same as when we call read_log_event()
 
4420
              above: for a hot log we take the mutex).
 
4421
 
 
4422
            - Because of scenario #4 above, we need to have a
 
4423
              my_b_seek here. Otherwise, we might hit the assertion
 
4424
              inside check_binlog_magic.
4303
4425
        */
 
4426
 
 
4427
        my_b_seek(cur_log, (my_off_t) 0);
4304
4428
        if (check_binlog_magic(cur_log,&errmsg))
4305
4429
        {
4306
4430
          if (!hot_log) pthread_mutex_unlock(log_lock);
4364
4488
  is void).
4365
4489
*/
4366
4490
 
4367
 
void rotate_relay_log(Master_info* mi)
 
4491
int rotate_relay_log(Master_info* mi)
4368
4492
{
4369
4493
  DBUG_ENTER("rotate_relay_log");
4370
4494
  Relay_log_info* rli= &mi->rli;
4371
 
 
4372
 
  /* We don't lock rli->run_lock. This would lead to deadlocks. */
4373
 
  pthread_mutex_lock(&mi->run_lock);
 
4495
  int error= 0;
4374
4496
 
4375
4497
  /*
4376
4498
     We need to test inited because otherwise, new_file() will attempt to lock
4383
4505
  }
4384
4506
 
4385
4507
  /* If the relay log is closed, new_file() will do nothing. */
4386
 
  rli->relay_log.new_file();
 
4508
  if ((error= rli->relay_log.new_file()))
 
4509
    goto end;
4387
4510
 
4388
4511
  /*
4389
4512
    We harvest now, because otherwise BIN_LOG_HEADER_SIZE will not immediately
4400
4523
  */
4401
4524
  rli->relay_log.harvest_bytes_written(&rli->log_space_total);
4402
4525
end:
4403
 
  pthread_mutex_unlock(&mi->run_lock);
4404
 
  DBUG_VOID_RETURN;
 
4526
  DBUG_RETURN(error);
4405
4527
}
4406
4528
 
4407
4529