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 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
|
# frozen_string_literal: true
# rubocop:todo all
require 'spec_helper'
describe 'Change stream integration' do
retry_test tries: 4
require_mri
max_example_run_time 7
min_server_fcv '3.6'
require_topology :replica_set
require_wired_tiger
let(:fail_point_base_command) do
{ 'configureFailPoint' => "failCommand" }
end
# There is value in not clearing fail points between tests because
# their triggering will distinguish fail points not being set vs
# them not being triggered
def clear_fail_point(collection)
collection.client.use(:admin).command(fail_point_base_command.merge(mode: "off"))
end
class << self
def clear_fail_point_before
before do
clear_fail_point(authorized_collection)
end
end
end
describe 'watch+next' do
let(:change_stream) { authorized_collection.watch }
shared_context 'returns a change document' do
it 'returns a change document' do
change_stream
authorized_collection.insert_one(:a => 1)
sleep 0.5
change = change_stream.to_enum.next
expect(change).to be_a(BSON::Document)
expect(change['operationType']).to eql('insert')
doc = change['fullDocument']
expect(doc['_id']).to be_a(BSON::ObjectId)
doc.delete('_id')
expect(doc).to eql('a' => 1)
end
end
shared_examples_for 'raises an exception' do
it 'raises an exception and does not attempt to resume' do
change_stream
subscriber = Mrss::EventSubscriber.new
authorized_client.subscribe(Mongo::Monitoring::COMMAND, subscriber)
expect do
change_stream.to_enum.next
end.to raise_error(Mongo::Error::OperationFailure)
aggregate_commands = subscriber.started_events.select { |e| e.command_name == 'aggregate' }
expect(aggregate_commands.length).to be 0
get_more_commands = subscriber.started_events.select { |e| e.command_name == 'getMore' }
expect(get_more_commands.length).to be 1
end
end
context 'no errors' do
it 'next returns changes' do
change_stream
authorized_collection.insert_one(:a => 1)
change = change_stream.to_enum.next
expect(change).to be_a(BSON::Document)
expect(change['operationType']).to eql('insert')
doc = change['fullDocument']
expect(doc['_id']).to be_a(BSON::ObjectId)
doc.delete('_id')
expect(doc).to eql('a' => 1)
end
end
context 'error on initial aggregation' do
min_server_fcv '4.0'
clear_fail_point_before
let(:client) do
authorized_client_without_any_retries
end
before do
client.use(:admin).command(fail_point_base_command.merge(
:mode => {:times => 1},
:data => {:failCommands => ['aggregate'], errorCode: 10107}))
end
it 'watch raises error' do
expect do
client['change-stream'].watch
end.to raise_error(Mongo::Error::OperationFailure, /10107\b.*Failing command (due to|via) 'failCommand' failpoint/)
end
end
context 'one error on getMore' do
min_server_fcv '4.0'
clear_fail_point_before
context 'error on first getMore' do
before do
authorized_collection.client.use(:admin).command(fail_point_base_command.merge(
mode: {times: 1},
data: {
failCommands: ['getMore'],
errorCode: error_code,
errorLabels: error_labels,
}))
end
context 'when the error is resumable' do
let(:error_code) { 10107 }
let(:error_labels) { ["ResumableChangeStreamError"] }
it_behaves_like 'returns a change document'
end
context 'when the error is Interrupted' do
let(:error_code) { 11601 }
let(:error_labels) { [] }
it_behaves_like 'raises an exception'
end
context 'when the error is CappedPositionLost' do
let(:error_code) { 136 }
let(:error_labels) { [] }
it_behaves_like 'raises an exception'
end
context 'when the error is CursorKilled' do
let(:error_code) { 237 }
let(:error_labels) { [] }
it_behaves_like 'raises an exception'
end
context 'when the error is ElectionInProgress' do
let(:error_code) { 216 }
let(:error_labels) { [] }
it_behaves_like 'raises an exception'
end
end
context 'error on a getMore other than first' do
before do
# Need to retrieve a change stream document successfully prior to
# failing to have the resume token, otherwise the change stream
# ignores documents inserted after the first aggregation
# and the test gets stuck
change_stream
authorized_collection.insert_one(:a => 1)
change_stream.to_enum.next
authorized_collection.insert_one(:a => 1)
authorized_collection.client.use(:admin).command(fail_point_base_command.merge(
mode: {times: 1},
data: {
failCommands: ['getMore'],
errorCode: error_code,
errorLabels: error_labels,
}))
end
context 'when the error is resumable' do
let(:error_code) { 10107 }
let(:error_labels) { ["ResumableChangeStreamError"] }
it_behaves_like 'returns a change document'
end
context 'when the error is Interrupted' do
let(:error_code) { 11601 }
let(:error_labels) { [] }
it_behaves_like 'raises an exception'
end
context 'when the error is CappedPositionLost' do
let(:error_code) { 136 }
let(:error_labels) { [] }
it_behaves_like 'raises an exception'
end
context 'when the error is CursorKilled' do
let(:error_code) { 237 }
let(:error_labels) { [] }
it_behaves_like 'raises an exception'
end
end
end
context 'two errors on getMore' do
min_server_fcv '4.0'
clear_fail_point_before
let(:error_code) { 10107 }
let(:error_labels) { ["ResumableChangeStreamError"] }
context 'error on first getMore' do
before do
authorized_collection.client.use(:admin).command(fail_point_base_command.merge(
mode: {times: 2},
data: {
failCommands: ['getMore'],
errorCode: error_code,
errorLabels: error_labels,
}))
end
# this retries twice because aggregation resets retry count,
# and ultimately succeeds and returns data
it_behaves_like 'returns a change document'
end
context 'error on a getMore other than first' do
before do
# Need to retrieve a change stream document successfully prior to
# failing to have the resume token, otherwise the change stream
# ignores documents inserted after the first aggregation
# and the test gets stuck
change_stream
authorized_collection.insert_one(:a => 1)
change_stream.to_enum.next
authorized_collection.insert_one(:a => 1)
authorized_collection.client.use(:admin).command(fail_point_base_command.merge(
mode: {times: 2},
data: {
failCommands: ['getMore'],
errorCode: error_code,
errorLabels: error_labels,
}))
end
# this retries twice because aggregation resets retry count,
# and ultimately succeeds and returns data
it_behaves_like 'returns a change document'
end
end
context 'two errors on getMore followed by an error on aggregation' do
min_server_fcv '4.0'
clear_fail_point_before
it 'next raises error' do
change_stream
sleep 0.5
authorized_collection.insert_one(:a => 1)
sleep 0.5
enum = change_stream.to_enum
authorized_collection.client.use(:admin).command(fail_point_base_command.merge(
:mode => {:times => 2},
:data => {:failCommands => ['getMore', 'aggregate'], errorCode: 101}))
sleep 0.5
expect do
enum.next
end.to raise_error(Mongo::Error::OperationFailure, /101\b.*Failing command (due to|via) 'failCommand' failpoint/)
end
after do
# TODO see RUBY-3135.
clear_fail_point(authorized_collection)
end
end
end
describe 'try_next' do
let(:change_stream) { authorized_collection.watch }
shared_context 'returns a change document' do
it 'returns a change document' do
change_stream
sleep 0.5
authorized_collection.insert_one(:a => 1)
sleep 0.5
change = change_stream.to_enum.try_next
expect(change).to be_a(BSON::Document)
expect(change['operationType']).to eql('insert')
doc = change['fullDocument']
expect(doc['_id']).to be_a(BSON::ObjectId)
doc.delete('_id')
expect(doc).to eql('a' => 1)
end
end
context 'there are changes' do
it_behaves_like 'returns a change document'
end
context 'there are no changes' do
it 'returns nil' do
change_stream
change = change_stream.to_enum.try_next
expect(change).to be nil
end
end
let(:error_code) { 10107 }
let(:error_labels) { ["ResumableChangeStreamError"] }
context 'one error on getMore' do
min_server_fcv '4.0'
clear_fail_point_before
context 'error on first getMore' do
before do
authorized_collection.client.use(:admin).command(fail_point_base_command.merge(
mode: {times: 1},
data: {
failCommands: ['getMore'],
errorCode: error_code,
errorLabels: error_labels,
}))
end
it_behaves_like 'returns a change document'
end
context 'error on a getMore other than first' do
before do
change_stream
authorized_collection.insert_one(:a => 1)
change_stream.to_enum.next
authorized_collection.insert_one(:a => 1)
authorized_collection.client.use(:admin).command(fail_point_base_command.merge(
mode: {times: 1},
data: {
failCommands: ['getMore'],
errorCode: error_code,
errorLabels: error_labels,
}))
end
it_behaves_like 'returns a change document'
end
end
context 'two errors on getMore' do
min_server_fcv '4.0'
clear_fail_point_before
before do
# Note: this fail point seems to be broken in 4.0 < 4.0.5
# (command to set it returns success but the fail point is not set).
# The test succeeds in this case but doesn't test two errors on
# getMore as no errors actually happen.
# 4.0.5-dev server appears to correctly set the fail point.
authorized_collection.client.use(:admin).command(fail_point_base_command.merge(
mode: {times: 2},
data: {
failCommands: ['getMore'],
errorCode: error_code,
errorLabels: error_labels,
}))
end
# this retries twice because aggregation resets retry count,
# and ultimately succeeds and returns data
it_behaves_like 'returns a change document'
end
context 'two errors on getMore followed by an error on aggregation' do
min_server_fcv '4.0'
clear_fail_point_before
context 'error on first getMore' do
it 'next raises error' do
change_stream
sleep 0.5
authorized_collection.insert_one(:a => 1)
sleep 0.5
enum = change_stream.to_enum
authorized_collection.client.use(:admin).command(fail_point_base_command.merge(
mode: {times: 3},
data: {
failCommands: ['getMore', 'aggregate'],
errorCode: error_code,
errorLabels: error_labels,
}))
sleep 0.5
expect do
enum.try_next
end.to raise_error(Mongo::Error::OperationFailure, /10107\b.*Failing command (due to|via) 'failCommand' failpoint/)
end
end
context 'error on a getMore other than first' do
it 'next raises error' do
change_stream
authorized_collection.insert_one(:a => 1)
change_stream.to_enum.next
authorized_collection.insert_one(:a => 1)
sleep 0.5
enum = change_stream.to_enum
authorized_collection.client.use(:admin).command(fail_point_base_command.merge(
mode: {times: 3},
data: {
failCommands: ['getMore', 'aggregate'],
errorCode: error_code,
errorLabels: error_labels,
}))
sleep 0.5
expect do
enum.try_next
end.to raise_error(Mongo::Error::OperationFailure, /10107\b.*Failing command (due to|via) 'failCommand' failpoint/)
end
end
end
end
describe ':start_at_operation_time option' do
min_server_fcv '4.0'
before do
authorized_collection.delete_many
end
it 'respects start time prior to beginning of aggregation' do
time = Time.now - 1
authorized_collection.insert_one(:a => 1)
sleep 0.5
cs = authorized_collection.watch([], start_at_operation_time: time)
document = cs.to_enum.next
expect(document).to be_a(BSON::Document)
end
it 'respects start time after beginning of aggregation' do
time = Time.now + 10
cs = authorized_collection.watch([], start_at_operation_time: time)
sleep 0.5
authorized_collection.insert_one(:a => 1)
sleep 0.5
document = cs.to_enum.try_next
expect(document).to be_nil
end
it 'accepts a Time' do
time = Time.now
cs = authorized_collection.watch([], start_at_operation_time: time)
end
it 'accepts a BSON::Timestamp' do
time = BSON::Timestamp.new(Time.now.to_i, 1)
cs = authorized_collection.watch([], start_at_operation_time: time)
end
it 'rejects a Date' do
time = Date.today
expect do
authorized_collection.watch([], start_at_operation_time: time)
end.to raise_error(ArgumentError, 'Time must be a Time or a BSON::Timestamp instance')
end
it 'rejects an integer' do
time = 1
expect do
authorized_collection.watch([], start_at_operation_time: time)
end.to raise_error(ArgumentError, 'Time must be a Time or a BSON::Timestamp instance')
end
end
describe ':start_after option' do
require_topology :replica_set
min_server_fcv '4.2'
let(:start_after) do
stream = authorized_collection.watch([])
authorized_collection.insert_one(x: 1)
start_after = stream.to_enum.next['_id']
end
let(:stream) do
authorized_collection.watch([], { start_after: start_after })
end
let(:events) do
start_after
subscriber = Mrss::EventSubscriber.new
authorized_client.subscribe(Mongo::Monitoring::COMMAND, subscriber)
use_stream
subscriber.started_events.select { |e| e.command_name == 'aggregate' }
end
context 'when an initial aggregation is run' do
let(:use_stream) do
stream
end
it 'sends startAfter' do
expect(events.size >= 1).to eq(true)
command = events.first.command
expect(command['pipeline'].size == 1).to eq(true)
expect(command['pipeline'].first.key?('$changeStream')).to eq(true)
expect(command['pipeline'].first['$changeStream'].key?('startAfter')).to eq(true)
end
end
context 'when resuming' do
let(:use_stream) do
stream
authorized_collection.insert_one(x: 1)
stream.to_enum.next
authorized_collection.insert_one(x: 1)
authorized_collection.client.use(:admin).command(fail_point_base_command.merge(
mode: {times: 1},
data: {
failCommands: ['getMore'],
errorCode: error_code,
errorLabels: error_labels,
}))
stream.to_enum.next
end
let(:error_code) { 10107 }
let(:error_labels) { ["ResumableChangeStreamError"] }
it 'does not startAfter even when passed in' do
expect(events.size == 2).to eq(true)
command = events.last.command
expect(command['pipeline'].size == 1).to eq(true)
expect(command['pipeline'].first.key?('$changeStream')).to eq(true)
expect(command['pipeline'].first['$changeStream'].key?('startAfter')).to eq(false)
end
end
end
describe 'resume_token' do
let(:stream) { authorized_collection.watch }
let(:events) do
subscriber = Mrss::EventSubscriber.new
authorized_client.subscribe(Mongo::Monitoring::COMMAND, subscriber)
use_stream
subscriber.succeeded_events.select { |e|
e.command_name == 'aggregate' || e.command_name === 'getMore'
}
end
let!(:sample_resume_token) do
cs = authorized_collection.watch
authorized_collection.insert_one(a: 1)
doc = cs.to_enum.next
cs.close
doc[:_id]
end
let(:use_stream) do
stream
authorized_collection.insert_one(x: 1)
stream.to_enum.next
end
context 'when batch has been emptied' do
context '4.2+' do
min_server_fcv '4.2'
it 'returns post batch resume token from current command response' do
expect(events.size).to eq(2)
aggregate_response = events.first.reply
get_more_response = events.last.reply
expect(aggregate_response['cursor'].key?('postBatchResumeToken')).to eq(true)
expect(get_more_response['cursor'].key?('postBatchResumeToken')).to eq(true)
res_tok = stream.resume_token
expect(res_tok).to eq(get_more_response['cursor']['postBatchResumeToken'])
expect(res_tok).to_not eq(aggregate_response['cursor']['postBatchResumeToken'])
end
end
context '4.0-' do
max_server_version '4.0'
it 'returns _id of previous document returned if one exists' do
doc = use_stream
expect(stream.resume_token).to eq(doc['_id'])
end
context 'when start_after is specified' do
min_server_fcv '4.2'
it 'must return startAfter from the initial aggregate if the option was specified' do
start_after = sample_resume_token
authorized_collection.insert_one(:a => 1)
stream = authorized_collection.watch([], { start_after: start_after })
expect(stream.resume_token).to eq(start_after)
end
end
it 'must return resumeAfter from the initial aggregate if the option was specified' do
resume_after = sample_resume_token
authorized_collection.insert_one(:a => 1)
stream = authorized_collection.watch([], { resume_after: resume_after })
expect(stream.resume_token).to eq(resume_after)
end
it 'must be empty if neither the startAfter nor resumeAfter options were specified' do
authorized_collection.insert_one(:a => 1)
stream = authorized_collection.watch
expect(stream.resume_token).to be(nil)
end
end
end
context 'before batch has been emptied' do
it 'returns _id of previous document returned' do
stream
authorized_collection.insert_one(:a => 1)
authorized_collection.insert_one(:a => 1)
authorized_collection.insert_one(:a => 1)
stream.to_enum.next
change = stream.to_enum.next
expect(stream.resume_token).to eq(change['_id'])
end
end
# Note that the watch method executes the initial aggregate command
context 'for non-empty, non-iterated batch, only the initial aggregate command executed' do
let (:use_stream) do
authorized_collection.insert_one(:a => 1)
stream
end
context 'if startAfter was specified' do
min_server_fcv '4.2'
let (:stream) do
authorized_collection.watch([], { start_after: sample_resume_token })
end
it 'must return startAfter from the initial aggregate' do
# Need to sample a doc id from the stream before we use the stream, so
# the events subscriber does not record these commands as part of the example.
sample_resume_token
# Verify that only the initial aggregate command was executed
expect(events.size).to eq(1)
expect(events.first.command_name).to eq('aggregate')
expect(stream.resume_token).to eq(sample_resume_token)
end
end
context 'if resumeAfter was specified' do
let (:stream) do
authorized_collection.watch([], { resume_after: sample_resume_token })
end
it 'must return resumeAfter from the initial aggregate' do
sample_resume_token
expect(events.size).to eq(1)
expect(events.first.command_name).to eq('aggregate')
expect(stream.resume_token).to eq(sample_resume_token)
end
end
context 'if neither the startAfter nor resumeAfter options were specified' do
it 'must be empty' do
expect(events.size).to eq(1)
expect(events.first.command_name).to eq('aggregate')
expect(stream.resume_token).to be(nil)
end
end
end
context 'for non-empty, non-iterated batch directly after get_more' do
let(:next_doc) do
authorized_collection.insert_one(:a => 1)
stream.to_enum.next
end
let(:do_get_more) do
authorized_collection.insert_one(:a => 1)
stream.instance_variable_get('@cursor').get_more
end
context '4.2+' do
min_server_fcv '4.2'
let(:use_stream) do
stream
next_doc
do_get_more
end
it 'returns post batch resume token from previous command response' do
expect(events.size).to eq(3)
expect(events.last.command_name).to eq('getMore')
first_get_more = events[1].reply
second_get_more = events[2].reply
expect(first_get_more['cursor'].key?('postBatchResumeToken')).to eq(true)
expect(second_get_more['cursor'].key?('postBatchResumeToken')).to eq(true)
res_tok = stream.resume_token
expect(res_tok).to eq(first_get_more['cursor']['postBatchResumeToken'])
expect(res_tok).not_to eq(second_get_more['cursor']['postBatchResumeToken'])
end
end
context '4.0-' do
max_server_version '4.0'
context 'if a document was returned' do
let(:use_stream) do
stream
next_doc
do_get_more
end
it 'returns _id of previous document' do
expect(events.last.command_name).to eq('getMore')
expect(stream.resume_token).to eq(next_doc['_id'])
end
end
context 'if a document was not returned' do
let(:use_stream) do
stream
do_get_more
end
context 'when resumeAfter is specified' do
let (:stream) do
authorized_collection.watch([], { resume_after: sample_resume_token })
end
it 'must return resumeAfter from the initial aggregate if the option was specified' do
sample_resume_token
expect(events.last.command_name).to eq('getMore')
expect(stream.resume_token).to eq(sample_resume_token)
end
end
context 'if neither the startAfter nor resumeAfter options were specified' do
it 'must be empty' do
expect(events.last.command_name).to eq('getMore')
expect(stream.resume_token).to be(nil)
end
end
end
end
end
end
end
|