blib/lib/Cassandra/Client/Policy/LoadBalancing/Default.pm

filter: Statementstmt 26.3% 19 / 72 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 33.3% 5 / 15 Podpod 0.0% 0 / 10 totaltotal 17.0% 24 / 141 CC 17 SCAR 27.7
CC
17
Cov
18%
get_next_candidate
42.0
on_new_node
20.0
check_backoff
20.0
CRAP 16.0
« blib/lib/Cassandra/Client/Policy/Retry/Default.pm ↑ Summary blib/lib/Cassandra/Client/NetworkStatus.pm »
1 package Cassandra::Client::Policy::LoadBalancing::Default;
2 13 our $AUTHORITY = 'cpan:TVDW';
3 13 $Cassandra::Client::Policy::LoadBalancing::Default::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 13 ▶use List::Util 'shuffle';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Time::HiRes qw/time/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
11 0 my ($class, %args)= @_;
12 0 return bless {
13 datacenter => undef,
14 nodes => {},
15 local_nodes => {},
16 connected => {},
17 candidates => [],
18 try_times => {},
19 }, $class;
20 }
21  
22 0 ▶sub get_distance {
Subroutine get_distance: not called CC=3 SCAR=24.8
Pod undocumented
23 0 my ($self, $peer)= @_;
24 0 my $node= $self->{nodes}{$peer};
25 0 ▶ if (!$node) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node) 0 0 0
26 0 warn 'Being asked about a distance for a node we don\'t know';
27 0 return 'ignored';
28 }
29  
30 0 ▶ if ($self->{local_nodes}{$peer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"local_nodes"}{$peer}) 0 0 0
31 0 return 'local';
32 }
33 0 return 'remote';
34 }
35  
36 0 ▶sub on_new_node {
Subroutine on_new_node: not called CC=4 SCAR=30.0
Pod undocumented
37 0 my ($self, $node)= @_;
38  
39 0 my $peer= $node->{peer};
40 0 ▶ if ($self->{nodes}{$peer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"nodes"}{$peer}) 0 0 0
41 0 warn 'BUG: "new" node is already known!';
42 }
43  
44 0 $self->{nodes}{$peer}= $node;
45 0 ▶ if (!$self->{datacenter} || $node->{data_center} eq $self->{datacenter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"datacenter"} or $node->{"data_center"} eq $self->{"datacenter"}) 0 0 0
Condition0%0/3
not $self->{"datacenter"} or $node->{"data_center"} eq $self->{"datacenter"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $self->{"datacenter"} or $node->{"data_center"} eq $self->{"datacenter"}
not $self->{"datacenter"} $node->{"data_center"} eq $self->{"datacenter"}
Truth table0%0/3
not(A) or B
A= not $self->{"datacenter"}B= $node->{"data_center"} eq $self->{"datacenter"}
ABresult
1X1
011
000
46 0 $self->{local_nodes}{$peer}= $node;
47 }
48 }
49  
50 0 ▶sub on_removed_node {
Subroutine on_removed_node: not called CC=2 SCAR=17.9
Pod undocumented
51 0 my ($self, $node)= @_;
52  
53 0 my $peer= $node->{peer};
54 0 ▶ if (!$self->{nodes}{$peer}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"nodes"}{$peer}) 0 0 0
55 0 warn 'BUG: "removed" node wasn\'t there!';
56 }
57  
58 0 delete $self->{nodes}{$peer};
59 0 delete $self->{local_nodes}{$peer};
60 }
61  
62 0 ▶sub get_next_candidate {
Subroutine get_next_candidate: not called CC=6 SCAR=37.4
Pod undocumented
63 0 my ($self)= @_;
64 0 my $candidates= $self->{candidates};
65 0 ▶ while (my $maybe= shift @$candidates) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $maybe = shift @$candidates) 0 0 0
66 0 ▶ if ($self->{local_nodes}{$maybe} && !$self->{connected}{$maybe} && $self->check_backoff($maybe)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"local_nodes"}{$maybe} and not $self->{"connected"}{$maybe} and $self->check_backoff($maybe)) 0 0 0
Condition0%0/6
$self->{"local_nodes"}{$maybe} and not $self->{"connected"}{$maybe} and $self->check_backoff($maybe)
!ll&&!rl&&r
000
$self->{"local_nodes"}{$maybe} and not $self->{"connected"}{$maybe}
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"local_nodes"}{$maybe} and not $self->{"connected"}{$maybe} and $self->check_backoff($maybe)
$self->{"local_nodes"}{$maybe} not $self->{"connected"}{$maybe} $self->check_backoff($maybe)
Truth table0%0/4
A and not(B) and C
A= $self->{"local_nodes"}{$maybe}B= not $self->{"connected"}{$maybe}C= $self->check_backoff($maybe)
ABCresult
0XX0
10X0
1100
1111
67 0 return $maybe;
68 }
69 }
70 0 ▶ @$candidates= shuffle grep { !$self->{connected}{$_} && $self->check_backoff($_) } keys %{$self->{local_nodes}};
Condition0%0/3
!$self->{'connected'}{$_} && $self->check_backoff($_)
!ll&&!rl&&r
000
MC/DC0%0/2
!$self->{'connected'}{$_} && $self->check_backoff($_)
!$self->{'connected'}{$_} $self->check_backoff($_)
Truth table0%0/3
not(A) && B
A= !$self->{'connected'}{$_}B= $self->check_backoff($_)
ABresult
0X0
100
111
71 0 return shift @$candidates;
72 }
73  
74 13 my @all_backoff= map { $_ * (rand()*.4 + 0.8) } (1, 5, 20, 60, 180, 600);
75 0 ▶sub check_backoff {
Subroutine check_backoff: not called CC=4 SCAR=30.0
Pod undocumented
76 0 my ($self, $peer)= @_;
77 0 my $times= $self->{try_times}{$peer};
78 0 ▶ return 1 unless $times;
Branch: 1 branch
BranchTrueFalseTotal
unless $times 0 0 0
79  
80 0 my $count= 0+@$times;
81 0 ▶ $count= @all_backoff if $count > @all_backoff;
Branch: 1 branch
BranchTrueFalseTotal
if $count > @all_backoff 0 0 0
82 0 my $backoff= $all_backoff[$count-1];
83  
84 0 ▶ if (time() - $times->[-1] < $backoff) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Cassandra::Client::Policy::LoadBalancing::Default::time() - $times->[-1] < $backoff) 0 0 0
85 0 return;
86 }
87  
88 0 return 1;
89 }
90  
91 0 ▶sub set_connecting {
Subroutine set_connecting: not called CC=2 SCAR=17.9
Pod undocumented
92 0 my ($self, $peer)= @_;
93 0 $self->{connected}{$peer}= 1;
94 0 ▶ push @{$self->{try_times}{$peer} ||= []}, time;
Condition0%0/2
$self->{"try_times"}{$peer} ||= []
l!l
00
MC/DC0%0/1
$self->{"try_times"}{$peer} ||= []
$self->{"try_times"}{$peer}
Truth table0%0/2
A
A= $self->{"try_times"}{$peer}
Aresult
11
00
95 }
96  
97 0 ▶sub set_connected {
Subroutine set_connected: not called CC=2 SCAR=17.9
Pod undocumented
98 0 my ($self, $peer)= @_;
99 0 ▶ warn "BUG" unless $self->{connected}{$peer};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"connected"}{$peer} 0 0 0
100 0 delete $self->{try_times}{$peer};
101 }
102  
103 0 ▶sub set_disconnected {
Subroutine set_disconnected: not called CC=1 SCAR=6.9
Pod undocumented
104 0 my ($self, $peer)= @_;
105 0 delete $self->{connected}{$peer};
106 }
107  
108 0 ▶sub known_node_count {
Subroutine known_node_count: not called CC=1 SCAR=6.9
Pod undocumented
109 0 my ($self)= @_;
110 0 return (0+ keys %{$self->{local_nodes}});
111 }
112  
113 13 1;
114  
115 __END__
« blib/lib/Cassandra/Client/Policy/Retry/Default.pm ↑ Summary blib/lib/Cassandra/Client/NetworkStatus.pm »