Stomper/Cluster.pm

filter: Statementstmt 57.8% 114 / 197 Branchbran 22.9% 11 / 48 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 65.6% 21 / 32 Podpod 100% 5 / 5 totaltotal 48.2% 151 / 313 CC 36 SCAR 30.8
CC
36
Cov
47%
_prepare
72.0
__ANON__
72.0
__ANON__
20.0
CRAP 21.7
« Stomper.pm ↑ Summary Stomper/Frame.pm »
1 package AnyEvent::Stomper::Cluster;
2  
3 3 ▶use 5.008000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = '0.36';
9  
10 3 ▶use AnyEvent::Stomper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use AnyEvent::Stomper::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 our %ERROR_CODES;
17  
18 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 %ERROR_CODES = %AnyEvent::Stomper::Error::ERROR_CODES;
20 3 our @EXPORT_OK = keys %ERROR_CODES;
21 3 our %EXPORT_TAGS = ( err_codes => \@EXPORT_OK );
22 }
23  
24 3 ▶use constant \%ERROR_CODES;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 3 my %ACK_CMDS = (
27 ACK => 1,
28 NACK => 1,
29 );
30  
31 3 my %CAN_REPEAT = (
32 SEND => 1,
33 SUBSCRIBE => 1,
34 BEGIN => 1,
35 );
36  
37  
38 4 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
39 4 my $class = shift;
40 4 my %params = @_;
41  
42 4 my $self = bless {}, $class;
43  
44 4 ▶ unless ( defined $params{nodes} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"nodes"}) 1 3 4
45 1 croak 'Nodes not specified';
46 }
47 3 ▶ unless ( ref( $params{nodes} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $params{"nodes"} eq "ARRAY") 1 2 3
48 1 croak 'Nodes must be specified as array reference';
49 }
50 2 ▶ unless ( @{ $params{nodes} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$params{"nodes"};}) 1 1 2
51 1 croak 'Specified empty list of nodes';
52 }
53  
54 1 $self->{nodes} = $params{nodes};
55 1 $self->{on_node_connect} = $params{on_node_connect};
56 1 $self->{on_node_disconnect} = $params{on_node_disconnect};
57 1 $self->{on_node_error} = $params{on_node_error};
58 1 $self->on_error( $params{on_error} );
59  
60 1 my %node_params;
61 1 foreach my $name ( qw( login passcode vhost heartbeat connection_timeout
62 reconnect_interval handle_params default_headers command_headers ) )
63 {
64 9 ▶ next unless defined $params{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{$name} 9 0 9
65 0 $node_params{$name} = $params{$name};
66 }
67 1 $self->{_node_params} = \%node_params;
68  
69 1 $self->_reset_internals;
70 1 $self->_init;
71  
72 1 return $self;
73 }
74  
75 0 ▶sub execute {
Subroutine execute: not called CC=1 SCAR=6.9
Pod documented
76 0 my $self = shift;
77 0 my $cmd_name = shift;
78  
79 0 my $cmd = $self->_prepare( $cmd_name, [@_] );
80 0 $self->_execute($cmd);
81  
82 0 return;
83 }
84  
85 # Generate methods
86 {
87 3 ▶ no strict qw( refs );
Subroutine BEGIN: called CC=1 SCAR=0.0
88  
89 3 foreach my $name ( qw( send subscribe unsubscribe ack nack begin commit
90 abort disconnect ) )
91 {
92 27 ▶ *{$name} = sub {
Subroutine __ANON__: not called CC=1 SCAR=4.1
93 0 my $self = shift;
94  
95 0 my $cmd = $self->_prepare( $name, [@_] );
96 0 $self->_execute($cmd);
97  
98 0 return;
99 }
100 27 }
101 }
102  
103 1 ▶sub nodes {
Subroutine nodes: called CC=1 SCAR=0.0
Pod documented
104 1 my $self = shift;
105 1 return values %{ $self->{_nodes_pool} };
106 }
107  
108 1 ▶sub on_error {
Subroutine on_error: called CC=3 SCAR=12.6
Pod documented
109 1 my $self = shift;
110  
111 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
112 1 my $on_error = shift;
113  
114 1 ▶ if ( defined $on_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $on_error) { } 0 1 1
115 0 $self->{on_error} = $on_error;
116 }
117 else {
118 0 ▶ $self->{on_error} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
119 0 my $err = shift;
120 0 warn $err->message . "\n";
121 1 };
122 }
123 }
124  
125 1 return $self->{on_error};
126 }
127  
128 0 ▶sub force_disconnect {
Subroutine force_disconnect: not called CC=2 SCAR=17.9
Pod documented
129 0 my $self = shift;
130  
131 0 foreach my $node ( $self->nodes ) {
132 0 $node->force_disconnect;
133 }
134 0 $self->_reset_internals;
135  
136 0 return;
137 }
138  
139 1 ▶sub _init {
Subroutine _init: called CC=3 SCAR=11.0
140 1 my $self = shift;
141  
142 1 my $nodes_pool = $self->{_nodes_pool};
143  
144 1 foreach my $node_params ( @{ $self->{nodes} } ) {
145 3 my $hostport = "$node_params->{host}:$node_params->{port}";
146  
147 3 ▶ unless ( defined $nodes_pool->{$hostport} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nodes_pool->{$hostport}) 3 0 3
148 $nodes_pool->{$hostport}
149 3 = $self->_new_node( $node_params->{host}, $node_params->{port} );
150 }
151 }
152  
153 1 $self->{_nodes} = [ keys %{ $self->{_nodes_pool} } ];
154 1 $self->{_active_node} = $self->_next_node;
155  
156 1 return;
157 }
158  
159 3 ▶sub _new_node {
Subroutine _new_node: called CC=1 SCAR=0.0
160 3 my $self = shift;
161 3 my $host = shift;
162 3 my $port = shift;
163  
164 return AnyEvent::Stomper->new(
165 3 %{ $self->{_node_params} },
166 host => $host,
167 port => $port,
168 lazy => 1,
169 on_connect => $self->_create_on_node_connect( $host, $port ),
170 on_disconnect => $self->_create_on_node_disconnect( $host, $port ),
171 on_error => $self->_create_on_node_error( $host, $port ),
172 );
173 }
174  
175 3 ▶sub _create_on_node_connect {
Subroutine _create_on_node_connect: called CC=1 SCAR=0.8
176 3 my $self = shift;
177 3 my $host = shift;
178 3 my $port = shift;
179  
180 3 weaken($self);
181  
182 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
183 0 ▶ if ( defined $self->{on_node_connect} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"on_node_connect"}) 0 0 0
184 0 $self->{on_node_connect}->( $host, $port );
185 }
186 3 };
187 }
188  
189 3 ▶sub _create_on_node_disconnect {
Subroutine _create_on_node_disconnect: called CC=1 SCAR=0.8
190 3 my $self = shift;
191 3 my $host = shift;
192 3 my $port = shift;
193  
194 3 weaken($self);
195  
196 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
197 0 ▶ if ( defined $self->{on_node_disconnect} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"on_node_disconnect"}) 0 0 0
198 0 $self->{on_node_disconnect}->( $host, $port );
199 }
200 3 };
201 }
202  
203 3 ▶sub _create_on_node_error {
Subroutine _create_on_node_error: called CC=1 SCAR=3.2
204 3 my $self = shift;
205 3 my $host = shift;
206 3 my $port = shift;
207  
208 3 weaken($self);
209  
210 0 ▶ return sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
211 0 my $err = shift;
212  
213 0 my $err_code = $err->code;
214  
215 0 ▶ if ( $err_code != E_OPRN_ERROR
Branch: 1 branch
BranchTrueFalseTotal
if ($err_code != 5 and $err_code != 4) 0 0 0
Condition0%0/3
$err_code != 5 and $err_code != 4
!ll&&!rl&&r
000
MC/DC0%0/2
$err_code != 5 and $err_code != 4
$err_code != 5 $err_code != 4
Truth table0%0/3
A and B
A= $err_code != 5B= $err_code != 4
ABresult
0X0
100
111
216 && $err_code != E_CONN_CLOSED_BY_CLIENT )
217 {
218 0 $self->{_active_node} = $self->_next_node;
219 }
220  
221 0 ▶ if ( defined $self->{on_node_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"on_node_error"}) 0 0 0
222 0 $self->{on_node_error}->( $err, $host, $port );
223 }
224 3 };
225 }
226  
227 0 ▶sub _prepare {
Subroutine _prepare: not called CC=8 SCAR=42.8
228 0 my $self = shift;
229 0 my $cmd_name = uc(shift);
230 0 my $args = shift;
231  
232 0 my %params;
233  
234 0 ▶ if ( ref( $args->[-1] ) eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->[-1] eq "CODE" and scalar @{$args;} % 2 > 0) 0 0 0
Condition0%0/3
ref $args->[-1] eq "CODE" and scalar @{$args;} % 2 > 0
!ll&&!rl&&r
000
MC/DC0%0/2
ref $args->[-1] eq "CODE" and scalar @{$args;} % 2 > 0
ref $args->[-1] eq "CODE" scalar @{$args;} % 2 > 0
Truth table0%0/3
A and B
A= ref $args->[-1] eq "CODE"B= scalar @{$args;} % 2 > 0
ABresult
0X0
100
111
235 0 && scalar @{$args} % 2 > 0 )
236 {
237 0 ▶ if ( $cmd_name eq 'SUBSCRIBE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd_name eq 'SUBSCRIBE') { } 0 0 0
238 0 $params{on_message} = pop @{$args};
239 }
240 else {
241 0 $params{on_receipt} = pop @{$args};
242 }
243 }
244  
245 0 my %headers = @{$args};
246  
247 0 foreach my $name ( qw( body on_receipt on_message on_node_error ) ) {
248 0 ▶ if ( defined $headers{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $headers{$name}) 0 0 0
249 0 $params{$name} = delete $headers{$name};
250 }
251 }
252 0 ▶ if ( exists $ACK_CMDS{$cmd_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ACK_CMDS{$cmd_name}) 0 0 0
253 0 $params{message} = delete $headers{message};
254 }
255  
256 0 my $cmd = {
257 name => $cmd_name,
258 headers => \%headers,
259 %params,
260 };
261  
262 0 ▶ unless ( defined $cmd->{on_receipt} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cmd->{"on_receipt"}) 0 0 0
263 0 weaken($self);
264  
265 0 ▶ $cmd->{on_receipt} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
266 0 my $receipt = shift;
267 0 my $err = shift;
268  
269 0 ▶ if ( defined $err ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 0 0 0
270 0 $self->{on_error}->($err);
271 0 return;
272 }
273 0 };
274 }
275  
276 0 return $cmd;
277 }
278  
279 0 ▶sub _execute {
Subroutine _execute: not called CC=4 SCAR=30.0
280 0 my $self = shift;
281 0 my $cmd = shift;
282 0 ▶ my $fails_cnt = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
283  
284 0 my $hostport = $self->{_active_node};
285 0 my $node = $self->{_nodes_pool}{$hostport};
286  
287 0 weaken($self);
288  
289 0 ▶ $node->execute( $cmd->{name}, %{ $cmd->{headers} },
Branch: 2 branches
BranchTrueFalseTotal
defined $cmd->{'message'} ? : 0 0 0
defined $cmd->{'on_message'} ? : 0 0 0
290 body => $cmd->{body},
291  
292 0 ▶ on_receipt => sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
293 0 my $receipt = shift;
294 0 my $err = shift;
295  
296 0 ▶ if ( defined $err ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 0 0 0
297 0 my $err_code = $err->code;
298  
299 0 ▶ my $on_node_error = $cmd->{on_node_error} || $self->{on_node_error};
Condition0%0/3
$cmd->{'on_node_error'} || $self->{'on_node_error'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$cmd->{'on_node_error'} || $self->{'on_node_error'}
$cmd->{'on_node_error'} $self->{'on_node_error'}
Truth table0%0/3
A || B
A= $cmd->{'on_node_error'}B= $self->{'on_node_error'}
ABresult
1X1
011
000
300 0 ▶ if ( defined $on_node_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $on_node_error) 0 0 0
301 0 my $node = $self->{_nodes_pool}{$hostport};
302 0 $on_node_error->( $err, $node->host, $node->port );
303 }
304  
305 0 ▶ if ( $CAN_REPEAT{ $cmd->{name} }
Branch: 1 branch
BranchTrueFalseTotal
if ($CAN_REPEAT{$cmd->{"name"}} and $err_code != 5 and $err_code != 4 and ++$fails_cnt < scalar @{$self->{"_nodes"};}) 0 0 0
Condition0%0/9
$CAN_REPEAT{$cmd->{"name"}} and $err_code != 5 and $err_code != 4 and ++$fails_cnt < scalar @{$self->{"_nodes"};}
!ll&&!rl&&r
000
$CAN_REPEAT{$cmd->{"name"}} and $err_code != 5 and $err_code != 4
!ll&&!rl&&r
000
$CAN_REPEAT{$cmd->{"name"}} and $err_code != 5
!ll&&!rl&&r
000
MC/DC0%0/4
$CAN_REPEAT{$cmd->{"name"}} and $err_code != 5 and $err_code != 4 and ++$fails_cnt < scalar @{$self->{"_nodes"};}
$CAN_REPEAT{$cmd->{"name"}} $err_code != 5 $err_code != 4 ++$fails_cnt < scalar @{$self->{"_nodes"};}
Truth table0%0/5
A and B and C and D
A= $CAN_REPEAT{$cmd->{"name"}}B= $err_code != 5C= $err_code != 4D= ++$fails_cnt < scalar @{$self->{"_nodes"};}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
306 && $err_code != E_OPRN_ERROR
307 && $err_code != E_CONN_CLOSED_BY_CLIENT
308 0 && ++$fails_cnt < scalar @{ $self->{_nodes} } )
309 {
310 0 $self->_execute( $cmd, $fails_cnt );
311 0 return;
312 }
313  
314 0 $cmd->{on_receipt}->( $receipt, $err );
315  
316 0 return;
317 }
318  
319 0 $cmd->{on_receipt}->($receipt);
320 },
321  
322 defined $cmd->{message}
323 ? ( message => $cmd->{message} )
324 : (),
325  
326 defined $cmd->{on_message}
327 ? ( on_message => $cmd->{on_message} )
328 0 : (),
329 );
330  
331 0 return;
332 }
333  
334 1 ▶sub _next_node {
Subroutine _next_node: called CC=3 SCAR=14.2
335 1 my $self = shift;
336  
337 1 ▶ unless ( defined $self->{_node_index} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'_node_index'}) { } 0 1 1
if (do { $self->{"_node_index"} == scalar @{$self->{"_nodes"};} }) 0 0 0
338 1 $self->{_node_index} = int( rand( scalar @{ $self->{_nodes} } ) );
339 }
340 0 elsif ( $self->{_node_index} == scalar @{ $self->{_nodes} } ) {
341 0 $self->{_node_index} = 0;
342 }
343  
344 1 return $self->{_nodes}[ $self->{_node_index}++ ];
345 }
346  
347 1 ▶sub _reset_internals {
Subroutine _reset_internals: called CC=1 SCAR=0.0
348 1 my $self = shift;
349  
350 1 $self->{_nodes_pool} = {};
351 1 $self->{_nodes} = undef;
352 1 $self->{_node_index} = undef;
353 1 $self->{_active_node} = undef;
354  
355 1 return;
356 }
357  
358 3 1;
359 __END__
« Stomper.pm ↑ Summary Stomper/Frame.pm »