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