MongoDB/_Server.pm

filter: Statementstmt 88.7% 103 / 116 Branchbran 75.0% 21 / 28 Conditioncond 88.8% 16 / 18 MC/DCmcdc 80.0% 8 / 10 Subroutinesub 93.1% 27 / 29 Podpod 0.0% 0 / 4 totaltotal 85.3% 175 / 205 CC 23 SCAR 13.5
CC
23
Cov
86%
status_struct
12.0
_build_type
10.0
status_string
2.2
CRAP 3.9
« MongoDB/Op/_InsertOne.pm ↑ Summary MongoDB/Role/_PrivateConstructor.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 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::_Server;
18  
19 61 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 61 our $VERSION = 'v2.2.2';
21  
22 61 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 61 ▶use MongoDB::_Types qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
24 Boolish
25 NonNegNum
26 HostAddress
27 ServerType
28 HostAddressList
29 61 );
30 61 ▶use Types::Standard qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
31 InstanceOf
32 HashRef
33 Str
34 Num
35 Maybe
36 61 );
37 61 ▶use List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 61 ▶use Time::HiRes qw/time/;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 61 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 # address: the hostname or IP, and the port number, that the client connects
42 # to. Note that this is not the server's ismaster.me field, in the case that
43 # the server reports an address different from the address the client uses.
44  
45 61 has address => (
46 is => 'ro',
47 isa => HostAddress,
48 required => 1,
49 );
50  
51 # lastUpdateTime: when this server was last checked. Default "infinity ago".
52  
53 61 has last_update_time => (
54 is => 'ro',
55 isa => Num, # floating point time
56 required => 1,
57 );
58  
59 # error: information about the last error related to this server. Default null.
60  
61 61 has error => (
62 is => 'ro',
63 isa => Str,
64 default => '',
65 );
66  
67 # roundTripTime: the duration of the ismaster call. Default null.
68  
69 61 has rtt_sec => (
70 is => 'ro',
71 isa => NonNegNum,
72 default => 0,
73 );
74  
75 # is_master: hashref returned from an is_master command
76  
77 has is_master => (
78 is => 'ro',
79 isa => HashRef,
80 521 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
81 61 );
82  
83 # compressor: hashref with id/callback values for used compression
84  
85 61 has compressor => (
86 is => 'ro',
87 isa => Maybe[HashRef],
88 );
89  
90 # type: a ServerType enum value. Default Unknown. Definitions from the Server
91 # Discovery and Monitoring Spec:
92 # - Unknown Initial, or after a network error or failed ismaster call, or "ok: 1"
93 # not in ismaster response.
94 # - Standalone No "msg: isdbgrid", no setName, and no "isreplicaset: true".
95 # - Mongos "msg: isdbgrid".
96 # - RSPrimary "ismaster: true", "setName" in response.
97 # - RSSecondary "secondary: true", "setName" in response.
98 # - RSArbiter "arbiterOnly: true", "setName" in response.
99 # - RSOther "setName" in response, "hidden: true" or not primary, secondary, nor arbiter.
100 # - RSGhost "isreplicaset: true" in response.
101 # - PossiblePrimary Not yet checked, but another member thinks it is the primary.
102  
103 61 has type => (
104 is => 'lazy',
105 isa => ServerType,
106 builder => '_build_type',
107 writer => '_set_type',
108 );
109  
110 543 ▶sub _build_type {
Subroutine _build_type: called CC=10 SCAR=23.0
111 543 my ($self) = @_;
112 543 my $is_master = $self->is_master;
113 543 ▶ if ( !$is_master->{ok} ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $is_master->{'ok'}) { } 325 218 543
elsif ($is_master->{'msg'} and $is_master->{'msg'} eq 'isdbgrid') { } 23 195 218
elsif ($is_master->{'isreplicaset'}) { } 3 192 195
elsif (exists $is_master->{'setName'}) { } 171 21 192
114 325 return 'Unknown';
115 }
116 ▶ elsif ( $is_master->{msg} && $is_master->{msg} eq 'isdbgrid' ) {
Condition66%2/3
$is_master->{"msg"} and $is_master->{"msg"} eq "isdbgrid"
!ll&&!rl&&r
195023
MC/DC50%1/2
$is_master->{"msg"} and $is_master->{"msg"} eq "isdbgrid"
$is_master->{"msg"} $is_master->{"msg"} eq "isdbgrid"
Truth table66%2/3
A and B
A= $is_master->{"msg"}B= $is_master->{"msg"} eq "isdbgrid"
ABresult
0X0
100
111
117 23 return 'Mongos';
118 }
119 elsif ( $is_master->{isreplicaset} ) {
120 3 return 'RSGhost';
121 }
122 elsif ( exists $is_master->{setName} ) {
123 return
124 $is_master->{ismaster} ? return 'RSPrimary'
125 : $is_master->{hidden} ? return 'RSOther'
126 : $is_master->{secondary} ? return 'RSSecondary'
127 171 ▶ : $is_master->{arbiterOnly} ? return 'RSArbiter'
Branch: 4 branches
BranchTrueFalseTotal
$is_master->{'ismaster'} ? : 84 87 171
$is_master->{'hidden'} ? : 2 85 87
$is_master->{'secondary'} ? : 81 4 85
$is_master->{'arbiterOnly'} ? : 2 2 4
128 : 'RSOther';
129 }
130 else {
131 21 return 'Standalone';
132 }
133 }
134  
135 # hosts, passives, arbiters: Sets of addresses. This server's opinion of the
136 # replica set's members, if any. Default empty. The client monitors all three
137 # types of servers in a replica set.
138  
139 61 for my $s (qw/hosts passives arbiters/) {
140 183 has $s => (
141 is => 'lazy',
142 isa => HostAddressList,
143 builder => "_build_$s",
144 );
145  
146 61 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
147 183 ▶ *{"_build_$s"} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
148 867 ▶ [ map { lc $_ } ( @{ $_[0]->is_master->{$s} || [] } ) ];
Condition100%2/2
$_[0]->is_master->{$s} || []
l!l
103764
MC/DC100%1/1
$_[0]->is_master->{$s} || []
$_[0]->is_master->{$s}
Truth table100%2/2
A
A= $_[0]->is_master->{$s}
Aresult
11
00
149 183 };
150 }
151  
152  
153 # address configured as part of replica set: string or null. Default null.
154  
155 61 has me => (
156 is => 'lazy',
157 isa => Str,
158 builder => "_build_me",
159 );
160  
161 289 ▶sub _build_me {
Subroutine _build_me: called CC=2 SCAR=6.9
162 289 my ($self) = @_;
163 289 ▶ return $self->is_master->{me} || '';
Condition100%2/2
$self->is_master->{'me'} || ''
l!l
6283
MC/DC100%1/1
$self->is_master->{'me'} || ''
$self->is_master->{'me'}
Truth table100%2/2
A
A= $self->is_master->{'me'}
Aresult
11
00
164 }
165  
166 # setName: string or null. Default null.
167  
168 61 has set_name => (
169 is => 'lazy',
170 isa => Str,
171 builder => "_build_set_name",
172 );
173  
174 188 ▶sub _build_set_name {
Subroutine _build_set_name: called CC=2 SCAR=6.9
175 188 my ($self) = @_;
176 188 ▶ return $self->is_master->{setName} || '';
Condition100%2/2
$self->is_master->{'setName'} || ''
l!l
9494
MC/DC100%1/1
$self->is_master->{'setName'} || ''
$self->is_master->{'setName'}
Truth table100%2/2
A
A= $self->is_master->{'setName'}
Aresult
11
00
177 }
178  
179 # primary: an address. This server's opinion of who the primary is. Default
180 # null.
181  
182 61 has primary => (
183 is => 'lazy',
184 isa => Str, # not HostAddress -- might be empty string
185 builder => "_build_primary",
186 );
187  
188 289 ▶sub _build_primary {
Subroutine _build_primary: called CC=2 SCAR=6.9
189 289 my ($self) = @_;
190 289 ▶ return $self->is_master->{primary} || '';
Condition100%2/2
$self->is_master->{'primary'} || ''
l!l
6283
MC/DC100%1/1
$self->is_master->{'primary'} || ''
$self->is_master->{'primary'}
Truth table100%2/2
A
A= $self->is_master->{'primary'}
Aresult
11
00
191 }
192  
193 # tags: (a tag set) map from string to string. Default empty.
194  
195 61 has tags => (
196 is => 'lazy',
197 isa => HashRef,
198 builder => "_build_tags",
199 );
200  
201 16 ▶sub _build_tags {
Subroutine _build_tags: called CC=2 SCAR=7.2
202 16 my ($self) = @_;
203 16 ▶ return $self->is_master->{tags} || {};
Condition50%1/2
$self->is_master->{'tags'} || {}
l!l
160
MC/DC0%0/1
$self->is_master->{'tags'} || {}
$self->is_master->{'tags'}
Truth table50%1/2
A
A= $self->is_master->{'tags'}
Aresult
11
00
204 }
205  
206 # last_write_date: for replica set and wire version 5+ (converted to
207 # seconds)
208 61 has last_write_date => (
209 is => 'lazy',
210 isa => Num,
211 builder => "_build_last_write_date",
212 );
213  
214 54 ▶sub _build_last_write_date {
Subroutine _build_last_write_date: called CC=2 SCAR=7.1
215 54 my ($self) = @_;
216 54 ▶ return 0 unless exists $self->is_master->{lastWrite}{lastWriteDate};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->is_master->{"lastWrite"}{"lastWriteDate"} 0 54 54
217 54 return $self->is_master->{lastWrite}{lastWriteDate}->epoch;
218 }
219  
220 61 has is_available => (
221 is => 'lazy',
222 isa => Boolish,
223 builder => "_build_is_available",
224 );
225  
226 414 ▶sub _build_is_available {
Subroutine _build_is_available: called CC=2 SCAR=6.9
227 414 my ($self) = @_;
228 414 ▶ return $self->type ne 'Unknown' && $self->type ne 'PossiblePrimary';
Condition100%3/3
$self->type ne 'Unknown' && $self->type ne 'PossiblePrimary'
!ll&&!rl&&r
1802232
MC/DC100%2/2
$self->type ne 'Unknown' && $self->type ne 'PossiblePrimary'
$self->type ne 'Unknown' $self->type ne 'PossiblePrimary'
Truth table100%3/3
A && B
A= $self->type ne 'Unknown'B= $self->type ne 'PossiblePrimary'
ABresult
0X0
100
111
229 }
230  
231 61 has is_readable => (
232 is => 'lazy',
233 isa => Boolish,
234 builder => "_build_is_readable",
235 );
236  
237 # any of these can take reads. Topologies will screen inappropriate
238 # ones out. E.g. "Standalone" won't be found in a replica set topology.
239 0 ▶sub _build_is_readable {
Subroutine _build_is_readable: not called CC=1 SCAR=6.9
240 0 my ($self) = @_;
241 0 my $type = $self->type;
242 0 return !! grep { $type eq $_ } qw/Standalone RSPrimary RSSecondary Mongos/;
243 }
244  
245 61 has is_writable => (
246 is => 'lazy',
247 isa => Boolish,
248 builder => "_build_is_writable",
249 );
250  
251 # any of these can take writes. Topologies will screen inappropriate
252 # ones out. E.g. "Standalone" won't be found in a replica set topology.
253 29 ▶sub _build_is_writable {
Subroutine _build_is_writable: called CC=1 SCAR=0.0
254 29 my ($self) = @_;
255 29 my $type = $self->type;
256 29 return !! grep { $type eq $_ } qw/Standalone RSPrimary Mongos/;
257 }
258  
259 61 has is_data_bearing => (
260 is => 'lazy',
261 isa => Boolish,
262 builder => "_build_is_data_bearing",
263 );
264  
265 305 ▶sub _build_is_data_bearing {
Subroutine _build_is_data_bearing: called CC=1 SCAR=0.0
266 305 my ( $self ) = @_;
267 305 my $type = $self->type;
268 305 return !! grep { $type eq $_ } qw/Standalone RSPrimary RSSecondary Mongos/;
269 }
270  
271 # logicalSessionTimeoutMinutes can be not set by a client
272 61 has logical_session_timeout_minutes => (
273 is => 'lazy',
274 isa => Maybe [NonNegNum],
275 builder => "_build_logical_session_timeout_minutes",
276 );
277  
278 104 ▶sub _build_logical_session_timeout_minutes {
Subroutine _build_logical_session_timeout_minutes: called CC=2 SCAR=6.9
279 104 my ( $self ) = @_;
280 104 ▶ return $self->is_master->{logicalSessionTimeoutMinutes} || undef;
Condition100%2/2
$self->is_master->{'logicalSessionTimeoutMinutes'} || undef
l!l
698
MC/DC100%1/1
$self->is_master->{'logicalSessionTimeoutMinutes'} || undef
$self->is_master->{'logicalSessionTimeoutMinutes'}
Truth table100%2/2
A
A= $self->is_master->{'logicalSessionTimeoutMinutes'}
Aresult
11
00
281 }
282  
283 1188 ▶sub updated_since {
Subroutine updated_since: called CC=1 SCAR=0.0
Pod undocumented
284 1188 my ( $self, $time ) = @_;
285 1188 return( ($self->last_update_time - $time) > 0 );
286 }
287  
288 # check if server matches a single tag set (NOT a tag set list)
289 58 ▶sub matches_tag_set {
Subroutine matches_tag_set: called CC=2 SCAR=6.9
Pod undocumented
290 58 my ( $self, $ts ) = @_;
291 61 ▶ no warnings 'uninitialized'; # let undef equal empty string without complaint
Subroutine BEGIN: called CC=1 SCAR=0.0
292  
293 58 my $tg = $self->tags;
294  
295 # check if ts is a subset of tg: if any tags in ts that aren't in tg or where
296 # the tag values aren't equal mean ts is NOT a subset
297 58 ▶ if ( !defined first { !exists( $tg->{$_} ) || $tg->{$_} ne $ts->{$_} } keys %$ts ) {
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 2 branches
BranchTrueFalseTotal
unless (defined &first(sub { not exists $tg->{$_} or $tg->{$_} ne $ts->{$_}; } , keys %$ts)) 30 28 58
not exists $tg->{$_} or $tg->{$_} ne $ts->{$_} 0 61 61
298 30 return 1;
299 }
300  
301 28 return;
302 }
303  
304 54 ▶sub status_string {
Subroutine status_string: called CC=2 SCAR=7.9
Pod undocumented
305 54 my ($self) = @_;
306 54 ▶ if ( my $err = $self->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $self->error) { } 54 0 54
307 54 $err =~ tr[\n][ ];
308 return
309 54 sprintf( "%s (type: %s, error: %s)", $self->{address}, $self->{type}, $err);
310 }
311 else {
312 0 return sprintf( "%s (type: %s)", map { $self->$_ } qw/address type/ );
313 }
314 }
315  
316 0 ▶sub status_struct {
Subroutine status_struct: not called CC=3 SCAR=24.8
Pod undocumented
317 0 my ($self) = @_;
318 0 my $info = {
319 address => $self->address,
320 type => $self->type,
321 last_update_time => $self->last_update_time,
322 };
323 0 ▶ $info->{error} = $self->error if $self->error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->error 0 0 0
324 0 ▶ $info->{tags} = { %{ $self->tags } } if %{ $self->tags };
Branch: 1 branch
BranchTrueFalseTotal
if %{$self->tags;} 0 0 0
325 0 return $info;
326 }
327  
328  
329 61 1;
330  
331 # vim: ts=4 sts=4 sw=4 et:
« MongoDB/Op/_InsertOne.pm ↑ Summary MongoDB/Role/_PrivateConstructor.pm »