diff options
Diffstat (limited to 'jstests/replsets/speculative_transaction.js')
-rw-r--r-- | jstests/replsets/speculative_transaction.js | 162 |
1 files changed, 81 insertions, 81 deletions
diff --git a/jstests/replsets/speculative_transaction.js b/jstests/replsets/speculative_transaction.js index 565b41c8300..e138612dcd1 100644 --- a/jstests/replsets/speculative_transaction.js +++ b/jstests/replsets/speculative_transaction.js @@ -6,120 +6,120 @@ * @tags: [uses_transactions, requires_majority_read_concern] */ (function() { - "use strict"; - load("jstests/libs/write_concern_util.js"); // For stopServerReplication +"use strict"; +load("jstests/libs/write_concern_util.js"); // For stopServerReplication - const dbName = "test"; - const collName = "speculative_transaction"; +const dbName = "test"; +const collName = "speculative_transaction"; - const rst = new ReplSetTest({name: collName, nodes: [{}, {rsConfig: {priority: 0}}]}); - rst.startSet(); - rst.initiate(); +const rst = new ReplSetTest({name: collName, nodes: [{}, {rsConfig: {priority: 0}}]}); +rst.startSet(); +rst.initiate(); - const primary = rst.getPrimary(); - const secondary = rst.getSecondary(); - var testDB = primary.getDB(dbName); - const coll = testDB[collName]; +const primary = rst.getPrimary(); +const secondary = rst.getSecondary(); +var testDB = primary.getDB(dbName); +const coll = testDB[collName]; - function runTest(sessionOptions) { - testDB.runCommand({drop: collName, writeConcern: {w: "majority"}}); +function runTest(sessionOptions) { + testDB.runCommand({drop: collName, writeConcern: {w: "majority"}}); - // Do an initial write so we have something to update. - assert.commandWorked(coll.insert([{_id: 0}, {_id: 1}], {w: "majority"})); - rst.awaitLastOpCommitted(); + // Do an initial write so we have something to update. + assert.commandWorked(coll.insert([{_id: 0}, {_id: 1}], {w: "majority"})); + rst.awaitLastOpCommitted(); - // Stop replication on the secondary so the majority commit never moves forward. - stopServerReplication(secondary); + // Stop replication on the secondary so the majority commit never moves forward. + stopServerReplication(secondary); - // Initiate a session on the primary. - const session = testDB.getMongo().startSession(sessionOptions); - const sessionDb = session.getDatabase(dbName); - const sessionColl = sessionDb.getCollection(collName); + // Initiate a session on the primary. + const session = testDB.getMongo().startSession(sessionOptions); + const sessionDb = session.getDatabase(dbName); + const sessionColl = sessionDb.getCollection(collName); - // Start the first transaction. Do not use majority commit for this one. - jsTestLog("Starting first transaction"); - session.startTransaction({readConcern: {level: "snapshot"}, writeConcern: {w: 1}}); + // Start the first transaction. Do not use majority commit for this one. + jsTestLog("Starting first transaction"); + session.startTransaction({readConcern: {level: "snapshot"}, writeConcern: {w: 1}}); - assert.commandWorked(sessionColl.update({_id: 0}, {$set: {x: 1}})); + assert.commandWorked(sessionColl.update({_id: 0}, {$set: {x: 1}})); - assert.commandWorked(session.commitTransaction_forTesting()); + assert.commandWorked(session.commitTransaction_forTesting()); - // The document should be updated on the local snapshot. - assert.eq(coll.findOne({_id: 0}), {_id: 0, x: 1}); + // The document should be updated on the local snapshot. + assert.eq(coll.findOne({_id: 0}), {_id: 0, x: 1}); - // The document should not be updated in the majority snapshot. - assert.eq(coll.find({_id: 0}).readConcern("majority").next(), {_id: 0}); + // The document should not be updated in the majority snapshot. + assert.eq(coll.find({_id: 0}).readConcern("majority").next(), {_id: 0}); - jsTestLog("Starting second transaction"); - // Start a second transaction. Still do not use majority commit for this one. - session.startTransaction({readConcern: {level: "snapshot"}, writeConcern: {w: 1}}); + jsTestLog("Starting second transaction"); + // Start a second transaction. Still do not use majority commit for this one. + session.startTransaction({readConcern: {level: "snapshot"}, writeConcern: {w: 1}}); - // We should see the updated doc within the transaction as a result of speculative read - // concern. - assert.eq(sessionColl.findOne({_id: 0}), {_id: 0, x: 1}); + // We should see the updated doc within the transaction as a result of speculative read + // concern. + assert.eq(sessionColl.findOne({_id: 0}), {_id: 0, x: 1}); - // Update it again. - assert.commandWorked(sessionColl.update({_id: 0}, {$inc: {x: 1}})); + // Update it again. + assert.commandWorked(sessionColl.update({_id: 0}, {$inc: {x: 1}})); - // Update a different document outside the transaction. - assert.commandWorked(coll.update({_id: 1}, {$set: {y: 1}})); + // Update a different document outside the transaction. + assert.commandWorked(coll.update({_id: 1}, {$set: {y: 1}})); - // Within the transaction, we should not see the out-of-transaction update. - assert.eq(sessionColl.findOne({_id: 1}), {_id: 1}); + // Within the transaction, we should not see the out-of-transaction update. + assert.eq(sessionColl.findOne({_id: 1}), {_id: 1}); - assert.commandWorked(session.commitTransaction_forTesting()); + assert.commandWorked(session.commitTransaction_forTesting()); - // The document should be updated on the local snapshot. - assert.eq(coll.findOne({_id: 0}), {_id: 0, x: 2}); + // The document should be updated on the local snapshot. + assert.eq(coll.findOne({_id: 0}), {_id: 0, x: 2}); - // The document should not be updated in the majority snapshot. - assert.eq(coll.find({_id: 0}).readConcern("majority").next(), {_id: 0}); + // The document should not be updated in the majority snapshot. + assert.eq(coll.find({_id: 0}).readConcern("majority").next(), {_id: 0}); - // Make sure write conflicts are caught with speculative transactions. - jsTestLog("Starting a conflicting transaction which will be auto-aborted"); - session.startTransaction({readConcern: {level: "snapshot"}, writeConcern: {w: 1}}); + // Make sure write conflicts are caught with speculative transactions. + jsTestLog("Starting a conflicting transaction which will be auto-aborted"); + session.startTransaction({readConcern: {level: "snapshot"}, writeConcern: {w: 1}}); - // Read some data inside the transaction. - assert.eq(sessionColl.findOne({_id: 1}), {_id: 1, y: 1}); + // Read some data inside the transaction. + assert.eq(sessionColl.findOne({_id: 1}), {_id: 1, y: 1}); - // Write it outside the transaction. - assert.commandWorked(coll.update({_id: 1}, {$inc: {x: 1}})); + // Write it outside the transaction. + assert.commandWorked(coll.update({_id: 1}, {$inc: {x: 1}})); - // Can still read old data in transaction. - assert.eq(sessionColl.findOne({_id: 1}), {_id: 1, y: 1}); + // Can still read old data in transaction. + assert.eq(sessionColl.findOne({_id: 1}), {_id: 1, y: 1}); - // But update fails - assert.commandFailedWithCode(sessionColl.update({_id: 1}, {$inc: {x: 1}}), - ErrorCodes.WriteConflict); + // But update fails + assert.commandFailedWithCode(sessionColl.update({_id: 1}, {$inc: {x: 1}}), + ErrorCodes.WriteConflict); - assert.commandFailedWithCode(session.abortTransaction_forTesting(), - ErrorCodes.NoSuchTransaction); + assert.commandFailedWithCode(session.abortTransaction_forTesting(), + ErrorCodes.NoSuchTransaction); - // Restart server replication to allow majority commit point to advance. - restartServerReplication(secondary); + // Restart server replication to allow majority commit point to advance. + restartServerReplication(secondary); - jsTestLog("Starting final transaction (with majority commit)"); - // Start a third transaction, with majority commit. - session.startTransaction({readConcern: {level: "snapshot"}, writeConcern: {w: "majority"}}); + jsTestLog("Starting final transaction (with majority commit)"); + // Start a third transaction, with majority commit. + session.startTransaction({readConcern: {level: "snapshot"}, writeConcern: {w: "majority"}}); - // We should see the updated doc within the transaction. - assert.eq(sessionColl.findOne({_id: 0}), {_id: 0, x: 2}); + // We should see the updated doc within the transaction. + assert.eq(sessionColl.findOne({_id: 0}), {_id: 0, x: 2}); - // Update it one more time. - assert.commandWorked(sessionColl.update({_id: 0}, {$inc: {x: 1}})); + // Update it one more time. + assert.commandWorked(sessionColl.update({_id: 0}, {$inc: {x: 1}})); - assert.commandWorked(session.commitTransaction_forTesting()); + assert.commandWorked(session.commitTransaction_forTesting()); - // The document should be updated on the local snapshot. - assert.eq(coll.findOne({_id: 0}), {_id: 0, x: 3}); + // The document should be updated on the local snapshot. + assert.eq(coll.findOne({_id: 0}), {_id: 0, x: 3}); - // The document should also be updated in the majority snapshot. - assert.eq(coll.find({_id: 0}).readConcern("majority").next(), {_id: 0, x: 3}); + // The document should also be updated in the majority snapshot. + assert.eq(coll.find({_id: 0}).readConcern("majority").next(), {_id: 0, x: 3}); - session.endSession(); - } + session.endSession(); +} - runTest({causalConsistency: false}); - runTest({causalConsistency: true}); - rst.stopSet(); +runTest({causalConsistency: false}); +runTest({causalConsistency: true}); +rst.stopSet(); }()); |