| 1 | # Copyright 2018 - 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::Role::_SessionSupport; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # MongoDB role to add support for sessions on Ops | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 60 | ▶ | use version; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 60 | our $VERSION = 'v2.2.2'; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 60 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 60 | ▶ | use MongoDB::_Types -types, 'to_IxHash'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 60 | ▶ | use MongoDB::_Constants; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 60 | ▶ | use Safe::Isa; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 60 | ▶ | use boolean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 60 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 60 | requires qw/ session retryable_write /; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub _apply_session_and_cluster_time { | ||||||||||||||||||||||||||
|
Subroutine
_apply_session_and_cluster_time: not called CC=13 SCAR=52.0
| |||||||||||||||||||||||||||||
| 34 | 0 | my ( $self, $link, $query_ref ) = @_; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # Assume that no session means no session support. Also means no way of | ||||||||||||||||||||||||||||
| 37 | # getting clusterTime. | ||||||||||||||||||||||||||||
| 38 | 0 | ▶ | return unless defined $self->session; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # Also assumption that the session was created from the current client - | ||||||||||||||||||||||||||||
| 41 | # not an issue in implicit sessions, but explicit sessions may have been | ||||||||||||||||||||||||||||
| 42 | # created by another client in the same scope. This should have been | ||||||||||||||||||||||||||||
| 43 | # checked further up the call chain | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | $$query_ref = to_IxHash( $$query_ref ); | |||||||||||||||||||||||||||
| 46 | 0 | ($$query_ref)->Push( 'lsid' => $self->session->session_id ); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | if ( $self->retryable_write || ! $self->session->_in_transaction_state( TXN_NONE ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->retryable_write or not $self->session->_in_transaction_state("none")
MC/DC0%0/2
$self->retryable_write or not $self->session->_in_transaction_state("none")
$self->retryable_write
not $self->session->_in_transaction_state("none")
Truth table0%0/3
A or not(B)
A= $self->retryable_writeB= not $self->session->_in_transaction_state("none")
| |||||||||||||||||||||||||||||
| 49 | 0 | ($$query_ref)->Push( 'txnNumber' => $self->session->_server_session->transaction_id ); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | if ( ! $self->session->_in_transaction_state( TXN_NONE ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | ($$query_ref)->Push( 'autocommit' => false ); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if ( $self->session->_in_transaction_state( TXN_STARTING ) ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 57 | 0 | ($$query_ref)->Push( 'startTransaction' => true ); | |||||||||||||||||||||||||||
| 58 | # delete first to not merge options | ||||||||||||||||||||||||||||
| 59 | 0 | ($$query_ref)->Delete( 'readConcern' ); | |||||||||||||||||||||||||||
| 60 | 0 | ($$query_ref)->Push( @{ $self->session->_get_transaction_read_concern->as_args( $self->session ) } ); | |||||||||||||||||||||||||||
| 61 | } elsif ( ! $self->session->_in_transaction_state( TXN_NONE ) ) { | ||||||||||||||||||||||||||||
| 62 | # read concern only valid outside a transaction or when starting | ||||||||||||||||||||||||||||
| 63 | 0 | ($$query_ref)->Delete( 'readConcern' ); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # write concern not allowed in transactions except when ending. We can | ||||||||||||||||||||||||||||
| 67 | # safely delete it here as you can only pass writeConcern through by | ||||||||||||||||||||||||||||
| 68 | # arguments to client of collection. | ||||||||||||||||||||||||||||
| 69 | 0 | ▶ | if ( $self->session->_in_transaction_state( TXN_STARTING, TXN_IN_PROGRESS ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | ($$query_ref)->Delete( 'writeConcern' ); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if ( $self->session->_in_transaction_state( TXN_ABORTED, TXN_COMMITTED ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->session->_in_transaction_state("aborted", "committed") and not $$query_ref->EXISTS("writeConcern")
MC/DC0%0/2
$self->session->_in_transaction_state("aborted", "committed") and not $$query_ref->EXISTS("writeConcern")
$self->session->_in_transaction_state("aborted", "committed")
not $$query_ref->EXISTS("writeConcern")
Truth table0%0/3
A and not(B)
A= $self->session->_in_transaction_state("aborted", "committed")B= not $$query_ref->EXISTS("writeConcern")
| |||||||||||||||||||||||||||||
| 74 | && ! ($$query_ref)->EXISTS('writeConcern') | ||||||||||||||||||||||||||||
| 75 | ) { | ||||||||||||||||||||||||||||
| 76 | 0 | ($$query_ref)->Push( @{ $self->session->_get_transaction_write_concern->as_args() } ); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # MUST be the last thing to touch the transaction state before sending, | ||||||||||||||||||||||||||||
| 80 | # so the various starting specific query modifications can be applied | ||||||||||||||||||||||||||||
| 81 | # The spec states that this should happen after the command even on error, | ||||||||||||||||||||||||||||
| 82 | # so happening before the command is sent is still valid | ||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if ( $self->session->_in_transaction_state( TXN_STARTING ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | $self->session->_set__transaction_state( TXN_IN_PROGRESS ); | |||||||||||||||||||||||||||
| 85 | # Set the session address for operation pinning in transactions against mongos | ||||||||||||||||||||||||||||
| 86 | 0 | $self->session->_set__address( $link->address ); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | $self->session->_server_session->update_last_use; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | my $cluster_time = $self->session->get_latest_cluster_time; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if ( defined $cluster_time && $link->supports_clusterTime ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $cluster_time and $link->supports_clusterTime
MC/DC0%0/2
defined $cluster_time and $link->supports_clusterTime
defined $cluster_time
$link->supports_clusterTime
Truth table0%0/3
A and B
A= defined $cluster_timeB= $link->supports_clusterTime
| |||||||||||||||||||||||||||||
| 94 | # Gossip the clusterTime | ||||||||||||||||||||||||||||
| 95 | 0 | ($$query_ref)->Push( '$clusterTime' => $cluster_time ); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | return; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # Somethign about this makes me wonder if clustertime is not going to be set | ||||||||||||||||||||||||||||
| 102 | # when it should: not all requests have sessions, but any response has the | ||||||||||||||||||||||||||||
| 103 | # chance to have a clustertime. If we do not have a client available to put the | ||||||||||||||||||||||||||||
| 104 | # clustertime in, then depending on the response there may be no way to | ||||||||||||||||||||||||||||
| 105 | # retreive the clustertime further down the call stack. | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | sub _update_session_and_cluster_time { | ||||||||||||||||||||||||||
|
Subroutine
_update_session_and_cluster_time: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 107 | 0 | my ( $self, $response ) = @_; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # No point continuing as theres nothing to do even if clusterTime is returned | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | return unless defined $self->session; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | my $cluster_time = $self->__extract_from( $response, '$clusterTime' ); | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | if ( defined $cluster_time ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | $self->session->client->_update_cluster_time( $cluster_time ); | |||||||||||||||||||||||||||
| 116 | 0 | $self->session->advance_cluster_time( $cluster_time ); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | my $recovery_token = $self->__extract_from( $response, 'recoveryToken' ); | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if ( defined $recovery_token ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | $self->session->_set__recovery_token( $recovery_token ); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | return; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub _update_session_pre_assert { | ||||||||||||||||||||||||||
|
Subroutine
_update_session_pre_assert: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 129 | 0 | my ( $self, $response ) = @_; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | return unless defined $self->session; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | my $operation_time = $self->__extract_from( $response, 'operationTime' ); | |||||||||||||||||||||||||||
| 134 | 0 | ▶ | $self->session->advance_operation_time( $operation_time ) if defined $operation_time; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | return; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # Certain errors have to happen as soon as possible, such as write concern | ||||||||||||||||||||||||||||
| 140 | # errors in a retryable write. This has to be seperate to the other functions | ||||||||||||||||||||||||||||
| 141 | # due to not all result objects having the base response inside, so cannot be | ||||||||||||||||||||||||||||
| 142 | # used to parse operationTime or $clusterTime | ||||||||||||||||||||||||||||
| 143 | 0 | ▶ | sub _assert_session_errors { | ||||||||||||||||||||||||||
|
Subroutine
_assert_session_errors: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 144 | 0 | my ( $self, $response ) = @_; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | if ( $self->retryable_write ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | $response->assert_no_write_concern_error; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | return; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | ▶ | sub _update_session_connection_error { | ||||||||||||||||||||||||||
|
Subroutine
_update_session_connection_error: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 154 | 0 | my ( $self, $err ) = @_; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | ▶ | return unless defined $self->session; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 0 | ▶ | $self->session->_server_session->mark_dirty | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$err->$MongoDB::Role::_SessionSupport::_isa("MongoDB::ConnectionError") or $err->$MongoDB::Role::_SessionSupport::_isa("MongoDB::NetworkTimeout")
MC/DC0%0/2
$err->$MongoDB::Role::_SessionSupport::_isa("MongoDB::ConnectionError") or $err->$MongoDB::Role::_SessionSupport::_isa("MongoDB::NetworkTimeout")
$err->$MongoDB::Role::_SessionSupport::_isa("MongoDB::ConnectionError")
$err->$MongoDB::Role::_SessionSupport::_isa("MongoDB::NetworkTimeout")
Truth table0%0/3
A or B
A= $err->$MongoDB::Role::_SessionSupport::_isa("MongoDB::ConnectionError")B= $err->$MongoDB::Role::_SessionSupport::_isa("MongoDB::NetworkTimeout")
| |||||||||||||||||||||||||||||
| 158 | if $err->$_isa("MongoDB::ConnectionError") || $err->$_isa("MongoDB::NetworkTimeout"); | ||||||||||||||||||||||||||||
| 159 | 0 | return $self->session->_maybe_apply_error_labels_and_unpin( $err ); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | ▶ | sub __extract_from { | ||||||||||||||||||||||||||
|
Subroutine
__extract_from: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 163 | 0 | my ( $self, $response, $key ) = @_; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | ▶ | if ( $response->$_isa( 'MongoDB::CommandResult' ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 0 | return $response->output->{ $key }; | |||||||||||||||||||||||||||
| 167 | } else { | ||||||||||||||||||||||||||||
| 168 | 0 | return $response->{ $key }; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 60 | 1; | |||||||||||||||||||||||||||