| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'Net::Server::Fork'; | ||
| 7 | |||
| 8 | use DBI; | ||
| 9 | use Scalar::Util qw{ weaken }; | ||
| 10 | use MySQL::Replication::Server; | ||
| 11 | |||
| 12 | use constant { | ||
| 13 | LOG_ERR => 0, | ||
| 14 | LOG_DEBUG => 4, | ||
| 15 | }; | ||
| 16 | |||
| 17 | my %HANDLERS = ( | ||
| 18 | QUIT => \&QUIT, | ||
| 19 | PING => \&PING, | ||
| 20 | GET => \&GET, | ||
| 21 | POLL => \&POLL, | ||
| 22 | ); | ||
| 23 | |||
| 24 | __PACKAGE__->new()->run( | ||
| 25 | syslog_ident => 'MySQLReplicationServer.pl', | ||
| 26 | port => 2603, | ||
| 27 | log_file => 'Sys::Syslog', | ||
| 28 | pid_file => '/var/run/mysql_replication_server.pid', | ||
| 29 | ); | ||
| 30 | |||
| 31 | sub options { | ||
| 32 | my ( $Self, $Template ) = @_; | ||
| 33 | |||
| 34 | $Self->{server}{binlog} ||= []; | ||
| 35 | $Template->{binlog} = $Self->{server}{binlog}; | ||
| 36 | |||
| 37 | $Self->SUPER::options( $Template ); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub post_configure { | ||
| 41 | my ( $Self ) = @_; | ||
| 42 | |||
| 43 | $Self->SUPER::post_configure(); | ||
| 44 | |||
| 45 | foreach my $Required ( qw{ binlog } ) { | ||
| 46 | next if @{ $Self->{server}{$Required} }; | ||
| 47 | die "Required argument '$Required' missing\n"; | ||
| 48 | } | ||
| 49 | |||
| 50 | foreach my $Binlog ( @{ $Self->{server}{binlog} } ) { | ||
| 51 | my ( $Name, $IndexFilename, $ServerId, $BinlogSize ) = split ':', $Binlog; | ||
| 52 | |||
| 53 | if ( not $Name or not $IndexFilename ) { | ||
| 54 | die "Invalid format for binlog '$Binlog'\n"; | ||
| 55 | } | ||
| 56 | |||
| 57 | my $BinlogFilename = _GetLatestBinlog( $IndexFilename ); | ||
| 58 | my ( $BinlogPath ) = $BinlogFilename =~ m{(.*)\.\d+$}; | ||
| 59 | |||
| 60 | $Self->{server}{BinlogPaths}{$Name} = $BinlogPath; | ||
| 61 | $Self->{server}{IndexFilenames}{$Name} = $IndexFilename; | ||
| 62 | $Self->{server}{BinlogSize}{$Name} = $BinlogSize || 2**20*100; | ||
| 63 | |||
| 64 | if ( $ServerId ) { | ||
| 65 | $Self->{server}{ServerIds}{$Name} = $ServerId; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | $SIG{__DIE__} = sub { | ||
| 70 | $Self->log( LOG_ERR, "Aborting: @_" ); | ||
| 71 | exit 1; | ||
| 72 | }; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub process_request { | ||
| 76 | my ( $Self ) = @_; | ||
| 77 | |||
| 78 | while ( 1 ) { | ||
| 79 | my $Request = MySQL::Replication::Command->NewFromSocket( | ||
| 80 | Socket => $Self->{server}{client}, | ||
| 81 | Buffer => \$Self->{Buffer}, | ||
| 82 | ); | ||
| 83 | |||
| 84 | if ( not $Request ) { | ||
| 85 | die "Error reading request of client ($MySQL::Replication::Command::Errstr)"; | ||
| 86 | } | ||
| 87 | |||
| 88 | if ( not $HANDLERS{ $Request->Command() } ) { | ||
| 89 | die sprintf( "Invalid command '%s' received from client", $Request->Command() ); | ||
| 90 | } | ||
| 91 | |||
| 92 | next if $HANDLERS{ $Request->Command() }->( $Self, $Request ); | ||
| 93 | last; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | sub QUIT { | ||
| 98 | my ( $Self ) = @_; | ||
| 99 | |||
| 100 | $Self->log( LOG_ERR, 'Quitting' ); | ||
| 101 | return; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub PING { | ||
| 105 | my ( $Self ) = @_; | ||
| 106 | |||
| 107 | my $Response = MySQL::Replication::Command->new( | ||
| 108 | Command => 'PONG', | ||
| 109 | ); | ||
| 110 | |||
| 111 | if ( not $Response ) { | ||
| 112 | die "Error creating PING response ($MySQL::Replication::Command::Errstr)"; | ||
| 113 | } | ||
| 114 | |||
| 115 | if ( not $Response->SendToSocket( $Self->{server}{client} ) ) { | ||
| 116 | die "Error sending PING response ($MySQL::Replication::Command::Errstr)"; | ||
| 117 | } | ||
| 118 | |||
| 119 | return 1; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub GET { | ||
| 123 | my ( $Self, $Request ) = @_; | ||
| 124 | |||
| 125 | foreach my $Required ( qw{ Binlog StartLog StartPos } ) { | ||
| 126 | next if $Request->Headers()->{$Required}; | ||
| 127 | die "Required header '$Required' missing"; | ||
| 128 | } | ||
| 129 | |||
| 130 | if ( defined $Request->Headers()->{EndLog} and not defined $Request->Headers()->{EndPos} ) { | ||
| 131 | die "EndLog header defined but EndPos missing"; | ||
| 132 | } | ||
| 133 | |||
| 134 | if ( defined $Request->Headers()->{EndPos} and not defined $Request->Headers()->{EndLog} ) { | ||
| 135 | die "EndPos header defined but EndLog missing"; | ||
| 136 | } | ||
| 137 | |||
| 138 | if ( not $Self->{server}{BinlogPaths}{ $Request->Headers()->{Binlog} } ) { | ||
| 139 | die sprintf( "Invalid binlog '%s' in request", $Request->Headers()->{Binlog} ); | ||
| 140 | } | ||
| 141 | |||
| 142 | my $ReplicationServer = MySQL::Replication::Server->new( | ||
| 143 | BinlogPath => $Self->{server}{BinlogPaths}{ $Request->Headers()->{Binlog} }, | ||
| 144 | ( | ||
| 145 | $Self->{server}{ServerIds}{ $Request->Headers()->{Binlog} } | ||
| 146 | ? ( ServerId => $Self->{server}{ServerIds}{ $Request->Headers()->{Binlog} } ) | ||
| 147 | : (), | ||
| 148 | ), | ||
| 149 | StartLog => $Request->Headers()->{StartLog}, | ||
| 150 | StartPos => $Request->Headers()->{StartPos}, | ||
| 151 | ( | ||
| 152 | $Request->Headers()->{EndLog} | ||
| 153 | ? ( | ||
| 154 | EndLog => $Request->Headers()->{EndLog}, | ||
| 155 | EndPos => $Request->Headers()->{EndPos}, | ||
| 156 | ) | ||
| 157 | : () | ||
| 158 | ), | ||
| 159 | ); | ||
| 160 | |||
| 161 | weaken( my $Weak = $ReplicationServer ); | ||
| 162 | |||
| 163 | my $Interrupted; | ||
| 164 | |||
| 165 | my $InterruptListener = AnyEvent->io( | ||
| 166 | fh => $Self->{server}{client}, | ||
| 167 | poll => 'r', | ||
| 168 | cb => sub { | ||
| 169 | $Weak->CondVar()->send(); | ||
| 170 | $Interrupted++; | ||
| 171 | }, | ||
| 172 | ); | ||
| 173 | |||
| 174 | while ( 1 ) { | ||
| 175 | my $Query = $ReplicationServer->GetQuery(); | ||
| 176 | last if not $Query; | ||
| 177 | last if $Interrupted; | ||
| 178 | |||
| 179 | if ( $Self->{server}{log_level} == LOG_DEBUG ) { | ||
| 180 | $Self->log( LOG_DEBUG, 'Sending: ' . $Query->Stringify() ); | ||
| 181 | } | ||
| 182 | |||
| 183 | if ( not $Query->SendToSocket( $Self->{server}{client} ) ) { | ||
| 184 | die "Error sending query to client ($MySQL::Replication::Command::Errstr)"; | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | my $RequestComplete = MySQL::Replication::Command->new( | ||
| 189 | Command => 'OK', | ||
| 190 | ); | ||
| 191 | |||
| 192 | if ( not $RequestComplete ) { | ||
| 193 | die "Error creating OK response ($MySQL::Replication::Command::Errstr)"; | ||
| 194 | } | ||
| 195 | |||
| 196 | if ( not $RequestComplete->SendToSocket( $Self->{server}{client} ) ) { | ||
| 197 | die "Error sending OK response ($MySQL::Replication::Command::Errstr)"; | ||
| 198 | } | ||
| 199 | |||
| 200 | return 1; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub POLL { | ||
| 204 | my ( $Self, $Request ) = @_; | ||
| 205 | |||
| 206 | if ( not $Request->Headers()->{Binlog} ) { | ||
| 207 | die "Required header 'Binlog' missing"; | ||
| 208 | } | ||
| 209 | |||
| 210 | my $IndexFilename = $Self->{server}{IndexFilenames}{ $Request->Headers()->{Binlog} } | ||
| 211 | or die sprintf( "Invalid binlog '%s' in request", $Request->Headers()->{Binlog} ); | ||
| 212 | |||
| 213 | my $LatestBinlog = _GetLatestBinlog( $IndexFilename ); | ||
| 214 | my ( $Log ) = $LatestBinlog =~ m{\.0*(\d+)$}; | ||
| 215 | |||
| 216 | if ( not $Log ) { | ||
| 217 | die sprintf( "Invalid binlog '%s' in index '%s'", $LatestBinlog, $IndexFilename ); | ||
| 218 | } | ||
| 219 | |||
| 220 | my $Response = MySQL::Replication::Command->new( | ||
| 221 | Command => 'STATS', | ||
| 222 | Headers => { | ||
| 223 | Log => $Log, | ||
| 224 | Pos => ( -s $LatestBinlog ), | ||
| 225 | BinlogSize => $Self->{server}{BinlogSize}{ $Request->Headers()->{Binlog} }, | ||
| 226 | }, | ||
| 227 | ) or die "Error creating STATS response ($MySQL::Replication::Command::Errstr)"; | ||
| 228 | |||
| 229 | $Response->SendToSocket( $Self->{server}{client} ) | ||
| 230 | or die "Error sending STATS response ($MySQL::Replication::Command::Errstr)"; | ||
| 231 | |||
| 232 | return 1; | ||
| 233 | } | ||
| 234 | |||
| 235 | sub _GetLatestBinlog { | ||
| 236 | my ( $IndexFilename ) = @_; | ||
| 237 | |||
| 238 | open my $IndexFh, '<', $IndexFilename | ||
| 239 | or die "Error opening index '$IndexFilename' ($!)"; | ||
| 240 | |||
| 241 | my @Binlogs = <$IndexFh>; | ||
| 242 | |||
| 243 | if ( not @Binlogs ) { | ||
| 244 | die "Index '$IndexFilename' contained no binlogs"; | ||
| 245 | } | ||
| 246 | |||
| 247 | chomp $Binlogs[-1]; | ||
| 248 | return $Binlogs[-1]; | ||
| 249 | } | ||
| 250 | |||
| 251 | =pod | ||
| 252 | |||
| 253 | =head1 NAME | ||
| 254 | |||
| 255 | MySQLReplicationServer.pl - Serves local binlog queries to replication clients | ||
| 256 | |||
| 257 | =head1 SYNOPSIS | ||
| 258 | |||
| 259 | MySQLReplicationServer.pl | ||
| 260 | --binlog <name:index> | ||
| 261 | --binlog <name:index:serverid> | ||
| 262 | --binlog <name:index:serverid:binlogsize> | ||
| 263 | --port <port> | ||
| 264 | |||
| 265 | =head1 OPTIONS | ||
| 266 | |||
| 267 | MySQLReplicationServer.pl is based on L<Net::Server>, which has a wide range of | ||
| 268 | configuation options. See L<Net::Server> for more information. | ||
| 269 | |||
| 270 | By default, logging is via syslog using the 'daemon' facility (/var/log/messages). | ||
| 271 | |||
| 272 | =over | ||
| 273 | |||
| 274 | =item binlog (mandatory) | ||
| 275 | |||
| 276 | Specifies where binlogs can be found. There are two formats: | ||
| 277 | |||
| 278 | =over | ||
| 279 | |||
| 280 | =item * | ||
| 281 | |||
| 282 | name:index | ||
| 283 | |||
| 284 | This specifies the name of the binlog and the path to the binlog index. All | ||
| 285 | queries will be replicated. | ||
| 286 | |||
| 287 | =item * | ||
| 288 | |||
| 289 | name:index:serverid | ||
| 290 | |||
| 291 | Like above, but only queries with the specified server-id will be replicated. | ||
| 292 | |||
| 293 | =item * | ||
| 294 | |||
| 295 | name:index:serverid:binlogsize | ||
| 296 | |||
| 297 | Like above, but specifies the size of the binlogs (default 100M). This is used | ||
| 298 | for instrumentation purposes (see L<Instrumentation> for more info) | ||
| 299 | |||
| 300 | =back | ||
| 301 | |||
| 302 | e.g.: | ||
| 303 | |||
| 304 | --binlog bigdata:/mnt/bitstorage/mysql-bin.index | ||
| 305 | --binlog sensitive:/mnt/securestorage/mysql-bin.index:16 | ||
| 306 | |||
| 307 | Here all queries from the C<bigdata> binlog will be served, but only queries | ||
| 308 | logged with server-id 16 from the C<sensitive> binlog will be served. | ||
| 309 | |||
| 310 | This option can be used multiple times to specify multiple binlogs. | ||
| 311 | |||
| 312 | =item port (default 2603) | ||
| 313 | |||
| 314 | The port to listen on for replication consumers. | ||
| 315 | |||
| 316 | =back | ||
| 317 | |||
| 318 | =head1 AUTHOR | ||
| 319 | |||
| 320 | Alfie John, C<alfiej@opera.com> | ||
| 321 | |||
| 322 | =head1 LICENSE AND COPYRIGHT | ||
| 323 | |||
| 324 | Copyright (c) 2011, Opera Software Australia Pty. Ltd. | ||
| 325 | All rights reserved. | ||
| 326 | |||
| 327 | Redistribution and use in source and binary forms, with or without | ||
| 328 | modification, are permitted provided that the following conditions are | ||
| 329 | met: | ||
| 330 | |||
| 331 | * Redistributions of source code must retain the above copyright notice, | ||
| 332 | this list of conditions and the following disclaimer. | ||
| 333 | * Redistributions in binary form must reproduce the above copyright notice, | ||
| 334 | this list of conditions and the following disclaimer in the documentation | ||
| 335 | and/or other materials provided with the distribution. | ||
| 336 | * Neither the name of the copyright holder nor the names of its contributors | ||
| 337 | may be used to endorse or promote products derived from this software | ||
| 338 | without specific prior written permission. | ||
| 339 | |||
| 340 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 341 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 342 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| 343 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
| 344 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 345 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| 346 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| 347 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
| 348 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| 349 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 350 | |||
| 351 | =cut |