Memcached.pm

filter: Statementstmt 40.9% 242 / 591 Branchbran 21.3% 61 / 286 Conditioncond 20.1% 25 / 124 MC/DCmcdc 4.1% 3 / 73 Subroutinesub 54.4% 37 / 68 Podpod 63.3% 19 / 30 totaltotal 33.0% 387 / 1172 CC 203 SCAR 49.2
CC
203
Cov
33%
stats
462.0
_load_multi
397.2
_set
345.4
CRAP 136.9
↑ Summary Memcached/GetParser.pm »
1 # $Id$
2 #
3 # Copyright (c) 2003, 2004 Brad Fitzpatrick <brad@danga.com>
4 #
5 # See COPYRIGHT section in pod text below for usage and distribution rights.
6 #
7  
8 package Cache::Memcached;
9  
10 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 9 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Storable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Socket qw( MSG_NOSIGNAL PF_INET PF_UNIX IPPROTO_TCP SOCK_STREAM );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use IO::Handle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶use String::CRC32;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 9 ▶use Errno qw( EINPROGRESS EWOULDBLOCK EISCONN );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 9 ▶use Cache::Memcached::GetParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 9 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 9 ▶use fields qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
23 debug no_rehash stats compress_threshold compress_enable stat_callback
24 readonly select_timeout namespace namespace_len servers active buckets
25 pref_ip
26 bucketcount _single_sock _stime
27 connect_timeout cb_connect_fail
28 parser_class
29 buck2sock buck2sock_generation
30 9 };
31  
32 # flag definitions
33 9 ▶use constant F_STORABLE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 9 ▶use constant F_COMPRESS => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 # size savings required before saving compressed value
37 9 ▶use constant COMPRESS_SAVINGS => 0.20; # percent
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 9 ▶use vars qw($VERSION $HAVE_ZLIB $FLAG_NOSIGNAL $HAVE_SOCKET6);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 9 $VERSION = "1.30";
41  
42 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.2
43 9 ▶ $HAVE_ZLIB = eval "use Compress::Zlib (); 1;";
Subroutine BEGIN: called CC=1 SCAR=0.0
44 0 ▶ $HAVE_SOCKET6 = eval "use Socket6 qw(AF_INET6 PF_INET6); 1;";
Subroutine BEGIN: called CC=1 SCAR=1.2
45 }
46  
47 0 ▶my $HAVE_XS = eval "use Cache::Memcached::GetParserXS; 1;";
Subroutine BEGIN: called CC=1 SCAR=1.2
48 9 ▶$HAVE_XS = 0 if $ENV{NO_XS};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"NO_XS"} 0 9 9
49  
50 9 ▶my $parser_class = $HAVE_XS ? "Cache::Memcached::GetParserXS" : "Cache::Memcached::GetParser";
Branch: 1 branch
BranchTrueFalseTotal
$HAVE_XS ? : 0 9 9
51 9 ▶if ($ENV{XS_DEBUG}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"XS_DEBUG"}) 0 9 9
52 0 print "using parser: $parser_class\n";
53 }
54  
55 9 $FLAG_NOSIGNAL = 0;
56 9 eval { $FLAG_NOSIGNAL = MSG_NOSIGNAL; };
57  
58 9 my %host_dead; # host -> unixtime marked dead until
59 my %cache_sock; # host -> socket
60 9 my $socket_cache_generation = 1; # Set to 1 here because below the buck2sock_generation is set to 0, keep them in order.
61  
62 9 my $PROTO_TCP;
63  
64 9 our $SOCK_TIMEOUT = 2.6; # default timeout in seconds
65  
66 2 ▶sub new {
Subroutine new: called CC=10 SCAR=24.2
Pod documented
67 2 my Cache::Memcached $self = shift;
68 2 ▶ $self = fields::new( $self ) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 2 0 2
69  
70 2 ▶ my $args = (@_ == 1) ? shift : { @_ }; # hashref-ify args
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 2 0 2
71  
72 2 $self->{'buck2sock'}= [];
73 2 $self->{'buck2sock_generation'} = 0;
74 2 $self->set_servers($args->{'servers'});
75 2 ▶ $self->{'debug'} = $args->{'debug'} || 0;
Condition50%1/2
$args->{'debug'} || 0
l!l
02
MC/DC0%0/1
$args->{'debug'} || 0
$args->{'debug'}
Truth table50%1/2
A
A= $args->{'debug'}
Aresult
11
00
76 2 $self->{'no_rehash'} = $args->{'no_rehash'};
77 2 $self->{'stats'} = {};
78 2 ▶ $self->{'pref_ip'} = $args->{'pref_ip'} || {};
Condition50%1/2
$args->{'pref_ip'} || {}
l!l
02
MC/DC0%0/1
$args->{'pref_ip'} || {}
$args->{'pref_ip'}
Truth table50%1/2
A
A= $args->{'pref_ip'}
Aresult
11
00
79 2 $self->{'compress_threshold'} = $args->{'compress_threshold'};
80 2 $self->{'compress_enable'} = 1;
81 2 ▶ $self->{'stat_callback'} = $args->{'stat_callback'} || undef;
Condition50%1/2
$args->{'stat_callback'} || undef
l!l
02
MC/DC0%0/1
$args->{'stat_callback'} || undef
$args->{'stat_callback'}
Truth table50%1/2
A
A= $args->{'stat_callback'}
Aresult
11
00
82 2 $self->{'readonly'} = $args->{'readonly'};
83 2 ▶ $self->{'parser_class'} = $args->{'parser_class'} || $parser_class;
Condition33%1/3
$args->{'parser_class'} || $parser_class
l!l&&r!l&&!r
020
MC/DC0%0/2
$args->{'parser_class'} || $parser_class
$args->{'parser_class'} $parser_class
Truth table33%1/3
A || B
A= $args->{'parser_class'}B= $parser_class
ABresult
1X1
011
000
84  
85 # TODO: undocumented
86 2 ▶ $self->{'connect_timeout'} = $args->{'connect_timeout'} || 0.25;
Condition50%1/2
$args->{'connect_timeout'} || 0.25
l!l
02
MC/DC0%0/1
$args->{'connect_timeout'} || 0.25
$args->{'connect_timeout'}
Truth table50%1/2
A
A= $args->{'connect_timeout'}
Aresult
11
00
87 2 ▶ $self->{'select_timeout'} = $args->{'select_timeout'} || 1.0;
Condition50%1/2
$args->{'select_timeout'} || 1.0
l!l
02
MC/DC0%0/1
$args->{'select_timeout'} || 1.0
$args->{'select_timeout'}
Truth table50%1/2
A
A= $args->{'select_timeout'}
Aresult
11
00
88 2 ▶ $self->{namespace} = $args->{namespace} || '';
Condition100%2/2
$args->{'namespace'} || ''
l!l
11
MC/DC100%1/1
$args->{'namespace'} || ''
$args->{'namespace'}
Truth table100%2/2
A
A= $args->{'namespace'}
Aresult
11
00
89 2 $self->{namespace_len} = length $self->{namespace};
90  
91 2 return $self;
92 }
93  
94 0 ▶sub set_pref_ip {
Subroutine set_pref_ip: not called CC=1 SCAR=6.9
Pod undocumented
95 0 my Cache::Memcached $self = shift;
96 0 $self->{'pref_ip'} = shift;
97 }
98  
99 2 ▶sub set_servers {
Subroutine set_servers: called CC=3 SCAR=11.0
Pod documented
100 2 my Cache::Memcached $self = shift;
101 2 my ($list) = @_;
102 2 ▶ $self->{'servers'} = $list || [];
Condition50%1/2
$list || []
l!l
20
MC/DC0%0/1
$list || []
$list
Truth table50%1/2
A
A= $list
Aresult
11
00
103 2 $self->{'active'} = scalar @{$self->{'servers'}};
104 2 $self->{'buckets'} = undef;
105 2 $self->{'bucketcount'} = 0;
106 2 $self->init_buckets;
107  
108 # We didn't close any sockets, so we reset the buck2sock generation, not increment the global socket cache generation.
109 2 $self->{'buck2sock_generation'} = 0;
110  
111 2 $self->{'_single_sock'} = undef;
112 2 ▶ if (@{$self->{'servers'}} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"servers"};} == 1) 2 0 2
113 2 $self->{'_single_sock'} = $self->{'servers'}[0];
114 }
115  
116 2 return $self;
117 }
118  
119 0 ▶sub set_cb_connect_fail {
Subroutine set_cb_connect_fail: not called CC=1 SCAR=6.9
Pod undocumented
120 0 my Cache::Memcached $self = shift;
121 0 $self->{'cb_connect_fail'} = shift;
122 }
123  
124 0 ▶sub set_connect_timeout {
Subroutine set_connect_timeout: not called CC=1 SCAR=6.9
Pod documented
125 0 my Cache::Memcached $self = shift;
126 0 $self->{'connect_timeout'} = shift;
127 }
128  
129 0 ▶sub set_debug {
Subroutine set_debug: not called CC=2 SCAR=17.9
Pod documented
130 0 my Cache::Memcached $self = shift;
131 0 my ($dbg) = @_;
132 0 ▶ $self->{'debug'} = $dbg || 0;
Condition0%0/2
$dbg || 0
l!l
00
MC/DC0%0/1
$dbg || 0
$dbg
Truth table0%0/2
A
A= $dbg
Aresult
11
00
133 }
134  
135 0 ▶sub set_readonly {
Subroutine set_readonly: not called CC=1 SCAR=6.9
Pod documented
136 0 my Cache::Memcached $self = shift;
137 0 my ($ro) = @_;
138 0 $self->{'readonly'} = $ro;
139 }
140  
141 0 ▶sub set_norehash {
Subroutine set_norehash: not called CC=1 SCAR=6.9
Pod documented
142 0 my Cache::Memcached $self = shift;
143 0 my ($val) = @_;
144 0 $self->{'no_rehash'} = $val;
145 }
146  
147 0 ▶sub set_compress_threshold {
Subroutine set_compress_threshold: not called CC=1 SCAR=6.9
Pod documented
148 0 my Cache::Memcached $self = shift;
149 0 my ($thresh) = @_;
150 0 $self->{'compress_threshold'} = $thresh;
151 }
152  
153 0 ▶sub enable_compress {
Subroutine enable_compress: not called CC=1 SCAR=6.9
Pod documented
154 0 my Cache::Memcached $self = shift;
155 0 my ($enable) = @_;
156 0 $self->{'compress_enable'} = $enable;
157 }
158  
159 0 ▶sub forget_dead_hosts {
Subroutine forget_dead_hosts: not called CC=1 SCAR=6.9
Pod undocumented
160 0 my Cache::Memcached $self = shift;
161 0 %host_dead = ();
162  
163 # We need to globally recalculate our buck2sock in all objects, so we increment the global generation.
164 0 $socket_cache_generation++;
165  
166 0 return 1;
167 }
168  
169 0 ▶sub set_stat_callback {
Subroutine set_stat_callback: not called CC=1 SCAR=6.9
Pod undocumented
170 0 my Cache::Memcached $self = shift;
171 0 my ($stat_callback) = @_;
172 0 $self->{'stat_callback'} = $stat_callback;
173 }
174  
175 9 my %sock_map; # stringified-$sock -> "$ip:$port"
176  
177 1 ▶sub _dead_sock {
Subroutine _dead_sock: called CC=3 SCAR=11.1
178 1 my ($self, $sock, $ret, $dead_for) = @_;
179 1 ▶ if (my $ipport = $sock_map{$sock}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ipport = $sock_map{$sock}) 1 0 1
180 1 my $now = time();
181 1 ▶ $host_dead{$ipport} = $now + $dead_for
Branch: 1 branch
BranchTrueFalseTotal
if $dead_for 1 0 1
182 if $dead_for;
183 1 delete $cache_sock{$ipport};
184 1 delete $sock_map{$sock};
185 }
186 # We need to globally recalculate our buck2sock in all objects, so we increment the global generation.
187 1 $socket_cache_generation++;
188  
189 1 return $ret; # 0 or undef, probably, depending on what caller wants
190 }
191  
192 0 ▶sub _close_sock {
Subroutine _close_sock: not called CC=2 SCAR=17.9
193 0 my ($self, $sock) = @_;
194 0 ▶ if (my $ipport = $sock_map{$sock}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ipport = $sock_map{$sock}) 0 0 0
195 0 close $sock;
196 0 delete $cache_sock{$ipport};
197 0 delete $sock_map{$sock};
198 }
199  
200 # We need to globally recalculate our buck2sock in all objects, so we increment the global generation.
201 0 $socket_cache_generation++;
202  
203 0 return 1;
204 }
205  
206 2 ▶sub _connect_sock { # sock, sin, timeout
Subroutine _connect_sock: called CC=10 SCAR=26.9
207 2 my ($sock, $sin, $timeout) = @_;
208 2 ▶ $timeout = 0.25 if not defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout 0 2 2
209  
210 # make the socket non-blocking from now on,
211 # except if someone wants 0 timeout, meaning
212 # a blocking connect, but even then turn it
213 # non-blocking at the end of this function
214  
215 2 ▶ if ($timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout) { } 2 0 2
216 2 IO::Handle::blocking($sock, 0);
217 } else {
218 0 IO::Handle::blocking($sock, 1);
219 }
220  
221 2 my $ret = connect($sock, $sin);
222  
223 2 ▶ if (!$ret && $timeout && $!==EINPROGRESS) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ret and $timeout and $! == 115) 2 0 2
Condition33%2/6
not $ret and $timeout and $! == 115
!ll&&!rl&&r
002
not $ret and $timeout
!ll&&!rl&&r
002
MC/DC0%0/3
not $ret and $timeout and $! == 115
not $ret $timeout $! == 115
Truth table25%1/4
not(A) and B and C
A= not $retB= $timeoutC= $! == 115
ABCresult
0XX0
10X0
1100
1111
224  
225 2 my $win='';
226 2 vec($win, fileno($sock), 1) = 1;
227  
228 2 ▶ if (select(undef, $win, undef, $timeout) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (select(undef, $win, undef, $timeout) > 0) 1 1 2
229 1 $ret = connect($sock, $sin);
230 # EISCONN means connected & won't re-connect, so success
231 1 ▶ $ret = 1 if !$ret && $!==EISCONN;
Branch: 1 branch
BranchTrueFalseTotal
if not $ret and $! == 106 0 1 1
Condition33%1/3
not $ret and $! == 106
!ll&&!rl&&r
100
MC/DC0%0/2
not $ret and $! == 106
not $ret $! == 106
Truth table33%1/3
not(A) and B
A= not $retB= $! == 106
ABresult
0X0
100
111
232 }
233 }
234  
235 2 ▶ unless ($timeout) { # socket was temporarily blocking, now revert
Branch: 1 branch
BranchTrueFalseTotal
unless ($timeout) 0 2 2
236 0 IO::Handle::blocking($sock, 0);
237 }
238  
239 # from here on, we use non-blocking (async) IO for the duration
240 # of the socket's life
241  
242 2 return $ret;
243 }
244  
245 9 ▶sub sock_to_host { # (host) #why is this public? I wouldn't have to worry about undef $self if it weren't.
Subroutine sock_to_host: called CC=25 SCAR=46.8
Pod undocumented
246 9 ▶ my Cache::Memcached $self = ref $_[0] ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 9 0 9
247 9 my $host = $_[0];
248 9 ▶ return $cache_sock{$host} if $cache_sock{$host};
Branch: 1 branch
BranchTrueFalseTotal
if $cache_sock{$host} 6 3 9
249  
250 3 my $now = time();
251 3 my ($ip, $port) = $host =~ /(.*):(\d+)$/;
252 3 ▶ if (defined($ip)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ip) 3 0 3
253 3 $ip =~ s/[\[\]]//g; # get rid of optional IPv6 brackets
254 }
255  
256 return undef if
257 3 ▶ $host_dead{$host} && $host_dead{$host} > $now;
Branch: 1 branch
BranchTrueFalseTotal
if $host_dead{$host} and $host_dead{$host} > $now 1 2 3
Condition66%2/3
$host_dead{$host} and $host_dead{$host} > $now
!ll&&!rl&&r
201
MC/DC50%1/2
$host_dead{$host} and $host_dead{$host} > $now
$host_dead{$host} $host_dead{$host} > $now
Truth table66%2/3
A and B
A= $host_dead{$host}B= $host_dead{$host} > $now
ABresult
0X0
100
111
258 2 my $sock;
259  
260 2 my $connected = 0;
261 2 my $sin;
262 2 ▶ my $proto = $PROTO_TCP ||= getprotobyname('tcp');
Condition33%1/3
$PROTO_TCP ||= getprotobyname "tcp"
l!l&&r!l&&!r
002
MC/DC0%0/2
$PROTO_TCP ||= getprotobyname "tcp"
$PROTO_TCP getprotobyname "tcp"
Truth table33%1/3
A ||= B
A= $PROTO_TCPB= getprotobyname "tcp"
ABresult
1X1
011
000
263  
264 2 ▶ if ( index($host, '/') != 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (index($host, '/') != 0) { } 2 0 2
265 {
266 # if a preferred IP is known, try that first.
267 2 ▶ if ($self && $self->{pref_ip}{$ip}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self and $self->{"pref_ip"}{$ip}) 0 2 2
Condition33%1/3
$self and $self->{"pref_ip"}{$ip}
!ll&&!rl&&r
020
MC/DC0%0/2
$self and $self->{"pref_ip"}{$ip}
$self $self->{"pref_ip"}{$ip}
Truth table33%1/3
A and B
A= $selfB= $self->{"pref_ip"}{$ip}
ABresult
0X0
100
111
268 0 my $prefip = $self->{pref_ip}{$ip};
269 0 ▶ if ($HAVE_SOCKET6 && index($prefip, ':') != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Cache::Memcached::HAVE_SOCKET6 and index($prefip, ':') != -1) { } 0 0 0
Condition0%0/3
$Cache::Memcached::HAVE_SOCKET6 and index($prefip, ":") != -1
!ll&&!rl&&r
000
MC/DC0%0/2
$Cache::Memcached::HAVE_SOCKET6 and index($prefip, ":") != -1
$Cache::Memcached::HAVE_SOCKET6 index($prefip, ":") != -1
Truth table0%0/3
A and B
A= $Cache::Memcached::HAVE_SOCKET6B= index($prefip, ":") != -1
ABresult
0X0
100
111
270 9 ▶ no strict 'subs'; # for PF_INET6 and AF_INET6, weirdly imported
Subroutine BEGIN: called CC=1 SCAR=0.0
271 0 socket($sock, PF_INET6, SOCK_STREAM, $proto);
272 0 $sock_map{$sock} = $host;
273 0 $sin = Socket6::pack_sockaddr_in6($port,
274 Socket6::inet_pton(AF_INET6, $prefip));
275 } else {
276 0 socket($sock, PF_INET, SOCK_STREAM, $proto);
277 0 $sock_map{$sock} = $host;
278 0 $sin = Socket::sockaddr_in($port, Socket::inet_aton($prefip));
279 }
280  
281 0 ▶ if (_connect_sock($sock,$sin,$self->{connect_timeout})) {
Branch: 1 branch
BranchTrueFalseTotal
if (_connect_sock($sock, $sin, $self->{'connect_timeout'})) { } 0 0 0
282 0 $connected = 1;
283 } else {
284 0 ▶ if (my $cb = $self->{cb_connect_fail}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->{"cb_connect_fail"}) 0 0 0
285 0 $cb->($prefip);
286 }
287 0 close $sock;
288 }
289 }
290  
291 # normal path, or fallback path if preferred IP failed
292 2 ▶ unless ($connected) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($connected) 2 0 2
293 2 ▶ if ($HAVE_SOCKET6 && index($ip, ':') != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Cache::Memcached::HAVE_SOCKET6 and index($ip, ':') != -1) { } 0 2 2
Condition33%1/3
$Cache::Memcached::HAVE_SOCKET6 and index($ip, ":") != -1
!ll&&!rl&&r
200
MC/DC0%0/2
$Cache::Memcached::HAVE_SOCKET6 and index($ip, ":") != -1
$Cache::Memcached::HAVE_SOCKET6 index($ip, ":") != -1
Truth table33%1/3
A and B
A= $Cache::Memcached::HAVE_SOCKET6B= index($ip, ":") != -1
ABresult
0X0
100
111
294 9 ▶ no strict 'subs'; # for PF_INET6 and AF_INET6, weirdly imported
Subroutine BEGIN: called CC=1 SCAR=0.0
295 0 socket($sock, PF_INET6, SOCK_STREAM, $proto);
296 0 $sock_map{$sock} = $host;
297 0 $sin = Socket6::pack_sockaddr_in6($port,
298 Socket6::inet_pton(AF_INET6, $ip));
299 } else {
300 2 socket($sock, PF_INET, SOCK_STREAM, $proto);
301 2 $sock_map{$sock} = $host;
302 2 $sin = Socket::sockaddr_in($port, Socket::inet_aton($ip));
303 }
304  
305 2 ▶ my $timeout = $self ? $self->{connect_timeout} : 0.25;
Branch: 1 branch
BranchTrueFalseTotal
$self ? : 2 0 2
306 2 ▶ unless (_connect_sock($sock, $sin, $timeout)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_connect_sock($sock, $sin, $timeout)) 1 1 2
307 1 ▶ my $cb = $self ? $self->{cb_connect_fail} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self ? : 1 0 1
308 1 ▶ $cb->($ip) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 1 1
309 1 return _dead_sock($self, $sock, undef, 20 + int(rand(10)));
310 }
311 }
312 } else { # it's a unix domain/local socket
313 0 socket($sock, PF_UNIX, SOCK_STREAM, 0);
314 0 $sock_map{$sock} = $host;
315 0 $sin = Socket::sockaddr_un($host);
316 0 ▶ my $timeout = $self ? $self->{connect_timeout} : 0.25;
Branch: 1 branch
BranchTrueFalseTotal
$self ? : 0 0 0
317 0 ▶ unless (_connect_sock($sock,$sin,$timeout)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_connect_sock($sock, $sin, $timeout)) 0 0 0
318 0 ▶ my $cb = $self ? $self->{cb_connect_fail} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self ? : 0 0 0
319 0 ▶ $cb->($host) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
320 0 return _dead_sock($self, $sock, undef, 20 + int(rand(10)));
321 }
322 }
323  
324 # make the new socket not buffer writes.
325 1 my $old = select($sock);
326 1 $| = 1;
327 1 select($old);
328  
329 1 $cache_sock{$host} = $sock;
330  
331 1 return $sock;
332 }
333  
334 2 ▶sub get_sock { # (key)
Subroutine get_sock: called CC=8 SCAR=38.8
Pod undocumented
335 2 my Cache::Memcached $self = $_[0];
336 2 my $key = $_[1];
337 2 ▶ return $self->sock_to_host($self->{'_single_sock'}) if $self->{'_single_sock'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_single_sock"} 2 0 2
338 0 ▶ return undef unless $self->{'active'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"active"} 0 0 0
339 0 ▶ my $hv = ref $key ? int($key->[0]) : _hashfunc($key);
Branch: 1 branch
BranchTrueFalseTotal
ref $key ? : 0 0 0
340  
341 0 ▶ my $real_key = ref $key ? $key->[1] : $key;
Branch: 1 branch
BranchTrueFalseTotal
ref $key ? : 0 0 0
342 0 my $tries = 0;
343 0 ▶ while ($tries++ < 20) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tries++ < 20) 0 0 0
344 0 my $host = $self->{'buckets'}->[$hv % $self->{'bucketcount'}];
345 0 my $sock = $self->sock_to_host($host);
346 0 ▶ return $sock if $sock;
Branch: 1 branch
BranchTrueFalseTotal
if $sock 0 0 0
347 0 ▶ return undef if $self->{'no_rehash'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"no_rehash"} 0 0 0
348 0 $hv += _hashfunc($tries . $real_key); # stupid, but works
349 }
350 0 return undef;
351 }
352  
353 2 ▶sub init_buckets {
Subroutine init_buckets: called CC=5 SCAR=17.0
Pod undocumented
354 2 my Cache::Memcached $self = shift;
355 2 ▶ return if $self->{'buckets'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"buckets"} 0 2 2
356 2 my $bu = $self->{'buckets'} = [];
357 2 foreach my $v (@{$self->{'servers'}}) {
358 2 ▶ if (ref $v eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq 'ARRAY') { } 0 2 2
359 0 for (1..$v->[1]) { push @$bu, $v->[0]; }
360 } else {
361 2 push @$bu, $v;
362 }
363 }
364 2 $self->{'bucketcount'} = scalar @{$self->{'buckets'}};
365 }
366  
367 0 ▶sub disconnect_all {
Subroutine disconnect_all: not called CC=2 SCAR=17.9
Pod documented
368 0 my Cache::Memcached $self = shift;
369 0 my $sock;
370 0 foreach $sock (values %cache_sock) {
371 0 close $sock;
372 }
373 0 %cache_sock = ();
374  
375 # We need to globally recalculate our buck2sock in all objects, so we increment the global generation.
376 0 $socket_cache_generation++;
377 }
378  
379 # writes a line, then reads result. by default stops reading after a
380 # single line, but caller can override the $check_complete subref,
381 # which gets passed a scalarref of buffer read thus far.
382 7 ▶sub _write_and_read {
Subroutine _write_and_read: called CC=19 SCAR=33.2
383 7 my Cache::Memcached $self = shift;
384 7 my ($sock, $line, $check_complete) = @_;
385 7 my $res;
386 7 my ($ret, $offset) = (undef, 0);
387  
388 7 ▶ $check_complete ||= sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
389 7 return (rindex($ret, "\r\n") + 2 == length($ret));
390 7 ▶ };
Condition33%1/3
$check_complete ||= sub { return rindex($ret, "\r\n") + 2 == length $ret; }
l!l&&r!l&&!r
070
MC/DC0%0/2
$check_complete ||= sub { return rindex($ret, "\r\n") + 2 == length $ret; }
$check_complete sub { return rindex($ret, "\r\n") + 2 == length $ret; }
Truth table33%1/3
A ||= B
A= $check_completeB= sub { return rindex($ret, "\r\n") + 2 == length $ret; }
ABresult
1X1
011
000
391  
392 # state: 0 - writing, 1 - reading, 2 - done
393 7 my $state = 0;
394  
395 # the bitsets for select
396 7 my ($rin, $rout, $win, $wout);
397 7 my $nfound;
398  
399 7 my $copy_state = -1;
400 7 ▶ local $SIG{'PIPE'} = "IGNORE" unless $FLAG_NOSIGNAL;
Branch: 1 branch
BranchTrueFalseTotal
unless $Cache::Memcached::FLAG_NOSIGNAL 0 7 7
401  
402 # the select loop
403 7 while(1) {
404 21 ▶ if ($copy_state!=$state) {
Branch: 1 branch
BranchTrueFalseTotal
if ($copy_state != $state) 21 0 21
405 21 ▶ last if $state==2;
Branch: 1 branch
BranchTrueFalseTotal
if $state == 2 7 14 21
406 14 ($rin, $win) = ('', '');
407 14 ▶ vec($rin, fileno($sock), 1) = 1 if $state==1;
Branch: 1 branch
BranchTrueFalseTotal
if $state == 1 7 7 14
408 14 ▶ vec($win, fileno($sock), 1) = 1 if $state==0;
Branch: 1 branch
BranchTrueFalseTotal
if $state == 0 7 7 14
409 14 $copy_state = $state;
410 }
411 $nfound = select($rout=$rin, $wout=$win, undef,
412 14 $self->{'select_timeout'});
413 14 ▶ last unless $nfound;
Branch: 1 branch
BranchTrueFalseTotal
unless $nfound 0 14 14
414  
415 14 ▶ if (vec($wout, fileno($sock), 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (vec $wout, fileno $sock, 1) 7 7 14
416 7 $res = send($sock, $line, $FLAG_NOSIGNAL);
417 next
418 7 ▶ if not defined $res and $!==EWOULDBLOCK;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $res and $! == 11 0 7 7
Condition33%1/3
not defined $res and $! == 11
!ll&&!rl&&r
700
MC/DC0%0/2
not defined $res and $! == 11
not defined $res $! == 11
Truth table33%1/3
not(A) and B
A= not defined $resB= $! == 11
ABresult
0X0
100
111
419 7 ▶ unless ($res > 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res > 0) 0 7 7
420 0 $self->_close_sock($sock);
421 0 return undef;
422 }
423 7 ▶ if ($res == length($line)) { # all sent
Branch: 1 branch
BranchTrueFalseTotal
if ($res == length $line) { } 7 0 7
424 7 $state = 1;
425 } else { # we only succeeded in sending some of it
426 0 substr($line, 0, $res, ''); # delete the part we sent
427 }
428 }
429  
430 14 ▶ if (vec($rout, fileno($sock), 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (vec $rout, fileno $sock, 1) 7 7 14
431 7 $res = sysread($sock, $ret, 255, $offset);
432 next
433 7 ▶ if !defined($res) and $!==EWOULDBLOCK;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $res and $! == 11 0 7 7
Condition33%1/3
not defined $res and $! == 11
!ll&&!rl&&r
700
MC/DC0%0/2
not defined $res and $! == 11
not defined $res $! == 11
Truth table33%1/3
not(A) and B
A= not defined $resB= $! == 11
ABresult
0X0
100
111
434 7 ▶ if ($res == 0) { # catches 0=conn closed or undef=error
Branch: 1 branch
BranchTrueFalseTotal
if ($res == 0) 0 7 7
435 0 $self->_close_sock($sock);
436 0 return undef;
437 }
438 7 $offset += $res;
439 7 ▶ $state = 2 if $check_complete->(\$ret);
Branch: 1 branch
BranchTrueFalseTotal
if &$check_complete(\$ret) 7 0 7
440 }
441 }
442  
443 7 ▶ unless ($state == 2) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state == 2) 0 7 7
444 0 $self->_dead_sock($sock); # improperly finished
445 0 return undef;
446 }
447  
448 7 return $ret;
449 }
450  
451 0 ▶sub delete {
Subroutine delete: not called CC=9 SCAR=45.0
Pod documented
452 0 my Cache::Memcached $self = shift;
453 0 my ($key, $time) = @_;
454 0 ▶ return 0 if ! $self->{'active'} || $self->{'readonly'};
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"active"} or $self->{"readonly"} 0 0 0
Condition0%0/3
not $self->{"active"} or $self->{"readonly"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $self->{"active"} or $self->{"readonly"}
not $self->{"active"} $self->{"readonly"}
Truth table0%0/3
not(A) or B
A= not $self->{"active"}B= $self->{"readonly"}
ABresult
1X1
011
000
455 0 ▶ my $stime = Time::HiRes::time() if $self->{'stat_callback'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stat_callback"} 0 0 0
456 0 my $sock = $self->get_sock($key);
457 0 ▶ return 0 unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 0 0
458  
459 0 $self->{'stats'}->{"delete"}++;
460 0 ▶ $key = ref $key ? $key->[1] : $key;
Branch: 1 branch
BranchTrueFalseTotal
ref $key ? : 0 0 0
461 0 ▶ $time = $time ? " $time" : "";
Branch: 1 branch
BranchTrueFalseTotal
$time ? : 0 0 0
462 0 my $cmd = "delete $self->{namespace}$key$time\r\n";
463 0 my $res = _write_and_read($self, $sock, $cmd);
464  
465 0 ▶ if ($self->{'stat_callback'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stat_callback"}) 0 0 0
466 0 my $etime = Time::HiRes::time();
467 0 $self->{'stat_callback'}->($stime, $etime, $sock, 'delete');
468 }
469  
470 0 ▶ return defined $res && $res eq "DELETED\r\n";
Condition0%0/3
defined $res && $res eq "DELETED\r\n"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $res && $res eq "DELETED\r\n"
defined $res $res eq "DELETED\r\n"
Truth table0%0/3
A && B
A= defined $resB= $res eq "DELETED\r\n"
ABresult
0X0
100
111
471 }
472 9 *remove = \&delete;
473  
474 0 ▶sub add {
Subroutine add: not called CC=1 SCAR=6.9
Pod documented
475 0 _set("add", @_);
476 }
477  
478 0 ▶sub replace {
Subroutine replace: not called CC=1 SCAR=6.9
Pod documented
479 0 _set("replace", @_);
480 }
481  
482 2 ▶sub set {
Subroutine set: called CC=1 SCAR=0.0
Pod documented
483 2 _set("set", @_);
484 }
485  
486 0 ▶sub append {
Subroutine append: not called CC=1 SCAR=6.9
Pod undocumented
487 0 _set("append", @_);
488 }
489  
490 0 ▶sub prepend {
Subroutine prepend: not called CC=1 SCAR=6.9
Pod undocumented
491 0 _set("prepend", @_);
492 }
493  
494 2 ▶sub _set {
Subroutine _set: called CC=23 SCAR=58.4
495 2 my $cmdname = shift;
496 2 my Cache::Memcached $self = shift;
497 2 my ($key, $val, $exptime) = @_;
498 2 ▶ return 0 if ! $self->{'active'} || $self->{'readonly'};
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"active"} or $self->{"readonly"} 0 2 2
Condition33%1/3
not $self->{"active"} or $self->{"readonly"}
l!l&&r!l&&!r
002
MC/DC0%0/2
not $self->{"active"} or $self->{"readonly"}
not $self->{"active"} $self->{"readonly"}
Truth table33%1/3
not(A) or B
A= not $self->{"active"}B= $self->{"readonly"}
ABresult
1X1
011
000
499 2 ▶ my $stime = Time::HiRes::time() if $self->{'stat_callback'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stat_callback"} 0 2 2
500 2 my $sock = $self->get_sock($key);
501 2 ▶ return 0 unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 2 0 2
502  
503 9 ▶ use bytes; # return bytes from length()
Subroutine BEGIN: called CC=1 SCAR=0.0
504  
505 0 ▶ my $app_or_prep = $cmdname eq 'append' || $cmdname eq 'prepend' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$cmdname eq 'append' || $cmdname eq 'prepend' ? : 0 0 0
Condition0%0/3
$cmdname eq "append" or $cmdname eq "prepend"
l!l&&r!l&&!r
000
MC/DC0%0/2
$cmdname eq "append" or $cmdname eq "prepend"
$cmdname eq "append" $cmdname eq "prepend"
Truth table0%0/3
A or B
A= $cmdname eq "append"B= $cmdname eq "prepend"
ABresult
1X1
011
000
506 0 $self->{'stats'}->{$cmdname}++;
507 0 my $flags = 0;
508 0 ▶ $key = ref $key ? $key->[1] : $key;
Branch: 1 branch
BranchTrueFalseTotal
ref $key ? : 0 0 0
509  
510 0 ▶ if (ref $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) 0 0 0
511 0 ▶ die "append or prepend cannot take a reference" if $app_or_prep;
Branch: 1 branch
BranchTrueFalseTotal
if $app_or_prep 0 0 0
512 0 local $Carp::CarpLevel = 2;
513 0 $val = Storable::nfreeze($val);
514 0 $flags |= F_STORABLE;
515 }
516 0 ▶ warn "value for memkey:$key is not defined" unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
517  
518 0 my $len = length($val);
519  
520 0 ▶ if ($self->{'compress_threshold'} && $HAVE_ZLIB && $self->{'compress_enable'} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"compress_threshold"} and $Cache::Memcached::HAVE_ZLIB and $self->{"compress_enable"} and $len >= $self->{"compress_threshold"} and not $app_or_prep) 0 0 0
Condition0%0/12
$self->{"compress_threshold"} and $Cache::Memcached::HAVE_ZLIB and $self->{"compress_enable"} and $len >= $self->{"compress_threshold"} and not $app_or_prep
!ll&&!rl&&r
000
$self->{"compress_threshold"} and $Cache::Memcached::HAVE_ZLIB and $self->{"compress_enable"} and $len >= $self->{"compress_threshold"}
!ll&&!rl&&r
000
$self->{"compress_threshold"} and $Cache::Memcached::HAVE_ZLIB and $self->{"compress_enable"}
!ll&&!rl&&r
000
$self->{"compress_threshold"} and $Cache::Memcached::HAVE_ZLIB
!ll&&!rl&&r
000
MC/DC0%0/5
$self->{"compress_threshold"} and $Cache::Memcached::HAVE_ZLIB and $self->{"compress_enable"} and $len >= $self->{"compress_threshold"} and not $app_or_prep
$self->{"compress_threshold"} $Cache::Memcached::HAVE_ZLIB $self->{"compress_enable"} $len >= $self->{"compress_threshold"} not $app_or_prep
Truth table0%0/6
A and B and C and D and not(E)
A= $self->{"compress_threshold"}B= $Cache::Memcached::HAVE_ZLIBC= $self->{"compress_enable"}D= $len >= $self->{"compress_threshold"}E= not $app_or_prep
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
521 $len >= $self->{'compress_threshold'} && !$app_or_prep) {
522  
523 0 my $c_val = Compress::Zlib::memGzip($val);
524 0 my $c_len = length($c_val);
525  
526 # do we want to keep it?
527 0 ▶ if ($c_len < $len*(1 - COMPRESS_SAVINGS)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c_len < $len * "0.8") 0 0 0
528 0 $val = $c_val;
529 0 $len = $c_len;
530 0 $flags |= F_COMPRESS;
531 }
532 }
533  
534 0 ▶ $exptime = int($exptime || 0);
Condition0%0/2
$exptime || 0
l!l
00
MC/DC0%0/1
$exptime || 0
$exptime
Truth table0%0/2
A
A= $exptime
Aresult
11
00
535  
536 0 ▶ local $SIG{'PIPE'} = "IGNORE" unless $FLAG_NOSIGNAL;
Branch: 1 branch
BranchTrueFalseTotal
unless $Cache::Memcached::FLAG_NOSIGNAL 0 0 0
537 0 my $line = "$cmdname $self->{namespace}$key $flags $exptime $len\r\n$val\r\n";
538  
539 0 my $res = _write_and_read($self, $sock, $line);
540  
541 0 ▶ if ($self->{'debug'} && $line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"} and $line) 0 0 0
Condition0%0/3
$self->{"debug"} and $line
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"debug"} and $line
$self->{"debug"} $line
Truth table0%0/3
A and B
A= $self->{"debug"}B= $line
ABresult
0X0
100
111
542 0 chop $line; chop $line;
543 0 print STDERR "Cache::Memcache: $cmdname $self->{namespace}$key = $val ($line)\n";
544 }
545  
546 0 ▶ if ($self->{'stat_callback'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stat_callback"}) 0 0 0
547 0 my $etime = Time::HiRes::time();
548 0 $self->{'stat_callback'}->($stime, $etime, $sock, $cmdname);
549 }
550  
551 0 ▶ return defined $res && $res eq "STORED\r\n";
Condition0%0/3
defined $res && $res eq "STORED\r\n"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $res && $res eq "STORED\r\n"
defined $res $res eq "STORED\r\n"
Truth table0%0/3
A && B
A= defined $resB= $res eq "STORED\r\n"
ABresult
0X0
100
111
552 }
553  
554 0 ▶sub incr {
Subroutine incr: not called CC=1 SCAR=6.9
Pod documented
555 0 _incrdecr("incr", @_);
556 }
557  
558 0 ▶sub decr {
Subroutine decr: not called CC=1 SCAR=6.9
Pod documented
559 0 _incrdecr("decr", @_);
560 }
561  
562 0 ▶sub _incrdecr {
Subroutine _incrdecr: not called CC=10 SCAR=47.0
563 0 my $cmdname = shift;
564 0 my Cache::Memcached $self = shift;
565 0 my ($key, $value) = @_;
566 0 ▶ return undef if ! $self->{'active'} || $self->{'readonly'};
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"active"} or $self->{"readonly"} 0 0 0
Condition0%0/3
not $self->{"active"} or $self->{"readonly"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $self->{"active"} or $self->{"readonly"}
not $self->{"active"} $self->{"readonly"}
Truth table0%0/3
not(A) or B
A= not $self->{"active"}B= $self->{"readonly"}
ABresult
1X1
011
000
567 0 ▶ my $stime = Time::HiRes::time() if $self->{'stat_callback'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stat_callback"} 0 0 0
568 0 my $sock = $self->get_sock($key);
569 0 ▶ return undef unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 0 0
570 0 ▶ $key = $key->[1] if ref $key;
Branch: 1 branch
BranchTrueFalseTotal
if ref $key 0 0 0
571 0 $self->{'stats'}->{$cmdname}++;
572 0 ▶ $value = 1 unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
573  
574 0 my $line = "$cmdname $self->{namespace}$key $value\r\n";
575 0 my $res = _write_and_read($self, $sock, $line);
576  
577 0 ▶ if ($self->{'stat_callback'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stat_callback"}) 0 0 0
578 0 my $etime = Time::HiRes::time();
579 0 $self->{'stat_callback'}->($stime, $etime, $sock, $cmdname);
580 }
581  
582 0 ▶ return undef unless defined $res && $res =~ /^(\d+)/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res and $res =~ /^(\d+)/ 0 0 0
Condition0%0/3
defined $res and $res =~ /^(\d+)/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $res and $res =~ /^(\d+)/
defined $res $res =~ /^(\d+)/
Truth table0%0/3
A and B
A= defined $resB= $res =~ /^(\d+)/
ABresult
0X0
100
111
583 0 return $1;
584 }
585  
586 0 ▶sub get {
Subroutine get: not called CC=3 SCAR=24.8
Pod documented
587 0 my Cache::Memcached $self = $_[0];
588 0 my $key = $_[1];
589  
590 # TODO: make a fast path for this? or just keep using get_multi?
591 0 my $r = $self->get_multi($key);
592 0 ▶ my $kval = ref $key ? $key->[1] : $key;
Branch: 1 branch
BranchTrueFalseTotal
ref $key ? : 0 0 0
593  
594 # key reconstituted from server won't have utf8 on, so turn it off on input
595 # scalar to allow hash lookup to succeed
596 0 ▶ Encode::_utf8_off($kval) if Encode::is_utf8($kval);
Branch: 1 branch
BranchTrueFalseTotal
if &Encode::is_utf8($kval) 0 0 0
597  
598 0 return $r->{$kval};
599 }
600  
601 0 ▶sub get_multi {
Subroutine get_multi: not called CC=16 SCAR=56.1
Pod documented
602 0 my Cache::Memcached $self = shift;
603 0 ▶ return {} unless $self->{'active'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"active"} 0 0 0
604 0 ▶ $self->{'_stime'} = Time::HiRes::time() if $self->{'stat_callback'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stat_callback"} 0 0 0
605 0 $self->{'stats'}->{"get_multi"}++;
606  
607 0 my %val; # what we'll be returning a reference to (realkey -> value)
608 my %sock_keys; # sockref_as_scalar -> [ realkeys ]
609 0 my $sock;
610  
611 0 ▶ if ($self->{'_single_sock'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_single_sock'}) { } 0 0 0
612 0 $sock = $self->sock_to_host($self->{'_single_sock'});
613 0 ▶ unless ($sock) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sock) 0 0 0
614 0 return {};
615 }
616 0 foreach my $key (@_) {
617 0 ▶ my $kval = ref $key ? $key->[1] : $key;
Branch: 1 branch
BranchTrueFalseTotal
ref $key ? : 0 0 0
618 0 push @{$sock_keys{$sock}}, $kval;
619 }
620 } else {
621 0 my $bcount = $self->{'bucketcount'};
622 0 my $sock;
623  
624 0 ▶ if ($self->{'buck2sock_generation'} != $socket_cache_generation) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"buck2sock_generation"} != $socket_cache_generation) 0 0 0
625 0 $self->{'buck2sock_generation'} = $socket_cache_generation;
626 0 $self->{'buck2sock'} = [];
627 }
628  
629 KEY:
630 0 foreach my $key (@_) {
631 0 ▶ my ($hv, $real_key) = ref $key ?
Branch: 1 branch
BranchTrueFalseTotal
ref $key ? : 0 0 0
632 (int($key->[0]), $key->[1]) :
633 ((crc32($key) >> 16) & 0x7fff, $key);
634  
635 0 my $tries;
636 0 while (1) {
637 0 my $bucket = $hv % $bcount;
638  
639 # this segfaults perl 5.8.4 (and others?) if sock_to_host returns undef... wtf?
640 #$sock = $buck2sock[$bucket] ||= $self->sock_to_host($self->{buckets}[ $bucket ])
641 # and last;
642  
643 # but this variant doesn't crash:
644 0 ▶ $sock = $self->{'buck2sock'}->[$bucket] || $self->sock_to_host($self->{buckets}[ $bucket ]);
Condition0%0/3
$self->{'buck2sock'}[$bucket] || $self->sock_to_host($self->{'buckets'}[$bucket])
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'buck2sock'}[$bucket] || $self->sock_to_host($self->{'buckets'}[$bucket])
$self->{'buck2sock'}[$bucket] $self->sock_to_host($self->{'buckets'}[$bucket])
Truth table0%0/3
A || B
A= $self->{'buck2sock'}[$bucket]B= $self->sock_to_host($self->{'buckets'}[$bucket])
ABresult
1X1
011
000
645 0 ▶ if ($sock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sock) 0 0 0
646 0 $self->{'buck2sock'}->[$bucket] = $sock;
647 0 last;
648 }
649  
650 0 ▶ next KEY if $tries++ >= 20;
Branch: 1 branch
BranchTrueFalseTotal
if $tries++ >= 20 0 0 0
651 0 $hv += _hashfunc($tries . $real_key);
652 }
653  
654 0 push @{$sock_keys{$sock}}, $real_key;
655 }
656 }
657  
658 0 $self->{'stats'}->{"get_keys"} += @_;
659 0 $self->{'stats'}->{"get_socks"} += keys %sock_keys;
660  
661 0 ▶ local $SIG{'PIPE'} = "IGNORE" unless $FLAG_NOSIGNAL;
Branch: 1 branch
BranchTrueFalseTotal
unless $Cache::Memcached::FLAG_NOSIGNAL 0 0 0
662  
663 0 _load_multi($self, \%sock_keys, \%val);
664  
665 0 ▶ if ($self->{'debug'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
666 0 ▶ while (my ($k, $v) = each %val) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %val) 0 0 0
667 0 print STDERR "MemCache: got $k = $v\n";
668 }
669 }
670 0 return \%val;
671 }
672  
673 0 ▶sub _load_multi {
Subroutine _load_multi: not called CC=20 SCAR=59.8
674 9 ▶ use bytes; # return bytes from length()
Subroutine BEGIN: called CC=1 SCAR=0.0
675 0 my Cache::Memcached $self;
676 0 my ($sock_keys, $ret);
677  
678 0 ($self, $sock_keys, $ret) = @_;
679  
680 # all keyed by $sockstr:
681 0 my %reading; # $sockstr -> $sock. bool, whether we're reading from this socket
682 my %writing; # $sockstr -> $sock. bool, whether we're writing to this socket
683 0 my %buf; # buffers, for writing
684  
685 0 my %parser; # $sockstr -> Cache::Memcached::GetParser
686  
687 0 my $active_changed = 1; # force rebuilding of select sets
688  
689 0 ▶ my $dead = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
690 0 my $sock = shift;
691 0 ▶ print STDERR "killing socket $sock\n" if $self->{'debug'} >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} >= 2 0 0 0
692 0 delete $reading{$sock};
693 0 delete $writing{$sock};
694  
695 0 ▶ if (my $p = $parser{$sock}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = $parser{$sock}) 0 0 0
696 0 my $key = $p->current_key;
697 0 ▶ delete $ret->{$key} if $key;
Branch: 1 branch
BranchTrueFalseTotal
if $key 0 0 0
698 }
699  
700 0 ▶ if ($self->{'stat_callback'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stat_callback"}) 0 0 0
701 0 my $etime = Time::HiRes::time();
702 0 $self->{'stat_callback'}->($self->{'_stime'}, $etime, $sock, 'get_multi');
703 }
704  
705 0 close $sock;
706 0 $self->_dead_sock($sock);
707 0 };
708  
709 # $finalize->($key, $flags)
710 # $finalize->({ $key => $flags, $key => $flags });
711 0 ▶ my $finalize = sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
712 0 my $map = $_[0];
713 0 ▶ $map = {@_} unless ref $map;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $map 0 0 0
714  
715 0 ▶ while (my ($k, $flags) = each %$map) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$map) 0 0 0
716  
717 # remove trailing \r\n
718 0 chop $ret->{$k}; chop $ret->{$k};
719  
720 0 ▶ $ret->{$k} = Compress::Zlib::memGunzip($ret->{$k})
Branch: 1 branch
BranchTrueFalseTotal
if $Cache::Memcached::HAVE_ZLIB and $flags & 2 0 0 0
Condition0%0/3
$Cache::Memcached::HAVE_ZLIB and $flags & 2
!ll&&!rl&&r
000
MC/DC0%0/2
$Cache::Memcached::HAVE_ZLIB and $flags & 2
$Cache::Memcached::HAVE_ZLIB $flags & 2
Truth table0%0/3
A and B
A= $Cache::Memcached::HAVE_ZLIBB= $flags & 2
ABresult
0X0
100
111
721 if $HAVE_ZLIB && $flags & F_COMPRESS;
722 0 ▶ if ($flags & F_STORABLE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 1) 0 0 0
723 # wrapped in eval in case a perl 5.6 Storable tries to
724 # unthaw data from a perl 5.8 Storable. (5.6 is stupid
725 # and dies if the version number changes at all. in 5.8
726 # they made it only die if it unencounters a new feature)
727 0 eval {
728 0 $ret->{$k} = Storable::thaw($ret->{$k});
729 };
730 # so if there was a problem, just treat it as a cache miss.
731 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
732 0 delete $ret->{$k};
733 }
734 }
735 }
736 0 };
737  
738 0 foreach (keys %$sock_keys) {
739 0 ▶ my $ipport = $sock_map{$_} or die "No map found matching for $_";
Branch: 1 branch
BranchTrueFalseTotal
my $ipport = $sock_map{$_} or die "No map found matching for $_" 0 0 0
740 0 ▶ my $sock = $cache_sock{$ipport} or die "No sock found for $ipport";
Branch: 1 branch
BranchTrueFalseTotal
my $sock = $cache_sock{$ipport} or die "No sock found for $ipport" 0 0 0
741 0 ▶ print STDERR "processing socket $_\n" if $self->{'debug'} >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} >= 2 0 0 0
742 0 $writing{$_} = $sock;
743 0 ▶ if ($self->{namespace}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'namespace'}) { } 0 0 0
744 0 $buf{$_} = join(" ", 'get', (map { "$self->{namespace}$_" } @{$sock_keys->{$_}}), "\r\n");
745 } else {
746 0 $buf{$_} = join(" ", 'get', @{$sock_keys->{$_}}, "\r\n");
747 }
748  
749 0 $parser{$_} = $self->{parser_class}->new($ret, $self->{namespace_len}, $finalize);
750 }
751  
752 0 ▶ my $read = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
753 0 my $sockstr = "$_[0]"; # $sock is $_[0];
754 0 ▶ my $p = $parser{$sockstr} or die;
Branch: 1 branch
BranchTrueFalseTotal
my $p = $parser{$sockstr} or die 0 0 0
755 0 my $rv = $p->parse_from_sock($_[0]);
756 0 ▶ if ($rv > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rv > 0) { } 0 0 0
elsif ($rv < 0) { } 0 0 0
757 # okay, finished with this socket
758 0 delete $reading{$sockstr};
759 } elsif ($rv < 0) {
760 0 $dead->($_[0]);
761 }
762 0 return $rv;
763 0 };
764  
765 # returns 1 when it's done, for success or error. 0 if still working.
766 0 ▶ my $write = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
767 0 my ($sock, $sockstr) = ($_[0], "$_[0]");
768 0 my $res;
769  
770 0 $res = send($sock, $buf{$sockstr}, $FLAG_NOSIGNAL);
771  
772 0 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
if not defined $res and $! == 11 0 0 0
Condition0%0/3
not defined $res and $! == 11
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $res and $! == 11
not defined $res $! == 11
Truth table0%0/3
not(A) and B
A= not defined $resB= $! == 11
ABresult
0X0
100
111
773 if not defined $res and $!==EWOULDBLOCK;
774 0 ▶ unless ($res > 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res > 0) 0 0 0
775 0 $dead->($sock);
776 0 return 1;
777 }
778 0 ▶ if ($res == length($buf{$sockstr})) { # all sent
Branch: 1 branch
BranchTrueFalseTotal
if ($res == length $buf{$sockstr}) { } 0 0 0
779 0 $buf{$sockstr} = "";
780  
781 # switch the socket from writing to reading
782 0 delete $writing{$sockstr};
783 0 $reading{$sockstr} = $sock;
784 0 return 1;
785 } else { # we only succeeded in sending some of it
786 0 substr($buf{$sockstr}, 0, $res, ''); # delete the part we sent
787 }
788 0 return 0;
789 0 };
790  
791 # the bitsets for select
792 0 my ($rin, $rout, $win, $wout);
793 0 my $nfound;
794  
795 # the big select loop
796 0 while(1) {
797 0 ▶ if ($active_changed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($active_changed) 0 0 0
798 0 ▶ last unless %reading or %writing; # no sockets left?
Branch: 1 branch
BranchTrueFalseTotal
unless %reading or %writing 0 0 0
Condition0%0/3
%reading or %writing
l!l&&r!l&&!r
000
MC/DC0%0/2
%reading or %writing
%reading %writing
Truth table0%0/3
A or B
A= %readingB= %writing
ABresult
1X1
011
000
799 0 ($rin, $win) = ('', '');
800 0 foreach (values %reading) {
801 0 vec($rin, fileno($_), 1) = 1;
802 }
803 0 foreach (values %writing) {
804 0 vec($win, fileno($_), 1) = 1;
805 }
806 0 $active_changed = 0;
807 }
808 # TODO: more intelligent cumulative timeout?
809 # TODO: select is interruptible w/ ptrace attach, signal, etc. should note that.
810 $nfound = select($rout=$rin, $wout=$win, undef,
811 0 $self->{'select_timeout'});
812 0 ▶ last unless $nfound;
Branch: 1 branch
BranchTrueFalseTotal
unless $nfound 0 0 0
813  
814 # TODO: possible robustness improvement: we could select
815 # writing sockets for reading also, and raise hell if they're
816 # ready (input unread from last time, etc.)
817 # maybe do that on the first loop only?
818 0 foreach (values %writing) {
819 0 ▶ if (vec($wout, fileno($_), 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (vec $wout, fileno $_, 1) 0 0 0
820 0 ▶ $active_changed = 1 if $write->($_);
Branch: 1 branch
BranchTrueFalseTotal
if &$write($_) 0 0 0
821 }
822 }
823 0 foreach (values %reading) {
824 0 ▶ if (vec($rout, fileno($_), 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (vec $rout, fileno $_, 1) 0 0 0
825 0 ▶ $active_changed = 1 if $read->($_);
Branch: 1 branch
BranchTrueFalseTotal
if &$read($_) 0 0 0
826 }
827 }
828 }
829  
830 # if there're active sockets left, they need to die
831 0 foreach (values %writing) {
832 0 $dead->($_);
833 }
834 0 foreach (values %reading) {
835 0 $dead->($_);
836 }
837  
838 0 return;
839 }
840  
841 0 ▶sub _hashfunc {
Subroutine _hashfunc: not called CC=1 SCAR=6.9
842 0 return (crc32($_[0]) >> 16) & 0x7fff;
843 }
844  
845 7 ▶sub flush_all {
Subroutine flush_all: called CC=5 SCAR=16.1
Pod documented
846 7 my Cache::Memcached $self = shift;
847  
848 7 my $success = 1;
849  
850 7 my @hosts = @{$self->{'buckets'}};
851 7 foreach my $host (@hosts) {
852 7 my $sock = $self->sock_to_host($host);
853 7 my @res = $self->run_command($sock, "flush_all\r\n");
854 7 ▶ $success = 0 unless (scalar @res == 1 && (($res[0] || "") eq "OK\r\n"));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @res == 1 and ($res[0] || "") eq "OK\r\n" 6 1 7
Condition80%4/5
scalar @res == 1 and ($res[0] || "") eq "OK\r\n"
!ll&&!rl&&r
421
$res[0] || ''
l!l
30
MC/DC50%1/2
scalar @res == 1 and ($res[0] || "") eq "OK\r\n"
scalar @res == 1 $res[0]
Truth table66%2/3
A and B
A= scalar @res == 1B= $res[0]
ABresult
0X0
100
111
855 }
856  
857 7 return $success;
858 }
859  
860 # returns array of lines, or () on failure.
861 7 ▶sub run_command {
Subroutine run_command: called CC=4 SCAR=14.0
Pod undocumented
862 7 my Cache::Memcached $self = shift;
863 7 my ($sock, $cmd) = @_;
864 7 ▶ return () unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 7 7
865 7 my $ret;
866 7 my $line = $cmd;
867 7 ▶ while (my $res = _write_and_read($self, $sock, $line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $res = _write_and_read($self, $sock, $line)) 7 0 7
868 7 undef $line;
869 7 $ret .= $res;
870 7 ▶ last if $ret =~ /(?:OK|END|ERROR)\r\n$/;
Branch: 1 branch
BranchTrueFalseTotal
if $ret =~ /(?:OK|END|ERROR)\r\n$/ 7 0 7
871 }
872 7 chop $ret; chop $ret;
873 7 return map { "$_\r\n" } split(/\r\n/, $ret);
874 }
875  
876 0 ▶sub stats {
Subroutine stats: not called CC=21 SCAR=61.4
Pod documented
877 0 my Cache::Memcached $self = shift;
878 0 my ($types) = @_;
879 0 ▶ return 0 unless $self->{'active'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"active"} 0 0 0
880 0 ▶ return 0 unless !ref($types) || ref($types) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $types or ref $types eq "ARRAY" 0 0 0
Condition0%0/3
not ref $types or ref $types eq "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/2
not ref $types or ref $types eq "ARRAY"
not ref $types ref $types eq "ARRAY"
Truth table0%0/3
not(A) or B
A= not ref $typesB= ref $types eq "ARRAY"
ABresult
1X1
011
000
881 0 ▶ if (!ref($types)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $types) 0 0 0
882 0 ▶ if (!$types) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $types) { } 0 0 0
883 # I don't much care what the default is, it should just
884 # be something reasonable. Obviously "reset" should not
885 # be on the list :) but other types that might go in here
886 # include maps, cachedump, slabs, or items. Note that
887 # this does NOT include 'sizes' anymore, as that can freeze
888 # bug servers for a couple seconds.
889 0 $types = [ qw( misc malloc self ) ];
890 } else {
891 0 $types = [ $types ];
892 }
893 }
894  
895 0 my $stats_hr = { };
896  
897 # The "self" stat type is special, it only applies to this very
898 # object.
899 0 ▶ if (grep /^self$/, @$types) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep /^self$/, @$types) 0 0 0
900 0 $stats_hr->{'self'} = \%{ $self->{'stats'} };
901 }
902  
903 0 my %misc_keys = map { $_ => 1 }
904 qw/ bytes bytes_read bytes_written
905 cmd_get cmd_set connection_structures curr_items
906 get_hits get_misses
907 total_connections total_items
908 /;
909  
910 # Now handle the other types, passing each type to each host server.
911 0 my @hosts = @{$self->{'buckets'}};
912 0 HOST: foreach my $host (@hosts) {
913 0 my $sock = $self->sock_to_host($host);
914 0 ▶ next HOST unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 0 0
915 0 TYPE: foreach my $typename (grep !/^self$/, @$types) {
916 0 ▶ my $type = $typename eq 'misc' ? "" : " $typename";
Branch: 1 branch
BranchTrueFalseTotal
$typename eq 'misc' ? : 0 0 0
917 0 ▶ my $lines = _write_and_read($self, $sock, "stats$type\r\n", sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
918 0 my $bref = shift;
919 0 return $$bref =~ /^(?:END|ERROR)\r?\n/m;
920 0 });
921 0 ▶ unless ($lines) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lines) 0 0 0
922 0 $self->_dead_sock($sock);
923 0 next HOST;
924 }
925  
926 0 $lines =~ s/\0//g; # 'stats sizes' starts with NULL?
927  
928 # And, most lines end in \r\n but 'stats maps' (as of
929 # July 2003 at least) ends in \n. ??
930 0 my @lines = split(/\r?\n/, $lines);
931  
932 # Some stats are key-value, some are not. malloc,
933 # sizes, and the empty string are key-value.
934 # ("self" was handled separately above.)
935 0 ▶ if ($typename =~ /^(malloc|sizes|misc)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($typename =~ /^(malloc|sizes|misc)$/) { } 0 0 0
936 # This stat is key-value.
937 0 foreach my $line (@lines) {
938 0 my ($key, $value) = $line =~ /^(?:STAT )?(\w+)\s(.*)/;
939 0 ▶ if ($key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key) 0 0 0
940 0 $stats_hr->{'hosts'}{$host}{$typename}{$key} = $value;
941 }
942 $stats_hr->{'total'}{$key} += $value
943 0 ▶ if $typename eq 'misc' && $key && $misc_keys{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $typename eq "misc" and $key and $misc_keys{$key} 0 0 0
Condition0%0/6
$typename eq "misc" and $key and $misc_keys{$key}
!ll&&!rl&&r
000
$typename eq "misc" and $key
!ll&&!rl&&r
000
MC/DC0%0/3
$typename eq "misc" and $key and $misc_keys{$key}
$typename eq "misc" $key $misc_keys{$key}
Truth table0%0/4
A and B and C
A= $typename eq "misc"B= $keyC= $misc_keys{$key}
ABCresult
0XX0
10X0
1100
1111
944 0 ▶ $stats_hr->{'total'}{"malloc_$key"} += $value
Branch: 1 branch
BranchTrueFalseTotal
if $typename eq "malloc" and $key 0 0 0
Condition0%0/3
$typename eq "malloc" and $key
!ll&&!rl&&r
000
MC/DC0%0/2
$typename eq "malloc" and $key
$typename eq "malloc" $key
Truth table0%0/3
A and B
A= $typename eq "malloc"B= $key
ABresult
0X0
100
111
945 if $typename eq 'malloc' && $key;
946 }
947 } else {
948 # This stat is not key-value so just pull it
949 # all out in one blob.
950 0 $lines =~ s/^END\r?\n//m;
951 0 ▶ $stats_hr->{'hosts'}{$host}{$typename} ||= "";
Condition0%0/2
$stats_hr->{"hosts"}{$host}{$typename} ||= ""
l!l
00
MC/DC0%0/1
$stats_hr->{"hosts"}{$host}{$typename} ||= ""
$stats_hr->{"hosts"}{$host}{$typename}
Truth table0%0/2
A
A= $stats_hr->{"hosts"}{$host}{$typename}
Aresult
11
00
952 0 $stats_hr->{'hosts'}{$host}{$typename} .= "$lines";
953 }
954 }
955 }
956  
957 0 return $stats_hr;
958 }
959  
960 0 ▶sub stats_reset {
Subroutine stats_reset: not called CC=6 SCAR=37.4
Pod undocumented
961 0 my Cache::Memcached $self = shift;
962 0 my ($types) = @_;
963 0 ▶ return 0 unless $self->{'active'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"active"} 0 0 0
964  
965 0 HOST: foreach my $host (@{$self->{'buckets'}}) {
966 0 my $sock = $self->sock_to_host($host);
967 0 ▶ next HOST unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 0 0
968 0 my $ok = _write_and_read($self, $sock, "stats reset");
969 0 ▶ unless (defined $ok && $ok eq "RESET\r\n") {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ok and $ok eq "RESET\r\n") 0 0 0
Condition0%0/3
defined $ok and $ok eq "RESET\r\n"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ok and $ok eq "RESET\r\n"
defined $ok $ok eq "RESET\r\n"
Truth table0%0/3
A and B
A= defined $okB= $ok eq "RESET\r\n"
ABresult
0X0
100
111
970 0 $self->_dead_sock($sock);
971 }
972 }
973 0 return 1;
974 }
975  
976 9 1;
977 __END__
↑ Summary Memcached/GetParser.pm »