~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/rpl_rli.h

Merged in latest plugin-slot-reorg.

Show diffs side-by-side

added added

removed removed

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