Perlbal.pm

filter: Statementstmt 42.8% 362 / 845 Branchbran 19.8% 69 / 348 Conditioncond 20.0% 17 / 85 MC/DCmcdc 3.8% 2 / 52 Subroutinesub 62.1% 69 / 111 Podpod 0.0% 0 / 58 totaltotal 34.6% 519 / 1499 CC 251 SCAR 44.5
CC
251
Cov
35%
MANAGE_server
597.7
MANAGE_states
182.0
MANAGE_xs
132.0
CRAP 85.2
« Perlbal/Plugin/Throttle.pm ↑ Summary Perlbal/Plugin/NotModified.pm »
1 #!/usr/bin/perl
2 #
3 # Copyright 2004, Danga Interactive, Inc.
4 # Copyright 2005-2007, Six Apart, Ltd.
5 #
6  
7 =head1 NAME
8  
9 Perlbal - Reverse-proxy load balancer and webserver
10  
11 =head1 SEE ALSO
12  
13 L<http://www.danga.com/perlbal/>
14  
15 =head1 CONTRIBUTING
16  
17 Got a patch? Or a bug report? Instructions on how to contribute
18 are located here:
19  
20 L<http://contributing.appspot.com/perlbal>
21  
22 Thanks!
23  
24 =head1 COPYRIGHT AND LICENSE
25  
26 Copyright 2004, Danga Interactive, Inc.
27 Copyright 2005-2010, Six Apart, Ltd.
28  
29 You can use and redistribute Perlbal under the same terms as Perl itself.
30  
31 =cut
32  
33 package Perlbal;
34  
35 23 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
36 # keep track of anonymous subs' origins:
37 23 $^P |= 0x200;
38 }
39  
40 0 ▶my $has_gladiator = eval "use Devel::Gladiator; 1;";
Subroutine BEGIN: called CC=1 SCAR=1.2
41 22 ▶my $has_cycle = eval "use Devel::Cycle; 1;";
Subroutine BEGIN: called CC=1 SCAR=0.0
42 22 ▶my $has_devel_peek = eval "use Devel::Peek; 1;";
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 23 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 22 $VERSION = '1.80';
46  
47 22 ▶use constant DEBUG => $ENV{PERLBAL_DEBUG} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'PERLBAL_DEBUG'} || 0
l!l
022
MC/DC0%0/1
$ENV{'PERLBAL_DEBUG'} || 0
$ENV{'PERLBAL_DEBUG'}
Truth table50%1/2
A
A= $ENV{'PERLBAL_DEBUG'}
Aresult
11
00
48 22 ▶use constant DEBUG_OBJ => $ENV{PERLBAL_DEBUG_OBJ} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'PERLBAL_DEBUG_OBJ'} || 0
l!l
022
MC/DC0%0/1
$ENV{'PERLBAL_DEBUG_OBJ'} || 0
$ENV{'PERLBAL_DEBUG_OBJ'}
Truth table50%1/2
A
A= $ENV{'PERLBAL_DEBUG_OBJ'}
Aresult
11
00
49 22 ▶use constant TRACK_STATES => $ENV{PERLBAL_TRACK_STATES} || 0; # if on, track states for "state changes" command
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'PERLBAL_TRACK_STATES'} || 0
l!l
022
MC/DC0%0/1
$ENV{'PERLBAL_TRACK_STATES'} || 0
$ENV{'PERLBAL_TRACK_STATES'}
Truth table50%1/2
A
A= $ENV{'PERLBAL_TRACK_STATES'}
Aresult
11
00
50  
51 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 22 ▶no warnings qw(deprecated);
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 22 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 22 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 22 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 22 $Perlbal::SYSLOG_AVAILABLE = eval { require Sys::Syslog; 1; };
60 22 $Perlbal::BSD_RESOURCE_AVAILABLE = eval { require BSD::Resource; 1; };
61  
62 # incremented every second by a timer:
63 22 $Perlbal::tick_time = time();
64  
65 # Set to 1 when we open syslog, and 0 when we close it
66 22 $Perlbal::syslog_open = 0;
67  
68 22 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 22 ▶use Carp qw(cluck croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
70 22 ▶use Errno qw(EBADF);
Subroutine BEGIN: called CC=1 SCAR=0.0
71 22 ▶use POSIX qw(SIG_BLOCK SIG_UNBLOCK SIGINT sigprocmask);
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 22 our(%TrackVar);
74 358 ▶sub track_var {
Subroutine track_var: called CC=1 SCAR=0.0
Pod undocumented
75 358 my ($name, $ref) = @_;
76 358 $TrackVar{$name} = $ref;
77 }
78  
79 22 ▶use Perlbal::AIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 22 ▶use Perlbal::HTTPHeaders;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 22 ▶use Perlbal::Service;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 22 ▶use Perlbal::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 22 ▶use Perlbal::TCPListener;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 22 ▶use Perlbal::UploadListener;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 22 ▶use Perlbal::ClientManage;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 22 ▶use Perlbal::ClientHTTPBase;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 22 ▶use Perlbal::ClientProxy;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 22 ▶use Perlbal::ClientHTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 22 ▶use Perlbal::BackendHTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 22 ▶use Perlbal::ReproxyManager;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 22 ▶use Perlbal::Pool;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 22 ▶use Perlbal::ManageCommand;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 22 ▶use Perlbal::CommandContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 22 ▶use Perlbal::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
95  
96 22 $SIG{'PIPE'} = "IGNORE"; # handled manually
97  
98 22 our(%hooks); # hookname => subref
99 22 our(%service); # servicename -> Perlbal::Service
100 22 our(%pool); # poolname => Perlbal::Pool
101 22 our(%plugins); # plugin => 1 (shows loaded plugins)
102 22 our($last_error);
103 22 our $service_autonumber = 1; # used to generate names for anonymous services created with Perlbal->create_service()
104 22 our $vivify_pools = 1; # if on, allow automatic creation of pools
105 22 our $foreground = 1; # default to foreground
106 22 our $track_obj = 0; # default to not track creation locations
107 22 our $reqs = 0; # total number of requests we've done
108 22 our $starttime = time(); # time we started
109 22 our $pidfile = ''; # full path, default to not writing pidfile
110 # used by pidfile (only makes sense before run started)
111 # don't rely on this variable, it might change.
112 22 our $run_started = 0;
113 22 our ($lastutime, $laststime, $lastreqs) = (0, 0, 0); # for deltas
114  
115 22 our %PluginCase = (); # lowercase plugin name -> as file is named
116  
117 # setup XS status data structures
118 22 our %XSModules; # ( 'headers' => 'Perlbal::XS::HTTPHeaders' )
119  
120 # now include XS files
121 0 ▶eval "use Perlbal::XS::HTTPHeaders;"; # if we have it, load it
Subroutine BEGIN: called CC=1 SCAR=1.2
122  
123 # activate modules as necessary
124 22 ▶if ($ENV{PERLBAL_XS_HEADERS} && $XSModules{headers}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"PERLBAL_XS_HEADERS"} and $Perlbal::XSModules{"headers"}) 0 22 22
Condition33%1/3
$ENV{"PERLBAL_XS_HEADERS"} and $Perlbal::XSModules{"headers"}
!ll&&!rl&&r
2200
MC/DC0%0/2
$ENV{"PERLBAL_XS_HEADERS"} and $Perlbal::XSModules{"headers"}
$ENV{"PERLBAL_XS_HEADERS"} $Perlbal::XSModules{"headers"}
Truth table33%1/3
A and B
A= $ENV{"PERLBAL_XS_HEADERS"}B= $Perlbal::XSModules{"headers"}
ABresult
0X0
100
111
125 0 Perlbal::XS::HTTPHeaders::enable();
126 }
127  
128 # unactivate field::new
129 22 ▶if ($ENV{PERLBAL_REMOVE_FIELDS}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"PERLBAL_REMOVE_FIELDS"}) 0 22 22
130 22 ▶ use Perlbal::Fields;
Subroutine BEGIN: called CC=1 SCAR=0.0
131 0 Perlbal::Fields->remove();
132 }
133  
134  
135 # setup a USR1 signal handler that tells us to dump some basic statistics
136 # of how we're doing to the syslog
137 0 ▶$SIG{'USR1'} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
138 0 ▶ my $dumper = sub { Perlbal::log('info', $_[0]); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
139 0 foreach my $svc (values %service) {
140 0 run_manage_command("show service $svc->{name}", $dumper);
141 }
142 0 run_manage_command('states', $dumper);
143 0 run_manage_command('queues', $dumper);
144 22 };
145  
146 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
Pod undocumented
147 0 $last_error = shift;
148 0 return 0;
149 }
150  
151 # Object instance counts, for debugging and leak detection
152 22 our(%ObjCount); # classname -> instances
153 22 our(%ObjTotal); # classname -> instances
154 22 our(%ObjTrack); # "$objref" -> creation location
155 999 ▶sub objctor {
Subroutine objctor: called CC=1 SCAR=0.0
Pod undocumented
156 999 if (DEBUG_OBJ) {
157 my $ref = ref $_[0];
158 $ref .= "-$_[1]" if $_[1];
159 $ObjCount{$ref}++;
160 $ObjTotal{$ref}++;
161  
162 # now, if we're tracing leaks, note this object's creation location
163 if ($track_obj) {
164 my $i = 1;
165 my @list;
166 while (my $sub = (caller($i++))[3]) {
167 push @list, $sub;
168 }
169 $ObjTrack{"$_[0]"} = [ time, join(', ', @list) ];
170 }
171 }
172 }
173 1050 ▶sub objdtor {
Subroutine objdtor: called CC=1 SCAR=0.0
Pod undocumented
174 1050 if (DEBUG_OBJ) {
175 my $ref = ref $_[0];
176 $ref .= "-$_[1]" if $_[1];
177 $ObjCount{$ref}--;
178  
179 # remove tracking for this object
180 if ($track_obj) {
181 delete $ObjTrack{"$_[0]"};
182 }
183 }
184 }
185  
186 5 ▶sub register_global_hook {
Subroutine register_global_hook: called CC=1 SCAR=0.0
Pod undocumented
187 5 $hooks{$_[0]} = $_[1];
188 5 return 1;
189 }
190  
191 0 ▶sub unregister_global_hook {
Subroutine unregister_global_hook: not called CC=1 SCAR=6.9
Pod undocumented
192 0 delete $hooks{$_[0]};
193 0 return 1;
194 }
195  
196 34 ▶sub run_global_hook {
Subroutine run_global_hook: called CC=2 SCAR=6.9
Pod undocumented
197 34 my $hookname = shift;
198 34 my $ref = $hooks{$hookname};
199 34 ▶ return $ref->(@_) if defined $ref; # @_ is $mc (a Perlbal::ManageCommand)
Branch: 1 branch
BranchTrueFalseTotal
if defined $ref 16 18 34
200 18 return undef;
201 }
202  
203 0 ▶sub service_names {
Subroutine service_names: not called CC=1 SCAR=6.9
Pod undocumented
204 0 return sort keys %service;
205 }
206  
207 # class method: given a service name, returns a service object
208 98 ▶sub service {
Subroutine service: called CC=1 SCAR=0.0
Pod undocumented
209 98 my $class = shift;
210 98 return $service{$_[0]};
211 }
212  
213 44 ▶sub create_service {
Subroutine create_service: called CC=4 SCAR=15.0
Pod undocumented
214 44 my $class = shift;
215 44 my $name = shift;
216  
217 44 ▶ unless (defined($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 44 44
218 0 $name = "____auto_".($service_autonumber++);
219 }
220  
221 44 ▶ croak("service '$name' already exists") if $service{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $Perlbal::service{$name} 0 44 44
222 44 ▶ croak("pool '$name' already exists") if $pool{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $Perlbal::pool{$name} 0 44 44
223  
224 # Create the new service and return it
225 44 return $service{$name} = Perlbal::Service->new($name);
226 }
227  
228 9 ▶sub pool {
Subroutine pool: called CC=1 SCAR=0.0
Pod undocumented
229 9 my $class = shift;
230 9 return $pool{$_[0]};
231 }
232  
233 # given some plugin name, return its correct case
234 6 ▶sub plugin_case {
Subroutine plugin_case: called CC=2 SCAR=8.1
Pod undocumented
235 6 my $pname = lc shift;
236 6 ▶ return $PluginCase{$pname} || $pname;
Condition33%1/3
$Perlbal::PluginCase{$pname} || $pname
l!l&&r!l&&!r
600
MC/DC0%0/2
$Perlbal::PluginCase{$pname} || $pname
$Perlbal::PluginCase{$pname} $pname
Truth table33%1/3
A || B
A= $Perlbal::PluginCase{$pname}B= $pname
ABresult
1X1
011
000
237 }
238  
239 # run a block of commands. returns true if they all passed
240 18 ▶sub run_manage_commands {
Subroutine run_manage_commands: called CC=4 SCAR=14.9
Pod undocumented
241 18 my ($cmd_block, $out, $ctx) = @_;
242  
243 18 ▶ $ctx ||= Perlbal::CommandContext->new;
Condition33%1/3
$ctx ||= "Perlbal::CommandContext"->new
l!l&&r!l&&!r
0180
MC/DC0%0/2
$ctx ||= "Perlbal::CommandContext"->new
$ctx "Perlbal::CommandContext"->new
Truth table33%1/3
A ||= B
A= $ctxB= "Perlbal::CommandContext"->new
ABresult
1X1
011
000
244 18 foreach my $cmd (split(/\n/, $cmd_block)) {
245 350 ▶ return 0 unless Perlbal::run_manage_command($cmd, $out, $ctx);
Branch: 1 branch
BranchTrueFalseTotal
unless run_manage_command($cmd, $out, $ctx) 0 350 350
246 }
247 18 return 1;
248 }
249  
250 # allows ${ip:eth0} in config. currently the only supported expansion
251 0 ▶sub _expand_config_var {
Subroutine _expand_config_var: not called CC=5 SCAR=34.0
252 0 my $cmd = shift;
253 0 ▶ $cmd =~ /^(\w+):(.+)/
Branch: 1 branch
BranchTrueFalseTotal
$cmd =~ /^(\w+):(.+)/ or die "Unknown config variable: $cmd\n" 0 0 0
254 or die "Unknown config variable: $cmd\n";
255 0 my ($type, $val) = ($1, $2);
256 0 ▶ if ($type eq "ip") {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "ip") 0 0 0
257 0 ▶ die "Bogus-looking iface name" unless $val =~ /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /^\w+$/ 0 0 0
258 0 my $conf = `/sbin/ifconfig $val`;
259 0 ▶ $conf =~ /inet addr:(\S+)/
Branch: 1 branch
BranchTrueFalseTotal
$conf =~ /inet addr:(\S+)/ or die "Can't find IP of interface '${val}'" 0 0 0
260 or die "Can't find IP of interface '$val'";
261 0 return $1;
262 }
263 0 die "Unknown config variable type: $type\n";
264 }
265  
266 # returns 1 if command succeeded, 0 otherwise
267 498 ▶sub run_manage_command {
Subroutine run_manage_command: called CC=13 SCAR=26.6
Pod undocumented
268 498 my ($cmd, $out, $ctx) = @_; # $out is output stream closure
269  
270 498 $cmd =~ s/\#.*//;
271 498 $cmd =~ s/^\s+//;
272 498 $cmd =~ s/\s+$//;
273 498 $cmd =~ s/\s+/ /g;
274  
275 # expand variables
276 498 my $orig = $cmd; # save original case for some commands
277  
278 0 $cmd =~ s/\$\{(.+?)\}/_expand_config_var($1)/eg;
279 498 $cmd =~ s/\$(\w+)/$ENV{$1}/g;
280  
281 498 $cmd =~ s/^([^=]+)/lc $1/e; # lowercase everything up to an =
282 498 ▶ return 1 unless $cmd =~ /^\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd =~ /^\S/ 71 427 498
283  
284 427 ▶ $out ||= sub {};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$out ||= sub { }
l!l&&r!l&&!r
42700
MC/DC0%0/2
$out ||= sub { }
$out sub { }
Truth table33%1/3
A ||= B
A= $outB= sub { }
ABresult
1X1
011
000
285 427 ▶ $ctx ||= Perlbal::CommandContext->new;
Condition33%1/3
$ctx ||= "Perlbal::CommandContext"->new
l!l&&r!l&&!r
42700
MC/DC0%0/2
$ctx ||= "Perlbal::CommandContext"->new
$ctx "Perlbal::CommandContext"->new
Truth table33%1/3
A ||= B
A= $ctxB= "Perlbal::CommandContext"->new
ABresult
1X1
011
000
286  
287 11 ▶ my $err = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
288 11 $out->("ERROR: $_[0]");
289 11 return 0;
290 427 };
291 372 ▶ my $ok = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
292 372 ▶ $out->("OK") if $ctx->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->verbose 70 302 372
293 372 return 1;
294 427 };
295  
296 427 ▶ return $err->("invalid command") unless $cmd =~ /^(\w+)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd =~ /^(\w+)/ 0 427 427
297 427 my $basecmd = $1;
298  
299 427 my $mc = Perlbal::ManageCommand->new($basecmd, $cmd, $out, $ok, $err, $orig, $ctx);
300  
301 # for testing auto crashing and recovery:
302 0 ▶ if ($basecmd eq "crash") { die "Intentional crash." };
Branch: 1 branch
BranchTrueFalseTotal
if ($basecmd eq "crash") 0 427 427
303  
304 22 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
305 427 my $handler;
306 427 ▶ if ($Perlbal::{"MANAGE_$basecmd"} && ($handler = *{"MANAGE_$basecmd"}{CODE})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Perlbal::{"MANAGE_$basecmd"} and $handler = *{"MANAGE_$basecmd";}{"CODE"}) 411 16 427
Condition100%2/2
$main::Perlbal::{"MANAGE_$basecmd"} and $handler = *{"MANAGE_$basecmd";}{"CODE"}
!ll
16411
MC/DC100%1/1
$main::Perlbal::{"MANAGE_$basecmd"} and $handler = *{"MANAGE_$basecmd";}{"CODE"}
$main::Perlbal::{"MANAGE_$basecmd"}
Truth table100%2/2
A
A= $main::Perlbal::{"MANAGE_$basecmd"}
Aresult
00
11
307 411 my $rv = eval { $handler->($mc); };
308 393 ▶ return $mc->err($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 393 393
309 393 return $rv;
310 }
311  
312 # if no handler found, look for plugins
313  
314 # call any hooks if they've been defined
315 16 my $rval = eval { run_global_hook("manage_command.$basecmd", $mc); };
316 16 ▶ return $mc->err($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 3 13 16
317 13 ▶ if (defined $rval) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rval) 13 0 13
318 # commands may return boolean, or arrayref to mass-print
319 13 ▶ if (ref $rval eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rval eq "ARRAY") 0 13 13
320 0 $mc->out($_) foreach @$rval;
321 0 return 1;
322 }
323 13 return $rval;
324 }
325  
326 0 return $mc->err("unknown command: $basecmd");
327 }
328  
329 0 ▶sub arena_ref_counts {
Subroutine arena_ref_counts: not called CC=4 SCAR=30.0
Pod undocumented
330 0 my $all = Devel::Gladiator::walk_arena();
331 0 my %ct;
332  
333 my %run_cycle;
334 0 foreach my $it (@$all) {
335 0 $ct{ref $it}++;
336 0 ▶ if (ref $it eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $it eq "CODE") 0 0 0
337 0 my $name = Devel::Peek::CvGV($it);
338 0 ▶ $ct{$name}++ if $name =~ /ANON/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /ANON/ 0 0 0
339 }
340 }
341 0 $all = undef;
342 0 return \%ct;
343 }
344  
345 22 my %last_gladiator;
346 0 ▶sub MANAGE_gladiator {
Subroutine MANAGE_gladiator: not called CC=6 SCAR=37.4
Pod undocumented
347 0 my $mc = shift->no_opts;
348 0 ▶ unless ($has_gladiator && $has_devel_peek) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_gladiator and $has_devel_peek) 0 0 0
Condition0%0/3
$has_gladiator and $has_devel_peek
!ll&&!rl&&r
000
MC/DC0%0/2
$has_gladiator and $has_devel_peek
$has_gladiator $has_devel_peek
Truth table0%0/3
A and B
A= $has_gladiatorB= $has_devel_peek
ABresult
0X0
100
111
349 0 $mc->end;
350 0 return;
351 }
352  
353 0 my $ct = arena_ref_counts();
354 0 my $ret;
355 0 $ret .= "ARENA COUNTS:\n";
356 0 foreach my $k (sort {$ct->{$b} <=> $ct->{$a}} keys %$ct) {
357 0 ▶ my $delta = $ct->{$k} - ($last_gladiator{$k} || 0);
Condition0%0/2
$last_gladiator{$k} || 0
l!l
00
MC/DC0%0/1
$last_gladiator{$k} || 0
$last_gladiator{$k}
Truth table0%0/2
A
A= $last_gladiator{$k}
Aresult
11
00
358 0 $last_gladiator{$k} = $ct->{$k};
359 0 ▶ next unless $ct->{$k} > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $ct->{$k} > 1 0 0 0
360 0 $ret .= sprintf(" %4d %-4d $k\n", $ct->{$k}, $delta);
361 }
362  
363 0 $mc->out($ret);
364 0 $mc->end;
365 }
366  
367 0 ▶sub MANAGE_varsize {
Subroutine MANAGE_varsize: not called CC=3 SCAR=24.8
Pod undocumented
368 0 my $mc = shift->no_opts;
369  
370 0 my $emit;
371 0 ▶ $emit = sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
372 0 my ($v, $depth, $name) = @_;
373 0 ▶ $name ||= "";
Condition0%0/2
$name ||= ""
l!l
00
MC/DC0%0/1
$name ||= ""
$name
Truth table0%0/2
A
A= $name
Aresult
11
00
374  
375 0 my $show;
376 0 ▶ if (ref $v eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $v eq 'ARRAY') { } 0 0 0
elsif (ref $v eq 'HASH') { } 0 0 0
377 0 ▶ return unless @$v;
Branch: 1 branch
BranchTrueFalseTotal
unless @$v 0 0 0
378 0 $show = "[] " . scalar @$v;
379 }
380 elsif (ref $v eq "HASH") {
381 0 ▶ return unless %$v;
Branch: 1 branch
BranchTrueFalseTotal
unless %$v 0 0 0
382 0 $show = "{} " . scalar keys %$v;
383 }
384 else {
385 0 $show = " = $v";
386 }
387 0 my $pre = " " x $depth;
388 0 $mc->out("$pre$name $show");
389  
390 0 ▶ if (ref $v eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq "HASH") 0 0 0
391 0 foreach my $k (sort keys %$v) {
392 0 $emit->($v->{$k}, $depth+1, "{$k}");
393 }
394 }
395 0 };
396  
397 0 foreach my $k (sort keys %TrackVar) {
398 0 ▶ my $v = $TrackVar{$k} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $v = $Perlbal::TrackVar{$k} or next 0 0 0
399 0 $emit->($v, 0, $k);
400 }
401  
402 0 $mc->end;
403 }
404  
405 0 ▶sub MANAGE_obj {
Subroutine MANAGE_obj: not called CC=2 SCAR=17.9
Pod undocumented
406 0 my $mc = shift->no_opts;
407  
408 0 foreach (sort keys %ObjCount) {
409 0 $mc->out("$_ = $ObjCount{$_} (tot=$ObjTotal{$_})");
410 }
411 0 $mc->end;
412 }
413  
414 0 ▶sub MANAGE_verbose {
Subroutine MANAGE_verbose: not called CC=2 SCAR=17.9
Pod undocumented
415 0 my $mc = shift->parse(qr/^verbose (on|off)$/,
416 "usage: VERBOSE {on|off}");
417 0 my $onoff = $mc->arg(1);
418 0 ▶ $mc->{ctx}->verbose(lc $onoff eq 'on' ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
lc $onoff eq 'on' ? : 0 0 0
419 0 return $mc->ok;
420 }
421  
422 18 ▶sub MANAGE_shutdown {
Subroutine MANAGE_shutdown: called CC=6 SCAR=35.2
Pod undocumented
423 18 my $mc = shift->parse(qr/^shutdown(\s?graceful)?\s?(\d+)?$/);
424  
425 # immediate shutdown
426 18 ▶ exit(0) unless $mc->arg(1);
Branch: 1 branch
BranchTrueFalseTotal
unless $mc->arg(1) 18 0 18
427  
428 # set connect ahead to 0 for all services so they don't spawn extra backends
429 0 foreach my $svc (values %service) {
430 0 $svc->{connect_ahead} = 0;
431 }
432  
433 # tell all sockets we're doing a graceful stop
434 0 my $sf = Perlbal::Socket->get_sock_ref;
435 0 foreach my $k (keys %$sf) {
436 0 my Perlbal::Socket $v = $sf->{$k};
437 0 ▶ $v->die_gracefully if $v->can("die_gracefully");
Branch: 1 branch
BranchTrueFalseTotal
if $v->can("die_gracefully") 0 0 0
438 }
439  
440 # register a post loop callback that will end the event loop when we only have
441 # a single socket left, the AIO socket
442 0 ▶ Perlbal::Socket->SetPostLoopCallback(sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
443 0 my ($descmap, $otherfds) = @_;
444  
445 # Ghetto: duplicate the code we already had for our postloopcallback
446 0 Perlbal::Socket::run_callbacks();
447  
448 # see what we have here; make sure we have no Clients and no unbored Backends
449 0 foreach my $sock (values %$descmap) {
450 0 my $ref = ref $sock;
451 0 ▶ return 1 if $ref =~ /^Perlbal::Client/ && $ref ne 'Perlbal::ClientManage';
Branch: 1 branch
BranchTrueFalseTotal
if $ref =~ /^Perlbal::Client/ and $ref ne "Perlbal::ClientManage" 0 0 0
Condition0%0/3
$ref =~ /^Perlbal::Client/ and $ref ne "Perlbal::ClientManage"
!ll&&!rl&&r
000
MC/DC0%0/2
$ref =~ /^Perlbal::Client/ and $ref ne "Perlbal::ClientManage"
$ref =~ /^Perlbal::Client/ $ref ne "Perlbal::ClientManage"
Truth table0%0/3
A and B
A= $ref =~ /^Perlbal::Client/B= $ref ne "Perlbal::ClientManage"
ABresult
0X0
100
111
452 0 ▶ return 1 if $sock->isa('Perlbal::BackendHTTP') && $sock->{state} ne 'bored';
Branch: 1 branch
BranchTrueFalseTotal
if $sock->isa("Perlbal::BackendHTTP") and $sock->{"state"} ne "bored" 0 0 0
Condition0%0/3
$sock->isa("Perlbal::BackendHTTP") and $sock->{"state"} ne "bored"
!ll&&!rl&&r
000
MC/DC0%0/2
$sock->isa("Perlbal::BackendHTTP") and $sock->{"state"} ne "bored"
$sock->isa("Perlbal::BackendHTTP") $sock->{"state"} ne "bored"
Truth table0%0/3
A and B
A= $sock->isa("Perlbal::BackendHTTP")B= $sock->{"state"} ne "bored"
ABresult
0X0
100
111
453 }
454 0 return 0; # end the event loop and thus we exit perlbal
455 0 });
456  
457 # If requested, register a callback to kill the perlbal process after a specified number of seconds
458 0 ▶ if (my $timeout = $mc->arg(2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $timeout = $mc->arg(2)) 0 0 0
459 0 ▶ Perlbal::Socket::register_callback($timeout, sub { exit(0); });
Subroutine __ANON__: not called CC=1 SCAR=6.9
460 }
461  
462 # so they know something happened
463 0 return $mc->ok;
464 }
465  
466 0 ▶sub MANAGE_mime {
Subroutine MANAGE_mime: not called CC=9 SCAR=45.0
Pod undocumented
467 0 my $mc = shift->parse(qr/^mime(?:\s+(\w+)(?:\s+(\w+))?(?:\s+(\S+))?)?$/);
468 0 my ($cmd, $arg1, $arg2) = ($mc->arg(1), $mc->arg(2), $mc->arg(3));
469  
470 0 ▶ if (!$cmd || $cmd eq 'list') {
Branch: 3 branches
BranchTrueFalseTotal
if (not $cmd or $cmd eq 'list') { } 0 0 0
elsif ($cmd eq 'set') { } 0 0 0
elsif ($cmd eq 'remove') { } 0 0 0
Condition0%0/3
not $cmd or $cmd eq "list"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $cmd or $cmd eq "list"
not $cmd $cmd eq "list"
Truth table0%0/3
not(A) or B
A= not $cmdB= $cmd eq "list"
ABresult
1X1
011
000
471 0 foreach my $key (sort keys %$Perlbal::ClientHTTPBase::MimeType) {
472 0 $mc->out("$key $Perlbal::ClientHTTPBase::MimeType->{$key}");
473 }
474 0 $mc->end;
475 } elsif ($cmd eq 'set') {
476 0 ▶ if (!$arg1 || !$arg2) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg1 and $arg2) 0 0 0
Condition0%0/3
$arg1 and $arg2
!ll&&!rl&&r
000
MC/DC0%0/2
$arg1 and $arg2
$arg1 $arg2
Truth table0%0/3
A and B
A= $arg1B= $arg2
ABresult
0X0
100
111
477 0 return $mc->err("Usage: set <ext> <mime>");
478 }
479  
480 0 $Perlbal::ClientHTTPBase::MimeType->{$arg1} = $arg2;
481 0 return $mc->out("$arg1 set to $arg2.");
482 } elsif ($cmd eq 'remove') {
483 0 ▶ if (delete $Perlbal::ClientHTTPBase::MimeType->{$arg1}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $Perlbal::ClientHTTPBase::MimeType->{$arg1}) { } 0 0 0
484 0 return $mc->out("$arg1 removed.");
485 } else {
486 0 return $mc->err("$arg1 not a defined extension.");
487 }
488 } else {
489 0 return $mc->err("Usage: list, remove <ext>, set <ext> <mime>");
490 }
491 }
492  
493 0 ▶sub MANAGE_xs {
Subroutine MANAGE_xs: not called CC=11 SCAR=48.8
Pod undocumented
494 0 my $mc = shift->parse(qr/^xs(?:\s+(\w+)\s+(\w+))?$/);
495 0 my ($cmd, $module) = ($mc->arg(1), $mc->arg(2));
496  
497 0 ▶ if ($cmd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd) { } 0 0 0
498 # command? verify
499 return $mc->err('Known XS modules: ' . join(', ', sort keys %XSModules) . '.')
500 0 ▶ unless $XSModules{$module};
Branch: 1 branch
BranchTrueFalseTotal
unless $Perlbal::XSModules{$module} 0 0 0
501  
502 # okay, so now enable or disable this module
503 0 ▶ if ($cmd eq 'enable') {
Branch: 2 branches
BranchTrueFalseTotal
if ($cmd eq 'enable') { } 0 0 0
elsif ($cmd eq 'disable') { } 0 0 0
504 0 my $res = eval "return $XSModules{$module}::enable();";
505 0 ▶ return $mc->err("Unable to enable module.")
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
506 unless $res;
507 0 return $mc->ok;
508 } elsif ($cmd eq 'disable') {
509 0 my $res = eval "return $XSModules{$module}::disable();";
510 0 ▶ return $mc->err("Unable to disable module.")
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
511 unless $res;
512 0 return $mc->out("Module disabled.");
513 } else {
514 0 return $mc->err('Usage: xs [ <enable|disable> <module> ]');
515 }
516 } else {
517 # no commands, so just check status
518 0 $mc->out('XS module status:', '');
519 0 foreach my $module (sort keys %XSModules) {
520 0 my $class = $XSModules{$module};
521 0 my $enabled = eval "return \$${class}::Enabled;";
522 0 ▶ my $status = defined $enabled ? ($enabled ? "installed, enabled" :
Branch: 2 branches
BranchTrueFalseTotal
defined $enabled ? : 0 0 0
$enabled ? : 0 0 0
523 "installed, disabled") : "not installed";
524 0 $mc->out(" $module: $status");
525 }
526 0 ▶ $mc->out(' No modules available.') unless %XSModules;
Branch: 1 branch
BranchTrueFalseTotal
unless %Perlbal::XSModules 0 0 0
527 0 $mc->out('');
528 0 $mc->out("To enable a module: xs enable <module>");
529 0 $mc->out("To disable a module: xs disable <module>");
530 }
531 0 $mc->end;
532 }
533  
534 0 ▶sub MANAGE_fd {
Subroutine MANAGE_fd: not called CC=6 SCAR=37.4
Pod undocumented
535 0 my $mc = shift->no_opts;
536 0 ▶ return $mc->err('This command is not available unless BSD::Resource is installed') unless $Perlbal::BSD_RESOURCE_AVAILABLE;
Branch: 1 branch
BranchTrueFalseTotal
unless $Perlbal::BSD_RESOURCE_AVAILABLE 0 0 0
537  
538 # called in list context on purpose, but we want the hard limit
539 0 my (undef, $max) = BSD::Resource::getrlimit(BSD::Resource::RLIMIT_NOFILE());
540 0 my $ct = 0;
541  
542 # first try procfs if one exists, as that's faster than iterating
543 0 ▶ if (opendir(DIR, "/proc/self/fd")) {
Branch: 1 branch
BranchTrueFalseTotal
if (opendir DIR, '/proc/self/fd') { } 0 0 0
544 0 my @dirs = readdir(DIR);
545 0 $ct = scalar(@dirs) - 2; # don't count . and ..
546 0 closedir(DIR);
547 } else {
548 # isatty() is cheap enough to do on everything
549 0 foreach (0..$max) {
550 0 my $res = POSIX::isatty($_);
551 0 ▶ $ct++ if $res || ($! != EBADF);
Branch: 1 branch
BranchTrueFalseTotal
if $res or $! != 9 0 0 0
Condition0%0/3
$res or $! != 9
l!l&&r!l&&!r
000
MC/DC0%0/2
$res or $! != 9
$res $! != 9
Truth table0%0/3
A or B
A= $resB= $! != 9
ABresult
1X1
011
000
552 }
553 }
554 0 $mc->out("max $max");
555 0 $mc->out("cur $ct");
556 0 $mc->end;
557 }
558  
559 18 ▶sub MANAGE_proc {
Subroutine MANAGE_proc: called CC=2 SCAR=6.9
Pod undocumented
560 18 my $mc = shift->no_opts;
561  
562 18 $mc->out('time: ' . time());
563 18 $mc->out('pid: ' . $$);
564  
565  
566 18 ▶ if ($Perlbal::BSD_RESOURCE_AVAILABLE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Perlbal::BSD_RESOURCE_AVAILABLE) 18 0 18
567 18 my $ru = BSD::Resource::getrusage();
568 18 my ($ut, $st) = ($ru->utime, $ru->stime);
569 18 my ($udelta, $sdelta) = ($ut - $lastutime, $st - $laststime);
570 18 $mc->out("utime: $ut (+$udelta)");
571 18 $mc->out("stime: $st (+$sdelta)");
572 18 ($lastutime, $laststime, $lastreqs) = ($ut, $st, $reqs);
573 }
574  
575 18 my $rdelta = $reqs - $lastreqs;
576 18 $mc->out("reqs: $reqs (+$rdelta)");
577 18 $lastreqs = $reqs;
578  
579 18 $mc->end;
580 }
581  
582 6 ▶sub MANAGE_nodes {
Subroutine MANAGE_nodes: called CC=5 SCAR=16.2
Pod undocumented
583 6 my $mc = shift->parse(qr/^nodes?(?:\s+(\d+.\d+.\d+.\d+)(?::(\d+))?)?$/);
584  
585 6 ▶ my ($ip, $port) = ($mc->arg(1), $mc->arg(2) || 80);
Condition50%1/2
$mc->arg(2) || 80
l!l
06
MC/DC0%0/1
$mc->arg(2) || 80
$mc->arg(2)
Truth table50%1/2
A
A= $mc->arg(2)
Aresult
11
00
586 6 ▶ my $spec_ipport = $ip ? "$ip:$port" : undef;
Branch: 1 branch
BranchTrueFalseTotal
$ip ? : 0 6 6
587 6 my $ref = \%Perlbal::BackendHTTP::NodeStats;
588  
589 11 ▶ my $dump = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
590 11 my $ipport = shift;
591 11 foreach my $key (keys %{$ref->{$ipport}}) {
592 78 ▶ if (ref $ref->{$ipport}->{$key} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ref->{$ipport}{$key} eq 'ARRAY') { } 11 67 78
593 11 my %temp;
594 11 $temp{$_}++ foreach @{$ref->{$ipport}->{$key}};
595 11 foreach my $tkey (keys %temp) {
596 11 $mc->out("$ipport $key $tkey $temp{$tkey}");
597 }
598 } else {
599 67 $mc->out("$ipport $key $ref->{$ipport}->{$key}");
600 }
601 }
602 6 };
603  
604 # dump a node, or all nodes
605 6 ▶ if ($spec_ipport) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec_ipport) { } 0 6 6
606 0 $dump->($spec_ipport);
607 } else {
608 6 foreach my $ipport (keys %$ref) {
609 11 $dump->($ipport);
610 }
611 }
612  
613 6 $mc->end;
614 }
615  
616 # singular also works for the nodes command
617 22 *MANAGE_node = \&MANAGE_nodes;
618  
619 0 ▶sub MANAGE_prof {
Subroutine MANAGE_prof: not called CC=6 SCAR=37.4
Pod undocumented
620 0 my $mc = shift->parse(qr/^prof\w*\s+(on|off|data)$/);
621 0 my $which = $mc->arg(1);
622  
623 0 ▶ if ($which eq 'on') {
Branch: 1 branch
BranchTrueFalseTotal
if ($which eq "on") 0 0 0
624 0 ▶ if (Danga::Socket->EnableProfiling) {
Branch: 1 branch
BranchTrueFalseTotal
if ('Danga::Socket'->EnableProfiling) { } 0 0 0
625 0 return $mc->ok;
626 } else {
627 0 return $mc->err('Unable to enable profiling. Please ensure you have the BSD::Resource module installed.');
628 }
629 }
630  
631 0 ▶ if ($which eq 'off') {
Branch: 1 branch
BranchTrueFalseTotal
if ($which eq "off") 0 0 0
632 0 Danga::Socket->DisableProfiling;
633 0 return $mc->ok;
634 }
635  
636 0 ▶ if ($which eq 'data') {
Branch: 1 branch
BranchTrueFalseTotal
if ($which eq "data") 0 0 0
637 0 my $href = Danga::Socket->ProfilingData;
638 0 foreach my $key (sort keys %$href) {
639 0 my ($utime, $stime, $calls) = @{$href->{$key}};
640 0 $mc->out(sprintf("%s %0.5f %0.5f %d %0.7f %0.7f",
641 $key, $utime, $stime, $calls, $utime / $calls, $stime / $calls));
642 }
643 0 $mc->end;
644 }
645 }
646  
647 0 ▶sub MANAGE_uptime {
Subroutine MANAGE_uptime: not called CC=1 SCAR=6.9
Pod undocumented
648 0 my $mc = shift->no_opts;
649  
650 0 $mc->out("starttime $starttime");
651 0 $mc->out("uptime " . (time() - $starttime));
652 0 $mc->out("version $Perlbal::VERSION");
653 0 $mc->end;
654 }
655  
656 22 *MANAGE_version = \&MANAGE_uptime;
657  
658 0 ▶sub MANAGE_track {
Subroutine MANAGE_track: not called CC=3 SCAR=24.8
Pod undocumented
659 0 my $mc = shift->no_opts;
660  
661 0 my $now = time();
662 0 my @list;
663 0 foreach (keys %ObjTrack) {
664 0 my $age = $now - $ObjTrack{$_}->[0];
665 0 push @list, [ $age, "${age}s $_: $ObjTrack{$_}->[1]" ];
666 }
667  
668 # now output based on sorted age
669 0 foreach (sort { $a->[0] <=> $b->[0] } @list) {
670 0 $mc->out($_->[1]);
671 }
672 0 $mc->end;
673 }
674  
675 0 ▶sub MANAGE_socks {
Subroutine MANAGE_socks: not called CC=10 SCAR=47.0
Pod undocumented
676 0 my $mc = shift->parse(qr/^socks(?: (\w+))?$/);
677 0 ▶ my $mode = $mc->arg(1) || "all";
Condition0%0/2
$mc->arg(1) || 'all'
l!l
00
MC/DC0%0/1
$mc->arg(1) || 'all'
$mc->arg(1)
Truth table0%0/2
A
A= $mc->arg(1)
Aresult
11
00
678  
679 0 my $sf = Perlbal::Socket->get_sock_ref;
680  
681 0 ▶ if ($mode eq "summary") {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 'summary') { } 0 0 0
elsif ($mode eq 'all') { } 0 0 0
682 0 my %count;
683 0 my $write_buf = 0;
684 0 my $open_files = 0;
685 0 ▶ while (my $k = each %$sf) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $k = each %$sf)) 0 0 0
686 0 my Perlbal::Socket $v = $sf->{$k};
687 0 $count{ref $v}++;
688 0 $write_buf += $v->{write_buf_size};
689 0 ▶ if ($v->isa("Perlbal::ClientHTTPBase")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->isa("Perlbal::ClientHTTPBase")) 0 0 0
690 0 my Perlbal::ClientHTTPBase $cv = $v;
691 0 ▶ $open_files++ if $cv->{'reproxy_fh'};
Branch: 1 branch
BranchTrueFalseTotal
if $cv->{"reproxy_fh"} 0 0 0
692 }
693 }
694  
695 0 foreach (sort keys %count) {
696 0 $mc->out(sprintf("%5d $_", $count{$_}));
697 }
698 0 $mc->out();
699 0 $mc->out(sprintf("Aggregate write buffer: %.1fk", $write_buf / 1024));
700 0 $mc->out(sprintf(" Open files: %d", $open_files));
701 } elsif ($mode eq "all") {
702 0 my $now = time;
703 0 $mc->out(sprintf("%5s %6s", "fd", "age"));
704 0 foreach (sort { $a <=> $b } keys %$sf) {
705 0 my $sock = $sf->{$_};
706 0 my $age;
707 0 eval {
708 0 $age = $now - $sock->{create_time};
709 };
710 0 ▶ $age ||= 0;
Condition0%0/2
$age ||= 0
l!l
00
MC/DC0%0/1
$age ||= 0
$age
Truth table0%0/2
A
A= $age
Aresult
11
00
711 0 $mc->out(sprintf("%5d %5ds %s", $_, $age, $sock->as_string));
712 }
713 }
714 0 $mc->end;
715 }
716  
717 0 ▶sub MANAGE_backends {
Subroutine MANAGE_backends: not called CC=4 SCAR=30.0
Pod undocumented
718 0 my $mc = shift->no_opts;
719  
720 0 my $sf = Perlbal::Socket->get_sock_ref;
721 0 my %nodes; # { "Backend" => int count }
722 0 foreach my $sock (values %$sf) {
723 0 ▶ if ($sock->isa("Perlbal::BackendHTTP")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sock->isa("Perlbal::BackendHTTP")) 0 0 0
724 0 my Perlbal::BackendHTTP $cv = $sock;
725 0 $nodes{"$cv->{ipport}"}++;
726 }
727 }
728  
729 # now print out text
730 0 foreach my $node (sort keys %nodes) {
731 0 $mc->out("$node " . $nodes{$node});
732 }
733  
734 0 $mc->end;
735 }
736  
737 0 ▶sub MANAGE_noverify {
Subroutine MANAGE_noverify: not called CC=2 SCAR=17.9
Pod undocumented
738 0 my $mc = shift->no_opts;
739  
740 # shows the amount of time left for each node marked as noverify
741 0 my $now = time;
742 0 foreach my $ipport (keys %Perlbal::BackendHTTP::NoVerify) {
743 0 my $until = $Perlbal::BackendHTTP::NoVerify{$ipport} - $now;
744 0 $mc->out("$ipport $until");
745 }
746 0 $mc->end;
747 }
748  
749 0 ▶sub MANAGE_pending {
Subroutine MANAGE_pending: not called CC=6 SCAR=37.4
Pod undocumented
750 0 my $mc = shift->no_opts;
751  
752 # shows pending backend connections by service, node, and age
753 0 my %pend; # { "service" => { "ip:port" => age } }
754 0 my $now = time;
755  
756 0 foreach my $svc (values %service) {
757 0 foreach my $ipport (keys %{$svc->{pending_connects}}) {
758 0 my Perlbal::BackendHTTP $be = $svc->{pending_connects}->{$ipport};
759 0 ▶ next unless defined $be;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $be 0 0 0
760 0 $pend{$svc->{name}}->{$ipport} = $now - $be->{create_time};
761 }
762 }
763  
764 0 foreach my $name (sort keys %pend) {
765 0 foreach my $ipport (sort keys %{$pend{$name}}) {
766 0 $mc->out("$name $ipport $pend{$name}{$ipport}");
767 }
768 }
769 0 $mc->end;
770 }
771  
772 0 ▶sub MANAGE_states {
Subroutine MANAGE_states: not called CC=13 SCAR=52.0
Pod undocumented
773 0 my $mc = shift->parse(qr/^states(?:\s+(.+))?$/);
774  
775 0 my $svc;
776 0 ▶ if (defined $mc->arg(1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mc->arg(1)) 0 0 0
777 0 $svc = $service{$mc->arg(1)};
778 0 ▶ return $mc->err("Service not found.")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $svc 0 0 0
779 unless defined $svc;
780 }
781  
782 0 my $sf = Perlbal::Socket->get_sock_ref;
783  
784 0 my %states; # { "Class" => { "State" => int count; } }
785 0 foreach my $sock (values %$sf) {
786 0 ▶ next unless $sock->can('state');
Branch: 1 branch
BranchTrueFalseTotal
unless $sock->can("state") 0 0 0
787 0 my $state = $sock->state;
788 0 ▶ next unless defined $state;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $state 0 0 0
789 0 ▶ if (defined $svc) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $svc) 0 0 0
790 0 ▶ next unless $sock->isa('Perlbal::ClientProxy') ||
Branch: 1 branch
BranchTrueFalseTotal
unless $sock->isa("Perlbal::ClientProxy") or $sock->isa("Perlbal::BackendHTTP") or $sock->isa("Perlbal::ClientHTTP") 0 0 0
Condition0%0/6
$sock->isa("Perlbal::ClientProxy") or $sock->isa("Perlbal::BackendHTTP") or $sock->isa("Perlbal::ClientHTTP")
l!l&&r!l&&!r
000
$sock->isa("Perlbal::ClientProxy") or $sock->isa("Perlbal::BackendHTTP")
l!l&&r!l&&!r
000
MC/DC0%0/3
$sock->isa("Perlbal::ClientProxy") or $sock->isa("Perlbal::BackendHTTP") or $sock->isa("Perlbal::ClientHTTP")
$sock->isa("Perlbal::ClientProxy") $sock->isa("Perlbal::BackendHTTP") $sock->isa("Perlbal::ClientHTTP")
Truth table0%0/4
A or B or C
A= $sock->isa("Perlbal::ClientProxy")B= $sock->isa("Perlbal::BackendHTTP")C= $sock->isa("Perlbal::ClientHTTP")
ABCresult
1XX1
01X1
0011
0000
791 $sock->isa('Perlbal::BackendHTTP') ||
792 $sock->isa('Perlbal::ClientHTTP');
793 0 ▶ next unless $sock->{service} == $svc;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock->{"service"} == $svc 0 0 0
794 }
795 0 $states{ref $sock}->{$state}++;
796 }
797  
798 # now print out text
799 0 foreach my $class (sort keys %states) {
800 0 foreach my $state (sort keys %{$states{$class}}) {
801 0 $mc->out("$class $state " . $states{$class}->{$state});
802 }
803 }
804 0 $mc->end;
805 }
806  
807 0 ▶sub MANAGE_queues {
Subroutine MANAGE_queues: not called CC=5 SCAR=34.0
Pod undocumented
808 0 my $mc = shift->no_opts;
809 0 my $now = time;
810  
811 0 foreach my $svc (values %service) {
812 0 ▶ next unless $svc->{role} eq 'reverse_proxy';
Branch: 1 branch
BranchTrueFalseTotal
unless $svc->{"role"} eq "reverse_proxy" 0 0 0
813  
814 0 my %queues = (
815 normal => 'waiting_clients',
816 highpri => 'waiting_clients_highpri',
817 lowpri => 'waiting_clients_lowpri',
818 );
819  
820 0 ▶ while (my ($queue_name, $clients_key) = each %queues) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %queues) 0 0 0
821 0 my $age = 0;
822 0 my $count = @{$svc->{$clients_key}};
823 0 my Perlbal::ClientProxy $oldest = $svc->{$clients_key}->[0];
824 0 ▶ $age = $now - $oldest->{last_request_time} if defined $oldest;
Branch: 1 branch
BranchTrueFalseTotal
if defined $oldest 0 0 0
825 0 $mc->out("$svc->{name}-$queue_name.age $age");
826 0 $mc->out("$svc->{name}-$queue_name.count $count");
827 }
828 }
829 0 $mc->end;
830 }
831  
832 0 ▶sub MANAGE_state {
Subroutine MANAGE_state: not called CC=3 SCAR=24.8
Pod undocumented
833 0 my $mc = shift->parse(qr/^state changes$/);
834 0 my $hr = Perlbal::Socket->get_statechange_ref;
835 0 my %final; # { "state" => count }
836 0 ▶ while (my ($obj, $arref) = each %$hr) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$hr) 0 0 0
837 0 $mc->out("$obj: " . join(', ', @$arref));
838 0 $final{$arref->[-1]}++;
839 }
840 0 foreach my $k (sort keys %final) {
841 0 $mc->out("$k $final{$k}");
842 }
843 0 $mc->end;
844 }
845  
846 0 ▶sub MANAGE_leaks {
Subroutine MANAGE_leaks: not called CC=7 SCAR=40.3
Pod undocumented
847 0 my $mc = shift->parse(qr/^leaks(?:\s+(.+))?$/);
848 return $mc->err("command disabled without \$ENV{PERLBAL_DEBUG} set")
849 0 ▶ unless $ENV{PERLBAL_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"PERLBAL_DEBUG"} 0 0 0
850  
851 0 my $what = $mc->arg(1);
852  
853 # iterates over active objects. if you specify an argument, it is treated as code
854 # with $_ being the reference to the object.
855 # shows objects that we think might have been leaked
856 0 my $ref = Perlbal::Socket::get_created_objects_ref;
857 0 foreach (@$ref) {
858 0 ▶ next unless $_; # might be undef!
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 0 0 0
859 0 ▶ if ($what) {
Branch: 1 branch
BranchTrueFalseTotal
if ($what) { } 0 0 0
860 0 my $rv = eval "$what";
861 0 ▶ return $mc->err("$@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
862 0 ▶ next unless defined $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 0 0
863 0 $mc->out($rv);
864 } else {
865 0 $mc->out($_->as_string);
866 }
867 }
868 0 $mc->end;
869 }
870  
871 1 ▶sub MANAGE_show {
Subroutine MANAGE_show: called CC=12 SCAR=40.5
Pod undocumented
872 1 my $mc = shift;
873  
874 1 ▶ if ($mc->cmd =~ /^show service (\w+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mc->cmd =~ /^show service (\w+)$/) 0 1 1
875 0 my $sname = $1;
876 0 my Perlbal::Service $svc = $service{$sname};
877 0 ▶ return $mc->err("Unknown service") unless $svc;
Branch: 1 branch
BranchTrueFalseTotal
unless $svc 0 0 0
878 0 $svc->stats_info($mc->out);
879 0 return $mc->end;
880 }
881  
882 1 ▶ if ($mc->cmd =~ /^show pool(?:\s+(\w+))?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mc->cmd =~ /^show pool(?:\s+(\w+))?$/) 0 1 1
883 0 my $pool = $1;
884 0 ▶ if ($pool) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pool) { } 0 0 0
885 0 my $pl = $pool{$pool};
886 0 ▶ return $mc->err("pool '$pool' does not exist") unless $pl;
Branch: 1 branch
BranchTrueFalseTotal
unless $pl 0 0 0
887  
888 0 foreach my $node (@{ $pl->nodes }) {
889 0 my $ipport = "$node->[0]:$node->[1]";
890 0 $mc->out($ipport . " " . $pl->node_used($ipport));
891 }
892 } else {
893 0 foreach my $name (sort keys %pool) {
894 0 my Perlbal::Pool $pl = $pool{$name};
895 0 $mc->out("$name nodes $pl->{node_count}");
896 0 $mc->out("$name services $pl->{use_count}");
897 }
898 }
899 0 return $mc->end;
900 }
901  
902 1 ▶ if ($mc->cmd =~ /^show service$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mc->cmd =~ /^show service$/) 1 0 1
903 1 foreach my $name (sort keys %service) {
904 4 my $svc = $service{$name};
905 4 ▶ my $listen = $svc->{listen} || "not_listening";
Condition50%1/2
$svc->{'listen'} || 'not_listening'
l!l
40
MC/DC0%0/1
$svc->{'listen'} || 'not_listening'
$svc->{'listen'}
Truth table50%1/2
A
A= $svc->{'listen'}
Aresult
11
00
906 4 ▶ $mc->out("$name $listen " . ($svc->{enabled} ? "ENABLED" : "DISABLED"));
Branch: 1 branch
BranchTrueFalseTotal
$svc->{'enabled'} ? : 4 0 4
907 }
908 1 return $mc->end;
909 }
910  
911 0 return $mc->parse_error;
912 }
913  
914 18 ▶sub MANAGE_server {
Subroutine MANAGE_server: called CC=32 SCAR=63.9
Pod undocumented
915 18 my $mc = shift->parse(qr/^server (\S+) ?= ?(.+)$/);
916 18 my ($key, $val) = ($mc->arg(1), $mc->arg(2));
917  
918 18 ▶ if ($key =~ /^max_reproxy_connections(?:\((.+)\))?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^max_reproxy_connections(?:\((.+)\))?/) 0 18 18
919 0 ▶ return $mc->err("Expected numeric parameter") unless $val =~ /^-?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /^-?\d+$/ 0 0 0
920 0 my $hostip = $1;
921 0 ▶ if (defined $hostip) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hostip) { } 0 0 0
922 0 $Perlbal::ReproxyManager::ReproxyMax{$hostip} = $val+0;
923 } else {
924 0 $Perlbal::ReproxyManager::ReproxyGlobalMax = $val+0;
925 }
926 0 return $mc->ok;
927 }
928  
929 18 ▶ if ($key eq "max_connections") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "max_connections") 0 18 18
930 0 ▶ return $mc->err('This command is not available unless BSD::Resource is installed') unless $Perlbal::BSD_RESOURCE_AVAILABLE;
Branch: 1 branch
BranchTrueFalseTotal
unless $Perlbal::BSD_RESOURCE_AVAILABLE 0 0 0
931 0 ▶ return $mc->err("Expected numeric parameter") unless $val =~ /^-?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /^-?\d+$/ 0 0 0
932 0 my $rv = BSD::Resource::setrlimit(BSD::Resource::RLIMIT_NOFILE(), $val, $val);
933 0 ▶ unless (defined $rv && $rv) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv and $rv) 0 0 0
Condition0%0/3
defined $rv and $rv
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rv and $rv
defined $rv $rv
Truth table0%0/3
A and B
A= defined $rvB= $rv
ABresult
0X0
100
111
934 0 ▶ if ($> == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($> == 0) { } 0 0 0
935 0 $mc->err("Unable to set limit.");
936 } else {
937 0 $mc->err("Need to be root to increase max connections.");
938 }
939 }
940 0 return $mc->ok;
941 }
942  
943 18 ▶ if ($key eq "nice_level") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "nice_level") 0 18 18
944 0 ▶ return $mc->err("Expected numeric parameter") unless $val =~ /^-?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /^-?\d+$/ 0 0 0
945 0 my $rv = POSIX::nice($val);
946 0 ▶ $mc->err("Unable to renice: $!")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 0 0
947 unless defined $rv;
948 0 return $mc->ok;
949 }
950  
951 18 ▶ if ($key eq "aio_mode") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "aio_mode") 18 0 18
952 18 ▶ return $mc->err("Unknown AIO mode") unless $val =~ /^none|linux|ioaio$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /^none|linux|ioaio$/ 0 18 18
953 18 ▶ return $mc->err("Linux::AIO no longer supported") if $val eq "linux";
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "linux" 0 18 18
954 18 ▶ return $mc->err("IO::AIO not available") if $val eq "ioaio" && ! $Perlbal::OPTMOD_IO_AIO;
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "ioaio" and not $Perlbal::OPTMOD_IO_AIO 6 12 18
Condition66%2/3
$val eq "ioaio" and not $Perlbal::OPTMOD_IO_AIO
!ll&&!rl&&r
1206
MC/DC50%1/2
$val eq "ioaio" and not $Perlbal::OPTMOD_IO_AIO
$val eq "ioaio" not $Perlbal::OPTMOD_IO_AIO
Truth table66%2/3
A and not(B)
A= $val eq "ioaio"B= not $Perlbal::OPTMOD_IO_AIO
ABresult
0X0
100
111
955 12 $Perlbal::AIO_MODE = $val;
956 12 return $mc->ok;
957 }
958  
959 0 ▶ if ($key eq "aio_threads") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "aio_threads") 0 0 0
960 0 ▶ return $mc->err("Expected numeric parameter") unless $val =~ /^-?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /^-?\d+$/ 0 0 0
961 0 ▶ IO::AIO::min_parallel($val)
Branch: 1 branch
BranchTrueFalseTotal
if $Perlbal::OPTMOD_IO_AIO 0 0 0
962 if $Perlbal::OPTMOD_IO_AIO;
963 0 return $mc->ok;
964 }
965  
966 0 ▶ if ($key eq "track_obj") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "track_obj") 0 0 0
967 0 ▶ return $mc->err("Expected 1 or 0") unless $val eq '1' || $val eq '0';
Branch: 1 branch
BranchTrueFalseTotal
unless $val eq 1 or $val eq 0 0 0 0
Condition0%0/3
$val eq 1 or $val eq 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$val eq 1 or $val eq 0
$val eq 1 $val eq 0
Truth table0%0/3
A or B
A= $val eq 1B= $val eq 0
ABresult
1X1
011
000
968 0 $track_obj = $val + 0;
969 0 ▶ %ObjTrack = () if $val; # if we're turning it on, clear it out
Branch: 1 branch
BranchTrueFalseTotal
if $val 0 0 0
970 0 return $mc->ok;
971 }
972  
973 0 ▶ if ($key eq "pidfile") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "pidfile") 0 0 0
974 0 ▶ return $mc->err("pidfile must be configured at startup, before Perlbal::run is called") if $run_started;
Branch: 1 branch
BranchTrueFalseTotal
if $Perlbal::run_started 0 0 0
975 0 ▶ return $mc->err("Expected full pathname to pidfile") unless $val;
Branch: 1 branch
BranchTrueFalseTotal
unless $val 0 0 0
976 0 $pidfile = $val;
977 0 return $mc->ok;
978 }
979  
980 0 ▶ if ($key eq "crash_backtrace") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "crash_backtrace") 0 0 0
981 0 ▶ return $mc->err("Expected 1 or 0") unless $val eq '1' || $val eq '0';
Branch: 1 branch
BranchTrueFalseTotal
unless $val eq 1 or $val eq 0 0 0 0
Condition0%0/3
$val eq 1 or $val eq 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$val eq 1 or $val eq 0
$val eq 1 $val eq 0
Truth table0%0/3
A or B
A= $val eq 1B= $val eq 0
ABresult
1X1
011
000
982 0 ▶ if ($val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val) { } 0 0 0
983 0 ▶ $SIG{__DIE__} = sub { Carp::confess(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
984 } else {
985 0 $SIG{__DIE__} = undef;
986 }
987 0 return $mc->ok;
988 }
989  
990 0 return $mc->err("unknown server option '$val'");
991 }
992  
993 0 ▶sub MANAGE_dumpconfig {
Subroutine MANAGE_dumpconfig: not called CC=8 SCAR=42.8
Pod undocumented
994 0 my $mc = shift;
995  
996 0 ▶ while (my ($name, $pool) = each %pool) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %Perlbal::pool) 0 0 0
997 0 $mc->out("CREATE POOL $name");
998  
999 0 ▶ if ($pool->can("dumpconfig")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pool->can('dumpconfig')) { } 0 0 0
1000 0 foreach my $line ($pool->dumpconfig) {
1001 0 $mc->out(" $line");
1002 }
1003 } else {
1004 0 my $class = ref($pool);
1005 0 $mc->out(" # Pool class '$class' is unable to dump config.");
1006 }
1007 } continue {
1008 0 $mc->out("");
1009 }
1010  
1011 0 ▶ while (my ($name, $service) = each %service) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %Perlbal::service) 0 0 0
1012 0 $mc->out("CREATE SERVICE $name");
1013  
1014 0 ▶ if ($service->can("dumpconfig")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($service->can('dumpconfig')) { } 0 0 0
1015 0 foreach my $line ($service->dumpconfig) {
1016 0 $mc->out(" $line");
1017 }
1018 } else {
1019 0 my $class = ref($service);
1020 0 $mc->out(" # Service class '$class' is unable to dump config.");
1021 }
1022  
1023 0 ▶ my $state = $service->{enabled} ? "ENABLE" : "DISABLE";
Branch: 1 branch
BranchTrueFalseTotal
$service->{'enabled'} ? : 0 0 0
1024 0 $mc->out("$state $name");
1025 } continue {
1026 0 $mc->out("");
1027 }
1028  
1029 0 return $mc->ok
1030 }
1031  
1032 0 ▶sub MANAGE_reproxy_state {
Subroutine MANAGE_reproxy_state: not called CC=1 SCAR=6.9
Pod undocumented
1033 0 my $mc = shift;
1034 0 Perlbal::ReproxyManager::dump_state($mc->out);
1035 0 return 1;
1036 }
1037  
1038 53 ▶sub MANAGE_create {
Subroutine MANAGE_create: called CC=7 SCAR=19.9
Pod undocumented
1039 53 my $mc = shift->parse(qr/^create (service|pool) (\w+)$/,
1040 "usage: CREATE {service|pool} <name>");
1041 53 my ($what, $name) = $mc->args;
1042  
1043 53 ▶ if ($what eq "service") {
Branch: 1 branch
BranchTrueFalseTotal
if ($what eq "service") 44 9 53
1044 44 ▶ return $mc->err("service '$name' already exists") if $service{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $Perlbal::service{$name} 0 44 44
1045 44 ▶ return $mc->err("pool '$name' already exists") if $pool{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $Perlbal::pool{$name} 0 44 44
1046 44 Perlbal->create_service($name);
1047 44 $mc->{ctx}{last_created} = $name;
1048 44 return $mc->ok;
1049 }
1050  
1051 9 ▶ if ($what eq "pool") {
Branch: 1 branch
BranchTrueFalseTotal
if ($what eq "pool") 9 0 9
1052 9 ▶ return $mc->err("pool '$name' already exists") if $pool{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $Perlbal::pool{$name} 0 9 9
1053 9 ▶ return $mc->err("service '$name' already exists") if $service{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $Perlbal::service{$name} 0 9 9
1054 9 $vivify_pools = 0;
1055 9 $pool{$name} = Perlbal::Pool->new($name);
1056 9 $mc->{ctx}{last_created} = $name;
1057 9 return $mc->ok;
1058 }
1059 }
1060  
1061 0 ▶sub MANAGE_use {
Subroutine MANAGE_use: not called CC=3 SCAR=24.8
Pod undocumented
1062 0 my $mc = shift->parse(qr/^use (\w+)$/,
1063 "usage: USE <service_or_pool_name>");
1064 0 my ($name) = $mc->args;
1065 0 ▶ return $mc->err("Non-existent pool or service '$name'") unless $pool{$name} || $service{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $Perlbal::pool{$name} or $Perlbal::service{$name} 0 0 0
Condition0%0/3
$Perlbal::pool{$name} or $Perlbal::service{$name}
l!l&&r!l&&!r
000
MC/DC0%0/2
$Perlbal::pool{$name} or $Perlbal::service{$name}
$Perlbal::pool{$name} $Perlbal::service{$name}
Truth table0%0/3
A or B
A= $Perlbal::pool{$name}B= $Perlbal::service{$name}
ABresult
1X1
011
000
1066  
1067 0 $mc->{ctx}{last_created} = $name;
1068 0 return $mc->ok;
1069 }
1070  
1071 15 ▶sub MANAGE_pool {
Subroutine MANAGE_pool: called CC=5 SCAR=16.6
Pod undocumented
1072 15 my $mc = shift->parse(qr/^pool (\w+) (\w+) (\d+.\d+.\d+.\d+)(?::(\d+))?$/);
1073 15 my ($cmd, $name, $ip, $port) = $mc->args;
1074 15 ▶ $port ||= 80;
Condition50%1/2
$port ||= 80
l!l
150
MC/DC0%0/1
$port ||= 80
$port
Truth table50%1/2
A
A= $port
Aresult
11
00
1075  
1076 15 my $good_cmd = qr/^(?:add|remove)$/;
1077  
1078 # "add" and "remove" can be in either order
1079 15 ▶ ($cmd, $name) = ($name, $cmd) if $name =~ /$good_cmd/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /$good_cmd/ 15 0 15
1080 15 ▶ return $mc->err("Invalid command: must be 'add' or 'remove'")
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd =~ /$good_cmd/ 0 15 15
1081 unless $cmd =~ /$good_cmd/;
1082  
1083 15 my $pl = $pool{$name};
1084 15 ▶ return $mc->err("Pool '$name' not found") unless $pl;
Branch: 1 branch
BranchTrueFalseTotal
unless $pl 0 15 15
1085 15 $pl->$cmd($ip, $port);
1086 15 return $mc->ok;
1087 }
1088  
1089 0 ▶sub MANAGE_default {
Subroutine MANAGE_default: not called CC=1 SCAR=6.9
Pod undocumented
1090 0 my $mc = shift->parse(qr/^default (\w+) ?= ?(.+)$/,
1091 "usage: DEFAULT <param> = <value>");
1092  
1093 0 my ($key, $val) = $mc->args;
1094 0 return Perlbal::Service::set_defaults($mc, $key => $val);
1095 }
1096  
1097 230 ▶sub MANAGE_set {
Subroutine MANAGE_set: called CC=5 SCAR=20.3
Pod undocumented
1098 230 my $mc = shift->parse(qr/^set (?:(\w+)[\. ])?([\w\.]+) ?= ?(.+)$/,
1099 "usage: SET [<service>] <param> = <value>");
1100 230 my ($name, $key, $val) = $mc->args;
1101 230 ▶ unless ($name ||= $mc->{ctx}{last_created}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name ||= $mc->{"ctx"}{"last_created"}) 0 230 230
Condition66%2/3
$name ||= $mc->{"ctx"}{"last_created"}
l!l&&r!l&&!r
184460
MC/DC0%0/2
$name ||= $mc->{"ctx"}{"last_created"}
$name $mc->{"ctx"}{"last_created"}
Truth table66%2/3
A ||= B
A= $nameB= $mc->{"ctx"}{"last_created"}
ABresult
1X1
011
000
1102 0 return $mc->err("omitted service/pool name not implied from context");
1103 }
1104  
1105 230 ▶ if (my Perlbal::Service $svc = $service{$name}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my Perlbal::Service $svc = $Perlbal::service{$name}) { } 230 0 230
elsif (my Perlbal::Pool $pl = $Perlbal::pool{$name}) { } 0 0 0
1106 230 return $svc->set($key, $val, $mc);
1107 } elsif (my Perlbal::Pool $pl = $pool{$name}) {
1108 0 return $pl->set($key, $val, $mc);
1109 }
1110 0 return $mc->err("service/pool '$name' does not exist");
1111 }
1112  
1113  
1114 2 ▶sub MANAGE_header {
Subroutine MANAGE_header: called CC=2 SCAR=7.0
Pod undocumented
1115 2 my $mc = shift->parse(qr/^header\s+(\w+)\s+(insert|remove)\s+(.+?)(?:\s*:\s*(.+))?$/i,
1116 "Usage: HEADER <service> {INSERT|REMOVE} <header>[: <value>]");
1117  
1118 2 my ($svc_name, $action, $header, $val) = $mc->args;
1119 2 my $svc = $service{$svc_name};
1120 2 ▶ return $mc->err("service '$svc_name' does not exist") unless $svc;
Branch: 1 branch
BranchTrueFalseTotal
unless $svc 0 2 2
1121 2 return $svc->header_management($action, $header, $val, $mc);
1122 }
1123  
1124 44 ▶sub MANAGE_enable {
Subroutine MANAGE_enable: called CC=2 SCAR=7.0
Pod undocumented
1125 44 my $mc = shift->parse(qr/^(disable|enable) (\w+)$/,
1126 "Usage: {ENABLE|DISABLE} <service>");
1127 44 my ($verb, $name) = $mc->args;
1128 44 my $svc = $service{$name};
1129 44 ▶ return $mc->err("service '$name' does not exist") unless $svc;
Branch: 1 branch
BranchTrueFalseTotal
unless $svc 0 44 44
1130 44 return $svc->$verb($mc);
1131 }
1132 22 *MANAGE_disable = \&MANAGE_enable;
1133  
1134 0 ▶sub MANAGE_unload {
Subroutine MANAGE_unload: not called CC=1 SCAR=6.9
Pod undocumented
1135 0 my $mc = shift->parse(qr/^unload (\w+)$/);
1136 0 my ($fn) = $mc->args;
1137 0 $fn = $PluginCase{lc $fn};
1138 0 my $rv = eval "Perlbal::Plugin::$fn->unload; 1;";
1139 0 $plugins{$fn} = 0;
1140 0 return $mc->ok;
1141 }
1142  
1143  
1144 6 ▶sub MANAGE_load {
Subroutine MANAGE_load: called CC=9 SCAR=26.2
Pod undocumented
1145 6 my $mc = shift->parse(qr/^load \w+$/);
1146  
1147 6 my $fn;
1148 6 ▶ $fn = $1 if $mc->orig =~ /^load (\w+)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $mc->orig =~ /^load (\w+)$/i 6 0 6
1149  
1150 6 my $last_case;
1151 my $last_class;
1152  
1153 6 my $good_error;
1154  
1155 # TODO case protection
1156  
1157 6 foreach my $name ($fn, lc $fn, ucfirst lc $fn) {
1158 6 $last_case = $name;
1159 6 my $class = $last_class = "Perlbal::Plugin::$name";
1160 6 my $file = $class . ".pm";
1161 6 $file =~ s!::!/!g;
1162  
1163 6 ▶ my $rv = eval "use $class; $class->can('load');";
Subroutine BEGIN: called CC=1 SCAR=0.0
1164  
1165 6 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 6 0 6
1166 6 $good_error = undef;
1167 6 last;
1168 }
1169  
1170 # If we don't have a good error yet, start with this one.
1171 0 ▶ $good_error = $@ unless defined $good_error;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $good_error 0 0 0
1172  
1173 # If the file existed perl will place an entry in %INC (though it will be undef due to compilation error)
1174 0 ▶ if ($@ and exists $INC{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ and exists $INC{$file}) 0 0 0
Condition0%0/3
$@ and exists $INC{$file}
!ll&&!rl&&r
000
MC/DC0%0/2
$@ and exists $INC{$file}
$@ exists $INC{$file}
Truth table0%0/3
A and B
A= $@B= exists $INC{$file}
ABresult
0X0
100
111
1175 0 $good_error = $@;
1176 0 last;
1177 }
1178 }
1179  
1180 6 ▶ unless (defined $good_error) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $good_error) 6 0 6
1181 6 my $rv = eval "$last_class->load; 1;";
1182  
1183 6 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 6 0 6
1184 6 $PluginCase{lc $fn} = $last_case;
1185 6 $plugins{$last_case} = $last_class;
1186 6 return $mc->ok;
1187 }
1188  
1189 0 $good_error = $@;
1190 }
1191  
1192 0 return $mc->err($good_error);
1193 }
1194  
1195 0 ▶sub MANAGE_reload {
Subroutine MANAGE_reload: not called CC=8 SCAR=40.0
Pod undocumented
1196 0 my $mc = shift->parse(qr/^reload (\w+)$/);
1197 0 my ($fn) = $mc->args;
1198  
1199 0 ▶ my $class = $PluginCase{lc $fn} or
Branch: 1 branch
BranchTrueFalseTotal
my $class = $Perlbal::PluginCase{lc $fn} or return $mc->err("Unknown/unloaded plugin '${fn}'") 0 0 0
1200 return $mc->err("Unknown/unloaded plugin '$fn'");
1201 0 $class = "Perlbal::Plugin::$class";
1202  
1203 0 ▶ eval "$class->can_reload" or
Branch: 1 branch
BranchTrueFalseTotal
eval "$class->can_reload" or return $mc->err("Plugin $class doesn't support reloading") 0 0 0
1204 return $mc->err("Plugin $class doesn't support reloading");
1205  
1206 0 ▶ if ($class->can("pre_reload_unload")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can("pre_reload_unload")) 0 0 0
1207 0 ▶ eval "$class->pre_reload_unload; 1" or
Branch: 1 branch
BranchTrueFalseTotal
eval "$class->pre_reload_unload; 1" or return $mc->err("Error running $class->pre_reload_unload: $@") 0 0 0
1208 return $mc->err("Error running $class->pre_reload_unload: $@");
1209 }
1210  
1211 0 ▶ eval "$class->unload; 1;" or
Branch: 1 branch
BranchTrueFalseTotal
eval "$class->unload; 1;" or return $mc->err("Failed to unload ${class}: $@") 0 0 0
1212 return $mc->err("Failed to unload $class: $@");
1213  
1214 0 my $file = $class . ".pm";
1215 0 $file =~ s!::!/!g;
1216  
1217 0 ▶ delete $INC{$file} or
Branch: 1 branch
BranchTrueFalseTotal
delete $INC{$file} or die $mc->err("Didn't find $file in %INC") 0 0 0
1218 die $mc->err("Didn't find $file in %INC");
1219  
1220 22 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
1221 0 ▶ eval "use $class; $class->load; 1;" or
Branch: 1 branch
BranchTrueFalseTotal
eval "use $class; $class->load; 1;" or return $mc->err("Failed to reload: $@") 0 0 0
1222 return $mc->err("Failed to reload: $@");
1223  
1224 0 return $mc->ok;
1225 }
1226  
1227 0 ▶sub MANAGE_plugins {
Subroutine MANAGE_plugins: not called CC=3 SCAR=24.8
Pod undocumented
1228 0 my $mc = shift->no_opts;
1229 0 foreach my $svc (values %service) {
1230 0 ▶ next unless @{$svc->{plugin_order}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$svc->{"plugin_order"};} 0 0 0
1231 0 $mc->out(join(' ', $svc->{name}, @{$svc->{plugin_order}}));
1232 }
1233 0 $mc->end;
1234 }
1235  
1236 0 ▶sub MANAGE_help {
Subroutine MANAGE_help: not called CC=3 SCAR=24.8
Pod undocumented
1237 0 my $mc = shift->no_opts;
1238 0 ▶ my @commands = sort map { m/^MANAGE_(\S+)$/ ? $1 : () }
Branch: 1 branch
BranchTrueFalseTotal
/^MANAGE_(\S+)$/ ? : 0 0 0
1239 keys %Perlbal::;
1240 0 foreach my $command (@commands) {
1241 0 $mc->out("$command");
1242 }
1243 0 $mc->end;
1244 }
1245  
1246 0 ▶sub MANAGE_aio {
Subroutine MANAGE_aio: not called CC=3 SCAR=24.8
Pod undocumented
1247 0 my $mc = shift->no_opts;
1248 0 my $stats = Perlbal::AIO::get_aio_stats();
1249 0 foreach my $c (sort keys %$stats) {
1250 0 my $r = $stats->{$c};
1251 0 foreach my $k (keys %$r) {
1252 0 $mc->out("$c $k $r->{$k}");
1253 }
1254 }
1255 0 $mc->end;
1256 }
1257  
1258 4 ▶sub load_config {
Subroutine load_config: called CC=4 SCAR=13.9
Pod undocumented
1259 4 my ($file, $writer) = @_;
1260 4 ▶ open (my $fh, $file) or die "Error opening config file ($file): $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $file or die "Error opening config file ($file): $!\n" 3 1 4
1261 3 my $ctx = Perlbal::CommandContext->new;
1262 3 $ctx->verbose(0);
1263 3 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 24 3 27
1264 24 ▶ return 0 unless run_manage_command($line, $writer, $ctx);
Branch: 1 branch
BranchTrueFalseTotal
unless run_manage_command($line, $writer, $ctx) 0 24 24
1265 }
1266 3 close($fh);
1267 3 return 1;
1268 }
1269  
1270 0 ▶sub daemonize {
Subroutine daemonize: not called CC=6 SCAR=37.4
Pod undocumented
1271 0 my($pid, $sess_id, $i);
1272  
1273 # note that we're not in the foreground (for logging purposes)
1274 0 $foreground = 0;
1275  
1276 # required before fork: (as of old Linux::AIO 1.1, still true?)
1277 0 ▶ IO::AIO::max_parallel(0)
Branch: 1 branch
BranchTrueFalseTotal
if $Perlbal::OPTMOD_IO_AIO 0 0 0
1278 if $Perlbal::OPTMOD_IO_AIO;
1279  
1280 0 my $sigset = POSIX::SigSet->new(SIGINT);
1281 0 ▶ sigprocmask(SIG_BLOCK, $sigset)
Branch: 1 branch
BranchTrueFalseTotal
sigprocmask(0, $sigset) or die "Can't block sigint for fork: $!" 0 0 0
1282 or die "Can't block sigint for fork: $!";
1283  
1284 ## Fork and exit parent
1285 0 ▶ if ($pid = fork) { exit 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($pid = fork) 0 0 0
1286  
1287 0 ▶ sigprocmask(SIG_UNBLOCK, $sigset)
Branch: 1 branch
BranchTrueFalseTotal
sigprocmask(1, $sigset) or die "Can't unblock sigint after fork: $!" 0 0 0
1288 or die "Can't unblock sigint after fork: $!";
1289  
1290 ## Detach ourselves from the terminal
1291 0 ▶ croak "Cannot detach from controlling terminal"
Branch: 1 branch
BranchTrueFalseTotal
unless $sess_id = POSIX::setsid() 0 0 0
1292 unless $sess_id = POSIX::setsid();
1293  
1294 # Handler for INT needs to be restored.
1295 0 $SIG{INT} = 'DEFAULT';
1296  
1297 ## Change working directory
1298 0 chdir "/";
1299  
1300 ## Clear file creation mask
1301 0 umask 0;
1302  
1303 ## Close open file descriptors
1304 0 close(STDIN);
1305 0 close(STDOUT);
1306 0 close(STDERR);
1307  
1308 ## Reopen stderr, stdout, stdin to /dev/null
1309 0 open(STDIN, "+>/dev/null");
1310 0 open(STDOUT, "+>&STDIN");
1311 0 open(STDERR, "+>&STDIN");
1312 }
1313  
1314 # For other apps using Danga::Socket that want to embed Perlbal, this can be called
1315 # directly to start it up. You can call this as many times as you like; it'll
1316 # only actually do what it does the first time it's called.
1317 18 ▶sub initialize {
Subroutine initialize: called CC=4 SCAR=15.2
Pod undocumented
1318 18 ▶ unless ($run_started) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Perlbal::run_started) 18 0 18
1319 18 $run_started = 1;
1320  
1321 # number of AIO threads. the number of outstanding requests isn't
1322 # affected by this
1323 18 ▶ IO::AIO::min_parallel(3) if $Perlbal::OPTMOD_IO_AIO;
Branch: 1 branch
BranchTrueFalseTotal
if $Perlbal::OPTMOD_IO_AIO 0 18 18
1324  
1325 # register IO::AIO pipe which gets written to from threads
1326 # doing blocking IO
1327 18 ▶ if ($Perlbal::OPTMOD_IO_AIO) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Perlbal::OPTMOD_IO_AIO) 0 18 18
1328 0 Perlbal::Socket->AddOtherFds(IO::AIO::poll_fileno() =>
1329 \&IO::AIO::poll_cb);
1330 }
1331  
1332 # The fact that this only runs the first time someone calls initialize()
1333 # means that some things which depend on it might be unreliable when
1334 # used in an embedded perlbal if there is a race for multiple components
1335 # to call initialize().
1336 18 run_global_hook("pre_event_loop");
1337 }
1338 }
1339  
1340 # This is the function to call if you want Perlbal to be in charge of the event loop.
1341 # It won't return until Perlbal is somehow told to exit.
1342 18 ▶sub run {
Subroutine run: called CC=7 SCAR=26.6
Pod undocumented
1343  
1344 # setup for logging
1345 18 ▶ Sys::Syslog::openlog('perlbal', 'pid', 'daemon') if $Perlbal::SYSLOG_AVAILABLE;
Branch: 1 branch
BranchTrueFalseTotal
if $Perlbal::SYSLOG_AVAILABLE 18 0 18
1346 18 $Perlbal::syslog_open = 1;
1347 18 Perlbal::log('info', 'beginning run');
1348 18 my $pidfile_written = 0;
1349 18 ▶ $pidfile_written = _write_pidfile( $pidfile ) if $pidfile;
Branch: 1 branch
BranchTrueFalseTotal
if $Perlbal::pidfile 0 18 18
1350  
1351 18 Perlbal::initialize();
1352  
1353 18 Danga::Socket->SetLoopTimeout(1000);
1354 847 ▶ Danga::Socket->SetPostLoopCallback(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1355 847 $Perlbal::tick_time = time();
1356 847 Perlbal::Socket::run_callbacks();
1357 847 return 1;
1358 18 });
1359  
1360 # begin the overall loop to try to capture if Perlbal dies at some point
1361 # so we can have a log of it
1362 18 eval {
1363 # wait for activity
1364 18 Perlbal::Socket->EventLoop();
1365 };
1366  
1367 0 my $clean_exit = 1;
1368  
1369 # closing messages
1370 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1371 0 Perlbal::log('crit', "crash log: $_") foreach split(/\r?\n/, $@);
1372 0 $clean_exit = 0;
1373 }
1374  
1375 # Note: This will only actually remove the pidfile on 'shutdown graceful'
1376 # A more reliable approach might be to have a pidfile object which fires
1377 # removal on DESTROY.
1378 0 ▶ _remove_pidfile( $pidfile ) if $pidfile_written;
Branch: 1 branch
BranchTrueFalseTotal
if $pidfile_written 0 0 0
1379  
1380 0 Perlbal::log('info', 'ending run');
1381 0 $Perlbal::syslog_open = 0;
1382 0 ▶ Sys::Syslog::closelog() if $Perlbal::SYSLOG_AVAILABLE;
Branch: 1 branch
BranchTrueFalseTotal
if $Perlbal::SYSLOG_AVAILABLE 0 0 0
1383  
1384 0 return $clean_exit;
1385 }
1386  
1387 18 ▶sub log {
Subroutine log: called CC=4 SCAR=17.2
Pod undocumented
1388 # simple logging functionality
1389 18 ▶ if ($foreground) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Perlbal::foreground) { } 18 0 18
1390 # syslog acts like printf so we have to use printf and append a \n
1391 18 shift; # ignore the first parameter (info, warn, crit, etc)
1392 18 my $message = shift;
1393 18 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 18 18
1394 0 printf("$message\n", @_);
1395 } else {
1396 18 print("$message\n");
1397 }
1398 } else {
1399 # just pass the parameters to syslog
1400 0 ▶ Sys::Syslog::syslog(@_) if $Perlbal::syslog_open;
Branch: 1 branch
BranchTrueFalseTotal
if $Perlbal::syslog_open 0 0 0
1401 }
1402 }
1403  
1404  
1405 0 ▶sub _write_pidfile {
Subroutine _write_pidfile: not called CC=4 SCAR=30.0
1406 0 my $file = shift;
1407  
1408 0 my $fh;
1409 0 ▶ unless (open($fh, ">$file")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, ">$file") 0 0 0
1410 0 Perlbal::log('info', "couldn't create pidfile '$file': $!" );
1411 0 return 0;
1412 }
1413 0 ▶ unless ((print $fh "$$\n") && close($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (print $fh "${$}\n" and close $fh) 0 0 0
Condition0%0/3
print $fh "${$}\n" and close $fh
!ll&&!rl&&r
000
MC/DC0%0/2
print $fh "${$}\n" and close $fh
print $fh "${$}\n" close $fh
Truth table0%0/3
A and B
A= print $fh "${$}\n"B= close $fh
ABresult
0X0
100
111
1414 0 Perlbal::log('info', "couldn't write into pidfile '$file': $!" );
1415 0 _remove_pidfile($file);
1416 0 return 0;
1417 }
1418 0 return 1;
1419 }
1420  
1421  
1422 0 ▶sub _remove_pidfile {
Subroutine _remove_pidfile: not called CC=1 SCAR=6.9
1423 0 my $file = shift;
1424
1425 0 unlink $file;
1426 0 return 1;
1427 }
1428  
1429  
1430 # Local Variables:
1431 # mode: perl
1432 # c-basic-indent: 4
1433 # indent-tabs-mode: nil
1434 # End:
1435  
1436 22 1;
« Perlbal/Plugin/Throttle.pm ↑ Summary Perlbal/Plugin/NotModified.pm »