~ubuntu-branches/ubuntu/maverick/mysql-5.1/maverick-proposed

« 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 14:16:05 UTC
  • mto: This revision was merged to the branch mainline in revision 20.
  • Revision ID: package-import@ubuntu.com-20120222141605-nxlu9yzc6attylc2
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
/**
97
99
registration on master",
98
100
    "Reconnecting after a failed registration on master",
99
101
    "failed registering on master, reconnecting to try again, \
100
 
log '%s' at postion %s",
 
102
log '%s' at position %s",
101
103
    "COM_REGISTER_SLAVE",
102
104
    "Slave I/O thread killed during or after reconnect"
103
105
  },
105
107
    "Waiting to reconnect after a failed binlog dump request",
106
108
    "Slave I/O thread killed while retrying master dump",
107
109
    "Reconnecting after a failed binlog dump request",
108
 
    "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",
109
111
    "COM_BINLOG_DUMP",
110
112
    "Slave I/O thread killed during or after reconnect"
111
113
  },
114
116
    "Slave I/O thread killed while waiting to reconnect after a failed read",
115
117
    "Reconnecting after a failed master event read",
116
118
    "Slave I/O thread: Failed reading log event, reconnecting to retry, \
117
 
log '%s' at postion %s",
 
119
log '%s' at position %s",
118
120
    "",
119
121
    "Slave I/O thread killed during or after a reconnect done to recover from \
120
122
failed read"
408
410
  int error,force_all = (thread_mask & SLAVE_FORCE_ALL);
409
411
  pthread_mutex_t *sql_lock = &mi->rli.run_lock, *io_lock = &mi->run_lock;
410
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
  }
411
424
  if (thread_mask & (SLAVE_IO|SLAVE_FORCE_ALL))
412
425
  {
413
426
    DBUG_PRINT("info",("Terminating IO thread"));
419
432
        !force_all)
420
433
      DBUG_RETURN(error);
421
434
  }
422
 
  if (thread_mask & (SLAVE_SQL|SLAVE_FORCE_ALL))
423
 
  {
424
 
    DBUG_PRINT("info",("Terminating SQL thread"));
425
 
    mi->rli.abort_slave=1;
426
 
    if ((error=terminate_slave_thread(mi->rli.sql_thd, sql_lock,
427
 
                                      &mi->rli.stop_cond,
428
 
                                      &mi->rli.slave_running,
429
 
                                      skip_lock)) &&
430
 
        !force_all)
431
 
      DBUG_RETURN(error);
432
 
  }
433
435
  DBUG_RETURN(0);
434
436
}
435
437
 
596
598
      DBUG_PRINT("sleep",("Waiting for slave thread to start"));
597
599
      const char* old_msg = thd->enter_cond(start_cond,cond_lock,
598
600
                                            "Waiting for slave thread to start");
599
 
      pthread_cond_wait(start_cond,cond_lock);
 
601
      pthread_cond_wait(start_cond, cond_lock);
600
602
      thd->exit_cond(old_msg);
601
603
      pthread_mutex_lock(cond_lock); // re-acquire it as exit_cond() released
602
604
      if (thd->killed)
 
605
      {
 
606
        if (start_lock)
 
607
          pthread_mutex_unlock(start_lock);
603
608
        DBUG_RETURN(thd->killed_errno());
 
609
      }
604
610
    }
605
611
  }
606
612
  if (start_lock)
740
746
  DBUG_ASSERT(rli->slave_running == 1);// tracking buffer overrun
741
747
  if (abort_loop || thd->killed || rli->abort_slave)
742
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
    */
743
757
    if (rli->abort_slave && rli->is_in_group() &&
744
 
        thd->transaction.all.modified_non_trans_table)
 
758
        (thd->transaction.all.modified_non_trans_table ||
 
759
         (thd->options & OPTION_KEEP_LOG)))
745
760
      DBUG_RETURN(0);
746
761
    /*
747
762
      If we are in an unsafe situation (stopping could corrupt replication),
2113
2128
  DBUG_PRINT("info", ("thd->options: %s%s; rli->last_event_start_time: %lu",
2114
2129
                      FLAGSTR(thd->options, OPTION_NOT_AUTOCOMMIT),
2115
2130
                      FLAGSTR(thd->options, OPTION_BEGIN),
2116
 
                      rli->last_event_start_time));
 
2131
                      (ulong) rli->last_event_start_time));
2117
2132
 
2118
2133
  /*
2119
2134
    Execute the event to change the database and update the binary
2312
2327
 
2313
2328
    if (slave_trans_retries)
2314
2329
    {
2315
 
      int temp_err;
 
2330
      int UNINIT_VAR(temp_err);
2316
2331
      if (exec_res && (temp_err= has_temporary_error(thd)))
2317
2332
      {
2318
2333
        const char *errmsg;
2343
2358
          else
2344
2359
          {
2345
2360
            exec_res= 0;
2346
 
            end_trans(thd, ROLLBACK);
 
2361
            rli->cleanup_context(thd, 1);
2347
2362
            /* chance for concurrent connection to get more locks */
2348
2363
            safe_sleep(thd, min(rli->trans_retries, MAX_SLAVE_RETRY_PAUSE),
2349
2364
                       (CHECK_KILLED_FUNC)sql_slave_killed, (void*)rli);
2520
2535
 
2521
2536
  thd= new THD; // note that contructor of THD uses DBUG_ !
2522
2537
  THD_CHECK_SENTRY(thd);
 
2538
  DBUG_ASSERT(mi->io_thd == 0);
2523
2539
  mi->io_thd = thd;
2524
2540
 
2525
2541
  pthread_detach_this_thread();
2885
2901
  char llbuff[22],llbuff1[22];
2886
2902
  char saved_log_name[FN_REFLEN];
2887
2903
  char saved_master_log_name[FN_REFLEN];
2888
 
  my_off_t saved_log_pos;
2889
 
  my_off_t saved_master_log_pos;
 
2904
  my_off_t UNINIT_VAR(saved_log_pos);
 
2905
  my_off_t UNINIT_VAR(saved_master_log_pos);
2890
2906
  my_off_t saved_skip= 0;
2891
2907
 
2892
2908
  Relay_log_info* rli = &((Master_info*)arg)->rli;
3035
3051
  pthread_mutex_lock(&rli->data_lock);
3036
3052
  if (rli->slave_skip_counter)
3037
3053
  {
3038
 
    char *pos;
3039
 
    pos= strmake(saved_log_name, rli->group_relay_log_name, FN_REFLEN - 1);
3040
 
    pos= '\0';
3041
 
    pos= strmake(saved_master_log_name, rli->group_master_log_name, FN_REFLEN - 1);
3042
 
    pos= '\0';
 
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);
3043
3056
    saved_log_pos= rli->group_relay_log_pos;
3044
3057
    saved_master_log_pos= rli->group_master_log_pos;
3045
3058
    saved_skip= rli->slave_skip_counter;
3161
3174
    request is detected only by the present function, not by events), so we
3162
3175
    must "proactively" clear playgrounds:
3163
3176
  */
 
3177
  thd->clear_error();
3164
3178
  rli->cleanup_context(thd, 1);
3165
3179
  /*
3166
3180
    Some extra safety, which should not been needed (normally, event deletion
3391
3405
    Rotate the relay log makes binlog format detection easier (at next slave
3392
3406
    start or mysqlbinlog)
3393
3407
  */
3394
 
  rotate_relay_log(mi); /* will take the right mutexes */
3395
 
  DBUG_RETURN(0);
 
3408
  DBUG_RETURN(rotate_relay_log(mi) /* will take the right mutexes */);
3396
3409
}
3397
3410
 
3398
3411
/*
4352
4365
        DBUG_ASSERT(rli->cur_log_fd == -1);
4353
4366
 
4354
4367
        /*
4355
 
          Read pointer has to be at the start since we are the only
4356
 
          reader.
4357
 
          We must keep the LOCK_log to read the 4 first bytes, as this is a hot
4358
 
          log (same as when we call read_log_event() above: for a hot log we
4359
 
          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.
4360
4425
        */
 
4426
 
 
4427
        my_b_seek(cur_log, (my_off_t) 0);
4361
4428
        if (check_binlog_magic(cur_log,&errmsg))
4362
4429
        {
4363
4430
          if (!hot_log) pthread_mutex_unlock(log_lock);
4421
4488
  is void).
4422
4489
*/
4423
4490
 
4424
 
void rotate_relay_log(Master_info* mi)
 
4491
int rotate_relay_log(Master_info* mi)
4425
4492
{
4426
4493
  DBUG_ENTER("rotate_relay_log");
4427
4494
  Relay_log_info* rli= &mi->rli;
4428
 
 
4429
 
  /* We don't lock rli->run_lock. This would lead to deadlocks. */
4430
 
  pthread_mutex_lock(&mi->run_lock);
 
4495
  int error= 0;
4431
4496
 
4432
4497
  /*
4433
4498
     We need to test inited because otherwise, new_file() will attempt to lock
4440
4505
  }
4441
4506
 
4442
4507
  /* If the relay log is closed, new_file() will do nothing. */
4443
 
  rli->relay_log.new_file();
 
4508
  if ((error= rli->relay_log.new_file()))
 
4509
    goto end;
4444
4510
 
4445
4511
  /*
4446
4512
    We harvest now, because otherwise BIN_LOG_HEADER_SIZE will not immediately
4457
4523
  */
4458
4524
  rli->relay_log.harvest_bytes_written(&rli->log_space_total);
4459
4525
end:
4460
 
  pthread_mutex_unlock(&mi->run_lock);
4461
 
  DBUG_VOID_RETURN;
 
4526
  DBUG_RETURN(error);
4462
4527
}
4463
4528
 
4464
4529