Perlbal/Pool.pm

filter: Statementstmt 39.6% 65 / 164 Branchbran 9.5% 4 / 42 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 53.5% 15 / 28 Podpod 0.0% 0 / 16 totaltotal 32.1% 84 / 261 CC 27 SCAR 26.2
CC
27
Cov
32%
set
42.0
_parse_nodefile
30.0
__ANON__
30.0
CRAP 13.7
« Perlbal/ClientHTTPBase.pm ↑ Summary Perlbal/Plugin/FlvStreaming.pm »
1 ######################################################################
2 # Pool class
3 ######################################################################
4 #
5 # Copyright 2004, Danga Interactive, Inc.
6 # Copyright 2005-2007, Six Apart, Ltd.
7 #
8  
9 package Perlbal::Pool;
10 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 22 ▶use Perlbal::BackendHTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # how often to reload the nodefile
16 22 ▶use constant NODEFILE_RELOAD_FREQ => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # balance methods we support (note: sendstats mode is now removed)
19 22 ▶use constant BM_ROUNDROBIN => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 22 ▶use constant BM_RANDOM => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 22 ▶use fields (
Subroutine BEGIN: called CC=1 SCAR=0.0
23 22 'name', # string; name of this pool
24 'use_count', # int; number of services using us
25 'nodes', # arrayref; [ip, port] values (port defaults to 80)
26 'node_count', # int; number of nodes
27 'node_used', # hashref; { ip:port => use count }
28 'balance_method', # int; BM_ constant from above
29  
30 # used in nodefile mode
31 'nodefile', # string; filename to read nodes from
32 'nodefile.lastmod', # unix time nodefile was last modified
33 'nodefile.lastcheck', # unix time nodefile was last stated
34 'nodefile.checking', # boolean; if true AIO is stating the file for us
35 22 );
36  
37 9 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
38 9 my Perlbal::Pool $self = shift;
39 9 ▶ $self = fields::new($self) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 9 0 9
40  
41 9 my ($name) = @_;
42  
43 9 $self->{name} = $name;
44 9 $self->{use_count} = 0;
45  
46 9 $self->{nodes} = [];
47 9 $self->{node_count} = 0;
48 9 $self->{node_used} = {};
49  
50 9 $self->{nodefile} = undef;
51 9 $self->{balance_method} = BM_RANDOM;
52  
53 9 return $self;
54 }
55  
56 0 ▶sub set {
Subroutine set: not called CC=6 SCAR=37.4
Pod undocumented
57 0 my Perlbal::Pool $self = shift;
58  
59 0 my ($key, $val, $mc) = @_;
60 0 ▶ my $set = sub { $self->{$key} = $val; return $mc->ok; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
61  
62 0 ▶ if ($key eq 'nodefile') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "nodefile") 0 0 0
63 # allow to unset it, which stops us from checking it further,
64 # but doesn't clear our current list of nodes
65 0 ▶ if ($val =~ /^(?:none|undef|null|""|'')$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^(?:none|undef|null|""|'')$/) 0 0 0
66 0 $self->{'nodefile'} = undef;
67 0 $self->{'nodefile.lastmod'} = 0;
68 0 $self->{'nodefile.checking'} = 0;
69 0 $self->{'nodefile.lastcheck'} = 0;
70 0 return $mc->ok;
71 }
72  
73 # enforce that it exists from here on out
74 0 ▶ return $mc->err("File not found")
Branch: 1 branch
BranchTrueFalseTotal
unless -e $val 0 0 0
75 unless -e $val;
76  
77 # force a reload
78 0 $self->{'nodefile'} = $val;
79 0 $self->{'nodefile.lastmod'} = 0;
80 0 $self->{'nodefile.checking'} = 0;
81 0 $self->load_nodefile;
82 0 $self->{'nodefile.lastcheck'} = time;
83 0 return $mc->ok;
84 }
85  
86 0 ▶ if ($key eq "balance_method") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "balance_method") 0 0 0
87 $val = {
88 'random' => BM_RANDOM,
89 0 }->{$val};
90 0 ▶ return $mc->err("Unknown balance method")
Branch: 1 branch
BranchTrueFalseTotal
unless $val 0 0 0
91 unless $val;
92 0 return $set->();
93 }
94  
95 }
96  
97 0 ▶sub dumpconfig {
Subroutine dumpconfig: not called CC=3 SCAR=24.8
Pod undocumented
98 0 my Perlbal::Pool $self = shift;
99 0 my $name = $self->{name};
100  
101 0 my @return;
102  
103 0 ▶ if (my $nodefile = $self->{'nodefile'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $nodefile = $self->{'nodefile'}) { } 0 0 0
104 0 push @return, "SET nodefile = $nodefile";
105 } else {
106 0 foreach my $node (@{$self->{nodes}}) {
107 0 my ($ip, $port) = @$node;
108 0 push @return, "POOL ADD $name $ip:$port";
109 }
110 }
111 0 return @return;
112 }
113  
114 # returns string of balance method
115 0 ▶sub balance_method {
Subroutine balance_method: not called CC=2 SCAR=17.9
Pod undocumented
116 0 my Perlbal::Pool $self = $_[0];
117 0 my $methods = {
118 &BM_ROUNDROBIN => "round_robin",
119 &BM_RANDOM => "random",
120 };
121 0 ▶ return $methods->{$self->{balance_method}} || $self->{balance_method};
Condition0%0/3
$$methods{$self->{'balance_method'}} || $self->{'balance_method'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$$methods{$self->{'balance_method'}} || $self->{'balance_method'}
$$methods{$self->{'balance_method'}} $self->{'balance_method'}
Truth table0%0/3
A || B
A= $$methods{$self->{'balance_method'}}B= $self->{'balance_method'}
ABresult
1X1
011
000
122 }
123  
124 0 ▶sub load_nodefile {
Subroutine load_nodefile: not called CC=3 SCAR=24.8
Pod undocumented
125 0 my Perlbal::Pool $self = shift;
126 0 ▶ return 0 unless $self->{'nodefile'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"nodefile"} 0 0 0
127  
128 0 ▶ if ($Perlbal::OPTMOD_LINUX_AIO) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Perlbal::OPTMOD_LINUX_AIO) { } 0 0 0
129 0 return $self->_load_nodefile_async;
130 } else {
131 0 return $self->_load_nodefile_sync;
132 }
133 }
134  
135 0 ▶sub _parse_nodefile {
Subroutine _parse_nodefile: not called CC=5 SCAR=34.0
136 0 my Perlbal::Pool $self = shift;
137 0 my $dataref = shift;
138  
139 0 my @nodes = split(/\r?\n/, $$dataref);
140  
141 # prepare for adding nodes
142 0 $self->{nodes} = [];
143 0 $self->{node_used} = {};
144  
145 0 foreach (@nodes) {
146 0 s/\#.*//;
147 0 ▶ if (/(\d+\.\d+\.\d+\.\d+)(?::(\d+))?/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/(\d+\.\d+\.\d+\.\d+)(?::(\d+))?/) 0 0 0
148 0 my ($ip, $port) = ($1, $2);
149 0 ▶ $port ||= 80;
Condition0%0/2
$port ||= 80
l!l
00
MC/DC0%0/1
$port ||= 80
$port
Truth table0%0/2
A
A= $port
Aresult
11
00
150 0 ▶ $self->{node_used}->{"$ip:$port"} ||= 0; # set to 0 if not set
Condition0%0/2
$self->{"node_used"}{"${ip}:$port"} ||= 0
l!l
00
MC/DC0%0/1
$self->{"node_used"}{"${ip}:$port"} ||= 0
$self->{"node_used"}{"${ip}:$port"}
Truth table0%0/2
A
A= $self->{"node_used"}{"${ip}:$port"}
Aresult
11
00
151 0 push @{$self->{nodes}}, [ $ip, $port ];
152 }
153 }
154  
155 # setup things using new data
156 0 $self->{node_count} = scalar @{$self->{nodes}};
157 }
158  
159 0 ▶sub _load_nodefile_sync {
Subroutine _load_nodefile_sync: not called CC=3 SCAR=24.8
160 0 my Perlbal::Pool $self = shift;
161  
162 0 my $mod = (stat($self->{nodefile}))[9];
163 0 ▶ return if $mod == $self->{'nodefile.lastmod'};
Branch: 1 branch
BranchTrueFalseTotal
if $mod == $self->{"nodefile.lastmod"} 0 0 0
164 0 $self->{'nodefile.lastmod'} = $mod;
165  
166 0 ▶ open NODEFILE, $self->{nodefile} or return;
Branch: 1 branch
BranchTrueFalseTotal
open NODEFILE, $self->{"nodefile"} or return 0 0 0
167 0 my $nodes;
168 0 { local $/ = undef; $nodes = <NODEFILE>; }
169 0 close NODEFILE;
170 0 $self->_parse_nodefile(\$nodes);
171 }
172  
173 0 ▶sub _load_nodefile_async {
Subroutine _load_nodefile_async: not called CC=2 SCAR=17.9
174 0 my Perlbal::Pool $self = shift;
175  
176 0 ▶ return if $self->{'nodefile.checking'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"nodefile.checking"} 0 0 0
177 0 $self->{'nodefile.checking'} = 1;
178  
179 0 ▶ Perlbal::AIO::aio_stat($self->{nodefile}, sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
180 0 $self->{'nodefile.checking'} = 0;
181  
182 # this might have gotten unset while we were out statting the file, which
183 # means that the user has instructed us not to use a node file, and may
184 # have changed the nodes in the pool, so we should do nothing and return
185 0 ▶ return unless $self->{'nodefile'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"nodefile"} 0 0 0
186  
187 # ignore if the file doesn't exist
188 0 ▶ return unless -e _;
Branch: 1 branch
BranchTrueFalseTotal
unless -e _ 0 0 0
189  
190 0 my $mod = (stat(_))[9];
191 0 ▶ return if $mod == $self->{'nodefile.lastmod'};
Branch: 1 branch
BranchTrueFalseTotal
if $mod == $self->{"nodefile.lastmod"} 0 0 0
192 0 $self->{'nodefile.lastmod'} = $mod;
193  
194 # construct a filehandle (we only have a descriptor here)
195 open NODEFILE, $self->{nodefile}
196 0 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
open NODEFILE, $self->{"nodefile"} or return 0 0 0
197 0 my $nodes;
198 0 { local $/ = undef; $nodes = <NODEFILE>; }
199 0 close NODEFILE;
200  
201 0 $self->_parse_nodefile(\$nodes);
202 0 return;
203 0 });
204  
205 0 return 1;
206 }
207  
208 15 ▶sub add {
Subroutine add: called CC=1 SCAR=0.0
Pod undocumented
209 15 my Perlbal::Pool $self = shift;
210 15 my ($ip, $port) = @_;
211  
212 15 $self->remove($ip, $port); # no dupes
213  
214 15 $self->{node_used}->{"$ip:$port"} = 0;
215 15 push @{$self->{nodes}}, [ $ip, $port ];
216 15 $self->{node_count} = scalar(@{$self->{nodes}});
217 }
218  
219 15 ▶sub remove {
Subroutine remove: called CC=1 SCAR=0.0
Pod undocumented
220 15 my Perlbal::Pool $self = shift;
221 15 my ($ip, $port) = @_;
222  
223 15 delete $self->{node_used}->{"$ip:$port"};
224 15 @{$self->{nodes}} = grep { "$_->[0]:$_->[1]" ne "$ip:$port" } @{$self->{nodes}};
225 15 $self->{node_count} = scalar(@{$self->{nodes}});
226 }
227  
228 21 ▶sub get_backend_endpoint {
Subroutine get_backend_endpoint: called CC=4 SCAR=17.9
Pod undocumented
229 21 my Perlbal::Pool $self = $_[0];
230  
231 21 my @endpoint; # (IP,port)
232  
233 # re-load nodefile if necessary
234 21 ▶ if ($self->{nodefile}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"nodefile"}) 0 21 21
235 0 my $now = time;
236 0 ▶ if ($now > $self->{'nodefile.lastcheck'} + NODEFILE_RELOAD_FREQ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($now > $self->{"nodefile.lastcheck"} + 3) 0 0 0
237 0 $self->{'nodefile.lastcheck'} = $now;
238 0 $self->load_nodefile;
239 }
240 }
241  
242 # no nodes?
243 21 ▶ return () unless $self->{node_count};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"node_count"} 0 21 21
244  
245 # pick one randomly
246 21 return @{$self->{nodes}[int(rand($self->{node_count}))]};
247 }
248  
249 129 ▶sub backend_should_live {
Subroutine backend_should_live: called CC=2 SCAR=7.6
Pod undocumented
250 129 my Perlbal::Pool $self = $_[0];
251 129 my Perlbal::BackendHTTP $be = $_[1];
252  
253 # a backend stays alive if we still have users. eventually this whole
254 # function might do more and actually take into account the individual
255 # backend, but for now, this suits us.
256 129 ▶ return 1 if $self->{use_count};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"use_count"} 129 0 129
257 0 return 0;
258 }
259  
260 181 ▶sub node_count {
Subroutine node_count: called CC=1 SCAR=0.0
Pod undocumented
261 181 my Perlbal::Pool $self = $_[0];
262 181 return $self->{node_count};
263 }
264  
265 0 ▶sub nodes {
Subroutine nodes: not called CC=1 SCAR=6.9
Pod undocumented
266 0 my Perlbal::Pool $self = $_[0];
267 0 return $self->{nodes};
268 }
269  
270 0 ▶sub node_used {
Subroutine node_used: not called CC=1 SCAR=6.9
Pod undocumented
271 0 my Perlbal::Pool $self = $_[0];
272 0 return $self->{node_used}->{$_[1]};
273 }
274  
275 144 ▶sub mark_node_used {
Subroutine mark_node_used: called CC=1 SCAR=0.0
Pod undocumented
276 144 my Perlbal::Pool $self = $_[0];
277 144 $self->{node_used}->{$_[1]}++;
278 }
279  
280 9 ▶sub increment_use_count {
Subroutine increment_use_count: called CC=1 SCAR=0.0
Pod undocumented
281 9 my Perlbal::Pool $self = $_[0];
282 9 $self->{use_count}++;
283 }
284  
285 0 ▶sub decrement_use_count {
Subroutine decrement_use_count: not called CC=1 SCAR=6.9
Pod undocumented
286 0 my Perlbal::Pool $self = $_[0];
287 0 $self->{use_count}--;
288 }
289  
290 0 ▶sub name {
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
291 0 my Perlbal::Pool $self = $_[0];
292 0 return $self->{name};
293 }
294  
295 22 1;
296  
297 # Local Variables:
298 # mode: perl
299 # c-basic-indent: 4
300 # indent-tabs-mode: nil
301 # End:
« Perlbal/ClientHTTPBase.pm ↑ Summary Perlbal/Plugin/FlvStreaming.pm »