This file is indexed.

/usr/share/perl5/MHA/BinlogHeaderParser.pm is in mha4mysql-node 0.54-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/usr/bin/env perl

#  Copyright (C) 2011 DeNA Co.,Ltd.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#  Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

package MHA::BinlogHeaderParser;

use strict;
use warnings FATAL => 'all';

use English qw(-no_match_vars);
use Carp qw(croak);
use File::Basename;
use File::Path;
use Errno();

sub new {
  my $class = shift;
  my $self  = {
    dir                        => undef,
    file                       => undef,
    self_server_id             => undef,
    target_rmlp                => undef,
    target_mlf                 => undef,
    master_version             => undef,
    current_mlf                => undef,
    starting_mlp               => undef,
    fp                         => undef,
    current_pos                => 4,
    prev_pos                   => 4,
    binlog_size                => undef,
    has_real_rotate_event      => 0,
    has_real_init_rotate_event => 0,
    debug                      => 0,
    @_,
  };
  return bless $self, $class;
}

our $ROTATE_EVENT                 = 4;
our $FORMAT_DESCRIPTION_EVENT     = 15;
our $XID_EVENT                    = 16;
our $LOG_EVENT_MINIMAL_HEADER_LEN = 19;
our $MAX_ALLOWED_PACKET           = 1024 * 1024 * 1024;

sub open_binlog($) {
  my $self = shift;
  croak "binlog dir must be set.\n"  unless ( $self->{dir} );
  croak "binlog file must be set.\n" unless ( $self->{file} );
  open( $self->{fp}, "<", "$self->{dir}/$self->{file}" )
    or croak "$!:$self->{dir}/$self->{file}";
  binmode $self->{fp};
  $self->{binlog_size} = -s "$self->{dir}/$self->{file}";
}

sub close_binlog($) {
  my $self = shift;
  close( $self->{fp} );
}

sub unpack_header {
  my $self = shift;
  my $pos  = shift;
  $pos = $self->{current_pos} if ( !defined($pos) );
  my $after_pos = shift;
  $after_pos = $pos if ( !defined($after_pos) );
  my $fp        = $self->{fp};
  my $file_size = $self->{binlog_size};
  return unpack_header_util( $fp, $file_size, $pos, $after_pos );
}

sub unpack_header_util {
  my $fp        = shift;
  my $file_size = shift;
  my $pos       = shift;
  my $after_pos = shift;
  $after_pos = $pos if ( !defined($after_pos) );
  if ( $pos >= $file_size || $after_pos >= $file_size ) {
    warn "Invalid position is set. pos=$pos after_pos=$after_pos\n";
    return;
  }
  my $event_type;
  my $server_id;
  my $event_length;
  my $end_log_pos;
  seek( $fp, $pos + 4, 0 );
  read( $fp, $event_type, 1 );
  seek( $fp, $pos + 5, 0 );
  read( $fp, $server_id, 4 );
  seek( $fp, $pos + 9, 0 );
  read( $fp, $event_length, 4 );
  seek( $fp, $pos + 13, 0 );
  read( $fp, $end_log_pos, 4 );
  seek( $fp, $after_pos, 0 );
  $event_type   = unpack( 'C', $event_type );
  $server_id    = unpack( 'V', $server_id );
  $event_length = unpack( 'V', $event_length );
  $end_log_pos  = unpack( 'V', $end_log_pos );

  if ( $event_length > $MAX_ALLOWED_PACKET ) {
    croak
"Event too large: pos: $pos, event_length: $event_length, event_type: $event_type\n";
  }

  if ( $event_length < $LOG_EVENT_MINIMAL_HEADER_LEN ) {
    croak
"Event too small: pos: $pos, event_length: $event_length, event_type: $event_type\n";
  }
  return ( $event_type, $server_id, $event_length, $end_log_pos );
}

sub get_master_version_from_fde($$) {
  my $self = shift;
  my $pos  = shift;
  my $fp   = $self->{fp};
  my ( $event_type, $server_id, $b, $c ) = $self->unpack_header($pos);
  if ( $event_type == $FORMAT_DESCRIPTION_EVENT ) {
    seek( $fp, $pos + 21, 0 );
    read( $fp, $self->{master_version}, 50 );
    $self->{master_version} =~ s/\0//g;
    print " Master Version is $self->{master_version}\n";
  }
  else {
    croak
      "This is not format description event! ev type=$event_type, pos=$pos.\n";
  }
  seek( $fp, $pos, 0 );
  return $self->{master_version};
}

sub parse_master_rotate_event($$) {
  my $self = shift;
  my $pos  = shift;
  my $fp   = $self->{fp};
  my ( $event_type, $server_id, $event_length, $end_log_pos ) =
    $self->unpack_header($pos);

  if ( $event_type == $ROTATE_EVENT ) {

    # real rotate event
    if ( $end_log_pos == 0 || $end_log_pos == $self->{current_pos} ) {
      my $offset     = 19;
      my $header_len = 8;
      my $rotate_pos;
      seek( $fp, $pos + $offset, 0 );
      read( $fp, $rotate_pos, $header_len );
      $end_log_pos = unpack( 'V', $rotate_pos );
      seek( $fp, $pos + $offset + $header_len, 0 );
      read( $fp, $self->{current_mlf}, $event_length - $offset - $header_len );
      $self->{has_real_rotate_event} = 1;
      $self->{has_real_init_rotate_event} = 1 if ( $end_log_pos <= 4 );
      print
"parse_master_rotate_event(real rotate event): file=$self->{file} event_type=$event_type server_id=$server_id length=$event_length elp=$end_log_pos\n"
        if ( $self->{debug} );
    }
    else {
      print
"parse_master_rotate_event(non-real rotate event): file=$self->{file} event_type=$event_type server_id=$server_id length=$event_length elp=$end_log_pos\n"
        if ( $self->{debug} );
    }
  }
  else {
    croak "This is not rotate event! ev type=$event_type, pos=$pos.\n";
  }
  seek( $fp, $pos, 0 );
  $self->{starting_mlp} = $end_log_pos unless ( $self->{starting_mlp} );
  return $end_log_pos;
}

sub parse_forward_header($) {
  my $self = shift;
  my $fp   = $self->{fp};
  my $pos  = $self->{current_pos};
  return if ( $self->{current_pos} >= $self->{binlog_size} );

  my ( $event_type, $server_id, $event_length, $end_log_pos ) =
    $self->unpack_header($pos);
  $self->{prev_pos}    = $self->{current_pos};
  $self->{current_pos} = $self->{current_pos} + $event_length;
  return ( $event_type, $server_id, $event_length, $end_log_pos );
}

sub parse_init_headers {
  my $self    = shift;
  my $do_dump = shift;
  my $outfile = shift;

  while ( my ( $event_type, $server_id, $event_length, $end_log_pos ) =
    $self->parse_forward_header() )
  {
    print
"parse_init_headers: file=$self->{file} event_type=$event_type server_id=$server_id length=$event_length nextmpos=$end_log_pos prevrelay=$self->{prev_pos} cur(post)relay=$self->{current_pos}\n"
      if ( $self->{debug} );
    if ( $event_type == $FORMAT_DESCRIPTION_EVENT ) {
      if ( $self->{self_server_id} && $server_id != $self->{self_server_id} ) {
        $self->{starting_mlp} = $end_log_pos
          if ( !$self->{starting_mlp} && $end_log_pos > 0 );
        if ( !$self->{master_version} ) {
          $self->get_master_version_from_fde( $self->{prev_pos} );
        }
      }
    }
    elsif ( $event_type == $ROTATE_EVENT ) {
      if ( $self->{self_server_id} && $server_id != $self->{self_server_id} ) {
        $end_log_pos = $self->parse_master_rotate_event( $self->{prev_pos} );
      }
      if ($do_dump) {
        print
"dumping rotate event from $self->{prev_pos} to $self->{current_pos}.\n"
          if ( $self->{debug} );
        my $buf;
        open( my $out, ">>", $outfile ) or croak "$!:$outfile";
        binmode $out;
        seek( $self->{fp}, $self->{prev_pos}, 0 );
        read( $self->{fp}, $buf, $self->{current_pos} - $self->{prev_pos} );
        print $out $buf;
        close($out);
      }
    }
    else {
      return $self->{prev_pos};
    }
  }
  return $self->{current_pos};
}

sub get_starting_mlp($) {
  my $self = shift;
  if ( $self->{starting_mlp} ) {
    print "starting_mlp: $self->{starting_mlp}\n" if ( $self->{debug} );
    return $self->{starting_mlp};
  }
  while ( my ( $event_type, $server_id, $event_length, $end_log_pos ) =
    $self->parse_forward_header() )
  {
    print
"get_starting_mlp: file=$self->{file} event_type=$event_type server_id=$server_id length=$event_length next=$end_log_pos\n"
      if ( $self->{debug} );
    next if ( $server_id == $self->{self_server_id} );
    next if ( $event_type == $FORMAT_DESCRIPTION_EVENT );
    next if ( $event_type == $ROTATE_EVENT );
    next if ( $end_log_pos <= 0 );
    $self->{starting_mlp} = $end_log_pos;
    return $self->{starting_mlp};
  }
  return;
}

sub get_starting_mlp_xid($) {
  my $self = shift;
  while ( my ( $event_type, $server_id, $event_length, $end_log_pos ) =
    $self->parse_forward_header() )
  {
    print
"get_starting_mlp_xid: file=$self->{file} event_type=$event_type server_id=$server_id length=$event_length next=$end_log_pos\n"
      if ( $self->{debug} );
    next if ( $server_id == $self->{self_server_id} );
    next if ( $event_type == $FORMAT_DESCRIPTION_EVENT );
    next if ( $event_type == $ROTATE_EVENT );
    next if ( $end_log_pos <= 0 );
    if ( $event_type == $XID_EVENT ) {
      $self->{starting_mlp} = $end_log_pos;
      return $self->{starting_mlp};
    }
    else {
      if ( $self->{starting_mlp} && $self->{starting_mlp} <= $end_log_pos ) {
        return $self->{starting_mlp};
      }
      else {
        return;
      }
    }
  }
  return;
}

sub find_target_relay_pos($) {
  my $self  = shift;
  my $found = 0;
  while ( my ( $event_type, $server_id, $event_length, $end_log_pos ) =
    $self->parse_forward_header() )
  {
    print
"find_target_relay_pos: file=$self->{file} event_type=$event_type server_id=$server_id length=$event_length next=$end_log_pos\n"
      if ( $self->{debug} );
    next if ( $server_id == $self->{self_server_id} );
    next if ( $event_type == $FORMAT_DESCRIPTION_EVENT );
    if ( $event_type == $ROTATE_EVENT ) {
      $end_log_pos = $self->parse_master_rotate_event( $self->{prev_pos} );
    }
    if ( $end_log_pos == $self->{target_rmlp} ) {
      $found = 1;
      return ( $found, $self->{current_pos} );
    }
  }
  $found = 0;
  return $found;
}

sub find_target_relay_pos_xid($$$$$) {
  my $self               = shift;
  my $last_absolute_mpos = shift;
  my $prev_mpos          = shift;
  my $in_relative        = shift;
  my $real_mpos          = shift;

  my $found = 0;
  my $start_rlp;

  while ( my ( $event_type, $server_id, $event_length, $end_log_pos ) =
    $self->parse_forward_header() )
  {
    print
"find_target_relay_pos_xid: file=$self->{file} event_type=$event_type server_id=$server_id length=$event_length next=$end_log_pos\n"
      if ( $self->{debug} );
    next if ( $event_type == $FORMAT_DESCRIPTION_EVENT );
    next if ( $server_id == $self->{self_server_id} );
    if ( $event_type == $ROTATE_EVENT ) {
      $end_log_pos = $self->parse_master_rotate_event( $self->{prev_pos} );
    }
    if ( $end_log_pos == $event_length
      || ( $end_log_pos < $prev_mpos && !$in_relative ) )
    {

      # Entering relative mode
      $last_absolute_mpos = $prev_mpos;
      $in_relative        = 1;
      $real_mpos          = $last_absolute_mpos + $end_log_pos;
    }
    elsif ( $end_log_pos < $prev_mpos && $in_relative ) {

      # void event
    }
    elsif ( $in_relative && $event_type != $XID_EVENT ) {

      # Continuing relative mode
      $real_mpos = $last_absolute_mpos + $end_log_pos;
    }
    elsif ( $event_type == $XID_EVENT ) {

      # Exiting relative mode
      $real_mpos          = $end_log_pos;
      $last_absolute_mpos = $end_log_pos;
      $in_relative        = 0;
    }
    else {

      # Not in relative mode
      $real_mpos          = $end_log_pos;
      $last_absolute_mpos = $end_log_pos;
    }
    $prev_mpos = $end_log_pos;
    if ( $real_mpos == $self->{target_rmlp} ) {
      $found     = 1;
      $start_rlp = $self->{current_pos};
      last;
    }
  }
  return ( $found, $start_rlp, $last_absolute_mpos, $prev_mpos, $in_relative,
    $real_mpos );
}

1;