line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Cassandra::Client::Policy::LoadBalancing::Default; |
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:TVDW'; |
3
|
|
|
|
|
|
|
$Cassandra::Client::Policy::LoadBalancing::Default::VERSION = '0.13_004'; # TRIAL |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
13
|
$Cassandra::Client::Policy::LoadBalancing::Default::VERSION = '0.13004';use 5.010; |
|
1
|
|
|
|
|
2
|
|
6
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
20
|
|
7
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
27
|
|
8
|
1
|
|
|
1
|
|
4
|
use List::Util 'shuffle'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
425
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new { |
11
|
0
|
|
|
0
|
0
|
|
my ($class, %args)= @_; |
12
|
0
|
|
|
|
|
|
return bless { |
13
|
|
|
|
|
|
|
datacenter => undef, |
14
|
|
|
|
|
|
|
nodes => {}, |
15
|
|
|
|
|
|
|
local_nodes => {}, |
16
|
|
|
|
|
|
|
connected => {}, |
17
|
|
|
|
|
|
|
candidates => [], |
18
|
|
|
|
|
|
|
}, $class; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub get_distance { |
22
|
0
|
|
|
0
|
0
|
|
my ($self, $peer)= @_; |
23
|
0
|
|
|
|
|
|
my $node= $self->{nodes}{$peer}; |
24
|
0
|
0
|
|
|
|
|
if (!$node) { |
25
|
0
|
|
|
|
|
|
warn 'Being asked about a distance for a node we don\'t know'; |
26
|
0
|
|
|
|
|
|
return 'ignored'; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
0
|
0
|
|
|
|
|
if ($self->{local_nodes}{$peer}) { |
30
|
0
|
|
|
|
|
|
return 'local'; |
31
|
|
|
|
|
|
|
} |
32
|
0
|
|
|
|
|
|
return 'remote'; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub on_new_node { |
36
|
0
|
|
|
0
|
0
|
|
my ($self, $node)= @_; |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
|
my $peer= $node->{peer}; |
39
|
0
|
0
|
|
|
|
|
if ($self->{nodes}{$peer}) { |
40
|
0
|
|
|
|
|
|
warn 'BUG: "new" node is already known!'; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
$self->{nodes}{$peer}= $node; |
44
|
0
|
0
|
0
|
|
|
|
if (!$self->{datacenter} || $node->{data_center} eq $self->{datacenter}) { |
45
|
0
|
|
|
|
|
|
$self->{local_nodes}{$peer}= $node; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub on_removed_node { |
50
|
0
|
|
|
0
|
0
|
|
my ($self, $node)= @_; |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
my $peer= $node->{peer}; |
53
|
0
|
0
|
|
|
|
|
if (!$self->{nodes}{$peer}) { |
54
|
0
|
|
|
|
|
|
warn 'BUG: "removed" node wasn\'t there!'; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
|
delete $self->{nodes}{$peer}; |
58
|
0
|
|
|
|
|
|
delete $self->{local_nodes}{$peer}; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub get_next_candidate { |
62
|
0
|
|
|
0
|
0
|
|
my ($self)= @_; |
63
|
0
|
|
|
|
|
|
my $candidates= $self->{candidates}; |
64
|
0
|
|
|
|
|
|
while (my $maybe= shift @$candidates) { |
65
|
0
|
0
|
0
|
|
|
|
if ($self->{local_nodes}{$maybe} && !$self->{connected}{$maybe}) { |
66
|
0
|
|
|
|
|
|
return $maybe; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} |
69
|
0
|
|
|
|
|
|
@$candidates= shuffle grep { !$self->{connected}{$_} } keys %{$self->{local_nodes}}; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
return shift @$candidates; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub set_connected { |
74
|
0
|
|
|
0
|
0
|
|
my ($self, $peer)= @_; |
75
|
0
|
|
|
|
|
|
$self->{connected}{$peer}= 1; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub set_disconnected { |
79
|
0
|
|
|
0
|
0
|
|
my ($self, $peer)= @_; |
80
|
0
|
|
|
|
|
|
delete $self->{connected}{$peer}; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub known_node_count { |
84
|
0
|
|
|
0
|
0
|
|
my ($self)= @_; |
85
|
0
|
|
|
|
|
|
return (0+ keys %{$self->{local_nodes}}); |
|
0
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
1; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
__END__ |