1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
# Copyright 2018 - present MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
use strict;
use warnings;
package MongoDB::_Dispatcher;
# Encapsulate op dispatching; breaking this out from client
# allows avoiding circular references with the session pool class.
use version;
our $VERSION = 'v2.2.2';
use Moo;
use MongoDB::_Constants;
use MongoDB::_Types qw(
Boolish
);
use Carp;
use Types::Standard qw(
InstanceOf
);
use Safe::Isa;
use namespace::clean;
has topology => (
is => 'ro',
required => 1,
isa => InstanceOf ['MongoDB::_Topology'],
);
has retry_writes => (
is => 'ro',
required => 1,
isa => Boolish,
);
has retry_reads => (
is => 'ro',
required => 1,
isa => Boolish,
);
# Reset session state if we're outside an active transaction, otherwise set
# that this transaction actually has operations
sub _maybe_update_session_state {
my ( $self, $op ) = @_;
if ( defined $op->session && ! $op->session->_active_transaction ) {
$op->session->_set__transaction_state( TXN_NONE );
} elsif ( defined $op->session ) {
$op->session->_set__has_transaction_operations( 1 );
}
}
# op dispatcher written in highly optimized style
sub send_direct_op {
my ( $self, $op, $address ) = @_;
my ( $link, $result );
$self->_maybe_update_session_state( $op );
( $link = $self->{topology}->get_specific_link( $address, $op ) ), (
eval { ($result) = $op->execute($link); 1 } or do {
my $err = length($@) ? $@ : "caught error, but it was lost in eval unwind";
if ( $err->$_isa("MongoDB::ConnectionError") || $err->$_isa("MongoDB::NetworkTimeout") ) {
$self->{topology}->mark_server_unknown( $link->server, $err );
}
elsif ( $err->$_isa("MongoDB::NotMasterError") ) {
$self->{topology}->mark_server_unknown( $link->server, $err );
$self->{topology}->mark_stale;
}
# regardless of cleanup, rethrow the error
WITH_ASSERTS ? ( confess $err ) : ( die $err );
}
),
return $result;
}
sub _retrieve_link_for {
my ( $self, $op, $rw ) = @_;
my $topology = $self->{'topology'};
my $link;
if ( $op->session
&& $op->session->_address # no point trying if theres no address....
&& $op->session->_active_transaction # this is true during a transaction and on every commit
&& $topology->_supports_mongos_pinning_transactions )
{
$link = $topology->get_specific_link( $op->session->_address, $op );
}
elsif ( $rw eq 'w' ) {
$link = $topology->get_writable_link( $op );
} else {
$link = $topology->get_readable_link( $op );
}
return $link;
}
# op dispatcher written in highly optimized style
sub send_write_op {
my ( $self, $op ) = @_;
my ( $link, $result );
$self->_maybe_update_session_state( $op );
( $link = $self->_retrieve_link_for( $op, 'w' ) ), (
eval { ($result) = $self->_try_op_for_link( $link, $op ); 1 } or do {
my $err = length($@) ? $@ : "caught error, but it was lost in eval unwind";
WITH_ASSERTS ? ( confess $err ) : ( die $err );
}
),
return $result;
}
# Sometimes, seeing an op dispatched as "send_write_op" is confusing when
# really, we're just insisting that it be sent only to a primary or
# directly connected server.
BEGIN {
no warnings 'once';
*send_primary_op = \&send_write_op;
}
sub send_retryable_write_op {
my ( $self, $op, $force ) = @_;
my ( $link, $result ) = ( $self->_retrieve_link_for( $op, 'w' ) );
$self->_maybe_update_session_state( $op );
# Need to force to do a retryable write on a Transaction Commit or Abort.
# $force is an override for retry_writes, but theres no point trying that
# if the link doesnt support it anyway.
# This triggers on the following:
# * $force is not set to 'force'
# (specifically for retrying writes in ending transaction operations)
# * retry writes is not enabled or the link doesnt support retryWrites
# * if an active transaction is starting or in progress
unless ( $link->supports_retryWrites
&& ( $self->retry_writes || ( defined $force && $force eq 'force' ) )
&& ( defined $op->session
&& ! $op->session->_in_transaction_state( TXN_STARTING, TXN_IN_PROGRESS )
)
) {
eval { ($result) = $self->_try_op_for_link( $link, $op ); 1 } or do {
my $err = length($@) ? $@ : "caught error, but it was lost in eval unwind";
WITH_ASSERTS ? ( confess $err ) : ( die $err );
};
return $result;
}
# If we get this far and there is no session, then somethings gone really
# wrong, so probably not worth worrying about.
# increment transaction id before write, but otherwise is the same for both
# attempts. If not in a transaction, is a no-op
$op->session->_increment_transaction_id;
$op->retryable_write( 1 );
# attempt the op the first time
eval { ($result) = $self->_try_op_for_link( $link, $op ); 1 } or do {
my $err = length($@) ? $@ : "caught error, but it was lost in eval unwind";
if ( $err->$_call_if_can('_is_storage_engine_not_retryable') ) {
# Break encapsulation to rewrite the message, then rethrow.
$err->{message} = "This MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string.";
die $err;
}
# If the error is not retryable, then drop out
unless ( $err->$_call_if_can('_is_retryable') ) {
WITH_ASSERTS ? ( confess $err ) : ( die $err );
}
# Must check if error is retryable before getting the link, in case we
# get a 'no writable servers' error. In the case of a mongos retry,
# this will end up as the same server by design.
my $retry_link = $self->_retrieve_link_for( $op, 'w' );
# Rare chance that the new link is not retryable
unless ( $retry_link->supports_retryWrites ) {
WITH_ASSERTS ? ( confess $err ) : ( die $err );
}
# Second attempt
eval { ($result) = $self->_try_op_for_link( $retry_link, $op ); 1 } or do {
my $retry_err = length($@) ? $@ : "caught error, but it was lost in eval unwind";
WITH_ASSERTS ? ( confess $retry_err ) : ( die $retry_err );
};
};
# just in case this gets reused for some reason
$op->retryable_write( 0 );
return $result;
}
sub _is_primary_stepdown {
my ($self, $err, $link) = @_;
my $err_info = $err->{result}->{output};
my $err_code_name = '';
$err_code_name = $err_info->{'codeName'} if defined $err_info->{'codeName'};
my @other_errors = qw(ShutdownInProgress InterruptedAtShutdown);
my $not_master = (
$err->$_isa('MongoDB::NotMasterError')
|| ( $err_info && $err_code_name eq 'NotMaster' )
) && $link->max_wire_version < 8;
return (
$err_info && grep { $err_code_name eq $_ } @other_errors
) || $not_master;
}
# op dispatcher written in highly optimized style
sub _try_op_for_link {
my ( $self, $link, $op ) = @_;
my $result;
(
eval { ($result) = $op->execute($link, $self->{topology}->type); 1 } or do {
my $err = length($@) ? $@ : "caught error, but it was lost in eval unwind";
if ( $err->$_isa("MongoDB::ConnectionError") || $err->$_isa("MongoDB::NetworkTimeout") ) {
$self->{topology}->mark_server_unknown( $link->server, $err );
}
elsif ( $self->_is_primary_stepdown($err, $link) ) {
$self->{topology}->mark_server_unknown( $link->server, $err );
$self->{topology}->mark_stale;
}
# normal die here instead of assert, which is used later
die $err;
}
),
return $result;
}
sub send_retryable_read_op {
my ( $self, $op ) = @_;
my $result;
# Get transaction read preference if in a transaction.
if ( defined $op->session && $op->session->_active_transaction ) {
# Transactions may only read from primary in MongoDB 4.0, so get and
# check the read preference from the transaction settings as per
# transaction spec - see MongoDB::_TransactionOptions
$op->read_preference( $op->session->_get_transaction_read_preference );
}
my $link = $self->_retrieve_link_for( $op, 'r' );
$self->_maybe_update_session_state( $op );
if ( ! $link->supports_retryReads
|| ! $self->retry_reads
|| ( defined $op->session && $op->session->_in_transaction_state( TXN_STARTING, TXN_IN_PROGRESS ))
) {
eval { ($result) = $self->_try_op_for_link( $link, $op ); 1 } or do {
my $err = length($@) ? $@ : "caught error, but it was lost in eval unwind";
WITH_ASSERTS ? ( confess $err ) : ( die $err );
};
return $result;
}
$op->session->_increment_transaction_id if $op->session;
$op->retryable_read( 1 );
# attempt the op the first time
eval { ($result) = $self->_try_op_for_link( $link, $op ); 1 } or do {
my $err = length($@) ? $@ : "caught error, but it was lost in eval unwind";
# If the error is not retryable, then drop out
unless ( $err->$_call_if_can('_is_retryable') ) {
WITH_ASSERTS ? ( confess $err ) : ( die $err );
}
my $retry_link = $self->_retrieve_link_for( $op, 'r' );
# Rare chance that the new link is not retryable
unless ( $retry_link->supports_retryReads ) {
WITH_ASSERTS ? ( confess $err ) : ( die $err );
}
# Second attempt
eval { ($result) = $self->_try_op_for_link( $retry_link, $op ); 1 } or do {
my $retry_err = length($@) ? $@ : "caught error, but it was lost in eval unwind";
WITH_ASSERTS ? ( confess $retry_err ) : ( die $retry_err );
};
};
# just in case this gets reused for some reason
$op->retryable_read( 0 );
return $result;
}
# op dispatcher written in highly optimized style
sub send_read_op {
my ( $self, $op ) = @_;
my ( $link, $type, $result );
# Get transaction read preference if in a transaction.
if ( defined $op->session && $op->session->_active_transaction ) {
# Transactions may only read from primary in MongoDB 4.0, so get and
# check the read preference from the transaction settings as per
# transaction spec - see MongoDB::_TransactionOptions
$op->read_preference( $op->session->_get_transaction_read_preference );
}
$self->_maybe_update_session_state( $op );
( $link = $self->_retrieve_link_for( $op, 'r' ) ),
( $type = $self->{topology}->type ), (
eval { ($result) = $op->execute( $link, $type ); 1 } or do {
my $err = length($@) ? $@ : "caught error, but it was lost in eval unwind";
if ( $err->$_isa("MongoDB::ConnectionError") || $err->$_isa("MongoDB::NetworkTimeout") ) {
$self->{topology}->mark_server_unknown( $link->server, $err );
}
elsif ( $err->$_isa("MongoDB::NotMasterError") ) {
$self->{topology}->mark_server_unknown( $link->server, $err );
$self->{topology}->mark_stale;
}
# regardless of cleanup, rethrow the error
WITH_ASSERTS ? ( confess $err ) : ( die $err );
}
),
return $result;
}
1;
|