summaryrefslogtreecommitdiff
path: root/src/mongo/db/transaction_participant_test.cpp
diff options
context:
space:
mode:
authorVesselina Ratcheva <vesselina.ratcheva@10gen.com>2019-03-05 16:40:30 -0500
committerVesselina Ratcheva <vesselina.ratcheva@10gen.com>2019-03-06 18:19:06 -0500
commitd989f45b6f9a802e3d4fe5c4cb34b5ddc28ed4eb (patch)
tree69efce41c2974b113e8d242ac8a8e5441a5ad4fa /src/mongo/db/transaction_participant_test.cpp
parentafd233fca7755f8bba574916ff049c24bfcf2052 (diff)
downloadmongo-d989f45b6f9a802e3d4fe5c4cb34b5ddc28ed4eb.tar.gz
SERVER-39139 Disallow starting transactions on secondaries
This reverts commit a74b2f39025fee2c59aa5437deea6e06f05e18ca.
Diffstat (limited to 'src/mongo/db/transaction_participant_test.cpp')
-rw-r--r--src/mongo/db/transaction_participant_test.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/mongo/db/transaction_participant_test.cpp b/src/mongo/db/transaction_participant_test.cpp
index 125454f99c0..d364f0020b0 100644
--- a/src/mongo/db/transaction_participant_test.cpp
+++ b/src/mongo/db/transaction_participant_test.cpp
@@ -884,6 +884,51 @@ TEST_F(TxnParticipantTest, CannotAbortArbitraryPreparedTransactions) {
ASSERT(_opObserver->transactionPrepared);
}
+TEST_F(TxnParticipantTest, CannotStartNewTransactionIfNotPrimary) {
+ ASSERT_OK(repl::ReplicationCoordinator::get(opCtx())->setFollowerMode(
+ repl::MemberState::RS_SECONDARY));
+
+ auto opCtxSession = std::make_unique<MongoDOperationContextSession>(opCtx());
+ auto txnParticipant = TransactionParticipant::get(opCtx());
+
+ // Include 'autocommit=false' for transactions.
+ ASSERT_THROWS_CODE(
+ txnParticipant.beginOrContinue(opCtx(), *opCtx()->getTxnNumber(), false, true),
+ AssertionException,
+ ErrorCodes::NotMaster);
+}
+
+TEST_F(TxnParticipantTest, CannotStartRetryableWriteIfNotPrimary) {
+ ASSERT_OK(repl::ReplicationCoordinator::get(opCtx())->setFollowerMode(
+ repl::MemberState::RS_SECONDARY));
+
+ auto opCtxSession = std::make_unique<MongoDOperationContextSession>(opCtx());
+ auto txnParticipant = TransactionParticipant::get(opCtx());
+
+ // Omit the 'autocommit' field for retryable writes.
+ ASSERT_THROWS_CODE(
+ txnParticipant.beginOrContinue(opCtx(), *opCtx()->getTxnNumber(), boost::none, true),
+ AssertionException,
+ ErrorCodes::NotMaster);
+}
+
+TEST_F(TxnParticipantTest, CannotContinueTransactionIfNotPrimary) {
+ // Will start the transaction.
+ auto sessionCheckout = checkOutSession();
+ auto txnParticipant = TransactionParticipant::get(opCtx());
+ ASSERT_TRUE(txnParticipant.inMultiDocumentTransaction());
+
+ ASSERT_OK(repl::ReplicationCoordinator::get(opCtx())->setFollowerMode(
+ repl::MemberState::RS_SECONDARY));
+
+ // Technically, the transaction should have been aborted on stepdown anyway, but it
+ // doesn't hurt to have this kind of coverage.
+ ASSERT_THROWS_CODE(
+ txnParticipant.beginOrContinue(opCtx(), *opCtx()->getTxnNumber(), false, false),
+ AssertionException,
+ ErrorCodes::NotMaster);
+}
+
TEST_F(TxnParticipantTest, CannotStartNewTransactionWhilePreparedTransactionInProgress) {
auto sessionCheckout = checkOutSession();
auto txnParticipant = TransactionParticipant::get(opCtx());