line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Copyright 2014 - present MongoDB, Inc. |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
4
|
|
|
|
|
|
|
# you may not use this file except in compliance with the License. |
5
|
|
|
|
|
|
|
# You may obtain a copy of the License at |
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software |
10
|
|
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
11
|
|
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12
|
|
|
|
|
|
|
# See the License for the specific language governing permissions and |
13
|
|
|
|
|
|
|
# limitations under the License. |
14
|
|
|
|
|
|
|
|
15
|
59
|
|
|
59
|
|
1058
|
use strict; |
|
59
|
|
|
|
|
195
|
|
|
59
|
|
|
|
|
1914
|
|
16
|
59
|
|
|
59
|
|
370
|
use warnings; |
|
59
|
|
|
|
|
157
|
|
|
59
|
|
|
|
|
2066
|
|
17
|
|
|
|
|
|
|
package MongoDB::_Topology; |
18
|
|
|
|
|
|
|
|
19
|
59
|
|
|
59
|
|
336
|
use version; |
|
59
|
|
|
|
|
124
|
|
|
59
|
|
|
|
|
401
|
|
20
|
|
|
|
|
|
|
our $VERSION = 'v2.2.0'; |
21
|
|
|
|
|
|
|
|
22
|
59
|
|
|
59
|
|
4594
|
use Moo; |
|
59
|
|
|
|
|
182
|
|
|
59
|
|
|
|
|
428
|
|
23
|
59
|
|
|
59
|
|
21779
|
use BSON; |
|
59
|
|
|
|
|
146
|
|
|
59
|
|
|
|
|
2933
|
|
24
|
59
|
|
|
59
|
|
416
|
use MongoDB::Error; |
|
59
|
|
|
|
|
155
|
|
|
59
|
|
|
|
|
6447
|
|
25
|
59
|
|
|
59
|
|
436
|
use MongoDB::Op::_Command; |
|
59
|
|
|
|
|
148
|
|
|
59
|
|
|
|
|
1729
|
|
26
|
59
|
|
|
59
|
|
22465
|
use MongoDB::_Platform; |
|
59
|
|
|
|
|
174
|
|
|
59
|
|
|
|
|
1690
|
|
27
|
59
|
|
|
59
|
|
416
|
use MongoDB::ReadPreference; |
|
59
|
|
|
|
|
138
|
|
|
59
|
|
|
|
|
1191
|
|
28
|
59
|
|
|
59
|
|
327
|
use MongoDB::_Constants; |
|
59
|
|
|
|
|
126
|
|
|
59
|
|
|
|
|
6344
|
|
29
|
59
|
|
|
59
|
|
25973
|
use MongoDB::_Link; |
|
59
|
|
|
|
|
236
|
|
|
59
|
|
|
|
|
2961
|
|
30
|
59
|
|
|
|
|
635
|
use MongoDB::_Types qw( |
31
|
|
|
|
|
|
|
Boolish |
32
|
|
|
|
|
|
|
BSONCodec |
33
|
|
|
|
|
|
|
CompressionType |
34
|
|
|
|
|
|
|
Document |
35
|
|
|
|
|
|
|
NonNegNum |
36
|
|
|
|
|
|
|
TopologyType |
37
|
|
|
|
|
|
|
ZlibCompressionLevel |
38
|
|
|
|
|
|
|
to_IxHash |
39
|
59
|
|
|
59
|
|
619
|
); |
|
59
|
|
|
|
|
142
|
|
40
|
59
|
|
|
|
|
517
|
use Types::Standard qw( |
41
|
|
|
|
|
|
|
CodeRef |
42
|
|
|
|
|
|
|
HashRef |
43
|
|
|
|
|
|
|
ArrayRef |
44
|
|
|
|
|
|
|
InstanceOf |
45
|
|
|
|
|
|
|
Num |
46
|
|
|
|
|
|
|
Str |
47
|
|
|
|
|
|
|
Maybe |
48
|
59
|
|
|
59
|
|
119755
|
); |
|
59
|
|
|
|
|
156
|
|
49
|
59
|
|
|
59
|
|
112419
|
use MongoDB::_Server; |
|
59
|
|
|
|
|
218
|
|
|
59
|
|
|
|
|
2429
|
|
50
|
59
|
|
|
59
|
|
529
|
use MongoDB::_Protocol; |
|
59
|
|
|
|
|
152
|
|
|
59
|
|
|
|
|
1376
|
|
51
|
59
|
|
|
59
|
|
326
|
use Config; |
|
59
|
|
|
|
|
133
|
|
|
59
|
|
|
|
|
2722
|
|
52
|
59
|
|
|
59
|
|
351
|
use List::Util qw/first max min/; |
|
59
|
|
|
|
|
177
|
|
|
59
|
|
|
|
|
4584
|
|
53
|
59
|
|
|
59
|
|
388
|
use Safe::Isa; |
|
59
|
|
|
|
|
134
|
|
|
59
|
|
|
|
|
7533
|
|
54
|
59
|
|
|
59
|
|
438
|
use Time::HiRes qw/time usleep/; |
|
59
|
|
|
|
|
140
|
|
|
59
|
|
|
|
|
424
|
|
55
|
|
|
|
|
|
|
|
56
|
59
|
|
|
59
|
|
8382
|
use namespace::clean; |
|
59
|
|
|
|
|
151
|
|
|
59
|
|
|
|
|
395
|
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
with $_ for qw( |
59
|
|
|
|
|
|
|
MongoDB::Role::_TopologyMonitoring |
60
|
|
|
|
|
|
|
); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
#--------------------------------------------------------------------------# |
63
|
|
|
|
|
|
|
# attributes |
64
|
|
|
|
|
|
|
#--------------------------------------------------------------------------# |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
has uri => ( |
67
|
|
|
|
|
|
|
is => 'ro', |
68
|
|
|
|
|
|
|
required => 1, |
69
|
|
|
|
|
|
|
isa => InstanceOf['MongoDB::_URI'], |
70
|
|
|
|
|
|
|
); |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
has min_server_version => ( |
73
|
|
|
|
|
|
|
is => 'ro', |
74
|
|
|
|
|
|
|
required => 1, |
75
|
|
|
|
|
|
|
isa => Str, |
76
|
|
|
|
|
|
|
); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
has max_wire_version => ( |
79
|
|
|
|
|
|
|
is => 'ro', |
80
|
|
|
|
|
|
|
required => 1, |
81
|
|
|
|
|
|
|
isa => Num, |
82
|
|
|
|
|
|
|
); |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
has min_wire_version => ( |
85
|
|
|
|
|
|
|
is => 'ro', |
86
|
|
|
|
|
|
|
required => 1, |
87
|
|
|
|
|
|
|
isa => Num, |
88
|
|
|
|
|
|
|
); |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
has credential => ( |
91
|
|
|
|
|
|
|
is => 'ro', |
92
|
|
|
|
|
|
|
required => 1, |
93
|
|
|
|
|
|
|
isa => InstanceOf['MongoDB::_Credential'], |
94
|
|
|
|
|
|
|
); |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# Required so it's passed explicitly, even if undef, to ensure it's wired |
97
|
|
|
|
|
|
|
# up correctly. |
98
|
|
|
|
|
|
|
has monitoring_callback => ( |
99
|
|
|
|
|
|
|
is => 'ro', |
100
|
|
|
|
|
|
|
required => 1, |
101
|
|
|
|
|
|
|
isa => Maybe[CodeRef], |
102
|
|
|
|
|
|
|
); |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
has compressors => ( |
105
|
|
|
|
|
|
|
is => 'ro', |
106
|
|
|
|
|
|
|
isa => ArrayRef[CompressionType], |
107
|
|
|
|
|
|
|
default => sub { [] }, |
108
|
|
|
|
|
|
|
); |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
has zlib_compression_level => ( |
111
|
|
|
|
|
|
|
is => 'ro', |
112
|
|
|
|
|
|
|
isa => ZlibCompressionLevel, |
113
|
|
|
|
|
|
|
default => sub { -1 }, |
114
|
|
|
|
|
|
|
); |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
has type => ( |
117
|
|
|
|
|
|
|
is => 'ro', |
118
|
|
|
|
|
|
|
writer => '_set_type', |
119
|
|
|
|
|
|
|
default => 'Unknown', |
120
|
|
|
|
|
|
|
isa => TopologyType, |
121
|
|
|
|
|
|
|
); |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
has app_name => ( |
124
|
|
|
|
|
|
|
is => 'ro', |
125
|
|
|
|
|
|
|
default => '', |
126
|
|
|
|
|
|
|
isa => Str, |
127
|
|
|
|
|
|
|
); |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
has replica_set_name => ( |
130
|
|
|
|
|
|
|
is => 'ro', |
131
|
|
|
|
|
|
|
default => '', |
132
|
|
|
|
|
|
|
writer => '_set_replica_set_name', # :-) |
133
|
|
|
|
|
|
|
isa => Str, |
134
|
|
|
|
|
|
|
); |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
has heartbeat_frequency_sec => ( |
137
|
|
|
|
|
|
|
is => 'ro', |
138
|
|
|
|
|
|
|
default => 60, |
139
|
|
|
|
|
|
|
isa => NonNegNum, |
140
|
|
|
|
|
|
|
); |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
has last_scan_time => ( |
143
|
|
|
|
|
|
|
is => 'ro', |
144
|
|
|
|
|
|
|
default => EPOCH, |
145
|
|
|
|
|
|
|
writer => '_set_last_scan_time', |
146
|
|
|
|
|
|
|
isa => Num, |
147
|
|
|
|
|
|
|
); |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
has local_threshold_sec => ( |
150
|
|
|
|
|
|
|
is => 'ro', |
151
|
|
|
|
|
|
|
default => 0.015, |
152
|
|
|
|
|
|
|
isa => Num, |
153
|
|
|
|
|
|
|
); |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
has logical_session_timeout_minutes => ( |
156
|
|
|
|
|
|
|
is => 'ro', |
157
|
|
|
|
|
|
|
default => undef, |
158
|
|
|
|
|
|
|
writer => '_set_logical_session_timeout_minutes', |
159
|
|
|
|
|
|
|
isa => Maybe [NonNegNum], |
160
|
|
|
|
|
|
|
); |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
has next_scan_time => ( |
163
|
|
|
|
|
|
|
is => 'ro', |
164
|
|
|
|
|
|
|
default => sub { time() }, |
165
|
|
|
|
|
|
|
writer => '_set_next_scan_time', |
166
|
|
|
|
|
|
|
isa => Num, |
167
|
|
|
|
|
|
|
); |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
has socket_check_interval_sec => ( |
170
|
|
|
|
|
|
|
is => 'ro', |
171
|
|
|
|
|
|
|
default => 5, |
172
|
|
|
|
|
|
|
isa => Num, |
173
|
|
|
|
|
|
|
); |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
has server_selection_timeout_sec => ( |
176
|
|
|
|
|
|
|
is => 'ro', |
177
|
|
|
|
|
|
|
default => 60, |
178
|
|
|
|
|
|
|
isa => Num, |
179
|
|
|
|
|
|
|
); |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
has server_selection_try_once => ( |
182
|
|
|
|
|
|
|
is => 'ro', |
183
|
|
|
|
|
|
|
default => 1, |
184
|
|
|
|
|
|
|
isa => Boolish, |
185
|
|
|
|
|
|
|
); |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
has server_selector => ( |
188
|
|
|
|
|
|
|
is => 'ro', |
189
|
|
|
|
|
|
|
isa => Maybe[CodeRef], |
190
|
|
|
|
|
|
|
); |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
has ewma_alpha => ( |
193
|
|
|
|
|
|
|
is => 'ro', |
194
|
|
|
|
|
|
|
default => 0.2, |
195
|
|
|
|
|
|
|
isa => Num, |
196
|
|
|
|
|
|
|
); |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
has link_options => ( |
199
|
|
|
|
|
|
|
is => 'ro', |
200
|
|
|
|
|
|
|
default => sub { {} }, |
201
|
|
|
|
|
|
|
isa => HashRef, |
202
|
|
|
|
|
|
|
); |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
has bson_codec => ( |
205
|
|
|
|
|
|
|
is => 'ro', |
206
|
|
|
|
|
|
|
default => sub { BSON->new }, |
207
|
|
|
|
|
|
|
isa => BSONCodec, |
208
|
|
|
|
|
|
|
init_arg => undef, |
209
|
|
|
|
|
|
|
); |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
has number_of_seeds => ( |
212
|
|
|
|
|
|
|
is => 'lazy', |
213
|
|
|
|
|
|
|
builder => '_build_number_of_seeds', |
214
|
|
|
|
|
|
|
isa => Num, |
215
|
|
|
|
|
|
|
); |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
has max_election_id => ( |
218
|
|
|
|
|
|
|
is => 'rw', |
219
|
|
|
|
|
|
|
writer => '_set_max_election_id', |
220
|
|
|
|
|
|
|
); |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
has max_set_version => ( |
223
|
|
|
|
|
|
|
is => 'rw', |
224
|
|
|
|
|
|
|
isa => Maybe [Num], |
225
|
|
|
|
|
|
|
writer => '_set_max_set_version', |
226
|
|
|
|
|
|
|
); |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
# generated only once per _Topology, for performance |
229
|
|
|
|
|
|
|
has handshake_document => ( |
230
|
|
|
|
|
|
|
is => 'lazy', |
231
|
|
|
|
|
|
|
isa => Document, |
232
|
|
|
|
|
|
|
builder => '_build_handshake_document', |
233
|
|
|
|
|
|
|
); |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
# compatible wire protocol |
236
|
|
|
|
|
|
|
has is_compatible => ( |
237
|
|
|
|
|
|
|
is => 'ro', |
238
|
|
|
|
|
|
|
writer => '_set_is_compatible', |
239
|
|
|
|
|
|
|
isa => Boolish, |
240
|
|
|
|
|
|
|
); |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
has compatibility_error => ( |
243
|
|
|
|
|
|
|
is => 'ro', |
244
|
|
|
|
|
|
|
default => '', |
245
|
|
|
|
|
|
|
writer => '_set_compatibility_error', |
246
|
|
|
|
|
|
|
isa => Str, |
247
|
|
|
|
|
|
|
); |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
has wire_version_floor => ( |
250
|
|
|
|
|
|
|
is => 'ro', |
251
|
|
|
|
|
|
|
writer => '_set_wire_version_floor', |
252
|
|
|
|
|
|
|
default => 0, |
253
|
|
|
|
|
|
|
); |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
has wire_version_ceil => ( |
256
|
|
|
|
|
|
|
is => 'ro', |
257
|
|
|
|
|
|
|
writer => '_set_wire_version_ceil', |
258
|
|
|
|
|
|
|
default => 0, |
259
|
|
|
|
|
|
|
); |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
has current_primary => ( |
262
|
|
|
|
|
|
|
is => 'rwp', |
263
|
|
|
|
|
|
|
clearer => '_clear_current_primary', |
264
|
|
|
|
|
|
|
init_arg => undef, |
265
|
|
|
|
|
|
|
); |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
has stale => ( |
268
|
|
|
|
|
|
|
is => 'rwp', |
269
|
|
|
|
|
|
|
init_arg => undef, |
270
|
|
|
|
|
|
|
default => 1, |
271
|
|
|
|
|
|
|
); |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
# servers, links and rtt_ewma_sec are all hashes on server address |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
has servers => ( |
276
|
|
|
|
|
|
|
is => 'ro', |
277
|
|
|
|
|
|
|
default => sub { {} }, |
278
|
|
|
|
|
|
|
isa => HashRef[InstanceOf['MongoDB::_Server']], |
279
|
|
|
|
|
|
|
); |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
has _incompatible_servers => ( |
282
|
|
|
|
|
|
|
is => 'rw', |
283
|
|
|
|
|
|
|
default => sub { [] }, |
284
|
|
|
|
|
|
|
isa => ArrayRef[InstanceOf['MongoDB::_Server']], |
285
|
|
|
|
|
|
|
); |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
has links => ( |
288
|
|
|
|
|
|
|
is => 'ro', |
289
|
|
|
|
|
|
|
default => sub { {} }, |
290
|
|
|
|
|
|
|
isa => HashRef[InstanceOf['MongoDB::_Link']], |
291
|
|
|
|
|
|
|
); |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
has rtt_ewma_sec => ( |
294
|
|
|
|
|
|
|
is => 'ro', |
295
|
|
|
|
|
|
|
default => sub { {} }, |
296
|
|
|
|
|
|
|
isa => HashRef[Num], |
297
|
|
|
|
|
|
|
); |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
has cluster_time => ( |
300
|
|
|
|
|
|
|
is => 'rwp', |
301
|
|
|
|
|
|
|
isa => Maybe[Document], |
302
|
|
|
|
|
|
|
init_arg => undef, |
303
|
|
|
|
|
|
|
default => undef, |
304
|
|
|
|
|
|
|
); |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
sub update_cluster_time { |
307
|
0
|
|
|
0
|
0
|
0
|
my ( $self, $cluster_time ) = @_; |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
return unless $cluster_time && exists $cluster_time->{clusterTime} |
310
|
0
|
0
|
0
|
|
|
0
|
&& ref($cluster_time->{clusterTime}) eq 'BSON::Timestamp'; |
|
|
|
0
|
|
|
|
|
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
# Only update the cluster time if it is more recent than the current entry |
313
|
0
|
0
|
|
|
|
0
|
if ( !defined $self->cluster_time ) { |
|
|
0
|
|
|
|
|
|
314
|
0
|
|
|
|
|
0
|
$self->_set_cluster_time($cluster_time); |
315
|
|
|
|
|
|
|
} |
316
|
|
|
|
|
|
|
elsif ( $cluster_time->{'clusterTime'} > $self->cluster_time->{'clusterTime'} ) { |
317
|
0
|
|
|
|
|
0
|
$self->_set_cluster_time($cluster_time); |
318
|
|
|
|
|
|
|
} |
319
|
0
|
|
|
|
|
0
|
return; |
320
|
|
|
|
|
|
|
} |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
#--------------------------------------------------------------------------# |
323
|
|
|
|
|
|
|
# builders |
324
|
|
|
|
|
|
|
#--------------------------------------------------------------------------# |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
sub _build_number_of_seeds { |
327
|
2
|
|
|
2
|
|
20
|
my ($self) = @_; |
328
|
2
|
|
|
|
|
5
|
return scalar @{ $self->uri->hostids }; |
|
2
|
|
|
|
|
29
|
|
329
|
|
|
|
|
|
|
} |
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
sub _truncate_for_handshake { |
332
|
0
|
|
|
0
|
|
0
|
my $str = shift; |
333
|
0
|
|
|
|
|
0
|
return substr( $str, 0, 64 ); |
334
|
|
|
|
|
|
|
} |
335
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
sub _build_handshake_document { |
337
|
0
|
|
|
0
|
|
0
|
my ($self) = @_; |
338
|
0
|
|
|
|
|
0
|
my $driver_version_without_leading_v = substr( $VERSION, 1 ); |
339
|
|
|
|
|
|
|
|
340
|
0
|
0
|
|
|
|
0
|
return to_IxHash( |
341
|
|
|
|
|
|
|
[ |
342
|
|
|
|
|
|
|
( length( $self->app_name ) ? ( application => { name => $self->app_name } ) : () ), |
343
|
|
|
|
|
|
|
driver => to_IxHash( |
344
|
|
|
|
|
|
|
[ |
345
|
|
|
|
|
|
|
name => "MongoDB Perl Driver", |
346
|
|
|
|
|
|
|
version => $driver_version_without_leading_v, |
347
|
|
|
|
|
|
|
] |
348
|
|
|
|
|
|
|
), |
349
|
|
|
|
|
|
|
os => { type => _truncate_for_handshake(MongoDB::_Platform::os_type) }, |
350
|
|
|
|
|
|
|
platform => _truncate_for_handshake(MongoDB::_Platform::platform_details) |
351
|
|
|
|
|
|
|
] |
352
|
|
|
|
|
|
|
); |
353
|
|
|
|
|
|
|
} |
354
|
|
|
|
|
|
|
|
355
|
|
|
|
|
|
|
sub BUILD { |
356
|
330
|
|
|
330
|
0
|
10593
|
my ($self) = @_; |
357
|
|
|
|
|
|
|
|
358
|
330
|
100
|
|
|
|
1763
|
$self->publish_topology_opening |
359
|
|
|
|
|
|
|
if $self->monitoring_callback; |
360
|
|
|
|
|
|
|
|
361
|
330
|
100
|
|
|
|
4657
|
$self->publish_old_topology_desc |
362
|
|
|
|
|
|
|
if $self->monitoring_callback; |
363
|
330
|
|
|
|
|
2117
|
my $type = $self->type; |
364
|
330
|
|
|
|
|
617
|
my @addresses = @{ $self->uri->hostids }; |
|
330
|
|
|
|
|
1769
|
|
365
|
|
|
|
|
|
|
|
366
|
|
|
|
|
|
|
# clone bson codec to disable dt_type |
367
|
330
|
|
|
|
|
1919
|
$self->{bson_codec} = $self->bson_codec->clone( dt_type => undef ); |
368
|
|
|
|
|
|
|
|
369
|
330
|
100
|
|
|
|
38513
|
if ( my $set_name = $self->replica_set_name ) { |
370
|
47
|
100
|
66
|
|
|
330
|
if ( $type eq 'Single' || $type eq 'ReplicaSetNoPrimary' ) { |
|
|
50
|
|
|
|
|
|
371
|
|
|
|
|
|
|
# these are valid, so nothing to do here |
372
|
|
|
|
|
|
|
} |
373
|
|
|
|
|
|
|
elsif ( $type eq 'Unknown' ) { |
374
|
45
|
|
|
|
|
702
|
$self->_set_type('ReplicaSetNoPrimary'); |
375
|
|
|
|
|
|
|
} |
376
|
|
|
|
|
|
|
else { |
377
|
0
|
|
|
|
|
0
|
MongoDB::InternalError->throw( |
378
|
|
|
|
|
|
|
"deployment with set name '$set_name' may not be initialized as type '$type'"); |
379
|
|
|
|
|
|
|
} |
380
|
|
|
|
|
|
|
} |
381
|
|
|
|
|
|
|
|
382
|
330
|
50
|
66
|
|
|
3133
|
if ( $type eq 'Single' && @addresses > 1 ) { |
383
|
0
|
|
|
|
|
0
|
MongoDB::InternalError->throw( |
384
|
|
|
|
|
|
|
"topology type 'Single' cannot be used with multiple addresses: @addresses"); |
385
|
|
|
|
|
|
|
} |
386
|
|
|
|
|
|
|
|
387
|
330
|
|
|
|
|
1655
|
$self->_add_address_as_unknown($_) for @addresses; |
388
|
|
|
|
|
|
|
|
389
|
330
|
100
|
|
|
|
11503
|
$self->publish_new_topology_desc |
390
|
|
|
|
|
|
|
if $self->monitoring_callback; |
391
|
|
|
|
|
|
|
|
392
|
330
|
|
|
|
|
9672
|
return; |
393
|
|
|
|
|
|
|
} |
394
|
|
|
|
|
|
|
|
395
|
|
|
|
|
|
|
sub DEMOLISH { |
396
|
330
|
|
|
330
|
0
|
840134
|
my $self = shift; |
397
|
|
|
|
|
|
|
|
398
|
330
|
100
|
|
|
|
1793
|
$self->publish_topology_closing |
399
|
|
|
|
|
|
|
if $self->monitoring_callback; |
400
|
|
|
|
|
|
|
|
401
|
330
|
|
|
|
|
8940
|
return; |
402
|
|
|
|
|
|
|
} |
403
|
|
|
|
|
|
|
|
404
|
|
|
|
|
|
|
sub _check_for_uri_changes { |
405
|
53
|
|
|
53
|
|
192
|
my ($self) = @_; |
406
|
|
|
|
|
|
|
|
407
|
53
|
|
|
|
|
264
|
my $type = $self->type; |
408
|
|
|
|
|
|
|
return unless |
409
|
53
|
50
|
33
|
|
|
647
|
$type eq 'Sharded' |
410
|
|
|
|
|
|
|
or $type eq 'Unknown'; |
411
|
|
|
|
|
|
|
|
412
|
0
|
|
|
|
|
0
|
my @existing = @{ $self->uri->hostids }; |
|
0
|
|
|
|
|
0
|
|
413
|
|
|
|
|
|
|
|
414
|
|
|
|
|
|
|
my $options = { |
415
|
|
|
|
|
|
|
fallback_ttl_sec => $self->{heartbeat_frequency_sec}, |
416
|
0
|
|
|
|
|
0
|
}; |
417
|
|
|
|
|
|
|
|
418
|
0
|
0
|
|
|
|
0
|
if ($self->uri->check_for_changes($options)) { |
419
|
0
|
|
|
|
|
0
|
my %new = map { ($_, 1) } @{ $self->uri->hostids }; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
420
|
0
|
|
|
|
|
0
|
for my $address (@existing) { |
421
|
0
|
0
|
|
|
|
0
|
if (!$new{$address}) { |
422
|
0
|
|
|
|
|
0
|
$self->_remove_address($address); |
423
|
|
|
|
|
|
|
} |
424
|
|
|
|
|
|
|
else { |
425
|
0
|
|
|
|
|
0
|
delete $new{$address}; |
426
|
|
|
|
|
|
|
} |
427
|
|
|
|
|
|
|
} |
428
|
0
|
|
|
|
|
0
|
for my $address (keys %new) { |
429
|
0
|
|
|
|
|
0
|
$self->_add_address_as_unknown($address); |
430
|
|
|
|
|
|
|
} |
431
|
|
|
|
|
|
|
} |
432
|
|
|
|
|
|
|
} |
433
|
|
|
|
|
|
|
|
434
|
|
|
|
|
|
|
#--------------------------------------------------------------------------# |
435
|
|
|
|
|
|
|
# public methods |
436
|
|
|
|
|
|
|
#--------------------------------------------------------------------------# |
437
|
|
|
|
|
|
|
|
438
|
4483
|
|
|
4483
|
0
|
8504
|
sub all_servers { return values %{ $_[0]->servers } } |
|
4483
|
|
|
|
|
28930
|
|
439
|
|
|
|
|
|
|
|
440
|
0
|
|
|
0
|
0
|
0
|
sub all_data_bearing_servers { return grep { $_->is_data_bearing } $_[0]->all_servers } |
|
0
|
|
|
|
|
0
|
|
441
|
|
|
|
|
|
|
|
442
|
|
|
|
|
|
|
sub check_address { |
443
|
106
|
|
|
106
|
0
|
376
|
my ( $self, $address ) = @_; |
444
|
|
|
|
|
|
|
|
445
|
106
|
|
|
|
|
587
|
my $link = $self->links->{$address}; |
446
|
106
|
50
|
33
|
|
|
608
|
if ( $link && $link->is_connected ) { |
447
|
0
|
|
|
|
|
0
|
$self->_update_topology_from_link( $link, with_handshake => 0 ); |
448
|
|
|
|
|
|
|
} |
449
|
|
|
|
|
|
|
else { |
450
|
|
|
|
|
|
|
# initialize_link will call update_topology_from_link |
451
|
106
|
|
|
|
|
634
|
$self->_initialize_link($address); |
452
|
|
|
|
|
|
|
} |
453
|
|
|
|
|
|
|
|
454
|
106
|
|
|
|
|
365
|
return; |
455
|
|
|
|
|
|
|
} |
456
|
|
|
|
|
|
|
|
457
|
|
|
|
|
|
|
sub close_all_links { |
458
|
0
|
|
|
0
|
0
|
0
|
my ($self) = @_; |
459
|
0
|
|
|
|
|
0
|
delete $self->links->{ $_->address } for $self->all_servers; |
460
|
0
|
|
|
|
|
0
|
return; |
461
|
|
|
|
|
|
|
} |
462
|
|
|
|
|
|
|
|
463
|
|
|
|
|
|
|
sub _maybe_get_txn_error_labels_and_unpin_from { |
464
|
53
|
|
|
53
|
|
245
|
my $op = shift; |
465
|
53
|
50
|
33
|
|
|
1725
|
return () unless defined $op |
466
|
|
|
|
|
|
|
&& defined $op->session; |
467
|
0
|
0
|
|
|
|
0
|
if ( $op->session->_in_transaction_state( TXN_STARTING, TXN_IN_PROGRESS ) ) { |
|
|
0
|
|
|
|
|
|
468
|
0
|
|
|
|
|
0
|
$op->session->_unpin_address; |
469
|
0
|
|
|
|
|
0
|
return ( error_labels => [ TXN_TRANSIENT_ERROR_MSG ] ); |
470
|
|
|
|
|
|
|
} elsif ( $op->session->_in_transaction_state( TXN_COMMITTED ) ) { |
471
|
0
|
|
|
|
|
0
|
return ( error_labels => [ TXN_UNKNOWN_COMMIT_MSG ] ); |
472
|
|
|
|
|
|
|
} |
473
|
0
|
|
|
|
|
0
|
return (); |
474
|
|
|
|
|
|
|
} |
475
|
|
|
|
|
|
|
|
476
|
|
|
|
|
|
|
sub get_readable_link { |
477
|
0
|
|
|
0
|
0
|
0
|
my ( $self, $op ) = @_; |
478
|
0
|
|
|
|
|
0
|
$self->_check_for_uri_changes; |
479
|
|
|
|
|
|
|
|
480
|
0
|
0
|
|
|
|
0
|
my $read_pref = defined $op ? $op->read_preference : undef; |
481
|
|
|
|
|
|
|
|
482
|
0
|
0
|
|
|
|
0
|
my $mode = $read_pref ? lc $read_pref->mode : 'primary'; |
483
|
0
|
0
|
|
|
|
0
|
my $method = |
|
|
0
|
|
|
|
|
|
484
|
|
|
|
|
|
|
$self->type eq "Single" ? '_find_available_server' |
485
|
|
|
|
|
|
|
: $self->type eq "Sharded" ? '_find_readable_mongos_server' |
486
|
|
|
|
|
|
|
: "_find_${mode}_server"; |
487
|
|
|
|
|
|
|
|
488
|
0
|
0
|
0
|
|
|
0
|
if ( $mode eq 'primary' && $self->current_primary && $self->next_scan_time > time() ) |
|
|
|
0
|
|
|
|
|
489
|
|
|
|
|
|
|
{ |
490
|
0
|
|
|
|
|
0
|
my $link = $self->_get_server_link( $self->current_primary, $method ); |
491
|
0
|
0
|
|
|
|
0
|
return $link if $link; |
492
|
|
|
|
|
|
|
} |
493
|
|
|
|
|
|
|
|
494
|
0
|
|
|
|
|
0
|
while ( my $server = $self->_selection_timeout( $method, $read_pref ) ) { |
495
|
0
|
|
|
|
|
0
|
my $link = $self->_get_server_link( $server, $method, $read_pref ); |
496
|
0
|
0
|
|
|
|
0
|
if ($link) { |
497
|
|
|
|
|
|
|
$self->_set_current_primary($server) |
498
|
|
|
|
|
|
|
if $mode eq 'primary' |
499
|
|
|
|
|
|
|
&& ( $self->type eq "ReplicaSetWithPrimary" |
500
|
0
|
0
|
0
|
|
|
0
|
|| 1 == keys %{ $self->servers } ); |
|
|
|
0
|
|
|
|
|
501
|
0
|
|
|
|
|
0
|
return $link; |
502
|
|
|
|
|
|
|
} |
503
|
|
|
|
|
|
|
} |
504
|
|
|
|
|
|
|
|
505
|
0
|
0
|
|
|
|
0
|
my $rp = $read_pref ? $read_pref->as_string : 'primary'; |
506
|
|
|
|
|
|
|
|
507
|
0
|
|
|
|
|
0
|
MongoDB::SelectionError->throw( |
508
|
|
|
|
|
|
|
message => "No readable server available for matching read preference $rp. MongoDB server status:\n" |
509
|
|
|
|
|
|
|
. $self->_status_string, |
510
|
|
|
|
|
|
|
_maybe_get_txn_error_labels_and_unpin_from( $op ), |
511
|
|
|
|
|
|
|
); |
512
|
|
|
|
|
|
|
} |
513
|
|
|
|
|
|
|
|
514
|
|
|
|
|
|
|
sub get_specific_link { |
515
|
0
|
|
|
0
|
0
|
0
|
my ( $self, $address, $op ) = @_; |
516
|
0
|
|
|
|
|
0
|
$self->_check_for_uri_changes; |
517
|
|
|
|
|
|
|
|
518
|
0
|
|
|
|
|
0
|
my $server = $self->servers->{$address}; |
519
|
0
|
0
|
0
|
|
|
0
|
if ( $server && ( my $link = $self->_get_server_link($server) ) ) { |
520
|
0
|
|
|
|
|
0
|
return $link; |
521
|
|
|
|
|
|
|
} |
522
|
|
|
|
|
|
|
else { |
523
|
0
|
|
|
|
|
0
|
MongoDB::SelectionError->throw( |
524
|
|
|
|
|
|
|
message => "Server $address is no longer available", |
525
|
|
|
|
|
|
|
_maybe_get_txn_error_labels_and_unpin_from( $op ), |
526
|
|
|
|
|
|
|
); |
527
|
|
|
|
|
|
|
} |
528
|
|
|
|
|
|
|
} |
529
|
|
|
|
|
|
|
|
530
|
|
|
|
|
|
|
sub get_writable_link { |
531
|
53
|
|
|
53
|
0
|
979
|
my ( $self, $op ) = @_; |
532
|
53
|
|
|
|
|
366
|
$self->_check_for_uri_changes; |
533
|
|
|
|
|
|
|
|
534
|
53
|
50
|
33
|
|
|
539
|
my $method = |
535
|
|
|
|
|
|
|
( $self->type eq "Single" || $self->type eq "Sharded" ) |
536
|
|
|
|
|
|
|
? '_find_available_server' |
537
|
|
|
|
|
|
|
: "_find_primary_server"; |
538
|
|
|
|
|
|
|
|
539
|
|
|
|
|
|
|
|
540
|
53
|
50
|
33
|
|
|
422
|
if ( $self->current_primary && $self->next_scan_time > time() ) { |
541
|
0
|
|
|
|
|
0
|
my $link = $self->_get_server_link( $self->current_primary, $method ); |
542
|
0
|
0
|
|
|
|
0
|
return $link if $link; |
543
|
|
|
|
|
|
|
} |
544
|
|
|
|
|
|
|
|
545
|
53
|
|
|
|
|
360
|
while ( my $server = $self->_selection_timeout($method) ) { |
546
|
0
|
|
|
|
|
0
|
my $link = $self->_get_server_link( $server, $method ); |
547
|
0
|
0
|
|
|
|
0
|
if ($link) { |
548
|
|
|
|
|
|
|
$self->_set_current_primary($server) |
549
|
|
|
|
|
|
|
if $self->type eq "ReplicaSetWithPrimary" |
550
|
0
|
0
|
0
|
|
|
0
|
|| 1 == keys %{ $self->servers }; |
|
0
|
|
|
|
|
0
|
|
551
|
0
|
|
|
|
|
0
|
return $link; |
552
|
|
|
|
|
|
|
} |
553
|
|
|
|
|
|
|
} |
554
|
|
|
|
|
|
|
|
555
|
|
|
|
|
|
|
MongoDB::SelectionError->throw( |
556
|
53
|
|
|
|
|
464
|
message => "No writable server available. MongoDB server status:\n" . $self->_status_string, |
557
|
|
|
|
|
|
|
_maybe_get_txn_error_labels_and_unpin_from( $op ), |
558
|
|
|
|
|
|
|
); |
559
|
|
|
|
|
|
|
} |
560
|
|
|
|
|
|
|
|
561
|
|
|
|
|
|
|
# Marking a server unknown from outside the topology indicates an operational |
562
|
|
|
|
|
|
|
# error, so the last scan is set to EPOCH so that the next scan won't wait for |
563
|
|
|
|
|
|
|
# the scanning cooldown. |
564
|
|
|
|
|
|
|
sub mark_server_unknown { |
565
|
0
|
|
|
0
|
0
|
0
|
my ( $self, $server, $error, $no_cooldown ) = @_; |
566
|
0
|
|
0
|
|
|
0
|
$self->_reset_address_to_unknown( $server->address, $error, $no_cooldown // EPOCH ); |
567
|
0
|
|
|
|
|
0
|
return; |
568
|
|
|
|
|
|
|
} |
569
|
|
|
|
|
|
|
|
570
|
|
|
|
|
|
|
sub mark_stale { |
571
|
0
|
|
|
0
|
0
|
0
|
my ($self) = @_; |
572
|
0
|
|
|
|
|
0
|
$self->_set_stale(1); |
573
|
0
|
|
|
|
|
0
|
return; |
574
|
|
|
|
|
|
|
} |
575
|
|
|
|
|
|
|
|
576
|
|
|
|
|
|
|
sub scan_all_servers { |
577
|
1060
|
|
|
1060
|
0
|
11560
|
my ($self, $force) = @_; |
578
|
|
|
|
|
|
|
|
579
|
1060
|
|
|
|
|
5207
|
my ( $next, @ordinary, @to_check ); |
580
|
1060
|
|
|
|
|
10142
|
my $start_time = time; |
581
|
1060
|
50
|
|
|
|
10381
|
my $cooldown_time = $force ? $start_time : $start_time - COOLDOWN_SECS; |
582
|
|
|
|
|
|
|
|
583
|
|
|
|
|
|
|
# anything not updated since scan start is eligible for a check; when all servers |
584
|
|
|
|
|
|
|
# are updated, the loop terminates; Unknown servers aren't checked if |
585
|
|
|
|
|
|
|
# they are in the cooldown window since we don't want to wait the connect |
586
|
|
|
|
|
|
|
# timeout each attempt when they are unlikely to have changed status |
587
|
1060
|
|
|
|
|
4671
|
while (1) { |
588
|
|
|
|
|
|
|
@to_check = |
589
|
|
|
|
|
|
|
grep { |
590
|
1166
|
50
|
|
|
|
9266
|
$_->type eq 'Unknown' |
|
1166
|
|
|
|
|
93901
|
|
591
|
|
|
|
|
|
|
? !$_->updated_since($cooldown_time) |
592
|
|
|
|
|
|
|
: !$_->updated_since($start_time) |
593
|
|
|
|
|
|
|
} $self->all_servers; |
594
|
|
|
|
|
|
|
|
595
|
1166
|
100
|
|
|
|
8864
|
last unless @to_check; |
596
|
|
|
|
|
|
|
|
597
|
106
|
50
|
|
106
|
|
1454
|
if ( $next = first { $_->type eq 'RSPrimary' } @to_check ) { |
|
106
|
50
|
|
|
|
2365
|
|
|
|
50
|
|
|
|
|
|
598
|
0
|
|
|
|
|
0
|
$self->check_address( $next->address ); |
599
|
|
|
|
|
|
|
} |
600
|
106
|
|
|
106
|
|
3486
|
elsif ( $next = first { $_->type eq 'PossiblePrimary' } @to_check ) { |
601
|
0
|
|
|
|
|
0
|
$self->check_address( $next->address ); |
602
|
|
|
|
|
|
|
} |
603
|
106
|
50
|
|
|
|
2717
|
elsif ( @ordinary = grep { $_->type ne 'Unknown' && $_->type ne 'RSGhost' } @to_check ) { |
604
|
0
|
|
|
|
|
0
|
$self->_check_oldest_server(@ordinary); |
605
|
|
|
|
|
|
|
} |
606
|
|
|
|
|
|
|
else { |
607
|
106
|
|
|
|
|
1867
|
$self->_check_oldest_server(@to_check); |
608
|
|
|
|
|
|
|
} |
609
|
|
|
|
|
|
|
} |
610
|
|
|
|
|
|
|
|
611
|
1060
|
|
|
|
|
5515
|
my $now = time(); |
612
|
1060
|
|
|
|
|
27658
|
$self->_set_last_scan_time( $now ); |
613
|
1060
|
|
|
|
|
88466
|
$self->_set_next_scan_time( $now + $self->heartbeat_frequency_sec ); |
614
|
1060
|
|
|
|
|
47672
|
$self->_set_stale( 0 ); |
615
|
1060
|
|
|
|
|
6733
|
$self->_check_wire_versions; |
616
|
1060
|
|
|
|
|
3775
|
return; |
617
|
|
|
|
|
|
|
} |
618
|
|
|
|
|
|
|
|
619
|
|
|
|
|
|
|
sub status_struct { |
620
|
0
|
|
|
0
|
0
|
0
|
my ($self) = @_; |
621
|
0
|
|
|
|
|
0
|
my $status = { topology_type => $self->type, }; |
622
|
0
|
0
|
|
|
|
0
|
$status->{replica_set_name} = $self->replica_set_name if $self->replica_set_name; |
623
|
|
|
|
|
|
|
|
624
|
|
|
|
|
|
|
# convert from [sec, microsec] array to floating point |
625
|
0
|
|
|
|
|
0
|
$status->{last_scan_time} = $self->last_scan_time; |
626
|
|
|
|
|
|
|
|
627
|
0
|
|
|
|
|
0
|
my $rtt_hash = $self->rtt_ewma_sec; |
628
|
0
|
|
|
|
|
0
|
my $ss = $status->{servers} = []; |
629
|
0
|
|
|
|
|
0
|
for my $server ( $self->all_servers ) { |
630
|
0
|
|
|
|
|
0
|
my $addr = $server->address; |
631
|
0
|
|
|
|
|
0
|
my $server_struct = $server->status_struct; |
632
|
0
|
0
|
|
|
|
0
|
if ( defined $rtt_hash->{$addr} ) { |
633
|
0
|
|
|
|
|
0
|
$server_struct->{ewma_rtt_sec} = $rtt_hash->{$addr}; |
634
|
|
|
|
|
|
|
} |
635
|
0
|
|
|
|
|
0
|
push @$ss, $server_struct; |
636
|
|
|
|
|
|
|
} |
637
|
0
|
|
|
|
|
0
|
return $status; |
638
|
|
|
|
|
|
|
} |
639
|
|
|
|
|
|
|
|
640
|
|
|
|
|
|
|
#--------------------------------------------------------------------------# |
641
|
|
|
|
|
|
|
# private methods |
642
|
|
|
|
|
|
|
#--------------------------------------------------------------------------# |
643
|
|
|
|
|
|
|
|
644
|
|
|
|
|
|
|
sub _add_address_as_unknown { |
645
|
517
|
|
|
517
|
|
2476
|
my ( $self, $address, $last_update, $error ) = @_; |
646
|
517
|
100
|
|
|
|
1587
|
$error = $error ? "$error" : ""; |
647
|
517
|
|
|
|
|
1327
|
$error =~ s/ at \S+ line \d+.*//ms; |
648
|
|
|
|
|
|
|
|
649
|
517
|
100
|
|
|
|
2019
|
$self->publish_server_opening($address) |
650
|
|
|
|
|
|
|
if $self->monitoring_callback; |
651
|
|
|
|
|
|
|
|
652
|
517
|
|
100
|
|
|
16521
|
return $self->servers->{$address} = MongoDB::_Server->new( |
653
|
|
|
|
|
|
|
address => $address, |
654
|
|
|
|
|
|
|
last_update_time => $last_update || EPOCH, |
655
|
|
|
|
|
|
|
error => $error, |
656
|
|
|
|
|
|
|
); |
657
|
|
|
|
|
|
|
} |
658
|
|
|
|
|
|
|
|
659
|
|
|
|
|
|
|
sub _check_for_primary { |
660
|
121
|
|
|
121
|
|
191
|
my ($self) = @_; |
661
|
121
|
100
|
|
|
|
219
|
if ( 0 == $self->_primaries ) { |
662
|
17
|
|
|
|
|
328
|
$self->_set_type('ReplicaSetNoPrimary'); |
663
|
17
|
|
|
|
|
569
|
$self->_clear_current_primary; |
664
|
17
|
|
|
|
|
87
|
return 0; |
665
|
|
|
|
|
|
|
} |
666
|
104
|
|
|
|
|
908
|
return 1; |
667
|
|
|
|
|
|
|
} |
668
|
|
|
|
|
|
|
|
669
|
|
|
|
|
|
|
sub _check_oldest_server { |
670
|
106
|
|
|
106
|
|
433
|
my ( $self, @to_check ) = @_; |
671
|
|
|
|
|
|
|
|
672
|
|
|
|
|
|
|
my @ordered = |
673
|
106
|
|
|
|
|
525
|
map { $_->[0] } |
674
|
0
|
0
|
|
|
|
0
|
sort { $a->[1] <=> $b->[1] || rand() <=> rand() } # random if equal |
675
|
106
|
|
|
|
|
373
|
map { [ $_, $_->last_update_time ] } # ignore partial secs |
|
106
|
|
|
|
|
875
|
|
676
|
|
|
|
|
|
|
@to_check; |
677
|
|
|
|
|
|
|
|
678
|
106
|
|
|
|
|
997
|
$self->check_address( $ordered[0]->address ); |
679
|
|
|
|
|
|
|
|
680
|
106
|
|
|
|
|
856
|
return; |
681
|
|
|
|
|
|
|
} |
682
|
|
|
|
|
|
|
|
683
|
|
|
|
|
|
|
my $max_int32 = 2147483647; |
684
|
|
|
|
|
|
|
|
685
|
|
|
|
|
|
|
sub _check_wire_versions { |
686
|
1215
|
|
|
1215
|
|
4485
|
my ($self) = @_; |
687
|
|
|
|
|
|
|
|
688
|
1215
|
|
|
|
|
3541
|
my $compat = 1; |
689
|
1215
|
|
|
|
|
3492
|
my $min_seen = $max_int32; |
690
|
1215
|
|
|
|
|
3439
|
my $max_seen = 0; |
691
|
1215
|
|
|
|
|
4732
|
for my $server ( grep { $_->is_available } $self->all_servers ) { |
|
1372
|
|
|
|
|
36775
|
|
692
|
|
|
|
|
|
|
my ( $server_min_wire_version, $server_max_wire_version ) = |
693
|
219
|
|
|
|
|
5506
|
@{ $server->is_master }{qw/minWireVersion maxWireVersion/}; |
|
219
|
|
|
|
|
551
|
|
694
|
|
|
|
|
|
|
|
695
|
|
|
|
|
|
|
# set to 0 as could be undefined. 0 is the equivalent to missing, and |
696
|
|
|
|
|
|
|
# also kept as 0 for legacy compatibility. |
697
|
219
|
100
|
|
|
|
498
|
$server_max_wire_version = 0 unless defined $server_max_wire_version; |
698
|
219
|
100
|
|
|
|
427
|
$server_min_wire_version = 0 unless defined $server_min_wire_version; |
699
|
|
|
|
|
|
|
|
700
|
219
|
100
|
66
|
|
|
960
|
if ( $server_min_wire_version > $self->max_wire_version |
701
|
|
|
|
|
|
|
|| $server_max_wire_version < $self->min_wire_version ) { |
702
|
3
|
|
|
|
|
6
|
$compat = 0; |
703
|
3
|
|
|
|
|
7
|
push @{ $self->_incompatible_servers }, $server; |
|
3
|
|
|
|
|
49
|
|
704
|
|
|
|
|
|
|
} |
705
|
|
|
|
|
|
|
|
706
|
219
|
100
|
|
|
|
478
|
$min_seen = $server_max_wire_version if $server_max_wire_version < $min_seen; |
707
|
219
|
100
|
|
|
|
474
|
$max_seen = $server_max_wire_version if $server_max_wire_version > $max_seen; |
708
|
|
|
|
|
|
|
} |
709
|
1215
|
|
|
|
|
45210
|
$self->_set_is_compatible($compat); |
710
|
1215
|
|
|
|
|
44691
|
$self->_set_wire_version_floor($min_seen); |
711
|
1215
|
|
|
|
|
5539
|
$self->_set_wire_version_ceil($max_seen); |
712
|
|
|
|
|
|
|
|
713
|
1215
|
|
|
|
|
3218
|
return; |
714
|
|
|
|
|
|
|
} |
715
|
|
|
|
|
|
|
|
716
|
|
|
|
|
|
|
sub _update_ls_timeout_minutes { |
717
|
259
|
|
|
259
|
|
673
|
my ( $self, $new_server ) = @_; |
718
|
|
|
|
|
|
|
|
719
|
259
|
|
|
|
|
741
|
my @data_bearing_servers = grep { $_->is_data_bearing } $self->all_servers; |
|
396
|
|
|
|
|
8247
|
|
720
|
|
|
|
|
|
|
my $timeout = min map { |
721
|
|
|
|
|
|
|
# use -1 as a flag to prevent undefined warnings |
722
|
259
|
100
|
|
|
|
5680
|
defined $_->logical_session_timeout_minutes |
|
155
|
|
|
|
|
2618
|
|
723
|
|
|
|
|
|
|
? $_->logical_session_timeout_minutes |
724
|
|
|
|
|
|
|
: -1 |
725
|
|
|
|
|
|
|
} @data_bearing_servers; |
726
|
|
|
|
|
|
|
# min will return undef if the array is empty |
727
|
259
|
100
|
100
|
|
|
5706
|
if ( defined $timeout && $timeout < 0 ) { |
728
|
117
|
|
|
|
|
193
|
$timeout = undef; |
729
|
|
|
|
|
|
|
} |
730
|
259
|
|
|
|
|
4538
|
$self->_set_logical_session_timeout_minutes( $timeout ); |
731
|
259
|
|
|
|
|
13313
|
return; |
732
|
|
|
|
|
|
|
} |
733
|
|
|
|
|
|
|
|
734
|
|
|
|
|
|
|
sub _supports_sessions { |
735
|
0
|
|
|
0
|
|
0
|
my ( $self ) = @_; |
736
|
|
|
|
|
|
|
|
737
|
0
|
0
|
|
|
|
0
|
$self->scan_all_servers if $self->stale; |
738
|
|
|
|
|
|
|
|
739
|
0
|
|
|
|
|
0
|
my @servers = $self->all_servers; |
740
|
0
|
0
|
0
|
|
|
0
|
return if @servers == 1 && $servers[0]->type eq 'Standalone'; |
741
|
|
|
|
|
|
|
|
742
|
0
|
|
|
|
|
0
|
return defined $self->logical_session_timeout_minutes; |
743
|
|
|
|
|
|
|
} |
744
|
|
|
|
|
|
|
|
745
|
|
|
|
|
|
|
sub _supports_transactions { |
746
|
0
|
|
|
0
|
|
0
|
my ( $self ) = @_; |
747
|
|
|
|
|
|
|
|
748
|
0
|
0
|
|
|
|
0
|
return unless $self->_supports_sessions; |
749
|
0
|
0
|
|
|
|
0
|
return $self->_supports_mongos_pinning_transactions if $self->type eq 'Sharded'; |
750
|
0
|
0
|
|
|
|
0
|
return if $self->wire_version_ceil < 7; |
751
|
0
|
|
|
|
|
0
|
return 1; |
752
|
|
|
|
|
|
|
} |
753
|
|
|
|
|
|
|
|
754
|
|
|
|
|
|
|
# Seperated out so can be used in dispatch logic |
755
|
|
|
|
|
|
|
sub _supports_mongos_pinning_transactions { |
756
|
0
|
|
|
0
|
|
0
|
my ( $self ) = @_; |
757
|
|
|
|
|
|
|
|
758
|
|
|
|
|
|
|
# Separated out so that it doesnt return 1 for wire version 8 non sharded |
759
|
0
|
0
|
|
|
|
0
|
return if $self->wire_version_ceil < 8; |
760
|
|
|
|
|
|
|
# This extra sharded check is required so this test can be standalone |
761
|
0
|
0
|
|
|
|
0
|
return if $self->type ne 'Sharded'; |
762
|
0
|
|
|
|
|
0
|
return 1; |
763
|
|
|
|
|
|
|
} |
764
|
|
|
|
|
|
|
|
765
|
|
|
|
|
|
|
sub _check_staleness_compatibility { |
766
|
76
|
|
|
76
|
|
155
|
my ($self, $read_pref) = @_; |
767
|
76
|
50
|
|
|
|
207
|
my $max_staleness_sec = $read_pref ? $read_pref->max_staleness_seconds : -1; |
768
|
|
|
|
|
|
|
|
769
|
76
|
100
|
|
|
|
222
|
if ( $max_staleness_sec > 0 ) { |
770
|
37
|
100
|
|
|
|
143
|
if ( $self->wire_version_floor < 5 ) { |
771
|
5
|
|
|
|
|
65
|
MongoDB::ProtocolError->throw( |
772
|
|
|
|
|
|
|
"Incompatible wire protocol version. You tried to use max_staleness_seconds with one or more servers that don't support it." |
773
|
|
|
|
|
|
|
); |
774
|
|
|
|
|
|
|
} |
775
|
|
|
|
|
|
|
|
776
|
32
|
100
|
100
|
|
|
299
|
if ( |
|
|
|
100
|
|
|
|
|
777
|
|
|
|
|
|
|
( $self->type eq "ReplicaSetWithPrimary" || $self->type eq "ReplicaSetNoPrimary" ) |
778
|
|
|
|
|
|
|
&& $max_staleness_sec < max( SMALLEST_MAX_STALENESS_SEC, |
779
|
|
|
|
|
|
|
$self->heartbeat_frequency_sec + IDLE_WRITE_PERIOD_SEC |
780
|
|
|
|
|
|
|
) |
781
|
|
|
|
|
|
|
) |
782
|
|
|
|
|
|
|
{ |
783
|
3
|
|
|
|
|
43
|
MongoDB::UsageError->throw( |
784
|
|
|
|
|
|
|
"max_staleness_seconds must be at least 90 seconds and at least heartbeat_frequency (in secs) + 10 secs" |
785
|
|
|
|
|
|
|
); |
786
|
|
|
|
|
|
|
} |
787
|
|
|
|
|
|
|
} |
788
|
|
|
|
|
|
|
|
789
|
68
|
|
|
|
|
129
|
return; |
790
|
|
|
|
|
|
|
} |
791
|
|
|
|
|
|
|
|
792
|
|
|
|
|
|
|
sub _dump { |
793
|
0
|
|
|
0
|
|
0
|
my ($self) = @_; |
794
|
0
|
|
|
|
|
0
|
print $self->_status_string . "\n"; |
795
|
|
|
|
|
|
|
} |
796
|
|
|
|
|
|
|
|
797
|
|
|
|
|
|
|
sub _eligible { |
798
|
42
|
|
|
42
|
|
122
|
my ( $self, $read_pref, @candidates ) = @_; |
799
|
|
|
|
|
|
|
|
800
|
|
|
|
|
|
|
# must filter on max staleness first, so that the remaining servers |
801
|
|
|
|
|
|
|
# are checked against the list of tag_sets |
802
|
42
|
100
|
|
|
|
150
|
if ( $read_pref->max_staleness_seconds > 0 ) { |
803
|
19
|
|
|
|
|
137
|
@candidates = $self->_filter_fresh_servers($read_pref, @candidates ); |
804
|
18
|
100
|
|
|
|
74
|
return unless @candidates; |
805
|
|
|
|
|
|
|
}; |
806
|
|
|
|
|
|
|
|
807
|
|
|
|
|
|
|
# given a tag set list, if a tag set matches at least one |
808
|
|
|
|
|
|
|
# candidate, then all candidates matching that tag set are eligible |
809
|
40
|
100
|
|
|
|
142
|
if ( ! $read_pref->has_empty_tag_sets ) { |
810
|
27
|
|
|
|
|
47
|
for my $ts ( @{ $read_pref->tag_sets } ) { |
|
27
|
|
|
|
|
83
|
|
811
|
31
|
100
|
|
|
|
60
|
if ( my @ts_candidates = grep { $_->matches_tag_set($ts) } @candidates ) { |
|
58
|
|
|
|
|
160
|
|
812
|
18
|
|
|
|
|
67
|
return @ts_candidates; |
813
|
|
|
|
|
|
|
} |
814
|
|
|
|
|
|
|
} |
815
|
9
|
|
|
|
|
24
|
return; |
816
|
|
|
|
|
|
|
} |
817
|
|
|
|
|
|
|
|
818
|
13
|
|
|
|
|
47
|
return @candidates; |
819
|
|
|
|
|
|
|
} |
820
|
|
|
|
|
|
|
|
821
|
|
|
|
|
|
|
sub _filter_fresh_servers { |
822
|
19
|
|
|
19
|
|
56
|
my ($self, $read_pref, @candidates) = @_; |
823
|
|
|
|
|
|
|
|
824
|
|
|
|
|
|
|
# all values should be floating point seconds |
825
|
19
|
|
|
|
|
57
|
my $max_staleness_sec = $read_pref->max_staleness_seconds; |
826
|
19
|
|
|
|
|
51
|
my $heartbeat_frequency_sec = $self->heartbeat_frequency_sec; |
827
|
|
|
|
|
|
|
|
828
|
19
|
100
|
|
|
|
74
|
if ( $self->type eq 'ReplicaSetWithPrimary' ) { |
829
|
10
|
|
|
|
|
32
|
my ($primary) = $self->_primaries; |
830
|
|
|
|
|
|
|
|
831
|
|
|
|
|
|
|
# all values should be floating point seconds |
832
|
10
|
|
|
|
|
238
|
my $p_last_write_date = $primary->last_write_date; |
833
|
10
|
|
|
|
|
551
|
my $p_last_update_time = $primary->last_update_time; |
834
|
|
|
|
|
|
|
|
835
|
16
|
|
|
|
|
54
|
return map { $_->[0] } |
836
|
26
|
|
|
|
|
501
|
grep { $_->[1] <= $max_staleness_sec } |
837
|
|
|
|
|
|
|
map { |
838
|
10
|
|
|
|
|
30
|
[ |
839
|
26
|
|
|
|
|
959
|
$_, |
840
|
|
|
|
|
|
|
$p_last_write_date |
841
|
|
|
|
|
|
|
+ ( $_->last_update_time - $p_last_update_time ) |
842
|
|
|
|
|
|
|
- $_->last_write_date |
843
|
|
|
|
|
|
|
+ $heartbeat_frequency_sec |
844
|
|
|
|
|
|
|
] |
845
|
|
|
|
|
|
|
} @candidates; |
846
|
|
|
|
|
|
|
} |
847
|
|
|
|
|
|
|
else { |
848
|
23
|
|
|
|
|
50
|
my ($smax) = map { $_->[0] } |
849
|
20
|
|
|
|
|
481
|
sort { $b->[1] <=> $a->[1] } |
850
|
9
|
|
|
|
|
30
|
map { [ $_, $_->last_write_date ] } $self->_secondaries; |
|
23
|
|
|
|
|
1364
|
|
851
|
9
|
|
|
|
|
171
|
my $smax_last_write_date = $smax->last_write_date; |
852
|
|
|
|
|
|
|
|
853
|
15
|
|
|
|
|
65
|
return map { $_->[0] } |
854
|
23
|
|
|
|
|
98
|
grep { $_->[1] <= $max_staleness_sec } |
855
|
|
|
|
|
|
|
map { |
856
|
8
|
|
|
|
|
64
|
[ $_, $smax_last_write_date - $_->last_write_date + $heartbeat_frequency_sec ] |
|
23
|
|
|
|
|
437
|
|
857
|
|
|
|
|
|
|
} @candidates; |
858
|
|
|
|
|
|
|
} |
859
|
|
|
|
|
|
|
} |
860
|
|
|
|
|
|
|
|
861
|
|
|
|
|
|
|
# This works for reads and writes; for writes, $read_pref will be undef |
862
|
|
|
|
|
|
|
sub _find_available_server { |
863
|
1077
|
|
|
1077
|
|
7060
|
my ( $self, $read_pref, @candidates ) = @_; |
864
|
1077
|
100
|
|
|
|
4288
|
$self->_check_staleness_compatibility($read_pref) if $read_pref; |
865
|
1076
|
50
|
|
|
|
5789
|
push @candidates, $self->all_servers unless @candidates; |
866
|
1076
|
|
|
|
|
5333
|
my $selector = $self->server_selector; |
867
|
|
|
|
|
|
|
return $self->_get_server_in_latency_window( |
868
|
1076
|
100
|
|
|
|
5198
|
[ grep { $_->is_available } |
|
1131
|
|
|
|
|
24889
|
|
869
|
|
|
|
|
|
|
$selector ? $selector->(@candidates) : @candidates ] |
870
|
|
|
|
|
|
|
); |
871
|
|
|
|
|
|
|
} |
872
|
|
|
|
|
|
|
|
873
|
|
|
|
|
|
|
# This uses read preference to check for max staleness compatibility in |
874
|
|
|
|
|
|
|
# mongos, but otherwise read preference is ignored (mongos will pass it on) |
875
|
|
|
|
|
|
|
sub _find_readable_mongos_server { |
876
|
7
|
|
|
7
|
|
67
|
my ( $self, $read_pref, @candidates ) = @_; |
877
|
7
|
|
|
|
|
25
|
$self->_check_staleness_compatibility($read_pref); |
878
|
6
|
50
|
|
|
|
26
|
push @candidates, $self->all_servers unless @candidates; |
879
|
6
|
|
|
|
|
19
|
my $selector = $self->server_selector; |
880
|
|
|
|
|
|
|
return $self->_get_server_in_latency_window( |
881
|
6
|
50
|
|
|
|
21
|
[ grep { $_->is_available } |
|
12
|
|
|
|
|
516
|
|
882
|
|
|
|
|
|
|
$selector ? $selector->(@candidates) : @candidates ] |
883
|
|
|
|
|
|
|
); |
884
|
|
|
|
|
|
|
} |
885
|
|
|
|
|
|
|
|
886
|
|
|
|
|
|
|
sub _find_nearest_server { |
887
|
23
|
|
|
23
|
|
284
|
my ( $self, $read_pref, @candidates ) = @_; |
888
|
23
|
|
|
|
|
88
|
$self->_check_staleness_compatibility($read_pref); |
889
|
18
|
50
|
|
|
|
91
|
push @candidates, ( $self->_primaries, $self->_secondaries ) unless @candidates; |
890
|
18
|
|
|
|
|
198
|
my @suitable = $self->_eligible( $read_pref, @candidates ); |
891
|
17
|
|
|
|
|
57
|
my $selector = $self->server_selector; |
892
|
17
|
50
|
|
|
|
85
|
return $self->_get_server_in_latency_window( |
893
|
|
|
|
|
|
|
[ $selector ? $selector->(@suitable) : @suitable ] |
894
|
|
|
|
|
|
|
); |
895
|
|
|
|
|
|
|
} |
896
|
|
|
|
|
|
|
|
897
|
|
|
|
|
|
|
sub _find_primary_server { |
898
|
18
|
|
|
18
|
|
117
|
my ( $self, undef, @candidates ) = @_; |
899
|
18
|
50
|
|
|
|
63
|
return $self->current_primary |
900
|
|
|
|
|
|
|
if $self->current_primary; |
901
|
18
|
50
|
|
|
|
66
|
push @candidates, $self->all_servers unless @candidates; |
902
|
18
|
|
|
24
|
|
127
|
return first { $_->is_writable } @candidates; |
|
24
|
|
|
|
|
646
|
|
903
|
|
|
|
|
|
|
} |
904
|
|
|
|
|
|
|
|
905
|
|
|
|
|
|
|
sub _find_primarypreferred_server { |
906
|
8
|
|
|
8
|
|
91
|
my ( $self, $read_pref, @candidates ) = @_; |
907
|
8
|
|
|
|
|
29
|
$self->_check_staleness_compatibility($read_pref); |
908
|
7
|
|
100
|
|
|
26
|
return $self->_find_primary_server(@candidates) |
909
|
|
|
|
|
|
|
|| $self->_find_secondary_server( $read_pref, @candidates ); |
910
|
|
|
|
|
|
|
} |
911
|
|
|
|
|
|
|
|
912
|
|
|
|
|
|
|
sub _find_secondary_server { |
913
|
24
|
|
|
24
|
|
211
|
my ( $self, $read_pref, @candidates ) = @_; |
914
|
24
|
|
|
|
|
69
|
$self->_check_staleness_compatibility($read_pref); |
915
|
24
|
50
|
|
|
|
88
|
push @candidates, $self->_secondaries unless @candidates; |
916
|
24
|
|
|
|
|
238
|
my @suitable = $self->_eligible( $read_pref, @candidates ); |
917
|
24
|
|
|
|
|
70
|
my $selector = $self->server_selector; |
918
|
24
|
50
|
|
|
|
137
|
return $self->_get_server_in_latency_window( |
919
|
|
|
|
|
|
|
[ $selector ? $selector->(@suitable) : @suitable ] |
920
|
|
|
|
|
|
|
); |
921
|
|
|
|
|
|
|
} |
922
|
|
|
|
|
|
|
|
923
|
|
|
|
|
|
|
sub _find_secondarypreferred_server { |
924
|
11
|
|
|
11
|
|
111
|
my ( $self, $read_pref, @candidates ) = @_; |
925
|
11
|
|
|
|
|
42
|
$self->_check_staleness_compatibility($read_pref); |
926
|
11
|
|
100
|
|
|
31
|
return $self->_find_secondary_server( $read_pref, @candidates ) |
927
|
|
|
|
|
|
|
|| $self->_find_primary_server(@candidates); |
928
|
|
|
|
|
|
|
} |
929
|
|
|
|
|
|
|
|
930
|
|
|
|
|
|
|
sub _get_server_in_latency_window { |
931
|
1123
|
|
|
1123
|
|
15836
|
my ( $self, $servers ) = @_; |
932
|
1123
|
100
|
|
|
|
5680
|
return unless @$servers; |
933
|
52
|
100
|
|
|
|
227
|
return $servers->[0] if @$servers == 1; |
934
|
|
|
|
|
|
|
|
935
|
|
|
|
|
|
|
# order servers by RTT EWMA |
936
|
32
|
|
|
|
|
76
|
my $rtt_hash = $self->rtt_ewma_sec; |
937
|
|
|
|
|
|
|
my @sorted = |
938
|
82
|
|
|
|
|
219
|
sort { $a->{rtt} <=> $b->{rtt} } |
939
|
32
|
|
|
|
|
133
|
map { { server => $_, rtt => $rtt_hash->{ $_->address } } } @$servers; |
|
114
|
|
|
|
|
469
|
|
940
|
|
|
|
|
|
|
# lowest RTT is always in the windows |
941
|
32
|
|
|
|
|
78
|
my @in_window = shift @sorted; |
942
|
|
|
|
|
|
|
|
943
|
|
|
|
|
|
|
# add any other servers in window and return a random one |
944
|
32
|
|
|
|
|
97
|
my $max_rtt = $in_window[0]->{rtt} + $self->local_threshold_sec; |
945
|
32
|
|
|
|
|
72
|
push @in_window, grep { $_->{rtt} <= $max_rtt } @sorted; |
|
82
|
|
|
|
|
179
|
|
946
|
32
|
|
|
|
|
263
|
return $in_window[ int( rand(@in_window) ) ]->{server}; |
947
|
|
|
|
|
|
|
} |
948
|
|
|
|
|
|
|
|
949
|
|
|
|
|
|
|
my $PRIMARY = MongoDB::ReadPreference->new; |
950
|
|
|
|
|
|
|
my $PRIMARY_PREF = MongoDB::ReadPreference->new( mode => 'primaryPreferred' ); |
951
|
|
|
|
|
|
|
|
952
|
|
|
|
|
|
|
sub _ping_server { |
953
|
0
|
|
|
0
|
|
0
|
my ($self, $link) = @_; |
954
|
0
|
|
|
|
|
0
|
return eval { |
955
|
0
|
|
|
|
|
0
|
my $op = MongoDB::Op::_Command->_new( |
956
|
|
|
|
|
|
|
db_name => 'admin', |
957
|
|
|
|
|
|
|
query => [ping => 1], |
958
|
|
|
|
|
|
|
query_flags => {}, |
959
|
|
|
|
|
|
|
bson_codec => $self->bson_codec, |
960
|
|
|
|
|
|
|
read_preference => $PRIMARY_PREF, |
961
|
|
|
|
|
|
|
monitoring_callback => $self->monitoring_callback, |
962
|
|
|
|
|
|
|
); |
963
|
0
|
|
|
|
|
0
|
$op->execute( $link )->output; |
964
|
|
|
|
|
|
|
}; |
965
|
|
|
|
|
|
|
} |
966
|
|
|
|
|
|
|
|
967
|
|
|
|
|
|
|
|
968
|
|
|
|
|
|
|
sub _get_server_link { |
969
|
0
|
|
|
0
|
|
0
|
my ( $self, $server, $method, $read_pref ) = @_; |
970
|
0
|
|
|
|
|
0
|
my $address = $server->address; |
971
|
0
|
|
|
|
|
0
|
my $link = $self->links->{$address}; |
972
|
|
|
|
|
|
|
|
973
|
|
|
|
|
|
|
# if no link, make a new connection or give up |
974
|
0
|
0
|
0
|
|
|
0
|
$link = $self->_initialize_link($address) unless $link && $link->connected; |
975
|
0
|
0
|
|
|
|
0
|
return unless $link; |
976
|
|
|
|
|
|
|
|
977
|
|
|
|
|
|
|
# for idle links, refresh the server and verify validity |
978
|
0
|
0
|
|
|
|
0
|
if ( time - $link->last_used > $self->socket_check_interval_sec ) { |
979
|
0
|
0
|
|
|
|
0
|
return $link if $self->_ping_server; |
980
|
0
|
|
|
|
|
0
|
$self->mark_server_unknown( |
981
|
|
|
|
|
|
|
$server, 'Lost connection with the server' |
982
|
|
|
|
|
|
|
); |
983
|
0
|
|
|
|
|
0
|
$self->check_address($address); |
984
|
|
|
|
|
|
|
|
985
|
|
|
|
|
|
|
# topology might have dropped the server |
986
|
0
|
0
|
|
|
|
0
|
$server = $self->servers->{$address} |
987
|
|
|
|
|
|
|
or return; |
988
|
|
|
|
|
|
|
|
989
|
0
|
|
|
|
|
0
|
my $fresh_link = $self->links->{$address}; |
990
|
0
|
0
|
|
|
|
0
|
return $fresh_link if !$method; |
991
|
|
|
|
|
|
|
|
992
|
|
|
|
|
|
|
# verify selection criteria |
993
|
0
|
0
|
|
|
|
0
|
return $self->$method( $read_pref, $server ) ? $fresh_link : undef; |
994
|
|
|
|
|
|
|
} |
995
|
|
|
|
|
|
|
|
996
|
0
|
|
|
|
|
0
|
return $link; |
997
|
|
|
|
|
|
|
} |
998
|
|
|
|
|
|
|
|
999
|
|
|
|
|
|
|
sub _initialize_link { |
1000
|
106
|
|
|
106
|
|
503
|
my ( $self, $address ) = @_; |
1001
|
|
|
|
|
|
|
|
1002
|
|
|
|
|
|
|
my $link = eval { |
1003
|
106
|
|
|
|
|
298
|
MongoDB::_Link->new( %{$self->link_options}, address => $address )->connect; |
|
106
|
|
|
|
|
3119
|
|
1004
|
106
|
50
|
|
|
|
350
|
} or do { |
1005
|
106
|
|
50
|
|
|
616
|
my $error = $@ || "Unknown error"; |
1006
|
|
|
|
|
|
|
# if connection failed, update topology with Unknown description |
1007
|
106
|
|
|
|
|
789
|
$self->_reset_address_to_unknown( $address, $error ); |
1008
|
|
|
|
|
|
|
}; |
1009
|
|
|
|
|
|
|
|
1010
|
106
|
50
|
|
|
|
1347
|
return unless $link; |
1011
|
|
|
|
|
|
|
|
1012
|
|
|
|
|
|
|
# connection succeeded, so register link and get a server description |
1013
|
0
|
|
|
|
|
0
|
$self->links->{$address} = $link; |
1014
|
0
|
|
|
|
|
0
|
$self->_update_topology_from_link( $link, with_handshake => 1 ); |
1015
|
|
|
|
|
|
|
|
1016
|
|
|
|
|
|
|
# after update, server might or might not exist in the topology; |
1017
|
|
|
|
|
|
|
# if not, return nothing |
1018
|
0
|
0
|
|
|
|
0
|
return unless my $server = $self->servers->{$address}; |
1019
|
|
|
|
|
|
|
|
1020
|
|
|
|
|
|
|
# we have a link and the server is a valid member, so |
1021
|
|
|
|
|
|
|
# try to authenticate; if authentication fails, all |
1022
|
|
|
|
|
|
|
# servers are considered invalid and we throw an error |
1023
|
0
|
0
|
0
|
0
|
|
0
|
if ( $self->type eq 'Single' || first { $_ eq $server->type } qw/Standalone Mongos RSPrimary RSSecondary/ ) { |
|
0
|
|
|
|
|
0
|
|
1024
|
|
|
|
|
|
|
eval { |
1025
|
0
|
|
|
|
|
0
|
$self->credential->authenticate($server, $link, $self->bson_codec); |
1026
|
0
|
|
|
|
|
0
|
1; |
1027
|
0
|
0
|
|
|
|
0
|
} or do { |
1028
|
0
|
|
|
|
|
0
|
my $err = $@; |
1029
|
0
|
0
|
|
|
|
0
|
my $msg = $err->$_isa("MongoDB::Error") ? $err->message : "$err"; |
1030
|
0
|
|
|
|
|
0
|
$self->_reset_address_to_unknown( $_->address, $err ) for $self->all_servers; |
1031
|
0
|
|
|
|
|
0
|
MongoDB::AuthError->throw("Authentication to $address failed: $msg"); |
1032
|
|
|
|
|
|
|
}; |
1033
|
|
|
|
|
|
|
} |
1034
|
|
|
|
|
|
|
|
1035
|
0
|
|
|
|
|
0
|
return $link; |
1036
|
|
|
|
|
|
|
} |
1037
|
|
|
|
|
|
|
|
1038
|
|
|
|
|
|
|
sub _primaries { |
1039
|
203
|
|
|
203
|
|
413
|
return grep { $_->type eq 'RSPrimary' } $_[0]->all_servers; |
|
424
|
|
|
|
|
6898
|
|
1040
|
|
|
|
|
|
|
} |
1041
|
|
|
|
|
|
|
|
1042
|
|
|
|
|
|
|
sub _remove_address { |
1043
|
223
|
|
|
223
|
|
893
|
my ( $self, $address ) = @_; |
1044
|
223
|
50
|
33
|
|
|
1184
|
if ( $self->current_primary && $self->current_primary->address eq $address ) { |
1045
|
0
|
|
|
|
|
0
|
$self->_clear_current_primary; |
1046
|
|
|
|
|
|
|
} |
1047
|
223
|
|
|
|
|
2084
|
delete $self->$_->{$address} for qw/servers links rtt_ewma_sec/; |
1048
|
223
|
100
|
|
|
|
1146
|
$self->publish_server_closing( $address ) |
1049
|
|
|
|
|
|
|
if $self->monitoring_callback; |
1050
|
223
|
|
|
|
|
1787
|
return; |
1051
|
|
|
|
|
|
|
} |
1052
|
|
|
|
|
|
|
|
1053
|
|
|
|
|
|
|
sub _remove_server { |
1054
|
13
|
|
|
13
|
|
36
|
my ( $self, $server ) = @_; |
1055
|
13
|
|
|
|
|
46
|
$self->_remove_address( $server->address ); |
1056
|
13
|
|
|
|
|
21
|
return; |
1057
|
|
|
|
|
|
|
} |
1058
|
|
|
|
|
|
|
|
1059
|
|
|
|
|
|
|
sub _reset_address_to_unknown { |
1060
|
116
|
|
|
116
|
|
479
|
my ( $self, $address, $error, $update_time ) = @_; |
1061
|
116
|
|
66
|
|
|
955
|
$update_time //= time; |
1062
|
|
|
|
|
|
|
|
1063
|
116
|
|
|
|
|
622
|
$self->_remove_address($address); |
1064
|
116
|
|
|
|
|
597
|
my $desc = $self->_add_address_as_unknown( $address, $update_time, $error ); |
1065
|
116
|
|
|
|
|
4389
|
$self->_update_topology_from_server_desc($address, $desc); |
1066
|
|
|
|
|
|
|
|
1067
|
116
|
|
|
|
|
384
|
return; |
1068
|
|
|
|
|
|
|
} |
1069
|
|
|
|
|
|
|
|
1070
|
|
|
|
|
|
|
sub _secondaries { |
1071
|
51
|
|
|
51
|
|
275
|
return grep { $_->type eq 'RSSecondary' } $_[0]->all_servers; |
|
129
|
|
|
|
|
2522
|
|
1072
|
|
|
|
|
|
|
} |
1073
|
|
|
|
|
|
|
|
1074
|
|
|
|
|
|
|
sub _status_string { |
1075
|
53
|
|
|
53
|
|
240
|
my ($self) = @_; |
1076
|
53
|
|
|
|
|
232
|
my $status = ''; |
1077
|
53
|
50
|
|
|
|
496
|
if ( $self->type =~ /^Replica/ ) { |
1078
|
0
|
|
|
|
|
0
|
$status .= sprintf( "Topology type: %s; Set name: %s, Member status:\n", |
1079
|
|
|
|
|
|
|
$self->type, $self->replica_set_name ); |
1080
|
|
|
|
|
|
|
} |
1081
|
|
|
|
|
|
|
else { |
1082
|
53
|
|
|
|
|
1738
|
$status .= sprintf( "Topology type: %s; Member status:\n", $self->type ); |
1083
|
|
|
|
|
|
|
} |
1084
|
|
|
|
|
|
|
|
1085
|
53
|
|
|
|
|
301
|
$status .= join( "\n", map { " $_" } map { $_->status_string } $self->all_servers ) . "\n"; |
|
53
|
|
|
|
|
461
|
|
|
53
|
|
|
|
|
432
|
|
1086
|
53
|
|
|
|
|
563
|
return $status; |
1087
|
|
|
|
|
|
|
} |
1088
|
|
|
|
|
|
|
|
1089
|
|
|
|
|
|
|
# this implements the server selection timeout around whatever actual method |
1090
|
|
|
|
|
|
|
# is used for returning a link |
1091
|
|
|
|
|
|
|
sub _selection_timeout { |
1092
|
53
|
|
|
53
|
|
224
|
my ( $self, $method, $read_pref ) = @_; |
1093
|
|
|
|
|
|
|
|
1094
|
53
|
|
|
|
|
270
|
my $start_time = my $loop_end_time = time(); |
1095
|
53
|
|
|
|
|
290
|
my $max_time = $start_time + $self->server_selection_timeout_sec; |
1096
|
|
|
|
|
|
|
|
1097
|
53
|
50
|
|
|
|
459
|
if ( $self->next_scan_time < $start_time ) { |
1098
|
0
|
|
|
|
|
0
|
$self->_set_stale(1); |
1099
|
|
|
|
|
|
|
} |
1100
|
|
|
|
|
|
|
|
1101
|
53
|
|
|
|
|
164
|
while (1) { |
1102
|
1113
|
100
|
|
|
|
5664
|
if ( $self->stale ) { |
1103
|
1060
|
|
|
|
|
4804
|
my $scan_ready_time = $self->last_scan_time + MIN_HEARTBEAT_FREQUENCY_SEC; |
1104
|
|
|
|
|
|
|
|
1105
|
|
|
|
|
|
|
# if not enough time left to wait to check; then caller throws error |
1106
|
1060
|
100
|
66
|
|
|
10806
|
return if !$self->server_selection_try_once && $scan_ready_time > $max_time; |
1107
|
|
|
|
|
|
|
|
1108
|
|
|
|
|
|
|
# loop_end_time is a proxy for time() to avoid overhead |
1109
|
1007
|
|
|
|
|
2938
|
my $sleep_time = $scan_ready_time - $loop_end_time; |
1110
|
|
|
|
|
|
|
|
1111
|
1007
|
50
|
|
|
|
503313702
|
usleep( 1e6 * $sleep_time ) if $sleep_time > 0; |
1112
|
|
|
|
|
|
|
|
1113
|
1007
|
|
|
|
|
30895
|
$self->scan_all_servers; |
1114
|
|
|
|
|
|
|
} |
1115
|
|
|
|
|
|
|
|
1116
|
1060
|
50
|
|
|
|
6596
|
unless ( $self->is_compatible ) { |
1117
|
0
|
|
|
|
|
0
|
$self->_set_stale(1); |
1118
|
0
|
|
|
|
|
0
|
my $error_string = ''; |
1119
|
0
|
|
|
|
|
0
|
for my $server ( @{ $self->_incompatible_servers } ) { |
|
0
|
|
|
|
|
0
|
|
1120
|
0
|
|
|
|
|
0
|
my $min_wire_ver = $server->is_master->{minWireVersion}; |
1121
|
0
|
|
|
|
|
0
|
my $max_wire_ver = $server->is_master->{maxWireVersion}; |
1122
|
0
|
|
|
|
|
0
|
my $host = $server->address; |
1123
|
0
|
0
|
|
|
|
0
|
if ( $min_wire_ver > $self->max_wire_version ) { |
1124
|
0
|
|
|
|
|
0
|
$error_string .= sprintf( |
1125
|
|
|
|
|
|
|
"Server at %s requires wire version %d, but this version of %s only supports up to %d.\n", |
1126
|
|
|
|
|
|
|
$host, |
1127
|
|
|
|
|
|
|
$min_wire_ver, |
1128
|
|
|
|
|
|
|
'Perl MongoDB Driver', |
1129
|
|
|
|
|
|
|
$self->max_wire_version |
1130
|
|
|
|
|
|
|
); |
1131
|
|
|
|
|
|
|
} else { |
1132
|
0
|
|
|
|
|
0
|
$error_string .= sprintf( |
1133
|
|
|
|
|
|
|
"Server at %s reports wire version %d, but this version of %s requires at least %d (MongoDB %s).\n", |
1134
|
|
|
|
|
|
|
$host, |
1135
|
|
|
|
|
|
|
$max_wire_ver, |
1136
|
|
|
|
|
|
|
'Perl MongoDB Driver', |
1137
|
|
|
|
|
|
|
$self->min_wire_version, |
1138
|
|
|
|
|
|
|
$self->min_server_version, |
1139
|
|
|
|
|
|
|
); |
1140
|
|
|
|
|
|
|
} |
1141
|
|
|
|
|
|
|
} |
1142
|
0
|
|
|
|
|
0
|
$self->_set_compatibility_error($error_string); |
1143
|
0
|
|
|
|
|
0
|
MongoDB::ProtocolError->throw( $error_string ); |
1144
|
|
|
|
|
|
|
} |
1145
|
|
|
|
|
|
|
|
1146
|
1060
|
|
|
|
|
9391
|
my $server = $self->$method($read_pref); |
1147
|
|
|
|
|
|
|
|
1148
|
1060
|
50
|
|
|
|
4245
|
return $server if $server; |
1149
|
|
|
|
|
|
|
|
1150
|
1060
|
|
|
|
|
3685
|
$self->_set_stale(1); |
1151
|
1060
|
|
|
|
|
4241
|
$loop_end_time = time(); |
1152
|
|
|
|
|
|
|
|
1153
|
1060
|
50
|
|
|
|
6029
|
if ( $self->server_selection_try_once ) { |
1154
|
|
|
|
|
|
|
# if already tried once; then caller throws error |
1155
|
0
|
0
|
|
|
|
0
|
return if $self->last_scan_time > $start_time; |
1156
|
|
|
|
|
|
|
} |
1157
|
|
|
|
|
|
|
else { |
1158
|
|
|
|
|
|
|
# if selection timed out; then caller throws error |
1159
|
1060
|
50
|
|
|
|
4645
|
return if $loop_end_time > $max_time; |
1160
|
|
|
|
|
|
|
} |
1161
|
|
|
|
|
|
|
} |
1162
|
|
|
|
|
|
|
} |
1163
|
|
|
|
|
|
|
|
1164
|
|
|
|
|
|
|
sub _generate_ismaster_request { |
1165
|
0
|
|
|
0
|
|
0
|
my ( $self, $link, $should_perform_handshake ) = @_; |
1166
|
0
|
|
|
|
|
0
|
my @opts; |
1167
|
0
|
0
|
|
|
|
0
|
if ($should_perform_handshake) { |
1168
|
0
|
|
|
|
|
0
|
push @opts, client => $self->handshake_document; |
1169
|
0
|
0
|
|
|
|
0
|
if ( $self->credential->mechanism eq 'DEFAULT' ) { |
1170
|
0
|
|
|
|
|
0
|
my $db_user = join( ".", map { $self->credential->$_ } qw/source username/ ); |
|
0
|
|
|
|
|
0
|
|
1171
|
0
|
|
|
|
|
0
|
push @opts, saslSupportedMechs => $db_user; |
1172
|
|
|
|
|
|
|
} |
1173
|
0
|
0
|
|
|
|
0
|
if (@{ $self->compressors }) { |
|
0
|
|
|
|
|
0
|
|
1174
|
0
|
|
|
|
|
0
|
push @opts, compression => $self->compressors; |
1175
|
|
|
|
|
|
|
} |
1176
|
|
|
|
|
|
|
} |
1177
|
0
|
0
|
0
|
|
|
0
|
if ( $link->supports_clusterTime && defined $self->cluster_time ) { |
1178
|
0
|
|
|
|
|
0
|
push @opts, '$clusterTime' => $self->cluster_time; |
1179
|
|
|
|
|
|
|
} |
1180
|
|
|
|
|
|
|
|
1181
|
0
|
|
|
|
|
0
|
return [ ismaster => 1, @opts ]; |
1182
|
|
|
|
|
|
|
} |
1183
|
|
|
|
|
|
|
|
1184
|
|
|
|
|
|
|
sub _update_topology_from_link { |
1185
|
0
|
|
|
0
|
|
0
|
my ( $self, $link, %opts ) = @_; |
1186
|
|
|
|
|
|
|
|
1187
|
0
|
0
|
|
|
|
0
|
$self->publish_server_heartbeat_started( $link ) |
1188
|
|
|
|
|
|
|
if $self->monitoring_callback; |
1189
|
|
|
|
|
|
|
|
1190
|
0
|
|
|
|
|
0
|
my $start_time = time; |
1191
|
0
|
|
|
|
|
0
|
my $is_master = eval { |
1192
|
|
|
|
|
|
|
my $op = MongoDB::Op::_Command->_new( |
1193
|
|
|
|
|
|
|
db_name => 'admin', |
1194
|
0
|
|
|
|
|
0
|
query => $self->_generate_ismaster_request( $link, $opts{with_handshake} ), |
1195
|
|
|
|
|
|
|
query_flags => {}, |
1196
|
|
|
|
|
|
|
bson_codec => $self->bson_codec, |
1197
|
|
|
|
|
|
|
read_preference => $PRIMARY, |
1198
|
|
|
|
|
|
|
monitoring_callback => $self->monitoring_callback, |
1199
|
|
|
|
|
|
|
); |
1200
|
|
|
|
|
|
|
# just for this command, use connect timeout as socket timeout; |
1201
|
|
|
|
|
|
|
# this violates encapsulation, but requires less API modification |
1202
|
|
|
|
|
|
|
# to support this specific exception to the socket timeout |
1203
|
0
|
|
|
|
|
0
|
local $link->{socket_timeout} = $link->{connect_timeout}; |
1204
|
0
|
|
|
|
|
0
|
$op->execute( $link )->output; |
1205
|
|
|
|
|
|
|
}; |
1206
|
0
|
0
|
|
|
|
0
|
if ( my $e = $@ ) { |
1207
|
0
|
|
|
|
|
0
|
my $end_time_fail = time; |
1208
|
0
|
|
|
|
|
0
|
my $rtt_sec_fail = $end_time_fail - $start_time; |
1209
|
0
|
0
|
|
|
|
0
|
$self->publish_server_heartbeat_failed( $link, $rtt_sec_fail, $e ) |
1210
|
|
|
|
|
|
|
if $self->monitoring_callback; |
1211
|
0
|
0
|
0
|
|
|
0
|
if ($e->$_isa("MongoDB::DatabaseError") && $e->code == USER_NOT_FOUND ) { |
1212
|
0
|
|
|
|
|
0
|
MongoDB::AuthError->throw("mechanism negotiation error: $e"); |
1213
|
|
|
|
|
|
|
} |
1214
|
0
|
|
|
|
|
0
|
warn "During MongoDB topology update for @{[$link->address]}: $e" |
1215
|
|
|
|
|
|
|
if WITH_ASSERTS; |
1216
|
0
|
|
|
|
|
0
|
$self->_reset_address_to_unknown( $link->address, $e ); |
1217
|
|
|
|
|
|
|
# retry a network error if server was previously known to us |
1218
|
0
|
0
|
0
|
|
|
0
|
if ( $e->$_isa("MongoDB::NetworkError") |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
1219
|
|
|
|
|
|
|
and $link->server |
1220
|
|
|
|
|
|
|
and $link->server->type ne 'Unknown' |
1221
|
|
|
|
|
|
|
and $link->server->type ne 'PossiblePrimary' ) |
1222
|
|
|
|
|
|
|
{ |
1223
|
|
|
|
|
|
|
# the earlier reset to unknown avoids us reaching this branch again |
1224
|
|
|
|
|
|
|
# and recursing forever |
1225
|
0
|
|
|
|
|
0
|
$self->check_address( $link->address ); |
1226
|
|
|
|
|
|
|
} |
1227
|
0
|
|
|
|
|
0
|
return; |
1228
|
|
|
|
|
|
|
}; |
1229
|
|
|
|
|
|
|
|
1230
|
0
|
0
|
|
|
|
0
|
return unless $is_master; |
1231
|
|
|
|
|
|
|
|
1232
|
0
|
0
|
|
|
|
0
|
if ( my $cluster_time = $is_master->{'$clusterTime'} ) { |
1233
|
0
|
|
|
|
|
0
|
$self->update_cluster_time($cluster_time); |
1234
|
|
|
|
|
|
|
} |
1235
|
|
|
|
|
|
|
|
1236
|
0
|
|
|
|
|
0
|
my $end_time = time; |
1237
|
0
|
|
|
|
|
0
|
my $rtt_sec = $end_time - $start_time; |
1238
|
|
|
|
|
|
|
# Protect against clock skew |
1239
|
0
|
0
|
|
|
|
0
|
$rtt_sec = 0 if $rtt_sec < 0; |
1240
|
|
|
|
|
|
|
|
1241
|
0
|
0
|
|
|
|
0
|
$self->publish_server_heartbeat_succeeded( $link, $rtt_sec, $is_master ) |
1242
|
|
|
|
|
|
|
if $self->monitoring_callback; |
1243
|
|
|
|
|
|
|
|
1244
|
0
|
|
|
|
|
0
|
my $new_server = MongoDB::_Server->new( |
1245
|
|
|
|
|
|
|
address => $link->address, |
1246
|
|
|
|
|
|
|
last_update_time => $end_time, |
1247
|
|
|
|
|
|
|
rtt_sec => $rtt_sec, |
1248
|
|
|
|
|
|
|
is_master => $is_master, |
1249
|
|
|
|
|
|
|
compressor => $self->_construct_compressor($is_master), |
1250
|
|
|
|
|
|
|
); |
1251
|
|
|
|
|
|
|
|
1252
|
0
|
|
|
|
|
0
|
$self->_update_topology_from_server_desc( $link->address, $new_server ); |
1253
|
|
|
|
|
|
|
|
1254
|
0
|
|
|
|
|
0
|
return; |
1255
|
|
|
|
|
|
|
} |
1256
|
|
|
|
|
|
|
|
1257
|
|
|
|
|
|
|
# find suitable compressor |
1258
|
|
|
|
|
|
|
# |
1259
|
|
|
|
|
|
|
# implemented here because the result is based on the specified |
1260
|
|
|
|
|
|
|
# order of compressors combined with the list of server supported |
1261
|
|
|
|
|
|
|
# compressors |
1262
|
|
|
|
|
|
|
sub _construct_compressor { |
1263
|
0
|
|
|
0
|
|
0
|
my ($self, $is_master) = @_; |
1264
|
|
|
|
|
|
|
|
1265
|
0
|
0
|
0
|
|
|
0
|
my @supported = @{ ($is_master || {})->{compression} || [] } |
|
0
|
0
|
|
|
|
0
|
|
1266
|
|
|
|
|
|
|
or return undef; ## no critic |
1267
|
|
|
|
|
|
|
|
1268
|
0
|
|
|
|
|
0
|
for my $name (@{ $self->compressors }) { |
|
0
|
|
|
|
|
0
|
|
1269
|
0
|
0
|
|
|
|
0
|
if (grep { $name eq $_ } @supported) { |
|
0
|
|
|
|
|
0
|
|
1270
|
0
|
|
|
|
|
0
|
return MongoDB::_Protocol::get_compressor($name, { |
1271
|
|
|
|
|
|
|
zlib_compression_level => $self->zlib_compression_level, |
1272
|
|
|
|
|
|
|
}); |
1273
|
|
|
|
|
|
|
} |
1274
|
|
|
|
|
|
|
} |
1275
|
|
|
|
|
|
|
|
1276
|
0
|
|
|
|
|
0
|
return undef; ## no critic |
1277
|
|
|
|
|
|
|
} |
1278
|
|
|
|
|
|
|
|
1279
|
|
|
|
|
|
|
sub _update_topology_from_server_desc { |
1280
|
261
|
|
|
261
|
|
4489
|
my ( $self, $address, $new_server ) = @_; |
1281
|
|
|
|
|
|
|
|
1282
|
|
|
|
|
|
|
# ignore spurious result not in the set; this isn't strictly necessary |
1283
|
|
|
|
|
|
|
# for single-threaded operation, but spec tests expect it and if we |
1284
|
|
|
|
|
|
|
# have async monitoring in the future, late responses could come back |
1285
|
|
|
|
|
|
|
# after a server has been removed |
1286
|
261
|
100
|
|
|
|
1168
|
return unless $self->servers->{$address}; |
1287
|
|
|
|
|
|
|
|
1288
|
259
|
100
|
|
|
|
1207
|
$self->publish_old_topology_desc( $address, $new_server ) |
1289
|
|
|
|
|
|
|
if $self->monitoring_callback; |
1290
|
|
|
|
|
|
|
|
1291
|
259
|
|
|
|
|
3577
|
$self->_update_ewma( $address, $new_server ); |
1292
|
|
|
|
|
|
|
|
1293
|
|
|
|
|
|
|
# must come after ewma update |
1294
|
259
|
|
|
|
|
1036
|
$self->servers->{$address} = $new_server; |
1295
|
|
|
|
|
|
|
|
1296
|
259
|
|
|
|
|
926
|
my $method = "_update_" . $self->type; |
1297
|
|
|
|
|
|
|
|
1298
|
259
|
|
|
|
|
1453
|
$self->$method( $address, $new_server ); |
1299
|
|
|
|
|
|
|
|
1300
|
|
|
|
|
|
|
# if link is still around, tag it with server specifics |
1301
|
259
|
|
|
|
|
942
|
$self->_update_link_metadata( $address, $new_server ); |
1302
|
|
|
|
|
|
|
|
1303
|
259
|
|
|
|
|
1028
|
$self->_update_ls_timeout_minutes( $new_server ); |
1304
|
|
|
|
|
|
|
|
1305
|
259
|
100
|
|
|
|
1221
|
$self->publish_new_topology_desc if $self->monitoring_callback; |
1306
|
|
|
|
|
|
|
|
1307
|
259
|
|
|
|
|
11247
|
return $new_server; |
1308
|
|
|
|
|
|
|
} |
1309
|
|
|
|
|
|
|
|
1310
|
|
|
|
|
|
|
sub _update_ewma { |
1311
|
481
|
|
|
481
|
|
12202
|
my ( $self, $address, $new_server ) = @_; |
1312
|
|
|
|
|
|
|
|
1313
|
481
|
100
|
|
|
|
7878
|
if ( $new_server->type eq 'Unknown' ) { |
1314
|
126
|
|
|
|
|
3756
|
delete $self->rtt_ewma_sec->{$address}; |
1315
|
|
|
|
|
|
|
} |
1316
|
|
|
|
|
|
|
else { |
1317
|
355
|
|
|
|
|
4334
|
my $old_avg = $self->rtt_ewma_sec->{$address}; |
1318
|
355
|
|
|
|
|
762
|
my $alpha = $self->ewma_alpha; |
1319
|
355
|
|
|
|
|
680
|
my $rtt_sec = $new_server->rtt_sec; |
1320
|
355
|
100
|
|
|
|
1085
|
$self->rtt_ewma_sec->{$address} = |
1321
|
|
|
|
|
|
|
defined($old_avg) ? ( $alpha * $rtt_sec + ( 1 - $alpha ) * $old_avg ) : $rtt_sec; |
1322
|
|
|
|
|
|
|
} |
1323
|
|
|
|
|
|
|
|
1324
|
481
|
|
|
|
|
1122
|
return; |
1325
|
|
|
|
|
|
|
} |
1326
|
|
|
|
|
|
|
|
1327
|
|
|
|
|
|
|
sub _update_link_metadata { |
1328
|
259
|
|
|
259
|
|
680
|
my ( $self, $address, $server ) = @_; |
1329
|
|
|
|
|
|
|
|
1330
|
|
|
|
|
|
|
# if the link didn't get dropped from the topology during the update, we |
1331
|
|
|
|
|
|
|
# attach the server so the link knows where it came from |
1332
|
259
|
50
|
|
|
|
1008
|
if ( $self->links->{$address} ) { |
1333
|
0
|
|
|
|
|
0
|
$self->links->{$address}->set_metadata($server); |
1334
|
|
|
|
|
|
|
} |
1335
|
|
|
|
|
|
|
|
1336
|
259
|
|
|
|
|
470
|
return; |
1337
|
|
|
|
|
|
|
} |
1338
|
|
|
|
|
|
|
|
1339
|
|
|
|
|
|
|
sub _update_rs_with_primary_from_member { |
1340
|
17
|
|
|
17
|
|
39
|
my ( $self, $new_server ) = @_; |
1341
|
|
|
|
|
|
|
|
1342
|
17
|
100
|
66
|
|
|
291
|
if ( !$self->servers->{ $new_server->address } |
1343
|
|
|
|
|
|
|
|| $self->replica_set_name ne $new_server->set_name ) |
1344
|
|
|
|
|
|
|
{ |
1345
|
2
|
|
|
|
|
44
|
$self->_remove_server($new_server); |
1346
|
|
|
|
|
|
|
} |
1347
|
|
|
|
|
|
|
|
1348
|
|
|
|
|
|
|
# require 'me' that matches expected address. |
1349
|
|
|
|
|
|
|
# check is case insensitive |
1350
|
17
|
50
|
66
|
|
|
532
|
if ( $new_server->me && lc $new_server->me ne $new_server->address ) { |
1351
|
0
|
|
|
|
|
0
|
$self->_remove_server($new_server); |
1352
|
0
|
|
|
|
|
0
|
$self->_check_for_primary; |
1353
|
0
|
|
|
|
|
0
|
return; |
1354
|
|
|
|
|
|
|
} |
1355
|
|
|
|
|
|
|
|
1356
|
17
|
100
|
|
|
|
159
|
if ( ! $self->_check_for_primary ) { |
1357
|
|
|
|
|
|
|
|
1358
|
|
|
|
|
|
|
# flag possible primary to amend scanning order |
1359
|
1
|
|
|
|
|
13
|
my $primary = $new_server->primary; |
1360
|
1
|
0
|
33
|
|
|
9
|
if ( length($primary) |
|
|
|
0
|
|
|
|
|
1361
|
|
|
|
|
|
|
&& $self->servers->{$primary} |
1362
|
|
|
|
|
|
|
&& $self->servers->{$primary}->type eq 'Unknown' ) |
1363
|
|
|
|
|
|
|
{ |
1364
|
0
|
|
|
|
|
0
|
$self->servers->{$primary}->_set_type('PossiblePrimary'); |
1365
|
|
|
|
|
|
|
} |
1366
|
|
|
|
|
|
|
} |
1367
|
|
|
|
|
|
|
|
1368
|
17
|
|
|
|
|
29
|
return; |
1369
|
|
|
|
|
|
|
} |
1370
|
|
|
|
|
|
|
|
1371
|
|
|
|
|
|
|
sub _update_rs_with_primary_from_primary { |
1372
|
63
|
|
|
63
|
|
114
|
my ( $self, $new_server ) = @_; |
1373
|
|
|
|
|
|
|
|
1374
|
63
|
100
|
|
|
|
933
|
if ( !length $self->replica_set_name ) { |
|
|
100
|
|
|
|
|
|
1375
|
3
|
|
|
|
|
66
|
$self->_set_replica_set_name( $new_server->set_name ); |
1376
|
|
|
|
|
|
|
} |
1377
|
|
|
|
|
|
|
elsif ( $self->replica_set_name ne $new_server->set_name ) { |
1378
|
|
|
|
|
|
|
# We found a primary but it doesn't have the setName |
1379
|
|
|
|
|
|
|
# provided by the user or previously discovered |
1380
|
3
|
|
|
|
|
66
|
$self->_remove_server($new_server); |
1381
|
3
|
|
|
|
|
7
|
return; |
1382
|
|
|
|
|
|
|
} |
1383
|
|
|
|
|
|
|
|
1384
|
60
|
|
|
|
|
1279
|
my $election_id = $new_server->is_master->{electionId}; |
1385
|
60
|
|
|
|
|
115
|
my $set_version = $new_server->is_master->{setVersion}; |
1386
|
60
|
|
|
|
|
121
|
my $max_election_id = $self->max_election_id; |
1387
|
60
|
|
|
|
|
779
|
my $max_set_version = $self->max_set_version; |
1388
|
|
|
|
|
|
|
|
1389
|
60
|
100
|
100
|
|
|
411
|
if ( defined $set_version && defined $election_id ) { |
1390
|
20
|
100
|
66
|
|
|
128
|
if ( |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
1391
|
|
|
|
|
|
|
defined $max_election_id |
1392
|
|
|
|
|
|
|
&& defined $max_set_version |
1393
|
|
|
|
|
|
|
&& ( |
1394
|
|
|
|
|
|
|
$max_set_version > $set_version |
1395
|
|
|
|
|
|
|
|| ( $max_set_version == $set_version |
1396
|
|
|
|
|
|
|
&& "$max_election_id" gt "$election_id" ) |
1397
|
|
|
|
|
|
|
) |
1398
|
|
|
|
|
|
|
) |
1399
|
|
|
|
|
|
|
{ |
1400
|
|
|
|
|
|
|
# stale primary |
1401
|
|
|
|
|
|
|
|
1402
|
6
|
|
|
|
|
73
|
$self->_remove_address( $new_server->address ); |
1403
|
6
|
|
|
|
|
29
|
$self->_add_address_as_unknown( $new_server->address ); |
1404
|
6
|
|
|
|
|
157
|
$self->_check_for_primary; |
1405
|
6
|
|
|
|
|
13
|
return; |
1406
|
|
|
|
|
|
|
} |
1407
|
14
|
|
|
|
|
109
|
$self->_set_max_election_id( $election_id ); |
1408
|
|
|
|
|
|
|
} |
1409
|
|
|
|
|
|
|
|
1410
|
54
|
100
|
100
|
|
|
182
|
if ( defined $set_version |
|
|
|
100
|
|
|
|
|
1411
|
|
|
|
|
|
|
&& ( !defined $max_set_version || $set_version > $max_set_version ) ) |
1412
|
|
|
|
|
|
|
{ |
1413
|
14
|
|
|
|
|
204
|
$self->_set_max_set_version($set_version); |
1414
|
|
|
|
|
|
|
} |
1415
|
|
|
|
|
|
|
|
1416
|
|
|
|
|
|
|
# possibly invalidate an old primary (even if more than one!) |
1417
|
54
|
|
|
|
|
459
|
for my $old_primary ( $self->_primaries ) { |
1418
|
64
|
100
|
|
|
|
543
|
if ( $old_primary->address ne $new_server->address ) { |
1419
|
10
|
|
|
|
|
40
|
$self->_reset_address_to_unknown( |
1420
|
|
|
|
|
|
|
$old_primary->address, |
1421
|
|
|
|
|
|
|
"no longer primary; update needed", |
1422
|
|
|
|
|
|
|
$old_primary->last_update_time |
1423
|
|
|
|
|
|
|
); |
1424
|
|
|
|
|
|
|
} |
1425
|
|
|
|
|
|
|
} |
1426
|
|
|
|
|
|
|
|
1427
|
|
|
|
|
|
|
# unknown set members need to be added to the topology |
1428
|
|
|
|
|
|
|
my %set_members = |
1429
|
54
|
|
|
|
|
144
|
map { $_ => undef } map { @{ $new_server->$_ } } qw/hosts passives arbiters/; |
|
108
|
|
|
|
|
539
|
|
|
162
|
|
|
|
|
713
|
|
|
162
|
|
|
|
|
2085
|
|
1430
|
|
|
|
|
|
|
|
1431
|
|
|
|
|
|
|
$self->_add_address_as_unknown($_) |
1432
|
54
|
|
|
|
|
125
|
for grep { !exists $self->servers->{$_} } keys %set_members; |
|
108
|
|
|
|
|
283
|
|
1433
|
|
|
|
|
|
|
|
1434
|
|
|
|
|
|
|
# topology servers no longer in the set need to be removed |
1435
|
|
|
|
|
|
|
$self->_remove_address($_) |
1436
|
54
|
|
|
|
|
593
|
for grep { !exists $set_members{$_} } keys %{ $self->servers }; |
|
116
|
|
|
|
|
269
|
|
|
54
|
|
|
|
|
124
|
|
1437
|
|
|
|
|
|
|
|
1438
|
54
|
|
|
|
|
119
|
return; |
1439
|
|
|
|
|
|
|
} |
1440
|
|
|
|
|
|
|
|
1441
|
|
|
|
|
|
|
sub _update_rs_without_primary { |
1442
|
10
|
|
|
10
|
|
20
|
my ( $self, $new_server ) = @_; |
1443
|
|
|
|
|
|
|
|
1444
|
10
|
100
|
|
|
|
139
|
if ( !length $self->replica_set_name ) { |
|
|
100
|
|
|
|
|
|
1445
|
1
|
|
|
|
|
17
|
$self->_set_replica_set_name( $new_server->set_name ); |
1446
|
|
|
|
|
|
|
} |
1447
|
|
|
|
|
|
|
elsif ( $self->replica_set_name ne $new_server->set_name ) { |
1448
|
2
|
|
|
|
|
47
|
$self->_remove_server($new_server); |
1449
|
2
|
|
|
|
|
3
|
return; |
1450
|
|
|
|
|
|
|
} |
1451
|
|
|
|
|
|
|
|
1452
|
|
|
|
|
|
|
# unknown set members need to be added to the topology |
1453
|
|
|
|
|
|
|
my %set_members = |
1454
|
8
|
|
|
|
|
201
|
map { $_ => undef } map { @{ $new_server->$_ } } qw/hosts passives arbiters/; |
|
18
|
|
|
|
|
79
|
|
|
24
|
|
|
|
|
104
|
|
|
24
|
|
|
|
|
305
|
|
1455
|
|
|
|
|
|
|
|
1456
|
|
|
|
|
|
|
$self->_add_address_as_unknown($_) |
1457
|
8
|
|
|
|
|
20
|
for grep { !exists $self->servers->{$_} } keys %set_members; |
|
18
|
|
|
|
|
56
|
|
1458
|
|
|
|
|
|
|
|
1459
|
|
|
|
|
|
|
# require 'me' that matches expected address |
1460
|
8
|
100
|
66
|
|
|
271
|
if ( $new_server->me && $new_server->me ne $new_server->address ) { |
1461
|
1
|
|
|
|
|
30
|
$self->_remove_server($new_server); |
1462
|
1
|
|
|
|
|
3
|
return; |
1463
|
|
|
|
|
|
|
} |
1464
|
|
|
|
|
|
|
|
1465
|
|
|
|
|
|
|
# flag possible primary to amend scanning order |
1466
|
7
|
|
|
|
|
136
|
my $primary = $new_server->primary; |
1467
|
7
|
50
|
66
|
|
|
89
|
if ( length($primary) |
|
|
|
33
|
|
|
|
|
1468
|
|
|
|
|
|
|
&& $self->servers->{$primary} |
1469
|
|
|
|
|
|
|
&& $self->servers->{$primary}->type eq 'Unknown' ) |
1470
|
|
|
|
|
|
|
{ |
1471
|
2
|
|
|
|
|
58
|
$self->servers->{$primary}->_set_type('PossiblePrimary'); |
1472
|
|
|
|
|
|
|
} |
1473
|
|
|
|
|
|
|
|
1474
|
7
|
|
|
|
|
83
|
return; |
1475
|
|
|
|
|
|
|
} |
1476
|
|
|
|
|
|
|
|
1477
|
|
|
|
|
|
|
#--------------------------------------------------------------------------# |
1478
|
|
|
|
|
|
|
# update methods by topology types: behavior in each depends on new server |
1479
|
|
|
|
|
|
|
# type received |
1480
|
|
|
|
|
|
|
#--------------------------------------------------------------------------# |
1481
|
|
|
|
|
|
|
|
1482
|
|
|
|
|
|
|
sub _update_ReplicaSetNoPrimary { |
1483
|
53
|
|
|
53
|
|
102
|
my ( $self, $address, $new_server ) = @_; |
1484
|
|
|
|
|
|
|
|
1485
|
53
|
|
|
|
|
708
|
my $server_type = $new_server->type; |
1486
|
|
|
|
|
|
|
|
1487
|
53
|
100
|
|
|
|
379
|
if ( $server_type eq 'RSPrimary' ) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
1488
|
41
|
|
|
|
|
541
|
$self->_set_type('ReplicaSetWithPrimary'); |
1489
|
41
|
|
|
|
|
925
|
$self->_update_rs_with_primary_from_primary($new_server); |
1490
|
|
|
|
|
|
|
# topology changes might have removed all primaries |
1491
|
41
|
|
|
|
|
110
|
$self->_check_for_primary; |
1492
|
|
|
|
|
|
|
} |
1493
|
36
|
|
|
|
|
78
|
elsif ( grep { $server_type eq $_ } qw/RSSecondary RSArbiter RSOther/ ) { |
1494
|
9
|
|
|
|
|
23
|
$self->_update_rs_without_primary($new_server); |
1495
|
|
|
|
|
|
|
} |
1496
|
6
|
|
|
|
|
15
|
elsif ( grep { $server_type eq $_ } qw/Standalone Mongos/ ) { |
1497
|
2
|
|
|
|
|
7
|
$self->_remove_server($new_server); |
1498
|
|
|
|
|
|
|
} |
1499
|
|
|
|
|
|
|
else { |
1500
|
|
|
|
|
|
|
# Unknown or RSGhost are no-ops |
1501
|
|
|
|
|
|
|
} |
1502
|
|
|
|
|
|
|
|
1503
|
53
|
|
|
|
|
92
|
return; |
1504
|
|
|
|
|
|
|
} |
1505
|
|
|
|
|
|
|
|
1506
|
|
|
|
|
|
|
sub _update_ReplicaSetWithPrimary { |
1507
|
54
|
|
|
54
|
|
116
|
my ( $self, $address, $new_server ) = @_; |
1508
|
|
|
|
|
|
|
|
1509
|
54
|
|
|
|
|
773
|
my $server_type = $new_server->type; |
1510
|
|
|
|
|
|
|
|
1511
|
54
|
100
|
|
|
|
369
|
if ( $server_type eq 'RSPrimary' ) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
1512
|
19
|
|
|
|
|
50
|
$self->_update_rs_with_primary_from_primary($new_server); |
1513
|
|
|
|
|
|
|
} |
1514
|
105
|
|
|
|
|
218
|
elsif ( grep { $server_type eq $_ } qw/RSSecondary RSArbiter RSOther/ ) { |
1515
|
17
|
|
|
|
|
51
|
$self->_update_rs_with_primary_from_member($new_server); |
1516
|
|
|
|
|
|
|
} |
1517
|
54
|
|
|
|
|
97
|
elsif ( grep { $server_type eq $_ } qw/Unknown Standalone Mongos/ ) { |
1518
|
16
|
100
|
|
|
|
46
|
$self->_remove_server($new_server) |
1519
|
|
|
|
|
|
|
unless $server_type eq 'Unknown'; |
1520
|
|
|
|
|
|
|
} |
1521
|
|
|
|
|
|
|
else { |
1522
|
|
|
|
|
|
|
# RSGhost is no-op |
1523
|
|
|
|
|
|
|
} |
1524
|
|
|
|
|
|
|
|
1525
|
|
|
|
|
|
|
# topology changes might have removed all primaries |
1526
|
54
|
|
|
|
|
141
|
$self->_check_for_primary; |
1527
|
|
|
|
|
|
|
|
1528
|
54
|
|
|
|
|
79
|
return; |
1529
|
|
|
|
|
|
|
} |
1530
|
|
|
|
|
|
|
|
1531
|
|
|
|
|
|
|
sub _update_Sharded { |
1532
|
11
|
|
|
11
|
|
27
|
my ( $self, $address, $new_server ) = @_; |
1533
|
|
|
|
|
|
|
|
1534
|
11
|
|
|
|
|
147
|
my $server_type = $new_server->type; |
1535
|
|
|
|
|
|
|
|
1536
|
11
|
100
|
|
|
|
63
|
if ( grep { $server_type eq $_ } qw/Unknown Mongos/ ) { |
|
22
|
|
|
|
|
51
|
|
1537
|
|
|
|
|
|
|
# no-op |
1538
|
|
|
|
|
|
|
} |
1539
|
|
|
|
|
|
|
else { |
1540
|
1
|
|
|
|
|
4
|
$self->_remove_server($new_server); |
1541
|
|
|
|
|
|
|
} |
1542
|
|
|
|
|
|
|
|
1543
|
11
|
|
|
|
|
19
|
return; |
1544
|
|
|
|
|
|
|
} |
1545
|
|
|
|
|
|
|
|
1546
|
|
|
|
|
|
|
sub _update_Single { |
1547
|
75
|
|
|
75
|
|
295
|
my ( $self, $address, $new_server ) = @_; |
1548
|
|
|
|
|
|
|
# Per the spec, TopologyType Single never changes type or membership |
1549
|
75
|
|
|
|
|
193
|
return; |
1550
|
|
|
|
|
|
|
} |
1551
|
|
|
|
|
|
|
|
1552
|
|
|
|
|
|
|
# Direct mode is like Unknown, except that it switches only between Sharded |
1553
|
|
|
|
|
|
|
# or Single based on the response. |
1554
|
|
|
|
|
|
|
sub _update_Direct { |
1555
|
53
|
|
|
53
|
|
207
|
my ( $self, $address, $new_server ) = @_; |
1556
|
|
|
|
|
|
|
|
1557
|
53
|
|
|
|
|
1018
|
my $server_type = $new_server->type; |
1558
|
|
|
|
|
|
|
|
1559
|
53
|
50
|
|
|
|
603
|
if ( $server_type eq 'Mongos' ) { |
1560
|
0
|
|
|
|
|
0
|
$self->_set_type('Sharded'); |
1561
|
0
|
|
|
|
|
0
|
return; |
1562
|
|
|
|
|
|
|
} |
1563
|
|
|
|
|
|
|
|
1564
|
53
|
|
|
|
|
1149
|
$self->_set_type('Single'); |
1565
|
53
|
|
|
|
|
1842
|
return; |
1566
|
|
|
|
|
|
|
} |
1567
|
|
|
|
|
|
|
|
1568
|
|
|
|
|
|
|
sub _update_Unknown { |
1569
|
13
|
|
|
13
|
|
28
|
my ( $self, $address, $new_server ) = @_; |
1570
|
|
|
|
|
|
|
|
1571
|
13
|
|
|
|
|
182
|
my $server_type = $new_server->type; |
1572
|
|
|
|
|
|
|
|
1573
|
|
|
|
|
|
|
# Starting from topology type 'unknown', a standalone server when we |
1574
|
|
|
|
|
|
|
# were given multiple seeds must be a replica set member in maintenance |
1575
|
|
|
|
|
|
|
# mode so we drop it and will rediscover it later. |
1576
|
13
|
100
|
|
|
|
97
|
if ( $server_type eq 'Standalone' ) { |
1577
|
2
|
50
|
|
|
|
30
|
if ( $self->number_of_seeds > 1 ) { |
1578
|
2
|
|
|
|
|
53
|
$self->_remove_address($address); |
1579
|
|
|
|
|
|
|
} |
1580
|
|
|
|
|
|
|
else { |
1581
|
0
|
|
|
|
|
0
|
$self->_set_type('Single'); |
1582
|
|
|
|
|
|
|
} |
1583
|
2
|
|
|
|
|
4
|
return; |
1584
|
|
|
|
|
|
|
} |
1585
|
|
|
|
|
|
|
|
1586
|
11
|
100
|
|
|
|
39
|
if ( $server_type eq 'Mongos' ) { |
1587
|
7
|
|
|
|
|
160
|
$self->_set_type('Sharded'); |
1588
|
7
|
|
|
|
|
196
|
return; |
1589
|
|
|
|
|
|
|
} |
1590
|
|
|
|
|
|
|
|
1591
|
4
|
100
|
|
|
|
15
|
if ( $server_type eq 'RSPrimary' ) { |
|
|
50
|
|
|
|
|
|
1592
|
3
|
|
|
|
|
44
|
$self->_set_type('ReplicaSetWithPrimary'); |
1593
|
3
|
|
|
|
|
89
|
$self->_update_rs_with_primary_from_primary($new_server); |
1594
|
|
|
|
|
|
|
# topology changes might have removed all primaries |
1595
|
3
|
|
|
|
|
10
|
$self->_check_for_primary; |
1596
|
|
|
|
|
|
|
} |
1597
|
3
|
|
|
|
|
7
|
elsif ( grep { $server_type eq $_ } qw/RSSecondary RSArbiter RSOther/ ) { |
1598
|
1
|
|
|
|
|
18
|
$self->_set_type('ReplicaSetNoPrimary'); |
1599
|
1
|
|
|
|
|
30
|
$self->_update_rs_without_primary($new_server); |
1600
|
|
|
|
|
|
|
} |
1601
|
|
|
|
|
|
|
else { |
1602
|
|
|
|
|
|
|
# Unknown or RSGhost are no-ops |
1603
|
|
|
|
|
|
|
} |
1604
|
|
|
|
|
|
|
|
1605
|
4
|
|
|
|
|
9
|
return; |
1606
|
|
|
|
|
|
|
} |
1607
|
|
|
|
|
|
|
|
1608
|
|
|
|
|
|
|
1; |
1609
|
|
|
|
|
|
|
|
1610
|
|
|
|
|
|
|
# vim: ts=4 sts=4 sw=4 et: |