| 1 | package Web::Session::Store::Redis; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use JSON; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 10 | our $VERSION = '1.2.0'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # Key of the session object holding the result of update(), see "DESCRIPTION" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 10 | ▶ | use constant STATUS => 'web_session_redis_update'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # Key of the session object holding the version read, see _writeMerged() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 10 | ▶ | use constant READ => 'web_session_redis_read'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # Writes tried before a session updated concurrently is overwritten | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 10 | our $MAX_TRIES = 5; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 10 | our $redis; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 10 | our %reused_connections; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 10 | our %selected; # reuse key => [ refaddr of the connection, database ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # A reused connection that lost its server (Redis restarted, network | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | # failure) stays broken unless the client reconnects by itself (reconnect | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # option): Redis.pm and Redis::Fast then fail every command with "Not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # connected to any server". After a read timeout, a late reply would be taken | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | # for the one of the next command. After a Sentinel failover, it may be | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | # connected to a read-only replica. So when an operation fails with such an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # error, the connection is forgotten and the next tie opens a new one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # (Sentinel giving the new primary). A read is tried again at once on a new | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # connection: it changes nothing; so is the write of a new session: same id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # same data. An update is not: the error is raised, as the update may have | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # been done. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # The messages of Redis.pm and Redis::Fast are in English whatever the locale; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | # only the system error that may follow them is translated. READONLY is a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | # reply of the server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 10 | our $CONNECTION_ERROR = qr/ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | Not\ connected\ to\ any\ server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | | Could\ not\ (?:connect|write)\ to\ Redis\ server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | | Error\ while\ reading\ from\ Redis\ server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | | EOF\ from\ server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | | Redis\ server\ closed\ connection | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | | \bREADONLY\b | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | /x; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 10 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 10 | $redis = 'Redis::Fast'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 10 | ▶ | eval 'use Redis::Fast'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 10 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | require Redis; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | $redis = 'Redis'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | # Redis clients croak on characters above U+00FF: key names built from a field | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | # name or a value are sent as Latin-1 bytes (existing index sets keep their | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | # name) or, if they hold such characters, as UTF-8 bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 224 | ▶ | sub keyName { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
keyName: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 224 | my $name = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 224 | $name = "$name"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 224 | ▶ | utf8::encode($name) unless ( utf8::downgrade( $name, 1 ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 224 | return $name; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 10 | our $HASH_INDEX_PREFIX = 'hidx:'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # Index set of value $value of field $field | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 139 | ▶ | sub indexName { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
indexName: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 139 | my ( $field, $value ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 139 | return keyName("${field}_$value"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | # Index hash of value $value of field $field, used with utimeOffset | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 61 | ▶ | sub hashIndexName { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hashIndexName: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 61 | my ( $field, $value ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 61 | return keyName("$HASH_INDEX_PREFIX${field}_$value"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | # '00' is truthy: the numeric test disables it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 224 | ▶ | sub hashIndex { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hashIndex: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 224 | my ($args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 224 | ▶ | return ( $args->{utimeOffset} and $args->{utimeOffset} > 0 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$args->{'utimeOffset'} && $args->{'utimeOffset'} > 0
MC/DC100%2/2
$args->{'utimeOffset'} && $args->{'utimeOffset'} > 0
$args->{'utimeOffset'}
$args->{'utimeOffset'} > 0
Truth table100%3/3
A && B
A= $args->{'utimeOffset'}B= $args->{'utimeOffset'} > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 5 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=32.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 5 | my ( $class, $session ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 5 | my $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 5 | my $offset = $session->{args}->{utimeOffset}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 5 | ▶ | die "utimeOffset must be a number of seconds\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $offset and length $offset and not $offset =~ /^[0-9]{1,10}\z/
defined $offset and length $offset
MC/DC0%0/3
defined $offset and length $offset and not $offset =~ /^[0-9]{1,10}\z/
defined $offset
length $offset
not $offset =~ /^[0-9]{1,10}\z/
Truth table25%1/4
A and B and not(C)
A= defined $offsetB= length $offsetC= not $offset =~ /^[0-9]{1,10}\z/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | if ( defined $offset | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | and length $offset | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | and $offset !~ /^[0-9]{1,10}\z/ ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | my $args = $session->{args}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | $self->{cache} = $class->_getRedis($args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | _checkHexpire( $self->{cache}, $args ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | if ( hashIndex($args) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | and ( ref( $args->{Index} ) ? @{ $args->{Index} } : $args->{Index} ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
hashIndex($args) and ref $args->{"Index"} ? @{$args->{"Index"};} : $args->{"Index"}
MC/DC0%0/2
hashIndex($args) and ref $args->{"Index"} ? @{$args->{"Index"};} : $args->{"Index"}
hashIndex($args)
ref $args->{"Index"} ? @{$args->{"Index"};} : $args->{"Index"}
Truth table0%0/3
A and B
A= hashIndex($args)B= ref $args->{"Index"} ? @{$args->{"Index"};} : $args->{"Index"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | # Result of the HEXPIRE probe per server: 1 if supported, otherwise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | # [ time of the next probe (undef: never), error to raise ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 10 | our %hexpire; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | # Seconds before probing again a read-only replica | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 10 | our $READONLY_RETRY = 60; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | # Fail at tie time: Apache::Session updates the session at untie, where an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | # error is only a warning | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 17 | ▶ | sub _checkHexpire { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_checkHexpire: called CC=12 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 17 | my ( $redisObj, $args ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | my $server = join ' ', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 17 | ▶ | map { ref($_) ? @$_ : $_ // '' } @$args{qw(server sock service sentinels)}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$_ // ""
MC/DC100%1/1
$_ // ""
$_
Truth table100%2/2
A
A= $_
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 17 | ▶ | if ( my $known = $hexpire{$server} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 9 | ▶ | return unless ( ref $known ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 7 | my ( $retry, $die ) = @$known; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 7 | ▶ | if ( !defined $retry or time < $retry ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $retry or time < $retry
MC/DC100%2/2
not defined $retry or time < $retry
not defined $retry
time < $retry
Truth table100%3/3
not(A) or B
A= not defined $retryB= time < $retry
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 6 | ▶ | die $die if ($die); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 5 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | # No index hash has this name. Errors other than a missing command (ACL, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | # read-only replica...) are left to the commands that will fail. So that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | # each tie doesn't pay a failed round trip, a missing command and NOPERM | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | # are remembered until the process ends, READONLY for $READONLY_RETRY | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | # seconds only (a Sentinel failover may promote the replica). Other errors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | # may be transient | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 9 | ▶ | unless ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | eval { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 9 | $redisObj->hexpire( $HASH_INDEX_PREFIX, 1, 'FIELDS', 1, 'x' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 6 | ( my $err = $@ ) =~ s/\n.*//s; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 6 | ▶ | if ( $err =~ /unknown command/i ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 1 | my $die = "utimeOffset needs Redis >= 7.4 or Valkey >= 9: $err\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 1 | $hexpire{$server} = [ undef, $die ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 1 | die $die; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 5 | ▶ | if ( $err =~ /\bNOPERM\b/ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 1 | $hexpire{$server} = [undef]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | elsif ( $err =~ /\bREADONLY\b/ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 1 | $hexpire{$server} = [ time + $READONLY_RETRY ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 3 | delete $hexpire{$server}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 5 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 3 | $hexpire{$server} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | # Expiration of a session in seconds, see "OPTIONS" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 130 | ▶ | sub _ttl { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_ttl: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 130 | my ($session) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 130 | my $args = $session->{args}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 130 | my $data = $session->{data}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | # Persistent sessions keep the _utime of their creation and never expire | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 130 | ▶ | if ( hashIndex($args) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%11/11
hashIndex($args) and defined $data->{"_utime"} and $data->{"_utime"} =~ /^[0-9]+(?:\.[0-9]+)?\z/ and ($data->{"_session_kind"} // "") ne "Persistent"
hashIndex($args) and defined $data->{"_utime"} and $data->{"_utime"} =~ /^[0-9]+(?:\.[0-9]+)?\z/
hashIndex($args) and defined $data->{"_utime"}
$data->{"_session_kind"} // ""
MC/DC100%4/4
hashIndex($args) and defined $data->{"_utime"} and $data->{"_utime"} =~ /^[0-9]+(?:\.[0-9]+)?\z/ and ($data->{"_session_kind"} // "") ne "Persistent"
hashIndex($args)
defined $data->{"_utime"}
$data->{"_utime"} =~ /^[0-9]+(?:\.[0-9]+)?\z/
$data->{"_session_kind"}
Truth table100%5/5
A and B and C and D
A= hashIndex($args)B= defined $data->{"_utime"}C= $data->{"_utime"} =~ /^[0-9]+(?:\.[0-9]+)?\z/D= $data->{"_session_kind"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | and defined $data->{_utime} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | and $data->{_utime} =~ /^[0-9]+(?:\.[0-9]+)?\z/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | and ( $data->{_session_kind} // '' ) ne 'Persistent' ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 47 | my $ttl = int( $data->{_utime} + $args->{utimeOffset} - time ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | # An expired session is still written: it may be read again before | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | # the end of the request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 47 | ▶ | return $ttl > 0 ? $ttl : 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 83 | return $args->{TTL}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 18 | ▶ | sub _set { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 18 | my ( $self, $session, @opts ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 18 | return $self->{cache}->set( _setArgs( $session, @opts ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 36 | ▶ | sub _setArgs { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_setArgs: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 36 | my ( $session, @opts ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 36 | my $ttl = _ttl($session); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | $session->{data}->{_session_id}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 36 | ▶ | $session->{serialized}, ( $ttl ? ( 'EX', $ttl ) : () ), @opts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | # Sends the commands [ $command, $fatal, @args ] in one round trip. Dies with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | # the error of the first $fatal command that failed; the errors of the others | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | # are ignored (key of another type) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 35 | ▶ | sub _pipeline { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pipeline: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 35 | my ( $self, @cmds ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 35 | my $r = $self->{cache}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 35 | my $err; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 35 | my $sent = eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 35 | foreach (@cmds) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 67 | my ( $cmd, $fatal, @args ) = @$_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | $r->$cmd( @args, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 59 | ▶ | sub { $err //= "[$cmd] $_[1]" if ( $fatal and defined $_[1] ) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
Condition100%5/5
$fatal and defined $_[1]
$err //= "[$cmd] $_[1]"
MC/DC100%2/2
$fatal and defined $_[1]
$fatal
defined $_[1]
MC/DC100%1/1
$err //= "[$cmd] $_[1]"
$err
Truth table100%3/3
A and B
A= $fatalB= defined $_[1]
Truth table100%2/2
A
A= $err
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 67 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 27 | $r->wait_all_responses; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 27 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | # Replies left unread would be taken for those of the next commands | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 35 | ▶ | unless ($sent) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 8 | my $e = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 8 | eval { $r->wait_all_responses }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 8 | die $e; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 27 | ▶ | die "$err\n" if ( defined $err ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 24 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | # XX: another process may have deleted the session (logout, revocation, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | # purge) or Redis may have expired it since it was read; a plain SET would | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | # write it back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 18 | ▶ | sub _overwrite { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_overwrite: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 18 | my ( $self, $session ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 18 | ▶ | return 1 if ( defined $self->_set( $session, 'XX' ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 5 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | # With utimeOffset, the session and its index entries are written in one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | # transaction. The entries of a session that expires are fields of an index | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | # hash that expire with it (HEXPIRE: Redis >= 7.4, Valkey >= 9; otherwise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | # Redis refuses the whole transaction); the others stay in index sets. $xx: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | # overwrite an existing session only. Returns false if it doesn't exist, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | # undef if EXEC was aborted by a WATCH of the caller | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 42 | ▶ | sub _writeHashIndex { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_writeHashIndex: called CC=15 SCAR=27.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 42 | my ( $self, $session, $index, $old, $xx ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 42 | my $r = $self->{cache}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 42 | my $ttl = _ttl($session); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 42 | my ( @cmds, $queueErr, $reply, $execErr ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | # $fatal: an error of this command fails the write, like outside a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | # transaction. Other errors are ignored (key of another type) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 196 | ▶ | my $queue = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 196 | my ( $cmd, $fatal, @args ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 196 | push @cmds, [ $cmd, $fatal, @args ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | $r->$cmd( @args, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 196 | ▶ | sub { $queueErr //= "[$cmd] $_[1]" if ( defined $_[1] ) } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
Condition50%1/2
$queueErr //= "[$cmd] $_[1]"
MC/DC0%0/1
$queueErr //= "[$cmd] $_[1]"
$queueErr
Truth table50%1/2
A
A= $queueErr
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 42 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 42 | my $sent = eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | $r->multi( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 42 | ▶ | sub { $queueErr //= "[multi] $_[1]" if ( defined $_[1] ) } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
Condition50%1/2
$queueErr //= "[multi] $_[1]"
MC/DC0%0/1
$queueErr //= "[multi] $_[1]"
$queueErr
Truth table50%1/2
A
A= $queueErr
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 42 | $self->_queueHashIndex( $queue, $session, $index, $old, $ttl, $xx ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 40 | ▶ | $r->exec( sub { ( $reply, $execErr ) = @_ } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 40 | $r->wait_all_responses; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 40 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | # A connection left in a transaction would queue the next commands | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 42 | ▶ | unless ($sent) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 2 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 2 | eval { $r->wait_all_responses }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 2 | eval { $r->discard }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 2 | die $err; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 40 | ▶ | if ( my $err = $queueErr // $execErr ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$queueErr // $execErr
MC/DC50%1/2
$queueErr // $execErr
$queueErr
$execErr
Truth table66%2/3
A // B
A= $queueErrB= $execErr
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 6 | ▶ | die "utimeOffset needs Redis >= 7.4 or Valkey >= 9: $err\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | if ( $err =~ /unknown command/i ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 4 | die "$err\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 34 | ▶ | return undef unless ( defined $reply ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | die "[exec] unexpected reply\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | unless ( ref($reply) eq 'ARRAY' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | and @$reply == @cmds | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 27 | ▶ | and !grep { ref($_) ne 'ARRAY' } @$reply ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
ref $reply eq "ARRAY" and @$reply == @cmds and not grep({ref $_ ne "ARRAY";} @$reply)
ref $reply eq "ARRAY" and @$reply == @cmds
MC/DC100%3/3
ref $reply eq "ARRAY" and @$reply == @cmds and not grep({ref $_ ne "ARRAY";} @$reply)
ref $reply eq "ARRAY"
@$reply == @cmds
not grep({ref $_ ne "ARRAY";} @$reply)
Truth table100%4/4
A and B and not(C)
A= ref $reply eq "ARRAY"B= @$reply == @cmdsC= not grep({ref $_ ne "ARRAY";} @$reply)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 19 | foreach ( 0 .. $#cmds ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 86 | my $err = $reply->[$_]->[1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 86 | ▶ | die "[$cmds[$_]->[0]] $err\n" if ( defined $err and $cmds[$_]->[1] ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $err and $cmds[$_][1]
MC/DC100%2/2
defined $err and $cmds[$_][1]
defined $err
$cmds[$_][1]
Truth table100%3/3
A and B
A= defined $errB= $cmds[$_][1]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 17 | ▶ | return 1 if ( defined $reply->[0]->[0] ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | # SET XX refused: the entries added in the transaction are orphans | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 1 | foreach ( grep { $_->[0] =~ /^(?:hset|sadd)\z/ } @cmds ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 1 | my ( $cmd, undef, $name, $id ) = @$_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 1 | eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 1 | ▶ | $cmd eq 'hset' ? $r->hdel( $name, $id ) : $r->srem( $name, $id ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 42 | ▶ | sub _queueHashIndex { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_queueHashIndex: called CC=12 SCAR=24.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 42 | my ( $self, $queue, $session, $index, $old, $ttl, $xx ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 42 | my $data = $session->{data}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 42 | my $id = $data->{_session_id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | $queue->( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 'set', 1, $id, $session->{serialized}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 42 | ▶ | ( $ttl ? ( 'EX', $ttl ) : () ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | ( $xx ? 'XX' : () ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 42 | foreach my $i (@$index) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 42 | ▶ | my $old_val = $old ? $old->{$i} : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 42 | my $new_val = $data->{$i}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 42 | ▶ | my $add = ( defined($new_val) and length($new_val) > 0 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
defined $new_val && length $new_val > 0
MC/DC50%1/2
defined $new_val && length $new_val > 0
defined $new_val
length $new_val > 0
Truth table66%2/3
A && B
A= defined $new_valB= length $new_val > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 42 | ▶ | if ( defined($old_val) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition88%8/9
defined $old_val and length $old_val > 0 and !$add || $old_val ne $new_val
defined $old_val and length $old_val > 0
not $add or $old_val ne $new_val
MC/DC75%3/4
defined $old_val and length $old_val > 0 and !$add || $old_val ne $new_val
defined $old_val
length $old_val > 0
not $add
$old_val ne $new_val
Truth table80%4/5
A and B and not(C) or D
A= defined $old_valB= length $old_val > 0C= not $addD= $old_val ne $new_val
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | and length($old_val) > 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | and ( !$add or $old_val ne $new_val ) ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 17 | $queue->( 'srem', 0, indexName( $i, $old_val ), $id ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 17 | $queue->( 'hdel', 0, hashIndexName( $i, $old_val ), $id ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 42 | ▶ | next unless ($add); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 41 | my $set = indexName( $i, $new_val ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 41 | my $hash = hashIndexName( $i, $new_val ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 41 | ▶ | if ($ttl) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | # The set holds the sessions stored before utimeOffset was set | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 40 | $queue->( 'srem', 0, $set, $id ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 40 | $queue->( 'hset', 1, $hash, $id, 1 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 38 | $queue->( 'hexpire', 1, $hash, $ttl, 'FIELDS', 1, $id ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 1 | $queue->( 'hdel', 0, $hash, $id ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 1 | $queue->( 'sadd', 1, $set, $id ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | # Stores of this process holding a new session not written yet (weak | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | # references), written by the END block if their session is still tied when | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | # the program ends: during the global destruction, the Redis client may be | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | # freed before the store | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 10 | my %PENDING; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 10 | ▶ | END { _flushPending() } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
END: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | # A new session is written once, by update() at untie or, if it is untied | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | # unmodified, when the store is freed (see "DESCRIPTION"). Neither the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | # session nor its arguments are kept: Lemonldap::NG's cache wrapper keeps | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | # this store in the arguments (object_store), a cycle that would keep both | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | # until the end of the program | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 29 | ▶ | sub insert { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
insert: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 29 | my ( $self, $session ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 29 | my $args = $session->{args}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | my $index = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 29 | ▶ | ref( $args->{Index} ) ? $args->{Index} : [ split /\s+/, $args->{Index} ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | $self->{pending} = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | data => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 67 | map { ( $_ => $session->{data}->{$_} ) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 117 | grep { exists $session->{data}->{$_} } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | qw(_session_id _utime _session_kind), @$index | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | serialized => $session->{serialized}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | # The arguments open a new connection if the reused one is broken | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | # (see $CONNECTION_ERROR); objects are left out | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | args => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 47 | map { ( $_ => $args->{$_} ) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 29 | grep { !Scalar::Util::blessed( $args->{$_} ) } keys %$args | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | Index => [@$index], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | pid => $$, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 29 | Scalar::Util::weaken( $PENDING{ Scalar::Util::refaddr($self) } = $self ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 29 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 11 | ▶ | sub _flushPending { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_flushPending: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 11 | foreach my $self ( grep { defined } values %PENDING ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 1 | ▶ | my $pending = $self->_forgetPending or next; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 1 | $self->_writePending($pending); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 11 | %PENDING = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 113 | ▶ | sub _forgetPending { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_forgetPending: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 113 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 113 | delete $PENDING{ Scalar::Util::refaddr($self) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 113 | return delete $self->{pending}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | # A failure is reported as a warning, like a failed update at untie. A child | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | # created by fork() leaves the sessions of its parent to it: the connection | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | # is the parent's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 14 | ▶ | sub _writePending { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_writePending: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 14 | my ( $self, $pending ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | 14 | ▶ | return if ( $pending->{pid} != $$ ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | 13 | local $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 13 | ▶ | return if ( eval { $self->_retried( $pending, \&_write ); 1 } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 10 | warn "Web::Session: unable to store new session " | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | . "$pending->{data}->{_session_id}: $@"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | 91 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 91 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 91 | ▶ | my $pending = $self->_forgetPending or return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 13 | ▶ | $self->_writePending($pending) unless ( ${^GLOBAL_PHASE} eq 'DESTRUCT' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | # Writes a new session | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 39 | ▶ | sub _write { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_write: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 39 | my ( $self, $session ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | my $index = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | ref( $session->{args}->{Index} ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | ? $session->{args}->{Index} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 39 | ▶ | : [ split /\s+/, $session->{args}->{Index} ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 39 | ▶ | if ( @$index and hashIndex( $session->{args} ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@$index and hashIndex($session->{"args"})
MC/DC100%2/2
@$index and hashIndex($session->{"args"})
@$index
hashIndex($session->{"args"})
Truth table100%3/3
A and B
A= @$indexB= hashIndex($session->{"args"})
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | 21 | ▶ | $self->_writeHashIndex( $session, $index ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->_writeHashIndex($session, $index) // die("[exec] unexpected reply\n")
MC/DC0%0/1
$self->_writeHashIndex($session, $index) // die("[exec] unexpected reply\n")
$self->_writeHashIndex($session, $index)
Truth table50%1/2
A
A= $self->_writeHashIndex($session, $index)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | // die "[exec] unexpected reply\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | 18 | my $id = $session->{data}->{_session_id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | 18 | my @cmds = ( [ 'set', 1, _setArgs($session) ] ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | 18 | foreach my $i (@$index) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | 20 | my $t = $session->{data}->{$i}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | 20 | ▶ | next unless ( defined($t) and ( length($t) > 0 ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $t and length $t > 0
MC/DC50%1/2
defined $t and length $t > 0
defined $t
length $t > 0
Truth table66%2/3
A and B
A= defined $tB= length $t > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | 17 | push @cmds, [ 'sadd', 1, indexName( $i, $t ), $id ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | 18 | $self->_pipeline(@cmds); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | 15 | $session->{ +READ } = $session->{serialized}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | 15 | $session->{ +STATUS } = 'written'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | 15 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | # Dies if the session was deleted meanwhile: a wrapper that caches it (such | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | # as the local cache of Lemonldap::NG) must not keep it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | 78 | ▶ | sub _updateSession { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_updateSession: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 78 | my ( $self, $session ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | 78 | delete $session->{ +STATUS }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | 78 | ▶ | if ( my $pending = $self->{pending} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | # Written under the id the session had at tie: Lemonldap::NG's hashed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | # session store changes _session_id while the session is tied and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | # puts it back before untie | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 25 | my $id = $pending->{data}->{_session_id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | # If the write fails, the store tries it again when it is freed: with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | # the data of this save, not those of the tie (an empty session that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | # would replace what the failed write may have stored, as a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | # transaction is not rolled back when one of its commands fails) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | 25 | $pending->{serialized} = $session->{serialized}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | $pending->{data} = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 41 | map { ( $_ => $session->{data}->{$_} ) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | 77 | grep { exists $session->{data}->{$_} } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | 25 | qw(_utime _session_kind), @{ $pending->{args}->{Index} } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | _session_id => $id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | 25 | $pending->{tried} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | 25 | local $session->{data} = { %{ $session->{data} }, _session_id => $id }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | 25 | $self->_write($session); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | # Forgotten once written only: after a failure, a new save() writes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | # the session again, without version check | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | 12 | $self->_forgetPending; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | 12 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | 53 | $session->{ +STATUS } = $self->_update($session); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | die "Session '$session->{data}->{_session_id}' was deleted or expired, " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | . "not stored again\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | 45 | ▶ | if ( $session->{ +STATUS } eq 'deleted' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | 34 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | # Returns the status of the update, see "DESCRIPTION" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | 53 | ▶ | sub _update { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_update: called CC=14 SCAR=26.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | 53 | my ( $self, $session ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | my $index = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | ref( $session->{args}->{Index} ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | ? $session->{args}->{Index} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | 53 | ▶ | : [ split /\s+/, $session->{args}->{Index} ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | return $self->_updateHashIndex( $session, $index ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | 53 | ▶ | if ( @$index and hashIndex( $session->{args} ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@$index and hashIndex($session->{"args"})
MC/DC100%2/2
@$index and hashIndex($session->{"args"})
@$index
hashIndex($session->{"args"})
Truth table100%3/3
A and B
A= @$indexB= hashIndex($session->{"args"})
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | 37 | my $id = $session->{data}->{_session_id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | 37 | my $old_data; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | 37 | my $status = 'overwritten'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | 37 | ▶ | if ( defined $session->{ +READ } ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | 26 | ( $status, my $old_raw, my $indexed, my $err ) = | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | $self->_writeMerged( $session, $id, $index ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | 23 | ▶ | return $status if ( $status eq 'deleted' or !@$index ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$status eq "deleted" or not @$index
MC/DC100%2/2
$status eq "deleted" or not @$index
$status eq "deleted"
not @$index
Truth table100%3/3
A or not(B)
A= $status eq "deleted"B= not @$index
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | # Index entries replaced by the Lua script (see $CAS) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | 12 | ▶ | if ($indexed) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | 3 | ▶ | die "[sadd] $err\n" if ( defined $err ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | 2 | return $status; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | 9 | $old_data = _decodeOld( $session, $old_raw ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | # The version read is unknown (unserialize isn't the one installed by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | # Web::Session::Redis): overwrite. Nothing to clean up without index | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | 11 | ▶ | unless (@$index) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | 6 | ▶ | return $self->_overwrite($session) ? $status : 'deleted'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | # Read old data to clean up stale index entries | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | 5 | my $old_raw = eval { $self->{cache}->get($id) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | 5 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | 0 | warn "Failed to read previous session '$id' from Redis: $@"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | 5 | $old_data = _decodeOld( $session, $old_raw ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | 5 | ▶ | return 'deleted' unless ( $self->_overwrite($session) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | 12 | my @cmds = _indexCmds( $index, $old_data, $session->{data}, $id ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | 12 | ▶ | $self->_pipeline(@cmds) if (@cmds); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | 12 | return $status; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | # Commands [ srem|sadd, fatal, set, $id ] that replace the index entries of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | # $old_data (the version replaced) by those of $data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | 43 | ▶ | sub _indexCmds { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_indexCmds: called CC=10 SCAR=23.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | 43 | my ( $index, $old_data, $data, $id ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | 43 | my @cmds; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | 43 | foreach my $i (@$index) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | 44 | ▶ | my $old_val = defined($old_data) ? $old_data->{$i} : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | 44 | my $new_val = $data->{$i}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | # Remove old index entry if value changed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | 44 | ▶ | if ( defined($old_val) && length($old_val) > 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $old_val and length $old_val > 0
MC/DC50%1/2
defined $old_val and length $old_val > 0
defined $old_val
length $old_val > 0
Truth table66%2/3
A and B
A= defined $old_valB= length $old_val > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | 32 | ▶ | if ( !defined($new_val) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
not defined $new_val or length $new_val == 0 or $old_val ne $new_val
not defined $new_val or length $new_val == 0
MC/DC33%1/3
not defined $new_val or length $new_val == 0 or $old_val ne $new_val
not defined $new_val
length $new_val == 0
$old_val ne $new_val
Truth table50%2/4
not(A) or B or C
A= not defined $new_valB= length $new_val == 0C= $old_val ne $new_val
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | || length($new_val) == 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | || $old_val ne $new_val ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | 21 | push @cmds, [ 'srem', 0, indexName( $i, $old_val ), $id ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | # Add new index entry | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | 44 | ▶ | if ( defined($new_val) && length($new_val) > 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $new_val and length $new_val > 0
MC/DC50%1/2
defined $new_val and length $new_val > 0
defined $new_val
length $new_val > 0
Truth table66%2/3
A and B
A= defined $new_valB= length $new_val > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | 34 | push @cmds, [ 'sadd', 1, indexName( $i, $new_val ), $id ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | 43 | return @cmds; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | # Decoded like the session itself: Redis returns Latin-1, not UTF-8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | 96 | ▶ | sub _decode { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_decode: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | 96 | my ( $session, $raw ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | 96 | my $tmp = { serialized => $raw }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | 96 | $session->{unserialize}->($tmp); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | 96 | return $tmp->{data}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | # Previous version, whose index entries are replaced | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | 66 | ▶ | sub _decodeOld { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_decodeOld: called CC=4 SCAR=14.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | 66 | my ( $session, $raw ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 556 | 66 | ▶ | return undef unless ( defined $raw and length $raw ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $raw and length $raw
MC/DC50%1/2
defined $raw and length $raw
defined $raw
length $raw
Truth table66%2/3
A and B
A= defined $rawB= length $raw
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 557 | 64 | my $data = eval { _decode( $session, $raw ) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 558 | 64 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | 0 | my $id = $session->{data}->{_session_id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | 0 | warn "Failed to decode previous session '$id': $@"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 561 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 562 | 64 | return $data; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 563 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 565 | # Sessions aren't locked (see "DESCRIPTION"): a write is accepted only if Redis | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 566 | # still holds the version this process read, otherwise the caller's changes are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 567 | # merged into the stored version and the write is tried again. The index | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 568 | # entries of the version replaced are replaced by the Lua script (see $CAS). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | # Returns the status of the update, the version replaced and, if the script | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | # replaced the index entries, a true value and the error of SADD (or undef) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | 26 | ▶ | sub _writeMerged { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_writeMerged: called CC=12 SCAR=24.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | 26 | my ( $self, $session, $id, $index ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | 26 | my $expected = $session->{ +READ }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | 26 | my $merge = _merger( $session, $id ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 575 | 26 | my ( $merged, $failed ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 576 | 26 | for ( 1 .. $MAX_TRIES ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 578 | # Index entries of the version that would be replaced | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 579 | my @cmds = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 580 | ( $index and @$index ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 581 | ? _indexCmds( $index, _decodeOld( $session, $expected ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 582 | 40 | ▶ | $session->{data}, $id ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$index and @$index
MC/DC50%1/2
$index and @$index
$index
@$index
Truth table66%2/3
A and B
A= $indexB= @$index
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 583 | : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 584 | my ( $res, $cur, $err ) = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 585 | 40 | @{ $self->_compareAndSet( $id, $expected, $session->{serialized}, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | 40 | _ttl($session), \@cmds ) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | 37 | ▶ | if ( $res eq 'written' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | 12 | $session->{ +READ } = $session->{serialized}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 589 | 12 | ▶ | return ( $merged ? 'merged' : 'written', $expected, $cur, $err ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | 25 | ▶ | return ('deleted') if ( $res eq 'deleted' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | 21 | ▶ | next if ( $res eq 'aborted' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | 20 | ▶ | if ( $res eq 'refused' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | 5 | _warnUnchecked($cur); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 595 | 5 | $failed = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 596 | 5 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 597 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 598 | 15 | $expected = $cur; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 599 | 15 | ▶ | unless ( $merge->($expected) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 600 | 0 | $failed = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 601 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 602 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 603 | 15 | $merged = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 604 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 605 | 7 | ▶ | _warnOverwrite($id) unless ($failed); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 606 | 7 | ▶ | return ('deleted') unless ( $self->_overwrite($session) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 607 | 6 | $session->{ +READ } = $session->{serialized}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 608 | 6 | return ( 'overwritten', $expected ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 609 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 611 | # Same as _writeMerged() with index hashes: the version check is part of the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 612 | # transaction of _writeHashIndex(), so that the index entries replaced are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 613 | # those of the version replaced. WATCH aborts EXEC if the session changes after | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 614 | # GET; if WATCH is refused, the transaction is run without it. Without the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | # version read, the session is overwritten. Returns the status of the update | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | 16 | ▶ | sub _updateHashIndex { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_updateHashIndex: called CC=16 SCAR=27.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 617 | 16 | my ( $self, $session, $index ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 618 | 16 | my $r = $self->{cache}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 619 | 16 | my $id = $session->{data}->{_session_id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 620 | 16 | my $expected = $session->{ +READ }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 621 | 16 | ▶ | my $status = defined($expected) ? 'written' : 'overwritten'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 622 | 16 | my $merge = _merger( $session, $id ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 623 | 16 | my ( $cur, $done, $failed ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 624 | eval { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 625 | 16 | for ( 1 .. $MAX_TRIES ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 626 | 28 | ( $cur, my $err ) = $self->_watchGet($id); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 627 | 27 | ▶ | if ( defined $err ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 628 | 2 | ▶ | die "$err\n" unless ( _refused($err) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 629 | 2 | _warnUnchecked($err); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 630 | 2 | $failed = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 631 | 2 | ▶ | $done = 0 unless ( defined $cur ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 632 | 2 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 633 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 634 | 25 | ▶ | unless ( defined $cur ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 635 | 1 | $r->unwatch; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 636 | 1 | $done = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 637 | 1 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 638 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 639 | 24 | ▶ | if ( defined $expected and $cur ne $expected ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $expected and $cur ne $expected
MC/DC100%2/2
defined $expected and $cur ne $expected
defined $expected
$cur ne $expected
Truth table100%3/3
A and B
A= defined $expectedB= $cur ne $expected
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 640 | 7 | $r->unwatch; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 641 | 7 | $expected = $cur; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 642 | 7 | $status = 'merged'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 643 | 7 | ▶ | next if ( $merge->($cur) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 644 | 0 | $failed = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 645 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 646 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 648 | # undef: the session changed after GET, read it again | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 649 | 17 | $done = $self->_writeHashIndex( $session, $index, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 650 | _decodeOld( $session, $cur ), 1 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 651 | 16 | ▶ | last if ( defined $done ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 652 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 653 | 14 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 654 | 16 | ▶ | } or do { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 655 | 2 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 656 | 2 | eval { $r->unwatch }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 657 | 2 | die $err; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 658 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 660 | # Not watched: the index entries replaced are those of the last version | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 661 | # read | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 662 | 14 | ▶ | unless ( defined $done ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 663 | 4 | ▶ | _warnOverwrite($id) unless ($failed); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 664 | 4 | $status = 'overwritten'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 665 | 4 | ▶ | $done = $self->_writeHashIndex( $session, $index, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->_writeHashIndex($session, $index, _decodeOld($session, $cur), 1) // die("[exec] unexpected reply\n")
MC/DC0%0/1
$self->_writeHashIndex($session, $index, _decodeOld($session, $cur), 1) // die("[exec] unexpected reply\n")
$self->_writeHashIndex($session, $index, _decodeOld($session, $cur), 1)
Truth table50%1/2
A
A= $self->_writeHashIndex($session, $index, _decodeOld($session, $cur), 1)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 666 | _decodeOld( $session, $cur ), 1 ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 667 | // die "[exec] unexpected reply\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 668 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 669 | 13 | ▶ | return 'deleted' unless ($done); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 670 | 11 | $session->{ +READ } = $session->{serialized}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 671 | 11 | return $status; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 672 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 674 | # WATCH and GET of session $id in one round trip: GET runs even if WATCH is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 675 | # refused. Returns the session and the error of WATCH; dies if GET fails | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 676 | 28 | ▶ | sub _watchGet { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_watchGet: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 677 | 28 | my ( $self, $id ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 678 | 28 | my $r = $self->{cache}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 679 | 28 | my ( $cur, $watchErr, $getErr ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 680 | 28 | my $sent = eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 681 | $r->watch( $id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 682 | 28 | ▶ | sub { $watchErr = "[watch] $_[1]" if ( defined $_[1] ) } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 683 | 28 | ▶ | $r->get( $id, sub { ( $cur, $getErr ) = @_ } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 684 | 27 | $r->wait_all_responses; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 685 | 27 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 686 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 687 | 28 | ▶ | unless ($sent) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 688 | 1 | my $e = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 689 | 1 | eval { $r->wait_all_responses }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 690 | 1 | die $e; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 691 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 692 | 27 | ▶ | die "[get] $getErr\n" if ( defined $getErr ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 693 | 27 | return ( $cur, $watchErr ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 694 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 696 | # Refusals that a retry won't change: ACL, command renamed or not supported by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 697 | # a proxy. Connection errors are not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 698 | 9 | ▶ | sub _refused { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_refused: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 699 | 9 | ▶ | return $_[0] =~ /\bNOPERM\b|unknown command|not (?:allowed|supported)/i | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$_[0] =~ /\bNOPERM\b|unknown command|not (?:allowed|supported)/i || $_[0] =~ /unsupported|forbid/i
MC/DC50%1/2
$_[0] =~ /\bNOPERM\b|unknown command|not (?:allowed|supported)/i || $_[0] =~ /unsupported|forbid/i
$_[0] =~ /\bNOPERM\b|unknown command|not (?:allowed|supported)/i
$_[0] =~ /unsupported|forbid/i
Truth table66%2/3
A || B
A= $_[0] =~ /\bNOPERM\b|unknown command|not (?:allowed|supported)/iB= $_[0] =~ /unsupported|forbid/i
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 700 | || $_[0] =~ /unsupported|forbid/i; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 701 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 702 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 703 | 10 | our $unchecked_warned; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 705 | 7 | ▶ | sub _warnUnchecked { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_warnUnchecked: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 706 | 7 | ▶ | return if ($unchecked_warned); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 707 | 5 | $unchecked_warned = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 708 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 709 | # First line only: Redis::Fast adds a backtrace holding the arguments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 710 | 5 | my ($err) = split /\n/, $_[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 711 | 5 | warn "Redis refused WATCH/MULTI, concurrent updates of sessions are not " | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 712 | . "merged: $err\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 713 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 715 | 4 | ▶ | sub _warnOverwrite { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_warnOverwrite: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 716 | 4 | warn "Session '$_[0]' was updated concurrently too often, overwriting it\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 717 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 719 | # Returns a function that applies to the version stored, its argument, what | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 720 | # the caller changed since the version it read, and serializes the result in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 721 | # $session. It returns false, with a warning, if they can't be merged | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 722 | 42 | ▶ | sub _merger { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_merger: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 723 | 42 | my ( $session, $id ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 724 | 42 | my ( $orig, $new ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 725 | 22 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=17.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 726 | 22 | my ($cur) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 727 | 22 | my $merged = eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 728 | 22 | ▶ | $orig //= _decode( $session, $session->{ +READ } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$orig //= _decode($session, $session->{"web_session_redis_read"})
MC/DC0%0/2
$orig //= _decode($session, $session->{"web_session_redis_read"})
$orig
_decode($session, $session->{"web_session_redis_read"})
Truth table66%2/3
A //= B
A= $origB= _decode($session, $session->{"web_session_redis_read"})
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 729 | 22 | ▶ | $new //= $session->{data}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$new //= $session->{"data"}
MC/DC0%0/2
$new //= $session->{"data"}
$new
$session->{"data"}
Truth table66%2/3
A //= B
A= $newB= $session->{"data"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 730 | 22 | _merge( $orig, _decode( $session, $cur ), $new, $id ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 731 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 732 | 22 | ▶ | unless ( ref($merged) eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 733 | 0 | ▶ | warn "Unable to merge session '$id' with a concurrent update, " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$@ || "not a session\n"
MC/DC0%0/1
$@ || "not a session\n"
$@
Truth table0%0/2
A
A= $@
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 734 | . "overwriting it: " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 735 | . ( $@ || "not a session\n" ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 736 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 737 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 738 | 22 | $session->{data} = $merged; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 739 | 22 | $session->{serialize}->($session); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 740 | 22 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 741 | 42 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 742 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 743 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 744 | # Three-way merge of top-level keys: what the caller changed or deleted since | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 745 | # $orig is applied to $cur | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 746 | 32 | ▶ | sub _merge { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_merge: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 747 | 32 | my ( $orig, $cur, $new, $id ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 748 | 32 | my %res = %$cur; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 749 | 32 | foreach my $k ( keys %$orig ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 750 | 123 | ▶ | delete $res{$k} unless ( exists $new->{$k} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 751 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 752 | 32 | foreach my $k ( keys %$new ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 753 | $res{$k} = $new->{$k} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 754 | 125 | ▶ | unless ( exists $orig->{$k} and _same( $orig->{$k}, $new->{$k} ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $orig->{$k} and _same($orig->{$k}, $new->{$k})
MC/DC100%2/2
exists $orig->{$k} and _same($orig->{$k}, $new->{$k})
exists $orig->{$k}
_same($orig->{$k}, $new->{$k})
Truth table100%3/3
A and B
A= exists $orig->{$k}B= _same($orig->{$k}, $new->{$k})
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 755 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 756 | 32 | $res{_session_id} = $id; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 757 | 32 | return \%res; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 758 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 759 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 760 | 10 | my $cmpJson = JSON->new->canonical->allow_nonref; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 761 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 762 | # A number used as a string is still the same value: scalars are compared | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 763 | # with eq, references by their canonical JSON | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 764 | 122 | ▶ | sub _same { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_same: called CC=8 SCAR=22.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 765 | 122 | my ( $x, $y ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 766 | 122 | ▶ | return !defined($y) unless ( defined $x ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 767 | 122 | ▶ | return 0 unless ( defined $y ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 768 | 122 | ▶ | return $x eq $y unless ( ref($x) or ref($y) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $x or ref $y
MC/DC50%1/2
ref $x or ref $y
ref $x
ref $y
Truth table66%2/3
A or B
A= ref $xB= ref $y
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 769 | 10 | ▶ | return 0 unless ( ref($x) and ref($y) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $x and ref $y
MC/DC0%0/2
ref $x and ref $y
ref $x
ref $y
Truth table33%1/3
A and B
A= ref $xB= ref $y
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 770 | 10 | ▶ | return eval { $cmpJson->encode($x) eq $cmpJson->encode($y) } ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 771 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 772 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 773 | # KEYS[1]: session, then the index sets to leave, then those to join. ARGV: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 774 | # version read, new version, TTL or '', number of sets to leave. Errors of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 775 | # SREM are ignored; the first error of SADD is returned, the session being | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 776 | # written, as outside the script (see "DESCRIPTION" for proxies) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 777 | 10 | our $CAS = q{local cur = redis.call('get', KEYS[1]) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 778 | if not cur then return {'deleted'} end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 779 | if cur ~= ARGV[1] then return {'conflict', cur} end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 780 | if ARGV[3] == '' then redis.call('set', KEYS[1], ARGV[2], 'XX') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 781 | else redis.call('set', KEYS[1], ARGV[2], 'EX', ARGV[3], 'XX') end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 782 | local nrem = tonumber(ARGV[4]) or 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 783 | local err | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 784 | for i = 2, #KEYS do | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 785 | if i <= nrem + 1 then redis.pcall('srem', KEYS[i], KEYS[1]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 786 | else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 787 | local r = redis.pcall('sadd', KEYS[i], KEYS[1]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 788 | if type(r) == 'table' and r.err and not err then err = r.err end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 789 | end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 790 | end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 791 | return {'written', 1, err}}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 792 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 793 | 10 | our $lua_warned; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 794 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 795 | # Write $serialized if $id still holds $expected, and replace its index | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 796 | # entries with the commands @$cmds of _indexCmds(). Returns ['written', 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 797 | # error of SADD or undef], ['written'] if the index entries were not replaced | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 798 | # (without Lua), ['deleted'], ['conflict', current version], ['aborted'] or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 799 | # ['refused', error]. Without Lua (EVAL forbidden by ACL, proxy...), WATCH the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 800 | # session so that a concurrent write aborts EXEC | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 801 | 40 | ▶ | sub _compareAndSet { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_compareAndSet: called CC=17 SCAR=28.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 802 | 40 | my ( $self, $id, $expected, $serialized, $ttl, $cmds ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 803 | 40 | my $redisObj = $self->{cache}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 804 | 40 | ▶ | my @rem = map { $_->[2] } grep { $_->[0] eq 'srem' } @{ $cmds || [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$cmds || []
MC/DC0%0/1
$cmds || []
$cmds
Truth table50%1/2
A
A= $cmds
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 805 | 40 | ▶ | my @add = map { $_->[2] } grep { $_->[0] eq 'sadd' } @{ $cmds || [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$cmds || []
MC/DC0%0/1
$cmds || []
$cmds
Truth table50%1/2
A
A= $cmds
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 806 | 40 | my $res = eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 807 | 40 | ▶ | $redisObj->eval( $CAS, 1 + @rem + @add, $id, @rem, @add, $expected, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 808 | $serialized, ( $ttl ? $ttl : '' ), scalar(@rem) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 809 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 810 | 40 | ▶ | return $res if ( ref($res) eq 'ARRAY' and !$@ ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $res eq "ARRAY" and not $@
MC/DC50%1/2
ref $res eq "ARRAY" and not $@
ref $res eq "ARRAY"
not $@
Truth table66%2/3
A and not(B)
A= ref $res eq "ARRAY"B= not $@
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 811 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 812 | # Lost connection or failover: not a refusal of EVAL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 813 | 14 | ▶ | die $@ if ( $@ =~ $CONNECTION_ERROR ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 814 | 13 | ▶ | unless ($lua_warned) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 815 | 10 | $lua_warned = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 816 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 817 | # First line only: Redis::Fast adds a backtrace holding the arguments, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 818 | # that is the session id and data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 819 | 10 | ▶ | my ($err) = split /\n/, ( $@ || 'unexpected reply' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$@ || 'unexpected reply'
MC/DC0%0/1
$@ || 'unexpected reply'
$@
Truth table50%1/2
A
A= $@
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 820 | 10 | print STDERR "Redis EVAL failed, sessions are updated without Lua: " | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 821 | . "$err\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 822 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 823 | eval { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 824 | 13 | $redisObj->watch($id); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 825 | 8 | my $cur = $redisObj->get($id); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 826 | 7 | ▶ | if ( !defined $cur or $cur ne $expected ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $cur or $cur ne $expected
MC/DC100%2/2
not defined $cur or $cur ne $expected
not defined $cur
$cur ne $expected
Truth table100%3/3
not(A) or B
A= not defined $curB= $cur ne $expected
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 827 | 2 | $redisObj->unwatch; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 828 | 2 | ▶ | $res = defined($cur) ? [ 'conflict', $cur ] : ['deleted']; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 829 | 2 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 830 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 831 | 5 | $redisObj->multi; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 832 | 4 | ▶ | $ttl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 833 | ? $redisObj->set( $id, $serialized, 'EX', $ttl, 'XX' ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 834 | : $redisObj->set( $id, $serialized, 'XX' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 835 | 4 | my $done = $redisObj->exec; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 836 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 837 | # undef: the session changed after GET | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 838 | 4 | ▶ | $res = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 839 | !$done ? ['aborted'] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 840 | : defined( $done->[0] ) ? ['written'] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 841 | : ['deleted']; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 842 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 843 | 13 | ▶ | } or do { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 844 | 7 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 845 | 7 | eval { $redisObj->discard }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 846 | 7 | eval { $redisObj->unwatch }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 847 | 7 | ▶ | return [ 'refused', $err ] if ( _refused($err) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 848 | 2 | die $err; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 849 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 850 | 6 | return $res; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 851 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 852 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 853 | 6 | ▶ | sub _read { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_read: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 854 | 6 | my ( $self, $session ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 855 | $session->{serialized} = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 856 | $self->{cache}->get( $session->{data}->{_session_id} ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 857 | 6 | ▶ | or die 'Object does not exist in data store.'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 858 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 859 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 860 | 8 | ▶ | sub _removeSession { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_removeSession: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 861 | 8 | my ( $self, $session ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 862 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 863 | # Created and removed in the same tie: nothing was written, so nothing is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 864 | # deleted, unless a write was tried | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 865 | 8 | my $pending = $self->_forgetPending; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 866 | 8 | ▶ | return 1 if ( $pending and !$pending->{tried} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$pending and not $pending->{"tried"}
MC/DC100%2/2
$pending and not $pending->{"tried"}
$pending
not $pending->{"tried"}
Truth table100%3/3
A and not(B)
A= $pendingB= not $pending->{"tried"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 867 | my $index = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 868 | ref( $session->{args}->{Index} ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 869 | ? $session->{args}->{Index} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 870 | 7 | ▶ | : [ split /\s+/, $session->{args}->{Index} ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 871 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 872 | 7 | my $id = $session->{data}->{_session_id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 873 | 7 | my $hash = hashIndex( $session->{args} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 874 | 7 | my @cmds; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 875 | 7 | foreach my $i (@$index) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 876 | 9 | my $t; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 877 | 9 | ▶ | next unless ( $t = $session->{data}->{$i} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 878 | 9 | push @cmds, [ 'srem', 0, indexName( $i, $t ), $id ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 879 | 9 | ▶ | push @cmds, [ 'hdel', 0, hashIndexName( $i, $t ), $id ] if ($hash); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 880 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 881 | 7 | $self->_pipeline( @cmds, [ 'del', 1, $id ] ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 882 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 883 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 884 | # Errors of a reused connection, see $CONNECTION_ERROR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 885 | 4 | ▶ | sub materialize { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
materialize: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 886 | 4 | my ( $self, $session ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 887 | 4 | return $self->_retried( $session, \&_read ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 888 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 889 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 890 | # The write of a new session is tried again at once on a new connection: it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 891 | # writes the same data under the same id. An update is not (see | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 892 | # $CONNECTION_ERROR) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 893 | 77 | ▶ | sub update { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 894 | 77 | my ( $self, $session ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 895 | return $self->_retried( $session, \&_updateSession ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 896 | 77 | ▶ | if ( $self->{pending} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 897 | 53 | return $self->_onConnection( $session, \&_updateSession ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 898 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 899 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 900 | # Runs $code; on a connection error, runs it again once on a new connection | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 901 | 41 | ▶ | sub _retried { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_retried: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 902 | 41 | my ( $self, $session, $code ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 903 | 41 | my $res; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 904 | 41 | ▶ | return $res if ( eval { $res = $self->$code($session); 1 } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 905 | 28 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 906 | 28 | ▶ | die $err unless ( $self->_forgetBroken( $session->{args}, $err ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 907 | 4 | $self->{cache} = ref($self)->_getRedis( $session->{args} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 908 | 4 | return $self->_onConnection( $session, $code ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 909 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 910 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 911 | 8 | ▶ | sub remove { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
remove: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 912 | 8 | my ( $self, $session ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 913 | 8 | return $self->_onConnection( $session, \&_removeSession ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 914 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 915 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 916 | 65 | ▶ | sub _onConnection { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_onConnection: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 917 | 65 | my ( $self, $session, $code ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 918 | 65 | my $res; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 919 | 65 | ▶ | return $res if ( eval { $res = $self->$code($session); 1 } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 920 | 22 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 921 | 22 | $self->_forgetBroken( $session->{args}, $err ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 922 | 22 | die $err; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 923 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 924 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 925 | # Forgets the reused connection of this store if $err is a connection error. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 926 | # Returns true then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 927 | 50 | ▶ | sub _forgetBroken { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_forgetBroken: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 928 | 50 | my ( $self, $args, $err ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 929 | 50 | ▶ | my $key = $args->{reuse} or return 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 930 | 9 | ▶ | return 0 unless ( $err =~ $CONNECTION_ERROR ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 931 | 8 | ▶ | my $cached = $reused_connections{$key} or return 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 932 | return 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 933 | unless ( Scalar::Util::refaddr($cached) == | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 934 | 8 | ▶ | Scalar::Util::refaddr( $self->{cache} ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 935 | 7 | delete $reused_connections{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 936 | 7 | delete $selected{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 937 | 7 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 938 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 939 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 940 | 10 | ▶ | sub _getRedis { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_getRedis: called CC=16 SCAR=29.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 941 | 10 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 942 | 10 | my $args = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 943 | 10 | my $redisObj; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 944 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 945 | # Manage undef encoding | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 946 | $args->{encoding} = undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 947 | if ( $args->{encoding} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 948 | 10 | ▶ | and $args->{encoding} eq "undef" ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$args->{"encoding"} and $args->{"encoding"} eq "undef"
MC/DC0%0/2
$args->{"encoding"} and $args->{"encoding"} eq "undef"
$args->{"encoding"}
$args->{"encoding"} eq "undef"
Truth table33%1/3
A and B
A= $args->{"encoding"}B= $args->{"encoding"} eq "undef"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 949 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 950 | # If sentinels is not given as an array ref, try to parse | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 951 | # a comma delimited list instead | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 952 | 10 | ▶ | if ( $args->{sentinels} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$args->{"sentinels"} and ref $args->{"sentinels"} ne "ARRAY"
MC/DC0%0/2
$args->{"sentinels"} and ref $args->{"sentinels"} ne "ARRAY"
$args->{"sentinels"}
ref $args->{"sentinels"} ne "ARRAY"
Truth table33%1/3
A and B
A= $args->{"sentinels"}B= ref $args->{"sentinels"} ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 953 | and ref $args->{sentinels} ne 'ARRAY' ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 954 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 955 | $args->{sentinels} = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 956 | 0 | [ split /[,\s]+/, $args->{sentinels} ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 957 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 958 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 959 | 10 | my $key = $args->{reuse}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 960 | 10 | my $db = $args->{database}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 961 | 10 | ▶ | if ( $key and my $cached = $reused_connections{$key} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$key and my $cached = $Web::Session::Store::Redis::reused_connections{$key}
MC/DC100%2/2
$key and my $cached = $Web::Session::Store::Redis::reused_connections{$key}
$key
my $cached = $Web::Session::Store::Redis::reused_connections{$key}
Truth table100%3/3
A and B
A= $keyB= my $cached = $Web::Session::Store::Redis::reused_connections{$key}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 962 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 963 | # A reused connection stays on the database it selected (Redis.pm and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 964 | # Redis::Fast select it again when they reconnect): no SELECT round | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 965 | # trip at each tie | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 966 | 7 | my $sel = $selected{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 967 | 7 | ▶ | return $cached | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%6/9
not defined $db or $sel and $sel->[0] == builtin::refaddr($cached) and $sel->[1] eq $db
$sel and $sel->[0] == builtin::refaddr($cached) and $sel->[1] eq $db
$sel and $sel->[0] == builtin::refaddr($cached)
MC/DC50%2/4
not defined $db or $sel and $sel->[0] == builtin::refaddr($cached) and $sel->[1] eq $db
not defined $db
$sel
$sel->[0] == builtin::refaddr($cached)
$sel->[1] eq $db
Truth table60%3/5
not(A) or B and C and D
A= not defined $dbB= $selC= $sel->[0] == builtin::refaddr($cached)D= $sel->[1] eq $db
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 968 | if ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 969 | !defined $db | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 970 | or ( $sel | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 971 | and $sel->[0] == Scalar::Util::refaddr($cached) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 972 | and $sel->[1] eq $db ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 973 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 974 | 3 | ▶ | if ( eval { $cached->select($db); 1 } ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 975 | 1 | $selected{$key} = [ Scalar::Util::refaddr($cached), $db ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 976 | 1 | return $cached; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 977 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 978 | 2 | ▶ | die $@ unless ( $@ =~ $CONNECTION_ERROR ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 979 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 980 | # Broken (see $CONNECTION_ERROR): replaced | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 981 | 2 | delete $reused_connections{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 982 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 983 | 5 | $redisObj = $redis->new( %{$args} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 984 | 5 | ▶ | $redisObj->select($db) if ( defined $db ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 985 | 4 | ▶ | if ($key) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 986 | 2 | $reused_connections{$key} = $redisObj; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 987 | 2 | ▶ | if ( defined $db ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 988 | 2 | $selected{$key} = [ Scalar::Util::refaddr($redisObj), $db ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 989 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 990 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 991 | 0 | delete $selected{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 992 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 993 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 994 | 4 | return $redisObj; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 995 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 996 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 997 | 10 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 998 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||