Debian Codegraph @

bin/filter_mysqlbinlog

raw
1 #!/usr/bin/env perl
2
3 # Copyright (C) 2011 DeNA Co.,Ltd.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
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.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
20 use strict;
21 use warnings FATAL => 'all';
22
23 use English qw(-no_match_vars);
24 use Getopt::Long;
25 use Pod::Usage;
26 use MHA::NodeConst;
27
28 GetOptions(
29 \my %opt, qw/
30 help
31 version
32 /,
33 ) or pod2usage(1);
34
35 if ( $opt{help} ) {
36 pod2usage(0);
37 }
38
39 if ( $opt{version} ) {
40 print "filter_mysqlbinlog version $MHA::NodeConst::VERSION.\n";
41 exit 0;
42 }
43
44 my $line;
45 my @rows_buf = ();
46 my $parsed_events = 0;
47
48 sub read_line() {
49 if ( $line = <STDIN> ) {
50 push @rows_buf, $line;
51 return $line;
52 }
53 else {
54 return;
55 }
56 }
57
58 sub print_rows() {
59 foreach (@rows_buf) {
60 print $_;
61 }
62 @rows_buf = ();
63 return 0;
64 }
65
66 while ( $line = read_line() ) {
67
68 if ( $line =~ m/^# at \d+\n$/ ) {
69 $parsed_events++;
70 do {
71 read_line();
72 } while ( ( $line =~ m/^# at \d+\n$/ ) && !print_rows() );
73
74 # Remove ROLLBACK statement and equivalent BINLOG statement in a START event.
75 # START event should show up at the beginning of the binary/relay logs,
76 # so ignore it if enough binlog events are already parsed.
77 if ( $line =~ m/^#.*?Start: binlog v.*?created.*\n$/
78 && $parsed_events < 10 )
79 {
80 read_line();
81 if ( $line =~ m/^# Warning: this binlog .*\n$/ ) {
82 read_line();
83 }
84 if ( $line =~ m/^ROLLBACK.*\n$/ ) {
85 pop @rows_buf;
86 push @rows_buf,
87 "# ROLLBACK and BINLOG events generated by start_event were trimmed.\n";
88 read_line();
89 while ( !( $line =~ m/(^# at \d+\n$)|(^DELIMITER ;\n$)/ ) ) {
90 pop @rows_buf;
91 read_line();
92 }
93 }
94 }
95 print_rows();
96 }
97
98 # Trim a tail ROLLBACK statement. mysqlbinlog adds the following statements
99 # at the tail of the output, if neither --disable-log-bin nor --set-charset
100 # was set.
101 # # End of log file
102 # ROLLBACK /* added by mysqlbinlog */;
103 # /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
104 elsif ( $line =~ m/^# End of log file\n$/ ) {
105 read_line();
106 if ( $line =~ m/^ROLLBACK \/\* added by mysqlbinlog \*\/;\n$/ ) {
107 read_line();
108 if ( $line =~
109 m/^\/\*!50003 SET COMPLETION_TYPE=\@OLD_COMPLETION_TYPE\*\/;\n$/ )
110 {
111 if ( !read_line() ) {
112 pop @rows_buf;
113 pop @rows_buf;
114 push @rows_buf,
115 "# Tail ROLLBACK event generated by mysqlbinlog was trimmed.\n";
116 goto EOF;
117 }
118 }
119 }
120 print_rows();
121 }
122 else {
123 print_rows();
124 }
125 }
126
127 EOF:
128 print_rows();
129
130 exit 0;
131
132 # ############################################################################
133 # Documentation
134 # ############################################################################
135
136 =pod
137
138 =head1 NAME
139
140 filter_mysqlbinlog - Trimming ROLLBACK statements and equivalent BINLOG events added by mysqlbinlog. This script is now obsolete.
141
142 =head1 SYNOPSIS
143
144 mysqlbinlog binary_or_relay_log_file | filter_mysqlbinlog
145
146 Note that this script is now obsolete and not used by MHA by default.
147
148 =head1 DESCRIPTION
149
150 mysqlbinlog command provided by Oracle implicitly adds ROLLBACK statements and equivalent BINLOG events. But this causes problems when recovering slave servers. To recover slaves, MHA might need to apply the following binlog events.
151 1) Relay log events from Relay_Log_Pos to the end of the relay log file
152 2) Differential relay log events from the latest slave
153 3) Differential binary log events from the dead master
154 mysqlbinlog command needs to be executed on these files separately. If a transaction does not end by 1) or 2), implicit ROLLBACK event rolls back the transaction, which will result in inconsistency.
155 filter_mysqlbinlog is a tool to fix this issue.
156 Note that ROLLBACK statements themselves are added in usual situations. For example, when you execute 1. BEGIN; 2. Updating transactional tables 3. Updating non-transactional tables 4. ROLLBACK, a ROLLBACK statement is written to the binary log to rollback transactional queries. This is normal situation so filter_mysqlbinlog must not remove all ROLLBACK events.
157
158 Note that this script is now obsolete and not used by MHA by default.
159