blib/lib/AnyEvent/RipeRedis/Cluster.pm

filter: Statementstmt 84.5% 388 / 459 Branchbran 71.5% 103 / 144 Conditioncond 57.6% 15 / 26 MC/DCmcdc 41.1% 7 / 17 Subroutinesub 85.9% 49 / 57 Podpod 100% 8 / 8 totaltotal 80.1% 570 / 711 CC 94 SCAR 19.3
CC
94
Cov
80%
_abort
30.0
__ANON__
19.9
_route
11.4
CRAP 6.9
↑ Summary
1 package AnyEvent::RipeRedis::Cluster;
2  
3 7 ▶use 5.008000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 our $VERSION = '0.32';
9  
10 7 ▶use AnyEvent::RipeRedis;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use AnyEvent::RipeRedis::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 7 ▶use AnyEvent::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use List::MoreUtils qw( bsearch );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use Scalar::Util qw( looks_like_number weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 7 my %ERROR_CODES;
19  
20 7 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
21 7 %ERROR_CODES = %AnyEvent::RipeRedis::Error::ERROR_CODES;
22 7 my @err_codes = keys %ERROR_CODES;
23 7 our @EXPORT_OK = ( @err_codes, qw( crc16 hash_slot ) );
24 7 our %EXPORT_TAGS = ( err_codes => \@err_codes );
25 }
26  
27 7 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
28 7 D_REFRESH_INTERVAL => 15,
29  
30 %ERROR_CODES,
31  
32 # Operation status
33 S_NEED_DO => 1,
34 S_IN_PROGRESS => 2,
35 S_DONE => 3,
36  
37 MAX_SLOTS => 16384,
38 7 };
39  
40 7 my @CRC16_TAB = (
41 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
42 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
43 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
44 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
45 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
46 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
47 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
48 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
49 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
50 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
51 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
52 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
53 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
54 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
55 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
56 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
57 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
58 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
59 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
60 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
61 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
62 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
63 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
64 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
65 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
66 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
67 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
68 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
69 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
70 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
71 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
72 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0,
73 );
74  
75 7 my %PREDEFINED_CMDS = (
76 sort => { readonly => 0, key_pos => 1 },
77 zunionstore => { readonly => 0, key_pos => 1 },
78 zinterstore => { readonly => 0, key_pos => 1 },
79 eval => { readonly => 0, movablekeys => 1, key_pos => 0 },
80 evalsha => { readonly => 0, movablekeys => 1, key_pos => 0 },
81 );
82  
83 7 my %SUB_CMDS = (
84 subscribe => 1,
85 psubscribe => 1,
86 );
87  
88  
89 10 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
90 10 my $class = shift;
91 10 my %params = @_;
92  
93 10 my $self = bless {}, $class;
94  
95 10 ▶ unless ( defined $params{startup_nodes} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"startup_nodes"}) 1 9 10
96 1 croak 'Startup nodes not specified';
97 }
98 9 ▶ unless ( ref( $params{startup_nodes} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $params{"startup_nodes"} eq "ARRAY") 1 8 9
99 1 croak 'Startup nodes must be specified as array reference';
100 }
101 8 ▶ unless ( @{ $params{startup_nodes} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$params{"startup_nodes"};}) 1 7 8
102 1 croak 'Specified empty list of startup nodes';
103 }
104  
105 7 $self->{startup_nodes} = $params{startup_nodes};
106 7 $self->{allow_slaves} = $params{allow_slaves};
107 7 $self->{lazy} = $params{lazy};
108 7 $self->refresh_interval( $params{refresh_interval} );
109  
110 5 $self->{on_node_connect} = $params{on_node_connect};
111 5 $self->{on_node_disconnect} = $params{on_node_disconnect};
112 5 $self->{on_node_error} = $params{on_node_error};
113 5 $self->on_error( $params{on_error} );
114  
115 5 my %node_params;
116 5 foreach my $name ( qw( password utf8 connection_timeout read_timeout
117 reconnect reconnect_interval handle_params ) )
118 {
119 35 ▶ next unless defined $params{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{$name} 32 3 35
120 3 $node_params{$name} = $params{$name};
121 }
122 5 $self->{_node_params} = \%node_params;
123  
124 5 $self->_reset_internals;
125 5 $self->{_input_queue} = [];
126 5 $self->{_temp_queue} = [];
127  
128 5 ▶ unless ( $self->{lazy} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"lazy"}) 4 1 5
129 4 $self->_init;
130 }
131  
132 5 return $self;
133 }
134  
135 1 ▶sub execute {
Subroutine execute: called CC=1 SCAR=0.0
Pod documented
136 1 my $self = shift;
137 1 my $cmd_name = shift;
138  
139 1 my $cmd = $self->_prepare( $cmd_name, [@_] );
140 1 $self->_route($cmd);
141  
142 1 return;
143 }
144  
145 0 ▶sub disconnect {
Subroutine disconnect: not called CC=2 SCAR=17.9
Pod documented
146 0 my $self = shift;
147  
148 0 foreach my $node ( values %{ $self->{_nodes_pool} } ) {
149 0 $node->disconnect;
150 }
151 0 $self->_reset_internals;
152 0 $self->_abort;
153  
154 0 return;
155 }
156  
157 4 ▶sub nodes {
Subroutine nodes: called CC=5 SCAR=16.3
Pod documented
158 4 my $self = shift;
159 4 my $key = shift;
160 4 my $allow_slaves = shift;
161  
162 4 ▶ return unless defined $self->{_slots};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_slots"} 0 4 4
163  
164 4 my $slot;
165 4 ▶ if ( defined $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 2 2 4
166 2 $slot = hash_slot($key);
167 }
168  
169 4 my $nodes = $self->_nodes( $slot, $allow_slaves );
170  
171 4 ▶ return unless defined $nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nodes 0 4 4
172  
173 return wantarray
174 4 ? @{ $self->{_nodes_pool} }{ @{$nodes} }
175 4 ▶ : $self->{_nodes_pool}{ $nodes->[0] };
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 0 4
176 }
177  
178 14 ▶sub refresh_interval {
Subroutine refresh_interval: called CC=5 SCAR=16.1
Pod documented
179 14 my $self = shift;
180  
181 14 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 11 3 14
182 11 my $seconds = shift;
183  
184 11 ▶ if ( defined $seconds ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $seconds) { } 7 4 11
185 7 ▶ if ( !looks_like_number($seconds) || $seconds < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not &looks_like_number($seconds) or $seconds < 0) 4 3 7
Condition100%3/3
not &looks_like_number($seconds) or $seconds < 0
l!l&&r!l&&!r
223
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
186 4 croak qq{"refresh_interval" must be a positive number};
187 }
188 3 $self->{refresh_interval} = $seconds;
189 }
190 else {
191 4 $self->{refresh_interval} = D_REFRESH_INTERVAL;
192 }
193 }
194  
195 10 return $self->{refresh_interval};
196 }
197  
198 10 ▶sub on_error {
Subroutine on_error: called CC=3 SCAR=11.0
Pod documented
199 10 my $self = shift;
200  
201 10 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 7 3 10
202 7 my $on_error = shift;
203  
204 7 ▶ if ( defined $on_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $on_error) { } 2 5 7
205 2 $self->{on_error} = $on_error;
206 }
207 else {
208 2 ▶ $self->{on_error} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
209 2 my $err = shift;
210 2 warn $err->message . "\n";
211 5 };
212 }
213 }
214  
215 10 return $self->{on_error};
216 }
217  
218 12 ▶sub crc16 {
Subroutine crc16: called CC=3 SCAR=11.3
Pod documented
219 12 my $data = shift;
220  
221 12 ▶ unless ( utf8::downgrade( $data, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (utf8::downgrade($data, 1)) 0 12 12
222 0 utf8::encode($data);
223 }
224  
225 12 my $crc = 0;
226 12 foreach my $char ( split //, $data ) {
227 49 $crc = ( $crc << 8 & 0xff00 )
228 ^ $CRC16_TAB[ ( ( $crc >> 8 ) ^ ord($char) ) & 0x00ff ];
229 }
230  
231 12 return $crc;
232 }
233  
234 11 ▶sub hash_slot {
Subroutine hash_slot: called CC=3 SCAR=11.0
Pod documented
235 11 my $hashtag = shift;
236  
237 11 ▶ if ( $hashtag =~ m/\{([^}]*?)\}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hashtag =~ /\{([^}]*?)\}/) 2 9 11
238 2 ▶ if ( length $1 > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $1 > 0) 1 1 2
239 1 $hashtag = $1;
240 }
241 }
242  
243 11 return crc16($hashtag) % MAX_SLOTS;
244 }
245  
246 4 ▶sub _init {
Subroutine _init: called CC=1 SCAR=0.4
247 4 my $self = shift;
248  
249 4 $self->{_init_state} = S_IN_PROGRESS;
250 4 undef $self->{_refresh_timer};
251  
252 4 weaken($self);
253  
254 $self->_discover_cluster(
255 2 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=13.7
256 2 my $err = $_[1];
257  
258 2 ▶ if ( defined $err ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 0 2 2
259 0 $self->{_init_state} = S_NEED_DO;
260  
261 0 $self->{_ready} = 0;
262 0 $self->_abort($err);
263  
264 0 return;
265 }
266  
267 2 $self->{_init_state} = S_DONE;
268  
269 2 $self->{_ready} = 1;
270 2 $self->_process_input_queue;
271  
272 2 ▶ if ( $self->{refresh_interval} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"refresh_interval"} > 0) 2 0 2
273 $self->{_refresh_timer} = AE::timer(
274 $self->{refresh_interval}, 0,
275 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
276 0 $self->{_init_state} = S_NEED_DO;
277 0 $self->{_ready} = 0;
278 }
279 2 );
280 }
281 }
282 4 );
283  
284 4 return;
285 }
286  
287 4 ▶sub _discover_cluster {
Subroutine _discover_cluster: called CC=4 SCAR=15.0
288 4 my $self = shift;
289 4 my $cb = shift;
290  
291 4 my $nodes;
292  
293 4 ▶ if ( defined $self->{_slots} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_slots'}) { } 0 4 4
294 0 $nodes = $self->_nodes( undef, $self->{allow_slaves} );
295 }
296 else {
297 4 my %nodes_pool;
298  
299 4 foreach my $node_params ( @{ $self->{startup_nodes} } ) {
300 12 my $hostport = "$node_params->{host}:$node_params->{port}";
301  
302 12 ▶ unless ( defined $nodes_pool{$hostport} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nodes_pool{$hostport}) 12 0 12
303 $nodes_pool{$hostport} = $self->_new_node(
304 12 $node_params->{host}, $node_params->{port} );
305 }
306 }
307  
308 4 $self->{_nodes_pool} = \%nodes_pool;
309 4 $nodes = [ keys %nodes_pool ];
310 }
311  
312 4 weaken($self);
313  
314 $self->_execute(
315 { name => 'cluster_state',
316 args => [],
317  
318 2 ▶ on_reply => sub {
Subroutine __ANON__: called CC=2 SCAR=8.4
319 2 my $err = $_[1];
320  
321 2 ▶ if ( defined $err ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 0 2 2
322 0 $cb->( undef, $err );
323 0 return;
324 }
325  
326 $self->_execute(
327 { name => 'cluster_slots',
328 args => [],
329  
330 2 ▶ on_reply => sub {
Subroutine __ANON__: called CC=3 SCAR=13.1
331 2 my $slots = shift;
332 2 my $err = shift;
333  
334 2 ▶ if ( defined $err ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 0 2 2
335 0 $cb->( undef, $err );
336 0 return;
337 }
338  
339 2 ▶ unless ( @{$slots} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$slots;}) 0 2 2
340 0 $err = _new_error( 'ERR Returned empty list of slots',
341 E_UNEXPECTED_DATA );
342 0 $cb->( undef, $err );
343  
344 0 return;
345 }
346  
347 $self->_prepare_nodes( $slots,
348 2 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
349 2 ▶ unless ( defined $self->{_commands} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_commands"}) 2 0 2
350 2 $self->_load_commands($cb);
351 2 return;
352 }
353  
354 0 $cb->();
355 }
356 2 );
357 }
358 },
359 2 $nodes
360 );
361 }
362 },
363 4 $nodes
364 );
365  
366 4 return;
367 }
368  
369 2 ▶sub _prepare_nodes {
Subroutine _prepare_nodes: called CC=9 SCAR=22.1
370 2 my $self = shift;
371 2 my $slots_raw = shift;
372 2 my $cb = shift;
373  
374 2 my %nodes_pool;
375 my @slots;
376 2 my @masters_nodes;
377 2 my @slave_nodes;
378  
379 2 my $nodes_pool_old = $self->{_nodes_pool};
380  
381 2 foreach my $range ( @{$slots_raw} ) {
382 8 my $range_start = shift @{$range};
383 8 my $range_end = shift @{$range};
384  
385 8 my @nodes;
386 8 my $node_cnt = 0;
387  
388 8 foreach my $node_info ( @{$range} ) {
389 20 my $hostport = "$node_info->[0]:$node_info->[1]";
390  
391 20 ▶ unless ( defined $nodes_pool{$hostport} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nodes_pool{$hostport}) 14 6 20
392 14 ▶ if ( defined $nodes_pool_old->{$hostport} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nodes_pool_old->{$hostport}) { } 6 8 14
393 6 $nodes_pool{$hostport} = delete $nodes_pool_old->{$hostport};
394 }
395 else {
396 8 $nodes_pool{$hostport} = $self->_new_node( @{$node_info}[ 0, 1 ] );
397 }
398  
399 14 ▶ if (++$node_cnt > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$node_cnt > 1) { } 8 6 14
400 8 push( @slave_nodes, $hostport );
401 }
402 else {
403 6 push( @masters_nodes, $hostport );
404 }
405 }
406  
407 20 push( @nodes, $hostport );
408 }
409  
410 8 push( @slots, [ $range_start, $range_end, \@nodes ] );
411 }
412  
413 2 @slots = sort { $a->[0] <=> $b->[0] } @slots;
414  
415 2 foreach my $node ( values %{$nodes_pool_old} ) {
416 0 $node->disconnect;
417 }
418  
419 2 $self->{_nodes_pool} = \%nodes_pool;
420 2 $self->{_nodes} = [ keys %nodes_pool ];
421 2 $self->{_master_nodes} = \@masters_nodes;
422 2 $self->{_slots} = \@slots;
423  
424 2 ▶ if ( $self->{allow_slaves} && @slave_nodes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"allow_slaves"} and @slave_nodes) 2 0 2
Condition33%1/3
$self->{"allow_slaves"} and @slave_nodes
!ll&&!rl&&r
002
MC/DC0%0/2
$self->{"allow_slaves"} and @slave_nodes
$self->{"allow_slaves"} @slave_nodes
Truth table33%1/3
A and B
A= $self->{"allow_slaves"}B= @slave_nodes
ABresult
0X0
100
111
425 2 $self->_prepare_slaves( \@slave_nodes, $cb );
426 2 return;
427 }
428  
429 0 $cb->();
430  
431 0 return;
432 }
433  
434 2 ▶sub _prepare_slaves {
Subroutine _prepare_slaves: called CC=2 SCAR=6.9
435 2 my $self = shift;
436 2 my $slave_nodes = shift;
437 2 my $cb = shift;
438  
439 2 my $reply_cnt = scalar @{$slave_nodes};
440  
441 my $cmd = {
442 name => 'readonly',
443 args => [],
444  
445 8 ▶ on_reply => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
446 8 ▶ return if --$reply_cnt > 0;
Branch: 1 branch
BranchTrueFalseTotal
if --$reply_cnt > 0 6 2 8
447 2 $cb->();
448 }
449 2 };
450  
451 2 foreach my $hostport ( @{$slave_nodes} ) {
452 8 $self->_execute( $cmd, [ $hostport ] );
453 }
454  
455 2 return;
456 }
457  
458 2 ▶sub _load_commands {
Subroutine _load_commands: called CC=1 SCAR=0.0
459 2 my $self = shift;
460 2 my $cb = shift;
461  
462 2 my $nodes = $self->_nodes( undef, $self->{allow_slaves} );
463  
464 2 weaken($self);
465  
466 $self->_execute(
467 { name => 'command',
468 args => [],
469  
470 2 ▶ on_reply => sub {
Subroutine __ANON__: called CC=6 SCAR=18.2
471 2 my $commands_raw = shift;
472 2 my $err = shift;
473  
474 2 ▶ if ( defined $err ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 0 2 2
475 0 $cb->( undef, $err);
476 0 return;
477 }
478  
479 2 my %commands = %PREDEFINED_CMDS;
480  
481 2 foreach my $cmd_raw ( @{$commands_raw} ) {
482 10 my $kwd = lc( $cmd_raw->[0] );
483  
484 10 ▶ next if exists $commands{$kwd};
Branch: 1 branch
BranchTrueFalseTotal
if exists $commands{$kwd} 0 10 10
485  
486 10 my $readonly = 0;
487 10 foreach my $flag ( @{ $cmd_raw->[2] } ) {
488 16 ▶ if ( $flag eq 'readonly' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag eq "readonly") 4 12 16
489 4 $readonly = 1;
490 4 last;
491 }
492 }
493  
494 10 $commands{$kwd} = {
495 readonly => $readonly,
496 key_pos => $cmd_raw->[3],
497 };
498 }
499  
500 2 $self->{_commands} = \%commands;
501  
502 2 $cb->();
503 }
504 },
505 2 $nodes
506 );
507  
508 2 return;
509 }
510  
511 20 ▶sub _new_node {
Subroutine _new_node: called CC=1 SCAR=0.0
512 20 my $self = shift;
513 20 my $host = shift;
514 20 my $port = shift;
515  
516 return AnyEvent::RipeRedis->new(
517 20 %{ $self->{_node_params} },
518 host => $host,
519 port => $port,
520 lazy => 1,
521 on_connect => $self->_create_on_node_connect( $host, $port ),
522 on_disconnect => $self->_create_on_node_disconnect( $host, $port ),
523 on_error => $self->_create_on_node_error( $host, $port ),
524 );
525 }
526  
527 20 ▶sub _create_on_node_connect {
Subroutine _create_on_node_connect: called CC=1 SCAR=0.8
528 20 my $self = shift;
529 20 my $host = shift;
530 20 my $port = shift;
531  
532 20 weaken($self);
533  
534 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
535 0 ▶ if ( defined $self->{on_node_connect} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"on_node_connect"}) 0 0 0
536 0 $self->{on_node_connect}->( $host, $port );
537 }
538 20 };
539 }
540  
541 20 ▶sub _create_on_node_disconnect {
Subroutine _create_on_node_disconnect: called CC=1 SCAR=0.8
542 20 my $self = shift;
543 20 my $host = shift;
544 20 my $port = shift;
545  
546 20 weaken($self);
547  
548 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
549 0 ▶ if ( defined $self->{on_node_disconnect} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"on_node_disconnect"}) 0 0 0
550 0 $self->{on_node_disconnect}->( $host, $port );
551 }
552 20 };
553 }
554  
555 20 ▶sub _create_on_node_error {
Subroutine _create_on_node_error: called CC=1 SCAR=1.2
556 20 my $self = shift;
557 20 my $host = shift;
558 20 my $port = shift;
559  
560 20 weaken($self);
561  
562 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
563 0 my $err = shift;
564  
565 0 ▶ if ( defined $self->{on_node_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"on_node_error"}) 0 0 0
566 0 $self->{on_node_error}->( $err, $host, $port );
567 }
568 20 };
569 }
570  
571 9 ▶sub _prepare {
Subroutine _prepare: called CC=6 SCAR=18.1
572 9 my $self = shift;
573 9 my $cmd_name = shift;
574 9 my $args = shift;
575  
576 9 weaken($self);
577  
578 9 my $cbs;
579 9 ▶ if ( ref( $args->[-1] ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->[-1] eq 'HASH') { } 1 8 9
580 1 $cbs = pop @{$args};
581 }
582 else {
583 8 $cbs = {};
584 8 ▶ if ( ref( $args->[-1] ) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->[-1] eq "CODE") 7 1 8
585 7 ▶ if ( exists $SUB_CMDS{$cmd_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $SUB_CMDS{$cmd_name}) { } 0 7 7
586 0 $cbs->{on_message} = pop @{$args};
587 }
588 else {
589 7 $cbs->{on_reply} = pop @{$args};
590 }
591 }
592 }
593  
594 my @kwds
595 9 ▶ = $cmd_name eq 'eval_cached'
Branch: 1 branch
BranchTrueFalseTotal
$cmd_name eq 'eval_cached' ? : 0 9 9
596 ? ('evalsha')
597 : split( m/_/, lc($cmd_name) );
598  
599 my $cmd = {
600 name => $cmd_name,
601 kwds => \@kwds,
602 args => $args,
603 9 %{$cbs},
604 };
605  
606 9 ▶ unless ( defined $cmd->{on_reply} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cmd->{"on_reply"}) 1 8 9
607 1 ▶ $cmd->{on_reply} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
608 1 my $err = $_[1];
609  
610 1 ▶ if ( defined $err ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 1 0 1
611 1 $self->{on_error}->($err);
612 1 return;
613 }
614 1 };
615 }
616  
617 9 return $cmd;
618 }
619  
620 11 ▶sub _route {
Subroutine _route: called CC=10 SCAR=24.3
621 11 my $self = shift;
622 11 my $cmd = shift;
623  
624 11 ▶ unless ( $self->{_ready} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_ready"}) 2 9 11
625 2 ▶ if ( $self->{_init_state} == S_NEED_DO ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_init_state"} == 1) 0 2 2
626 0 $self->_init;
627 }
628 2 push( @{ $self->{_input_queue} }, $cmd );
629  
630 2 return;
631 }
632  
633 9 my $key;
634 9 my $kwds = $cmd->{kwds};
635 9 my $args = $cmd->{args};
636 9 my $cmd_info = $self->{_commands}{ $kwds->[0] };
637  
638 9 ▶ if ( defined $cmd_info ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cmd_info) 8 1 9
639 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
640 7 $key = $args->[ $cmd_info->{key_pos} - scalar @{$kwds} ];
641 }
642 # Exception for EVAL and EVALSHA commands
643 ▶ 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
644 && $args->[1] > 0 )
645 {
646 0 $key = $args->[2];
647 }
648 }
649  
650 9 my $slot;
651 9 my $allow_slaves = $self->{allow_slaves};
652  
653 9 ▶ if ( defined $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 7 2 9
654 7 $slot = hash_slot($key);
655 7 ▶ $allow_slaves &&= $cmd_info->{readonly};
Condition66%2/3
$allow_slaves &&= $cmd_info->{"readonly"}
!ll&&!rl&&r
025
MC/DC50%1/2
$allow_slaves &&= $cmd_info->{"readonly"}
$allow_slaves $cmd_info->{"readonly"}
Truth table66%2/3
A &&= B
A= $allow_slavesB= $cmd_info->{"readonly"}
ABresult
0X0
100
111
656 }
657  
658 9 my $nodes = $self->_nodes( $slot, $allow_slaves );
659  
660 9 ▶ unless ( defined $nodes ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nodes) 0 9 9
661 0 my $err = _new_error(
662 'ERR Target node not found. Maybe not all slots are served',
663 E_OPRN_ERROR
664 );
665 0 ▶ AE::postpone { $cmd->{on_reply}->( undef, $err ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
666  
667 0 return;
668 }
669  
670 9 $self->_execute( $cmd, $nodes );
671  
672 9 return;
673 }
674  
675 29 ▶sub _execute {
Subroutine _execute: called CC=6 SCAR=18.9
676 29 my $self = shift;
677 29 my $cmd = shift;
678 29 my $nodes = shift;
679 29 my $node_index = shift;
680 29 ▶ my $fails_cnt = shift || 0;
Condition100%2/2
shift() || 0
l!l
425
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
681  
682 29 ▶ unless ( defined $node_index ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $node_index) { } 4 25 29
if (do { $node_index == scalar @{$nodes;} }) 3 1 4
683 25 $node_index = int( rand( scalar @{$nodes} ) );
684 }
685 0 elsif ( $node_index == scalar @{$nodes} ) {
686 3 $node_index = 0;
687 }
688 29 my $hostport = $nodes->[$node_index];
689 29 my $node = $self->{_nodes_pool}{$hostport};
690  
691 my $cmd_name = $cmd->{name} eq 'cluster_state'
692 ? 'cluster_info'
693 29 ▶ : $cmd->{name};
Branch: 1 branch
BranchTrueFalseTotal
$cmd->{'name'} eq 'cluster_state' ? : 4 25 29
694  
695 29 weaken($self);
696  
697 29 ▶ $node->execute( $cmd_name, @{ $cmd->{args} },
Branch: 1 branch
BranchTrueFalseTotal
defined $cmd->{'on_message'} ? : 0 29 29
698 27 ▶ { on_reply => sub {
Subroutine __ANON__: called CC=13 SCAR=29.9
699 27 my $reply = shift;
700 27 my $err = shift;
701  
702 27 ▶ if ( $cmd->{name} eq 'cluster_state' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd->{"name"} eq "cluster_state") 2 25 27
703 2 ▶ unless ( defined $err ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $err) 2 0 2
704 2 ▶ if ( $reply->{cluster_state} eq 'ok' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reply->{'cluster_state'} eq 'ok') { } 2 0 2
705 2 $reply = 1;
706 }
707 else {
708 0 $err = _new_error( 'CLUSTERDOWN The cluster is down',
709 E_CLUSTER_DOWN );
710 }
711 }
712 }
713  
714 27 ▶ if ( defined $err ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 8 19 27
715 8 my $err_code = $err->code;
716 8 my $nodes_pool = $self->{_nodes_pool};
717  
718 8 ▶ if ( $err_code == E_MOVED || $err_code == E_ASK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err_code == 26 or $err_code == 25) 0 8 8
Condition33%1/3
$err_code == 26 or $err_code == 25
l!l&&r!l&&!r
008
MC/DC0%0/2
$err_code == 26 or $err_code == 25
$err_code == 26 $err_code == 25
Truth table33%1/3
A or B
A= $err_code == 26B= $err_code == 25
ABresult
1X1
011
000
719 0 ▶ if ( $err_code == E_MOVED ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err_code == 26) 0 0 0
720 0 $self->{_init_state} = S_NEED_DO;
721 0 $self->{_ready} = 0;
722 }
723  
724 0 my ($fwd_hostport) = ( split( m/\s+/, $err->message ) )[2];
725  
726 0 ▶ unless ( defined $nodes_pool->{$fwd_hostport} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nodes_pool->{$fwd_hostport}) 0 0 0
727 0 my ( $host, $port ) = parse_hostport($fwd_hostport);
728 0 $nodes_pool->{$fwd_hostport} = $self->_new_node( $host, $port );
729 }
730  
731 0 $self->_execute( $cmd, [ $fwd_hostport ] );
732  
733 0 return;
734 }
735  
736 8 ▶ my $on_node_error = $cmd->{on_node_error} || $self->{on_node_error};
Condition100%3/3
$cmd->{'on_node_error'} || $self->{'on_node_error'}
l!l&&r!l&&!r
224
MC/DC100%2/2
$cmd->{'on_node_error'} || $self->{'on_node_error'}
$cmd->{'on_node_error'} $self->{'on_node_error'}
Truth table100%3/3
A || B
A= $cmd->{'on_node_error'}B= $self->{'on_node_error'}
ABresult
1X1
011
000
737 8 ▶ if ( defined $on_node_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $on_node_error) 4 4 8
738 4 my $node = $nodes_pool->{$hostport};
739 4 $on_node_error->( $err, $node->host, $node->port );
740 }
741  
742 8 ▶ if ( $err_code != E_CONN_CLOSED_BY_CLIENT
Branch: 1 branch
BranchTrueFalseTotal
if ($err_code != 5 and ++$fails_cnt < scalar @{$nodes;}) 4 4 8
Condition66%2/3
$err_code != 5 and ++$fails_cnt < scalar @{$nodes;}
!ll&&!rl&&r
044
MC/DC50%1/2
$err_code != 5 and ++$fails_cnt < scalar @{$nodes;}
$err_code != 5 ++$fails_cnt < scalar @{$nodes;}
Truth table66%2/3
A and B
A= $err_code != 5B= ++$fails_cnt < scalar @{$nodes;}
ABresult
0X0
100
111
743 8 && ++$fails_cnt < scalar @{$nodes} )
744 {
745 4 $self->_execute( $cmd, $nodes, ++$node_index, $fails_cnt );
746 4 return;
747 }
748  
749 4 $cmd->{on_reply}->( $reply, $err );
750  
751 4 return;
752 }
753  
754 19 $cmd->{on_reply}->($reply);
755 },
756  
757 defined $cmd->{on_message}
758 ? ( on_message => $cmd->{on_message} )
759 29 : (),
760 }
761 );
762  
763 29 return;
764 }
765  
766 15 ▶sub _nodes {
Subroutine _nodes: called CC=5 SCAR=16.1
767 15 my $self = shift;
768 15 my $slot = shift;
769 15 my $allow_slaves = shift;
770  
771 15 ▶ if ( defined $slot ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $slot) 9 6 15
772 18 ▶ my ($range) = bsearch {
Subroutine __ANON__: called CC=3 SCAR=11.2
773 18 ▶ $slot > $_->[1] ? -1 : $slot < $_->[0] ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
$slot > $_->[1] ? : 9 9 18
$slot < $_->[0] ? : 0 9 9
774 }
775 9 @{ $self->{_slots} };
776  
777 9 ▶ return unless defined $range;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $range 0 9 9
778  
779 9 ▶ return $allow_slaves
Branch: 1 branch
BranchTrueFalseTotal
$allow_slaves ? : 6 3 9
780 ? $range->[2]
781 : [ $range->[2][0] ];
782 }
783  
784 return $allow_slaves
785 ? $self->{_nodes}
786 6 ▶ : $self->{_master_nodes};
Branch: 1 branch
BranchTrueFalseTotal
$allow_slaves ? : 5 1 6
787 }
788  
789 2 ▶sub _process_input_queue {
Subroutine _process_input_queue: called CC=2 SCAR=6.9
790 2 my $self = shift;
791  
792 2 $self->{_temp_queue} = $self->{_input_queue};
793 2 $self->{_input_queue} = [];
794  
795 2 ▶ while ( my $cmd = shift @{ $self->{_temp_queue} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cmd = shift @{$$self{"_temp_queue"};}) 2 2 4
796 2 $self->_route($cmd);
797 }
798  
799 2 return;
800 }
801  
802 5 ▶sub _reset_internals {
Subroutine _reset_internals: called CC=1 SCAR=0.0
803 5 my $self = shift;
804  
805 5 $self->{_nodes_pool} = undef;
806 5 $self->{_nodes} = undef;
807 5 $self->{_master_nodes} = undef;
808 5 $self->{_slots} = undef;
809 5 $self->{_commands} = undef;
810 5 $self->{_init_state} = S_NEED_DO;
811 5 $self->{_refresh_timer} = undef;
812 5 $self->{_ready} = 0;
813  
814 5 return;
815 }
816  
817 0 ▶sub _abort {
Subroutine _abort: not called CC=5 SCAR=34.0
818 0 my $self = shift;
819 0 my $err = shift;
820  
821 0 my @queued_commands = $self->_queued_commands;
822  
823 0 $self->{_input_queue} = [];
824 0 $self->{_temp_queue} = [];
825  
826 0 ▶ if ( !defined $err && @queued_commands ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $err and @queued_commands) 0 0 0
Condition0%0/3
not defined $err and @queued_commands
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $err and @queued_commands
not defined $err @queued_commands
Truth table0%0/3
not(A) and B
A= not defined $errB= @queued_commands
ABresult
0X0
100
111
827 0 $err = _new_error( 'Connection closed by client prematurely',
828 E_CONN_CLOSED_BY_CLIENT );
829 }
830  
831 0 ▶ if ( defined $err ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 0 0 0
832 0 my $err_msg = $err->message;
833 0 my $err_code = $err->code;
834  
835 0 $self->{on_error}->($err);
836  
837 0 foreach my $cmd (@queued_commands) {
838 0 my $err = _new_error( qq{Operation "$cmd->{name}" aborted: $err_msg},
839 $err_code );
840  
841 0 $cmd->{on_reply}->( undef, $err );
842 }
843 }
844  
845 0 return;
846 }
847  
848 3 ▶sub _queued_commands {
Subroutine _queued_commands: called CC=1 SCAR=0.0
849 3 my $self = shift;
850  
851 return (
852 3 @{ $self->{_temp_queue} },
853 3 @{ $self->{_input_queue} },
854 );
855 }
856  
857 0 ▶sub _new_error {
Subroutine _new_error: not called CC=1 SCAR=6.9
858 0 return AnyEvent::RipeRedis::Error->new(@_);
859 }
860  
861 5 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
862 5 our $AUTOLOAD;
863 5 my $cmd_name = $AUTOLOAD;
864 5 $cmd_name =~ s/^.+:://;
865  
866 8 ▶ my $sub = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
867 8 my $self = shift;
868  
869 8 my $cmd = $self->_prepare( $cmd_name, [@_] );
870 8 $self->_route($cmd);
871  
872 8 return;
873 5 };
874  
875 5 do {
876 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
877 5 *{$cmd_name} = $sub;
878 };
879  
880 5 goto &{$sub};
881 }
882  
883 8 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.0
884 8 my $self = shift;
885  
886 8 ▶ if ( defined $self->{_input_queue} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_input_queue"}) 3 5 8
887 3 my @queued_commands = $self->_queued_commands;
888  
889 3 foreach my $cmd (@queued_commands) {
890 0 warn "Operation \"$cmd->{name}\" aborted:"
891 . " Client object destroyed prematurely.\n";
892 }
893 }
894  
895 8 return;
896 }
897  
898 7 1;
899 __END__
↑ Summary