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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
|
# frozen_string_literal: true
# rubocop:todo all
require 'spec_helper'
class RetryableTestConsumer
include Mongo::Retryable
attr_reader :client
attr_reader :cluster
attr_reader :operation
def initialize(operation, cluster, client)
@operation = operation
@cluster = cluster
@client = client
end
def max_read_retries
client.max_read_retries
end
def read_retry_interval
client.read_retry_interval
end
def read
read_with_retry(nil, Mongo::ServerSelector.get(mode: :primary)) do
operation.execute
end
end
def read_legacy
read_with_retry do
operation.execute
end
end
def write
# This passes a nil session and therefore triggers
# legacy_write_with_retry code path
context = Mongo::Operation::Context.new(client: @client, session: session)
write_with_retry(write_concern, context: context) do
operation.execute
end
end
def retry_write_allowed_as_configured?
write_worker.retry_write_allowed?(session, write_concern)
end
end
class LegacyRetryableTestConsumer < RetryableTestConsumer
def session
nil
end
def write_concern
nil
end
end
class ModernRetryableTestConsumer < LegacyRetryableTestConsumer
include RSpec::Mocks::ExampleMethods
def session
double('session').tap do |session|
expect(session).to receive(:retry_writes?).and_return(true)
allow(session).to receive(:materialize_if_needed)
# mock everything else that is in the way
i = 1
allow(session).to receive(:next_txn_num) { i += 1 }
allow(session).to receive(:in_transaction?).and_return(false)
allow(session).to receive(:pinned_server)
allow(session).to receive(:pinned_connection_global_id)
allow(session).to receive(:starting_transaction?).and_return(false)
allow(session).to receive(:materialize)
allow(session).to receive(:with_transaction_deadline).and_return(nil)
end
end
def write_concern
nil
end
end
class RetryableHost
include Mongo::Retryable
def retry_write_allowed?(*args)
write_worker.retry_write_allowed?(*args)
end
end
describe Mongo::Retryable do
let(:operation) do
double('operation')
end
let(:connection) do
double('connection')
end
let(:server) do
double('server').tap do |server|
allow(server).to receive('with_connection').and_yield(connection)
end
end
let(:max_read_retries) { 1 }
let(:max_write_retries) { 1 }
let(:cluster) do
double('cluster', next_primary: server).tap do |cluster|
allow(cluster).to receive(:replica_set?).and_return(true)
allow(cluster).to receive(:addresses).and_return(['x'])
end
end
let(:client) do
double('client').tap do |client|
allow(client).to receive(:cluster).and_return(cluster)
allow(client).to receive(:max_read_retries).and_return(max_read_retries)
allow(client).to receive(:max_write_retries).and_return(max_write_retries)
end
end
let(:server_selector) do
double('server_selector', select_server: server)
end
let(:retryable) do
LegacyRetryableTestConsumer.new(operation, cluster, client)
end
let(:session) do
double('session').tap do |session|
allow(session).to receive(:pinned_connection_global_id).and_return(nil)
allow(session).to receive(:materialize_if_needed)
end
end
let(:context) do
Mongo::Operation::Context.new(client: client, session: session)
end
before do
# Retryable reads perform server selection
allow_any_instance_of(Mongo::ServerSelector::Primary).to receive(:select_server).and_return(server)
end
shared_examples_for 'reads with retries' do
context 'when no exception occurs' do
before do
expect(operation).to receive(:execute).and_return(true)
end
it 'executes the operation once' do
expect(read_operation).to be true
end
end
context 'when ending_transaction is true' do
let(:retryable) { RetryableTestConsumer.new(operation, cluster, client) }
let(:context) do
Mongo::Operation::Context.new(client: client, session: nil)
end
it 'raises ArgumentError' do
expect do
retryable.write_with_retry(nil, ending_transaction: true, context: context) do
fail 'Expected not to get here'
end
end.to raise_error(ArgumentError, 'Cannot end a transaction without a session')
end
end
context 'when a socket error occurs' do
before do
expect(retryable).to receive(:select_server).ordered
expect(operation).to receive(:execute).and_raise(Mongo::Error::SocketError).ordered
expect(retryable).to receive(:select_server).ordered
expect(operation).to receive(:execute).and_return(true).ordered
end
it 'executes the operation twice' do
expect(read_operation).to be true
end
end
context 'when a socket timeout error occurs' do
before do
expect(retryable).to receive(:select_server).ordered
expect(operation).to receive(:execute).and_raise(Mongo::Error::SocketTimeoutError).ordered
expect(retryable).to receive(:select_server).ordered
expect(operation).to receive(:execute).and_return(true).ordered
end
it 'executes the operation twice' do
expect(read_operation).to be true
end
end
context 'when an operation failure occurs' do
context 'when the operation failure is not retryable' do
let(:error) do
Mongo::Error::OperationFailure.new('not authorized')
end
before do
expect(operation).to receive(:execute).and_raise(error).ordered
end
it 'raises the exception' do
expect {
read_operation
}.to raise_error(Mongo::Error::OperationFailure)
end
end
context 'when the operation failure is retryable' do
let(:error) do
Mongo::Error::OperationFailure.new('not master')
end
context 'when the retry succeeds' do
before do
expect(retryable).to receive(:select_server).ordered
expect(operation).to receive(:execute).and_raise(error).ordered
expect(client).to receive(:read_retry_interval).and_return(0.1).ordered
expect(retryable).to receive(:select_server).ordered
expect(operation).to receive(:execute).and_return(true).ordered
end
it 'returns the result' do
expect(read_operation).to be true
end
end
context 'when the retry fails once and then succeeds' do
let(:max_read_retries) { 2 }
before do
expect(retryable).to receive(:select_server).ordered
expect(operation).to receive(:execute).and_raise(error.dup).ordered
expect(client).to receive(:read_retry_interval).and_return(0.1).ordered
expect(retryable).to receive(:select_server).ordered
# Since exception is mutated when notes are added to it,
# we need to ensure each attempt starts with a pristine exception.
expect(operation).to receive(:execute).and_raise(error.dup).ordered
expect(client).to receive(:read_retry_interval).and_return(0.1).ordered
expect(retryable).to receive(:select_server).ordered
expect(operation).to receive(:execute).and_return(true).ordered
end
it 'returns the result' do
expect(read_operation).to be true
end
end
end
end
end
describe '#read_with_retry' do
let(:read_operation) do
retryable.read
end
it_behaves_like 'reads with retries'
context 'zero argument legacy invocation' do
before do
allow_any_instance_of(Mongo::ServerSelector::PrimaryPreferred).to receive(:select_server).and_return(server)
end
let(:read_operation) do
retryable.read_legacy
end
it_behaves_like 'reads with retries'
end
end
describe '#retry_write_allowed?' do
let(:retryable) { RetryableHost.new }
context 'nil session' do
it 'returns false' do
expect(retryable.retry_write_allowed?(nil, nil)).to be false
end
end
context 'with session' do
let(:session) { double('session') }
context 'retry writes enabled' do
context 'nil write concern' do
let(:write_concern) { nil }
it 'returns true' do
expect(session).to receive(:retry_writes?).and_return(true)
expect(retryable.retry_write_allowed?(session, write_concern)).to be true
end
end
context 'hash write concern with w: 0' do
let(:write_concern) { {w: 0} }
it 'returns false' do
expect(session).to receive(:retry_writes?).and_return(true)
expect(retryable.retry_write_allowed?(session, write_concern)).to be false
end
end
context 'hash write concern with w: :majority' do
let(:write_concern) { {w: :majority} }
it 'returns true' do
expect(session).to receive(:retry_writes?).and_return(true)
expect(retryable.retry_write_allowed?(session, write_concern)).to be true
end
end
context 'write concern object with w: 0' do
let(:write_concern) { Mongo::WriteConcern.get(w: 0) }
it 'returns false' do
expect(session).to receive(:retry_writes?).and_return(true)
expect(retryable.retry_write_allowed?(session, write_concern)).to be false
end
end
context 'write concern object with w: :majority' do
let(:write_concern) { Mongo::WriteConcern.get(w: :majority) }
it 'returns true' do
expect(session).to receive(:retry_writes?).and_return(true)
expect(retryable.retry_write_allowed?(session, write_concern)).to be true
end
end
end
context 'retry writes not enabled' do
it 'returns false' do
expect(session).to receive(:retry_writes?).and_return(false)
expect(retryable.retry_write_allowed?(session, nil)).to be false
end
end
end
end
describe '#write_with_retry - legacy' do
before do
# Quick sanity check that the expected code path is being exercised
expect(retryable.retry_write_allowed_as_configured?).to be false
end
context 'when no exception occurs' do
before do
expect(operation).to receive(:execute).and_return(true)
end
it 'executes the operation once' do
expect(retryable.write).to be true
end
end
shared_examples 'executes the operation twice' do
it 'executes the operation twice' do
expect(retryable.write).to be true
end
end
context 'when an operation failure error occurs with a RetryableWriteError label' do
let(:error) do
Mongo::Error::OperationFailure.new(nil, nil, labels: ['RetryableWriteError'])
end
before do
expect(operation).to receive(:execute).and_raise(error).ordered
expect(cluster).to receive(:scan!).and_return(true).ordered
expect(operation).to receive(:execute).and_return(true).ordered
end
it_behaves_like 'executes the operation twice'
end
context 'when an operation failure error occurs without a RetryableWriteError label' do
before do
expect(operation).to receive(:execute).and_raise(Mongo::Error::OperationFailure).ordered
end
it 'raises an exception' do
expect {
retryable.write
}.to raise_error(Mongo::Error::OperationFailure)
end
end
context 'when a socket error occurs with a RetryableWriteError label' do
let(:error) do
error = Mongo::Error::SocketError.new('socket error')
error.add_label('RetryableWriteError')
error
end
before do
expect(operation).to receive(:execute).and_raise(error).ordered
end
it 'raises an exception' do
expect {
retryable.write
}.to raise_error(Mongo::Error::SocketError)
end
end
context 'when a socket timeout error occurs with a RetryableWriteError label' do
let(:error) do
error = Mongo::Error::SocketTimeoutError.new('socket timeout error')
error.add_label('RetryableWriteError')
error
end
before do
expect(operation).to receive(:execute).and_raise(error).ordered
end
it 'raises an exception' do
expect {
retryable.write
}.to raise_error(Mongo::Error::SocketTimeoutError)
end
end
context 'when a non-retryable exception occurs' do
before do
expect(operation).to receive(:execute).and_raise(
Mongo::Error::UnsupportedCollation.new('unsupported collation')).ordered
end
it 'raises an exception' do
expect {
retryable.write
}.to raise_error(Mongo::Error::UnsupportedCollation)
end
end
end
describe '#write_with_retry - modern' do
let(:retryable) do
ModernRetryableTestConsumer.new(operation, cluster, client)
end
before do
# Quick sanity check that the expected code path is being exercised
expect(retryable.retry_write_allowed_as_configured?).to be true
allow(server).to receive(:retry_writes?).and_return(true)
allow(cluster).to receive(:scan!)
end
context 'when no exception occurs' do
before do
expect(operation).to receive(:execute).and_return(true)
end
it 'executes the operation once' do
expect(retryable.write).to be true
end
end
shared_examples 'executes the operation twice' do
it 'executes the operation twice' do
expect(retryable.write).to be true
end
end
context 'when an operation failure error occurs with a RetryableWriteError label' do
let(:error) do
Mongo::Error::OperationFailure.new(nil, nil, labels: ['RetryableWriteError'])
end
before do
server = cluster.next_primary
expect(operation).to receive(:execute).and_raise(error).ordered
expect(operation).to receive(:execute).and_return(true).ordered
end
it_behaves_like 'executes the operation twice'
end
context 'when an operation failure error occurs without a RetryableWriteError label' do
before do
expect(operation).to receive(:execute).and_raise(Mongo::Error::OperationFailure).ordered
end
it 'raises an exception' do
expect {
retryable.write
}.to raise_error(Mongo::Error::OperationFailure)
end
end
context 'when a socket error occurs with a RetryableWriteError label' do
let(:error) do
error = Mongo::Error::SocketError.new('socket error')
error.add_label('RetryableWriteError')
error
end
before do
expect(operation).to receive(:execute).and_raise(error).ordered
# This is where the server would be marked unknown, but since
# we are not tracking which server the operation was sent to,
# we are not able to assert this.
# There is no explicit cluster scan requested.
expect(operation).to receive(:execute).and_return(true).ordered
end
it_behaves_like 'executes the operation twice'
end
context 'when a socket error occurs without a RetryableWriteError label' do
let(:error) do
Mongo::Error::SocketError.new('socket error')
end
before do
expect(operation).to receive(:execute).and_raise(error).ordered
end
it 'raises an exception' do
expect {
retryable.write
}.to raise_error(Mongo::Error::SocketError)
end
end
context 'when a socket timeout occurs with a RetryableWriteError label' do
let(:error) do
error = Mongo::Error::SocketTimeoutError.new('socket timeout error')
error.add_label('RetryableWriteError')
error
end
before do
expect(operation).to receive(:execute).and_raise(error).ordered
# This is where the server would be marked unknown, but since
# we are not tracking which server the operation was sent to,
# we are not able to assert this.
# There is no explicit cluster scan requested (and the operation may
# end up being sent to the same server it was sent to originally).
expect(operation).to receive(:execute).and_return(true).ordered
end
it_behaves_like 'executes the operation twice'
end
context 'when a socket timeout occurs without a RetryableWriteError label' do
let(:error) do
Mongo::Error::SocketTimeoutError.new('socket timeout error')
end
before do
expect(operation).to receive(:execute).and_raise(error).ordered
end
it 'raises an exception' do
expect {
retryable.write
}.to raise_error(Mongo::Error::SocketTimeoutError)
end
end
context 'when a non-retryable exception occurs' do
before do
expect(operation).to receive(:execute).and_raise(
Mongo::Error::UnsupportedCollation.new('unsupported collation')).ordered
end
it 'raises an exception' do
expect {
retryable.write
}.to raise_error(Mongo::Error::UnsupportedCollation)
end
end
context 'when an error due to using an unsupported storage engine occurs' do
before do
expect(operation).to receive(:execute).and_raise(
Mongo::Error::OperationFailure.new('message which is not checked',
nil, code: 20, server_message: 'Transaction numbers are only allowed on...',
)).ordered
end
it 'raises an exception with the correct error message' do
expect {
retryable.write
}.to raise_error(Mongo::Error::OperationFailure, /This MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string or use the retry_writes: false Ruby client option/)
end
end
end
end
|