From 1887ae5770946be80b277596e77338abac18d2ec Mon Sep 17 00:00:00 2001 From: Frank Cizmich Date: Mon, 6 Oct 2014 17:25:52 -0200 Subject: [PATCH 1/2] pt-qry-dgst: clearer doc about type binlog. Also try to detect binlog file type-1377888 --- bin/pt-query-digest | 30 +++++++++++++++++++++++++++-- t/pt-query-digest/binlog_analyses.t | 15 ++++++++++++++- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/bin/pt-query-digest b/bin/pt-query-digest index 5d974edf..8be749d4 100755 --- a/bin/pt-query-digest +++ b/bin/pt-query-digest @@ -13181,6 +13181,13 @@ sub main { if ( $fh ) { PTDEBUG && _d('Reading', $filename); PTDEBUG && _d('File size:', $filesize); + # catch if user is trying to use an uncoverted (raw) binlog # issue 1377888 + if ( $filename && $o->get('type')->[0] eq 'binlog') { + if (is_raw_binlog($filename)) { + warn "Binlog file $filename must first be converted to text format using mysqlbinlog"; + exit 1; + } + } push @read_files, { name => ($filename || "STDIN"), size => $filesize }; # Read the file offset for --resume. @@ -14722,6 +14729,24 @@ sub sanitize_event { return; } +# make an effort to check if file is a raw binlog +# ( i.e was not converted to text using mysqlbinlog ) +sub is_raw_binlog { + my $filename = shift; + + my $output; + eval { + $output = `file $filename`; + }; + + # if we can't determine file type, let's assume it's ok + if ($EVAL_ERROR || !$output) { + return 0; + } + + return $output =~ /MySQL\s*replication/i; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } @@ -14798,7 +14823,8 @@ server. Before using this tool, please: pt-query-digest is a sophisticated but easy to use tool for analyzing MySQL queries. It can analyze queries from MySQL slow, general, and binary -logs, as well as C and MySQL protocol data from tcpdump. +logs. ( Binary logs must first be converted to text, see L<"--type"> ). +It can also use C and MySQL protocol data from tcpdump. By default, the tool reports which queries are the slowest, and therefore the most important to optimize. More complex and custom-tailored reports can be created by using options like L<"--group-by">, L<"--filter">, and @@ -16134,7 +16160,7 @@ The type of input to parse. The permitted types are =item binlog -Parse a binary log file. +Parse a binary log file that has been converted to text using mysqlbinlog. =item genlog diff --git a/t/pt-query-digest/binlog_analyses.t b/t/pt-query-digest/binlog_analyses.t index 26de9308..a0fde6d9 100644 --- a/t/pt-query-digest/binlog_analyses.t +++ b/t/pt-query-digest/binlog_analyses.t @@ -9,7 +9,7 @@ BEGIN { use strict; use warnings FATAL => 'all'; use English qw(-no_match_vars); -use Test::More tests => 3; +use Test::More tests => 4; use PerconaTest; require "$trunk/bin/pt-query-digest"; @@ -46,6 +46,19 @@ ok( 'Analysis for binlog011 - Handles 5.6 binlog with checksum CRC32', ) or diag($test_diff); + + +# ############################################################################# +# Issue 1377888: refuse to parse raw binary log +# ############################################################################# + +my $output = `$trunk/bin/pt-query-digest --type binlog '$trunk/t/pt-query-digest/samples/raw_binlog.log' 2>&1`; +ok( + $output =~ /mysqlbinlog/i , + 'Refuses to parse raw binlog file.' + ); + + # ############################################################################# # Done. # ############################################################################# From 5edb7366f38b949a245905417cd003f57fea23e4 Mon Sep 17 00:00:00 2001 From: Frank Cizmich Date: Wed, 5 Nov 2014 12:41:15 -0200 Subject: [PATCH 2/2] Improved test. Changed test file location. Added doc example. --- bin/pt-query-digest | 28 ++++++++++++--------------- t/lib/samples/binlogs/raw_binlog.log | Bin 0 -> 2551 bytes t/pt-query-digest/binlog_analyses.t | 17 +++++++++++----- 3 files changed, 24 insertions(+), 21 deletions(-) create mode 100644 t/lib/samples/binlogs/raw_binlog.log diff --git a/bin/pt-query-digest b/bin/pt-query-digest index 8be749d4..12bf8097 100755 --- a/bin/pt-query-digest +++ b/bin/pt-query-digest @@ -13185,7 +13185,7 @@ sub main { if ( $filename && $o->get('type')->[0] eq 'binlog') { if (is_raw_binlog($filename)) { warn "Binlog file $filename must first be converted to text format using mysqlbinlog"; - exit 1; + return 1; } } push @read_files, { name => ($filename || "STDIN"), size => $filesize }; @@ -14730,21 +14730,11 @@ sub sanitize_event { } # make an effort to check if file is a raw binlog -# ( i.e was not converted to text using mysqlbinlog ) +# (i.e. was not converted to text using mysqlbinlog) sub is_raw_binlog { my $filename = shift; - - my $output; - eval { - $output = `file $filename`; - }; - - # if we can't determine file type, let's assume it's ok - if ($EVAL_ERROR || !$output) { - return 0; - } - - return $output =~ /MySQL\s*replication/i; + + return -B $filename; } sub _d { @@ -14823,7 +14813,7 @@ server. Before using this tool, please: pt-query-digest is a sophisticated but easy to use tool for analyzing MySQL queries. It can analyze queries from MySQL slow, general, and binary -logs. ( Binary logs must first be converted to text, see L<"--type"> ). +logs. (Binary logs must first be converted to text, see L<"--type">). It can also use C and MySQL protocol data from tcpdump. By default, the tool reports which queries are the slowest, and therefore the most important to optimize. More complex and custom-tailored reports @@ -16160,7 +16150,13 @@ The type of input to parse. The permitted types are =item binlog -Parse a binary log file that has been converted to text using mysqlbinlog. +Parse a binary log file that has first been converted to text using mysqlbinlog. + +For example: + + mysqlbinlog mysql-bin.000441 > mysql-bin.000441.txt + + pt-query-digest --type binlog mysql-bin.000441.txt =item genlog diff --git a/t/lib/samples/binlogs/raw_binlog.log b/t/lib/samples/binlogs/raw_binlog.log new file mode 100644 index 0000000000000000000000000000000000000000..58bd214d4908dd1b33225c78f7239b7a359e967c GIT binary patch literal 2551 zcmcJQJxIe)5XY}=v=J1g4u!g;AXLGIv|1>*h$tcjr*6f;(jcWJg(fMuSU>9A&Bc#` zlbfr17x%6XYFBp`5$~Sikw6fdzCL&%kaDiTZCE6&w&gvkZ{ESiXbIq?3HCX3^!O0(`|NeKhafK z#v?5WAn|ubb+)D>rd5%Wki<4iH!axS-P=Flj6-aiWzC~7s)ngIEv0H$4aI2G^b@6Z zRJV0gS;?$rR|&pA%q83@25&MKfLphF4NC#sIfL9^1iKTQyAK)MblmUU?x}Dfcg`U9 z4l-b*XZL^Z1n2HO2Dkj&hXUNUA`^5kF}Urx-@9|~VJX1Oa0cc6E!dslJoh-hg8(hZ z{mt!O!cqWt&LH<^h8Op)5}dma82r@FJw7$QyM+vq^X_r)GI-l@e|P6T2x9z zE%5v9ac?vDrQ`nLp7(w*cg~=BS7yYz6P(ZcCWCkU+}l_Rkh{nIb&~F141VRf{{l># Bcmx0d literal 0 HcmV?d00001 diff --git a/t/pt-query-digest/binlog_analyses.t b/t/pt-query-digest/binlog_analyses.t index a0fde6d9..f8f76016 100644 --- a/t/pt-query-digest/binlog_analyses.t +++ b/t/pt-query-digest/binlog_analyses.t @@ -52,11 +52,18 @@ ok( # Issue 1377888: refuse to parse raw binary log # ############################################################################# -my $output = `$trunk/bin/pt-query-digest --type binlog '$trunk/t/pt-query-digest/samples/raw_binlog.log' 2>&1`; -ok( - $output =~ /mysqlbinlog/i , - 'Refuses to parse raw binlog file.' - ); +my $output = output( + sub { pt_query_digest::main(@args, "$trunk/t/lib/samples/binlogs/raw_binlog.log") }, + stderr => 1 +); + +like( + $output, + qr/mysqlbinlog/i, + 'Refuses to parse raw binlog file' +); + + # #############################################################################