diff options
-rw-r--r-- | jstests/core/txns/prepare_conflict.js | 37 |
1 files changed, 18 insertions, 19 deletions
diff --git a/jstests/core/txns/prepare_conflict.js b/jstests/core/txns/prepare_conflict.js index 2abb942ff75..eb360f478c0 100644 --- a/jstests/core/txns/prepare_conflict.js +++ b/jstests/core/txns/prepare_conflict.js @@ -12,14 +12,18 @@ const dbName = "test"; const collName = "prepare_conflict"; const testDB = db.getSiblingDB(dbName); const testColl = testDB.getCollection(collName); - -// Logs must be cleared each run. -assert.commandWorked(testDB.adminCommand({clearLog: "global"})); +const prepareConflictDurationLogMsg = "prepareConflictDuration"; testColl.drop({writeConcern: {w: "majority"}}); assert.commandWorked(testDB.runCommand({create: collName, writeConcern: {w: "majority"}})); -function assertPrepareConflict(filter, clusterTime, count) { +/** + * Asserts that a prepare read conflict occurs, and is recorded through the profiler and logs + * accordingly, by running a find command that uses the provided filter and clusterTime. + */ +const assertPrepareConflict = function assertPrepareReadConflict(filter, clusterTime) { + assert.commandWorked(testDB.adminCommand({clearLog: "global"})); + // Uses a 5 second timeout so that there is enough time for the prepared transaction to // release its locks and for the command to obtain those locks. assert.commandFailedWithCode( @@ -32,8 +36,7 @@ function assertPrepareConflict(filter, clusterTime, count) { }), ErrorCodes.MaxTimeMSExpired); - // Ensures prepareConflictDuration is logged each time a prepare conflict is encountered. - checkLog.containsWithCount(testDB, "prepareConflictDuration", count); + checkLog.contains(testDB, prepareConflictDurationLogMsg); let prepareConflicted = false; const cur = @@ -46,15 +49,15 @@ function assertPrepareConflict(filter, clusterTime, count) { } } assert(prepareConflicted); -} +}; -// Inserts a document modified by the transaction. +// Insert a document modified by the transaction. const txnDoc = { _id: 1, x: 1 }; assert.commandWorked(testColl.insert(txnDoc)); -// Inserts a document unmodified by the transaction. +// Insert a document unmodified by the transaction. const otherDoc = { _id: 2, y: 2 @@ -77,24 +80,21 @@ assert.commandWorked(sessionDB.runCommand({ })); const prepareTimestamp = PrepareHelpers.prepareTransaction(session); -// Stores the number of times prepareConflictDuration should appear in the log. -let conflictCount = 1; - // Conflict on _id of prepared document. -assertPrepareConflict({_id: txnDoc._id}, prepareTimestamp, conflictCount); +assertPrepareConflict({_id: txnDoc._id}, prepareTimestamp); // Conflict on field that could be added to a prepared document. -assertPrepareConflict({randomField: "random"}, prepareTimestamp, ++conflictCount); +assertPrepareConflict({randomField: "random"}, prepareTimestamp); // No conflict on _id of a non-prepared document. +assert.commandWorked(testDB.adminCommand({clearLog: "global"})); assert.commandWorked(testDB.runCommand({find: collName, filter: {_id: otherDoc._id}})); +assert.eq(false, checkLog.checkContainsOnce(testDB, prepareConflictDurationLogMsg)); // No conflict on indexed field of a non-prepared document. +assert.commandWorked(testDB.adminCommand({clearLog: "global"})); assert.commandWorked(testDB.runCommand({find: collName, filter: {y: otherDoc.y}})); - -// Ensures that prepareConflictDuration is only logged when a prepare conflict is encountered. We -// expect conflictCount to be 2. -checkLog.containsWithCount(testDB, "prepareConflictDuration", conflictCount); +assert.eq(false, checkLog.checkContainsOnce(testDB, prepareConflictDurationLogMsg)); // At this point, we can guarantee all subsequent reads will conflict. Do a read in a parallel // shell, abort the transaction, then ensure the read succeeded with the old document. @@ -105,7 +105,6 @@ const findAwait = startParallelShell(function() { const it = db.getSiblingDB(TestData.dbName) .runCommand({find: TestData.collName, filter: {_id: TestData.txnDoc._id}}); }, db.getMongo().port); - assert.commandWorked(session.abortTransaction_forTesting()); // The find command should be successful. |