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