blib/lib/Cassandra/Client/Pool.pm

filter: Statementstmt 12.9% 20 / 154 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 24.0% 6 / 25 Podpod 0.0% 0 / 14 totaltotal 9.7% 26 / 267 CC 36 SCAR 36.6
CC
36
Cov
9%
__ANON__
110.0
connect_if_needed
90.0
get_one_cb
30.0
CRAP 38.8
« blib/lib/Cassandra/Client/Protocol.pm ↑ Summary blib/lib/Cassandra/Client.pm »
1 package Cassandra::Client::Pool;
2 13 our $AUTHORITY = 'cpan:TVDW';
3 13 $Cassandra::Client::Pool::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 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Cassandra::Client::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use Cassandra::Client::NetworkStatus;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
13 0 my ($class, %args)= @_;
14 my $self= bless {
15 client => $args{client},
16 options => $args{options},
17 metadata => $args{metadata},
18 max_connections => $args{options}{max_connections},
19 async_io => $args{async_io},
20 policy => $args{load_balancing_policy},
21  
22 0 shutdown => 0,
23 pool => {},
24 count => 0,
25 list => [],
26  
27 last_id => 0,
28 id2ip => {},
29  
30 i => 0,
31  
32 connecting => {},
33 wait_connect => [],
34 }, $class;
35 0 weaken($self->{client});
36 0 $self->{network_status}= Cassandra::Client::NetworkStatus->new(pool => $self, async_io => $args{async_io});
37 0 return $self;
38 }
39  
40 0 ▶sub init {
Subroutine init: not called CC=2 SCAR=17.9
Pod undocumented
41 0 my ($self, $callback, $first_connection)= @_;
42  
43 # This code can be called twice.
44  
45 # If we didn't have a datacenter pinned before, now we do
46 0 ▶ $self->{policy}{datacenter} ||= $first_connection->{datacenter};
Condition0%0/3
$self->{"policy"}{"datacenter"} ||= $first_connection->{"datacenter"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"policy"}{"datacenter"} ||= $first_connection->{"datacenter"}
$self->{"policy"}{"datacenter"} $first_connection->{"datacenter"}
Truth table0%0/3
A ||= B
A= $self->{"policy"}{"datacenter"}B= $first_connection->{"datacenter"}
ABresult
1X1
011
000
47  
48 0 $self->add($first_connection);
49 0 $self->{policy}->set_connecting($first_connection->ip_address);
50 0 $self->{policy}->set_connected($first_connection->ip_address);
51  
52 # Master selection, warmup, etc
53 series([
54 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 0 my ($next)= @_;
56 0 $self->{network_status}->init($next);
57 },
58 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
59 0 my ($next)= @_;
60  
61 0 ▶ if ($self->{config}{warmup}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'config'}{'warmup'}) { } 0 0 0
62 0 $self->connect_if_needed($next);
63 } else {
64 0 $self->connect_if_needed();
65 0 return $next->();
66 }
67 },
68 0 ], $callback);
69 }
70  
71 0 ▶sub get_one {
Subroutine get_one: not called CC=2 SCAR=17.9
Pod undocumented
72 0 my ($self)= @_;
73 0 ▶ return undef unless $self->{count};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"count"} 0 0 0
74  
75 # Round-robin: pick the next one
76 0 return $self->{list}[$self->{i}= (($self->{i}+1) % $self->{count})];
77 }
78  
79 0 ▶sub get_one_cb {
Subroutine get_one_cb: not called CC=5 SCAR=34.0
Pod undocumented
80 0 my ($self, $callback)= @_;
81  
82 0 ▶ return $callback->(undef, $self->get_one) if $self->{count};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"count"} 0 0 0
83  
84 0 ▶ if (!%{$self->{connecting}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%{$self->{"connecting"};}) 0 0 0
85 0 $self->connect_if_needed;
86 }
87 0 ▶ if (!%{$self->{connecting}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%{$self->{"connecting"};}) 0 0 0
88 0 return $callback->("Disconnected: all servers unreachable");
89 }
90  
91 0 ▶ push @{$self->{wait_connect} ||= []}, {
Condition0%0/2
$self->{"wait_connect"} ||= []
l!l
00
MC/DC0%0/1
$self->{"wait_connect"} ||= []
$self->{"wait_connect"}
Truth table0%0/2
A
A= $self->{"wait_connect"}
Aresult
11
00
92 callback => $callback,
93 attempts => 0,
94 };
95 }
96  
97 0 ▶sub remove {
Subroutine remove: not called CC=4 SCAR=30.0
Pod undocumented
98 0 my ($self, $id)= @_;
99 0 ▶ if (!$id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id) 0 0 0
100 # Probably never got added. Ignore.
101 0 return;
102 }
103  
104 0 my $ipaddress= delete $self->{id2ip}{$id};
105 0 ▶ if (!$ipaddress) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ipaddress) 0 0 0
106 0 warn 'BUG: Tried to remove an unregistered connection. Probably a bad idea.';
107 0 return;
108 }
109  
110 0 my $connection= delete $self->{pool}{$ipaddress};
111 0 ▶ if (!$connection) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($connection) 0 0 0
112 0 warn 'BUG: Found a registered but unknown connection. This should not happen.';
113 0 return;
114 }
115  
116 0 $self->rebuild;
117  
118 0 $self->{policy}->set_disconnected($ipaddress);
119 0 $self->{network_status}->disconnected($connection->get_pool_id);
120 0 $self->connect_if_needed;
121  
122 0 return;
123 }
124  
125 0 ▶sub add {
Subroutine add: not called CC=3 SCAR=24.8
Pod undocumented
126 0 my ($self, $connection)= @_;
127  
128 0 my $ipaddress= $connection->ip_address;
129  
130 0 ▶ if ($self->{pool}{$ipaddress}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pool"}{$ipaddress}) 0 0 0
131 0 warn 'BUG: Duplicate connection for '.$ipaddress.'!';
132 }
133  
134 0 my $id= (++($self->{last_id}));
135 0 $connection->set_pool_id($id);
136 0 $self->{pool}{$ipaddress}= $connection;
137 0 $self->{id2ip}{$id}= $ipaddress;
138  
139 0 $self->rebuild;
140  
141 0 my $waiters= delete $self->{wait_connect};
142 0 $_->{callback}->(undef, $connection) for @$waiters;
143  
144 0 ▶ $self->{network_status}->select_master(sub{});
Subroutine __ANON__: not called CC=1 SCAR=6.9
145  
146 0 return;
147 }
148  
149 0 ▶sub rebuild {
Subroutine rebuild: not called CC=1 SCAR=6.9
Pod undocumented
150 0 my ($self)= @_;
151  
152 0 $self->{list}= [ values %{$self->{pool}} ];
153 0 $self->{count}= 0+ @{$self->{list}};
154  
155 0 return;
156 }
157  
158 0 ▶sub shutdown {
Subroutine shutdown: not called CC=3 SCAR=24.8
Pod undocumented
159 0 my ($self)= @_;
160  
161 0 $self->{network_status}->shutdown;
162 0 $self->{shutdown}= 1;
163  
164 0 my @pool= @{$self->{list}};
165 0 $_->shutdown("Shutting down") for @pool;
166  
167 0 my @connecting= values %{$self->{connecting}};
168 0 $_->shutdown("Shutting down") for @connecting;
169  
170 0 return;
171 }
172  
173 0 ▶sub connect_if_needed {
Subroutine connect_if_needed: not called CC=9 SCAR=45.0
Pod undocumented
174 0 my ($self, $callback)= @_;
175  
176 0 my $max_connect= $self->{max_connections} - $self->{count};
177 0 ▶ return if $max_connect <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $max_connect <= 0 0 0 0
178  
179 0 $max_connect -= keys %{$self->{connecting}};
180 0 ▶ return if $max_connect <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $max_connect <= 0 0 0 0
181  
182 0 ▶ return if $self->{shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shutdown"} 0 0 0
183  
184 0 ▶ if ($self->{_in_connect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_in_connect"}) 0 0 0
185 0 return;
186 }
187 0 local $self->{_in_connect}= 1;
188  
189 0 my $done= 0;
190 0 my $expect= $max_connect;
191 0 for (1..$max_connect) {
192 0 ▶ $expect-- unless $self->spawn_new_connection(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
193 0 $done++;
194  
195 0 ▶ if ($done == $expect) {
Branch: 1 branch
BranchTrueFalseTotal
if ($done == $expect) 0 0 0
196 0 ▶ $callback->() if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 0 0 0
197 0 undef $callback;
198 }
199 0 ▶ });
Branch: 1 branch
BranchTrueFalseTotal
unless $self->spawn_new_connection(sub { ++$done; if ($done == $expect) { &$callback() if $callback; undef $callback; } ; } ) 0 0 0
200 }
201 0 ▶ if ($callback && !$expect) {
Branch: 1 branch
BranchTrueFalseTotal
if ($callback and not $expect) 0 0 0
Condition0%0/3
$callback and not $expect
!ll&&!rl&&r
000
MC/DC0%0/2
$callback and not $expect
$callback not $expect
Truth table0%0/3
A and not(B)
A= $callbackB= not $expect
ABresult
0X0
100
111
202 0 $callback->();
203 }
204 }
205  
206 0 ▶sub spawn_new_connection {
Subroutine spawn_new_connection: not called CC=2 SCAR=17.9
Pod undocumented
207 0 my ($self, $callback)= @_;
208  
209 0 my $host= $self->{policy}->get_next_candidate;
210 0 ▶ return unless $host;
Branch: 1 branch
BranchTrueFalseTotal
unless $host 0 0 0
211  
212 my $connection= Cassandra::Client::Connection->new(
213 client => $self->{client},
214 options => $self->{options},
215 host => $host,
216 async_io => $self->{async_io},
217 metadata => $self->{metadata},
218 0 );
219  
220 0 $self->{connecting}{$host}= $connection;
221 0 $self->{policy}->set_connecting($host);
222  
223 0 ▶ $connection->connect(sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
224 0 my ($error)= @_;
225  
226 0 delete $self->{connecting}{$host};
227 0 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) { } 0 0 0
228 0 $self->{policy}->set_disconnected($host);
229  
230 0 ▶ if (my $waiters= delete $self->{wait_connect}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $waiters = delete $self->{"wait_connect"}) 0 0 0
231 0 ▶ if ($self->{count} && @$waiters) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"count"} and @$waiters) 0 0 0
Condition0%0/3
$self->{"count"} and @$waiters
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"count"} and @$waiters
$self->{"count"} @$waiters
Truth table0%0/3
A and B
A= $self->{"count"}B= @$waiters
ABresult
0X0
100
111
232 0 warn 'We have callbacks waiting for a connection while we\'re connected';
233 }
234  
235 0 my $max_conn= $self->{max_connections};
236 0 my $known_node_count= $self->{policy}->known_node_count;
237 0 ▶ my $max_attempts = ($max_conn < $known_node_count ? $max_conn : $known_node_count) + 1;
Branch: 1 branch
BranchTrueFalseTotal
$max_conn < $known_node_count ? : 0 0 0
238  
239 0 for my $waiter (@$waiters) {
240 0 ▶ if ((++$waiter->{attempts}) >= $max_attempts || !%{$self->{connecting}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$waiter->{'attempts'} >= $max_attempts or not %{$self->{'connecting'};}) { } 0 0 0
Condition0%0/3
++$waiter->{"attempts"} >= $max_attempts or not %{$self->{"connecting"};}
l!l&&r!l&&!r
000
MC/DC0%0/2
++$waiter->{"attempts"} >= $max_attempts or not %{$self->{"connecting"};}
++$waiter->{"attempts"} >= $max_attempts not %{$self->{"connecting"};}
Truth table0%0/3
A or not(B)
A= ++$waiter->{"attempts"} >= $max_attemptsB= not %{$self->{"connecting"};}
ABresult
1X1
011
000
241 0 $waiter->{callback}->("Failed to connect to server: $error");
242 } else {
243 0 ▶ push @{$self->{wait_connect} ||= []}, $waiter;
Condition0%0/2
$self->{"wait_connect"} ||= []
l!l
00
MC/DC0%0/1
$self->{"wait_connect"} ||= []
$self->{"wait_connect"}
Truth table0%0/2
A
A= $self->{"wait_connect"}
Aresult
11
00
244 }
245 }
246 }
247  
248 0 $self->connect_if_needed;
249 } else {
250 0 $self->{policy}->set_connected($host);
251  
252 0 $self->add($connection);
253 }
254  
255 0 $callback->($error);
256 0 });
257  
258 0 return 1;
259 }
260  
261 # Events coming from the network
262 0 ▶sub event_added_node {
Subroutine event_added_node: not called CC=1 SCAR=6.9
Pod undocumented
263 0 my ($self, $ipaddress)= @_;
264 0 $self->{network_status}->event_added_node($ipaddress);
265 }
266  
267 0 ▶sub event_removed_node {
Subroutine event_removed_node: not called CC=2 SCAR=17.9
Pod undocumented
268 0 my ($self, $ipaddress)= @_;
269 0 $self->{network_status}->event_removed_node($ipaddress);
270  
271 0 ▶ if (my $conn= $self->{pool}{$ipaddress}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $conn = $self->{"pool"}{$ipaddress}) 0 0 0
272 0 $conn->shutdown("Removed from pool");
273 }
274 }
275  
276 # Events coming from network_status
277 0 ▶sub on_new_node {
Subroutine on_new_node: not called CC=1 SCAR=6.9
Pod undocumented
278 0 my ($self, $node)= @_;
279 0 $self->{policy}->on_new_node($node);
280 }
281  
282 0 ▶sub on_removed_node {
Subroutine on_removed_node: not called CC=1 SCAR=6.9
Pod undocumented
283 0 my ($self, $node)= @_;
284 0 $self->{policy}->on_removed_node($node);
285 }
286  
287 13 1;
288  
289 __END__
« blib/lib/Cassandra/Client/Protocol.pm ↑ Summary blib/lib/Cassandra/Client.pm »