~ubuntu-branches/ubuntu/precise/mysql-5.5/precise-201203300109

« back to all changes in this revision

Viewing changes to sql/rpl_rli.h

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2011-11-08 11:31:13 UTC
  • Revision ID: package-import@ubuntu.com-20111108113113-3ulw01fvi4vn8m25
Tags: upstream-5.5.17
ImportĀ upstreamĀ versionĀ 5.5.17

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
 
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
 
15
 
 
16
#ifndef RPL_RLI_H
 
17
#define RPL_RLI_H
 
18
 
 
19
#include "rpl_tblmap.h"
 
20
#include "rpl_reporting.h"
 
21
#include "rpl_utility.h"
 
22
#include "log.h"                         /* LOG_INFO, MYSQL_BIN_LOG */
 
23
#include "sql_class.h"                   /* THD */
 
24
 
 
25
struct RPL_TABLE_LIST;
 
26
class Master_info;
 
27
extern uint sql_slave_skip_counter;
 
28
 
 
29
/****************************************************************************
 
30
 
 
31
  Replication SQL Thread
 
32
 
 
33
  Relay_log_info contains:
 
34
    - the current relay log
 
35
    - the current relay log offset
 
36
    - master log name
 
37
    - master log sequence corresponding to the last update
 
38
    - misc information specific to the SQL thread
 
39
 
 
40
  Relay_log_info is initialized from the slave.info file if such
 
41
  exists.  Otherwise, data members are intialized with defaults. The
 
42
  initialization is done with init_relay_log_info() call.
 
43
 
 
44
  The format of slave.info file:
 
45
 
 
46
  relay_log_name
 
47
  relay_log_pos
 
48
  master_log_name
 
49
  master_log_pos
 
50
 
 
51
  To clean up, call end_relay_log_info()
 
52
 
 
53
*****************************************************************************/
 
54
 
 
55
class Relay_log_info : public Slave_reporting_capability
 
56
{
 
57
public:
 
58
  /**
 
59
     Flags for the state of the replication.
 
60
   */
 
61
  enum enum_state_flag {
 
62
    /** The replication thread is inside a statement */
 
63
    IN_STMT,
 
64
 
 
65
    /** Flag counter.  Should always be last */
 
66
    STATE_FLAGS_COUNT
 
67
  };
 
68
 
 
69
  /*
 
70
    If flag set, then rli does not store its state in any info file.
 
71
    This is the case only when we execute BINLOG SQL commands inside
 
72
    a client, non-replication thread.
 
73
  */
 
74
  bool no_storage;
 
75
 
 
76
  /*
 
77
    If true, events with the same server id should be replicated. This
 
78
    field is set on creation of a relay log info structure by copying
 
79
    the value of ::replicate_same_server_id and can be overridden if
 
80
    necessary. For example of when this is done, check sql_binlog.cc,
 
81
    where the BINLOG statement can be used to execute "raw" events.
 
82
   */
 
83
  bool replicate_same_server_id;
 
84
 
 
85
  /*** The following variables can only be read when protect by data lock ****/
 
86
 
 
87
  /*
 
88
    info_fd - file descriptor of the info file. set only during
 
89
    initialization or clean up - safe to read anytime
 
90
    cur_log_fd - file descriptor of the current read  relay log
 
91
  */
 
92
  File info_fd,cur_log_fd;
 
93
 
 
94
  /*
 
95
    Protected with internal locks.
 
96
    Must get data_lock when resetting the logs.
 
97
  */
 
98
  MYSQL_BIN_LOG relay_log;
 
99
  LOG_INFO linfo;
 
100
 
 
101
  /*
 
102
   cur_log
 
103
     Pointer that either points at relay_log.get_log_file() or
 
104
     &rli->cache_buf, depending on whether the log is hot or there was
 
105
     the need to open a cold relay_log.
 
106
 
 
107
   cache_buf 
 
108
     IO_CACHE used when opening cold relay logs.
 
109
   */
 
110
  IO_CACHE cache_buf,*cur_log;
 
111
 
 
112
  /*
 
113
    Keeps track of the number of transactions that commits
 
114
    before fsyncing. The option --sync-relay-log-info determines 
 
115
    how many transactions should commit before fsyncing.
 
116
  */ 
 
117
  uint sync_counter;
 
118
 
 
119
  /*
 
120
    Identifies when the recovery process is going on.
 
121
    See sql/slave.cc:init_recovery for further details.
 
122
  */ 
 
123
  bool is_relay_log_recovery;
 
124
 
 
125
  /* The following variables are safe to read any time */
 
126
 
 
127
  /* IO_CACHE of the info file - set only during init or end */
 
128
  IO_CACHE info_file;
 
129
 
 
130
  /*
 
131
    When we restart slave thread we need to have access to the previously
 
132
    created temporary tables. Modified only on init/end and by the SQL
 
133
    thread, read only by SQL thread.
 
134
  */
 
135
  TABLE *save_temporary_tables;
 
136
 
 
137
  /*
 
138
    standard lock acquisition order to avoid deadlocks:
 
139
    run_lock, data_lock, relay_log.LOCK_log, relay_log.LOCK_index
 
140
  */
 
141
  mysql_mutex_t data_lock, run_lock;
 
142
 
 
143
  /*
 
144
    start_cond is broadcast when SQL thread is started
 
145
    stop_cond - when stopped
 
146
    data_cond - when data protected by data_lock changes
 
147
  */
 
148
  mysql_cond_t start_cond, stop_cond, data_cond;
 
149
 
 
150
  /* parent Master_info structure */
 
151
  Master_info *mi;
 
152
 
 
153
  /*
 
154
    Needed to deal properly with cur_log getting closed and re-opened with
 
155
    a different log under our feet
 
156
  */
 
157
  uint32 cur_log_old_open_count;
 
158
  
 
159
  /*
 
160
    Let's call a group (of events) :
 
161
      - a transaction
 
162
      or
 
163
      - an autocommiting query + its associated events (INSERT_ID,
 
164
    TIMESTAMP...)
 
165
    We need these rli coordinates :
 
166
    - relay log name and position of the beginning of the group we currently are
 
167
    executing. Needed to know where we have to restart when replication has
 
168
    stopped in the middle of a group (which has been rolled back by the slave).
 
169
    - relay log name and position just after the event we have just
 
170
    executed. This event is part of the current group.
 
171
    Formerly we only had the immediately above coordinates, plus a 'pending'
 
172
    variable, but this dealt wrong with the case of a transaction starting on a
 
173
    relay log and finishing (commiting) on another relay log. Case which can
 
174
    happen when, for example, the relay log gets rotated because of
 
175
    max_binlog_size.
 
176
  */
 
177
  char group_relay_log_name[FN_REFLEN];
 
178
  ulonglong group_relay_log_pos;
 
179
  char event_relay_log_name[FN_REFLEN];
 
180
  ulonglong event_relay_log_pos;
 
181
  ulonglong future_event_relay_log_pos;
 
182
 
 
183
#ifdef HAVE_purify
 
184
  bool is_fake; /* Mark that this is a fake relay log info structure */
 
185
#endif
 
186
 
 
187
  /* 
 
188
     Original log name and position of the group we're currently executing
 
189
     (whose coordinates are group_relay_log_name/pos in the relay log)
 
190
     in the master's binlog. These concern the *group*, because in the master's
 
191
     binlog the log_pos that comes with each event is the position of the
 
192
     beginning of the group.
 
193
  */
 
194
  char group_master_log_name[FN_REFLEN];
 
195
  volatile my_off_t group_master_log_pos;
 
196
 
 
197
  /*
 
198
    Handling of the relay_log_space_limit optional constraint.
 
199
    ignore_log_space_limit is used to resolve a deadlock between I/O and SQL
 
200
    threads, the SQL thread sets it to unblock the I/O thread and make it
 
201
    temporarily forget about the constraint.
 
202
  */
 
203
  ulonglong log_space_limit,log_space_total;
 
204
  bool ignore_log_space_limit;
 
205
 
 
206
  /*
 
207
    When it commits, InnoDB internally stores the master log position it has
 
208
    processed so far; the position to store is the one of the end of the
 
209
    committing event (the COMMIT query event, or the event if in autocommit
 
210
    mode).
 
211
  */
 
212
#if MYSQL_VERSION_ID < 40100
 
213
  ulonglong future_master_log_pos;
 
214
#else
 
215
  ulonglong future_group_master_log_pos;
 
216
#endif
 
217
 
 
218
  time_t last_master_timestamp;
 
219
 
 
220
  void clear_until_condition();
 
221
 
 
222
  /*
 
223
    Needed for problems when slave stops and we want to restart it
 
224
    skipping one or more events in the master log that have caused
 
225
    errors, and have been manually applied by DBA already.
 
226
  */
 
227
  volatile uint32 slave_skip_counter;
 
228
  volatile ulong abort_pos_wait;        /* Incremented on change master */
 
229
  volatile ulong slave_run_id;          /* Incremented on slave start */
 
230
  mysql_mutex_t log_space_lock;
 
231
  mysql_cond_t log_space_cond;
 
232
  THD * sql_thd;
 
233
#ifndef DBUG_OFF
 
234
  int events_till_abort;
 
235
#endif  
 
236
 
 
237
  /*
 
238
    inited changes its value within LOCK_active_mi-guarded critical
 
239
    sections  at times of start_slave_threads() (0->1) and end_slave() (1->0).
 
240
    Readers may not acquire the mutex while they realize potential concurrency
 
241
    issue.
 
242
    If not set, the value of other members of the structure are undefined.
 
243
  */
 
244
  volatile bool inited;
 
245
  volatile bool abort_slave;
 
246
  volatile uint slave_running;
 
247
 
 
248
  /* 
 
249
     Condition and its parameters from START SLAVE UNTIL clause.
 
250
     
 
251
     UNTIL condition is tested with is_until_satisfied() method that is 
 
252
     called by exec_relay_log_event(). is_until_satisfied() caches the result
 
253
     of the comparison of log names because log names don't change very often;
 
254
     this cache is invalidated by parts of code which change log names with
 
255
     notify_*_log_name_updated() methods. (They need to be called only if SQL
 
256
     thread is running).
 
257
   */
 
258
  
 
259
  enum {UNTIL_NONE= 0, UNTIL_MASTER_POS, UNTIL_RELAY_POS} until_condition;
 
260
  char until_log_name[FN_REFLEN];
 
261
  ulonglong until_log_pos;
 
262
  /* extension extracted from log_name and converted to int */
 
263
  ulong until_log_name_extension;   
 
264
  /* 
 
265
     Cached result of comparison of until_log_name and current log name
 
266
     -2 means unitialised, -1,0,1 are comarison results 
 
267
  */
 
268
  enum 
 
269
  { 
 
270
    UNTIL_LOG_NAMES_CMP_UNKNOWN= -2, UNTIL_LOG_NAMES_CMP_LESS= -1,
 
271
    UNTIL_LOG_NAMES_CMP_EQUAL= 0, UNTIL_LOG_NAMES_CMP_GREATER= 1
 
272
  } until_log_names_cmp_result;
 
273
 
 
274
  char cached_charset[6];
 
275
  /*
 
276
    trans_retries varies between 0 to slave_transaction_retries and counts how
 
277
    many times the slave has retried the present transaction; gets reset to 0
 
278
    when the transaction finally succeeds. retried_trans is a cumulative
 
279
    counter: how many times the slave has retried a transaction (any) since
 
280
    slave started.
 
281
  */
 
282
  ulong trans_retries, retried_trans;
 
283
 
 
284
  /*
 
285
    If the end of the hot relay log is made of master's events ignored by the
 
286
    slave I/O thread, these two keep track of the coords (in the master's
 
287
    binlog) of the last of these events seen by the slave I/O thread. If not,
 
288
    ign_master_log_name_end[0] == 0.
 
289
    As they are like a Rotate event read/written from/to the relay log, they
 
290
    are both protected by rli->relay_log.LOCK_log.
 
291
  */
 
292
  char ign_master_log_name_end[FN_REFLEN];
 
293
  ulonglong ign_master_log_pos_end;
 
294
 
 
295
  /* 
 
296
    Indentifies where the SQL Thread should create temporary files for the
 
297
    LOAD DATA INFILE. This is used for security reasons.
 
298
   */ 
 
299
  char slave_patternload_file[FN_REFLEN]; 
 
300
  size_t slave_patternload_file_size;  
 
301
 
 
302
  Relay_log_info(bool is_slave_recovery);
 
303
  ~Relay_log_info();
 
304
 
 
305
  /*
 
306
    Invalidate cached until_log_name and group_relay_log_name comparison 
 
307
    result. Should be called after any update of group_realy_log_name if
 
308
    there chances that sql_thread is running.
 
309
  */
 
310
  inline void notify_group_relay_log_name_update()
 
311
  {
 
312
    if (until_condition==UNTIL_RELAY_POS)
 
313
      until_log_names_cmp_result= UNTIL_LOG_NAMES_CMP_UNKNOWN;
 
314
  }
 
315
 
 
316
  /*
 
317
    The same as previous but for group_master_log_name. 
 
318
  */
 
319
  inline void notify_group_master_log_name_update()
 
320
  {
 
321
    if (until_condition==UNTIL_MASTER_POS)
 
322
      until_log_names_cmp_result= UNTIL_LOG_NAMES_CMP_UNKNOWN;
 
323
  }
 
324
  
 
325
  inline void inc_event_relay_log_pos()
 
326
  {
 
327
    event_relay_log_pos= future_event_relay_log_pos;
 
328
  }
 
329
 
 
330
  void inc_group_relay_log_pos(ulonglong log_pos,
 
331
                               bool skip_lock=0);
 
332
 
 
333
  int wait_for_pos(THD* thd, String* log_name, longlong log_pos, 
 
334
                   longlong timeout);
 
335
  void close_temporary_tables();
 
336
 
 
337
  /* Check if UNTIL condition is satisfied. See slave.cc for more. */
 
338
  bool is_until_satisfied(THD *thd, Log_event *ev);
 
339
  inline ulonglong until_pos()
 
340
  {
 
341
    return ((until_condition == UNTIL_MASTER_POS) ? group_master_log_pos :
 
342
            group_relay_log_pos);
 
343
  }
 
344
 
 
345
  RPL_TABLE_LIST *tables_to_lock;           /* RBR: Tables to lock  */
 
346
  uint tables_to_lock_count;        /* RBR: Count of tables to lock */
 
347
  table_mapping m_table_map;      /* RBR: Mapping table-id to table */
 
348
 
 
349
  bool get_table_data(TABLE *table_arg, table_def **tabledef_var, TABLE **conv_table_var) const
 
350
  {
 
351
    DBUG_ASSERT(tabledef_var && conv_table_var);
 
352
    for (TABLE_LIST *ptr= tables_to_lock ; ptr != NULL ; ptr= ptr->next_global)
 
353
      if (ptr->table == table_arg)
 
354
      {
 
355
        *tabledef_var= &static_cast<RPL_TABLE_LIST*>(ptr)->m_tabledef;
 
356
        *conv_table_var= static_cast<RPL_TABLE_LIST*>(ptr)->m_conv_table;
 
357
        DBUG_PRINT("debug", ("Fetching table data for table %s.%s:"
 
358
                             " tabledef: %p, conv_table: %p",
 
359
                             table_arg->s->db.str, table_arg->s->table_name.str,
 
360
                             *tabledef_var, *conv_table_var));
 
361
        return true;
 
362
      }
 
363
    return false;
 
364
  }
 
365
 
 
366
  /*
 
367
    Last charset (6 bytes) seen by slave SQL thread is cached here; it helps
 
368
    the thread save 3 get_charset() per Query_log_event if the charset is not
 
369
    changing from event to event (common situation).
 
370
    When the 6 bytes are equal to 0 is used to mean "cache is invalidated".
 
371
  */
 
372
  void cached_charset_invalidate();
 
373
  bool cached_charset_compare(char *charset) const;
 
374
 
 
375
  void cleanup_context(THD *, bool);
 
376
  void slave_close_thread_tables(THD *);
 
377
  void clear_tables_to_lock();
 
378
 
 
379
  /*
 
380
    Used to defer stopping the SQL thread to give it a chance
 
381
    to finish up the current group of events.
 
382
    The timestamp is set and reset in @c sql_slave_killed().
 
383
  */
 
384
  time_t last_event_start_time;
 
385
 
 
386
  /**
 
387
    Helper function to do after statement completion.
 
388
 
 
389
    This function is called from an event to complete the group by
 
390
    either stepping the group position, if the "statement" is not
 
391
    inside a transaction; or increase the event position, if the
 
392
    "statement" is inside a transaction.
 
393
 
 
394
    @param event_log_pos
 
395
    Master log position of the event. The position is recorded in the
 
396
    relay log info and used to produce information for <code>SHOW
 
397
    SLAVE STATUS</code>.
 
398
 
 
399
    @param event_creation_time
 
400
    Timestamp for the creation of the event on the master side. The
 
401
    time stamp is recorded in the relay log info and used to compute
 
402
    the <code>Seconds_behind_master</code> field.
 
403
  */
 
404
  void stmt_done(my_off_t event_log_pos,
 
405
                 time_t event_creation_time);
 
406
 
 
407
 
 
408
  /**
 
409
     Set the value of a replication state flag.
 
410
 
 
411
     @param flag Flag to set
 
412
   */
 
413
  void set_flag(enum_state_flag flag)
 
414
  {
 
415
    m_flags |= (1UL << flag);
 
416
  }
 
417
 
 
418
  /**
 
419
     Get the value of a replication state flag.
 
420
 
 
421
     @param flag Flag to get value of
 
422
 
 
423
     @return @c true if the flag was set, @c false otherwise.
 
424
   */
 
425
  bool get_flag(enum_state_flag flag)
 
426
  {
 
427
    return m_flags & (1UL << flag);
 
428
  }
 
429
 
 
430
  /**
 
431
     Clear the value of a replication state flag.
 
432
 
 
433
     @param flag Flag to clear
 
434
   */
 
435
  void clear_flag(enum_state_flag flag)
 
436
  {
 
437
    m_flags &= ~(1UL << flag);
 
438
  }
 
439
 
 
440
  /**
 
441
     Is the replication inside a group?
 
442
 
 
443
     Replication is inside a group if either:
 
444
     - The OPTION_BEGIN flag is set, meaning we're inside a transaction
 
445
     - The RLI_IN_STMT flag is set, meaning we're inside a statement
 
446
 
 
447
     @retval true Replication thread is currently inside a group
 
448
     @retval false Replication thread is currently not inside a group
 
449
   */
 
450
  bool is_in_group() const {
 
451
    return (sql_thd->variables.option_bits & OPTION_BEGIN) ||
 
452
      (m_flags & (1UL << IN_STMT));
 
453
  }
 
454
 
 
455
private:
 
456
  uint32 m_flags;
 
457
};
 
458
 
 
459
 
 
460
// Defined in rpl_rli.cc
 
461
int init_relay_log_info(Relay_log_info* rli, const char* info_fname);
 
462
 
 
463
 
 
464
#endif /* RPL_RLI_H */