blib/lib/Redis/ClusterRider.pm

filter: Statementstmt 94.9% 282 / 297 Branchbran 79.0% 87 / 110 Conditioncond 66.6% 12 / 18 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 96.9% 32 / 33 Podpod 100% 6 / 6 totaltotal 89.4% 425 / 475 CC 69 SCAR 19.7
CC
69
Cov
90%
_run_command
16.4
_route
12.0
new
10.9
CRAP 7.2
↑ Summary
1 package Redis::ClusterRider;
2  
3 6 ▶use 5.008000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 our $VERSION = '0.26';
9  
10 6 ▶use Redis;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use List::MoreUtils qw( bsearch );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Scalar::Util qw( looks_like_number weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 our @EXPORT_OK = qw( crc16 hash_slot );
18 }
19  
20 6 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 D_REFRESH_INTERVAL => 15,
22 MAX_SLOTS => 16384,
23 EOL => "\r\n",
24 6 };
25  
26 6 my @CRC16_TAB = (
27 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
28 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
29 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
30 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
31 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
32 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
33 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
34 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
35 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
36 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
37 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
38 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
39 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
40 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
41 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
42 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
43 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
44 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
45 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
46 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
47 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
48 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
49 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
50 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
51 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
52 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
53 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
54 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
55 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
56 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
57 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
58 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0,
59 );
60  
61 6 my %PREDEFINED_CMDS = (
62 sort => { readonly => 0, key_pos => 1 },
63 zunionstore => { readonly => 0, key_pos => 1 },
64 zinterstore => { readonly => 0, key_pos => 1 },
65 eval => { readonly => 0, movablekeys => 1, key_pos => 0 },
66 evalsha => { readonly => 0, movablekeys => 1, key_pos => 0 },
67 );
68  
69 6 $Carp::Internal{ (__PACKAGE__) }++;
70  
71  
72 12 ▶sub new {
Subroutine new: called CC=10 SCAR=23.9
Pod documented
73 12 my $class = shift;
74 12 my %params = @_;
75  
76 12 my $self = bless {}, $class;
77  
78 12 ▶ unless ( defined $params{startup_nodes} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"startup_nodes"}) 1 11 12
79 1 croak 'Startup nodes not specified';
80 }
81 11 ▶ unless ( ref( $params{startup_nodes} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $params{"startup_nodes"} eq "ARRAY") 1 10 11
82 1 croak 'Startup nodes must be specified as array reference';
83 }
84 10 ▶ unless ( @{ $params{startup_nodes} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$params{"startup_nodes"};}) 1 9 10
85 1 croak 'Specified empty list of startup nodes';
86 }
87  
88 9 ▶ if ( $params{fallback} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"fallback"}) 0 9 9
89 0 ▶ if ( $params{lazy} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"lazy"}) 0 0 0
90 0 carp 'Fallback mode revokes lazy for ' . $params{startup_nodes}->[0];
91 }
92  
93 0 my $node = Redis->new(%params, server => $params{startup_nodes}->[0]);
94 0 ▶ eval { $node->cluster_info(); 1 } or return $node;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $node->cluster_info; 1 } } or return $node 0 0 0
95 }
96  
97 9 $self->{startup_nodes} = $params{startup_nodes};
98 9 $self->{allow_slaves} = $params{allow_slaves};
99 9 $self->{lazy} = $params{lazy};
100 9 $self->refresh_interval( $params{refresh_interval} );
101  
102 7 $self->{on_node_connect} = $params{on_node_connect};
103 7 $self->{on_node_error} = $params{on_node_error};
104  
105 7 my %node_params;
106 7 foreach my $name ( qw( conservative_reconnect cnx_timeout read_timeout
107 write_timeout password username name debug ) )
108 {
109 56 ▶ next unless defined $params{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{$name} 50 6 56
110 6 $node_params{$name} = $params{$name};
111 }
112 7 $self->{_node_params} = \%node_params;
113  
114 7 $self->{_nodes_pool} = undef;
115 7 $self->{_nodes} = undef;
116 7 $self->{_master_nodes} = undef;
117 7 $self->{_slots} = undef;
118 7 $self->{_commands} = undef;
119 7 $self->{_refresh_timestamp} = undef;
120  
121 7 ▶ unless ( $self->{lazy} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"lazy"}) 5 2 7
122 5 $self->_init;
123 }
124  
125 7 return $self;
126 }
127  
128 1 ▶sub run_command {
Subroutine run_command: called CC=1 SCAR=0.0
Pod documented
129 1 my $self = shift;
130 1 my $cmd_name = shift;
131  
132 1 return $self->_route( $cmd_name, [ @_ ] );
133 }
134  
135 4 ▶sub nodes {
Subroutine nodes: called CC=4 SCAR=14.0
Pod documented
136 4 my $self = shift;
137 4 my $key = shift;
138 4 my $allow_slaves = shift;
139  
140 4 ▶ unless ( defined $self->{_slots} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_slots"}) 0 4 4
141 0 $self->_init;
142 }
143  
144 4 my $slot;
145 4 ▶ if ( defined $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 2 2 4
146 2 $slot = hash_slot($key);
147 }
148  
149 4 my $nodes = $self->_nodes( $slot, $allow_slaves );
150  
151 return wantarray
152 4 ? @{ $self->{_nodes_pool} }{ @{$nodes} }
153 4 ▶ : $self->{_nodes_pool}{ $nodes->[0] };
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 0 4
154 }
155  
156 16 ▶sub refresh_interval {
Subroutine refresh_interval: called CC=5 SCAR=16.1
Pod documented
157 16 my $self = shift;
158  
159 16 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 13 3 16
160 13 my $seconds = shift;
161  
162 13 ▶ if ( defined $seconds ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $seconds) { } 10 3 13
163 10 ▶ if ( !looks_like_number($seconds) || $seconds < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not &looks_like_number($seconds) or $seconds < 0) 4 6 10
Condition100%3/3
not &looks_like_number($seconds) or $seconds < 0
l!l&&r!l&&!r
226
MC/DC100%2/2
not &looks_like_number($seconds) or $seconds < 0
not &looks_like_number($seconds) $seconds < 0
Truth table100%3/3
not(A) or B
A= not &looks_like_number($seconds)B= $seconds < 0
ABresult
1X1
011
000
164 4 croak qq{"refresh_interval" must be a positive number};
165 }
166 6 $self->{refresh_interval} = $seconds;
167 }
168 else {
169 3 $self->{refresh_interval} = D_REFRESH_INTERVAL;
170 }
171 }
172  
173 12 return $self->{refresh_interval};
174 }
175  
176 11 ▶sub crc16 {
Subroutine crc16: called CC=3 SCAR=11.3
Pod documented
177 11 my $data = shift;
178  
179 11 ▶ unless ( utf8::downgrade( $data, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (utf8::downgrade($data, 1)) 0 11 11
180 0 utf8::encode($data);
181 }
182  
183 11 my $crc = 0;
184 11 foreach my $char ( split //, $data ) {
185 39 $crc = ( $crc << 8 & 0xff00 )
186 ^ $CRC16_TAB[ ( ( $crc >> 8 ) ^ ord($char) ) & 0x00ff ];
187 }
188  
189 11 return $crc;
190 }
191  
192 10 ▶sub hash_slot {
Subroutine hash_slot: called CC=3 SCAR=11.0
Pod documented
193 10 my $key = shift;
194  
195 10 my $hashtag = $key;
196  
197 10 ▶ if ( $key =~ m/\{([^}]*?)\}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\{([^}]*?)\}/) 1 9 10
198 1 ▶ if ( length $1 > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $1 > 0) 1 0 1
199 1 $hashtag = $1;
200 }
201 }
202  
203 10 return crc16($hashtag) % MAX_SLOTS;
204 }
205  
206 7 ▶sub _init {
Subroutine _init: called CC=2 SCAR=6.9
207 7 my $self = shift;
208  
209 7 $self->_discover_cluster;
210  
211 7 ▶ if ( $self->{refresh_interval} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"refresh_interval"} > 0) 6 1 7
212 6 $self->{_refresh_timestamp} = [Time::HiRes::gettimeofday];
213 }
214  
215 7 return;
216 }
217  
218 7 ▶sub _discover_cluster {
Subroutine _discover_cluster: called CC=6 SCAR=18.0
219 7 my $self = shift;
220  
221 7 my $nodes;
222  
223 7 ▶ if ( defined $self->{_slots} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_slots'}) { } 1 6 7
224 1 $nodes = $self->_nodes( undef, $self->{allow_slaves} );
225 }
226 else {
227 6 my %nodes_pool;
228  
229 6 foreach my $hostport ( @{ $self->{startup_nodes} } ) {
230 18 ▶ unless ( defined $nodes_pool{$hostport} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nodes_pool{$hostport}) 18 0 18
231 18 $nodes_pool{$hostport} = $self->_new_node($hostport);
232 }
233 }
234  
235 6 $self->{_nodes_pool} = \%nodes_pool;
236 6 $nodes = [ keys %nodes_pool ];
237 }
238  
239 7 $self->_run_command( 'cluster_state', [], $nodes );
240 7 my $slots = $self->_run_command( 'cluster_slots', [], $nodes );
241  
242 7 ▶ unless ( @{$slots} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$slots;}) 0 7 7
243 0 croak 'ERR Returned empty list of slots';
244 }
245  
246 7 $self->_prepare_nodes($slots);
247  
248 7 ▶ unless ( defined $self->{_commands} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_commands"}) 6 1 7
249 6 $self->_load_commands;
250 }
251  
252 7 return;
253 }
254  
255 7 ▶sub _prepare_nodes {
Subroutine _prepare_nodes: called CC=6 SCAR=17.9
256 7 my $self = shift;
257 7 my $slots_raw = shift;
258  
259 7 my %nodes_pool;
260 my @slots;
261 7 my @masters_nodes;
262  
263 7 my $nodes_pool_old = $self->{_nodes_pool};
264  
265 7 foreach my $range ( @{$slots_raw} ) {
266 28 my $range_start = shift @{$range};
267 28 my $range_end = shift @{$range};
268  
269 28 my @nodes;
270 28 my $is_master = 1;
271  
272 28 foreach my $node_info ( @{$range} ) {
273 70 my $hostport = "$node_info->[0]:$node_info->[1]";
274  
275 70 ▶ unless ( defined $nodes_pool{$hostport} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nodes_pool{$hostport}) 49 21 70
276 49 ▶ if ( defined $nodes_pool_old->{$hostport} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nodes_pool_old->{$hostport}) { } 25 24 49
277 25 $nodes_pool{$hostport} = delete $nodes_pool_old->{$hostport};
278 }
279 else {
280 24 $nodes_pool{$hostport} = $self->_new_node($hostport);
281 }
282  
283 49 ▶ if ($is_master) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_master) 21 28 49
284 21 push( @masters_nodes, $hostport );
285 21 $is_master = 0;
286 }
287 }
288  
289 70 push( @nodes, $hostport );
290 }
291  
292 28 push( @slots, [ $range_start, $range_end, \@nodes ] );
293 }
294  
295 7 @slots = sort { $a->[0] <=> $b->[0] } @slots;
296  
297 7 $self->{_nodes_pool} = \%nodes_pool;
298 7 $self->{_nodes} = [ keys %nodes_pool ];
299 7 $self->{_master_nodes} = \@masters_nodes;
300 7 $self->{_slots} = \@slots;
301  
302 7 return;
303 }
304  
305 6 ▶sub _load_commands {
Subroutine _load_commands: called CC=5 SCAR=16.1
306 6 my $self = shift;
307  
308 6 my $nodes = $self->_nodes( undef, $self->{allow_slaves} );
309 6 my $commands_raw = $self->_run_command( 'command', [], $nodes );
310  
311 6 my %commands = %PREDEFINED_CMDS;
312  
313 6 foreach my $cmd_raw ( @{$commands_raw} ) {
314 30 my $kwd = lc( $cmd_raw->[0] );
315  
316 30 ▶ next if exists $commands{$kwd};
Branch: 1 branch
BranchTrueFalseTotal
if exists $commands{$kwd} 0 30 30
317  
318 30 my $readonly = 0;
319 30 foreach my $flag ( @{ $cmd_raw->[2] } ) {
320 48 ▶ if ( $flag eq 'readonly' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag eq "readonly") 12 36 48
321 12 $readonly = 1;
322 12 last;
323 }
324 }
325  
326 30 $commands{$kwd} = {
327 readonly => $readonly,
328 key_pos => $cmd_raw->[3],
329 };
330 }
331  
332 6 $self->{_commands} = \%commands;
333  
334 6 return;
335 }
336  
337 43 ▶sub _new_node {
Subroutine _new_node: called CC=1 SCAR=0.0
338 43 my $self = shift;
339 43 my $hostport = shift;
340  
341 return Redis->new(
342 43 %{ $self->{_node_params} },
343 server => $hostport,
344 reconnect => 0.001, # reconnect only once
345 every => 1000,
346 no_auto_connect_on_new => 1,
347  
348 on_connect => $self->_create_on_node_connect($hostport),
349 );
350 }
351  
352 43 ▶sub _create_on_node_connect {
Subroutine _create_on_node_connect: called CC=1 SCAR=0.0
353 43 my $self = shift;
354 43 my $hostport = shift;
355  
356 43 weaken($self);
357  
358 43 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
359 43 my $redis = shift;
360  
361 43 ▶ if ( $self->{allow_slaves} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"allow_slaves"}) 14 29 43
362 14 $redis->readonly;
363 }
364  
365 43 ▶ if ( defined $self->{on_node_connect} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"on_node_connect"}) 0 43 43
366 0 $self->{on_node_connect}->($hostport);
367 }
368 43 };
369 }
370  
371 10 ▶sub _route {
Subroutine _route: called CC=11 SCAR=24.8
372 10 my $self = shift;
373 10 my $cmd_name = shift;
374 10 my $args = shift;
375  
376 10 ▶ if ( !defined $self->{_slots} || (
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"_slots"} or $self->{"refresh_interval"} > 0 and Time::HiRes::tv_interval($self->{"_refresh_timestamp"}) > $self->{"refresh_interval"}) 1 9 10
Condition50%3/6
not defined $self->{"_slots"} or $self->{"refresh_interval"} > 0 and Time::HiRes::tv_interval($self->{"_refresh_timestamp"}) > $self->{"refresh_interval"}
l!l&&r!l&&!r
109
$self->{"refresh_interval"} > 0 and Time::HiRes::tv_interval($self->{"_refresh_timestamp"}) > $self->{"refresh_interval"}
!ll&&!rl&&r
090
MC/DC33%1/3
not defined $self->{"_slots"} or $self->{"refresh_interval"} > 0 and Time::HiRes::tv_interval($self->{"_refresh_timestamp"}) > $self->{"refresh_interval"}
not defined $self->{"_slots"} $self->{"refresh_interval"} > 0 Time::HiRes::tv_interval($self->{"_refresh_timestamp"}) > $self->{"refresh_interval"}
Truth table50%2/4
not(A) or B and C
A= not defined $self->{"_slots"}B= $self->{"refresh_interval"} > 0C= Time::HiRes::tv_interval($self->{"_refresh_timestamp"}) > $self->{"refresh_interval"}
ABCresult
1XX1
0111
00X0
0100
377 $self->{refresh_interval} > 0
378 && Time::HiRes::tv_interval( $self->{_refresh_timestamp} )
379 > $self->{refresh_interval} ) )
380 {
381 1 $self->_init;
382 }
383  
384 10 my $key;
385 10 my @kwds = split( m/_/, lc($cmd_name) );
386 10 my $cmd_info = $self->{_commands}{ $kwds[0] };
387  
388 10 ▶ if ( defined $cmd_info ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cmd_info) 8 2 10
389 8 ▶ if ( $cmd_info->{key_pos} > 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cmd_info->{'key_pos'} > 0) { } 7 1 8
elsif ($cmd_info->{'movablekeys'} and $args->[1] > 0) { } 0 2 2
390 7 $key = $args->[ $cmd_info->{key_pos} - scalar @kwds ];
391 }
392 # Exception for EVAL and EVALSHA commands
393 ▶ elsif ( $cmd_info->{movablekeys}
Condition33%1/3
$cmd_info->{"movablekeys"} and $args->[1] > 0
!ll&&!rl&&r
100
MC/DC0%0/2
$cmd_info->{"movablekeys"} and $args->[1] > 0
$cmd_info->{"movablekeys"} $args->[1] > 0
Truth table33%1/3
A and B
A= $cmd_info->{"movablekeys"}B= $args->[1] > 0
ABresult
0X0
100
111
394 && $args->[1] > 0 )
395 {
396 0 $key = $args->[2];
397 }
398 }
399  
400 10 my $slot;
401 10 my $allow_slaves = $self->{allow_slaves};
402  
403 10 ▶ if ( defined $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 7 3 10
404 7 $slot = hash_slot($key);
405 7 ▶ $allow_slaves &&= $cmd_info->{readonly};
Condition100%3/3
$allow_slaves &&= $cmd_info->{"readonly"}
!ll&&!rl&&r
214
MC/DC100%2/2
$allow_slaves &&= $cmd_info->{"readonly"}
$allow_slaves $cmd_info->{"readonly"}
Truth table100%3/3
A &&= B
A= $allow_slavesB= $cmd_info->{"readonly"}
ABresult
0X0
100
111
406 }
407  
408 10 my $nodes = $self->_nodes( $slot, $allow_slaves );
409  
410 10 ▶ unless ( defined $nodes ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nodes) 0 10 10
411 0 croak 'ERR Target node not found. Maybe not all slots are served';
412 }
413  
414 10 return $self->_run_command( $cmd_name, $args, $nodes );
415 }
416  
417 31 ▶sub _run_command {
Subroutine _run_command: called CC=16 SCAR=28.0
418 31 my $self = shift;
419 31 my $cmd_name = shift;
420 31 my $args = shift;
421 31 my $nodes = shift;
422  
423 31 my $nodes_pool = $self->{_nodes_pool};
424  
425 31 my $nodes_num = scalar @{$nodes};
426 31 my $node_index = int( rand($nodes_num) );
427 31 my $fails_cnt = 0;
428 31 my $wantarray = wantarray;
429  
430 31 ▶ my $cmd_method
Branch: 1 branch
BranchTrueFalseTotal
$cmd_name eq 'cluster_state' ? : 7 24 31
431 = $cmd_name eq 'cluster_state'
432 ? 'cluster_info'
433 : $cmd_name;
434  
435 31 while (1) {
436 32 my $hostport = $nodes->[$node_index];
437 32 my $node = $nodes_pool->{$hostport};
438  
439 32 my $reply;
440 my @arr_reply;
441 32 my $err_msg;
442  
443 {
444 32 local $@;
445  
446 32 eval {
447 32 ▶ if ( $cmd_name eq 'cluster_state' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cmd_name eq 'cluster_state') { } 7 25 32
elsif ($wantarray) { } 1 24 25
448 7 undef $wantarray;
449 7 my $reply_raw = $node->$cmd_method( @{$args} );
450 7 $reply = _parse_info($reply_raw);
451  
452 7 ▶ if ( $reply->{cluster_state} eq 'ok' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reply->{'cluster_state'} eq 'ok') { } 7 0 7
453 7 $reply = 1;
454 }
455 else {
456 0 croak 'CLUSTERDOWN The cluster is down';
457 }
458 }
459 elsif ( $wantarray ) {
460 1 @arr_reply = $node->$cmd_method( @{$args} );
461 }
462 else {
463 24 $reply = $node->$cmd_method( @{$args} );
464 }
465 };
466  
467 32 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 3 29 32
468 3 $err_msg = $@;
469 }
470 }
471  
472 32 ▶ if ($err_msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err_msg) 3 29 32
473 3 my $err_code = 'ERR';
474 3 ▶ if ( $err_msg =~ m/^(?:\[\w+\]\s+)?([A-Z]{3,})/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err_msg =~ /^(?:\[\w+\]\s+)?([A-Z]{3,})/) 3 0 3
475 3 $err_code = $1;
476 }
477  
478 3 ▶ if ( $err_code eq 'MOVED' || $err_code eq 'ASK' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err_code eq "MOVED" or $err_code eq "ASK") 1 2 3
Condition66%2/3
$err_code eq "MOVED" or $err_code eq "ASK"
l!l&&r!l&&!r
102
MC/DC50%1/2
$err_code eq "MOVED" or $err_code eq "ASK"
$err_code eq "MOVED" $err_code eq "ASK"
Truth table66%2/3
A or B
A= $err_code eq "MOVED"B= $err_code eq "ASK"
ABresult
1X1
011
000
479 1 ▶ if ( $err_code eq 'MOVED' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err_code eq "MOVED") 1 0 1
480 1 $self->_init;
481 }
482  
483 1 my ($fwd_hostport) = ( split( m/\s+/, $err_msg ) )[3];
484 1 $fwd_hostport =~ s/,$//;
485  
486 1 ▶ unless ( defined $nodes_pool->{$fwd_hostport} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nodes_pool->{$fwd_hostport}) 1 0 1
487 1 $nodes_pool->{$fwd_hostport} = $self->_new_node( $fwd_hostport );
488 }
489  
490 1 return $self->_run_command( $cmd_name, $args, [ $fwd_hostport ] );
491 }
492  
493 2 ▶ if ( defined $self->{on_node_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"on_node_error"}) 0 2 2
494 0 $self->{on_node_error}->( $err_msg, $hostport );
495 }
496  
497 2 ▶ if ( ++$fails_cnt < $nodes_num ) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$fails_cnt < $nodes_num) 1 1 2
498 1 ▶ if ( ++$node_index == $nodes_num ) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$node_index == $nodes_num) 0 1 1
499 0 $node_index = 0;
500 }
501  
502 1 next;
503 }
504  
505 1 die $err_msg;
506 }
507  
508 29 ▶ return $wantarray ? @arr_reply : $reply;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 1 28 29
509 }
510 }
511  
512 21 ▶sub _nodes {
Subroutine _nodes: called CC=5 SCAR=16.1
513 21 my $self = shift;
514 21 my $slot = shift;
515 21 my $allow_slaves = shift;
516  
517 21 ▶ if ( defined $slot ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $slot) 9 12 21
518 18 ▶ my ($range) = bsearch {
Subroutine __ANON__: called CC=3 SCAR=11.2
519 18 ▶ $slot > $_->[1] ? -1 : $slot < $_->[0] ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
$slot > $_->[1] ? : 9 9 18
$slot < $_->[0] ? : 0 9 9
520 }
521 9 @{ $self->{_slots} };
522  
523 9 ▶ return unless defined $range;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $range 0 9 9
524  
525 9 ▶ return $allow_slaves
Branch: 1 branch
BranchTrueFalseTotal
$allow_slaves ? : 5 4 9
526 ? $range->[2]
527 : [ $range->[2][0] ];
528 }
529  
530 return $allow_slaves
531 ? $self->{_nodes}
532 12 ▶ : $self->{_master_nodes};
Branch: 1 branch
BranchTrueFalseTotal
$allow_slaves ? : 6 6 12
533 }
534  
535 7 ▶sub _parse_info {
Subroutine _parse_info: called CC=1 SCAR=0.0
536 7 return { map { split( m/:/, $_, 2 ) }
537 7 grep { m/^[^#]/ } split( EOL, $_[0] ) };
538 }
539  
540 5 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
541 5 our $AUTOLOAD;
542 5 my $cmd_name = $AUTOLOAD;
543 5 $cmd_name =~ s/^.+:://;
544  
545 9 ▶ my $sub = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
546 9 my $self = shift;
547 9 return $self->_route( $cmd_name, [@_] );
548 5 };
549  
550 5 do {
551 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
552 5 *{$cmd_name} = $sub;
553 };
554  
555 5 goto &{$sub};
556 }
557  
558 0 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
559  
560 6 1;
561 __END__
↑ Summary