| 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 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | package MongoDB::Role::_ReadPrefModifier; | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | # MongoDB role to modify OP_QUERY query document or flags to account | ||||||||||||||||||||||||||||||||||||||||||
| 21 | # for topology-specific treatment of a read-preference (if any) | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 60 | ▶ | use version; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 60 | our $VERSION = 'v2.2.2'; | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 60 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 60 | ▶ | use MongoDB::Error; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 60 | ▶ | use MongoDB::ReadPreference; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 60 | ▶ | use MongoDB::_Types -types, 'to_IxHash'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 60 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 60 | requires qw/read_preference/; | |||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 60 | my $PRIMARY = MongoDB::ReadPreference->new()->_as_hashref; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 60 | my $PRIMARYPREFERRED = | |||||||||||||||||||||||||||||||||||||||||
| 38 | MongoDB::ReadPreference->new( mode => 'primaryPreferred' )->_as_hashref; | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | sub _apply_op_msg_read_prefs { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_apply_op_msg_read_prefs: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | my ( $self, $link, $topology_type, $query_flags, $query_ref ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | $topology_type ||= "<undef>"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$topology_type ||= "<undef>"
MC/DC0%0/1
$topology_type ||= "<undef>"
$topology_type
Truth table0%0/2
A
A= $topology_type
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | my $read_pref = $self->read_preference; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | my $read_pref_doc = $read_pref ? $read_pref->_as_hashref : $PRIMARY; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | if ( $topology_type eq 'Single' && ! ($link->server && $link->server->type eq 'Mongos') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$topology_type eq "Single" and not $link->server && $link->server->type eq "Mongos"
$link->server && $link->server->type eq 'Mongos'
MC/DC0%0/3
$topology_type eq "Single" and not $link->server && $link->server->type eq "Mongos"
$topology_type eq "Single"
$link->server
$link->server->type eq 'Mongos'
Truth table0%0/4
A and not(B && C)
A= $topology_type eq "Single"B= $link->serverC= $link->server->type eq 'Mongos'
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | # For direct connection to a non-mongos single server, allow any server | ||||||||||||||||||||||||||||||||||||||||||
| 49 | # type, overriding the provided read preference | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | $read_pref_doc = $PRIMARYPREFERRED; | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | $$query_ref = to_IxHash($$query_ref); | |||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ($$query_ref)->Push( '$readPreference' => $read_pref_doc ); | |||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | sub _apply_op_query_read_prefs { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_apply_op_query_read_prefs: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | my ( $self, $link, $topology_type, $query_flags, $query_ref ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | $topology_type ||= "<undef>"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$topology_type ||= "<undef>"
MC/DC0%0/1
$topology_type ||= "<undef>"
$topology_type
Truth table0%0/2
A
A= $topology_type
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | my $read_pref = $self->read_preference; | |||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if ( $topology_type eq 'Single' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | if ( $link->server && $link->server->type eq 'Mongos' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$link->server and $link->server->type eq "Mongos"
MC/DC0%0/2
$link->server and $link->server->type eq "Mongos"
$link->server
$link->server->type eq "Mongos"
Truth table0%0/3
A and B
A= $link->serverB= $link->server->type eq "Mongos"
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | $self->_apply_mongos_read_prefs( $read_pref, $query_flags, $query_ref ); | |||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | $query_flags->{slave_ok} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | elsif ( grep { $topology_type eq $_ } qw/ReplicaSetNoPrimary ReplicaSetWithPrimary/ ) | |||||||||||||||||||||||||||||||||||||||||
| 74 | { | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | if ( !$read_pref || $read_pref->mode eq 'primary' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $read_pref or $read_pref->mode eq "primary"
MC/DC0%0/2
not $read_pref or $read_pref->mode eq "primary"
not $read_pref
$read_pref->mode eq "primary"
Truth table0%0/3
not(A) or B
A= not $read_prefB= $read_pref->mode eq "primary"
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | $query_flags->{slave_ok} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | $query_flags->{slave_ok} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | elsif ( $topology_type eq 'Sharded' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | $self->_apply_mongos_read_prefs( $read_pref, $query_flags, $query_ref ); | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | MongoDB::InternalError->throw("can't query topology type '$topology_type'"); | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | sub _apply_mongos_read_prefs { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_apply_mongos_read_prefs: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | my ( $self, $read_pref, $query_flags, $query_ref ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | my $mode = $read_pref ? $read_pref->mode : 'primary'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | my $need_read_pref; | |||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | if ( $mode eq 'primary' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | $query_flags->{slave_ok} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | elsif ( grep { $mode eq $_ } qw/secondary primaryPreferred nearest/ ) { | |||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | $query_flags->{slave_ok} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | $need_read_pref = 1; | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | elsif ( $mode eq 'secondaryPreferred' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | $query_flags->{slave_ok} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | $need_read_pref = 1 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$read_pref->has_empty_tag_sets and $read_pref->max_staleness_seconds == -1
MC/DC0%0/2
$read_pref->has_empty_tag_sets and $read_pref->max_staleness_seconds == -1
$read_pref->has_empty_tag_sets
$read_pref->max_staleness_seconds == -1
Truth table0%0/3
A and B
A= $read_pref->has_empty_tag_setsB= $read_pref->max_staleness_seconds == -1
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | unless $read_pref->has_empty_tag_sets && $read_pref->max_staleness_seconds == -1; | ||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | MongoDB::InternalError->throw("invalid read preference mode '$mode'"); | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | if ($need_read_pref) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | $$query_ref = to_IxHash($$query_ref); | |||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | if ( !($$query_ref)->FETCH('$query') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | $$query_ref = Tie::IxHash->new( '$query' => $$query_ref ); | |||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ($$query_ref)->Push( '$readPreference' => $read_pref->_as_hashref ); | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 60 | 1; | |||||||||||||||||||||||||||||||||||||||||