From 73b75eee65455053e5662ce528c1a1e5a119c147 Mon Sep 17 00:00:00 2001 From: Benety Goh Date: Mon, 28 Nov 2022 11:13:21 -0500 Subject: SERVER-71488 TransactionOperations::getApplyOpsInfo() returns prepared transaction info in result --- src/mongo/db/repl/oplog_applier_impl_test_fixture.cpp | 2 +- src/mongo/db/repl/replication_recovery_test.cpp | 2 +- .../resharding_oplog_session_application_test.cpp | 2 +- src/mongo/db/transaction/transaction_operations.cpp | 5 +++-- src/mongo/db/transaction/transaction_operations.h | 14 ++++++++++++++ src/mongo/db/transaction/transaction_operations_test.cpp | 8 ++++++++ src/mongo/db/transaction/transaction_participant_test.cpp | 3 +-- 7 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/mongo/db/repl/oplog_applier_impl_test_fixture.cpp b/src/mongo/db/repl/oplog_applier_impl_test_fixture.cpp index 348e31c767a..1b33b968662 100644 --- a/src/mongo/db/repl/oplog_applier_impl_test_fixture.cpp +++ b/src/mongo/db/repl/oplog_applier_impl_test_fixture.cpp @@ -153,7 +153,7 @@ OplogApplierImplOpObserver::preTransactionPrepare(OperationContext* opCtx, const std::vector& reservedSlots, Date_t wallClockTime, TransactionOperations* transactionOperations) { - return std::make_unique(); + return std::make_unique(/*prepare=*/true); } void OplogApplierImplTest::setUp() { diff --git a/src/mongo/db/repl/replication_recovery_test.cpp b/src/mongo/db/repl/replication_recovery_test.cpp index 8bbbf00ef18..1e9ea0c24a2 100644 --- a/src/mongo/db/repl/replication_recovery_test.cpp +++ b/src/mongo/db/repl/replication_recovery_test.cpp @@ -144,7 +144,7 @@ public: const std::vector& reservedSlots, Date_t wallClockTime, TransactionOperations* transactionOperations) override { - return std::make_unique(); + return std::make_unique(/*prepare=*/false); } const repl::OpTime dropOpTime = {Timestamp(Seconds(100), 1U), 1LL}; diff --git a/src/mongo/db/s/resharding/resharding_oplog_session_application_test.cpp b/src/mongo/db/s/resharding/resharding_oplog_session_application_test.cpp index 08d3a1b94c8..917c489caa6 100644 --- a/src/mongo/db/s/resharding/resharding_oplog_session_application_test.cpp +++ b/src/mongo/db/s/resharding/resharding_oplog_session_application_test.cpp @@ -71,7 +71,7 @@ public: const std::vector& reservedSlots, Date_t wallClockTime, TransactionOperations* transactionOperations) override { - return std::make_unique(); + return std::make_unique(/*prepare=*/false); } }; diff --git a/src/mongo/db/transaction/transaction_operations.cpp b/src/mongo/db/transaction/transaction_operations.cpp index 39270f36c47..30ac357ce29 100644 --- a/src/mongo/db/transaction/transaction_operations.cpp +++ b/src/mongo/db/transaction/transaction_operations.cpp @@ -215,7 +215,7 @@ TransactionOperations::ApplyOpsInfo TransactionOperations::getApplyOpsInfo( bool prepare) const { const auto& operations = _transactionOperations; if (operations.empty()) { - return {{}, /*numberOfOplogSlotsUsed=*/0}; + return {/*applyOpsEntries=*/{}, /*numberOfOplogSlotsUsed=*/0, prepare}; } tassert(6278504, "Insufficient number of oplogSlots", operations.size() <= oplogSlots.size()); @@ -251,7 +251,8 @@ TransactionOperations::ApplyOpsInfo TransactionOperations::getApplyOpsInfo( applyOpsEntries.back().oplogSlot = oplogSlots.back(); } return {std::move(applyOpsEntries), - static_cast(oplogSlotIter - oplogSlots.begin())}; + /*numberOfOplogSlotsUsed=*/static_cast(oplogSlotIter - oplogSlots.begin()), + prepare}; } std::vector* diff --git a/src/mongo/db/transaction/transaction_operations.h b/src/mongo/db/transaction/transaction_operations.h index 4cfa0ffd55a..3522e72fc0c 100644 --- a/src/mongo/db/transaction/transaction_operations.h +++ b/src/mongo/db/transaction/transaction_operations.h @@ -67,11 +67,25 @@ public: std::vector operations; }; + ApplyOpsInfo(std::vector applyOpsEntries, + std::size_t numberOfOplogSlotsUsed, + bool prepare) + : applyOpsEntries(std::move(applyOpsEntries)), + numberOfOplogSlotsUsed(numberOfOplogSlotsUsed), + prepare(prepare) {} + + explicit ApplyOpsInfo(bool prepare) + : applyOpsEntries(), numberOfOplogSlotsUsed(0), prepare(prepare) {} + // Representation of "applyOps" oplog entries. std::vector applyOpsEntries; // Number of oplog slots utilized. std::size_t numberOfOplogSlotsUsed; + + // Indicates if we are generating "applyOps" oplog entries for a prepared transaction. + // This is derived from the 'prepared' parameter passed to the getApplyOpsInfo() function. + bool prepare; }; /** diff --git a/src/mongo/db/transaction/transaction_operations_test.cpp b/src/mongo/db/transaction/transaction_operations_test.cpp index ce75a153f2f..792aeb70527 100644 --- a/src/mongo/db/transaction/transaction_operations_test.cpp +++ b/src/mongo/db/transaction/transaction_operations_test.cpp @@ -216,6 +216,7 @@ TEST(TransactionOperationsTest, GetApplyOpsInfoEmptyOps) { /*prepare=*/false); ASSERT_EQ(info.applyOpsEntries.size(), 0); ASSERT_EQ(info.numberOfOplogSlotsUsed, 0); + ASSERT_FALSE(info.prepare); } DEATH_TEST(TransactionOperationsTest, @@ -258,6 +259,8 @@ TEST(TransactionOperationsTest, GetApplyOpsInfoReturnsOneEntryContainingTwoOpera ASSERT_EQ(info.numberOfOplogSlotsUsed, 1U); ASSERT_EQ(info.applyOpsEntries.size(), 1U); + ASSERT_FALSE(info.prepare); + ASSERT_EQ(info.applyOpsEntries[0].oplogSlot, oplogSlots[0]); // first oplog slot ASSERT_EQ(info.applyOpsEntries[0].operations.size(), 2U); ASSERT_BSONOBJ_EQ(info.applyOpsEntries[0].operations[0], op1.toBSON()); @@ -294,6 +297,7 @@ TEST(TransactionOperationsTest, GetApplyOpsInfoRespectsOperationCountLimit) { ASSERT_EQ(info.numberOfOplogSlotsUsed, 2U); ASSERT_EQ(info.applyOpsEntries.size(), 2U); + ASSERT_FALSE(info.prepare); // Check first applyOps entry. ASSERT_EQ(info.applyOpsEntries[0].oplogSlot, oplogSlots[0]); @@ -337,6 +341,7 @@ TEST(TransactionOperationsTest, GetApplyOpsInfoRespectsOperationSizeLimit) { ASSERT_EQ(info.numberOfOplogSlotsUsed, 2U); ASSERT_EQ(info.applyOpsEntries.size(), 2U); + ASSERT_FALSE(info.prepare); // Check first applyOps entry. ASSERT_EQ(info.applyOpsEntries[0].oplogSlot, oplogSlots[0]); @@ -399,6 +404,8 @@ TEST(TransactionOperationsTest, GetApplyOpsInfoAssignsPreImageSlotBeforeOperatio ASSERT_EQ(info.numberOfOplogSlotsUsed, 2U); ASSERT_EQ(info.applyOpsEntries.size(), 1U); + ASSERT_FALSE(info.prepare); + ASSERT_EQ(info.applyOpsEntries[0].oplogSlot, oplogSlots[1]); ASSERT_EQ(info.applyOpsEntries[0].operations.size(), 1U); ASSERT_BSONOBJ_EQ(info.applyOpsEntries[0].operations[0], op.toBSON()); @@ -429,6 +436,7 @@ TEST(TransactionOperationsTest, GetApplyOpsInfoAssignsLastOplogSlotForPrepare) { ASSERT_EQ(info.applyOpsEntries[0].oplogSlot, oplogSlots[1]); // last oplog slot ASSERT_EQ(info.applyOpsEntries[0].operations.size(), 1U); ASSERT_BSONOBJ_EQ(info.applyOpsEntries[0].operations[0], op.toBSON()); + ASSERT(info.prepare); } } // namespace diff --git a/src/mongo/db/transaction/transaction_participant_test.cpp b/src/mongo/db/transaction/transaction_participant_test.cpp index 8681ba784d2..15a2f2e3af6 100644 --- a/src/mongo/db/transaction/transaction_participant_test.cpp +++ b/src/mongo/db/transaction/transaction_participant_test.cpp @@ -157,8 +157,7 @@ OpObserverMock::preTransactionPrepare(OperationContext* opCtx, const std::vector& reservedSlots, Date_t wallClockTime, TransactionOperations* transactionOperations) { - return std::make_unique( - OpObserver::ApplyOpsOplogSlotAndOperationAssignment{{}, {}}); + return std::make_unique(/*prepare=*/true); } void OpObserverMock::onTransactionPrepare( -- cgit v1.2.1