~linuxjedi/drizzle/trunk-bug-667053

« back to all changes in this revision

Viewing changes to sql/rpl_rli.h

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

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