MySQL.pm

filter: Statementstmt 32.6% 30 / 92 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 45.4% 10 / 22 Podpod n/a totaltotal 24.8% 40 / 161 CC 17 SCAR 26.1
CC
17
Cov
23%
_do_fork
42.0
_server_start
20.0
_ticks
20.0
CRAP 13.6
« MySQL/Forwarder.pm ↑ Summary
1 package POE::Component::Proxy::MySQL;
2 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use MooseX::MethodAttributes;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 our $VERSION = "0.04";
6  
7 1 ▶use strict; # for kwalitee
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings; # for kwalitee
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 with 'MooseX::Getopt';
11  
12 1 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use POSIX qw(errno_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Module::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use POE qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
17 Wheel::ReadWrite
18 Wheel::SocketFactory
19 Filter::Stream
20 1 );
21 1 ▶use POE::Component::Proxy::MySQL::Forwarder;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23  
24  
25 has 'src_address' => (is => 'rw', isa => 'Str', default => '127.0.0.1');
26 has 'src_port' => (is => 'rw', isa => 'Int', default => 23306);
27  
28 has 'dst_address' => (is => 'rw', isa => 'Str', default => '127.0.0.1');
29 has 'dst_port' => (is => 'rw', isa => 'Int', default => 3306);
30  
31 has 'processes' => (is => 'rw', isa => 'Int', default => 4);
32  
33 has 'conn_per_child' => (is => 'rw', isa => 'Int', default => 4096);
34  
35  
36 0 ▶sub DEBUG {1}
Subroutine DEBUG: not called CC=1 SCAR=6.9
37  
38 0 ▶sub BUILD {
Subroutine BUILD: not called CC=2 SCAR=17.9
39 0 my ($self, $opt) = @_;
40  
41 0 $self->processes($self->processes - 1);
42 0 ▶ $self->processes(1) if $self->processes < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->processes < 1 0 0 0
43  
44 0 POE::Session->create(
45 object_states => [
46 $self => {
47 _start => '_server_start',
48 _stop => '_server_stop',
49 _do_fork => '_do_fork',
50 accept_success => '_server_accept_success',
51 accept_failure => '_server_accept_failure',
52 got_sig_int => '_got_sig_int',
53 got_sig_child => '_got_sig_child',
54 restart => '_restart',
55 ticks => '_ticks',
56 }
57 ],
58 args => [$self->src_address, $self->src_port, $self->dst_address, $self->dst_port]
59 );
60  
61 0 return $self;
62 }
63  
64  
65  
66 0 ▶sub _server_start {
Subroutine _server_start: not called CC=4 SCAR=30.0
67 0 my ($self, $heap, $session, $local_addr, $local_port, $remote_addr, $remote_port) =
68 @_[OBJECT, HEAP, SESSION, ARG0, ARG1, ARG2, ARG3];
69
70 0 print "Redirecting $local_addr:$local_port to $remote_addr:$remote_port\n";
71
72 0 $heap->{local_addr} = $local_addr;
73 0 $heap->{local_port} = $local_port;
74 0 $heap->{remote_addr} = $remote_addr;
75 0 $heap->{remote_port} = $remote_port;
76 0 $heap->{is_a_child} = 0;
77 0 ▶ $heap->{processes} ||= $self->processes;
Condition0%0/3
$heap->{"processes"} ||= $self->processes
l!l&&r!l&&!r
000
MC/DC0%0/2
$heap->{"processes"} ||= $self->processes
$heap->{"processes"} $self->processes
Truth table0%0/3
A ||= B
A= $heap->{"processes"}B= $self->processes
ABresult
1X1
011
000
78 0 ▶ $heap->{conn_per_child} ||= $self->conn_per_child;
Condition0%0/3
$heap->{"conn_per_child"} ||= $self->conn_per_child
l!l&&r!l&&!r
000
MC/DC0%0/2
$heap->{"conn_per_child"} ||= $self->conn_per_child
$heap->{"conn_per_child"} $self->conn_per_child
Truth table0%0/3
A ||= B
A= $heap->{"conn_per_child"}B= $self->conn_per_child
ABresult
1X1
011
000
79 0 $heap->{can_fork} = 0;
80
81 0 $heap->{server_wheel} = POE::Wheel::SocketFactory->new(
82 BindAddress => $local_addr,
83 BindPort => $local_port,
84 Reuse => 'yes',
85 SuccessEvent => 'accept_success',
86 FailureEvent => 'accept_failure',
87 );
88  
89 0 $heap->{pid} = $$;
90
91 0 ▶ if ($heap->{processes} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"processes"} > 0) 0 0 0
92 0 $_[KERNEL]->yield('_do_fork');
93 }
94 }
95  
96  
97 0 ▶sub _ticks {
Subroutine _ticks: not called CC=4 SCAR=30.0
98 0 my ($self, $heap, $kernel) = @_[OBJECT, HEAP, KERNEL];
99
100 0 ▶ if ($heap->{is_a_child}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"is_a_child"}) 0 0 0
101
102 0 ▶ if ($heap->{connections_per_child}->{$$} >= $heap->{conn_per_child}
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{'connections_per_child'}{$$} >= $heap->{'conn_per_child'} and $heap->{'is_a_child'}) { } 0 0 0
Condition0%0/3
$heap->{"connections_per_child"}{$$} >= $heap->{"conn_per_child"} and $heap->{"is_a_child"}
!ll&&!rl&&r
000
MC/DC0%0/2
$heap->{"connections_per_child"}{$$} >= $heap->{"conn_per_child"} and $heap->{"is_a_child"}
$heap->{"connections_per_child"}{$$} >= $heap->{"conn_per_child"} $heap->{"is_a_child"}
Truth table0%0/3
A and B
A= $heap->{"connections_per_child"}{$$} >= $heap->{"conn_per_child"}B= $heap->{"is_a_child"}
ABresult
0X0
100
111
103 && $heap->{is_a_child}) {
104 0 $heap->{can_fork} = 1;
105 0 $kernel->yield('_do_fork');
106 0 $heap->{server_wheel}->pause_accept;
107 }
108 else {
109 0 $kernel->delay_set('ticks', 10);
110 }
111
112 }
113  
114 }
115  
116 0 ▶sub _restart {
Subroutine _restart: not called CC=1 SCAR=6.9
117 0 my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
118
119 0 $_[KERNEL]->delay_set('tick', 60);
120 }
121  
122 0 ▶sub _got_sig_int {
Subroutine _got_sig_int: not called CC=1 SCAR=6.9
123 0 my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
124
125 0 delete $heap->{server};
126 0 $kernel->sig_handled();
127 }
128  
129 0 ▶sub _got_sig_child {
Subroutine _got_sig_child: not called CC=3 SCAR=24.8
130 0 my ($kernel, $heap, $child_pid) = @_[KERNEL, HEAP, ARG1];
131  
132 0 ▶ return unless delete $heap->{children}->{$child_pid};
Branch: 1 branch
BranchTrueFalseTotal
unless delete $heap->{"children"}{$child_pid} 0 0 0
133  
134 0 ▶ $kernel->yield("_do_fork") if exists $_[HEAP]->{server_wheel};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[3]{"server_wheel"} 0 0 0
135 }
136  
137 0 ▶sub _do_fork {
Subroutine _do_fork: not called CC=6 SCAR=37.4
138 0 my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
139
140 0 ▶ return if $heap->{is_a_child} && !$heap->{can_fork};
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"is_a_child"} and not $heap->{"can_fork"} 0 0 0
Condition0%0/3
$heap->{"is_a_child"} and not $heap->{"can_fork"}
!ll&&!rl&&r
000
MC/DC0%0/2
$heap->{"is_a_child"} and not $heap->{"can_fork"}
$heap->{"is_a_child"} not $heap->{"can_fork"}
Truth table0%0/3
A and not(B)
A= $heap->{"is_a_child"}B= not $heap->{"can_fork"}
ABresult
0X0
100
111
141
142 0 ▶ while (scalar(keys %{$heap->{children}}) < $heap->{processes}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$$heap{"children"};} < $heap->{"processes"}) 0 0 0
143 0 my $pid = fork();
144
145 0 ▶ unless (defined($pid)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 0 0
146 0 $kernel->delay(do_fork => 1);
147 0 return;
148 }
149
150 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 0 0 0
151 0 $heap->{server_wheel}->pause_accept;
152 0 $heap->{children}->{$pid} = 1;
153 0 $kernel->sig_child($pid, "got_sig_child");
154 0 next;
155 }
156
157 0 $heap->{connections_per_child}->{$$} = 0;
158 0 $heap->{active_connections}->{$$} = 0;
159
160 0 $heap->{server_wheel}->resume_accept;
161 0 $kernel->has_forked();
162 0 $heap->{is_a_child} = 1;
163 0 $heap->{children} = {};
164 0 $_[KERNEL]->yield('ticks');
165 0 return;
166 }
167 }
168  
169 0 ▶sub _server_stop {
Subroutine _server_stop: not called CC=1 SCAR=6.9
170 0 my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
171
172 }
173  
174 0 ▶sub _server_accept_success {
Subroutine _server_accept_success: not called CC=1 SCAR=6.9
175 0 my ($self, $heap, $socket, $peer_addr, $peer_port) = @_[OBJECT, HEAP, ARG0, ARG1, ARG2];
176  
177 my $forwarder = POE::Component::Proxy::MySQL::Forwarder->new({
178 socket => $socket,
179 peer_addr => $peer_addr,
180 peer_port => $peer_port,
181 remote_addr => $heap->{remote_addr},
182 remote_port => $heap->{remote_port},
183 0 heap => $heap,
184 namespace => ref($self),
185 });
186  
187 }
188  
189 0 ▶sub _server_accept_failure {
Subroutine _server_accept_failure: not called CC=3 SCAR=24.8
190 0 my ($heap, $operation, $errnum, $errstr) = @_[HEAP, ARG0, ARG1, ARG2];
191  
192 0 ▶ delete $heap->{server_wheel} if $errnum == ENFILE or $errnum == EMFILE;
Branch: 1 branch
BranchTrueFalseTotal
if $errnum == 23 or $errnum == 24 0 0 0
Condition0%0/3
$errnum == 23 or $errnum == 24
l!l&&r!l&&!r
000
MC/DC0%0/2
$errnum == 23 or $errnum == 24
$errnum == 23 $errnum == 24
Truth table0%0/3
A or B
A= $errnum == 23B= $errnum == 24
ABresult
1X1
011
000
193 }
194  
195 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
196 0 POE::Kernel->run();
197 }
198  
199 =head1 NAME
200  
201 POE::Component::Proxy::MySQL - A MySQL POE Proxy
202  
203 =head1 DESCRIPTION
204  
205 This modules helps building a MySQL proxy in which you can write
206 handler to deal with specific queries.
207  
208 You can modifiy the query, write a specific response, relay a query
209 or do wahtever you want within each handler.
210  
211 This is the evolution of POE::Component::DBIx::MyServer, it
212 uses Moose and POE.
213  
214 =head1 SYNOPSYS
215  
216 First you create a server class that extends POE::Component::Server::MySQL.
217  
218 package MyMySQL;
219 use Moose;
220
221 extends 'POE::Component::Server::MySQL';
222 with 'MooseX::Getopt';
223  
224 Then in a perl script you can instantiate your new server
225  
226 use MyMySQL;
227 my $server = MyMySQL->new_with_options();
228 $server->run;
229  
230 In the MyMySQL namespace you can add roles which will act as handlers
231 for your trapped queries:
232  
233 package MyMySQL::Hello;
234
235 use POE;
236 use MooseX::MethodAttributes::Role;
237
238 sub fortune : Regexp('qr{fortune}io') {
239 my ($self) = $_[OBJECT];
240
241 my $fortune = `fortune`;
242 chomp($fortune);
243
244 $self->client_send_results(
245 ['fortune'],
246 [[$fortune]]
247 );
248
249 }
250  
251  
252 Then run your proxy
253  
254 perl mymysql.pl --src_address localhost --src_port 23306 --dst_address localhost --dst_port 3306
255  
256  
257 and finally
258  
259 [eriam@fbsd ~]# mysql -u root -pxxx --host localhost --port 23306
260 Welcome to the MySQL monitor. Commands end with ; or \g.
261 Your MySQL connection id is 6
262 Server version: 5.1.40 Source distribution
263
264 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
265
266 mysql> fortune();
267 +---------------------------------------------------------------------+
268 | fortune |
269 +---------------------------------------------------------------------+
270 | How sharper than a hound's tooth it is to have a thankless serpent. |
271 +---------------------------------------------------------------------+
272 1 row in set (0,10 sec)
273  
274 =over 4
275  
276 =item DEBUG()
277  
278 =item BUILD()
279  
280 =item run()
281  
282 =back
283  
284 =head1 AUTHORS
285  
286 Eriam Schaffter, C<eriam@cpan.org> with original work
287 done by Philip Stoev in the DBIx::MyServer module.
288  
289 =head1 BUGS
290  
291 None that I know.
292  
293 =head1 LICENSE
294  
295 This program is free software, you can redistribute it and/or modify it
296 under the same terms as Perl itself.
297  
298 =cut
299  
300  
301  
302 1;
303  
« MySQL/Forwarder.pm ↑ Summary