File Coverage

blib/lib/MongoDB/Op/_EndTxn.pm
Criterion Covered Total %
statement 27 44 61.3
branch 0 6 0.0
condition 0 3 0.0
subroutine 9 11 81.8
pod 0 1 0.0
total 36 65 55.3


line stmt bran cond sub pod time code
1             # Copyright 2015 - 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 59     59   357 use strict;
  59         108  
  59         1537  
16 59     59   271 use warnings;
  59         97  
  59         2104  
17             package MongoDB::Op::_EndTxn;
18              
19             # Encapsulate code path for end transaction commands
20              
21 59     59   284 use version;
  59         99  
  59         382  
22             our $VERSION = 'v2.2.2';
23              
24 59     59   4486 use Moo;
  59         106  
  59         374  
25              
26 59     59   39661 use MongoDB::Op::_Command;
  59         214  
  59         3027  
27 59         336 use MongoDB::_Types qw(
28             Document
29             to_IxHash
30 59     59   456 );
  59         119  
31 59     59   38286 use MongoDB::_Constants qw( TXN_WTIMEOUT_RETRY_DEFAULT );
  59         164  
  59         4510  
32 59         432 use Types::Standard qw(
33             HashRef
34             Maybe
35             Int
36 59     59   367 );
  59         116  
37              
38 59     59   66257 use namespace::clean;
  59         129  
  59         275  
39              
40             with $_ for qw(
41             MongoDB::Role::_PrivateConstructor
42             MongoDB::Role::_DatabaseOp
43             MongoDB::Role::_SessionSupport
44             MongoDB::Role::_CommandMonitoring
45             );
46              
47             has query => (
48             is => 'ro',
49             required => 1,
50             writer => '_set_query',
51             isa => Document,
52             );
53              
54             sub _get_query_maybe_with_write_concern {
55 0     0     my ( $self, $topology ) = @_;
56              
57 0           my $query = to_IxHash( $self->query );
58              
59 0 0         if ( $self->session->_has_attempted_end_transaction ) {
60 0           my $wc_existing = $self->session->_get_transaction_write_concern;
61 0           my $wc = $wc_existing->as_args->[1];
62              
63 0 0         $query->Push( writeConcern => {
64             # allows for an override if set
65             wtimeout => TXN_WTIMEOUT_RETRY_DEFAULT,
66             ( $wc ? %$wc : () ),
67             # must be a majority on retrying
68             w => 'majority',
69             });
70             }
71              
72             # If we've gotten this far and a sharded topology doesnt support
73             # transactions, something has gone seriously wrong
74 0 0 0       if ( $topology eq 'Sharded' && defined $self->session->_recovery_token ) {
75 0           $query->Push( recoveryToken => $self->session->_recovery_token );
76             }
77              
78 0           return $query;
79             }
80              
81             sub execute {
82 0     0 0   my ( $self, $link, $topology ) = @_;
83              
84 0           my $query = $self->_get_query_maybe_with_write_concern( $topology );
85             # Set that an attempt to commit the transaction has been made after getting
86             # query but before execute - stops error unwind losing it
87 0           $self->session->_has_attempted_end_transaction( 1 );
88             my $op = MongoDB::Op::_Command->_new(
89             query_flags => {},
90             query => $query,
91 0           map { $_ => $self->$_ } qw(db_name bson_codec session monitoring_callback)
  0            
92             );
93 0           my $result = $op->execute( $link, $topology );
94 0           $result->assert_no_write_concern_error;
95 0           return $result;
96             }
97              
98             1;