diff options
author | Lingzhi Deng <lingzhi.deng@mongodb.com> | 2019-05-07 10:10:02 -0400 |
---|---|---|
committer | Lingzhi Deng <lingzhi.deng@mongodb.com> | 2019-05-08 18:08:16 -0400 |
commit | 44f49a9a97b382e07beca0d3fabf5991eb3a41fa (patch) | |
tree | 3b10ba122e620d256fd23a6d2239680101bc0c04 /jstests/replsets | |
parent | c24b5c1df7d946dd1c931f5c93c7098c9cf8545d (diff) | |
download | mongo-44f49a9a97b382e07beca0d3fabf5991eb3a41fa.tar.gz |
SERVER-40705: No need to delay unlocking if lock is recursively acquired
Diffstat (limited to 'jstests/replsets')
-rw-r--r-- | jstests/replsets/prepared_transaction_on_failover.js | 64 |
1 files changed, 55 insertions, 9 deletions
diff --git a/jstests/replsets/prepared_transaction_on_failover.js b/jstests/replsets/prepared_transaction_on_failover.js index a87abbfc512..48c75700a42 100644 --- a/jstests/replsets/prepared_transaction_on_failover.js +++ b/jstests/replsets/prepared_transaction_on_failover.js @@ -16,7 +16,7 @@ const collName = "coll"; const otherDbName = dbName + "_other"; - function testTransactionsWithFailover(stepDownFunction) { + function testTransactionsWithFailover(doWork, stepDown, postCommit) { const primary = replTest.getPrimary(); const newPrimary = replTest.getSecondary(); const testDB = primary.getDB(dbName); @@ -27,17 +27,15 @@ jsTestLog("Starting transaction"); const session = primary.startSession({causalConsistency: false}); - const sessionDB = session.getDatabase(dbName); session.startTransaction({writeConcern: {w: "majority"}}); - const doc = {_id: "txn on primary " + primary}; - assert.commandWorked(sessionDB.getCollection(collName).insert(doc)); + doWork(primary, session); jsTestLog("Putting transaction into prepare"); const prepareTimestamp = PrepareHelpers.prepareTransaction(session); replTest.awaitReplication(); - stepDownFunction(); + stepDown(); reconnect(primary); jsTestLog("Waiting for the other node to run for election and become primary"); @@ -61,8 +59,7 @@ assert.commandWorked(PrepareHelpers.commitTransaction(newSession, prepareTimestamp)); replTest.awaitReplication(); - assert.docEq(doc, testDB.getCollection(collName).findOne()); - assert.docEq(doc, newPrimary.getDB(dbName).getCollection(collName).findOne()); + postCommit(primary, newPrimary); jsTestLog("Running another transaction on the new primary"); const secondSession = newPrimary.startSession({causalConsistency: false}); @@ -70,19 +67,68 @@ assert.commandWorked( secondSession.getDatabase(dbName).getCollection(collName).insert({_id: "second-doc"})); secondSession.commitTransaction(); + + // Unfreeze the original primary so that it can stand for election again for the next test. + assert.commandWorked(primary.adminCommand({replSetFreeze: 0})); + } + + function doInsert(primary, session) { + const doc = {_id: "txn on primary " + primary}; + jsTestLog("Inserting a document in a transaction."); + assert.commandWorked(session.getDatabase(dbName).getCollection(collName).insert(doc)); + } + function postInsert(primary, newPrimary) { + const doc = {_id: "txn on primary " + primary}; + assert.docEq(doc, primary.getDB(dbName).getCollection(collName).findOne()); + assert.docEq(doc, newPrimary.getDB(dbName).getCollection(collName).findOne()); + } + + function doInsertTextSearch(primary, session) { + // Create an index outside of the transaction. + assert.commandWorked( + primary.getDB(dbName).getCollection(collName).createIndex({text: "text"})); + + // Do the followings in a transaction. + jsTestLog("Inserting a document in a transaction."); + assert.commandWorked( + session.getDatabase(dbName).getCollection(collName).insert({text: "text"})); + // Text search will recursively acquire the global lock. This tests that yielding + // recursively held locks works on step down. + jsTestLog("Doing a text search in a transaction."); + assert.eq(1, + session.getDatabase(dbName) + .getCollection(collName) + .find({$text: {$search: "text"}}) + .itcount()); + } + function postInsertTextSearch(primary, newPrimary) { + assert.eq(1, + primary.getDB(dbName) + .getCollection(collName) + .find({$text: {$search: "text"}}) + .itcount()); + assert.eq(1, + newPrimary.getDB(dbName) + .getCollection(collName) + .find({$text: {$search: "text"}}) + .itcount()); } function stepDownViaHeartbeat() { jsTestLog("Stepping down primary via heartbeat"); replTest.stepUp(replTest.getSecondary()); } - testTransactionsWithFailover(stepDownViaHeartbeat); function stepDownViaCommand() { jsTestLog("Stepping down primary via command"); assert.commandWorked(replTest.getPrimary().adminCommand({replSetStepDown: 10})); } - testTransactionsWithFailover(stepDownViaCommand); + + testTransactionsWithFailover(doInsert, stepDownViaHeartbeat, postInsert); + testTransactionsWithFailover(doInsert, stepDownViaCommand, postInsert); + + testTransactionsWithFailover(doInsertTextSearch, stepDownViaHeartbeat, postInsertTextSearch); + testTransactionsWithFailover(doInsertTextSearch, stepDownViaCommand, postInsertTextSearch); replTest.stopSet(); })(); |