~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to lib/GenTest/Server/ReplMySQLd.pm

Added replicating server (actually a master/slave pair) + unit test

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010 Sun Microsystems, Inc. All rights reserved.
 
2
# Use is subject to license terms.
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; version 2 of the License.
 
7
#
 
8
# This program is distributed in the hope that it will be useful, but
 
9
# WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
11
# General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
 
16
# USA
 
17
 
 
18
package GenTest::Server::ReplMySQLd;
 
19
 
 
20
@ISA = qw(GenTest);
 
21
 
 
22
use DBI;
 
23
use GenTest;
 
24
use GenTest::Constants;
 
25
use GenTest::Server::MySQLd;
 
26
use if windows(), Win32::Process;
 
27
use Time::HiRes;
 
28
 
 
29
use strict;
 
30
 
 
31
use Carp;
 
32
use Data::Dumper;
 
33
 
 
34
use constant REPLMYSQLD_BASEDIR => 0;
 
35
use constant REPLMYSQLD_MASTER_VARDIR => 1;
 
36
use constant REPLMYSQLD_SLAVE_VARDIR => 2;
 
37
use constant REPLMYSQLD_MASTER_PORT => 3;
 
38
use constant REPLMYSQLD_SLAVE_PORT => 4;
 
39
use constant REPLMYSQLD_MODE => 5;
 
40
use constant REPLMYSQLD_START_DIRTY => 6;
 
41
use constant REPLMYSQLD_SERVER_OPTIONS => 7;
 
42
use constant REPLMYSQLD_MASTER => 8;
 
43
use constant REPLMYSQLD_SLAVE => 9;
 
44
 
 
45
sub new {
 
46
    my $class = shift;
 
47
 
 
48
    my $self = $class->SUPER::new({'basedir' => REPLMYSQLD_BASEDIR,
 
49
                                   'master_vardir' => REPLMYSQLD_MASTER_VARDIR,
 
50
                                   'master_port' => REPLMYSQLD_MASTER_PORT,
 
51
                                   'slave_vardir' => REPLMYSQLD_SLAVE_VARDIR,
 
52
                                   'slave_port' => REPLMYSQLD_SLAVE_PORT,
 
53
                                   'mode' => REPLMYSQLD_MODE,
 
54
                                   'server_options' => REPLMYSQLD_SERVER_OPTIONS,
 
55
                                   'start_dirty' => REPLMYSQLD_START_DIRTY},@_);
 
56
    
 
57
    if (not defined $self->[REPLMYSQLD_MASTER_PORT]) {
 
58
        $self->[REPLMYSQLD_MASTER_PORT] = GenTest::Server::MySQLd::MYSQLD_DEFAULT_PORT;
 
59
    }
 
60
    
 
61
    if (not defined $self->[REPLMYSQLD_SLAVE_PORT]) {
 
62
        $self->[REPLMYSQLD_SLAVE_PORT] = $self->[REPLMYSQLD_MASTER_PORT] + 2;        
 
63
    }
 
64
 
 
65
    if (not defined $self->[REPLMYSQLD_MODE]) {
 
66
        $self->[REPLMYSQLD_MODE] = 'default';
 
67
    }
 
68
    
 
69
    my @master_options;
 
70
    push(@master_options, 
 
71
         "--server_id=0",
 
72
         "--report-host=127.0.0.1",
 
73
         "--report_port=".$self->[REPLMYSQLD_MASTER_PORT]);
 
74
    if (defined $self->[REPLMYSQLD_SERVER_OPTIONS]) {
 
75
        push(@master_options, 
 
76
             @{$self->[REPLMYSQLD_SERVER_OPTIONS]});
 
77
    }
 
78
 
 
79
 
 
80
    $self->[REPLMYSQLD_MASTER] = GenTest::Server::MySQLd->new(basedir => $self->basedir,
 
81
                                                              vardir => $self->[REPLMYSQLD_MASTER_VARDIR],
 
82
                                                              port => $self->[REPLMYSQLD_MASTER_PORT],
 
83
                                                              server_options => \@master_options,
 
84
                                                              start_dirty => $self->[REPLMYSQLD_START_DIRTY]);
 
85
    
 
86
    if (not defined $self->master) {
 
87
        croak("Could not create master");
 
88
    }
 
89
    
 
90
    my @slave_options;
 
91
    push(@slave_options, 
 
92
         "--server_id=1",
 
93
         "--report-host=127.0.0.1",
 
94
         "--report_port=".$self->[REPLMYSQLD_SLAVE_PORT]);
 
95
    if (defined $self->[REPLMYSQLD_SERVER_OPTIONS]) {
 
96
        push(@slave_options, 
 
97
             @{$self->[REPLMYSQLD_SERVER_OPTIONS]});
 
98
    }
 
99
    
 
100
    
 
101
    $self->[REPLMYSQLD_SLAVE] = GenTest::Server::MySQLd->new(basedir => $self->basedir,
 
102
                                                             vardir => $self->[REPLMYSQLD_SLAVE_VARDIR],
 
103
                                                             port => $self->[REPLMYSQLD_SLAVE_PORT],
 
104
                                                             server_options => \@slave_options,
 
105
                                                             start_dirty => $self->[REPLMYSQLD_START_DIRTY]);
 
106
    
 
107
    if (not defined $self->slave) {
 
108
        croak("Could not create slave");
 
109
    }
 
110
    
 
111
    return $self;
 
112
}
 
113
 
 
114
sub basedir {
 
115
    return $_[0]->[REPLMYSQLD_BASEDIR];
 
116
}
 
117
 
 
118
sub master {
 
119
    return $_[0]->[REPLMYSQLD_MASTER];
 
120
}
 
121
 
 
122
sub slave {
 
123
    return $_[0]->[REPLMYSQLD_SLAVE];
 
124
}
 
125
 
 
126
sub mode {
 
127
    return $_[0]->[REPLMYSQLD_MODE];
 
128
}
 
129
 
 
130
sub startServer {
 
131
    my ($self) = @_;
 
132
 
 
133
    $self->master->startServer;
 
134
    my $master_dbh = $self->master->dbh;
 
135
    $self->slave->startServer;
 
136
    my $slave_dbh = $self->slave->dbh;
 
137
 
 
138
        my ($foo, $master_version) = $master_dbh->selectrow_array("SHOW VARIABLES LIKE 'version'");
 
139
 
 
140
        if (($master_version !~ m{^5\.0}sio) && ($self->mode ne 'default')) {
 
141
                $master_dbh->do("SET GLOBAL BINLOG_FORMAT = '".$self->mode."'");
 
142
                $slave_dbh->do("SET GLOBAL BINLOG_FORMAT = '".$self->mode."'");
 
143
        }
 
144
    
 
145
        $slave_dbh->do("STOP SLAVE");
 
146
 
 
147
#       $slave_dbh->do("SET GLOBAL storage_engine = '$engine'") if defined $engine;
 
148
    
 
149
        $slave_dbh->do("CHANGE MASTER TO
 
150
                MASTER_PORT = ".$self->master->port.",
 
151
                MASTER_HOST = '127.0.0.1',
 
152
               MASTER_USER = 'root',
 
153
               MASTER_CONNECT_RETRY = 1
 
154
        ");
 
155
    
 
156
        $slave_dbh->do("START SLAVE");
 
157
    
 
158
 
 
159
    
 
160
}
 
161
 
 
162
sub stopServer {
 
163
    my ($self) = @_;
 
164
    
 
165
    $self->master->stopServer;
 
166
    $self->slave->stopServer;
 
167
}
 
168
 
 
169
1;