IProto/Cluster.pm

filter: Statementstmt 33.9% 35 / 103 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 35.0% 7 / 20 Podpod 100% 2 / 2 totaltotal 27.5% 44 / 160 CC 24 SCAR 29.5
CC
24
Cov
26%
timeout
56.0
_balance_ketama
30.0
server
20.0
CRAP 19.1
« Pending.pm ↑ Summary IProto/Connection/Async.pm »
1 package MR::IProto::Cluster;
2  
3 =head1 NAME
4  
5 MR::IProto::Cluster - cluster of servers
6  
7 =head1 DESCRIPTION
8  
9 This class is used to implement balancing between several servers.
10  
11 =cut
12  
13 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Mouse::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use MR::IProto::Cluster::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use String::CRC32 qw(crc32);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head1 EXPORTED CONSTANTS
19  
20 =over
21  
22 =item RR
23  
24 Round robin algorithm
25  
26 =item HASH
27  
28 Hashing algorithm using CRC32
29  
30 =item KETAMA
31  
32 Ketama algorithm
33  
34 =back
35  
36 =cut
37  
38 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 our @EXPORT_OK = qw( RR HASH KETAMA );
40  
41 coerce 'MR::IProto::Cluster'
42 => from 'Str'
43 0 ▶ => via { __PACKAGE__->new( servers => $_ ) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
44  
45 subtype 'MR::IProto::Cluster::Servers'
46 => as 'ArrayRef[MR::IProto::Cluster::Server]'
47 0 ▶ => where { scalar @_ };
Subroutine __ANON__: not called CC=1 SCAR=1.2
48 coerce 'MR::IProto::Cluster::Servers'
49 => from 'Str'
50 0 ▶ => via {
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 0 my $type = find_type_constraint('MR::IProto::Cluster::Server');
52 0 [ map $type->coerce($_), split /,/, $_ ];
53 1 };
54  
55 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 RR => 1,
57 HASH => 2,
58 KETAMA => 3,
59 1 };
60  
61 1 enum 'MR::IProto::Balance' => (
62 RR,
63 HASH,
64 KETAMA,
65 );
66 coerce 'MR::IProto::Balance'
67 => from 'Str',
68 0 ▶ => via {
Subroutine __ANON__: not called CC=3 SCAR=24.8
69 0 ▶ $_ eq 'hash-crc32' ? HASH
Branch: 2 branches
BranchTrueFalseTotal
$_ eq 'hash-crc32' ? : 0 0 0
$_ eq 'ketama' ? : 0 0 0
70 : $_ eq 'ketama' ? KETAMA
71 : RR;
72 1 };
73  
74 =head1 ATTRIBUTES
75  
76 =over
77  
78 =item balance
79  
80 Balancing algorithms.
81 Possible values are constants: RR, HASH, KETAMA.
82 Or their string analogs: 'round-robin', 'hash-crc32', 'ketama'.
83  
84 =cut
85  
86 1 has balance => (
87 is => 'ro',
88 isa => 'MR::IProto::Balance',
89 default => RR,
90 coerce => 1,
91 );
92  
93 =item servers
94  
95 ArrayRef of L<MR::IProto::Cluster::Server>.
96  
97 =cut
98  
99 1 has servers => (
100 is => 'ro',
101 isa => 'MR::IProto::Cluster::Servers',
102 required => 1,
103 coerce => 1,
104 );
105  
106 =back
107  
108 =cut
109  
110 1 has _one => (
111 is => 'ro',
112 isa => 'Maybe[MR::IProto::Cluster::Server]',
113 lazy_build => 1,
114 );
115  
116 1 has _ketama => (
117 is => 'ro',
118 isa => 'ArrayRef[ArrayRef]',
119 lazy_build => 1,
120 );
121  
122 1 has _rr_servers => (
123 is => 'rw',
124 isa => 'MR::IProto::Cluster::Servers',
125 lazy_build => 1,
126 );
127  
128 1 has _hash_servers => (
129 is => 'rw',
130 isa => 'MR::IProto::Cluster::Servers',
131 lazy_build => 1,
132 );
133  
134 =head1 PUBLIC METHODS
135  
136 =over
137  
138 =item server( $key? )
139  
140 Get server from balancing using C<$key>.
141  
142 =cut
143  
144 0 ▶sub server {
Subroutine server: not called CC=4 SCAR=30.0
Pod documented
145 0 my ($self, $key) = @_;
146 0 my $one = $self->_one;
147 0 ▶ return $one if defined $one;
Branch: 1 branch
BranchTrueFalseTotal
if defined $one 0 0 0
148 0 ▶ my $method = $self->balance == RR ? '_balance_rr'
Branch: 2 branches
BranchTrueFalseTotal
$self->balance == 1 ? : 0 0 0
$self->balance == 3 ? : 0 0 0
149 : $self->balance == KETAMA ? '_balance_ketama'
150 : '_balance_hash';
151 0 return $self->$method($key);
152 }
153  
154 =item timeout( $new? )
155  
156 Used to set C<$new> timeout value to all servers.
157 If argument is skipped and timeout is equal for all servers then returns
158 it value, if timeout is different then returns undef.
159  
160 =cut
161  
162 0 ▶sub timeout {
Subroutine timeout: not called CC=7 SCAR=40.3
Pod documented
163 0 my $self = shift;
164 0 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
165 0 my $timeout = shift;
166 0 $_->timeout($timeout) foreach @{$self->servers};
167 0 return $timeout;
168 }
169 else {
170 0 my $timeout;
171 0 foreach my $t ( map $_->timeout, @{$self->servers} ) {
172 0 ▶ return if defined $timeout && $timeout != $t;
Branch: 1 branch
BranchTrueFalseTotal
if defined $timeout and $timeout != $t 0 0 0
Condition0%0/3
defined $timeout and $timeout != $t
!ll&&!rl&&r
000
MC/DC0%0/2
defined $timeout and $timeout != $t
defined $timeout $timeout != $t
Truth table0%0/3
A and B
A= defined $timeoutB= $timeout != $t
ABresult
0X0
100
111
173 0 ▶ $timeout = $t unless defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout 0 0 0
174 }
175 0 return $timeout;
176 }
177 }
178  
179 =back
180  
181 =cut
182  
183 0 ▶sub _build__one {
Subroutine _build__one: not called CC=2 SCAR=17.9
184 0 my ($self) = @_;
185 0 ▶ return @{$self->servers} == 1 ? $self->servers->[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@{$self->servers;} == 1 ? : 0 0 0
186 }
187  
188 0 ▶sub _build__ketama {
Subroutine _build__ketama: not called CC=3 SCAR=24.8
189 0 my $self = shift;
190 0 my @ketama;
191 0 foreach my $server (@{$self->servers}) {
192 0 for my $i (0..10) {
193 0 push @ketama, [crc32($server->host.$server->port.$i), $server];
194 }
195 }
196 0 return [ sort { $a->[0] cmp $b->[0] } @ketama ];
197 }
198  
199 0 ▶sub _build__rr_servers {
Subroutine _build__rr_servers: not called CC=1 SCAR=6.9
200 0 my ($self) = @_;
201 0 return [ @{ $self->servers } ];
202 }
203  
204 0 ▶sub _build__hash_servers {
Subroutine _build__hash_servers: not called CC=2 SCAR=17.9
205 0 my ($self) = @_;
206 0 return [ map { my @s; my $s = $_; push @s, $s for ( 1 .. $s->weight ); @s } @{ $self->servers } ];
207 }
208  
209 0 ▶sub _balance_rr {
Subroutine _balance_rr: not called CC=2 SCAR=17.9
210 0 my ($self) = @_;
211 0 ▶ $self->_clear_rr_servers() if @{$self->_rr_servers} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->_rr_servers;} == 0 0 0 0
212 0 return splice(@{$self->_rr_servers}, int(rand(@{$self->_rr_servers})), 1);
213 }
214  
215 0 ▶sub _balance_hash {
Subroutine _balance_hash: not called CC=4 SCAR=30.0
216 0 my ($self, $key) = @_;
217 0 my ($hash_, $hash, $server);
218  
219 0 ▶ die "Cannot balance hash without key" unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 0 0
220 0 $hash = $hash_ = crc32($key) >> 16;
221 0 for (0..19) {
222 0 $server = $self->_hash_servers->[$hash % @{$self->_hash_servers}];
223 0 ▶ return $server if $server->active;
Branch: 1 branch
BranchTrueFalseTotal
if $server->active 0 0 0
224 0 $hash += crc32($_, $hash_) >> 16;
225 }
226  
227 0 return $self->_hash_servers->[rand @{$self->_hash_servers}]; #last resort
228 }
229  
230 0 ▶sub _balance_ketama {
Subroutine _balance_ketama: not called CC=5 SCAR=34.0
231 0 my ($self, $key) = @_;
232  
233 0 ▶ die "Cannot balance ketama without key" unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 0 0
234 0 my $idx = crc32($key);
235  
236 0 foreach (@{$self->_ketama}) {
237 0 ▶ next unless ($_);
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 0 0 0
238 0 ▶ return $_->[1] if ($_->[0] >= $idx);
Branch: 1 branch
BranchTrueFalseTotal
if $_->[0] >= $idx 0 0 0
239 }
240  
241 0 return $self->_ketama->[0]->[1];
242 }
243  
244 =head1 SEE ALSO
245  
246 L<MR::IProto>, L<MR::IProto::Cluster::Server>.
247  
248 =cut
249  
250 1 ▶no Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
251 1 __PACKAGE__->meta->make_immutable();
252  
253 1 1;
« Pending.pm ↑ Summary IProto/Connection/Async.pm »