1
/* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
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.
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.
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 */
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 */
25
struct RPL_TABLE_LIST;
27
extern uint sql_slave_skip_counter;
29
/****************************************************************************
31
Replication SQL Thread
33
Relay_log_info contains:
34
- the current relay log
35
- the current relay log offset
37
- master log sequence corresponding to the last update
38
- misc information specific to the SQL thread
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.
44
The format of slave.info file:
51
To clean up, call end_relay_log_info()
53
*****************************************************************************/
55
class Relay_log_info : public Slave_reporting_capability
59
Flags for the state of the replication.
61
enum enum_state_flag {
62
/** The replication thread is inside a statement */
65
/** Flag counter. Should always be last */
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.
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.
83
bool replicate_same_server_id;
85
/*** The following variables can only be read when protect by data lock ****/
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
92
File info_fd,cur_log_fd;
95
Protected with internal locks.
96
Must get data_lock when resetting the logs.
98
MYSQL_BIN_LOG relay_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.
108
IO_CACHE used when opening cold relay logs.
110
IO_CACHE cache_buf,*cur_log;
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.
120
Identifies when the recovery process is going on.
121
See sql/slave.cc:init_recovery for further details.
123
bool is_relay_log_recovery;
125
/* The following variables are safe to read any time */
127
/* IO_CACHE of the info file - set only during init or end */
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.
135
TABLE *save_temporary_tables;
138
standard lock acquisition order to avoid deadlocks:
139
run_lock, data_lock, relay_log.LOCK_log, relay_log.LOCK_index
141
mysql_mutex_t data_lock, run_lock;
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
148
mysql_cond_t start_cond, stop_cond, data_cond;
150
/* parent Master_info structure */
154
Needed to deal properly with cur_log getting closed and re-opened with
155
a different log under our feet
157
uint32 cur_log_old_open_count;
160
Let's call a group (of events) :
163
- an autocommiting query + its associated events (INSERT_ID,
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
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;
184
bool is_fake; /* Mark that this is a fake relay log info structure */
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.
194
char group_master_log_name[FN_REFLEN];
195
volatile my_off_t group_master_log_pos;
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.
203
ulonglong log_space_limit,log_space_total;
204
bool ignore_log_space_limit;
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
212
#if MYSQL_VERSION_ID < 40100
213
ulonglong future_master_log_pos;
215
ulonglong future_group_master_log_pos;
218
time_t last_master_timestamp;
220
void clear_until_condition();
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.
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;
234
int events_till_abort;
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
242
If not set, the value of other members of the structure are undefined.
244
volatile bool inited;
245
volatile bool abort_slave;
246
volatile uint slave_running;
249
Condition and its parameters from START SLAVE UNTIL clause.
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
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;
265
Cached result of comparison of until_log_name and current log name
266
-2 means unitialised, -1,0,1 are comarison results
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;
274
char cached_charset[6];
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
282
ulong trans_retries, retried_trans;
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.
292
char ign_master_log_name_end[FN_REFLEN];
293
ulonglong ign_master_log_pos_end;
296
Indentifies where the SQL Thread should create temporary files for the
297
LOAD DATA INFILE. This is used for security reasons.
299
char slave_patternload_file[FN_REFLEN];
300
size_t slave_patternload_file_size;
302
Relay_log_info(bool is_slave_recovery);
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.
310
inline void notify_group_relay_log_name_update()
312
if (until_condition==UNTIL_RELAY_POS)
313
until_log_names_cmp_result= UNTIL_LOG_NAMES_CMP_UNKNOWN;
317
The same as previous but for group_master_log_name.
319
inline void notify_group_master_log_name_update()
321
if (until_condition==UNTIL_MASTER_POS)
322
until_log_names_cmp_result= UNTIL_LOG_NAMES_CMP_UNKNOWN;
325
inline void inc_event_relay_log_pos()
327
event_relay_log_pos= future_event_relay_log_pos;
330
void inc_group_relay_log_pos(ulonglong log_pos,
333
int wait_for_pos(THD* thd, String* log_name, longlong log_pos,
335
void close_temporary_tables();
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()
341
return ((until_condition == UNTIL_MASTER_POS) ? group_master_log_pos :
342
group_relay_log_pos);
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 */
349
bool get_table_data(TABLE *table_arg, table_def **tabledef_var, TABLE **conv_table_var) const
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)
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));
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".
372
void cached_charset_invalidate();
373
bool cached_charset_compare(char *charset) const;
375
void cleanup_context(THD *, bool);
376
void slave_close_thread_tables(THD *);
377
void clear_tables_to_lock();
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().
384
time_t last_event_start_time;
387
Helper function to do after statement completion.
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.
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
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.
404
void stmt_done(my_off_t event_log_pos,
405
time_t event_creation_time);
409
Set the value of a replication state flag.
411
@param flag Flag to set
413
void set_flag(enum_state_flag flag)
415
m_flags |= (1UL << flag);
419
Get the value of a replication state flag.
421
@param flag Flag to get value of
423
@return @c true if the flag was set, @c false otherwise.
425
bool get_flag(enum_state_flag flag)
427
return m_flags & (1UL << flag);
431
Clear the value of a replication state flag.
433
@param flag Flag to clear
435
void clear_flag(enum_state_flag flag)
437
m_flags &= ~(1UL << flag);
441
Is the replication inside a group?
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
447
@retval true Replication thread is currently inside a group
448
@retval false Replication thread is currently not inside a group
450
bool is_in_group() const {
451
return (sql_thd->variables.option_bits & OPTION_BEGIN) ||
452
(m_flags & (1UL << IN_STMT));
460
// Defined in rpl_rli.cc
461
int init_relay_log_info(Relay_log_info* rli, const char* info_fname);
464
#endif /* RPL_RLI_H */