Perlbal/ReproxyManager.pm

filter: Statementstmt 74.6% 103 / 138 Branchbran 48.2% 27 / 56 Conditioncond 45.6% 21 / 46 MC/DCmcdc 20.8% 5 / 24 Subroutinesub 90.9% 10 / 11 Podpod 0.0% 0 / 8 totaltotal 58.6% 166 / 283 CC 49 SCAR 35.2
CC
49
Cov
63%
dump_state
110.0
spawn_backend
37.3
register_boredom
14.2
CRAP 33.9
« Perlbal/Plugin/Highpri.pm ↑ Summary Perlbal/Plugin/LazyCDN.pm »
1 # HTTP connection to non-pool backend nodes (probably fast event-based webservers)
2 #
3 # Copyright 2004, Danga Interactive, Inc.
4 # Copyright 2005-2007, Six Apart, Ltd.
5 #
6  
7 package Perlbal::ReproxyManager;
8 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 22 ▶no warnings qw(deprecated);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # class storage to store 'host:ip' => $service objects, for making
13 # reproxies use a service that you can then track
14 22 our $ReproxySelf;
15 22 our %ReproxyConnecting; # ( host:ip => $backend ); keeps track of outstanding connections to backend that
16 # are in the connecting state
17 22 our %ReproxyBored; # ( host:ip => [ $backend, ... ] ); list of our bored backends
18 22 our %ReproxyQueues; # ( host:ip => [ $clientproxy, ... ] ); queued up requests for this backend
19 22 our %ReproxyBackends; # ( host:ip => [ $backend, ... ] ); array of backends we have connected
20 22 our %ReproxyMax; # ( host:ip => int ); maximum number of connections to have open at any one time
21 22 our $ReproxyGlobalMax; # int; the global cap used if no per-host cap is specified
22 22 our $NoSpawn = 0; # bool; when set, spawn_backend immediately returns without running
23 22 our $LastCleanup = 0; # int; time we last ran our cleanup logic (FIXME: temp hack)
24  
25 22 Perlbal::track_var("rep_connecting", \%ReproxyConnecting);
26 22 Perlbal::track_var("rep_bored", \%ReproxyBored);
27 22 Perlbal::track_var("rep_queues", \%ReproxyQueues);
28 22 Perlbal::track_var("rep_backends", \%ReproxyBackends);
29  
30 # singleton new function; returns us if we exist, else creates us
31 21 ▶sub get {
Subroutine get: called CC=2 SCAR=6.9
Pod undocumented
32 21 ▶ return $ReproxySelf if $ReproxySelf;
Branch: 1 branch
BranchTrueFalseTotal
if $Perlbal::ReproxyManager::ReproxySelf 19 2 21
33  
34 # doesn't exist, so create it and return it
35 2 my $class = shift;
36 2 my $self = {};
37 2 bless $self, $class;
38 2 return $ReproxySelf = $self;
39 }
40  
41 # given (clientproxy, primary_res_hdrs), initiate proceedings to process a
42 # request for a reproxy resource
43 21 ▶sub do_reproxy {
Subroutine do_reproxy: called CC=7 SCAR=21.6
Pod undocumented
44 21 my Perlbal::ReproxyManager $self = Perlbal::ReproxyManager->get; # singleton
45 21 my Perlbal::ClientProxy $cp = $_[0];
46 21 ▶ return undef unless $self && $cp;
Branch: 1 branch
BranchTrueFalseTotal
unless $self and $cp 0 21 21
Condition33%1/3
$self and $cp
!ll&&!rl&&r
0021
MC/DC0%0/2
$self and $cp
$self $cp
Truth table33%1/3
A and B
A= $selfB= $cp
ABresult
0X0
100
111
47  
48 # get data we use
49 21 my $datref = $cp->{reproxy_uris}->[0];
50 21 my $ipport = "$datref->[0]:$datref->[1]";
51 21 ▶ push @{$ReproxyQueues{$ipport} ||= []}, $cp;
Condition100%2/2
$Perlbal::ReproxyManager::ReproxyQueues{$ipport} ||= []
l!l
183
MC/DC100%1/1
$Perlbal::ReproxyManager::ReproxyQueues{$ipport} ||= []
$Perlbal::ReproxyManager::ReproxyQueues{$ipport}
Truth table100%2/2
A
A= $Perlbal::ReproxyManager::ReproxyQueues{$ipport}
Aresult
11
00
52  
53 # see if we should do cleanup (FIXME: temp hack)
54 21 my $now = time();
55 21 ▶ if ($LastCleanup < $now - 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Perlbal::ReproxyManager::LastCleanup < $now - 5) 2 19 21
56 # remove closed backends from our array. this is O(n) but n is small
57 # and we're paranoid that just keeping a count would get corrupt over
58 # time. also removes the backends that have clients that are closed.
59 2 @{$ReproxyBackends{$ipport}} = grep {
60 ! $_->{closed} && (! $_->{client} || ! $_->{client}->{closed})
61 0 ▶ } @{$ReproxyBackends{$ipport}};
Condition0%0/6
$_->{'closed'} || $_->{'client'} && $_->{'client'}{'closed'}
l!l&&r!l&&!r
000
$_->{"client"} and $_->{"client"}{"closed"}
!ll&&!rl&&r
000
MC/DC0%0/3
$_->{'closed'} || $_->{'client'} && $_->{'client'}{'closed'}
$_->{'closed'} $_->{"client"} $_->{"client"}{"closed"}
Truth table0%0/4
A || B and C
A= $_->{'closed'}B= $_->{"client"}C= $_->{"client"}{"closed"}
ABCresult
1XX1
0111
00X0
0100
62  
63 2 $LastCleanup = $now;
64 }
65  
66 # now start a new backend
67 21 $self->spawn_backend($ipport);
68 21 return 1;
69 }
70  
71 # part of the reportto interface; this is called when a backend is unable to establish
72 # a connection with a backend. we simply try the next uri.
73 1 ▶sub note_bad_backend_connect {
Subroutine note_bad_backend_connect: called CC=6 SCAR=18.3
Pod undocumented
74 1 my Perlbal::ReproxyManager $self = $_[0];
75 1 my Perlbal::BackendHTTP $be = $_[1];
76  
77 # decrement counts and undef connecting backend
78 1 $ReproxyConnecting{$be->{ipport}} = undef;
79  
80 # if nobody waiting, doesn't matter if we couldn't get to this backend
81 1 ▶ return unless @{$ReproxyQueues{$be->{ipport}} || []};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$Perlbal::ReproxyManager::ReproxyQueues{$be->{"ipport"}} or [];} 0 1 1
Condition50%1/2
$Perlbal::ReproxyManager::ReproxyQueues{$be->{'ipport'}} || []
l!l
10
MC/DC0%0/1
$Perlbal::ReproxyManager::ReproxyQueues{$be->{'ipport'}} || []
$Perlbal::ReproxyManager::ReproxyQueues{$be->{'ipport'}}
Truth table50%1/2
A
A= $Perlbal::ReproxyManager::ReproxyQueues{$be->{'ipport'}}
Aresult
11
00
82  
83 # if we still have some connected backends then ignore this bad connection attempt
84 1 ▶ return if scalar @{$ReproxyBackends{$be->{ipport}} || []};
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$Perlbal::ReproxyManager::ReproxyBackends{$be->{"ipport"}} or [];} 0 1 1
Condition50%1/2
$Perlbal::ReproxyManager::ReproxyBackends{$be->{'ipport'}} || []
l!l
01
MC/DC0%0/1
$Perlbal::ReproxyManager::ReproxyBackends{$be->{'ipport'}} || []
$Perlbal::ReproxyManager::ReproxyBackends{$be->{'ipport'}}
Truth table50%1/2
A
A= $Perlbal::ReproxyManager::ReproxyBackends{$be->{'ipport'}}
Aresult
11
00
85  
86 # at this point, we have no connected backends, and our connecting one failed
87 # so we want to tell all of the waiting clients to try their next uri, because
88 # this host is down.
89 1 ▶ while (my Perlbal::ClientProxy $cp = shift @{$ReproxyQueues{$be->{ipport}}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my Perlbal::ClientProxy $cp = shift @{$Perlbal::ReproxyManager::ReproxyQueues{$be->{"ipport"}};}) 1 1 2
90 1 $cp->try_next_uri;
91 }
92 1 return 1;
93 }
94  
95 # called by a backend when it's ready for a request
96 39 ▶sub register_boredom {
Subroutine register_boredom: called CC=11 SCAR=26.5
Pod undocumented
97 39 my Perlbal::ReproxyManager $self = $_[0];
98 39 my Perlbal::BackendHTTP $be = $_[1];
99  
100 # if this backend was connecting
101 39 my $ipport = $be->{ipport};
102 39 ▶ if ($ReproxyConnecting{$ipport} && $ReproxyConnecting{$ipport} == $be) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Perlbal::ReproxyManager::ReproxyConnecting{$ipport} and $Perlbal::ReproxyManager::ReproxyConnecting{$ipport} == $be) 3 36 39
Condition66%2/3
$Perlbal::ReproxyManager::ReproxyConnecting{$ipport} and $Perlbal::ReproxyManager::ReproxyConnecting{$ipport} == $be
!ll&&!rl&&r
3603
MC/DC50%1/2
$Perlbal::ReproxyManager::ReproxyConnecting{$ipport} and $Perlbal::ReproxyManager::ReproxyConnecting{$ipport} == $be
$Perlbal::ReproxyManager::ReproxyConnecting{$ipport} $Perlbal::ReproxyManager::ReproxyConnecting{$ipport} == $be
Truth table66%2/3
A and B
A= $Perlbal::ReproxyManager::ReproxyConnecting{$ipport}B= $Perlbal::ReproxyManager::ReproxyConnecting{$ipport} == $be
ABresult
0X0
100
111
103 3 $ReproxyConnecting{$ipport} = undef;
104 3 ▶ $ReproxyBackends{$ipport} ||= [];
Condition50%1/2
$Perlbal::ReproxyManager::ReproxyBackends{$ipport} ||= []
l!l
30
MC/DC0%0/1
$Perlbal::ReproxyManager::ReproxyBackends{$ipport} ||= []
$Perlbal::ReproxyManager::ReproxyBackends{$ipport}
Truth table50%1/2
A
A= $Perlbal::ReproxyManager::ReproxyBackends{$ipport}
Aresult
11
00
105 3 push @{$ReproxyBackends{$ipport}}, $be;
106 }
107  
108 # sometimes a backend is closed but it tries to register with us anyway... ignore it
109 # but since this might have been our only one, spawn another
110 39 ▶ if ($be->{closed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($be->{"closed"}) 0 39 39
111 0 $self->spawn_backend($ipport);
112 0 return;
113 }
114  
115 # find some clients to use
116 39 ▶ while (my Perlbal::ClientProxy $cp = shift @{$ReproxyQueues{$ipport} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my Perlbal::ClientProxy $cp = shift @{$Perlbal::ReproxyManager::ReproxyQueues{$ipport} or [];}) 20 19 39
Condition50%1/2
$Perlbal::ReproxyManager::ReproxyQueues{$ipport} || []
l!l
390
MC/DC0%0/1
$Perlbal::ReproxyManager::ReproxyQueues{$ipport} || []
$Perlbal::ReproxyManager::ReproxyQueues{$ipport}
Truth table50%1/2
A
A= $Perlbal::ReproxyManager::ReproxyQueues{$ipport}
Aresult
11
00
117 # safety checks
118 20 ▶ next if $cp->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $cp->{"closed"} 0 20 20
119  
120 # give backend to client
121 20 $cp->use_reproxy_backend($be);
122 20 return;
123 }
124  
125 # no clients if we get here, so push onto bored backend list
126 19 ▶ push @{$ReproxyBored{$ipport} ||= []}, $be;
Condition100%2/2
$Perlbal::ReproxyManager::ReproxyBored{$ipport} ||= []
l!l
172
MC/DC100%1/1
$Perlbal::ReproxyManager::ReproxyBored{$ipport} ||= []
$Perlbal::ReproxyManager::ReproxyBored{$ipport}
Truth table100%2/2
A
A= $Perlbal::ReproxyManager::ReproxyBored{$ipport}
Aresult
11
00
127  
128 # clean up the front of our list if we can (see docs above)
129 19 ▶ if (my Perlbal::BackendHTTP $bbe = $ReproxyBored{$ipport}->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my Perlbal::BackendHTTP $bbe = $Perlbal::ReproxyManager::ReproxyBored{$ipport}[0]) 19 0 19
130 19 ▶ if ($bbe->{alive_time} < time() - 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bbe->{"alive_time"} < time - 5) 0 19 19
131 0 $NoSpawn = 1;
132 0 $bbe->close('have_newer_bored');
133 0 shift @{$ReproxyBored{$ipport}};
134 0 $NoSpawn = 0;
135 }
136 }
137 19 return 0;
138 }
139  
140 # backend closed, decrease counts, etc
141 2 ▶sub note_backend_close {
Subroutine note_backend_close: called CC=1 SCAR=0.0
Pod undocumented
142 2 my Perlbal::ReproxyManager $self = $_[0];
143 2 my Perlbal::BackendHTTP $be = $_[1];
144  
145 # remove closed backends from our array. this is O(n) but n is small
146 # and we're paranoid that just keeping a count would get corrupt over
147 # time.
148 2 @{$ReproxyBackends{$be->{ipport}}} = grep {
149 ! $_->{closed}
150 2 } @{$ReproxyBackends{$be->{ipport}}};
151  
152 # spawn more if needed
153 2 $self->spawn_backend($be->{ipport});
154 }
155  
156 23 ▶sub spawn_backend {
Subroutine spawn_backend: called CC=17 SCAR=36.2
Pod undocumented
157 23 ▶ return if $NoSpawn;
Branch: 1 branch
BranchTrueFalseTotal
if $Perlbal::ReproxyManager::NoSpawn 0 23 23
158  
159 23 my Perlbal::ReproxyManager $self = $_[0];
160 23 my $ipport = $_[1];
161  
162 # if we're already connecting, we don't want to spawn another one
163 23 ▶ if (my Perlbal::BackendHTTP $be = $ReproxyConnecting{$ipport}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my Perlbal::BackendHTTP $be = $Perlbal::ReproxyManager::ReproxyConnecting{$ipport}) 0 23 23
164 # see if this one is too old?
165 0 ▶ if ($be->{create_time} < (time() - 5)) { # older than 5 seconds?
Branch: 1 branch
BranchTrueFalseTotal
if ($be->{'create_time'} < time - 5) { } 0 0 0
166 0 $self->note_bad_backend_connect($be);
167 0 $be->close("connection_timeout");
168  
169 # we return here instead of spawning because closing the backend calls
170 # note_backend_close which will call spawn_backend again, and at that
171 # point we won't have a pending connection and can spawn
172 0 return;
173 } else {
174 # don't spawn more if we're already connecting
175 0 return;
176 }
177 }
178  
179 # if nobody waiting, don't spawn extra connections
180 23 ▶ return unless @{$ReproxyQueues{$ipport} || []};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$Perlbal::ReproxyManager::ReproxyQueues{$ipport} or [];} 2 21 23
Condition50%1/2
$Perlbal::ReproxyManager::ReproxyQueues{$ipport} || []
l!l
230
MC/DC0%0/1
$Perlbal::ReproxyManager::ReproxyQueues{$ipport} || []
$Perlbal::ReproxyManager::ReproxyQueues{$ipport}
Truth table50%1/2
A
A= $Perlbal::ReproxyManager::ReproxyQueues{$ipport}
Aresult
11
00
181  
182 # don't spawn if we have a bored one already
183 21 ▶ while (my Perlbal::BackendHTTP $bbe = pop @{$ReproxyBored{$ipport} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my Perlbal::BackendHTTP $bbe = pop @{$Perlbal::ReproxyManager::ReproxyBored{$ipport} or [];}) 17 4 21
Condition100%2/2
$Perlbal::ReproxyManager::ReproxyBored{$ipport} || []
l!l
183
MC/DC100%1/1
$Perlbal::ReproxyManager::ReproxyBored{$ipport} || []
$Perlbal::ReproxyManager::ReproxyBored{$ipport}
Truth table100%2/2
A
A= $Perlbal::ReproxyManager::ReproxyBored{$ipport}
Aresult
11
00
184  
185 # don't use keep-alive connections if we know the server's
186 # just about to kill the connection for being idle
187 17 my $now = time();
188 17 ▶ if ($bbe->{disconnect_at} && $now + 2 > $bbe->{disconnect_at} ||
Branch: 1 branch
BranchTrueFalseTotal
if ($bbe->{"disconnect_at"} and $now + 2 > $bbe->{"disconnect_at"} or $bbe->{"alive_time"} < $now - 5) 0 17 17
Condition33%2/6
$bbe->{"disconnect_at"} and $now + 2 > $bbe->{"disconnect_at"} or $bbe->{"alive_time"} < $now - 5
l!l&&r!l&&!r
0017
$bbe->{"disconnect_at"} and $now + 2 > $bbe->{"disconnect_at"}
!ll&&!rl&&r
0170
MC/DC0%0/3
$bbe->{"disconnect_at"} and $now + 2 > $bbe->{"disconnect_at"} or $bbe->{"alive_time"} < $now - 5
$bbe->{"disconnect_at"} $now + 2 > $bbe->{"disconnect_at"} $bbe->{"alive_time"} < $now - 5
Truth table20%1/5
A and B or C
A= $bbe->{"disconnect_at"}B= $now + 2 > $bbe->{"disconnect_at"}C= $bbe->{"alive_time"} < $now - 5
ABCresult
11X1
0X11
1011
0X00
1000
189 $bbe->{alive_time} < $now - 5)
190 {
191 0 $NoSpawn = 1;
192 0 $bbe->close("too_close_disconnect");
193 0 $NoSpawn = 0;
194 0 next;
195 }
196  
197 # it's good, give it to someone
198 17 $self->register_boredom($bbe);
199 17 return;
200 }
201  
202 # see if we have too many already?
203 4 ▶ my $max = $ReproxyMax{$ipport} || $ReproxyGlobalMax || 0;
Condition40%2/5
$Perlbal::ReproxyManager::ReproxyMax{$ipport} || $Perlbal::ReproxyManager::ReproxyGlobalMax || 0
l!l
04
$Perlbal::ReproxyManager::ReproxyMax{$ipport} or $Perlbal::ReproxyManager::ReproxyGlobalMax
l!l&&r!l&&!r
004
MC/DC0%0/2
$Perlbal::ReproxyManager::ReproxyMax{$ipport} || $Perlbal::ReproxyManager::ReproxyGlobalMax || 0
$Perlbal::ReproxyManager::ReproxyMax{$ipport} $Perlbal::ReproxyManager::ReproxyGlobalMax
Truth table33%1/3
A or B
A= $Perlbal::ReproxyManager::ReproxyMax{$ipport}B= $Perlbal::ReproxyManager::ReproxyGlobalMax
ABresult
1X1
011
000
204 4 ▶ my $count = scalar @{$ReproxyBackends{$ipport} || []};
Condition100%2/2
$Perlbal::ReproxyManager::ReproxyBackends{$ipport} || []
l!l
31
MC/DC100%1/1
$Perlbal::ReproxyManager::ReproxyBackends{$ipport} || []
$Perlbal::ReproxyManager::ReproxyBackends{$ipport}
Truth table100%2/2
A
A= $Perlbal::ReproxyManager::ReproxyBackends{$ipport}
Aresult
11
00
205 4 ▶ return if $max && ($count >= $max);
Branch: 1 branch
BranchTrueFalseTotal
if $max and $count >= $max 0 4 4
Condition33%1/3
$max and $count >= $max
!ll&&!rl&&r
400
MC/DC0%0/2
$max and $count >= $max
$max $count >= $max
Truth table33%1/3
A and B
A= $maxB= $count >= $max
ABresult
0X0
100
111
206  
207 # start one connecting and enqueue
208 4 ▶ my $be = Perlbal::BackendHTTP->new(undef, split(/:/, $ipport), { reportto => $self })
Branch: 1 branch
BranchTrueFalseTotal
my $be = "Perlbal::BackendHTTP"->new(undef, split(/:/, $ipport, 0), {"reportto", $self}) or return 0 4 0 4
209 or return 0;
210 4 $ReproxyConnecting{$ipport} = $be;
211 }
212  
213 20 ▶sub backend_response_received {
Subroutine backend_response_received: called CC=2 SCAR=7.6
Pod undocumented
214 20 my Perlbal::ReproxyManager $self = $_[0];
215 20 my Perlbal::BackendHTTP $be = $_[1];
216 20 my Perlbal::ClientProxy $cp = $be->{client};
217  
218 # if no client, close backend and return 1
219 20 ▶ unless ($cp) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cp) 0 20 20
220 0 $be->close("lost_client");
221 0 return 1;
222 }
223  
224 # pass on to client
225 20 return $cp->backend_response_received($be);
226 }
227  
228 0 ▶sub dump_state {
Subroutine dump_state: not called CC=10 SCAR=47.0
Pod undocumented
229 0 my $out = shift;
230 0 ▶ return unless $out;
Branch: 1 branch
BranchTrueFalseTotal
unless $out 0 0 0
231  
232 # spits out what we have connecting
233 0 ▶ while (my ($hostip, $dat) = each %ReproxyConnecting) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %Perlbal::ReproxyManager::ReproxyConnecting) 0 0 0
234 0 ▶ $out->("connecting $hostip 1") if defined $dat;
Branch: 1 branch
BranchTrueFalseTotal
if defined $dat 0 0 0
235 }
236 0 ▶ while (my ($hostip, $dat) = each %ReproxyBored) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %Perlbal::ReproxyManager::ReproxyBored) 0 0 0
237 0 $out->("bored $hostip " . scalar(@$dat));
238 }
239 0 ▶ while (my ($hostip, $dat) = each %ReproxyQueues) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %Perlbal::ReproxyManager::ReproxyQueues) 0 0 0
240 0 $out->("clients_queued $hostip " . scalar(@$dat));
241 }
242 0 ▶ while (my ($hostip, $dat) = each %ReproxyBackends) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %Perlbal::ReproxyManager::ReproxyBackends) 0 0 0
243 0 $out->("backends $hostip " . scalar(@$dat));
244 0 foreach my $be (@$dat) {
245 0 $out->("... " . $be->as_string);
246 }
247 }
248 0 ▶ while (my ($hostip, $dat) = each %ReproxyMax) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %Perlbal::ReproxyManager::ReproxyMax) 0 0 0
249 0 $out->("SERVER max_reproxy_connections($hostip) = $dat");
250 }
251 0 ▶ $out->("SERVER max_reproxy_connections = " . ($ReproxyGlobalMax || 0));
Condition0%0/2
$Perlbal::ReproxyManager::ReproxyGlobalMax || 0
l!l
00
MC/DC0%0/1
$Perlbal::ReproxyManager::ReproxyGlobalMax || 0
$Perlbal::ReproxyManager::ReproxyGlobalMax
Truth table0%0/2
A
A= $Perlbal::ReproxyManager::ReproxyGlobalMax
Aresult
11
00
252 0 $out->('.');
253 }
254  
255 22 1;
« Perlbal/Plugin/Highpri.pm ↑ Summary Perlbal/Plugin/LazyCDN.pm »