File Coverage

blib/lib/AnyEvent/RipeRedis/Cluster.pm
Criterion Covered Total %
statement 378 441 85.7
branch 95 134 70.9
condition 16 29 55.1
subroutine 47 53 88.6
pod 8 8 100.0
total 544 665 81.8


line stmt bran cond sub pod time code
1             package AnyEvent::RipeRedis::Cluster;
2              
3 6     6   122081 use 5.008000;
  6         20  
4 6     6   28 use strict;
  6         12  
  6         111  
5 6     6   25 use warnings;
  6         14  
  6         148  
6 6     6   29 use base qw( Exporter );
  6         690  
  6         528  
7              
8             our $VERSION = '0.28';
9              
10 6     6   2116 use AnyEvent::RipeRedis;
  6         82732  
  6         202  
11 6     6   36 use AnyEvent::RipeRedis::Error;
  6         12  
  6         92  
12              
13 6     6   2828 use AnyEvent::Socket;
  6         98257  
  6         736  
14 6     6   3090 use List::MoreUtils qw( bsearch );
  6         42328  
  6         63  
15 6     6   4169 use Scalar::Util qw( looks_like_number weaken );
  6         14  
  6         321  
16 6     6   58 use Carp qw( croak );
  6         13  
  6         481  
17              
18             my %ERROR_CODES;
19              
20             BEGIN {
21 6     6   102 %ERROR_CODES = %AnyEvent::RipeRedis::Error::ERROR_CODES;
22 6         50 my @err_codes = keys %ERROR_CODES;
23 6         33 our @EXPORT_OK = ( @err_codes, qw( crc16 hash_slot ) );
24 6         175 our %EXPORT_TAGS = ( err_codes => \@err_codes );
25             }
26              
27             use constant {
28 6         19798 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 6     6   33 };
  6         12  
39              
40             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             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             my %SUB_CMDS = (
84             subscribe => 1,
85             psubscribe => 1,
86             );
87              
88              
89             sub new {
90 10     10 1 4455 my $class = shift;
91 10         32 my %params = @_;
92              
93 10         22 my $self = bless {}, $class;
94              
95 10 100       34 unless ( defined $params{startup_nodes} ) {
96 1         160 croak 'Startup nodes not specified';
97             }
98 9 100       34 unless ( ref( $params{startup_nodes} ) eq 'ARRAY' ) {
99 1         107 croak 'Startup nodes must be specified as array reference';
100             }
101 8 100       22 unless ( @{ $params{startup_nodes} } ) {
  8         30  
102 1         91 croak 'Specified empty list of startup nodes';
103             }
104              
105 7         33 $self->{startup_nodes} = $params{startup_nodes};
106 7         17 $self->{allow_slaves} = $params{allow_slaves};
107 7         13 $self->{lazy} = $params{lazy};
108 7         42 $self->refresh_interval( $params{refresh_interval} );
109              
110 5         11 $self->{on_node_connect} = $params{on_node_connect};
111 5         12 $self->{on_node_disconnect} = $params{on_node_disconnect};
112 5         8 $self->{on_node_error} = $params{on_node_error};
113 5         22 $self->on_error( $params{on_error} );
114              
115 5         10 my %node_params;
116 5         12 foreach my $name ( qw( password utf8 connection_timeout read_timeout
117             reconnect reconnect_interval handle_params ) )
118             {
119 35 100       83 next unless defined $params{$name};
120 3         5 $node_params{$name} = $params{$name};
121             }
122 5         13 $self->{_node_params} = \%node_params;
123              
124 5         21 $self->_reset_internals;
125 5         10 $self->{_input_queue} = [];
126 5         11 $self->{_temp_queue} = [];
127              
128 5 100       16 unless ( $self->{lazy} ) {
129 4         13 $self->_init;
130             }
131              
132 5         20 return $self;
133             }
134              
135             sub execute {
136 1     1 1 1073 my $self = shift;
137 1         2 my $cmd_name = shift;
138              
139 1         5 my $cmd = $self->_prepare( $cmd_name, [@_] );
140 1         4 $self->_route($cmd);
141              
142 1         2 return;
143             }
144              
145             sub disconnect {
146 0     0 1 0 my $self = shift;
147              
148 0         0 foreach my $node ( values %{ $self->{_nodes_pool} } ) {
  0         0  
149 0         0 $node->disconnect;
150             }
151 0         0 $self->_reset_internals;
152 0         0 $self->_abort;
153              
154 0         0 return;
155             }
156              
157             sub nodes {
158 4     4 1 5009 my $self = shift;
159 4         8 my $key = shift;
160 4         8 my $allow_slaves = shift;
161              
162 4 50       12 return unless defined $self->{_slots};
163              
164 4         6 my $slot;
165 4 100       11 if ( defined $key ) {
166 2         6 $slot = hash_slot($key);
167             }
168              
169 4         14 my $nodes = $self->_nodes( $slot, $allow_slaves );
170              
171             return wantarray
172 4         12 ? @{ $self->{_nodes_pool} }{ @{$nodes} }
  4         9  
173 4 50       9 : $self->{_nodes_pool}{ $nodes->[0] };
174             }
175              
176             sub refresh_interval {
177 14     14 1 1697 my $self = shift;
178              
179 14 100       40 if (@_) {
180 11         26 my $seconds = shift;
181              
182 11 100       32 if ( defined $seconds ) {
183 7 100 100     50 if ( !looks_like_number($seconds) || $seconds < 0 ) {
184 4         363 croak qq{"refresh_interval" must be a positive number};
185             }
186 3         8 $self->{refresh_interval} = $seconds;
187             }
188             else {
189 4         12 $self->{refresh_interval} = D_REFRESH_INTERVAL;
190             }
191             }
192              
193 10         26 return $self->{refresh_interval};
194             }
195              
196             sub on_error {
197 10     10 1 429 my $self = shift;
198              
199 10 100       29 if ( @_ ) {
200 7         13 my $on_error = shift;
201              
202 7 100       18 if ( defined $on_error ) {
203 2         5 $self->{on_error} = $on_error;
204             }
205             else {
206             $self->{on_error} = sub {
207 2     2   5 my $err = shift;
208 2         7 warn $err->message . "\n";
209 5         36 };
210             }
211             }
212              
213 10         27 return $self->{on_error};
214             }
215              
216             sub crc16 {
217 11     11 1 20 my $data = shift;
218              
219 11 50       36 unless ( utf8::downgrade( $data, 1 ) ) {
220 0         0 utf8::encode($data);
221             }
222              
223 11         20 my $crc = 0;
224 11         34 foreach my $char ( split //, $data ) {
225 39         89 $crc = ( $crc << 8 & 0xff00 )
226             ^ $CRC16_TAB[ ( ( $crc >> 8 ) ^ ord($char) ) & 0x00ff ];
227             }
228              
229 11         31 return $crc;
230             }
231              
232             sub hash_slot {
233 10     10 1 537 my $key = shift;
234              
235 10         17 my $hashtag = $key;
236              
237 10 100       33 if ( $key =~ m/\{([^}]*?)\}/ ) {
238 1 50       6 if ( length $1 > 0 ) {
239 1         4 $hashtag = $1;
240             }
241             }
242              
243 10         22 return crc16($hashtag) % MAX_SLOTS;
244             }
245              
246             sub _init {
247 4     4   7 my $self = shift;
248              
249 4         10 $self->{_init_state} = S_IN_PROGRESS;
250 4         6 undef $self->{_refresh_timer};
251              
252 4         16 weaken($self);
253              
254             $self->_discover_cluster(
255             sub {
256 2     2   4 my $err = $_[1];
257              
258 2 50       6 if ( defined $err ) {
259 0         0 $self->{_init_state} = S_NEED_DO;
260              
261 0         0 $self->{_ready} = 0;
262 0         0 $self->_abort($err);
263              
264 0         0 return;
265             }
266              
267 2         3 $self->{_init_state} = S_DONE;
268              
269 2         4 $self->{_ready} = 1;
270 2         5 $self->_process_input_queue;
271              
272 2 50       7 if ( $self->{refresh_interval} > 0 ) {
273             $self->{_refresh_timer} = AE::timer(
274             $self->{refresh_interval}, 0,
275             sub {
276 0         0 $self->{_init_state} = S_NEED_DO;
277 0         0 $self->{_ready} = 0;
278             }
279 2         19 );
280             }
281             }
282 4         23 );
283              
284 4         9 return;
285             }
286              
287             sub _discover_cluster {
288 4     4   8 my $self = shift;
289 4         7 my $cb = shift;
290              
291 4         6 my $nodes;
292              
293 4 50       11 if ( defined $self->{_slots} ) {
294 0         0 $nodes = $self->_nodes( undef, $self->{allow_slaves} );
295             }
296             else {
297 4         9 my %nodes_pool;
298              
299 4         8 foreach my $node_params ( @{ $self->{startup_nodes} } ) {
  4         11  
300 12         1110 my $hostport = "$node_params->{host}:$node_params->{port}";
301              
302 12 50       34 unless ( defined $nodes_pool{$hostport} ) {
303             $nodes_pool{$hostport} = $self->_new_node(
304 12         35 $node_params->{host}, $node_params->{port} );
305             }
306             }
307              
308 4         423 $self->{_nodes_pool} = \%nodes_pool;
309 4         15 $nodes = [ keys %nodes_pool ];
310             }
311              
312 4         14 weaken($self);
313              
314             $self->_execute(
315             { name => 'cluster_state',
316             args => [],
317              
318             on_reply => sub {
319 2     2   3 my $err = $_[1];
320              
321 2 50       7 if ( defined $err ) {
322 0         0 $cb->( undef, $err );
323 0         0 return;
324             }
325              
326             $self->_execute(
327             { name => 'cluster_slots',
328             args => [],
329              
330             on_reply => sub {
331 2         4 my $slots = shift;
332 2         3 my $err = shift;
333              
334 2 50       5 if ( defined $err ) {
335 0         0 $cb->( undef, $err );
336 0         0 return;
337             }
338              
339             $self->_prepare_nodes( $slots,
340             sub {
341 2 50       5 unless ( defined $self->{_commands} ) {
342 2         6 $self->_load_commands($cb);
343 2         8 return;
344             }
345              
346 0         0 $cb->();
347             }
348 2         14 );
349             }
350             },
351 2         18 $nodes
352             );
353             }
354             },
355 4         28 $nodes
356             );
357              
358 4         8 return;
359             }
360              
361             sub _prepare_nodes {
362 2     2   4 my $self = shift;
363 2         3 my $slots_raw = shift;
364 2         4 my $cb = shift;
365              
366 2         7 my %nodes_pool;
367             my @slots;
368 2         0 my @masters_nodes;
369 2         0 my @slave_nodes;
370              
371 2         3 my $nodes_pool_old = $self->{_nodes_pool};
372              
373 2         5 foreach my $range ( @{$slots_raw} ) {
  2         4  
374 8         14 my $range_start = shift @{$range};
  8         14  
375 8         12 my $range_end = shift @{$range};
  8         12  
376              
377 8         13 my @nodes;
378 8         12 my $is_master = 1;
379              
380 8         11 foreach my $node_info ( @{$range} ) {
  8         13  
381 20         43 my $hostport = "$node_info->[0]:$node_info->[1]";
382              
383 20 100       47 unless ( defined $nodes_pool{$hostport} ) {
384 14 100       31 if ( defined $nodes_pool_old->{$hostport} ) {
385 6         13 $nodes_pool{$hostport} = delete $nodes_pool_old->{$hostport};
386             }
387             else {
388 8         12 $nodes_pool{$hostport} = $self->_new_node( @{$node_info}[ 0, 1 ] );
  8         20  
389              
390 8 50       965 unless ($is_master) {
391 8         16 push( @slave_nodes, $hostport );
392             }
393             }
394              
395 14 100       52 if ($is_master) {
396 6         8 push( @masters_nodes, $hostport );
397 6         11 $is_master = 0;
398             }
399             }
400              
401 20         33 push( @nodes, $hostport );
402             }
403              
404 8         23 push( @slots, [ $range_start, $range_end, \@nodes ] );
405             }
406              
407 2         7 @slots = sort { $a->[0] <=> $b->[0] } @slots;
  8         18  
408              
409 2         4 foreach my $node ( values %{$nodes_pool_old} ) {
  2         6  
410 0         0 $node->disconnect;
411             }
412              
413 2         8 $self->{_nodes_pool} = \%nodes_pool;
414 2         7 $self->{_nodes} = [ keys %nodes_pool ];
415 2         5 $self->{_master_nodes} = \@masters_nodes;
416 2         4 $self->{_slots} = \@slots;
417              
418 2 50 33     11 if ( $self->{allow_slaves} && @slave_nodes ) {
419 2         8 $self->_prepare_slaves( \@slave_nodes, $cb );
420 2         15 return;
421             }
422              
423 0         0 $cb->();
424              
425 0         0 return;
426             }
427              
428             sub _prepare_slaves {
429 2     2   3 my $self = shift;
430 2         4 my $slave_nodes = shift;
431 2         3 my $cb = shift;
432              
433 2         5 my $reply_cnt = scalar @{$slave_nodes};
  2         4  
434              
435             my $cmd = {
436             name => 'readonly',
437             args => [],
438              
439             on_reply => sub {
440 8 100   8   25 return if --$reply_cnt > 0;
441 2         5 $cb->();
442             }
443 2         13 };
444              
445 2         3 foreach my $hostport ( @{$slave_nodes} ) {
  2         5  
446 8         20 $self->_execute( $cmd, [ $hostport ] );
447             }
448              
449 2         4 return;
450             }
451              
452             sub _load_commands {
453 2     2   4 my $self = shift;
454 2         3 my $cb = shift;
455              
456 2         6 my $nodes = $self->_nodes( undef, $self->{allow_slaves} );
457              
458 2         7 weaken($self);
459              
460             $self->_execute(
461             { name => 'command',
462             args => [],
463              
464             on_reply => sub {
465 2     2   4 my $commands_raw = shift;
466 2         3 my $err = shift;
467              
468 2 50       5 if ( defined $err ) {
469 0         0 $cb->( undef, $err);
470 0         0 return;
471             }
472              
473 2         10 my %commands = %PREDEFINED_CMDS;
474              
475 2         4 foreach my $cmd_raw ( @{$commands_raw} ) {
  2         5  
476 10         20 my $kwd = lc( $cmd_raw->[0] );
477              
478 10 50       20 next if exists $commands{$kwd};
479              
480 10         15 my $readonly = 0;
481 10         14 foreach my $flag ( @{ $cmd_raw->[2] } ) {
  10         17  
482 16 100       34 if ( $flag eq 'readonly' ) {
483 4         5 $readonly = 1;
484 4         6 last;
485             }
486             }
487              
488 10         26 $commands{$kwd} = {
489             readonly => $readonly,
490             key_pos => $cmd_raw->[3],
491             };
492             }
493              
494 2         5 $self->{_commands} = \%commands;
495              
496 2         5 $cb->();
497             }
498             },
499 2         12 $nodes
500             );
501              
502 2         4 return;
503             }
504              
505             sub _new_node {
506 20     20   28 my $self = shift;
507 20         33 my $host = shift;
508 20         29 my $port = shift;
509              
510             return AnyEvent::RipeRedis->new(
511 20         31 %{ $self->{_node_params} },
  20         67  
512             host => $host,
513             port => $port,
514             lazy => 1,
515             on_connect => $self->_create_on_node_connect( $host, $port ),
516             on_disconnect => $self->_create_on_node_disconnect( $host, $port ),
517             on_error => $self->_create_on_node_error( $host, $port ),
518             );
519             }
520              
521             sub _create_on_node_connect {
522 20     20   32 my $self = shift;
523 20         31 my $host = shift;
524 20         34 my $port = shift;
525              
526 20         51 weaken($self);
527              
528             return sub {
529 0 0   0   0 if ( defined $self->{on_node_connect} ) {
530 0         0 $self->{on_node_connect}->( $host, $port );
531             }
532 20         95 };
533             }
534              
535             sub _create_on_node_disconnect {
536 20     20   33 my $self = shift;
537 20         30 my $host = shift;
538 20         34 my $port = shift;
539              
540 20         58 weaken($self);
541              
542             return sub {
543 0 0   0   0 if ( defined $self->{on_node_disconnect} ) {
544 0         0 $self->{on_node_disconnect}->( $host, $port );
545             }
546 20         69 };
547             }
548              
549             sub _create_on_node_error {
550 20     20   34 my $self = shift;
551 20         33 my $host = shift;
552 20         31 my $port = shift;
553              
554 20         45 weaken($self);
555              
556             return sub {
557 0     0   0 my $err = shift;
558              
559 0 0       0 if ( defined $self->{on_node_error} ) {
560 0         0 $self->{on_node_error}->( $err, $host, $port );
561             }
562 20         85 };
563             }
564              
565             sub _prepare {
566 9     9   16 my $self = shift;
567 9         14 my $cmd_name = shift;
568 9         13 my $args = shift;
569              
570 9         30 weaken($self);
571              
572 9         14 my $cbs;
573 9 100       28 if ( ref( $args->[-1] ) eq 'HASH' ) {
574 1         3 $cbs = pop @{$args};
  1         2  
575             }
576             else {
577 8         12 $cbs = {};
578 8 100       25 if ( ref( $args->[-1] ) eq 'CODE' ) {
579 7 50       19 if ( exists $SUB_CMDS{$cmd_name} ) {
580 0         0 $cbs->{on_message} = pop @{$args};
  0         0  
581             }
582             else {
583 7         9 $cbs->{on_reply} = pop @{$args};
  7         19  
584             }
585             }
586             }
587              
588             my @kwds
589 9 50       38 = $cmd_name eq 'eval_cached'
590             ? ('evalsha')
591             : split( m/_/, lc($cmd_name) );
592              
593             my $cmd = {
594             name => $cmd_name,
595             kwds => \@kwds,
596             args => $args,
597 9         21 %{$cbs},
  9         32  
598             };
599              
600 9 100       30 unless ( defined $cmd->{on_reply} ) {
601             $cmd->{on_reply} = sub {
602 1     1   2 my $err = $_[1];
603              
604 1 50       3 if ( defined $err ) {
605 1         4 $self->{on_error}->($err);
606 1         27 return;
607             }
608 1         6 };
609             }
610              
611 9         24 return $cmd;
612             }
613              
614             sub _route {
615 11     11   17 my $self = shift;
616 11         16 my $cmd = shift;
617              
618 11 100       39 unless ( $self->{_ready} ) {
619 2 50       7 if ( $self->{_init_state} == S_NEED_DO ) {
620 0         0 $self->_init;
621             }
622 2         2 push( @{ $self->{_input_queue} }, $cmd );
  2         14  
623              
624 2         4 return;
625             }
626              
627 9         13 my $key;
628 9         18 my $kwds = $cmd->{kwds};
629 9         12 my $args = $cmd->{args};
630 9         22 my $cmd_info = $self->{_commands}{ $kwds->[0] };
631              
632 9 100       21 if ( defined $cmd_info ) {
633 8 100 33     29 if ( $cmd_info->{key_pos} > 0 ) {
    50          
634 7         10 $key = $args->[ $cmd_info->{key_pos} - scalar @{$kwds} ];
  7         15  
635             }
636             # Exception for EVAL and EVALSHA commands
637             elsif ( $cmd_info->{movablekeys}
638             && $args->[1] > 0 )
639             {
640 0         0 $key = $args->[2];
641             }
642             }
643              
644 9         16 my $slot;
645 9         13 my $allow_slaves = $self->{allow_slaves};
646              
647 9 100       20 if ( defined $key ) {
648 7         19 $slot = hash_slot($key);
649 7   66     33 $allow_slaves &&= $cmd_info->{readonly};
650             }
651              
652 9         21 my $nodes = $self->_nodes( $slot, $allow_slaves );
653 9         25 $self->_execute( $cmd, $nodes );
654              
655 9         19 return;
656             }
657              
658             sub _execute {
659 29     29   48 my $self = shift;
660 29         41 my $cmd = shift;
661 29         41 my $nodes = shift;
662 29         47 my $node_index = shift;
663 29   100     110 my $fails_cnt = shift || 0;
664              
665 29 100 66     62 unless ( defined $node_index ) {
666 25         33 $node_index = int( rand( scalar @{$nodes} ) );
  25         66  
667             }
668             elsif ( $node_index == scalar @{$nodes} ) {
669             $node_index = 0;
670             }
671 29         60 my $hostport = $nodes->[$node_index];
672 29         49 my $node = $self->{_nodes_pool}{$hostport};
673              
674             my $cmd_name = $cmd->{name} eq 'cluster_state'
675             ? 'cluster_info'
676 29 100       71 : $cmd->{name};
677              
678 29         75 weaken($self);
679              
680 29         292 $node->execute( $cmd_name, @{ $cmd->{args} },
681             { on_reply => sub {
682 27     27   10319 my $reply = shift;
683 27         44 my $err = shift;
684              
685 27 100       73 if ( $cmd->{name} eq 'cluster_state' ) {
686 2 50       7 unless ( defined $err ) {
687 2 50       6 if ( $reply->{cluster_state} eq 'ok' ) {
688 2         4 $reply = 1;
689             }
690             else {
691 0         0 $err = _new_error( 'CLUSTERDOWN The cluster is down',
692             E_CLUSTER_DOWN );
693             }
694             }
695             }
696              
697 27 100       60 if ( defined $err ) {
698 8         22 my $err_code = $err->code;
699 8         38 my $nodes_pool = $self->{_nodes_pool};
700              
701 8 50 33     37 if ( $err_code == E_MOVED || $err_code == E_ASK ) {
702 0 0       0 if ( $err_code == E_MOVED ) {
703 0         0 $self->{_init_state} = S_NEED_DO;
704 0         0 $self->{_ready} = 0;
705             }
706              
707 0         0 my ($fwd_hostport) = ( split( m/\s+/, $err->message ) )[2];
708              
709 0 0       0 unless ( defined $nodes_pool->{$fwd_hostport} ) {
710 0         0 my ( $host, $port ) = parse_hostport($fwd_hostport);
711 0         0 $nodes_pool->{$fwd_hostport} = $self->_new_node( $host, $port );
712             }
713              
714 0         0 $self->_execute( $cmd, [ $fwd_hostport ] );
715              
716 0         0 return;
717             }
718              
719 8   66     23 my $on_node_error = $cmd->{on_node_error} || $self->{on_node_error};
720 8 100       20 if ( defined $on_node_error ) {
721 4         8 my $node = $nodes_pool->{$hostport};
722 4         20 $on_node_error->( $err, $node->host, $node->port );
723             }
724              
725 8 100 66     483 if ( $err_code != E_CONN_CLOSED_BY_CLIENT
726 8         28 && ++$fails_cnt < scalar @{$nodes} )
727             {
728 4         13 $self->_execute( $cmd, $nodes, ++$node_index, $fails_cnt );
729 4         24 return;
730             }
731              
732 4         12 $cmd->{on_reply}->( $reply, $err );
733              
734 4         51 return;
735             }
736              
737 19         45 $cmd->{on_reply}->($reply);
738             },
739              
740             defined $cmd->{on_message}
741             ? ( on_message => $cmd->{on_message} )
742 29 50       48 : (),
743             }
744             );
745              
746 29         11669 return;
747             }
748              
749             sub _nodes {
750 15     15   25 my $self = shift;
751 15         21 my $slot = shift;
752 15         21 my $allow_slaves = shift;
753              
754 15 100       34 if ( defined $slot ) {
755             my ($range) = bsearch {
756 18 50   18   244 $slot > $_->[1] ? -1 : $slot < $_->[0] ? 1 : 0;
    100          
757             }
758 9         29 @{ $self->{_slots} };
  9         36  
759              
760 9 100       70 return $allow_slaves
761             ? $range->[2]
762             : [ $range->[2][0] ];
763             }
764              
765             return $allow_slaves
766             ? $self->{_nodes}
767 6 100       16 : $self->{_master_nodes};
768             }
769              
770             sub _process_input_queue {
771 2     2   3 my $self = shift;
772              
773 2         5 $self->{_temp_queue} = $self->{_input_queue};
774 2         7 $self->{_input_queue} = [];
775              
776 2         3 while ( my $cmd = shift @{ $self->{_temp_queue} } ) {
  4         12  
777 2         6 $self->_route($cmd);
778             }
779              
780 2         4 return;
781             }
782              
783             sub _reset_internals {
784 5     5   9 my $self = shift;
785              
786 5         12 $self->{_nodes_pool} = undef;
787 5         11 $self->{_nodes} = undef;
788 5         12 $self->{_master_nodes} = undef;
789 5         10 $self->{_slots} = undef;
790 5         12 $self->{_commands} = undef;
791 5         10 $self->{_init_state} = S_NEED_DO;
792 5         13 $self->{_refresh_timer} = undef;
793 5         12 $self->{_ready} = 0;
794              
795 5         10 return;
796             }
797              
798             sub _abort {
799 0     0   0 my $self = shift;
800 0         0 my $err = shift;
801              
802 0         0 my @queued_commands = $self->_queued_commands;
803              
804 0         0 $self->{_input_queue} = [];
805 0         0 $self->{_temp_queue} = [];
806              
807 0 0 0     0 if ( !defined $err && @queued_commands ) {
808 0         0 $err = _new_error( 'Connection closed by client prematurely.',
809             E_CONN_CLOSED_BY_CLIENT );
810             }
811              
812 0 0       0 if ( defined $err ) {
813 0         0 my $err_msg = $err->message;
814 0         0 my $err_code = $err->code;
815              
816 0         0 $self->{on_error}->($err);
817              
818 0         0 foreach my $cmd (@queued_commands) {
819 0         0 my $err = _new_error( qq{Operation "$cmd->{name}" aborted: $err_msg},
820             $err_code );
821              
822 0         0 $cmd->{on_reply}->( undef, $err );
823             }
824             }
825              
826 0         0 return;
827             }
828              
829             sub _queued_commands {
830 3     3   8 my $self = shift;
831              
832             return (
833 3         8 @{ $self->{_temp_queue} },
834 3         5 @{ $self->{_input_queue} },
  3         11  
835             );
836             }
837              
838             sub _new_error {
839 0     0   0 return AnyEvent::RipeRedis::Error->new(@_);
840             }
841              
842             sub AUTOLOAD {
843 5     5   6335 our $AUTOLOAD;
844 5         9 my $cmd_name = $AUTOLOAD;
845 5         20 $cmd_name =~ s/^.+:://;
846              
847             my $sub = sub {
848 8     8   5347 my $self = shift;
849              
850 8         27 my $cmd = $self->_prepare( $cmd_name, [@_] );
851 8         25 $self->_route($cmd);
852              
853 8         14 return;
854 5         19 };
855              
856 5         8 do {
857 6     6   50 no strict 'refs';
  6         16  
  6         816  
858 5         10 *{$cmd_name} = $sub;
  5         15  
859             };
860              
861 5         10 goto &{$sub};
  5         13  
862             }
863              
864             sub DESTROY {
865 8     8   1498 my $self = shift;
866              
867 8 100       34 if ( defined $self->{_input_queue} ) {
868 3         11 my @queued_commands = $self->_queued_commands;
869              
870 3         9 foreach my $cmd (@queued_commands) {
871 0         0 warn "Operation \"$cmd->{name}\" aborted:"
872             . " Client object destroyed prematurely.\n";
873             }
874             }
875              
876 8         62 return;
877             }
878              
879             1;
880             __END__