diff options
author | William Schultz <william.schultz@mongodb.com> | 2018-04-23 14:53:42 -0400 |
---|---|---|
committer | William Schultz <william.schultz@mongodb.com> | 2018-04-23 14:53:58 -0400 |
commit | 38487257b6b1d054619075641ec20d1aa79555d5 (patch) | |
tree | f9e42ad2b5693481955d0545f3907591360309a3 | |
parent | 8941fac630e0ac31649a1e32b0ea54c3930bdfec (diff) | |
download | mongo-38487257b6b1d054619075641ec20d1aa79555d5.tar.gz |
SERVER-34259 Convert find_and_modify_in_transaction.js to use transactions shell API
-rw-r--r-- | jstests/core/txns/find_and_modify_in_transaction.js | 297 |
1 files changed, 111 insertions, 186 deletions
diff --git a/jstests/core/txns/find_and_modify_in_transaction.js b/jstests/core/txns/find_and_modify_in_transaction.js index 6e4bf05b794..29b41d799dc 100644 --- a/jstests/core/txns/find_and_modify_in_transaction.js +++ b/jstests/core/txns/find_and_modify_in_transaction.js @@ -12,215 +12,140 @@ assert.commandWorked( testDB.createCollection(testColl.getName(), {writeConcern: {w: "majority"}})); - let txnNumber = 0; const sessionOptions = {causalConsistency: false}; const session = db.getMongo().startSession(sessionOptions); const sessionDb = session.getDatabase(dbName); + const sessionColl = sessionDb[collName]; jsTest.log("Prepopulate the collection."); assert.writeOK(testColl.insert([{_id: 0, a: 0}, {_id: 1, a: 1}, {_id: 2, a: 2}], {writeConcern: {w: "majority"}})); + /*********************************************************************************************** + * Do a non-matching find-and-modify with remove. + **********************************************************************************************/ + jsTest.log("Do a non-matching find-and-modify with remove."); - let res = assert.commandWorked(sessionDb.runCommand({ - findAndModify: collName, - query: {a: 99}, - remove: true, - readConcern: {level: "snapshot"}, - txnNumber: NumberLong(txnNumber), - startTransaction: true, - autocommit: false - })); - assert.eq(null, res.value); - - res = assert.commandWorked(sessionDb.runCommand( - {find: collName, filter: {}, txnNumber: NumberLong(txnNumber), autocommit: false})); - assert.docEq(res.cursor.firstBatch, [{_id: 0, a: 0}, {_id: 1, a: 1}, {_id: 2, a: 2}]); - - // commitTransaction can only be run on the admin database. - assert.commandWorked(sessionDb.adminCommand({ - commitTransaction: 1, - txnNumber: NumberLong(txnNumber++), - writeConcern: {w: "majority"}, - autocommit: false - })); + session.startTransaction({writeConcern: {w: "majority"}}); + + // Do a findAndModify that affects no documents. + let res = sessionColl.findAndModify({query: {a: 99}, remove: true}); + assert.eq(null, res); + let docs = sessionColl.find({}).toArray(); + assert.docEq(docs, [{_id: 0, a: 0}, {_id: 1, a: 1}, {_id: 2, a: 2}]); + + // Commit the transaction. + session.commitTransaction(); + + /*********************************************************************************************** + * Do a non-matching find-and-modify with update. + **********************************************************************************************/ jsTest.log("Do a non-matching find-and-modify with update."); - res = assert.commandWorked(sessionDb.runCommand({ - findAndModify: collName, - query: {a: 99}, - update: {$inc: {a: 100}}, - readConcern: {level: "snapshot"}, - txnNumber: NumberLong(txnNumber), - startTransaction: true, - autocommit: false - })); - assert.eq(null, res.value); - - res = assert.commandWorked(sessionDb.runCommand( - {find: collName, filter: {}, txnNumber: NumberLong(txnNumber), autocommit: false})); - assert.docEq(res.cursor.firstBatch, [{_id: 0, a: 0}, {_id: 1, a: 1}, {_id: 2, a: 2}]); - - // commitTransaction can only be run on the admin database. - assert.commandWorked(sessionDb.adminCommand({ - commitTransaction: 1, - txnNumber: NumberLong(txnNumber++), - writeConcern: {w: "majority"}, - autocommit: false - })); + + session.startTransaction({writeConcern: {w: "majority"}}); + + res = sessionColl.findAndModify({query: {a: 99}, update: {$inc: {a: 100}}}); + assert.eq(null, res); + docs = sessionColl.find({}).toArray(); + assert.docEq(docs, [{_id: 0, a: 0}, {_id: 1, a: 1}, {_id: 2, a: 2}]); + + // Commit the transaction. + session.commitTransaction(); + + /*********************************************************************************************** + * Do a matching find-and-modify with remove. + **********************************************************************************************/ jsTest.log("Do a matching find-and-modify with remove."); - res = assert.commandWorked(sessionDb.runCommand({ - findAndModify: collName, - query: {a: 0}, - remove: true, - readConcern: {level: "snapshot"}, - txnNumber: NumberLong(txnNumber), - startTransaction: true, - autocommit: false - })); - assert.eq({_id: 0, a: 0}, res.value); - - res = assert.commandWorked(sessionDb.runCommand( - {find: collName, filter: {}, txnNumber: NumberLong(txnNumber), autocommit: false})); - assert.docEq(res.cursor.firstBatch, [{_id: 1, a: 1}, {_id: 2, a: 2}]); - - // commitTransaction can only be run on the admin database. - assert.commandWorked(sessionDb.adminCommand({ - commitTransaction: 1, - txnNumber: NumberLong(txnNumber++), - writeConcern: {w: "majority"}, - autocommit: false - })); + + session.startTransaction({writeConcern: {w: "majority"}}); + + res = sessionColl.findAndModify({query: {a: 0}, remove: true}); + assert.eq({_id: 0, a: 0}, res); + docs = sessionColl.find({}).toArray(); + assert.docEq(docs, [{_id: 1, a: 1}, {_id: 2, a: 2}]); + + // Commit the transaction. + session.commitTransaction(); + + /*********************************************************************************************** + * Do a matching find-and-modify with update, requesting the old doc. + **********************************************************************************************/ jsTest.log("Do a matching find-and-modify with update, requesting the old doc."); - res = assert.commandWorked(sessionDb.runCommand({ - findAndModify: collName, - query: {a: 1}, - update: {$inc: {a: 100}}, - readConcern: {level: "snapshot"}, - txnNumber: NumberLong(txnNumber), - startTransaction: true, - autocommit: false - })); - assert.eq({_id: 1, a: 1}, res.value); - - res = assert.commandWorked(sessionDb.runCommand( - {find: collName, filter: {}, txnNumber: NumberLong(txnNumber), autocommit: false})); - assert.docEq(res.cursor.firstBatch, [{_id: 1, a: 101}, {_id: 2, a: 2}]); - - // commitTransaction can only be run on the admin database. - assert.commandWorked(sessionDb.adminCommand({ - commitTransaction: 1, - txnNumber: NumberLong(txnNumber++), - writeConcern: {w: "majority"}, - autocommit: false - })); + session.startTransaction({writeConcern: {w: "majority"}}); + + res = sessionColl.findAndModify({query: {a: 1}, update: {$inc: {a: 100}}}); + assert.eq({_id: 1, a: 1}, res); + docs = sessionColl.find({}).toArray(); + assert.docEq(docs, [{_id: 1, a: 101}, {_id: 2, a: 2}]); + + // Commit the transaction. + session.commitTransaction(); + + /*********************************************************************************************** + * Do a matching find-and-modify with update, requesting the new doc. + **********************************************************************************************/ jsTest.log("Do a matching find-and-modify with update, requesting the new doc."); - res = assert.commandWorked(sessionDb.runCommand({ - findAndModify: collName, - query: {a: 2}, - update: {$inc: {a: 100}}, - new: true, - readConcern: {level: "snapshot"}, - txnNumber: NumberLong(txnNumber), - startTransaction: true, - autocommit: false - })); - assert.eq({_id: 2, a: 102}, res.value); - - res = assert.commandWorked(sessionDb.runCommand( - {find: collName, filter: {}, txnNumber: NumberLong(txnNumber), autocommit: false})); - assert.docEq(res.cursor.firstBatch, [{_id: 1, a: 101}, {_id: 2, a: 102}]); - - // commitTransaction can only be run on the admin database. - assert.commandWorked(sessionDb.adminCommand({ - commitTransaction: 1, - txnNumber: NumberLong(txnNumber++), - writeConcern: {w: "majority"}, - autocommit: false - })); + session.startTransaction({writeConcern: {w: "majority"}}); + + res = sessionColl.findAndModify({query: {a: 2}, update: {$inc: {a: 100}}, new: true}); + assert.eq({_id: 2, a: 102}, res); + docs = sessionColl.find({}).toArray(); + assert.docEq(docs, [{_id: 1, a: 101}, {_id: 2, a: 102}]); + + // Commit the transaction. + session.commitTransaction(); + + /*********************************************************************************************** + * Do a matching find-and-modify with upsert, requesting the new doc. + **********************************************************************************************/ jsTest.log("Do a matching find-and-modify with upsert, requesting the new doc."); - res = assert.commandWorked(sessionDb.runCommand({ - findAndModify: collName, - query: {_id: 2}, - update: {$inc: {a: 100}}, - upsert: true, - new: true, - readConcern: {level: "snapshot"}, - txnNumber: NumberLong(txnNumber), - startTransaction: true, - autocommit: false - })); - assert.eq({_id: 2, a: 202}, res.value); - - res = assert.commandWorked(sessionDb.runCommand( - {find: collName, filter: {}, txnNumber: NumberLong(txnNumber), autocommit: false})); - assert.docEq(res.cursor.firstBatch, [{_id: 1, a: 101}, {_id: 2, a: 202}]); - - // commitTransaction can only be run on the admin database. - assert.commandWorked(sessionDb.adminCommand({ - commitTransaction: 1, - txnNumber: NumberLong(txnNumber++), - writeConcern: {w: "majority"}, - autocommit: false - })); + session.startTransaction({writeConcern: {w: "majority"}}); + + res = sessionColl.findAndModify( + {query: {_id: 2}, update: {$inc: {a: 100}}, upsert: true, new: true}); + assert.eq({_id: 2, a: 202}, res); + docs = sessionColl.find({}).toArray(); + assert.docEq(docs, [{_id: 1, a: 101}, {_id: 2, a: 202}]); + + // Commit the transaction. + session.commitTransaction(); + + /*********************************************************************************************** + * Do a non-matching find-and-modify with upsert, requesting the old doc. + **********************************************************************************************/ jsTest.log("Do a non-matching find-and-modify with upsert, requesting the old doc."); - res = assert.commandWorked(sessionDb.runCommand({ - findAndModify: collName, - query: {a: 3}, - upsert: true, - update: {$inc: {a: 100}}, - readConcern: {level: "snapshot"}, - txnNumber: NumberLong(txnNumber), - startTransaction: true, - autocommit: false - })); - assert.eq(null, res.value); - res = assert.commandWorked(sessionDb.runCommand({ - find: collName, - filter: {a: 103}, - projection: {_id: 0}, - txnNumber: NumberLong(txnNumber), - autocommit: false - })); - assert.docEq(res.cursor.firstBatch, [{a: 103}]); - - // commitTransaction can only be run on the admin database. - assert.commandWorked(sessionDb.adminCommand({ - commitTransaction: 1, - txnNumber: NumberLong(txnNumber++), - writeConcern: {w: "majority"}, - autocommit: false - })); + session.startTransaction({writeConcern: {w: "majority"}}); + + res = sessionColl.findAndModify({query: {a: 3}, update: {$inc: {a: 100}}, upsert: true}); + assert.eq(null, res); + docs = sessionColl.find({a: 103}, {_id: 0}).toArray(); + assert.docEq(docs, [{a: 103}]); + + // Commit the transaction. + session.commitTransaction(); + + /*********************************************************************************************** + * Do a non-matching find-and-modify with upsert, requesting the new doc. + **********************************************************************************************/ jsTest.log("Do a non-matching find-and-modify with upsert, requesting the new doc."); - res = assert.commandWorked(sessionDb.runCommand({ - findAndModify: collName, - query: {a: 4}, - update: {$inc: {a: 200}}, - upsert: true, - new: true, - readConcern: {level: "snapshot"}, - txnNumber: NumberLong(txnNumber), - startTransaction: true, - autocommit: false - })); - const newdoc = res.value; + session.startTransaction({writeConcern: {w: "majority"}}); + res = sessionColl.findAndModify( + {query: {a: 4}, update: {$inc: {a: 200}}, upsert: true, new: true}); + + const newdoc = res; assert.eq(204, newdoc.a); - res = assert.commandWorked(sessionDb.runCommand( - {find: collName, filter: {a: 204}, txnNumber: NumberLong(txnNumber), autocommit: false})); - assert.docEq(res.cursor.firstBatch, [newdoc]); - - // commitTransaction can only be run on the admin database. - assert.commandWorked(sessionDb.adminCommand({ - commitTransaction: 1, - txnNumber: NumberLong(txnNumber++), - writeConcern: {w: "majority"}, - autocommit: false - })); + docs = sessionColl.find({a: 204}).toArray(); + assert.docEq(docs, [newdoc]); + + // Commit the transaction. + session.commitTransaction(); + session.endSession(); }()); |