blib/lib/Cassandra/Client/NetworkStatus.pm

filter: Statementstmt 18.2% 17 / 93 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 16.6% 5 / 30 Podpod 0.0% 0 / 9 totaltotal 13.6% 22 / 161 CC 14 SCAR 18.6
CC
14
Cov
15%
__ANON__
20.0
select_master
12.0
load_status
12.0
CRAP 6.4
« blib/lib/Cassandra/Client/Policy/LoadBalancing/Default.pm ↑ Summary blib/lib/Cassandra/Client/Policy/Queue/Default.pm »
1 package Cassandra::Client::NetworkStatus;
2 13 our $AUTHORITY = 'cpan:TVDW';
3 13 $Cassandra::Client::NetworkStatus::VERSION = '0.21';
4 13 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Cassandra::Client::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
12 0 my ($class, %args)= @_;
13  
14 my $self= bless {
15 pool => $args{pool},
16 async_io => $args{async_io},
17  
18 0 waiting_for_cb => [],
19 master_id => undef,
20  
21 shutdown => undef,
22 }, $class;
23 0 weaken($self->{pool});
24 0 return $self;
25 }
26  
27 0 ▶sub init {
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
28 0 my ($self, $callback)= @_;
29 0 $self->select_master($callback);
30 }
31  
32 0 ▶sub select_master {
Subroutine select_master: not called CC=3 SCAR=24.8
Pod undocumented
33 0 my ($self, $callback)= @_;
34  
35 0 ▶ return $callback->() if $self->{master_id};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"master_id"} 0 0 0
36 0 ▶ if (@{$self->{waiting_for_cb}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"waiting_for_cb"};}) 0 0 0
37 0 push @{$self->{waiting_for_cb}}, $callback;
38 0 return;
39 }
40 0 push @{$self->{waiting_for_cb}}, $callback;
41  
42 0 my $pool= $self->{pool}; # non-weak
43  
44 0 my $attempts= 0;
45 whilst(
46 0 ▶ sub { # condition
Subroutine __ANON__: not called CC=2 SCAR=17.9
47 !$self->{shutdown} && !$self->{master_id}
48 0 ▶ },
Condition0%0/3
$self->{'shutdown'} || $self->{'master_id'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'shutdown'} || $self->{'master_id'}
$self->{'shutdown'} $self->{'master_id'}
Truth table0%0/3
A || B
A= $self->{'shutdown'}B= $self->{'master_id'}
ABresult
1X1
011
000
49 0 ▶ sub { # while
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 0 my ($wnext)= @_;
51 series([
52 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
53 0 my ($next)= @_;
54 0 ▶ if ($attempts++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attempts++) { } 0 0 0
55 # Don't retry immediately
56 0 $self->{async_io}->timer($next, 1);
57 } else {
58 0 $next->();
59 }
60 },
61 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 0 my ($next)= @_;
63 0 $pool->get_one_cb($next);
64 },
65 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 my ($next, $connection)= @_;
67 parallel([
68 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
69 0 my ($pnext)= @_;
70 0 $connection->register_events($pnext);
71 },
72 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 0 my ($pnext)= @_;
74 0 $connection->get_network_status($pnext);
75 },
76 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
77 0 $_[0]->(undef, $connection);
78 },
79 0 ], $next);
80 0 ▶ }, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
81 0 my ($next, undef, $networkstatus, $connection)= @_;
82 0 $self->{master_id}= $connection->get_pool_id;
83 0 $self->load_status($networkstatus);
84 0 $next->();
85 },
86 0 ▶ ], sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 0 $wnext->();
88 0 });
89 },
90 0 ▶ sub { # finish
Subroutine __ANON__: not called CC=4 SCAR=30.0
91 0 my ($error)= @_;
92 0 my @cb= @{$self->{waiting_for_cb}};
93 0 $self->{waiting_for_cb}= [];
94 0 ▶ $error= $error || ($self->{master_id} ? undef : "Master selection aborted");
Branch: 1 branch
BranchTrueFalseTotal
$self->{'master_id'} ? : 0 0 0
Condition0%0/3
$error || ($self->{'master_id'} ? undef : 'Master selection aborted')
l!l&&r!l&&!r
000
MC/DC0%0/2
$error || ($self->{'master_id'} ? undef : 'Master selection aborted')
$error ($self->{'master_id'} ? undef : 'Master selection aborted')
Truth table0%0/3
A || B
A= $errorB= ($self->{'master_id'} ? undef : 'Master selection aborted')
ABresult
1X1
011
000
95 0 $_->($error) for @cb;
96 }
97 0 );
98 }
99  
100 0 ▶sub shutdown {
Subroutine shutdown: not called CC=1 SCAR=6.9
Pod undocumented
101 0 my ($self)= @_;
102 0 $self->{shutdown}= 1;
103 }
104  
105 0 ▶sub load_status {
Subroutine load_status: not called CC=3 SCAR=24.8
Pod undocumented
106 0 my ($self, $new_status)= @_;
107 0 my $old_status= $self->{status};
108 0 $self->{status}= $new_status;
109  
110 0 my @old_hosts= grep {!$new_status->{$_}} keys %$old_status;
111 0 my @new_hosts= grep {!$old_status->{$_}} keys %$new_status;
112  
113 0 $self->{pool}->on_removed_node($old_status->{$_}) for @old_hosts;
114 0 $self->{pool}->on_new_node($new_status->{$_}) for @new_hosts;
115 }
116  
117 0 ▶sub event_added_node {
Subroutine event_added_node: not called CC=2 SCAR=17.9
Pod undocumented
118 0 my ($self, $ipaddress)= @_;
119 0 ▶ $self->refresh_network_status unless $self->{status}{$ipaddress};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"status"}{$ipaddress} 0 0 0
120 }
121  
122 0 ▶sub event_removed_node {
Subroutine event_removed_node: not called CC=2 SCAR=17.9
Pod undocumented
123 0 my ($self, $ipaddress)= @_;
124 0 my $old_node= delete $self->{status}{$ipaddress};
125 0 ▶ if ($old_node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_node) 0 0 0
126 0 $self->{pool}->on_removed_node($old_node);
127 }
128 }
129  
130 0 ▶sub disconnected {
Subroutine disconnected: not called CC=3 SCAR=24.8
Pod undocumented
131 0 my ($self, $id)= @_;
132 0 ▶ if ($self->{master_id} && $self->{master_id} == $id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"master_id"} and $self->{"master_id"} == $id) 0 0 0
Condition0%0/3
$self->{"master_id"} and $self->{"master_id"} == $id
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"master_id"} and $self->{"master_id"} == $id
$self->{"master_id"} $self->{"master_id"} == $id
Truth table0%0/3
A and B
A= $self->{"master_id"}B= $self->{"master_id"} == $id
ABresult
0X0
100
111
133 0 $self->{master_id}= undef;
134 0 ▶ $self->select_master(sub{});
Subroutine __ANON__: not called CC=1 SCAR=6.9
135 }
136 }
137  
138 0 ▶sub refresh_network_status {
Subroutine refresh_network_status: not called CC=1 SCAR=6.9
Pod undocumented
139 0 my ($self)= @_;
140  
141 series([
142 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
143 0 my ($next)= @_;
144 0 $self->{pool}->get_one_cb($next);
145 0 ▶ }, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
146 0 my ($next, $connection)= @_;
147 0 $connection->get_network_status($next);
148 0 ▶ }, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
149 0 my ($next, $status)= @_;
150 0 $self->load_status($status);
151 0 return $next->();
152 }
153 0 ▶ ], sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
154 0 my ($error)= @_;
155 # XXX And now?
156 0 });
157 }
158  
159 13 1;
160  
161 __END__
« blib/lib/Cassandra/Client/Policy/LoadBalancing/Default.pm ↑ Summary blib/lib/Cassandra/Client/Policy/Queue/Default.pm »