blib/lib/Redis/Cluster.pm

filter: Statementstmt 15.6% 42 / 268 Branchbran 0.0% 0 / 122 Conditioncond 2.4% 2 / 82 MC/DCmcdc 0.0% 0 / 45 Subroutinesub 20.3% 11 / 54 Podpod 100% 7 / 7 totaltotal 10.7% 62 / 578 CC 93 SCAR 58.5
CC
93
Cov
9%
_exec_cmd
1332.0
_get_slots
110.0
_set_mode_by_cmd
72.0
CRAP 348.9
↑ Summary
1 package Redis::Cluster;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Digest::CRC 'crc';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use List::MoreUtils qw(any bsearch);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Params::Validate ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Redis;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 {
15 1 $Data::Dumper::Indent = 0;
16 1 $Data::Dumper::Terse = 1;
17 }
18  
19 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
20 # Default maximum number of slot redirects.
21 1 DEFAULT_MAX_REDIRECTS => 5,
22 # Default cluster state refresh interval (in seconds).
23 # If set to zero, cluster state will be updated only on MOVED redirect.
24 DEFAULT_REFRESH => 0,
25 # Default maximum internal queue size (used in 'multi' mode)
26 DEFAULT_MAX_QUEUE_SIZE => 10,
27 # Minimum number of nodes
28 MIN_NODES => 3,
29 # Total slots in cluster. Used for execution commands
30 # without keys in arguments on random nodes.
31 TOTAL_SLOTS => 0x4000,
32 # Redis responses
33 REDIS_RESPONSE_OK => 'OK',
34 REDIS_RESPONSE_QUEUED => 'QUEUED',
35 1 };
36  
37 1 our $VERSION = '0.14';
38 1 our $AUTOLOAD;
39  
40 1 my %NODES;
41  
42 # Supported commands.
43 # - 'key' may be a Scalar (index of first key in command
44 # arguments) or CodeRef (should return key value)
45 # - 'allow_slave' flag means that command may be executed
46 # on a slave node
47 my %CMD = (
48 append => { key => 0 },
49 bitcount => { key => 0, allow_slave => 1 },
50 bitop => { key => 1 },
51 bitpos => { key => 0, allow_slave => 1 },
52 blpop => { key => 0 },
53 brpop => { key => 0 },
54 brpoplpush => { key => 0 },
55 debug_object => { key => 0 },
56 decr => { key => 0 },
57 decrby => { key => 0 },
58 del => { key => 0 },
59 discard => {
60 0 ▶ key => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
61 0 my $self = shift(@_);
62  
63 0 ▶ croak('[discard] without [multi] is not allowed') unless $self->{_multi};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_multi"} 0 0 0
64 0 return $self->{_multi_key};
65 },
66 },
67 dump => { key => 0, allow_slave => 1 },
68 eval => {
69 0 ▶ key => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
70 0 my $self = shift(@_);
71 0 my ($args) = @_;
72  
73 0 ▶ return $args->[1] ? $args->[2] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$args->[1] ? : 0 0 0
74 },
75 },
76 evalsha => {
77 0 ▶ key => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
78 0 my $self = shift(@_);
79 0 my ($args) = @_;
80  
81 0 ▶ return $args->[1] ? $args->[2] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$args->[1] ? : 0 0 0
82 },
83 },
84 exec => {
85 0 ▶ key => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
86 0 my $self = shift(@_);
87  
88 0 ▶ croak('[exec] without [multi] is not allowed') unless $self->{_multi};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_multi"} 0 0 0
89 0 return $self->{_multi_key};
90 },
91 },
92 exists => { key => 0, allow_slave => 1 },
93 expire => { key => 0 },
94 expireat => { key => 0 },
95 geoadd => { key => 0 },
96 geodist => { key => 0, allow_slave => 1 },
97 geohash => { key => 0, allow_slave => 1 },
98 geopos => { key => 0, allow_slave => 1 },
99 georadius => { key => 0, allow_slave => 1 },
100 georadiusmember => { key => 0, allow_slave => 1 },
101 get => { key => 0, allow_slave => 1 },
102 getbit => { key => 0, allow_slave => 1 },
103 getrange => { key => 0, allow_slave => 1 },
104 getset => { key => 0 },
105 hdel => { key => 0 },
106 hexists => { key => 0, allow_slave => 1 },
107 hget => { key => 0, allow_slave => 1 },
108 hgetall => { key => 0, allow_slave => 1 },
109 hincrby => { key => 0 },
110 hincrbyfloat => { key => 0 },
111 hkeys => { key => 0, allow_slave => 1 },
112 hlen => { key => 0, allow_slave => 1 },
113 hmget => { key => 0, allow_slave => 1 },
114 hmset => { key => 0 },
115 hscan => { key => 0, allow_slave => 1 },
116 hset => { key => 0 },
117 hsetnx => { key => 0 },
118 hstrlen => { key => 0, allow_slave => 1 },
119 hvals => { key => 0, allow_slave => 1 },
120 incr => { key => 0 },
121 incrby => { key => 0 },
122 incrbyfloat => { key => 0 },
123 lindex => { key => 0, allow_slave => 1 },
124 linsert => { key => 0 },
125 llen => { key => 0, allow_slave => 1 },
126 lpop => { key => 0 },
127 lpush => { key => 0 },
128 lpushx => { key => 0 },
129 lrange => { key => 0, allow_slave => 1 },
130 lrem => { key => 0 },
131 lset => { key => 0 },
132 ltrim => { key => 0 },
133 mget => { key => 0, allow_slave => 1 },
134 move => { key => 0 },
135 mset => { key => 0 },
136 msetnx => { key => 0 },
137 multi => {
138 0 ▶ key => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
139 0 my $self = shift(@_);
140  
141 0 ▶ croak("Nested [multi] is not allowed") if $self->{_multi};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_multi"} 0 0 0
142 0 ▶ return $self->{_watch} ? $self->{_multi_key} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_watch'} ? : 0 0 0
143 },
144 },
145 persist => { key => 0 },
146 pexpire => { key => 0 },
147 pexpireat => { key => 0 },
148 pfadd => { key => 0 },
149 pfcount => { key => 0, allow_slave => 1 },
150 pfmerge => { key => 0 },
151 psetex => { key => 0 },
152 pttl => { key => 0, allow_slave => 1 },
153 rename => { key => 0 },
154 renamenx => { key => 0 },
155 restore => { key => 0 },
156 rpop => { key => 0 },
157 rpoplpush => { key => 0 },
158 rpush => { key => 0 },
159 rpushx => { key => 0 },
160 sadd => { key => 0 },
161 scard => { key => 0, allow_slave => 1 },
162 sdiff => { key => 0, allow_slave => 1 },
163 sdiffstore => { key => 0 },
164 set => { key => 0 },
165 setbit => { key => 0 },
166 setex => { key => 0 },
167 setnx => { key => 0 },
168 setrange => { key => 0 },
169 sinter => { key => 0, allow_slave => 1 },
170 sinterstore => { key => 0 },
171 sismember => { key => 0, allow_slave => 1 },
172 smembers => { key => 0, allow_slave => 1 },
173 smove => { key => 0 },
174 sort => { key => 0, allow_slave => 1 },
175 spop => { key => 0 },
176 srandmember => { key => 0, allow_slave => 1 },
177 srem => { key => 0 },
178 sscan => { key => 0, allow_slave => 1 },
179 strlen => { key => 0, allow_slave => 1 },
180 sunion => { key => 0, allow_slave => 1 },
181 sunionstore => { key => 0 },
182 ttl => { key => 0, allow_slave => 1 },
183 type => { key => 0, allow_slave => 1 },
184 unwatch => {
185 0 ▶ key => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
186 0 my $self = shift(@_);
187 0 ▶ return $self->{_watch} ? $self->{_multi_key} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_watch'} ? : 0 0 0
188 },
189 },
190 wait => {
191 0 ▶ key => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
192 0 my $self = shift(@_);
193  
194 0 ▶ if (defined($self->{_last_key})) { return $self->{_last_key}; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_last_key'}) { } 0 0 0
195 0 else { croak('[wait] cannot select node'); }
196 },
197 },
198 watch => {
199 0 ▶ key => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
200 0 my $self = shift(@_);
201 0 my ($args) = @_;
202  
203 0 ▶ return $self->{_watch} ? $self->{_multi_key} : $args->[0];
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_watch'} ? : 0 0 0
204 },
205 },
206 1 zadd => { key => 0 },
207 zcard => { key => 0, allow_slave => 1 },
208 zcount => { key => 0, allow_slave => 1 },
209 zincrby => { key => 0 },
210 zinterstore => { key => 0 },
211 zlexcount => { key => 0, allow_slave => 1 },
212 zrange => { key => 0, allow_slave => 1 },
213 zrangebylex => { key => 0, allow_slave => 1 },
214 zrangebyscore => { key => 0, allow_slave => 1 },
215 zrank => { key => 0, allow_slave => 1 },
216 zrem => { key => 0 },
217 zremrangebylex => { key => 0 },
218 zremrangebyrank => { key => 0 },
219 zremrangebyscore => { key => 0 },
220 zrevrange => { key => 0, allow_slave => 1 },
221 zrevrangebylex => { key => 0, allow_slave => 1 },
222 zrevrangebyscore => { key => 0, allow_slave => 1 },
223 zrevrank => { key => 0, allow_slave => 1 },
224 zscan => { key => 0, allow_slave => 1 },
225 zscore => { key => 0, allow_slave => 1 },
226 zunionstore => { key => 0 },
227 );
228  
229 # Validation spec
230 my %SPEC = (
231 new => {
232 server => {
233 type => SCALAR | ARRAYREF,
234 default => $ENV{REDIS_CLUSTER} || '',
235 },
236 refresh => {
237 type => SCALAR,
238 regex => qr/^\d+$/,
239 default => DEFAULT_REFRESH,
240 },
241 max_redirects => {
242 type => SCALAR,
243 regex => qr/^\d+$/,
244 default => DEFAULT_MAX_REDIRECTS,
245 },
246 max_queue_size => {
247 type => SCALAR,
248 regex => qr/^\d+$/,
249 default => DEFAULT_MAX_QUEUE_SIZE,
250 },
251 allow_slave => { type => BOOLEAN, default => 0 },
252 default_slot => {
253 type => SCALAR,
254 0 ▶ callbacks => { 'total_slots' => sub { int($_[0] || 0) <= TOTAL_SLOTS } },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$_[0] || 0
l!l
00
MC/DC0%0/1
$_[0] || 0
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
255 regex => qr/^\d+$/,
256 optional => 1,
257 },
258 debug => {
259 type => BOOLEAN,
260 1 ▶ default => $ENV{REDIS_CLUSTER_DEBUG} || 0,
Condition50%2/4
$ENV{'REDIS_CLUSTER'} || ''
l!l
01
$ENV{'REDIS_CLUSTER_DEBUG'} || 0
l!l
01
MC/DC0%0/1
$ENV{'REDIS_CLUSTER'} || ''
$ENV{'REDIS_CLUSTER'}
MC/DC0%0/1
$ENV{'REDIS_CLUSTER_DEBUG'} || 0
$ENV{'REDIS_CLUSTER_DEBUG'}
Truth table50%1/2
A
A= $ENV{'REDIS_CLUSTER'}
Aresult
11
00
Truth table50%1/2
A
A= $ENV{'REDIS_CLUSTER_DEBUG'}
Aresult
11
00
261 },
262 },
263 );
264  
265 ####
266 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
267 0 my $class = shift(@_);
268  
269 my $self = {
270 validate_with(
271 params => \@_,
272 spec => $SPEC{new},
273 0 allow_extra => 1,
274 ),
275 _slots => [],
276 _redirects => 0,
277 };
278  
279 # Parse nodes from string
280 0 ▶ unless (ref($self->{server})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->{"server"}) 0 0 0
281 0 $self->{server} = [ split(m/[\s,;]+/, $self->{server}) ];
282 }
283  
284 # Check minimum number of nodes
285 0 ▶ if (@{$self->{server}} < MIN_NODES) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"server"};} < 3) 0 0 0
286 0 croak('At least ' . MIN_NODES . ' nodes should be specified');
287 }
288  
289 0 return bless($self, $class);
290 }
291  
292 ####
293 0 ▶sub get_master_by_key {
Subroutine get_master_by_key: not called CC=1 SCAR=6.9
Pod documented
294 0 my $self = shift(@_);
295 0 my ($key) = @_;
296  
297 0 my $slot = $self->_get_slot_by_key($key);
298 0 return $self->_get_master_by_slot($slot);
299 }
300  
301 ####
302 0 ▶sub get_slave_by_key {
Subroutine get_slave_by_key: not called CC=1 SCAR=6.9
Pod documented
303 0 my $self = shift(@_);
304 0 my ($key, $num) = @_;
305  
306 0 my $slot = $self->_get_slot_by_key($key);
307 0 return $self->_get_slave_by_slot($slot, $num);
308 }
309  
310 ####
311 0 ▶sub get_node_by_key {
Subroutine get_node_by_key: not called CC=1 SCAR=6.9
Pod documented
312 0 my $self = shift(@_);
313 0 my ($key, $num) = @_;
314  
315 0 my $slot = $self->_get_slot_by_key($key);
316 0 return $self->_get_node_by_slot($slot, $num);
317 }
318  
319 ####
320 0 ▶sub get_node {
Subroutine get_node: not called CC=2 SCAR=17.9
Pod documented
321 0 my $self = shift(@_);
322 0 my ($node) = @_;
323  
324 0 ▶ unless ($self->_is_member($node)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_member($node)) 0 0 0
325 0 croak("Node is not a member of cluster: $node");
326 }
327  
328 0 return $self->_get_node($node);
329 }
330  
331 ####
332 0 ▶sub get_random_master {
Subroutine get_random_master: not called CC=1 SCAR=6.9
Pod documented
333 0 my $self = shift(@_);
334 0 return $self->_get_master_by_slot(int(rand(TOTAL_SLOTS)));
335 }
336  
337 ####
338 0 ▶sub get_random_slave {
Subroutine get_random_slave: not called CC=1 SCAR=6.9
Pod documented
339 0 my $self = shift(@_);
340 0 return $self->_get_slave_by_slot(int(rand(TOTAL_SLOTS)));
341 }
342  
343 ####
344 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=1 SCAR=4.0
345 0 my $cmd = $AUTOLOAD;
346 0 $cmd =~ s/.*://;
347  
348 0 ▶ my $method = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
349 0 my $self = shift(@_);
350  
351 0 my $res = $self->_exec_cmd($cmd, @_);
352 0 ▶ warn("[debug] - result: " . Dumper($res) . "\n") if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
353  
354 0 return $res;
355 0 };
356  
357 # Create method if not exists
358 1 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
359 0 *$AUTOLOAD = $method;
360  
361 0 goto $method;
362 }
363  
364 ####
365 0 ▶sub DESTROY { } # Avoid AUTOLOAD
Subroutine DESTROY: not called CC=1 SCAR=0.0
366  
367 ####
368 0 ▶sub _exec_cmd {
Subroutine _exec_cmd: not called CC=36 SCAR=71.9
369 0 my $self = shift(@_);
370 0 my ($cmd, @args) = @_;
371  
372 0 $cmd = lc($cmd);
373  
374 0 ▶ if ($self->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
375 0 ▶ my $multi = $self->{_multi} ? ' [multi]' : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_multi'} ? : 0 0 0
376 0 ▶ my $watch = $self->{_watch} ? ' [watch]' : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_watch'} ? : 0 0 0
377 0 my $cmd_with_args = join(' ', $cmd, @args);
378 0 warn("[debug]${watch}${multi} command: $cmd_with_args\n");
379 }
380  
381 0 ▶ my $multi = $self->{_multi} || $self->{_watch};
Condition0%0/3
$self->{'_multi'} || $self->{'_watch'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'_multi'} || $self->{'_watch'}
$self->{'_multi'} $self->{'_watch'}
Truth table0%0/3
A || B
A= $self->{'_multi'}B= $self->{'_watch'}
ABresult
1X1
011
000
382 0 ▶ my $cmd_params = exists($CMD{$cmd}) ? $CMD{$cmd} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $CMD{$cmd} ? : 0 0 0
383 0 my $key;
384  
385 0 ▶ if ($multi && defined($self->{_multi_key})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multi and defined $self->{'_multi_key'}) { } 0 0 0
Condition0%0/3
$multi and defined $self->{"_multi_key"}
!ll&&!rl&&r
000
MC/DC0%0/2
$multi and defined $self->{"_multi_key"}
$multi defined $self->{"_multi_key"}
Truth table0%0/3
A and B
A= $multiB= defined $self->{"_multi_key"}
ABresult
0X0
100
111
386 # Use 'multi' key
387 0 $key = $self->{_multi_key};
388 }
389 else {
390 # Use command key
391 0 ▶ $key = $cmd_params ? $cmd_params->{key} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$cmd_params ? : 0 0 0
392  
393 0 ▶ if (defined($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 0 0 0
394 # Key is CodeRef, perform function call to get value
395 0 ▶ if (ref($key) eq 'CODE') { $key = &{$key}($self, \@args); }
Branch: 2 branches
BranchTrueFalseTotal
if (ref $key eq 'CODE') { } 0 0 0
elsif (not ref $key) { } 0 0 0
396 # Get key value from command arguments
397 0 elsif (!ref($key)) { $key = $args[$key]; }
398 # Invalid key
399 0 else { croak("Invalid key: $key"); }
400 }
401 }
402  
403 # Store last key for 'wait' command
404 0 $self->{_last_key} = $key;
405 0 ▶ warn('[debug] - key: ' . ($key || '') . "\n") if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
Condition0%0/2
$key || ''
l!l
00
MC/DC0%0/1
$key || ''
$key
Truth table0%0/2
A
A= $key
Aresult
11
00
406  
407 0 my ($redis, $node_type);
408  
409 0 ▶ if (defined($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) { } 0 0 0
410 # Store 'multi' key if nesessary
411 $self->{_multi_key} = $key if !defined($self->{_multi_key}) &&
412 0 ▶ ($multi || any { $cmd eq $_ } qw(multi watch));
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->{"_multi_key"} and $multi || &any(sub { $cmd eq $_; } , "multi", "watch") 0 0 0
Condition0%0/6
not defined $self->{"_multi_key"} and $multi || &any(sub { $cmd eq $_; } , "multi", "watch")
!ll&&!rl&&r
000
$multi or &any(sub { $cmd eq $_; } , "multi", "watch")
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $self->{"_multi_key"} and $multi || &any(sub { $cmd eq $_; } , "multi", "watch")
not defined $self->{"_multi_key"} $multi &any(sub { $cmd eq $_; } , "multi", "watch")
Truth table0%0/4
not(A) and B or C
A= not defined $self->{"_multi_key"}B= $multiC= &any(sub { $cmd eq $_; } , "multi", "watch")
ABCresult
0XX0
1000
11X1
1011
413  
414 # Check if command may be executed on a slave node
415 my $allow_slave = $self->{allow_slave} &&
416 0 ▶ $cmd_params && $cmd_params->{allow_slave} && !$multi;
Condition0%0/9
$self->{'allow_slave'} && $cmd_params && $cmd_params->{'allow_slave'} && !$multi
!ll&&!rl&&r
000
$self->{"allow_slave"} and $cmd_params and $cmd_params->{"allow_slave"}
!ll&&!rl&&r
000
$self->{"allow_slave"} and $cmd_params
!ll&&!rl&&r
000
MC/DC0%0/4
$self->{'allow_slave'} && $cmd_params && $cmd_params->{'allow_slave'} && !$multi
$self->{"allow_slave"} $cmd_params $cmd_params->{"allow_slave"} !$multi
Truth table0%0/5
A and B and C && not(D)
A= $self->{"allow_slave"}B= $cmd_paramsC= $cmd_params->{"allow_slave"}D= !$multi
ABCDresult
0XXX0
10XX0
110X0
11100
11111
417  
418 # Get any node by key
419 0 ▶ if ($allow_slave) { $redis = $self->get_node_by_key($key); }
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_slave) { } 0 0 0
420 # Get master node by key
421 0 else { $redis = $self->get_master_by_key($key); }
422  
423 0 ▶ $node_type = $allow_slave ? 'any' : 'master';
Branch: 1 branch
BranchTrueFalseTotal
$allow_slave ? : 0 0 0
424 }
425 else {
426 my $enqueue = ($self->{_multi} || $cmd eq 'multi') &&
427 0 ▶ !any { $cmd eq $_ } qw(exec discard);
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/6
($self->{'_multi'} || $cmd eq 'multi') && !&any(sub { $cmd eq $_; } , 'exec', 'discard')
!ll&&!rl&&r
000
$self->{"_multi"} or $cmd eq "multi"
l!l&&r!l&&!r
000
MC/DC0%0/3
($self->{'_multi'} || $cmd eq 'multi') && !&any(sub { $cmd eq $_; } , 'exec', 'discard')
$self->{"_multi"} $cmd eq "multi" !&any(sub { $cmd eq $_; } , 'exec', 'discard')
Truth table0%0/5
A or B && not(C)
A= $self->{"_multi"}B= $cmd eq "multi"C= !&any(sub { $cmd eq $_; } , 'exec', 'discard')
ABCresult
00X0
1X00
0100
1X11
0111
428  
429 my $ignore_exec = $cmd eq 'exec' && $self->{_queue} &&
430 0 ▶ @{$self->{_queue}} == 1 && $self->{_queue}->[0]->[0] eq 'multi';
Condition0%0/9
$cmd eq 'exec' && $self->{'_queue'} && @{$self->{'_queue'};} == 1 && $self->{'_queue'}[0][0] eq 'multi'
!ll&&!rl&&r
000
$cmd eq "exec" and $self->{"_queue"} and @{$self->{"_queue"};} == 1
!ll&&!rl&&r
000
$cmd eq "exec" and $self->{"_queue"}
!ll&&!rl&&r
000
MC/DC0%0/4
$cmd eq 'exec' && $self->{'_queue'} && @{$self->{'_queue'};} == 1 && $self->{'_queue'}[0][0] eq 'multi'
$cmd eq "exec" $self->{"_queue"} @{$self->{"_queue"};} == 1 $self->{'_queue'}[0][0] eq 'multi'
Truth table0%0/5
A and B and C && D
A= $cmd eq "exec"B= $self->{"_queue"}C= @{$self->{"_queue"};} == 1D= $self->{'_queue'}[0][0] eq 'multi'
ABCDresult
0XXX0
10XX0
110X0
11100
11111
431  
432 0 ▶ if ($enqueue) {
Branch: 2 branches
BranchTrueFalseTotal
if ($enqueue) { } 0 0 0
elsif ($ignore_exec or &any(sub { $cmd eq $_; } , 'discard', 'unwatch')) { } 0 0 0
433 # Enqueue command without key in 'multi' mode except 'exec' and 'discard'
434 0 $self->_enqueue($cmd, @args);
435 0 ▶ $self->_set_mode_by_cmd($cmd) if $cmd eq 'multi';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "multi" 0 0 0
436  
437 0 ▶ return $cmd eq 'multi' ? REDIS_RESPONSE_OK : REDIS_RESPONSE_QUEUED;
Branch: 1 branch
BranchTrueFalseTotal
$cmd eq 'multi' ? : 0 0 0
438 }
439 0 ▶ elsif ($ignore_exec || any { $cmd eq $_ } qw(discard unwatch)) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$ignore_exec or &any(sub { $cmd eq $_; } , "discard", "unwatch")
l!l&&r!l&&!r
000
MC/DC0%0/2
$ignore_exec or &any(sub { $cmd eq $_; } , "discard", "unwatch")
$ignore_exec &any(sub { $cmd eq $_; } , "discard", "unwatch")
Truth table0%0/3
A or B
A= $ignore_execB= &any(sub { $cmd eq $_; } , "discard", "unwatch")
ABresult
1X1
011
000
440 # - Ignore 'exec' command if queue is empty
441 # - Ignore all enqueued commands on 'discard'
442 # - Ignore 'unwatch' command without previous 'watch'
443 # or after execution of 'exec' or 'discard' commands
444 0 $self->_set_mode_by_cmd($cmd);
445 0 ▶ return $cmd eq 'exec' ? [] : REDIS_RESPONSE_OK;
Branch: 1 branch
BranchTrueFalseTotal
$cmd eq 'exec' ? : 0 0 0
446 }
447 else {
448 0 ▶ if (defined($self->{default_slot})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'default_slot'}) { } 0 0 0
449 # Get default master node
450 0 $redis = $self->_get_master_by_slot($self->{default_slot});
451 0 $node_type = 'default master';
452 }
453 else {
454 # Get random master node
455 0 $redis = $self->get_random_master();
456 0 $node_type = 'random master';
457 }
458 }
459 }
460  
461 0 ▶ warn("[debug] - node ($node_type): $redis->{server}\n") if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
462 0 my $res;
463  
464 0 ▶ try {
Subroutine __ANON__: not called CC=4 SCAR=30.0
465 # Execute all enqueued commands
466 0 ▶ my $queue = delete($self->{_queue}) || [];
Condition0%0/2
delete $self->{'_queue'} || []
l!l
00
MC/DC0%0/1
delete $self->{'_queue'} || []
delete $self->{'_queue'}
Truth table0%0/2
A
A= delete $self->{'_queue'}
Aresult
11
00
467 0 $redis->__std_cmd(@$_) for @$queue; ## no critic
468  
469 # Execute current command
470 0 $res = $redis->__std_cmd($cmd, @args); ## no critic
471 0 ▶ $self->{_redirects} = 0 if $self->{_redirects};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_redirects"} 0 0 0
472 }
473 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
474 0 $res = $self->_on_error($redis, $cmd, \@args, $_);
475 0 };
476  
477 # Set mode
478 0 $self->_set_mode_by_cmd($cmd);
479 0 return $res;
480 }
481  
482 ####
483 0 ▶sub _enqueue {
Subroutine _enqueue: not called CC=3 SCAR=24.8
484 0 my $self = shift(@_);
485 0 my ($cmd, @args) = @_;
486  
487 0 ▶ $self->{_queue} ||= [];
Condition0%0/2
$self->{"_queue"} ||= []
l!l
00
MC/DC0%0/1
$self->{"_queue"} ||= []
$self->{"_queue"}
Truth table0%0/2
A
A= $self->{"_queue"}
Aresult
11
00
488  
489 # Check if max queue size is exceeded
490 0 ▶ if (@{$self->{_queue}} >= $self->{max_queue_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"_queue"};} >= $self->{"max_queue_size"}) 0 0 0
491 0 croak('Max queue size exceeded');
492 }
493  
494 0 push(@{$self->{_queue}}, [ $cmd, @args ]);
495 }
496  
497 ####
498 0 ▶sub _set_mode_by_cmd {
Subroutine _set_mode_by_cmd: not called CC=8 SCAR=42.8
499 0 my $self = shift(@_);
500 0 my ($cmd) = @_;
501  
502 0 ▶ if (any { $cmd eq $_ } qw(exec discard unwatch)) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $cmd eq $_; } , 'exec', 'discard', 'unwatch')) { } 0 0 0
503 # Reset 'multi' and/or 'watch' mode if nesessary
504 0 ▶ delete($self->{_multi}) if $cmd ne 'unwatch';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd ne "unwatch" 0 0 0
505 0 ▶ delete($self->{_multi_key}) unless $cmd eq 'unwatch' && $self->{_multi};
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd eq "unwatch" and $self->{"_multi"} 0 0 0
Condition0%0/3
$cmd eq "unwatch" and $self->{"_multi"}
!ll&&!rl&&r
000
MC/DC0%0/2
$cmd eq "unwatch" and $self->{"_multi"}
$cmd eq "unwatch" $self->{"_multi"}
Truth table0%0/3
A and B
A= $cmd eq "unwatch"B= $self->{"_multi"}
ABresult
0X0
100
111
506 0 ▶ delete($self->{_queue}) if any { $cmd eq $_ } qw(exec discard);
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $cmd eq $_; } , "exec", "discard") 0 0 0
507 0 delete($self->{_watch});
508 }
509 else {
510 # Set 'multi' and/or 'watch' mode if nesessary
511 0 ▶ $self->{_multi} = 1 if $cmd eq 'multi';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "multi" 0 0 0
512 0 ▶ $self->{_watch} = 1 if $cmd eq 'watch';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "watch" 0 0 0
513 }
514 }
515  
516 ####
517 0 ▶sub _on_error {
Subroutine _on_error: not called CC=8 SCAR=42.8
518 0 my $self = shift(@_);
519 0 my ($redis, $cmd, $args, $err_msg) = @_;
520  
521 # Parse error message
522 0 my @err = split(m/[\s,]+/, $err_msg);
523  
524 # Rethrow error unless redirect
525 0 my ($type, $node) = ($err[1], $err[3]);
526 0 ▶ my $redirect = any { $type eq $_ } qw(MOVED ASK);
Subroutine __ANON__: not called CC=1 SCAR=6.9
527 0 ▶ croak($err_msg) unless $redirect;
Branch: 1 branch
BranchTrueFalseTotal
unless $redirect 0 0 0
528  
529 0 ▶ warn("[warn] $err_msg") if $self->{debug} || !$self->{_test};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} or not $self->{"_test"} 0 0 0
Condition0%0/3
$self->{"debug"} or not $self->{"_test"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"debug"} or not $self->{"_test"}
$self->{"debug"} not $self->{"_test"}
Truth table0%0/3
A or not(B)
A= $self->{"debug"}B= not $self->{"_test"}
ABresult
1X1
011
000
530  
531 # Redirect inside 'multi' is not allowed
532 0 ▶ if (exists($self->{_multi})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_multi"}) 0 0 0
533 0 $self->_set_mode_by_cmd('discard');
534 0 $redis->__std_cmd('discard'); ## no critic
535 0 croak('Redirect inside [multi] is not allowed');
536 }
537  
538 # Redirect inside 'watch' is not allowed
539 0 ▶ if (exists($self->{_watch})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_watch"}) 0 0 0
540 0 $self->_set_mode_by_cmd('unwatch');
541 0 $redis->__std_cmd('unwatch'); ## no critic
542 0 croak('Redirect inside [watch] is not allowed');
543 }
544  
545 # Check if max number of slot redirects exceeded
546 0 ▶ if ($self->{_redirects} >= $self->{max_redirects}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_redirects"} >= $self->{"max_redirects"}) 0 0 0
547 0 croak('Too many redirects');
548 }
549  
550 # Perform redirect
551 0 $self->{_redirects}++;
552 # Refresh cluster state immediately if redirect type is 'MOVED'
553 0 ▶ $self->_get_slots('force') if $type eq 'MOVED';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "MOVED" 0 0 0
554  
555 # Get node
556 0 $redis = $self->_get_node($node);
557 0 my $res;
558  
559 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
560 # Retry command on specified node
561 0 $res = $redis->__std_cmd($cmd, @$args); ## no critic
562 0 $self->{_redirects} = 0;
563 }
564 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
565 0 $res = $self->_on_error($redis, $cmd, $args, $_);
566 0 };
567  
568 0 return $res;
569 }
570  
571 ####
572 0 ▶sub _get_slot_by_key {
Subroutine _get_slot_by_key: not called CC=3 SCAR=24.8
573 0 my $self = shift(@_);
574 0 my ($key) = @_;
575  
576 # Hash tag (e.g. {tag}.key)
577 0 ▶ my $tag = ($key =~ m/{([^}]+)}/ ? $1 : $key);
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /{([^}]+)}/ ? : 0 0 0
578  
579 # Redis-specific CRC16
580 0 my $slot = crc($tag, 0x10, 0, 0, 0, 0x1021, 0, 0) & 0x3fff;
581 0 ▶ warn("[debug] - slot: $slot\n") if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
582  
583 0 return $slot;
584 }
585  
586 ####
587 0 ▶sub _get_master_by_slot {
Subroutine _get_master_by_slot: not called CC=1 SCAR=6.9
588 0 my $self = shift(@_);
589 0 my ($slot) = @_;
590  
591 0 my $range = $self->_get_range_by_slot($slot);
592 0 return $self->_get_node(join(':', @{$range->[2]}));
593 }
594  
595 ####
596 0 ▶sub _get_slave_by_slot {
Subroutine _get_slave_by_slot: not called CC=1 SCAR=6.9
597 0 my $self = shift(@_);
598 0 my ($slot, $num) = @_;
599  
600 0 return $self->_get_node_by_slot($slot, $num, 1);
601 }
602  
603 ####
604 0 ▶sub _get_node_by_slot {
Subroutine _get_node_by_slot: not called CC=5 SCAR=34.0
605 0 my $self = shift(@_);
606 0 my ($slot, $num, $offset) = @_;
607  
608 0 ▶ ($num, $offset) = (int($num || 0), int($offset || 0));
Condition0%0/4
$num || 0
l!l
00
$offset || 0
l!l
00
MC/DC0%0/1
$num || 0
$num
MC/DC0%0/1
$offset || 0
$offset
Truth table0%0/2
A
A= $num
Aresult
11
00
Truth table0%0/2
A
A= $offset
Aresult
11
00
609  
610 0 my $range = $self->_get_range_by_slot($slot);
611 0 my $node_cnt = @$range - 2 - $offset;
612 0 ▶ $num = 1 + int(rand($node_cnt)) if $num < 1 || $num > $node_cnt;
Branch: 1 branch
BranchTrueFalseTotal
if $num < 1 or $num > $node_cnt 0 0 0
Condition0%0/3
$num < 1 or $num > $node_cnt
l!l&&r!l&&!r
000
MC/DC0%0/2
$num < 1 or $num > $node_cnt
$num < 1 $num > $node_cnt
Truth table0%0/3
A or B
A= $num < 1B= $num > $node_cnt
ABresult
1X1
011
000
613  
614 0 return $self->_get_node(join(':', @{$range->[ 1 + $offset + $num ]}));
615 }
616  
617 ####
618 0 ▶sub _get_range_by_slot {
Subroutine _get_range_by_slot: not called CC=3 SCAR=24.8
619 0 my $self = shift(@_);
620 0 my ($slot) = @_;
621  
622 0 $slot = int($slot);
623  
624 0 ▶ my ($range) = bsearch { $slot < $_->[0] ? 1 :
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 2 branches
BranchTrueFalseTotal
$slot < $_->[0] ? : 0 0 0
$slot > $_->[1] ? : 0 0 0
625 0 $slot > $_->[1] ? -1 : 0 } @{$self->_get_slots()};
626  
627 0 ▶ if ($range && @$range) { return $range; }
Branch: 1 branch
BranchTrueFalseTotal
if ($range and @$range) { } 0 0 0
Condition0%0/3
$range and @$range
!ll&&!rl&&r
000
MC/DC0%0/2
$range and @$range
$range @$range
Truth table0%0/3
A and B
A= $rangeB= @$range
ABresult
0X0
100
111
628 0 else { croak("Cannot find slot: $slot"); }
629 }
630  
631 ####
632 0 ▶sub _get_slots {
Subroutine _get_slots: not called CC=10 SCAR=47.0
633 0 my $self = shift(@_);
634 0 my ($force) = @_;
635  
636 # No need to refresh cluster state
637 0 ▶ return $self->{_slots} if @{$self->{_slots}} && !$force &&
Condition0%0/3
not $self->{"refresh"} or time - $self->{"_utime"} < $self->{"refresh"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $self->{"refresh"} or time - $self->{"_utime"} < $self->{"refresh"}
not $self->{"refresh"} time - $self->{"_utime"} < $self->{"refresh"}
Truth table0%0/3
not(A) or B
A= not $self->{"refresh"}B= time - $self->{"_utime"} < $self->{"refresh"}
ABresult
1X1
011
000
638 0 ▶ (!$self->{refresh} || time() - $self->{_utime} < $self->{refresh});
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"_slots"};} and not $force and !$self->{"refresh"} || time - $self->{"_utime"} < $self->{"refresh"} 0 0 0
Condition0%0/6
@{$self->{"_slots"};} and not $force and !$self->{"refresh"} || time - $self->{"_utime"} < $self->{"refresh"}
!ll&&!rl&&r
000
@{$self->{"_slots"};} and not $force
!ll&&!rl&&r
000
MC/DC0%0/3
@{$self->{"_slots"};} and not $force and !$self->{"refresh"} || time - $self->{"_utime"} < $self->{"refresh"}
@{$self->{"_slots"};} not $force !$self->{"refresh"} || time - $self->{"_utime"} < $self->{"refresh"}
Truth table0%0/4
A and not(B) and C
A= @{$self->{"_slots"};}B= not $forceC= !$self->{"refresh"} || time - $self->{"_utime"} < $self->{"refresh"}
ABCresult
0XX0
10X0
1100
1111
639  
640 # Get cluster slots
641 0 my $slots;
642  
643 0 foreach my $node (@{$self->{server}}) {
644 0 my $redis = $self->_get_node($node);
645 0 $slots = $redis->cluster_slots();
646  
647 0 ▶ last if $slots && @$slots;
Branch: 1 branch
BranchTrueFalseTotal
if $slots and @$slots 0 0 0
Condition0%0/3
$slots and @$slots
!ll&&!rl&&r
000
MC/DC0%0/2
$slots and @$slots
$slots @$slots
Truth table0%0/3
A and B
A= $slotsB= @$slots
ABresult
0X0
100
111
648 }
649  
650 0 ▶ croak('Cannot get cluster state') unless $slots && @$slots;
Branch: 1 branch
BranchTrueFalseTotal
unless $slots and @$slots 0 0 0
Condition0%0/3
$slots and @$slots
!ll&&!rl&&r
000
MC/DC0%0/2
$slots and @$slots
$slots @$slots
Truth table0%0/3
A and B
A= $slotsB= @$slots
ABresult
0X0
100
111
651  
652 # Sort slots for binary search
653 0 $self->{_slots} = [ sort { $a->[0] <=> $b->[0] } @$slots ];
654 # Update refresh time
655 0 $self->{_utime} = time();
656  
657 0 return $self->{_slots};
658 }
659  
660 ####
661 0 ▶sub _get_node {
Subroutine _get_node: not called CC=3 SCAR=24.8
662 0 my $self = shift(@_);
663 0 my ($node) = @_;
664  
665 0 ▶ $node = join(':', @$node) if ref($node);
Branch: 1 branch
BranchTrueFalseTotal
if ref $node 0 0 0
666  
667 0 ▶ unless (exists($NODES{$node})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $NODES{$node}) 0 0 0
668 0 $NODES{$node} = Redis->new(%$self, server => $node);
669 }
670  
671 0 return $NODES{$node};
672 }
673  
674 ####
675 0 ▶sub _is_member {
Subroutine _is_member: not called CC=4 SCAR=30.0
676 0 my $self = shift(@_);
677 0 my ($node) = @_;
678  
679 0 ▶ $node = join(':', @$node) if ref($node);
Branch: 1 branch
BranchTrueFalseTotal
if ref $node 0 0 0
680  
681 0 foreach my $range (@{$self->_get_slots()}) {
682 0 ▶ return 1 if any { $node eq join(':', @$_) } @$range[ 2 .. $#$range ];
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $node eq join(":", @$_); } , @$range[2 .. $range->$#*]) 0 0 0
683 }
684  
685 0 return;
686 }
687  
688 1 1;
689  
690 __END__
↑ Summary