summaryrefslogtreecommitdiff
path: root/jstests/replsets/transactions_on_secondaries_not_allowed.js
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 /jstests/replsets/transactions_on_secondaries_not_allowed.js
parentafd233fca7755f8bba574916ff049c24bfcf2052 (diff)
downloadmongo-d989f45b6f9a802e3d4fe5c4cb34b5ddc28ed4eb.tar.gz
SERVER-39139 Disallow starting transactions on secondaries
This reverts commit a74b2f39025fee2c59aa5437deea6e06f05e18ca.
Diffstat (limited to 'jstests/replsets/transactions_on_secondaries_not_allowed.js')
-rw-r--r--jstests/replsets/transactions_on_secondaries_not_allowed.js65
1 files changed, 65 insertions, 0 deletions
diff --git a/jstests/replsets/transactions_on_secondaries_not_allowed.js b/jstests/replsets/transactions_on_secondaries_not_allowed.js
new file mode 100644
index 00000000000..d857cef6717
--- /dev/null
+++ b/jstests/replsets/transactions_on_secondaries_not_allowed.js
@@ -0,0 +1,65 @@
+/**
+ * Test that starting transactions and running commitTransaction and abortTransaction commands are
+ * not allowed on replica set secondaries.
+ *
+ * @tags: [uses_transactions]
+ */
+(function() {
+ "use strict";
+
+ const dbName = "test";
+ const collName = "transactions_on_secondaries_not_allowed";
+
+ const rst = new ReplSetTest({name: collName, nodes: 2});
+ rst.startSet();
+ // We want a stable topology, so make the secondary unelectable.
+ let config = rst.getReplSetConfig();
+ config.members[1].priority = 0;
+ rst.initiate(config);
+
+ const primary = rst.getPrimary();
+ const secondary = rst.getSecondary();
+ const secondaryTestDB = secondary.getDB(dbName);
+
+ // Do an initial write so we have something to find.
+ const initialDoc = {_id: 0};
+ assert.commandWorked(primary.getDB(dbName)[collName].insert(initialDoc));
+ rst.awaitLastOpCommitted();
+
+ // Initiate a session on the secondary.
+ const sessionOptions = {causalConsistency: false};
+ const session = secondaryTestDB.getMongo().startSession(sessionOptions);
+ const sessionDb = session.getDatabase(dbName);
+
+ /**
+ * Test starting a transaction and issuing a commitTransaction command.
+ */
+
+ jsTestLog("Start a read-only transaction on the secondary.");
+ session.startTransaction({readConcern: {level: "snapshot"}});
+
+ // Try to read a document (the first statement in the transaction) and verify that this fails.
+ assert.commandFailedWithCode(sessionDb.runCommand({find: collName}), ErrorCodes.NotMaster);
+
+ // The check for "NotMaster" supercedes the check for "NoSuchTransaction" in this case.
+ jsTestLog(
+ "Make sure we are not allowed to run the commitTransaction command on the secondary.");
+ assert.commandFailedWithCode(session.commitTransaction_forTesting(), ErrorCodes.NotMaster);
+
+ /**
+ * Test starting a transaction and issuing an abortTransaction command.
+ */
+
+ jsTestLog("Start a different read-only transaction on the secondary.");
+ session.startTransaction({readConcern: {level: "snapshot"}});
+
+ // Try to read a document (the first statement in the transaction) and verify that this fails.
+ assert.commandFailedWithCode(sessionDb.runCommand({find: collName}), ErrorCodes.NotMaster);
+
+ // The check for "NotMaster" supercedes the check for "NoSuchTransaction" in this case.
+ jsTestLog("Make sure we are not allowed to run the abortTransaction command on the secondary.");
+ assert.commandFailedWithCode(session.abortTransaction_forTesting(), ErrorCodes.NotMaster);
+
+ session.endSession();
+ rst.stopSet(undefined, false, {skipValidation: true});
+}());