MongoDB/_Topology.pm

filter: Statementstmt 69.6% 484 / 695 Branchbran 50.3% 159 / 316 Conditioncond 43.5% 67 / 154 MC/DCmcdc 28.4% 25 / 88 Subroutinesub 79.3% 77 / 97 Podpod 0.0% 0 / 14 totaltotal 59.5% 812 / 1364 CC 227 SCAR 35.5
CC
227
Cov
61%
get_readable_link
240.0
_update_topology_from_link
210.0
_get_server_link
90.0
CRAP 34.7
« MongoDB/_Protocol.pm ↑ Summary MongoDB/Role/_UpdatePreEncoder.pm »
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 60 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 60 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 package MongoDB::_Topology;
18  
19 60 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 60 our $VERSION = 'v2.2.2';
21  
22 60 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 60 ▶use BSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 60 ▶use MongoDB::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 60 ▶use MongoDB::Op::_Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 60 ▶use MongoDB::_Platform;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 60 ▶use MongoDB::ReadPreference;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 60 ▶use MongoDB::_Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 60 ▶use MongoDB::_Link;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 60 ▶use MongoDB::_Types qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
31 Boolish
32 BSONCodec
33 CompressionType
34 Document
35 NonNegNum
36 TopologyType
37 ZlibCompressionLevel
38 to_IxHash
39 60 );
40 60 ▶use Types::Standard qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
41 CodeRef
42 HashRef
43 ArrayRef
44 InstanceOf
45 Num
46 Str
47 Maybe
48 60 );
49 60 ▶use MongoDB::_Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 60 ▶use MongoDB::_Protocol;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 60 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 60 ▶use List::Util qw/first max min/;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 60 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 60 ▶use Time::HiRes qw/time usleep/;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 60 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 60 with $_ for qw(
59 MongoDB::Role::_TopologyMonitoring
60 );
61  
62 #--------------------------------------------------------------------------#
63 # attributes
64 #--------------------------------------------------------------------------#
65  
66 60 has uri => (
67 is => 'ro',
68 required => 1,
69 isa => InstanceOf['MongoDB::_URI'],
70 );
71  
72 60 has min_server_version => (
73 is => 'ro',
74 required => 1,
75 isa => Str,
76 );
77  
78 60 has max_wire_version => (
79 is => 'ro',
80 required => 1,
81 isa => Num,
82 );
83  
84 60 has min_wire_version => (
85 is => 'ro',
86 required => 1,
87 isa => Num,
88 );
89  
90 60 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 60 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 157 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
108 60 );
109  
110 has zlib_compression_level => (
111 is => 'ro',
112 isa => ZlibCompressionLevel,
113 157 ▶ default => sub { -1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
114 60 );
115  
116 60 has type => (
117 is => 'ro',
118 writer => '_set_type',
119 default => 'Unknown',
120 isa => TopologyType,
121 );
122  
123 60 has app_name => (
124 is => 'ro',
125 default => '',
126 isa => Str,
127 );
128  
129 60 has replica_set_name => (
130 is => 'ro',
131 default => '',
132 writer => '_set_replica_set_name', # :-)
133 isa => Str,
134 );
135  
136 60 has heartbeat_frequency_sec => (
137 is => 'ro',
138 default => 60,
139 isa => NonNegNum,
140 );
141  
142 60 has last_scan_time => (
143 is => 'ro',
144 default => EPOCH,
145 writer => '_set_last_scan_time',
146 isa => Num,
147 );
148  
149 60 has local_threshold_sec => (
150 is => 'ro',
151 default => 0.015,
152 isa => Num,
153 );
154  
155 60 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 331 ▶ default => sub { time() },
Subroutine __ANON__: called CC=1 SCAR=0.0
165 60 writer => '_set_next_scan_time',
166 isa => Num,
167 );
168  
169 60 has socket_check_interval_sec => (
170 is => 'ro',
171 default => 5,
172 isa => Num,
173 );
174  
175 60 has server_selection_timeout_sec => (
176 is => 'ro',
177 default => 60,
178 isa => Num,
179 );
180  
181 60 has server_selection_try_once => (
182 is => 'ro',
183 default => 1,
184 isa => Boolish,
185 );
186  
187 60 has server_selector => (
188 is => 'ro',
189 isa => Maybe[CodeRef],
190 );
191  
192 60 has ewma_alpha => (
193 is => 'ro',
194 default => 0.2,
195 isa => Num,
196 );
197  
198 has link_options => (
199 is => 'ro',
200 157 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
201 60 isa => HashRef,
202 );
203  
204 has bson_codec => (
205 is => 'ro',
206 331 ▶ default => sub { BSON->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
207 60 isa => BSONCodec,
208 init_arg => undef,
209 );
210  
211 60 has number_of_seeds => (
212 is => 'lazy',
213 builder => '_build_number_of_seeds',
214 isa => Num,
215 );
216  
217 60 has max_election_id => (
218 is => 'rw',
219 writer => '_set_max_election_id',
220 );
221  
222 60 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 60 has handshake_document => (
230 is => 'lazy',
231 isa => Document,
232 builder => '_build_handshake_document',
233 );
234  
235 # compatible wire protocol
236 60 has is_compatible => (
237 is => 'ro',
238 writer => '_set_is_compatible',
239 isa => Boolish,
240 );
241  
242 60 has compatibility_error => (
243 is => 'ro',
244 default => '',
245 writer => '_set_compatibility_error',
246 isa => Str,
247 );
248  
249 60 has wire_version_floor => (
250 is => 'ro',
251 writer => '_set_wire_version_floor',
252 default => 0,
253 );
254  
255 60 has wire_version_ceil => (
256 is => 'ro',
257 writer => '_set_wire_version_ceil',
258 default => 0,
259 );
260  
261 60 has current_primary => (
262 is => 'rwp',
263 clearer => '_clear_current_primary',
264 init_arg => undef,
265 );
266  
267 60 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 331 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
278 60 isa => HashRef[InstanceOf['MongoDB::_Server']],
279 );
280  
281 has _incompatible_servers => (
282 is => 'rw',
283 331 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
284 60 isa => ArrayRef[InstanceOf['MongoDB::_Server']],
285 );
286  
287 has links => (
288 is => 'ro',
289 331 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
290 60 isa => HashRef[InstanceOf['MongoDB::_Link']],
291 );
292  
293 has rtt_ewma_sec => (
294 is => 'ro',
295 331 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
296 60 isa => HashRef[Num],
297 );
298  
299 60 has cluster_time => (
300 is => 'rwp',
301 isa => Maybe[Document],
302 init_arg => undef,
303 default => undef,
304 );
305  
306 0 ▶sub update_cluster_time {
Subroutine update_cluster_time: not called CC=6 SCAR=37.4
Pod undocumented
307 0 my ( $self, $cluster_time ) = @_;
308  
309 return unless $cluster_time && exists $cluster_time->{clusterTime}
310 0 ▶ && ref($cluster_time->{clusterTime}) eq 'BSON::Timestamp';
Branch: 1 branch
BranchTrueFalseTotal
unless $cluster_time and exists $cluster_time->{"clusterTime"} and ref $cluster_time->{"clusterTime"} eq "BSON::Timestamp" 0 0 0
Condition0%0/6
$cluster_time and exists $cluster_time->{"clusterTime"} and ref $cluster_time->{"clusterTime"} eq "BSON::Timestamp"
!ll&&!rl&&r
000
$cluster_time and exists $cluster_time->{"clusterTime"}
!ll&&!rl&&r
000
MC/DC0%0/3
$cluster_time and exists $cluster_time->{"clusterTime"} and ref $cluster_time->{"clusterTime"} eq "BSON::Timestamp"
$cluster_time exists $cluster_time->{"clusterTime"} ref $cluster_time->{"clusterTime"} eq "BSON::Timestamp"
Truth table0%0/4
A and B and C
A= $cluster_timeB= exists $cluster_time->{"clusterTime"}C= ref $cluster_time->{"clusterTime"} eq "BSON::Timestamp"
ABCresult
0XX0
10X0
1100
1111
311  
312 # Only update the cluster time if it is more recent than the current entry
313 0 ▶ if ( !defined $self->cluster_time ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $self->cluster_time) { } 0 0 0
elsif ($cluster_time->{'clusterTime'} > $self->cluster_time->{'clusterTime'}) { } 0 0 0
314 0 $self->_set_cluster_time($cluster_time);
315 }
316 elsif ( $cluster_time->{'clusterTime'} > $self->cluster_time->{'clusterTime'} ) {
317 0 $self->_set_cluster_time($cluster_time);
318 }
319 0 return;
320 }
321  
322 #--------------------------------------------------------------------------#
323 # builders
324 #--------------------------------------------------------------------------#
325  
326 2 ▶sub _build_number_of_seeds {
Subroutine _build_number_of_seeds: called CC=1 SCAR=0.0
327 2 my ($self) = @_;
328 2 return scalar @{ $self->uri->hostids };
329 }
330  
331 0 ▶sub _truncate_for_handshake {
Subroutine _truncate_for_handshake: not called CC=1 SCAR=6.9
332 0 my $str = shift;
333 0 return substr( $str, 0, 64 );
334 }
335  
336 0 ▶sub _build_handshake_document {
Subroutine _build_handshake_document: not called CC=2 SCAR=17.9
337 0 my ($self) = @_;
338 0 my $driver_version_without_leading_v = substr( $VERSION, 1 );
339  
340 0 ▶ return to_IxHash(
Branch: 1 branch
BranchTrueFalseTotal
length $self->app_name ? : 0 0 0
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 331 ▶sub BUILD {
Subroutine BUILD: called CC=11 SCAR=24.5
Pod undocumented
356 331 my ($self) = @_;
357  
358 331 ▶ $self->publish_topology_opening
Branch: 1 branch
BranchTrueFalseTotal
if $self->monitoring_callback 72 259 331
359 if $self->monitoring_callback;
360  
361 331 ▶ $self->publish_old_topology_desc
Branch: 1 branch
BranchTrueFalseTotal
if $self->monitoring_callback 72 259 331
362 if $self->monitoring_callback;
363 331 my $type = $self->type;
364 331 my @addresses = @{ $self->uri->hostids };
365  
366 # clone bson codec to disable dt_type
367 331 $self->{bson_codec} = $self->bson_codec->clone( dt_type => undef );
368  
369 331 ▶ if ( my $set_name = $self->replica_set_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $set_name = $self->replica_set_name) 47 284 331
370 47 ▶ if ( $type eq 'Single' || $type eq 'ReplicaSetNoPrimary' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'Single' or $type eq 'ReplicaSetNoPrimary') { } 2 45 47
elsif ($type eq 'Unknown') { } 45 0 45
Condition66%2/3
$type eq "Single" or $type eq "ReplicaSetNoPrimary"
l!l&&r!l&&!r
0245
MC/DC50%1/2
$type eq "Single" or $type eq "ReplicaSetNoPrimary"
$type eq "Single" $type eq "ReplicaSetNoPrimary"
Truth table66%2/3
A or B
A= $type eq "Single"B= $type eq "ReplicaSetNoPrimary"
ABresult
1X1
011
000
371 # these are valid, so nothing to do here
372 }
373 elsif ( $type eq 'Unknown' ) {
374 45 $self->_set_type('ReplicaSetNoPrimary');
375 }
376 else {
377 0 MongoDB::InternalError->throw(
378 "deployment with set name '$set_name' may not be initialized as type '$type'");
379 }
380 }
381  
382 331 ▶ if ( $type eq 'Single' && @addresses > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "Single" and @addresses > 1) 0 331 331
Condition66%2/3
$type eq "Single" and @addresses > 1
!ll&&!rl&&r
306250
MC/DC0%0/2
$type eq "Single" and @addresses > 1
$type eq "Single" @addresses > 1
Truth table66%2/3
A and B
A= $type eq "Single"B= @addresses > 1
ABresult
0X0
100
111
383 0 MongoDB::InternalError->throw(
384 "topology type 'Single' cannot be used with multiple addresses: @addresses");
385 }
386  
387 331 $self->_add_address_as_unknown($_) for @addresses;
388  
389 331 ▶ $self->publish_new_topology_desc
Branch: 1 branch
BranchTrueFalseTotal
if $self->monitoring_callback 72 259 331
390 if $self->monitoring_callback;
391  
392 331 return;
393 }
394  
395 331 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=2 SCAR=6.9
Pod undocumented
396 331 my $self = shift;
397  
398 331 ▶ $self->publish_topology_closing
Branch: 1 branch
BranchTrueFalseTotal
if $self->monitoring_callback 72 259 331
399 if $self->monitoring_callback;
400  
401 331 return;
402 }
403  
404 54 ▶sub _check_for_uri_changes {
Subroutine _check_for_uri_changes: called CC=7 SCAR=34.7
405 54 my ($self) = @_;
406  
407 54 my $type = $self->type;
408 return unless
409 54 ▶ $type eq 'Sharded'
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "Sharded" or $type eq "Unknown" 54 0 54
Condition33%1/3
$type eq "Sharded" or $type eq "Unknown"
l!l&&r!l&&!r
0054
MC/DC0%0/2
$type eq "Sharded" or $type eq "Unknown"
$type eq "Sharded" $type eq "Unknown"
Truth table33%1/3
A or B
A= $type eq "Sharded"B= $type eq "Unknown"
ABresult
1X1
011
000
410 or $type eq 'Unknown';
411  
412 0 my @existing = @{ $self->uri->hostids };
413  
414 my $options = {
415 fallback_ttl_sec => $self->{heartbeat_frequency_sec},
416 0 };
417  
418 0 ▶ if ($self->uri->check_for_changes($options)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->uri->check_for_changes($options)) 0 0 0
419 0 my %new = map { ($_, 1) } @{ $self->uri->hostids };
420 0 for my $address (@existing) {
421 0 ▶ if (!$new{$address}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $new{$address}) { } 0 0 0
422 0 $self->_remove_address($address);
423 }
424 else {
425 0 delete $new{$address};
426 }
427 }
428 0 for my $address (keys %new) {
429 0 $self->_add_address_as_unknown($address);
430 }
431 }
432 }
433  
434 #--------------------------------------------------------------------------#
435 # public methods
436 #--------------------------------------------------------------------------#
437  
438 4548 ▶sub all_servers { return values %{ $_[0]->servers } }
Subroutine all_servers: called CC=1 SCAR=0.0
Pod undocumented
439  
440 0 ▶sub all_data_bearing_servers { return grep { $_->is_data_bearing } $_[0]->all_servers }
Subroutine all_data_bearing_servers: not called CC=1 SCAR=6.9
Pod undocumented
441  
442 108 ▶sub check_address {
Subroutine check_address: called CC=3 SCAR=12.3
Pod undocumented
443 108 my ( $self, $address ) = @_;
444  
445 108 my $link = $self->links->{$address};
446 108 ▶ if ( $link && $link->is_connected ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link and $link->is_connected) { } 0 108 108
Condition33%1/3
$link and $link->is_connected
!ll&&!rl&&r
10800
MC/DC0%0/2
$link and $link->is_connected
$link $link->is_connected
Truth table33%1/3
A and B
A= $linkB= $link->is_connected
ABresult
0X0
100
111
447 0 $self->_update_topology_from_link( $link, with_handshake => 0 );
448 }
449 else {
450 # initialize_link will call update_topology_from_link
451 108 $self->_initialize_link($address);
452 }
453  
454 108 return;
455 }
456  
457 0 ▶sub close_all_links {
Subroutine close_all_links: not called CC=2 SCAR=17.9
Pod undocumented
458 0 my ($self) = @_;
459 0 delete $self->links->{ $_->address } for $self->all_servers;
460 0 return;
461 }
462  
463 54 ▶sub _maybe_get_txn_error_labels_and_unpin_from {
Subroutine _maybe_get_txn_error_labels_and_unpin_from: called CC=5 SCAR=27.4
464 54 my $op = shift;
465 54 ▶ return () unless defined $op
Branch: 1 branch
BranchTrueFalseTotal
unless defined $op and defined $op->session 54 0 54
Condition33%1/3
defined $op and defined $op->session
!ll&&!rl&&r
5400
MC/DC0%0/2
defined $op and defined $op->session
defined $op defined $op->session
Truth table33%1/3
A and B
A= defined $opB= defined $op->session
ABresult
0X0
100
111
466 && defined $op->session;
467 0 ▶ if ( $op->session->_in_transaction_state( TXN_STARTING, TXN_IN_PROGRESS ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($op->session->_in_transaction_state('starting', 'in_progress')) { } 0 0 0
elsif ($op->session->_in_transaction_state('committed')) { } 0 0 0
468 0 $op->session->_unpin_address;
469 0 return ( error_labels => [ TXN_TRANSIENT_ERROR_MSG ] );
470 } elsif ( $op->session->_in_transaction_state( TXN_COMMITTED ) ) {
471 0 return ( error_labels => [ TXN_UNKNOWN_COMMIT_MSG ] );
472 }
473 0 return ();
474 }
475  
476 0 ▶sub get_readable_link {
Subroutine get_readable_link: not called CC=15 SCAR=54.8
Pod undocumented
477 0 my ( $self, $op ) = @_;
478 0 $self->_check_for_uri_changes;
479  
480 0 ▶ my $read_pref = defined $op ? $op->read_preference : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $op ? : 0 0 0
481  
482 0 ▶ my $mode = $read_pref ? lc $read_pref->mode : 'primary';
Branch: 1 branch
BranchTrueFalseTotal
$read_pref ? : 0 0 0
483 0 ▶ my $method =
Branch: 2 branches
BranchTrueFalseTotal
$self->type eq 'Single' ? : 0 0 0
$self->type eq 'Sharded' ? : 0 0 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 ▶ if ( $mode eq 'primary' && $self->current_primary && $self->next_scan_time > time() )
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "primary" and $self->current_primary and $self->next_scan_time > &MongoDB::_Topology::time()) 0 0 0
Condition0%0/6
$mode eq "primary" and $self->current_primary and $self->next_scan_time > &MongoDB::_Topology::time()
!ll&&!rl&&r
000
$mode eq "primary" and $self->current_primary
!ll&&!rl&&r
000
MC/DC0%0/3
$mode eq "primary" and $self->current_primary and $self->next_scan_time > &MongoDB::_Topology::time()
$mode eq "primary" $self->current_primary $self->next_scan_time > &MongoDB::_Topology::time()
Truth table0%0/4
A and B and C
A= $mode eq "primary"B= $self->current_primaryC= $self->next_scan_time > &MongoDB::_Topology::time()
ABCresult
0XX0
10X0
1100
1111
489 {
490 0 my $link = $self->_get_server_link( $self->current_primary, $method );
491 0 ▶ return $link if $link;
Branch: 1 branch
BranchTrueFalseTotal
if $link 0 0 0
492 }
493  
494 0 ▶ while ( my $server = $self->_selection_timeout( $method, $read_pref ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $server = $self->_selection_timeout($method, $read_pref)) 0 0 0
495 0 my $link = $self->_get_server_link( $server, $method, $read_pref );
496 0 ▶ if ($link) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link) 0 0 0
497 $self->_set_current_primary($server)
498 if $mode eq 'primary'
499 && ( $self->type eq "ReplicaSetWithPrimary"
500 0 ▶ || 1 == keys %{ $self->servers } );
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "primary" and $self->type eq "ReplicaSetWithPrimary" || 1 == keys %{$self->servers;} 0 0 0
Condition0%0/6
$mode eq "primary" and $self->type eq "ReplicaSetWithPrimary" || 1 == keys %{$self->servers;}
!ll&&!rl&&r
000
$self->type eq "ReplicaSetWithPrimary" or 1 == keys %{$self->servers;}
l!l&&r!l&&!r
000
MC/DC0%0/3
$mode eq "primary" and $self->type eq "ReplicaSetWithPrimary" || 1 == keys %{$self->servers;}
$mode eq "primary" $self->type eq "ReplicaSetWithPrimary" 1 == keys %{$self->servers;}
Truth table0%0/4
A and B or C
A= $mode eq "primary"B= $self->type eq "ReplicaSetWithPrimary"C= 1 == keys %{$self->servers;}
ABCresult
0XX0
1000
11X1
1011
501 0 return $link;
502 }
503 }
504  
505 0 ▶ my $rp = $read_pref ? $read_pref->as_string : 'primary';
Branch: 1 branch
BranchTrueFalseTotal
$read_pref ? : 0 0 0
506  
507 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 0 ▶sub get_specific_link {
Subroutine get_specific_link: not called CC=3 SCAR=24.8
Pod undocumented
515 0 my ( $self, $address, $op ) = @_;
516 0 $self->_check_for_uri_changes;
517  
518 0 my $server = $self->servers->{$address};
519 0 ▶ if ( $server && ( my $link = $self->_get_server_link($server) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($server and my $link = $self->_get_server_link($server)) { } 0 0 0
Condition0%0/3
$server and my $link = $self->_get_server_link($server)
!ll&&!rl&&r
000
MC/DC0%0/2
$server and my $link = $self->_get_server_link($server)
$server my $link = $self->_get_server_link($server)
Truth table0%0/3
A and B
A= $serverB= my $link = $self->_get_server_link($server)
ABresult
0X0
100
111
520 0 return $link;
521 }
522 else {
523 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 54 ▶sub get_writable_link {
Subroutine get_writable_link: called CC=10 SCAR=37.1
Pod undocumented
531 54 my ( $self, $op ) = @_;
532 54 $self->_check_for_uri_changes;
533  
534 54 ▶ my $method =
Branch: 1 branch
BranchTrueFalseTotal
$self->type eq 'Single' || $self->type eq 'Sharded' ? : 54 0 54
Condition33%1/3
$self->type eq "Single" or $self->type eq "Sharded"
l!l&&r!l&&!r
5400
MC/DC0%0/2
$self->type eq "Single" or $self->type eq "Sharded"
$self->type eq "Single" $self->type eq "Sharded"
Truth table33%1/3
A or B
A= $self->type eq "Single"B= $self->type eq "Sharded"
ABresult
1X1
011
000
535 ( $self->type eq "Single" || $self->type eq "Sharded" )
536 ? '_find_available_server'
537 : "_find_primary_server";
538  
539  
540 54 ▶ if ( $self->current_primary && $self->next_scan_time > time() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->current_primary and $self->next_scan_time > &MongoDB::_Topology::time()) 0 54 54
Condition33%1/3
$self->current_primary and $self->next_scan_time > &MongoDB::_Topology::time()
!ll&&!rl&&r
5400
MC/DC0%0/2
$self->current_primary and $self->next_scan_time > &MongoDB::_Topology::time()
$self->current_primary $self->next_scan_time > &MongoDB::_Topology::time()
Truth table33%1/3
A and B
A= $self->current_primaryB= $self->next_scan_time > &MongoDB::_Topology::time()
ABresult
0X0
100
111
541 0 my $link = $self->_get_server_link( $self->current_primary, $method );
542 0 ▶ return $link if $link;
Branch: 1 branch
BranchTrueFalseTotal
if $link 0 0 0
543 }
544  
545 54 ▶ while ( my $server = $self->_selection_timeout($method) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $server = $self->_selection_timeout($method)) 0 54 54
546 0 my $link = $self->_get_server_link( $server, $method );
547 0 ▶ if ($link) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link) 0 0 0
548 $self->_set_current_primary($server)
549 if $self->type eq "ReplicaSetWithPrimary"
550 0 ▶ || 1 == keys %{ $self->servers };
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "ReplicaSetWithPrimary" or 1 == keys %{$self->servers;} 0 0 0
Condition0%0/3
$self->type eq "ReplicaSetWithPrimary" or 1 == keys %{$self->servers;}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->type eq "ReplicaSetWithPrimary" or 1 == keys %{$self->servers;}
$self->type eq "ReplicaSetWithPrimary" 1 == keys %{$self->servers;}
Truth table0%0/3
A or B
A= $self->type eq "ReplicaSetWithPrimary"B= 1 == keys %{$self->servers;}
ABresult
1X1
011
000
551 0 return $link;
552 }
553 }
554  
555 MongoDB::SelectionError->throw(
556 54 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 0 ▶sub mark_server_unknown {
Subroutine mark_server_unknown: not called CC=2 SCAR=17.9
Pod undocumented
565 0 my ( $self, $server, $error, $no_cooldown ) = @_;
566 0 ▶ $self->_reset_address_to_unknown( $server->address, $error, $no_cooldown // EPOCH );
Condition0%0/2
$no_cooldown // 0
l!l
00
MC/DC0%0/1
$no_cooldown // 0
$no_cooldown
Truth table0%0/2
A
A= $no_cooldown
Aresult
11
00
567 0 return;
568 }
569  
570 0 ▶sub mark_stale {
Subroutine mark_stale: not called CC=1 SCAR=6.9
Pod undocumented
571 0 my ($self) = @_;
572 0 $self->_set_stale(1);
573 0 return;
574 }
575  
576 1080 ▶sub scan_all_servers {
Subroutine scan_all_servers: called CC=8 SCAR=22.3
Pod undocumented
577 1080 my ($self, $force) = @_;
578  
579 1080 my ( $next, @ordinary, @to_check );
580 1080 my $start_time = time;
581 1080 ▶ my $cooldown_time = $force ? $start_time : $start_time - COOLDOWN_SECS;
Branch: 1 branch
BranchTrueFalseTotal
$force ? : 0 1080 1080
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 1080 while (1) {
588 @to_check =
589 grep {
590 1188 ▶ $_->type eq 'Unknown'
Branch: 1 branch
BranchTrueFalseTotal
$_->type eq 'Unknown' ? : 1188 0 1188
591 ? !$_->updated_since($cooldown_time)
592 : !$_->updated_since($start_time)
593 } $self->all_servers;
594  
595 1188 ▶ last unless @to_check;
Branch: 1 branch
BranchTrueFalseTotal
unless @to_check 1080 108 1188
596  
597 108 ▶ if ( $next = first { $_->type eq 'RSPrimary' } @to_check ) {
Subroutine __ANON__: called CC=1 SCAR=0.5
Branch: 3 branches
BranchTrueFalseTotal
if ($next = &first(sub { $_->type eq 'RSPrimary'; } , @to_check)) { } 0 108 108
elsif ($next = &first(sub { $_->type eq 'PossiblePrimary'; } , @to_check)) { } 0 108 108
elsif (@ordinary = grep({$_->type ne 'Unknown' and $_->type ne 'RSGhost';} @to_check)) { } 0 108 108
598 0 $self->check_address( $next->address );
599 }
600 108 ▶ elsif ( $next = first { $_->type eq 'PossiblePrimary' } @to_check ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
601 0 $self->check_address( $next->address );
602 }
603 108 ▶ elsif ( @ordinary = grep { $_->type ne 'Unknown' && $_->type ne 'RSGhost' } @to_check ) {
Condition33%1/3
$_->type ne 'Unknown' && $_->type ne 'RSGhost'
!ll&&!rl&&r
10800
MC/DC0%0/2
$_->type ne 'Unknown' && $_->type ne 'RSGhost'
$_->type ne 'Unknown' $_->type ne 'RSGhost'
Truth table33%1/3
A && B
A= $_->type ne 'Unknown'B= $_->type ne 'RSGhost'
ABresult
0X0
100
111
604 0 $self->_check_oldest_server(@ordinary);
605 }
606 else {
607 108 $self->_check_oldest_server(@to_check);
608 }
609 }
610  
611 1080 my $now = time();
612 1080 $self->_set_last_scan_time( $now );
613 1080 $self->_set_next_scan_time( $now + $self->heartbeat_frequency_sec );
614 1080 $self->_set_stale( 0 );
615 1080 $self->_check_wire_versions;
616 1080 return;
617 }
618  
619 0 ▶sub status_struct {
Subroutine status_struct: not called CC=4 SCAR=30.0
Pod undocumented
620 0 my ($self) = @_;
621 0 my $status = { topology_type => $self->type, };
622 0 ▶ $status->{replica_set_name} = $self->replica_set_name if $self->replica_set_name;
Branch: 1 branch
BranchTrueFalseTotal
if $self->replica_set_name 0 0 0
623  
624 # convert from [sec, microsec] array to floating point
625 0 $status->{last_scan_time} = $self->last_scan_time;
626  
627 0 my $rtt_hash = $self->rtt_ewma_sec;
628 0 my $ss = $status->{servers} = [];
629 0 for my $server ( $self->all_servers ) {
630 0 my $addr = $server->address;
631 0 my $server_struct = $server->status_struct;
632 0 ▶ if ( defined $rtt_hash->{$addr} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rtt_hash->{$addr}) 0 0 0
633 0 $server_struct->{ewma_rtt_sec} = $rtt_hash->{$addr};
634 }
635 0 push @$ss, $server_struct;
636 }
637 0 return $status;
638 }
639  
640 #--------------------------------------------------------------------------#
641 # private methods
642 #--------------------------------------------------------------------------#
643  
644 520 ▶sub _add_address_as_unknown {
Subroutine _add_address_as_unknown: called CC=4 SCAR=13.9
645 520 my ( $self, $address, $last_update, $error ) = @_;
646 520 ▶ $error = $error ? "$error" : "";
Branch: 1 branch
BranchTrueFalseTotal
$error ? : 118 402 520
647 520 $error =~ s/ at \S+ line \d+.*//ms;
648  
649 520 ▶ $self->publish_server_opening($address)
Branch: 1 branch
BranchTrueFalseTotal
if $self->monitoring_callback 153 367 520
650 if $self->monitoring_callback;
651  
652 520 ▶ return $self->servers->{$address} = MongoDB::_Server->new(
Condition100%2/2
$last_update || 0
l!l
118402
MC/DC100%1/1
$last_update || 0
$last_update
Truth table100%2/2
A
A= $last_update
Aresult
11
00
653 address => $address,
654 last_update_time => $last_update || EPOCH,
655 error => $error,
656 );
657 }
658  
659 121 ▶sub _check_for_primary {
Subroutine _check_for_primary: called CC=2 SCAR=6.9
660 121 my ($self) = @_;
661 121 ▶ if ( 0 == $self->_primaries ) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == $self->_primaries) 17 104 121
662 17 $self->_set_type('ReplicaSetNoPrimary');
663 17 $self->_clear_current_primary;
664 17 return 0;
665 }
666 104 return 1;
667 }
668  
669 108 ▶sub _check_oldest_server {
Subroutine _check_oldest_server: called CC=2 SCAR=8.1
670 108 my ( $self, @to_check ) = @_;
671  
672 my @ordered =
673 108 map { $_->[0] }
674 0 ▶ sort { $a->[1] <=> $b->[1] || rand() <=> rand() } # random if equal
Condition0%0/3
$MongoDB::_Topology::a->[1] <=> $MongoDB::_Topology::b->[1] || rand() <=> rand()
l!l&&r!l&&!r
000
MC/DC0%0/2
$MongoDB::_Topology::a->[1] <=> $MongoDB::_Topology::b->[1] || rand() <=> rand()
$MongoDB::_Topology::a->[1] <=> $MongoDB::_Topology::b->[1] rand() <=> rand()
Truth table0%0/3
A || B
A= $MongoDB::_Topology::a->[1] <=> $MongoDB::_Topology::b->[1]B= rand() <=> rand()
ABresult
1X1
011
000
675 108 map { [ $_, $_->last_update_time ] } # ignore partial secs
676 @to_check;
677  
678 108 $self->check_address( $ordered[0]->address );
679  
680 108 return;
681 }
682  
683 60 my $max_int32 = 2147483647;
684  
685 1235 ▶sub _check_wire_versions {
Subroutine _check_wire_versions: called CC=8 SCAR=20.8
686 1235 my ($self) = @_;
687  
688 1235 my $compat = 1;
689 1235 my $min_seen = $max_int32;
690 1235 my $max_seen = 0;
691 1235 for my $server ( grep { $_->is_available } $self->all_servers ) {
692 my ( $server_min_wire_version, $server_max_wire_version ) =
693 219 @{ $server->is_master }{qw/minWireVersion maxWireVersion/};
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 ▶ $server_max_wire_version = 0 unless defined $server_max_wire_version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $server_max_wire_version 4 215 219
698 219 ▶ $server_min_wire_version = 0 unless defined $server_min_wire_version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $server_min_wire_version 4 215 219
699  
700 219 ▶ if ( $server_min_wire_version > $self->max_wire_version
Branch: 1 branch
BranchTrueFalseTotal
if ($server_min_wire_version > $self->max_wire_version or $server_max_wire_version < $self->min_wire_version) 3 216 219
Condition66%2/3
$server_min_wire_version > $self->max_wire_version or $server_max_wire_version < $self->min_wire_version
l!l&&r!l&&!r
30216
MC/DC50%1/2
$server_min_wire_version > $self->max_wire_version or $server_max_wire_version < $self->min_wire_version
$server_min_wire_version > $self->max_wire_version $server_max_wire_version < $self->min_wire_version
Truth table66%2/3
A or B
A= $server_min_wire_version > $self->max_wire_versionB= $server_max_wire_version < $self->min_wire_version
ABresult
1X1
011
000
701 || $server_max_wire_version < $self->min_wire_version ) {
702 3 $compat = 0;
703 3 push @{ $self->_incompatible_servers }, $server;
704 }
705  
706 219 ▶ $min_seen = $server_max_wire_version if $server_max_wire_version < $min_seen;
Branch: 1 branch
BranchTrueFalseTotal
if $server_max_wire_version < $min_seen 134 85 219
707 219 ▶ $max_seen = $server_max_wire_version if $server_max_wire_version > $max_seen;
Branch: 1 branch
BranchTrueFalseTotal
if $server_max_wire_version > $max_seen 135 84 219
708 }
709 1235 $self->_set_is_compatible($compat);
710 1235 $self->_set_wire_version_floor($min_seen);
711 1235 $self->_set_wire_version_ceil($max_seen);
712  
713 1235 return;
714 }
715  
716 261 ▶sub _update_ls_timeout_minutes {
Subroutine _update_ls_timeout_minutes: called CC=4 SCAR=13.9
717 261 my ( $self, $new_server ) = @_;
718  
719 261 my @data_bearing_servers = grep { $_->is_data_bearing } $self->all_servers;
720 my $timeout = min map {
721 # use -1 as a flag to prevent undefined warnings
722 261 ▶ defined $_->logical_session_timeout_minutes
Branch: 1 branch
BranchTrueFalseTotal
defined $_->logical_session_timeout_minutes ? : 15 140 155
723 ? $_->logical_session_timeout_minutes
724 : -1
725 } @data_bearing_servers;
726 # min will return undef if the array is empty
727 261 ▶ if ( defined $timeout && $timeout < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timeout and $timeout < 0) 117 144 261
Condition100%3/3
defined $timeout and $timeout < 0
!ll&&!rl&&r
1359117
MC/DC100%2/2
defined $timeout and $timeout < 0
defined $timeout $timeout < 0
Truth table100%3/3
A and B
A= defined $timeoutB= $timeout < 0
ABresult
0X0
100
111
728 117 $timeout = undef;
729 }
730 261 $self->_set_logical_session_timeout_minutes( $timeout );
731 261 return;
732 }
733  
734 0 ▶sub _supports_sessions {
Subroutine _supports_sessions: not called CC=4 SCAR=30.0
735 0 my ( $self ) = @_;
736  
737 0 ▶ $self->scan_all_servers if $self->stale;
Branch: 1 branch
BranchTrueFalseTotal
if $self->stale 0 0 0
738  
739 0 my @servers = $self->all_servers;
740 0 ▶ return if @servers == 1 && $servers[0]->type eq 'Standalone';
Branch: 1 branch
BranchTrueFalseTotal
if @servers == 1 and $servers[0]->type eq "Standalone" 0 0 0
Condition0%0/3
@servers == 1 and $servers[0]->type eq "Standalone"
!ll&&!rl&&r
000
MC/DC0%0/2
@servers == 1 and $servers[0]->type eq "Standalone"
@servers == 1 $servers[0]->type eq "Standalone"
Truth table0%0/3
A and B
A= @servers == 1B= $servers[0]->type eq "Standalone"
ABresult
0X0
100
111
741  
742 0 return defined $self->logical_session_timeout_minutes;
743 }
744  
745 0 ▶sub _supports_transactions {
Subroutine _supports_transactions: not called CC=4 SCAR=30.0
746 0 my ( $self ) = @_;
747  
748 0 ▶ return unless $self->_supports_sessions;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_supports_sessions 0 0 0
749 0 ▶ return $self->_supports_mongos_pinning_transactions if $self->type eq 'Sharded';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "Sharded" 0 0 0
750 0 ▶ return if $self->wire_version_ceil < 7;
Branch: 1 branch
BranchTrueFalseTotal
if $self->wire_version_ceil < 7 0 0 0
751 0 return 1;
752 }
753  
754 # Seperated out so can be used in dispatch logic
755 0 ▶sub _supports_mongos_pinning_transactions {
Subroutine _supports_mongos_pinning_transactions: not called CC=3 SCAR=24.8
756 0 my ( $self ) = @_;
757  
758 # Separated out so that it doesnt return 1 for wire version 8 non sharded
759 0 ▶ return if $self->wire_version_ceil < 8;
Branch: 1 branch
BranchTrueFalseTotal
if $self->wire_version_ceil < 8 0 0 0
760 # This extra sharded check is required so this test can be standalone
761 0 ▶ return if $self->type ne 'Sharded';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type ne "Sharded" 0 0 0
762 0 return 1;
763 }
764  
765 76 ▶sub _check_staleness_compatibility {
Subroutine _check_staleness_compatibility: called CC=7 SCAR=19.5
766 76 my ($self, $read_pref) = @_;
767 76 ▶ my $max_staleness_sec = $read_pref ? $read_pref->max_staleness_seconds : -1;
Branch: 1 branch
BranchTrueFalseTotal
$read_pref ? : 76 0 76
768  
769 76 ▶ if ( $max_staleness_sec > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_staleness_sec > 0) 37 39 76
770 37 ▶ if ( $self->wire_version_floor < 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->wire_version_floor < 5) 5 32 37
771 5 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 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type eq "ReplicaSetWithPrimary" || $self->type eq "ReplicaSetNoPrimary" and $max_staleness_sec < &max(90, $self->heartbeat_frequency_sec + 10)) 3 29 32
Condition100%6/6
$self->type eq "ReplicaSetWithPrimary" || $self->type eq "ReplicaSetNoPrimary" and $max_staleness_sec < &max(90, $self->heartbeat_frequency_sec + 10)
!ll&&!rl&&r
3263
$self->type eq "ReplicaSetWithPrimary" or $self->type eq "ReplicaSetNoPrimary"
l!l&&r!l&&!r
16133
MC/DC100%3/3
$self->type eq "ReplicaSetWithPrimary" || $self->type eq "ReplicaSetNoPrimary" and $max_staleness_sec < &max(90, $self->heartbeat_frequency_sec + 10)
$self->type eq "ReplicaSetWithPrimary" $self->type eq "ReplicaSetNoPrimary" $max_staleness_sec < &max(90, $self->heartbeat_frequency_sec + 10)
Truth table100%5/5
A or B and C
A= $self->type eq "ReplicaSetWithPrimary"B= $self->type eq "ReplicaSetNoPrimary"C= $max_staleness_sec < &max(90, $self->heartbeat_frequency_sec + 10)
ABCresult
00X0
1X00
0100
1X11
0111
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 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 return;
790 }
791  
792 0 ▶sub _dump {
Subroutine _dump: not called CC=1 SCAR=6.9
793 0 my ($self) = @_;
794 0 print $self->_status_string . "\n";
795 }
796  
797 42 ▶sub _eligible {
Subroutine _eligible: called CC=6 SCAR=17.9
798 42 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 ▶ if ( $read_pref->max_staleness_seconds > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read_pref->max_staleness_seconds > 0) 19 23 42
803 19 @candidates = $self->_filter_fresh_servers($read_pref, @candidates );
804 18 ▶ return unless @candidates;
Branch: 1 branch
BranchTrueFalseTotal
unless @candidates 1 17 18
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 ▶ if ( ! $read_pref->has_empty_tag_sets ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($read_pref->has_empty_tag_sets) 27 13 40
810 27 for my $ts ( @{ $read_pref->tag_sets } ) {
811 31 ▶ if ( my @ts_candidates = grep { $_->matches_tag_set($ts) } @candidates ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ts_candidates) = grep({$_->matches_tag_set($ts);} @candidates)) 18 13 31
812 18 return @ts_candidates;
813 }
814 }
815 9 return;
816 }
817  
818 13 return @candidates;
819 }
820  
821 19 ▶sub _filter_fresh_servers {
Subroutine _filter_fresh_servers: called CC=2 SCAR=6.9
822 19 my ($self, $read_pref, @candidates) = @_;
823  
824 # all values should be floating point seconds
825 19 my $max_staleness_sec = $read_pref->max_staleness_seconds;
826 19 my $heartbeat_frequency_sec = $self->heartbeat_frequency_sec;
827  
828 19 ▶ if ( $self->type eq 'ReplicaSetWithPrimary' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type eq 'ReplicaSetWithPrimary') { } 10 9 19
829 10 my ($primary) = $self->_primaries;
830  
831 # all values should be floating point seconds
832 10 my $p_last_write_date = $primary->last_write_date;
833 10 my $p_last_update_time = $primary->last_update_time;
834  
835 16 return map { $_->[0] }
836 26 grep { $_->[1] <= $max_staleness_sec }
837 map {
838 10 [
839 26 $_,
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 my ($smax) = map { $_->[0] }
849 21 sort { $b->[1] <=> $a->[1] }
850 9 map { [ $_, $_->last_write_date ] } $self->_secondaries;
851 9 my $smax_last_write_date = $smax->last_write_date;
852  
853 15 return map { $_->[0] }
854 23 grep { $_->[1] <= $max_staleness_sec }
855 map {
856 8 [ $_, $smax_last_write_date - $_->last_write_date + $heartbeat_frequency_sec ]
857 } @candidates;
858 }
859 }
860  
861 # This works for reads and writes; for writes, $read_pref will be undef
862 1097 ▶sub _find_available_server {
Subroutine _find_available_server: called CC=4 SCAR=13.9
863 1097 my ( $self, $read_pref, @candidates ) = @_;
864 1097 ▶ $self->_check_staleness_compatibility($read_pref) if $read_pref;
Branch: 1 branch
BranchTrueFalseTotal
if $read_pref 3 1094 1097
865 1096 ▶ push @candidates, $self->all_servers unless @candidates;
Branch: 1 branch
BranchTrueFalseTotal
unless @candidates 1096 0 1096
866 1096 my $selector = $self->server_selector;
867 return $self->_get_server_in_latency_window(
868 1096 ▶ [ grep { $_->is_available }
Branch: 1 branch
BranchTrueFalseTotal
$selector ? : 6 1090 1096
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 7 ▶sub _find_readable_mongos_server {
Subroutine _find_readable_mongos_server: called CC=3 SCAR=11.2
876 7 my ( $self, $read_pref, @candidates ) = @_;
877 7 $self->_check_staleness_compatibility($read_pref);
878 6 ▶ push @candidates, $self->all_servers unless @candidates;
Branch: 1 branch
BranchTrueFalseTotal
unless @candidates 6 0 6
879 6 my $selector = $self->server_selector;
880 return $self->_get_server_in_latency_window(
881 6 ▶ [ grep { $_->is_available }
Branch: 1 branch
BranchTrueFalseTotal
$selector ? : 0 6 6
882 $selector ? $selector->(@candidates) : @candidates ]
883 );
884 }
885  
886 23 ▶sub _find_nearest_server {
Subroutine _find_nearest_server: called CC=3 SCAR=11.2
887 23 my ( $self, $read_pref, @candidates ) = @_;
888 23 $self->_check_staleness_compatibility($read_pref);
889 18 ▶ push @candidates, ( $self->_primaries, $self->_secondaries ) unless @candidates;
Branch: 1 branch
BranchTrueFalseTotal
unless @candidates 18 0 18
890 18 my @suitable = $self->_eligible( $read_pref, @candidates );
891 17 my $selector = $self->server_selector;
892 17 ▶ return $self->_get_server_in_latency_window(
Branch: 1 branch
BranchTrueFalseTotal
$selector ? : 0 17 17
893 [ $selector ? $selector->(@suitable) : @suitable ]
894 );
895 }
896  
897 18 ▶sub _find_primary_server {
Subroutine _find_primary_server: called CC=3 SCAR=11.3
898 18 my ( $self, undef, @candidates ) = @_;
899 18 ▶ return $self->current_primary
Branch: 1 branch
BranchTrueFalseTotal
if $self->current_primary 0 18 18
900 if $self->current_primary;
901 18 ▶ push @candidates, $self->all_servers unless @candidates;
Branch: 1 branch
BranchTrueFalseTotal
unless @candidates 18 0 18
902 18 ▶ return first { $_->is_writable } @candidates;
Subroutine __ANON__: called CC=1 SCAR=0.0
903 }
904  
905 8 ▶sub _find_primarypreferred_server {
Subroutine _find_primarypreferred_server: called CC=2 SCAR=6.9
906 8 my ( $self, $read_pref, @candidates ) = @_;
907 8 $self->_check_staleness_compatibility($read_pref);
908 7 ▶ return $self->_find_primary_server(@candidates)
Condition100%3/3
$self->_find_primary_server(@candidates) || $self->_find_secondary_server($read_pref, @candidates)
l!l&&r!l&&!r
331
MC/DC100%2/2
$self->_find_primary_server(@candidates) || $self->_find_secondary_server($read_pref, @candidates)
$self->_find_primary_server(@candidates) $self->_find_secondary_server($read_pref, @candidates)
Truth table100%3/3
A || B
A= $self->_find_primary_server(@candidates)B= $self->_find_secondary_server($read_pref, @candidates)
ABresult
1X1
011
000
909 || $self->_find_secondary_server( $read_pref, @candidates );
910 }
911  
912 24 ▶sub _find_secondary_server {
Subroutine _find_secondary_server: called CC=3 SCAR=11.2
913 24 my ( $self, $read_pref, @candidates ) = @_;
914 24 $self->_check_staleness_compatibility($read_pref);
915 24 ▶ push @candidates, $self->_secondaries unless @candidates;
Branch: 1 branch
BranchTrueFalseTotal
unless @candidates 24 0 24
916 24 my @suitable = $self->_eligible( $read_pref, @candidates );
917 24 my $selector = $self->server_selector;
918 24 ▶ return $self->_get_server_in_latency_window(
Branch: 1 branch
BranchTrueFalseTotal
$selector ? : 0 24 24
919 [ $selector ? $selector->(@suitable) : @suitable ]
920 );
921 }
922  
923 11 ▶sub _find_secondarypreferred_server {
Subroutine _find_secondarypreferred_server: called CC=2 SCAR=6.9
924 11 my ( $self, $read_pref, @candidates ) = @_;
925 11 $self->_check_staleness_compatibility($read_pref);
926 11 ▶ return $self->_find_secondary_server( $read_pref, @candidates )
Condition100%3/3
$self->_find_secondary_server($read_pref, @candidates) || $self->_find_primary_server(@candidates)
l!l&&r!l&&!r
632
MC/DC100%2/2
$self->_find_secondary_server($read_pref, @candidates) || $self->_find_primary_server(@candidates)
$self->_find_secondary_server($read_pref, @candidates) $self->_find_primary_server(@candidates)
Truth table100%3/3
A || B
A= $self->_find_secondary_server($read_pref, @candidates)B= $self->_find_primary_server(@candidates)
ABresult
1X1
011
000
927 || $self->_find_primary_server(@candidates);
928 }
929  
930 1143 ▶sub _get_server_in_latency_window {
Subroutine _get_server_in_latency_window: called CC=3 SCAR=11.0
931 1143 my ( $self, $servers ) = @_;
932 1143 ▶ return unless @$servers;
Branch: 1 branch
BranchTrueFalseTotal
unless @$servers 1091 52 1143
933 52 ▶ return $servers->[0] if @$servers == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$servers == 1 20 32 52
934  
935 # order servers by RTT EWMA
936 32 my $rtt_hash = $self->rtt_ewma_sec;
937 my @sorted =
938 83 sort { $a->{rtt} <=> $b->{rtt} }
939 32 map { { server => $_, rtt => $rtt_hash->{ $_->address } } } @$servers;
940 # lowest RTT is always in the windows
941 32 my @in_window = shift @sorted;
942  
943 # add any other servers in window and return a random one
944 32 my $max_rtt = $in_window[0]->{rtt} + $self->local_threshold_sec;
945 32 push @in_window, grep { $_->{rtt} <= $max_rtt } @sorted;
946 32 return $in_window[ int( rand(@in_window) ) ]->{server};
947 }
948  
949 60 my $PRIMARY = MongoDB::ReadPreference->new;
950 60 my $PRIMARY_PREF = MongoDB::ReadPreference->new( mode => 'primaryPreferred' );
951  
952 0 ▶sub _ping_server {
Subroutine _ping_server: not called CC=1 SCAR=6.9
953 0 my ($self, $link) = @_;
954 0 return eval {
955 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 $op->execute( $link )->output;
964 };
965 }
966  
967  
968 0 ▶sub _get_server_link {
Subroutine _get_server_link: not called CC=9 SCAR=45.0
969 0 my ( $self, $server, $method, $read_pref ) = @_;
970 0 my $address = $server->address;
971 0 my $link = $self->links->{$address};
972  
973 # if no link, make a new connection or give up
974 0 ▶ $link = $self->_initialize_link($address) unless $link && $link->connected;
Branch: 1 branch
BranchTrueFalseTotal
unless $link and $link->connected 0 0 0
Condition0%0/3
$link and $link->connected
!ll&&!rl&&r
000
MC/DC0%0/2
$link and $link->connected
$link $link->connected
Truth table0%0/3
A and B
A= $linkB= $link->connected
ABresult
0X0
100
111
975 0 ▶ return unless $link;
Branch: 1 branch
BranchTrueFalseTotal
unless $link 0 0 0
976  
977 # for idle links, refresh the server and verify validity
978 0 ▶ if ( time - $link->last_used > $self->socket_check_interval_sec ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&MongoDB::_Topology::time() - $link->last_used > $self->socket_check_interval_sec) 0 0 0
979 0 ▶ return $link if $self->_ping_server;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_ping_server 0 0 0
980 0 $self->mark_server_unknown(
981 $server, 'Lost connection with the server'
982 );
983 0 $self->check_address($address);
984  
985 # topology might have dropped the server
986 0 ▶ $server = $self->servers->{$address}
Branch: 1 branch
BranchTrueFalseTotal
$server = $self->servers->{$address} or return 0 0 0
987 or return;
988  
989 0 my $fresh_link = $self->links->{$address};
990 0 ▶ return $fresh_link if !$method;
Branch: 1 branch
BranchTrueFalseTotal
unless $method 0 0 0
991  
992 # verify selection criteria
993 0 ▶ return $self->$method( $read_pref, $server ) ? $fresh_link : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->$method($read_pref, $server) ? : 0 0 0
994 }
995  
996 0 return $link;
997 }
998  
999 108 ▶sub _initialize_link {
Subroutine _initialize_link: called CC=10 SCAR=38.9
1000 108 my ( $self, $address ) = @_;
1001  
1002 my $link = eval {
1003 108 MongoDB::_Link->new( %{$self->link_options}, address => $address )->connect;
1004 108 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
my $link = eval { do { "MongoDB::_Link"->new(%{$self->link_options;}, "address", $address)->connect } } or do { my $error = $@ || "Unknown error"; $self->_reset_address_to_unknown($address, $error) } 0 108 108
1005 108 ▶ my $error = $@ || "Unknown error";
Condition50%1/2
$@ || 'Unknown error'
l!l
1080
MC/DC0%0/1
$@ || 'Unknown error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
1006 # if connection failed, update topology with Unknown description
1007 108 $self->_reset_address_to_unknown( $address, $error );
1008 };
1009  
1010 108 ▶ return unless $link;
Branch: 1 branch
BranchTrueFalseTotal
unless $link 108 0 108
1011  
1012 # connection succeeded, so register link and get a server description
1013 0 $self->links->{$address} = $link;
1014 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 ▶ return unless my $server = $self->servers->{$address};
Branch: 1 branch
BranchTrueFalseTotal
unless my $server = $self->servers->{$address} 0 0 0
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 ▶ if ( $self->type eq 'Single' || first { $_ eq $server->type } qw/Standalone Mongos RSPrimary RSSecondary/ ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type eq "Single" or &first(sub { $_ eq $server->type; } , "Standalone", "Mongos", "RSPrimary", "RSSecondary")) 0 0 0
Condition0%0/3
$self->type eq "Single" or &first(sub { $_ eq $server->type; } , "Standalone", "Mongos", "RSPrimary", "RSSecondary")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->type eq "Single" or &first(sub { $_ eq $server->type; } , "Standalone", "Mongos", "RSPrimary", "RSSecondary")
$self->type eq "Single" &first(sub { $_ eq $server->type; } , "Standalone", "Mongos", "RSPrimary", "RSSecondary")
Truth table0%0/3
A or B
A= $self->type eq "Single"B= &first(sub { $_ eq $server->type; } , "Standalone", "Mongos", "RSPrimary", "RSSecondary")
ABresult
1X1
011
000
1024 eval {
1025 0 $self->credential->authenticate($server, $link, $self->bson_codec);
1026 0 1;
1027 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->credential->authenticate($server, $link, $self->bson_codec); 1 } } or do { my $err = $@; my $msg = $err->$MongoDB::_Topology::_isa("MongoDB::Error") ? $err->message : "$err"; $self->_reset_address_to_unknown($_->address, $err) foreach ($self->all_servers); "MongoDB::AuthError"->throw("Authentication to $address failed: $msg") } 0 0 0
1028 0 my $err = $@;
1029 0 ▶ my $msg = $err->$_isa("MongoDB::Error") ? $err->message : "$err";
Branch: 1 branch
BranchTrueFalseTotal
$err->$MongoDB::_Topology::_isa('MongoDB::Error') ? : 0 0 0
1030 0 $self->_reset_address_to_unknown( $_->address, $err ) for $self->all_servers;
1031 0 MongoDB::AuthError->throw("Authentication to $address failed: $msg");
1032 };
1033 }
1034  
1035 0 return $link;
1036 }
1037  
1038 203 ▶sub _primaries {
Subroutine _primaries: called CC=1 SCAR=0.0
1039 203 return grep { $_->type eq 'RSPrimary' } $_[0]->all_servers;
1040 }
1041  
1042 225 ▶sub _remove_address {
Subroutine _remove_address: called CC=5 SCAR=17.5
1043 225 my ( $self, $address ) = @_;
1044 225 ▶ if ( $self->current_primary && $self->current_primary->address eq $address ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->current_primary and $self->current_primary->address eq $address) 0 225 225
Condition33%1/3
$self->current_primary and $self->current_primary->address eq $address
!ll&&!rl&&r
22500
MC/DC0%0/2
$self->current_primary and $self->current_primary->address eq $address
$self->current_primary $self->current_primary->address eq $address
Truth table33%1/3
A and B
A= $self->current_primaryB= $self->current_primary->address eq $address
ABresult
0X0
100
111
1045 0 $self->_clear_current_primary;
1046 }
1047 225 delete $self->$_->{$address} for qw/servers links rtt_ewma_sec/;
1048 225 ▶ $self->publish_server_closing( $address )
Branch: 1 branch
BranchTrueFalseTotal
if $self->monitoring_callback 39 186 225
1049 if $self->monitoring_callback;
1050 225 return;
1051 }
1052  
1053 13 ▶sub _remove_server {
Subroutine _remove_server: called CC=1 SCAR=0.0
1054 13 my ( $self, $server ) = @_;
1055 13 $self->_remove_address( $server->address );
1056 13 return;
1057 }
1058  
1059 118 ▶sub _reset_address_to_unknown {
Subroutine _reset_address_to_unknown: called CC=2 SCAR=7.0
1060 118 my ( $self, $address, $error, $update_time ) = @_;
1061 118 ▶ $update_time //= time;
Condition66%2/3
$update_time //= &MongoDB::_Topology::time()
l!l&&r!l&&!r
101080
MC/DC0%0/2
$update_time //= &MongoDB::_Topology::time()
$update_time &MongoDB::_Topology::time()
Truth table66%2/3
A //= B
A= $update_timeB= &MongoDB::_Topology::time()
ABresult
1X1
011
000
1062  
1063 118 $self->_remove_address($address);
1064 118 my $desc = $self->_add_address_as_unknown( $address, $update_time, $error );
1065 118 $self->_update_topology_from_server_desc($address, $desc);
1066  
1067 118 return;
1068 }
1069  
1070 51 ▶sub _secondaries {
Subroutine _secondaries: called CC=1 SCAR=0.0
1071 51 return grep { $_->type eq 'RSSecondary' } $_[0]->all_servers;
1072 }
1073  
1074 54 ▶sub _status_string {
Subroutine _status_string: called CC=2 SCAR=7.1
1075 54 my ($self) = @_;
1076 54 my $status = '';
1077 54 ▶ if ( $self->type =~ /^Replica/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type =~ /^Replica/) { } 0 54 54
1078 0 $status .= sprintf( "Topology type: %s; Set name: %s, Member status:\n",
1079 $self->type, $self->replica_set_name );
1080 }
1081 else {
1082 54 $status .= sprintf( "Topology type: %s; Member status:\n", $self->type );
1083 }
1084  
1085 54 $status .= join( "\n", map { " $_" } map { $_->status_string } $self->all_servers ) . "\n";
1086 54 return $status;
1087 }
1088  
1089 # this implements the server selection timeout around whatever actual method
1090 # is used for returning a link
1091 54 ▶sub _selection_timeout {
Subroutine _selection_timeout: called CC=13 SCAR=33.6
1092 54 my ( $self, $method, $read_pref ) = @_;
1093  
1094 54 my $start_time = my $loop_end_time = time();
1095 54 my $max_time = $start_time + $self->server_selection_timeout_sec;
1096  
1097 54 ▶ if ( $self->next_scan_time < $start_time ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->next_scan_time < $start_time) 0 54 54
1098 0 $self->_set_stale(1);
1099 }
1100  
1101 54 while (1) {
1102 1134 ▶ if ( $self->stale ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->stale) 1080 54 1134
1103 1080 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 1080 ▶ return if !$self->server_selection_try_once && $scan_ready_time > $max_time;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->server_selection_try_once and $scan_ready_time > $max_time 54 1026 1080
Condition66%2/3
not $self->server_selection_try_once and $scan_ready_time > $max_time
!ll&&!rl&&r
0102654
MC/DC50%1/2
not $self->server_selection_try_once and $scan_ready_time > $max_time
not $self->server_selection_try_once $scan_ready_time > $max_time
Truth table66%2/3
not(A) and B
A= not $self->server_selection_try_onceB= $scan_ready_time > $max_time
ABresult
0X0
100
111
1107  
1108 # loop_end_time is a proxy for time() to avoid overhead
1109 1026 my $sleep_time = $scan_ready_time - $loop_end_time;
1110  
1111 1026 ▶ usleep( 1e6 * $sleep_time ) if $sleep_time > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $sleep_time > 0 1026 0 1026
1112  
1113 1026 $self->scan_all_servers;
1114 }
1115  
1116 1080 ▶ unless ( $self->is_compatible ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_compatible) 0 1080 1080
1117 0 $self->_set_stale(1);
1118 0 my $error_string = '';
1119 0 for my $server ( @{ $self->_incompatible_servers } ) {
1120 0 my $min_wire_ver = $server->is_master->{minWireVersion};
1121 0 my $max_wire_ver = $server->is_master->{maxWireVersion};
1122 0 my $host = $server->address;
1123 0 ▶ if ( $min_wire_ver > $self->max_wire_version ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min_wire_ver > $self->max_wire_version) { } 0 0 0
1124 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 $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 $self->_set_compatibility_error($error_string);
1143 0 MongoDB::ProtocolError->throw( $error_string );
1144 }
1145  
1146 1080 my $server = $self->$method($read_pref);
1147  
1148 1080 ▶ return $server if $server;
Branch: 1 branch
BranchTrueFalseTotal
if $server 0 1080 1080
1149  
1150 1080 $self->_set_stale(1);
1151 1080 $loop_end_time = time();
1152  
1153 1080 ▶ if ( $self->server_selection_try_once ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->server_selection_try_once) { } 0 1080 1080
1154 # if already tried once; then caller throws error
1155 0 ▶ return if $self->last_scan_time > $start_time;
Branch: 1 branch
BranchTrueFalseTotal
if $self->last_scan_time > $start_time 0 0 0
1156 }
1157 else {
1158 # if selection timed out; then caller throws error
1159 1080 ▶ return if $loop_end_time > $max_time;
Branch: 1 branch
BranchTrueFalseTotal
if $loop_end_time > $max_time 0 1080 1080
1160 }
1161 }
1162 }
1163  
1164 0 ▶sub _generate_ismaster_request {
Subroutine _generate_ismaster_request: not called CC=6 SCAR=37.4
1165 0 my ( $self, $link, $should_perform_handshake ) = @_;
1166 0 my @opts;
1167 0 ▶ if ($should_perform_handshake) {
Branch: 1 branch
BranchTrueFalseTotal
if ($should_perform_handshake) 0 0 0
1168 0 push @opts, client => $self->handshake_document;
1169 0 ▶ if ( $self->credential->mechanism eq 'DEFAULT' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->credential->mechanism eq "DEFAULT") 0 0 0
1170 0 my $db_user = join( ".", map { $self->credential->$_ } qw/source username/ );
1171 0 push @opts, saslSupportedMechs => $db_user;
1172 }
1173 0 ▶ if (@{ $self->compressors }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->compressors;}) 0 0 0
1174 0 push @opts, compression => $self->compressors;
1175 }
1176 }
1177 0 ▶ if ( $link->supports_clusterTime && defined $self->cluster_time ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link->supports_clusterTime and defined $self->cluster_time) 0 0 0
Condition0%0/3
$link->supports_clusterTime and defined $self->cluster_time
!ll&&!rl&&r
000
MC/DC0%0/2
$link->supports_clusterTime and defined $self->cluster_time
$link->supports_clusterTime defined $self->cluster_time
Truth table0%0/3
A and B
A= $link->supports_clusterTimeB= defined $self->cluster_time
ABresult
0X0
100
111
1178 0 push @opts, '$clusterTime' => $self->cluster_time;
1179 }
1180  
1181 0 return [ ismaster => 1, @opts ];
1182 }
1183  
1184 0 ▶sub _update_topology_from_link {
Subroutine _update_topology_from_link: not called CC=14 SCAR=53.5
1185 0 my ( $self, $link, %opts ) = @_;
1186  
1187 0 ▶ $self->publish_server_heartbeat_started( $link )
Branch: 1 branch
BranchTrueFalseTotal
if $self->monitoring_callback 0 0 0
1188 if $self->monitoring_callback;
1189  
1190 0 my $start_time = time;
1191 0 my $is_master = eval {
1192 my $op = MongoDB::Op::_Command->_new(
1193 db_name => 'admin',
1194 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 local $link->{socket_timeout} = $link->{connect_timeout};
1204 0 $op->execute( $link )->output;
1205 };
1206 0 ▶ if ( my $e = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 0 0
1207 0 my $end_time_fail = time;
1208 0 my $rtt_sec_fail = $end_time_fail - $start_time;
1209 0 ▶ $self->publish_server_heartbeat_failed( $link, $rtt_sec_fail, $e )
Branch: 1 branch
BranchTrueFalseTotal
if $self->monitoring_callback 0 0 0
1210 if $self->monitoring_callback;
1211 0 ▶ if ($e->$_isa("MongoDB::DatabaseError") && $e->code == USER_NOT_FOUND ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->$MongoDB::_Topology::_isa("MongoDB::DatabaseError") and $e->code == 11) 0 0 0
Condition0%0/3
$e->$MongoDB::_Topology::_isa("MongoDB::DatabaseError") and $e->code == 11
!ll&&!rl&&r
000
MC/DC0%0/2
$e->$MongoDB::_Topology::_isa("MongoDB::DatabaseError") and $e->code == 11
$e->$MongoDB::_Topology::_isa("MongoDB::DatabaseError") $e->code == 11
Truth table0%0/3
A and B
A= $e->$MongoDB::_Topology::_isa("MongoDB::DatabaseError")B= $e->code == 11
ABresult
0X0
100
111
1212 0 MongoDB::AuthError->throw("mechanism negotiation error: $e");
1213 }
1214 0 warn "During MongoDB topology update for @{[$link->address]}: $e"
1215 if WITH_ASSERTS;
1216 0 $self->_reset_address_to_unknown( $link->address, $e );
1217 # retry a network error if server was previously known to us
1218 0 ▶ if ( $e->$_isa("MongoDB::NetworkError")
Branch: 1 branch
BranchTrueFalseTotal
if ($e->$MongoDB::_Topology::_isa("MongoDB::NetworkError") and $link->server and $link->server->type ne "Unknown" and $link->server->type ne "PossiblePrimary") 0 0 0
Condition0%0/9
$e->$MongoDB::_Topology::_isa("MongoDB::NetworkError") and $link->server and $link->server->type ne "Unknown" and $link->server->type ne "PossiblePrimary"
!ll&&!rl&&r
000
$e->$MongoDB::_Topology::_isa("MongoDB::NetworkError") and $link->server and $link->server->type ne "Unknown"
!ll&&!rl&&r
000
$e->$MongoDB::_Topology::_isa("MongoDB::NetworkError") and $link->server
!ll&&!rl&&r
000
MC/DC0%0/4
$e->$MongoDB::_Topology::_isa("MongoDB::NetworkError") and $link->server and $link->server->type ne "Unknown" and $link->server->type ne "PossiblePrimary"
$e->$MongoDB::_Topology::_isa("MongoDB::NetworkError") $link->server $link->server->type ne "Unknown" $link->server->type ne "PossiblePrimary"
Truth table0%0/5
A and B and C and D
A= $e->$MongoDB::_Topology::_isa("MongoDB::NetworkError")B= $link->serverC= $link->server->type ne "Unknown"D= $link->server->type ne "PossiblePrimary"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
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 $self->check_address( $link->address );
1226 }
1227 0 return;
1228 };
1229  
1230 0 ▶ return unless $is_master;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_master 0 0 0
1231  
1232 0 ▶ if ( my $cluster_time = $is_master->{'$clusterTime'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cluster_time = $is_master->{"\$clusterTime"}) 0 0 0
1233 0 $self->update_cluster_time($cluster_time);
1234 }
1235  
1236 0 my $end_time = time;
1237 0 my $rtt_sec = $end_time - $start_time;
1238 # Protect against clock skew
1239 0 ▶ $rtt_sec = 0 if $rtt_sec < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $rtt_sec < 0 0 0 0
1240  
1241 0 ▶ $self->publish_server_heartbeat_succeeded( $link, $rtt_sec, $is_master )
Branch: 1 branch
BranchTrueFalseTotal
if $self->monitoring_callback 0 0 0
1242 if $self->monitoring_callback;
1243  
1244 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 $self->_update_topology_from_server_desc( $link->address, $new_server );
1253  
1254 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 0 ▶sub _construct_compressor {
Subroutine _construct_compressor: not called CC=6 SCAR=37.4
1263 0 my ($self, $is_master) = @_;
1264  
1265 0 ▶ my @supported = @{ ($is_master || {})->{compression} || [] }
Branch: 1 branch
BranchTrueFalseTotal
my(@supported) = @{+($is_master || {})->{"compression"} or [];} or return undef 0 0 0
Condition0%0/4
+($is_master || {})->{'compression'} || []
l!l
00
$is_master || {}
l!l
00
MC/DC0%0/1
+($is_master || {})->{'compression'} || []
$is_master
Truth table0%0/2
A
A= $is_master
Aresult
11
00
1266 or return undef; ## no critic
1267  
1268 0 for my $name (@{ $self->compressors }) {
1269 0 ▶ if (grep { $name eq $_ } @supported) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$name eq $_;} @supported) 0 0 0
1270 0 return MongoDB::_Protocol::get_compressor($name, {
1271 zlib_compression_level => $self->zlib_compression_level,
1272 });
1273 }
1274 }
1275  
1276 0 return undef; ## no critic
1277 }
1278  
1279 263 ▶sub _update_topology_from_server_desc {
Subroutine _update_topology_from_server_desc: called CC=4 SCAR=13.9
1280 263 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 263 ▶ return unless $self->servers->{$address};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->servers->{$address} 2 261 263
1287  
1288 261 ▶ $self->publish_old_topology_desc( $address, $new_server )
Branch: 1 branch
BranchTrueFalseTotal
if $self->monitoring_callback 146 115 261
1289 if $self->monitoring_callback;
1290  
1291 261 $self->_update_ewma( $address, $new_server );
1292  
1293 # must come after ewma update
1294 261 $self->servers->{$address} = $new_server;
1295  
1296 261 my $method = "_update_" . $self->type;
1297  
1298 261 $self->$method( $address, $new_server );
1299  
1300 # if link is still around, tag it with server specifics
1301 261 $self->_update_link_metadata( $address, $new_server );
1302  
1303 261 $self->_update_ls_timeout_minutes( $new_server );
1304  
1305 261 ▶ $self->publish_new_topology_desc if $self->monitoring_callback;
Branch: 1 branch
BranchTrueFalseTotal
if $self->monitoring_callback 146 115 261
1306  
1307 261 return $new_server;
1308 }
1309  
1310 483 ▶sub _update_ewma {
Subroutine _update_ewma: called CC=3 SCAR=11.0
1311 483 my ( $self, $address, $new_server ) = @_;
1312  
1313 483 ▶ if ( $new_server->type eq 'Unknown' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_server->type eq 'Unknown') { } 128 355 483
1314 128 delete $self->rtt_ewma_sec->{$address};
1315 }
1316 else {
1317 355 my $old_avg = $self->rtt_ewma_sec->{$address};
1318 355 my $alpha = $self->ewma_alpha;
1319 355 my $rtt_sec = $new_server->rtt_sec;
1320 355 ▶ $self->rtt_ewma_sec->{$address} =
Branch: 1 branch
BranchTrueFalseTotal
defined $old_avg ? : 16 339 355
1321 defined($old_avg) ? ( $alpha * $rtt_sec + ( 1 - $alpha ) * $old_avg ) : $rtt_sec;
1322 }
1323  
1324 483 return;
1325 }
1326  
1327 261 ▶sub _update_link_metadata {
Subroutine _update_link_metadata: called CC=2 SCAR=7.6
1328 261 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 261 ▶ if ( $self->links->{$address} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->links->{$address}) 0 261 261
1333 0 $self->links->{$address}->set_metadata($server);
1334 }
1335  
1336 261 return;
1337 }
1338  
1339 17 ▶sub _update_rs_with_primary_from_member {
Subroutine _update_rs_with_primary_from_member: called CC=9 SCAR=25.9
1340 17 my ( $self, $new_server ) = @_;
1341  
1342 17 ▶ if ( !$self->servers->{ $new_server->address }
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->servers->{$new_server->address} or $self->replica_set_name ne $new_server->set_name) 2 15 17
Condition66%2/3
not $self->servers->{$new_server->address} or $self->replica_set_name ne $new_server->set_name
l!l&&r!l&&!r
0215
MC/DC50%1/2
not $self->servers->{$new_server->address} or $self->replica_set_name ne $new_server->set_name
not $self->servers->{$new_server->address} $self->replica_set_name ne $new_server->set_name
Truth table66%2/3
not(A) or B
A= not $self->servers->{$new_server->address}B= $self->replica_set_name ne $new_server->set_name
ABresult
1X1
011
000
1343 || $self->replica_set_name ne $new_server->set_name )
1344 {
1345 2 $self->_remove_server($new_server);
1346 }
1347  
1348 # require 'me' that matches expected address.
1349 # check is case insensitive
1350 17 ▶ if ( $new_server->me && lc $new_server->me ne $new_server->address ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_server->me and lc $new_server->me ne $new_server->address) 0 17 17
Condition66%2/3
$new_server->me and lc $new_server->me ne $new_server->address
!ll&&!rl&&r
1610
MC/DC0%0/2
$new_server->me and lc $new_server->me ne $new_server->address
$new_server->me lc $new_server->me ne $new_server->address
Truth table66%2/3
A and B
A= $new_server->meB= lc $new_server->me ne $new_server->address
ABresult
0X0
100
111
1351 0 $self->_remove_server($new_server);
1352 0 $self->_check_for_primary;
1353 0 return;
1354 }
1355  
1356 17 ▶ if ( ! $self->_check_for_primary ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_check_for_primary) 1 16 17
1357  
1358 # flag possible primary to amend scanning order
1359 1 my $primary = $new_server->primary;
1360 1 ▶ if ( length($primary)
Branch: 1 branch
BranchTrueFalseTotal
if (length $primary and $self->servers->{$primary} and $self->servers->{$primary}->type eq "Unknown") 0 1 1
Condition33%2/6
length $primary and $self->servers->{$primary} and $self->servers->{$primary}->type eq "Unknown"
!ll&&!rl&&r
100
length $primary and $self->servers->{$primary}
!ll&&!rl&&r
100
MC/DC0%0/3
length $primary and $self->servers->{$primary} and $self->servers->{$primary}->type eq "Unknown"
length $primary $self->servers->{$primary} $self->servers->{$primary}->type eq "Unknown"
Truth table25%1/4
A and B and C
A= length $primaryB= $self->servers->{$primary}C= $self->servers->{$primary}->type eq "Unknown"
ABCresult
0XX0
10X0
1100
1111
1361 && $self->servers->{$primary}
1362 && $self->servers->{$primary}->type eq 'Unknown' )
1363 {
1364 0 $self->servers->{$primary}->_set_type('PossiblePrimary');
1365 }
1366 }
1367  
1368 17 return;
1369 }
1370  
1371 63 ▶sub _update_rs_with_primary_from_primary {
Subroutine _update_rs_with_primary_from_primary: called CC=17 SCAR=28.3
1372 63 my ( $self, $new_server ) = @_;
1373  
1374 63 ▶ if ( !length $self->replica_set_name ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not length $self->replica_set_name) { } 3 60 63
elsif ($self->replica_set_name ne $new_server->set_name) { } 3 57 60
1375 3 $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 $self->_remove_server($new_server);
1381 3 return;
1382 }
1383  
1384 60 my $election_id = $new_server->is_master->{electionId};
1385 60 my $set_version = $new_server->is_master->{setVersion};
1386 60 my $max_election_id = $self->max_election_id;
1387 60 my $max_set_version = $self->max_set_version;
1388  
1389 60 ▶ if ( defined $set_version && defined $election_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $set_version and defined $election_id) 20 40 60
Condition100%3/3
defined $set_version and defined $election_id
!ll&&!rl&&r
32820
MC/DC100%2/2
defined $set_version and defined $election_id
defined $set_version defined $election_id
Truth table100%3/3
A and B
A= defined $set_versionB= defined $election_id
ABresult
0X0
100
111
1390 20 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max_election_id and defined $max_set_version and $max_set_version > $set_version || $max_set_version == $set_version && "$max_election_id" gt "$election_id") 6 14 20
Condition91%11/12
defined $max_election_id and defined $max_set_version and $max_set_version > $set_version || $max_set_version == $set_version && "$max_election_id" gt "$election_id"
!ll&&!rl&&r
776
defined $max_election_id and defined $max_set_version
!ll&&!rl&&r
7013
$max_set_version > $set_version or $max_set_version == $set_version and "$max_election_id" gt "$election_id"
l!l&&r!l&&!r
337
$max_set_version == $set_version and "$max_election_id" gt "$election_id"
!ll&&!rl&&r
253
MC/DC80%4/5
defined $max_election_id and defined $max_set_version and $max_set_version > $set_version || $max_set_version == $set_version && "$max_election_id" gt "$election_id"
defined $max_election_id defined $max_set_version $max_set_version > $set_version $max_set_version == $set_version "$max_election_id" gt "$election_id"
Truth table83%5/6
A and B and C or D and E
A= defined $max_election_idB= defined $max_set_versionC= $max_set_version > $set_versionD= $max_set_version == $set_versionE= "$max_election_id" gt "$election_id"
ABCDEresult
0XXXX0
10XXX0
1100X0
110100
111XX1
110111
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 $self->_remove_address( $new_server->address );
1403 6 $self->_add_address_as_unknown( $new_server->address );
1404 6 $self->_check_for_primary;
1405 6 return;
1406 }
1407 14 $self->_set_max_election_id( $election_id );
1408 }
1409  
1410 54 ▶ if ( defined $set_version
Branch: 1 branch
BranchTrueFalseTotal
if (defined $set_version and !defined($max_set_version) || $set_version > $max_set_version) 14 40 54
Condition100%6/6
defined $set_version and !defined($max_set_version) || $set_version > $max_set_version
!ll&&!rl&&r
32814
not defined $max_set_version or $set_version > $max_set_version
l!l&&r!l&&!r
1138
MC/DC100%3/3
defined $set_version and !defined($max_set_version) || $set_version > $max_set_version
defined $set_version not defined $max_set_version $set_version > $max_set_version
Truth table100%4/4
A and not(B) or C
A= defined $set_versionB= not defined $max_set_versionC= $set_version > $max_set_version
ABCresult
0XX0
1000
11X1
1011
1411 && ( !defined $max_set_version || $set_version > $max_set_version ) )
1412 {
1413 14 $self->_set_max_set_version($set_version);
1414 }
1415  
1416 # possibly invalidate an old primary (even if more than one!)
1417 54 for my $old_primary ( $self->_primaries ) {
1418 64 ▶ if ( $old_primary->address ne $new_server->address ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_primary->address ne $new_server->address) 10 54 64
1419 10 $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 map { $_ => undef } map { @{ $new_server->$_ } } qw/hosts passives arbiters/;
1430  
1431 $self->_add_address_as_unknown($_)
1432 54 for grep { !exists $self->servers->{$_} } keys %set_members;
1433  
1434 # topology servers no longer in the set need to be removed
1435 $self->_remove_address($_)
1436 54 for grep { !exists $set_members{$_} } keys %{ $self->servers };
1437  
1438 54 return;
1439 }
1440  
1441 10 ▶sub _update_rs_without_primary {
Subroutine _update_rs_without_primary: called CC=9 SCAR=22.0
1442 10 my ( $self, $new_server ) = @_;
1443  
1444 10 ▶ if ( !length $self->replica_set_name ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not length $self->replica_set_name) { } 1 9 10
elsif ($self->replica_set_name ne $new_server->set_name) { } 2 7 9
1445 1 $self->_set_replica_set_name( $new_server->set_name );
1446 }
1447 elsif ( $self->replica_set_name ne $new_server->set_name ) {
1448 2 $self->_remove_server($new_server);
1449 2 return;
1450 }
1451  
1452 # unknown set members need to be added to the topology
1453 my %set_members =
1454 8 map { $_ => undef } map { @{ $new_server->$_ } } qw/hosts passives arbiters/;
1455  
1456 $self->_add_address_as_unknown($_)
1457 8 for grep { !exists $self->servers->{$_} } keys %set_members;
1458  
1459 # require 'me' that matches expected address
1460 8 ▶ if ( $new_server->me && $new_server->me ne $new_server->address ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_server->me and $new_server->me ne $new_server->address) 1 7 8
Condition66%2/3
$new_server->me and $new_server->me ne $new_server->address
!ll&&!rl&&r
701
MC/DC50%1/2
$new_server->me and $new_server->me ne $new_server->address
$new_server->me $new_server->me ne $new_server->address
Truth table66%2/3
A and B
A= $new_server->meB= $new_server->me ne $new_server->address
ABresult
0X0
100
111
1461 1 $self->_remove_server($new_server);
1462 1 return;
1463 }
1464  
1465 # flag possible primary to amend scanning order
1466 7 my $primary = $new_server->primary;
1467 7 ▶ if ( length($primary)
Branch: 1 branch
BranchTrueFalseTotal
if (length $primary and $self->servers->{$primary} and $self->servers->{$primary}->type eq "Unknown") 2 5 7
Condition66%4/6
length $primary and $self->servers->{$primary} and $self->servers->{$primary}->type eq "Unknown"
!ll&&!rl&&r
502
length $primary and $self->servers->{$primary}
!ll&&!rl&&r
502
MC/DC33%1/3
length $primary and $self->servers->{$primary} and $self->servers->{$primary}->type eq "Unknown"
length $primary $self->servers->{$primary} $self->servers->{$primary}->type eq "Unknown"
Truth table50%2/4
A and B and C
A= length $primaryB= $self->servers->{$primary}C= $self->servers->{$primary}->type eq "Unknown"
ABCresult
0XX0
10X0
1100
1111
1468 && $self->servers->{$primary}
1469 && $self->servers->{$primary}->type eq 'Unknown' )
1470 {
1471 2 $self->servers->{$primary}->_set_type('PossiblePrimary');
1472 }
1473  
1474 7 return;
1475 }
1476  
1477 #--------------------------------------------------------------------------#
1478 # update methods by topology types: behavior in each depends on new server
1479 # type received
1480 #--------------------------------------------------------------------------#
1481  
1482 53 ▶sub _update_ReplicaSetNoPrimary {
Subroutine _update_ReplicaSetNoPrimary: called CC=4 SCAR=13.9
1483 53 my ( $self, $address, $new_server ) = @_;
1484  
1485 53 my $server_type = $new_server->type;
1486  
1487 53 ▶ if ( $server_type eq 'RSPrimary' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($server_type eq 'RSPrimary') { } 41 12 53
elsif (grep {$server_type eq $_;} 'RSSecondary', 'RSArbiter', 'RSOther') { } 9 3 12
elsif (grep {$server_type eq $_;} 'Standalone', 'Mongos') { } 2 1 3
1488 41 $self->_set_type('ReplicaSetWithPrimary');
1489 41 $self->_update_rs_with_primary_from_primary($new_server);
1490 # topology changes might have removed all primaries
1491 41 $self->_check_for_primary;
1492 }
1493 36 elsif ( grep { $server_type eq $_ } qw/RSSecondary RSArbiter RSOther/ ) {
1494 9 $self->_update_rs_without_primary($new_server);
1495 }
1496 6 elsif ( grep { $server_type eq $_ } qw/Standalone Mongos/ ) {
1497 2 $self->_remove_server($new_server);
1498 }
1499 else {
1500 # Unknown or RSGhost are no-ops
1501 }
1502  
1503 53 return;
1504 }
1505  
1506 54 ▶sub _update_ReplicaSetWithPrimary {
Subroutine _update_ReplicaSetWithPrimary: called CC=5 SCAR=16.1
1507 54 my ( $self, $address, $new_server ) = @_;
1508  
1509 54 my $server_type = $new_server->type;
1510  
1511 54 ▶ if ( $server_type eq 'RSPrimary' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($server_type eq 'RSPrimary') { } 19 35 54
elsif (grep {$server_type eq $_;} 'RSSecondary', 'RSArbiter', 'RSOther') { } 17 18 35
elsif (grep {$server_type eq $_;} 'Unknown', 'Standalone', 'Mongos') { } 16 2 18
1512 19 $self->_update_rs_with_primary_from_primary($new_server);
1513 }
1514 105 elsif ( grep { $server_type eq $_ } qw/RSSecondary RSArbiter RSOther/ ) {
1515 17 $self->_update_rs_with_primary_from_member($new_server);
1516 }
1517 54 elsif ( grep { $server_type eq $_ } qw/Unknown Standalone Mongos/ ) {
1518 16 ▶ $self->_remove_server($new_server)
Branch: 1 branch
BranchTrueFalseTotal
unless $server_type eq "Unknown" 2 14 16
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 $self->_check_for_primary;
1527  
1528 54 return;
1529 }
1530  
1531 11 ▶sub _update_Sharded {
Subroutine _update_Sharded: called CC=2 SCAR=6.9
1532 11 my ( $self, $address, $new_server ) = @_;
1533  
1534 11 my $server_type = $new_server->type;
1535  
1536 11 ▶ if ( grep { $server_type eq $_ } qw/Unknown Mongos/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$server_type eq $_;} 'Unknown', 'Mongos') { } 10 1 11
1537 # no-op
1538 }
1539 else {
1540 1 $self->_remove_server($new_server);
1541 }
1542  
1543 11 return;
1544 }
1545  
1546 76 ▶sub _update_Single {
Subroutine _update_Single: called CC=1 SCAR=0.0
1547 76 my ( $self, $address, $new_server ) = @_;
1548 # Per the spec, TopologyType Single never changes type or membership
1549 76 return;
1550 }
1551  
1552 # Direct mode is like Unknown, except that it switches only between Sharded
1553 # or Single based on the response.
1554 54 ▶sub _update_Direct {
Subroutine _update_Direct: called CC=2 SCAR=7.6
1555 54 my ( $self, $address, $new_server ) = @_;
1556  
1557 54 my $server_type = $new_server->type;
1558  
1559 54 ▶ if ( $server_type eq 'Mongos' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($server_type eq "Mongos") 0 54 54
1560 0 $self->_set_type('Sharded');
1561 0 return;
1562 }
1563  
1564 54 $self->_set_type('Single');
1565 54 return;
1566 }
1567  
1568 13 ▶sub _update_Unknown {
Subroutine _update_Unknown: called CC=6 SCAR=18.0
1569 13 my ( $self, $address, $new_server ) = @_;
1570  
1571 13 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 ▶ if ( $server_type eq 'Standalone' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($server_type eq "Standalone") 2 11 13
1577 2 ▶ if ( $self->number_of_seeds > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->number_of_seeds > 1) { } 2 0 2
1578 2 $self->_remove_address($address);
1579 }
1580 else {
1581 0 $self->_set_type('Single');
1582 }
1583 2 return;
1584 }
1585  
1586 11 ▶ if ( $server_type eq 'Mongos' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($server_type eq "Mongos") 7 4 11
1587 7 $self->_set_type('Sharded');
1588 7 return;
1589 }
1590  
1591 4 ▶ if ( $server_type eq 'RSPrimary' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($server_type eq 'RSPrimary') { } 3 1 4
elsif (grep {$server_type eq $_;} 'RSSecondary', 'RSArbiter', 'RSOther') { } 1 0 1
1592 3 $self->_set_type('ReplicaSetWithPrimary');
1593 3 $self->_update_rs_with_primary_from_primary($new_server);
1594 # topology changes might have removed all primaries
1595 3 $self->_check_for_primary;
1596 }
1597 3 elsif ( grep { $server_type eq $_ } qw/RSSecondary RSArbiter RSOther/ ) {
1598 1 $self->_set_type('ReplicaSetNoPrimary');
1599 1 $self->_update_rs_without_primary($new_server);
1600 }
1601 else {
1602 # Unknown or RSGhost are no-ops
1603 }
1604  
1605 4 return;
1606 }
1607  
1608 60 1;
1609  
1610 # vim: ts=4 sts=4 sw=4 et:
« MongoDB/_Protocol.pm ↑ Summary MongoDB/Role/_UpdatePreEncoder.pm »