|
17 | 17 |
|
18 | 18 | #include "evp_store_single_threaded_test.h" |
19 | 19 |
|
| 20 | +#include "../mock/mock_dcp.h" |
20 | 21 | #include "../mock/mock_dcp_producer.h" |
21 | 22 | #include "../mock/mock_dcp_consumer.h" |
22 | 23 | #include "../mock/mock_stream.h" |
| 24 | + |
| 25 | +#include "failover-table.h" |
23 | 26 | #include "programs/engine_testapp/mock_server.h" |
24 | 27 |
|
| 28 | +void SingleThreadedEPStoreTest::notifyAndStepToCheckpoint( |
| 29 | + MockDcpProducer& producer, dcp_message_producers* producers) { |
| 30 | + |
| 31 | + auto vb = store->getVBucket(vbid); |
| 32 | + ASSERT_NE(nullptr, vb.get()); |
| 33 | + |
| 34 | + producer.notifySeqnoAvailable(vbid, vb->getHighSeqno()); |
| 35 | + |
| 36 | + /* Step which will notify the checkpoint processor task */ |
| 37 | + EXPECT_EQ(ENGINE_SUCCESS, producer.step(producers)); |
| 38 | + EXPECT_EQ(1, producer.getCheckpointSnapshotTask().queueSize()); |
| 39 | + |
| 40 | + /* Run the task */ |
| 41 | + producer.getCheckpointSnapshotTask().run(); |
| 42 | + |
| 43 | + /* This time the step should return something that is read from the |
| 44 | + checkpoint processor */ |
| 45 | + EXPECT_EQ(ENGINE_WANT_MORE, producer.step(producers)); |
| 46 | +} |
| 47 | + |
25 | 48 | /* |
26 | 49 | * The following test checks to see if we call handleSlowStream when in a |
27 | 50 | * backfilling state, but the backfillTask is not running, we |
@@ -674,3 +697,194 @@ TEST_F(SingleThreadedEPStoreTest, MB18953_taskWake) { |
674 | 697 | EXPECT_EQ(1, task_executor->getNumReadyTasks(NONIO_TASK_IDX)); |
675 | 698 | runNextTask(lpNonioQ, "TestTask DefragmenterTask"); // lptask goes second |
676 | 699 | } |
| 700 | + |
| 701 | +extern uint8_t dcp_last_op; |
| 702 | +extern std::string dcp_last_key; |
| 703 | + |
| 704 | +class MB_29287 : public SingleThreadedEPStoreTest { |
| 705 | +public: |
| 706 | + void SetUp() override { |
| 707 | + SingleThreadedEPStoreTest::SetUp(); |
| 708 | + cookie = create_mock_cookie(); |
| 709 | + setVBucketStateAndRunPersistTask(vbid, vbucket_state_active); |
| 710 | + // 1. Mock producer |
| 711 | + producer = new MockDcpProducer(*engine, |
| 712 | + cookie, |
| 713 | + "test_producer", |
| 714 | + /*notifyOnly*/ false); |
| 715 | + |
| 716 | + producers = get_dcp_producers( |
| 717 | + reinterpret_cast<ENGINE_HANDLE*>(engine.get()), |
| 718 | + reinterpret_cast<ENGINE_HANDLE_V1*>(engine.get())); |
| 719 | + |
| 720 | + // 2. Mock active stream |
| 721 | + producer->mockActiveStreamRequest(0, // flags |
| 722 | + 1, // opaque |
| 723 | + vbid, |
| 724 | + 0, // start_seqno |
| 725 | + ~0, // end_seqno |
| 726 | + 0, // vbucket_uuid, |
| 727 | + 0, // snap_start_seqno, |
| 728 | + 0); // snap_end_seqno, |
| 729 | + |
| 730 | + store_item(vbid, "1", "value1"); |
| 731 | + store_item(vbid, "2", "value2"); |
| 732 | + store_item(vbid, "3", "value3"); |
| 733 | + flush_vbucket_to_disk(vbid, 3); |
| 734 | + notifyAndStepToCheckpoint(*producer, producers.get()); |
| 735 | + |
| 736 | + for (int i = 0; i < 3; i++) { // 1, 2 and 3 |
| 737 | + EXPECT_EQ(ENGINE_WANT_MORE, producer->step(producers.get())); |
| 738 | + EXPECT_EQ(PROTOCOL_BINARY_CMD_DCP_MUTATION, dcp_last_op); |
| 739 | + } |
| 740 | + |
| 741 | + store_item(vbid, "4", "value4"); |
| 742 | + |
| 743 | + auto stream = producer->findStream(vbid); |
| 744 | + auto* mockStream = static_cast<MockActiveStream*>(stream.get()); |
| 745 | + mockStream->preGetOutstandingItemsCallback = |
| 746 | + std::bind(&MB_29287::closeAndRecreateStream, this); |
| 747 | + |
| 748 | + // call next - get success (nothing ready, but task has been scheduled) |
| 749 | + EXPECT_EQ(ENGINE_SUCCESS, producer->step(producers.get())); |
| 750 | + |
| 751 | + // Run the snapshot task and step (triggering |
| 752 | + // preGetOutstandingItemsCallback) |
| 753 | + notifyAndStepToCheckpoint(*producer, producers.get()); |
| 754 | + } |
| 755 | + |
| 756 | + void TearDown() override { |
| 757 | + destroy_mock_cookie(cookie); |
| 758 | + producer->closeAllStreams(); |
| 759 | + producer->cancelCheckpointCreatorTask(); |
| 760 | + producer.reset(); |
| 761 | + SingleThreadedEPStoreTest::TearDown(); |
| 762 | + } |
| 763 | + |
| 764 | + void closeAndRecreateStream() { |
| 765 | + // Without the fix, 5 will be lost |
| 766 | + store_item(vbid, "5", "don't lose me"); |
| 767 | + producer->closeStream(1, 0); |
| 768 | + auto vb = store->getVBuckets().getBucket(vbid); |
| 769 | + ASSERT_NE(nullptr, vb.get()); |
| 770 | + producer->mockActiveStreamRequest(DCP_ADD_STREAM_FLAG_TAKEOVER, |
| 771 | + 1, // opaque |
| 772 | + vbid, |
| 773 | + 3, // start_seqno |
| 774 | + ~0, // end_seqno |
| 775 | + vb->failovers->getLatestUUID(), |
| 776 | + 3, // snap_start_seqno |
| 777 | + ~0); // snap_end_seqno |
| 778 | + } |
| 779 | + |
| 780 | + const void* cookie = nullptr; |
| 781 | + mock_dcp_producer_t producer; |
| 782 | + std::unique_ptr<dcp_message_producers> producers; |
| 783 | +}; |
| 784 | + |
| 785 | +// Stream takeover with no more writes |
| 786 | +TEST_F(MB_29287, dataloss_end) { |
| 787 | + auto stream = producer->findStream(vbid); |
| 788 | + auto* as = static_cast<ActiveStream*>(stream.get()); |
| 789 | + |
| 790 | + EXPECT_EQ(STREAM_TAKEOVER_SEND, stream->getState()); |
| 791 | + EXPECT_EQ(ENGINE_WANT_MORE, producer->step(producers.get())); |
| 792 | + EXPECT_EQ(PROTOCOL_BINARY_CMD_DCP_MUTATION, dcp_last_op); |
| 793 | + dcp_last_op = 0; |
| 794 | + EXPECT_EQ("4", dcp_last_key); |
| 795 | + |
| 796 | + EXPECT_EQ(ENGINE_WANT_MORE, producer->step(producers.get())); |
| 797 | + EXPECT_EQ(PROTOCOL_BINARY_CMD_DCP_MUTATION, dcp_last_op); |
| 798 | + dcp_last_op = 0; |
| 799 | + EXPECT_EQ("5", dcp_last_key); |
| 800 | + |
| 801 | + // Snapshot received |
| 802 | + as->snapshotMarkerAckReceived(); |
| 803 | + |
| 804 | + // set-vb-state now underway |
| 805 | + EXPECT_EQ(ENGINE_WANT_MORE, producer->step(producers.get())); |
| 806 | + EXPECT_EQ(PROTOCOL_BINARY_CMD_DCP_SET_VBUCKET_STATE, dcp_last_op); |
| 807 | + |
| 808 | + // Move stream to pending and vb to dead |
| 809 | + as->setVBucketStateAckRecieved(); |
| 810 | + |
| 811 | + // Cannot store anymore items |
| 812 | + store_item(vbid, |
| 813 | + "K6", |
| 814 | + "value6", |
| 815 | + ENGINE_NOT_MY_VBUCKET); |
| 816 | + |
| 817 | + EXPECT_EQ(ENGINE_WANT_MORE, producer->step(producers.get())); |
| 818 | + EXPECT_EQ(PROTOCOL_BINARY_CMD_DCP_SET_VBUCKET_STATE, dcp_last_op); |
| 819 | + as->setVBucketStateAckRecieved(); |
| 820 | + EXPECT_TRUE(!stream->isActive()); |
| 821 | + |
| 822 | + auto vb = store->getVBuckets().getBucket(vbid); |
| 823 | + ASSERT_NE(nullptr, vb.get()); |
| 824 | + // Have persistence cursor only (dcp now closed down) |
| 825 | + EXPECT_EQ(1, vb->checkpointManager.getNumOfCursors()); |
| 826 | +} |
| 827 | + |
| 828 | +// takeover when more writes occur |
| 829 | +TEST_F(MB_29287, dataloss_hole) { |
| 830 | + auto stream = producer->findStream(vbid); |
| 831 | + auto* as = static_cast<ActiveStream*>(stream.get()); |
| 832 | + |
| 833 | + store_item(vbid, "6", "value6"); |
| 834 | + |
| 835 | + EXPECT_EQ(STREAM_TAKEOVER_SEND, stream->getState()); |
| 836 | + EXPECT_EQ(ENGINE_WANT_MORE, producer->step(producers.get())); |
| 837 | + EXPECT_EQ(PROTOCOL_BINARY_CMD_DCP_MUTATION, dcp_last_op); |
| 838 | + dcp_last_op = 0; |
| 839 | + EXPECT_EQ("4", dcp_last_key); |
| 840 | + |
| 841 | + EXPECT_EQ(ENGINE_WANT_MORE, producer->step(producers.get())); |
| 842 | + EXPECT_EQ(PROTOCOL_BINARY_CMD_DCP_MUTATION, dcp_last_op); |
| 843 | + dcp_last_op = 0; |
| 844 | + EXPECT_EQ("5", dcp_last_key); |
| 845 | + |
| 846 | + // Snapshot received |
| 847 | + as->snapshotMarkerAckReceived(); |
| 848 | + |
| 849 | + // More data in the checkpoint (key 6) |
| 850 | + |
| 851 | + // call next - get success (nothing ready, but task has been scheduled) |
| 852 | + EXPECT_EQ(ENGINE_SUCCESS, producer->step(producers.get())); |
| 853 | + |
| 854 | + // Run the snapshot task and step |
| 855 | + notifyAndStepToCheckpoint(*producer, producers.get()); |
| 856 | + |
| 857 | + EXPECT_EQ(ENGINE_WANT_MORE, producer->step(producers.get())); |
| 858 | + EXPECT_EQ(PROTOCOL_BINARY_CMD_DCP_MUTATION, dcp_last_op); |
| 859 | + EXPECT_EQ("6", dcp_last_key); |
| 860 | + |
| 861 | + // Snapshot received |
| 862 | + as->snapshotMarkerAckReceived(); |
| 863 | + |
| 864 | + // Now send |
| 865 | + EXPECT_EQ(STREAM_TAKEOVER_SEND, stream->getState()); |
| 866 | + |
| 867 | + // set-vb-state now underway |
| 868 | + EXPECT_EQ(ENGINE_WANT_MORE, producer->step(producers.get())); |
| 869 | + EXPECT_EQ(PROTOCOL_BINARY_CMD_DCP_SET_VBUCKET_STATE, dcp_last_op); |
| 870 | + dcp_last_op = 0; |
| 871 | + |
| 872 | + // Move stream to pending and vb to dead |
| 873 | + as->setVBucketStateAckRecieved(); |
| 874 | + |
| 875 | + // Cannot store anymore items |
| 876 | + store_item(vbid, |
| 877 | + "K6", |
| 878 | + "value6", |
| 879 | + ENGINE_NOT_MY_VBUCKET); |
| 880 | + |
| 881 | + EXPECT_EQ(ENGINE_WANT_MORE, producer->step(producers.get())); |
| 882 | + EXPECT_EQ(PROTOCOL_BINARY_CMD_DCP_SET_VBUCKET_STATE, dcp_last_op); |
| 883 | + as->setVBucketStateAckRecieved(); |
| 884 | + EXPECT_TRUE(!stream->isActive()); |
| 885 | + |
| 886 | + auto vb = store->getVBuckets().getBucket(vbid); |
| 887 | + ASSERT_NE(nullptr, vb.get()); |
| 888 | + // Have persistence cursor only (dcp now closed down) |
| 889 | + EXPECT_EQ(1, vb->checkpointManager.getNumOfCursors()); |
| 890 | +} |
0 commit comments