| 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 | 61 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 16 | 61 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 17 | package MongoDB::ReadPreference; | ||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||
| 19 | # ABSTRACT: Encapsulate and validate read preferences | ||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||
| 21 | 61 | ▶ | use version; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 22 | 61 | our $VERSION = 'v2.2.2'; | |||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | 61 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 25 | 61 | ▶ | use MongoDB::Error; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 26 | 61 | ▶ | use MongoDB::_Types qw( | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 27 | ArrayOfHashRef | ||||||||||||||||||||||||||||||||||
| 28 | MaxStalenessNum | ||||||||||||||||||||||||||||||||||
| 29 | NonNegNum | ||||||||||||||||||||||||||||||||||
| 30 | ReadPrefMode | ||||||||||||||||||||||||||||||||||
| 31 | 61 | ); | |||||||||||||||||||||||||||||||||
| 32 | 61 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||
| 34 | 61 | ▶ | use overload ( | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 35 | 143 | ▶ | q[""] => sub { $_[0]->mode }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 36 | 61 | fallback => 1, | |||||||||||||||||||||||||||||||||
| 37 | 61 | ); | |||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||
| 39 | #pod =attr mode | ||||||||||||||||||||||||||||||||||
| 40 | #pod | ||||||||||||||||||||||||||||||||||
| 41 | #pod The read preference mode determines which server types are candidates | ||||||||||||||||||||||||||||||||||
| 42 | #pod for a read operation. Valid values are: | ||||||||||||||||||||||||||||||||||
| 43 | #pod | ||||||||||||||||||||||||||||||||||
| 44 | #pod =for :list | ||||||||||||||||||||||||||||||||||
| 45 | #pod * primary | ||||||||||||||||||||||||||||||||||
| 46 | #pod * primaryPreferred | ||||||||||||||||||||||||||||||||||
| 47 | #pod * secondary | ||||||||||||||||||||||||||||||||||
| 48 | #pod * secondaryPreferred | ||||||||||||||||||||||||||||||||||
| 49 | #pod * nearest | ||||||||||||||||||||||||||||||||||
| 50 | #pod | ||||||||||||||||||||||||||||||||||
| 51 | #pod =cut | ||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||
| 53 | 61 | has mode => ( | |||||||||||||||||||||||||||||||||
| 54 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 55 | isa => ReadPrefMode, | ||||||||||||||||||||||||||||||||||
| 56 | default => 'primary', | ||||||||||||||||||||||||||||||||||
| 57 | coerce => ReadPrefMode->coercion, | ||||||||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||
| 60 | #pod =attr tag_sets | ||||||||||||||||||||||||||||||||||
| 61 | #pod | ||||||||||||||||||||||||||||||||||
| 62 | #pod The C<tag_sets> parameter is an ordered list of tag sets used to restrict the | ||||||||||||||||||||||||||||||||||
| 63 | #pod eligibility of servers, such as for data center awareness. | ||||||||||||||||||||||||||||||||||
| 64 | #pod | ||||||||||||||||||||||||||||||||||
| 65 | #pod The application of C<tag_sets> varies depending on the C<mode> parameter. If | ||||||||||||||||||||||||||||||||||
| 66 | #pod the C<mode> is 'primary', then C<tag_sets> must not be supplied. | ||||||||||||||||||||||||||||||||||
| 67 | #pod | ||||||||||||||||||||||||||||||||||
| 68 | #pod =cut | ||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||
| 70 | has tag_sets => ( | ||||||||||||||||||||||||||||||||||
| 71 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 72 | isa => ArrayOfHashRef, | ||||||||||||||||||||||||||||||||||
| 73 | 281 | ▶ | default => sub { [ {} ] }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 74 | 61 | coerce => ArrayOfHashRef->coercion, | |||||||||||||||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||
| 77 | #pod =attr max_staleness_seconds | ||||||||||||||||||||||||||||||||||
| 78 | #pod | ||||||||||||||||||||||||||||||||||
| 79 | #pod The C<max_staleness_seconds> parameter represents the maximum replication lag in | ||||||||||||||||||||||||||||||||||
| 80 | #pod seconds (wall clock time) that a secondary can suffer and still be | ||||||||||||||||||||||||||||||||||
| 81 | #pod eligible for reads. The default is -1, which disables staleness checks. | ||||||||||||||||||||||||||||||||||
| 82 | #pod | ||||||||||||||||||||||||||||||||||
| 83 | #pod If the C<mode> is 'primary', then C<max_staleness_seconds> must not be supplied. | ||||||||||||||||||||||||||||||||||
| 84 | #pod | ||||||||||||||||||||||||||||||||||
| 85 | #pod =cut | ||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||
| 87 | 61 | has max_staleness_seconds => ( | |||||||||||||||||||||||||||||||||
| 88 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 89 | isa => MaxStalenessNum, | ||||||||||||||||||||||||||||||||||
| 90 | default => -1, | ||||||||||||||||||||||||||||||||||
| 91 | ); | ||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||
| 93 | 509 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 94 | 509 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||
| 96 | 509 | ▶ | if ( $self->mode eq 'primary' && !$self->has_empty_tag_sets ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->mode eq "primary" and not $self->has_empty_tag_sets
MC/DC100%2/2
$self->mode eq "primary" and not $self->has_empty_tag_sets
$self->mode eq "primary"
not $self->has_empty_tag_sets
Truth table100%3/3
A and not(B)
A= $self->mode eq "primary"B= not $self->has_empty_tag_sets
| |||||||||||||||||||||||||||||||||||
| 97 | 1 | MongoDB::UsageError->throw("A tag set list is not allowed with read preference mode 'primary'"); | |||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||
| 100 | 508 | ▶ | if ( $self->mode eq 'primary' && $self->max_staleness_seconds > 0 ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->mode eq "primary" and $self->max_staleness_seconds > 0
MC/DC100%2/2
$self->mode eq "primary" and $self->max_staleness_seconds > 0
$self->mode eq "primary"
$self->max_staleness_seconds > 0
Truth table100%3/3
A and B
A= $self->mode eq "primary"B= $self->max_staleness_seconds > 0
| |||||||||||||||||||||||||||||||||||
| 101 | 4 | MongoDB::UsageError->throw("A positive max_staleness_seconds is not allowed with read preference mode 'primary'"); | |||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||
| 104 | 504 | return; | |||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||
| 107 | # Returns true if the C<tag_sets> array is empty or if it consists only of a | ||||||||||||||||||||||||||||||||||
| 108 | # single, empty hash reference. | ||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||
| 110 | 459 | ▶ | sub has_empty_tag_sets { | ||||||||||||||||||||||||||||||||
|
Subroutine
has_empty_tag_sets: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 111 | 459 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 112 | 459 | my $tag_sets = $self->tag_sets; | |||||||||||||||||||||||||||||||||
| 113 | 459 | ▶ | return @$tag_sets == 0 || ( @$tag_sets == 1 && !keys %{ $tag_sets->[0] } ); | ||||||||||||||||||||||||||||||||
|
Condition83%5/6
@$tag_sets == 0 || @$tag_sets == 1 && !keys(%{$$tag_sets[0];})
@$tag_sets == 1 and not keys %{$$tag_sets[0];}
MC/DC66%2/3
@$tag_sets == 0 || @$tag_sets == 1 && !keys(%{$$tag_sets[0];})
@$tag_sets == 0
@$tag_sets == 1
not keys %{$$tag_sets[0];}
Truth table75%3/4
A || B and not(C)
A= @$tag_sets == 0B= @$tag_sets == 1C= not keys %{$$tag_sets[0];}
| |||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||
| 116 | # Reformat to the document needed by mongos in $readPreference | ||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||
| 118 | 120 | ▶ | sub _as_hashref { | ||||||||||||||||||||||||||||||||
|
Subroutine
_as_hashref: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||||||||
| 119 | 120 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 120 | return { | ||||||||||||||||||||||||||||||||||
| 121 | 120 | ▶ | mode => $self->mode, | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 122 | ( $self->has_empty_tag_sets ? () : ( tags => $self->tag_sets ) ), | ||||||||||||||||||||||||||||||||||
| 123 | ( $self->max_staleness_seconds > 0 ? ( maxStalenessSeconds => int($self->max_staleness_seconds )) : () ), | ||||||||||||||||||||||||||||||||||
| 124 | }; | ||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||
| 127 | # Format as a string for error messages | ||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||
| 129 | 3 | ▶ | sub as_string { | ||||||||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 130 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 131 | 3 | my $string = $self->mode; | |||||||||||||||||||||||||||||||||
| 132 | 3 | ▶ | unless ( $self->has_empty_tag_sets ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 133 | 1 | my @ts; | |||||||||||||||||||||||||||||||||
| 134 | 1 | for my $set ( @{ $self->tag_sets } ) { | |||||||||||||||||||||||||||||||||
| 135 | 3 | ▶ | push @ts, keys(%$set) ? join( ",", map { "$_\:$set->{$_}" } sort keys %$set ) : ""; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||
| 137 | 1 | $string .= " (" . join( ",", map { "{$_}" } @ts ) . ")"; | |||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||
| 139 | 3 | ▶ | if ( $self->max_staleness_seconds > 0) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 140 | 0 | $string .= " ( maxStalenessSeconds: " . $self->max_staleness_seconds . " )"; | |||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||
| 142 | 3 | return $string; | |||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||
| 146 | 61 | 1; | |||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||
| 148 | __END__ | ||||||||||||||||||||||||||||||||||