diff options
author | William Schultz <william.schultz@mongodb.com> | 2018-04-18 14:56:17 -0400 |
---|---|---|
committer | William Schultz <william.schultz@mongodb.com> | 2018-04-18 14:56:17 -0400 |
commit | f214e4c71c1dbcfe103fa3de4fae7afbcbb60b64 (patch) | |
tree | 25ecdb3ed7d3380e95e9c5fe00c5307ad6dea580 /jstests | |
parent | 32b1be84310f2bdb617f79d50b5a5b8b44a99bed (diff) | |
download | mongo-f214e4c71c1dbcfe103fa3de4fae7afbcbb60b64.tar.gz |
SERVER-34259 Update multi_{delete,update}_in_transaction.js to use transactions shell API
Diffstat (limited to 'jstests')
-rw-r--r-- | jstests/core/txns/multi_delete_in_transaction.js | 84 | ||||
-rw-r--r-- | jstests/core/txns/multi_update_in_transaction.js | 155 |
2 files changed, 72 insertions, 167 deletions
diff --git a/jstests/core/txns/multi_delete_in_transaction.js b/jstests/core/txns/multi_delete_in_transaction.js index 693379add84..7ff3b8eb506 100644 --- a/jstests/core/txns/multi_delete_in_transaction.js +++ b/jstests/core/txns/multi_delete_in_transaction.js @@ -12,84 +12,48 @@ 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: 0}, {_id: 2, a: 1}], {writeConcern: {w: "majority"}})); jsTest.log("Do an empty multi-delete."); - let res = assert.commandWorked(sessionDb.runCommand({ - delete: collName, - deletes: [{q: {a: 99}, limit: 0}], - readConcern: {level: "snapshot"}, - txnNumber: NumberLong(txnNumber), - startTransaction: true, - autocommit: false - })); - assert.eq(0, res.n); + session.startTransaction({writeConcern: {w: "majority"}}); - 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: 0}, {_id: 2, a: 1}]); + // Remove no docs. + let res = sessionColl.remove({a: 99}, {justOne: false}); + assert.eq(0, res.nRemoved); + res = sessionColl.find({}); + assert.docEq(res.toArray(), [{_id: 0, a: 0}, {_id: 1, a: 0}, {_id: 2, a: 1}]); - // commitTransaction can only be called on the admin database. - assert.commandWorked(sessionDb.adminCommand({ - commitTransaction: 1, - txnNumber: NumberLong(txnNumber++), - // TODO(russotto): Majority write concern on commit is to avoid a WriteConflictError - // writing to the transaction table. - writeConcern: {w: "majority"}, - autocommit: false - })); + session.commitTransaction(); jsTest.log("Do a single-result multi-delete."); - res = assert.commandWorked(sessionDb.runCommand({ - delete: collName, - deletes: [{q: {a: 1}, limit: 0}], - readConcern: {level: "snapshot"}, - txnNumber: NumberLong(txnNumber), - startTransaction: true, - autocommit: false - })); - assert.eq(1, res.n); - 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: 0}]); + session.startTransaction({writeConcern: {w: "majority"}}); - // commitTransaction can only be called on the admin database. - assert.commandWorked(sessionDb.adminCommand({ - commitTransaction: 1, - txnNumber: NumberLong(txnNumber++), - writeConcern: {w: "majority"}, - autocommit: false - })); + // Remove one doc. + res = sessionColl.remove({a: 1}, {justOne: false}); + assert.eq(1, res.nRemoved); + res = sessionColl.find({}); + assert.docEq(res.toArray(), [{_id: 0, a: 0}, {_id: 1, a: 0}]); + + session.commitTransaction(); jsTest.log("Do a multiple-result multi-delete."); - res = assert.commandWorked(sessionDb.runCommand({ - delete: collName, - deletes: [{q: {a: 0}, limit: 0}], - readConcern: {level: "snapshot"}, - txnNumber: NumberLong(txnNumber), - startTransaction: true, - autocommit: false - })); - assert.eq(2, res.n); - res = assert.commandWorked(sessionDb.runCommand( - {find: collName, filter: {}, txnNumber: NumberLong(txnNumber), autocommit: false})); - assert.docEq(res.cursor.firstBatch, []); + session.startTransaction({writeConcern: {w: "majority"}}); + + // Remove 2 docs. + res = sessionColl.remove({a: 0}, {justOne: false}); + assert.eq(2, res.nRemoved); + res = sessionColl.find({}); + assert.docEq(res.toArray(), []); - // commitTransaction can only be called on the admin database. - assert.commandWorked(sessionDb.adminCommand({ - commitTransaction: 1, - txnNumber: NumberLong(txnNumber++), - writeConcern: {w: "majority"}, - autocommit: false - })); + session.commitTransaction(); // Collection should be empty. assert.eq(0, testColl.find().itcount()); diff --git a/jstests/core/txns/multi_update_in_transaction.js b/jstests/core/txns/multi_update_in_transaction.js index 33c3e3654d7..f957bf748fd 100644 --- a/jstests/core/txns/multi_update_in_transaction.js +++ b/jstests/core/txns/multi_update_in_transaction.js @@ -12,138 +12,79 @@ 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: 0}, {_id: 2, a: 1}], {writeConcern: {w: "majority"}})); jsTest.log("Do an empty multi-update."); - let res = assert.commandWorked(sessionDb.runCommand({ - update: collName, - updates: [{q: {a: 99}, u: {$set: {b: 1}}, multi: true}], - readConcern: {level: "snapshot"}, - txnNumber: NumberLong(txnNumber), - stmtId: NumberInt(0), - startTransaction: true, - autocommit: false - })); - assert.eq(0, res.n); - - 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: 0}, {_id: 2, a: 1}]); - - assert.commandWorked(sessionDb.adminCommand({ - commitTransaction: 1, - txnNumber: NumberLong(txnNumber++), - // TODO(russotto): Majority write concern on commit is to avoid a WriteConflictError - // writing to the transaction table. - writeConcern: {w: "majority"}, - autocommit: false - })); + session.startTransaction({writeConcern: {w: "majority"}}); + + // Update 0 docs. + let res = sessionColl.update({a: 99}, {$set: {b: 1}}, {multi: true}); + assert.eq(0, res.nModified); + res = sessionColl.find({}); + assert.docEq(res.toArray(), [{_id: 0, a: 0}, {_id: 1, a: 0}, {_id: 2, a: 1}]); + + session.commitTransaction(); jsTest.log("Do a single-result multi-update."); - res = assert.commandWorked(sessionDb.runCommand({ - update: collName, - updates: [{q: {a: 1}, u: {$set: {b: 1}}, multi: true}], - readConcern: {level: "snapshot"}, - txnNumber: NumberLong(txnNumber), - stmtId: NumberInt(0), - startTransaction: true, - autocommit: false - })); - assert.eq(1, res.n); - 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: 0}, {_id: 2, a: 1, b: 1}]); - - assert.commandWorked(sessionDb.adminCommand({ - commitTransaction: 1, - txnNumber: NumberLong(txnNumber++), - writeConcern: {w: "majority"}, - autocommit: false - })); + session.startTransaction({writeConcern: {w: "majority"}}); + + // Update 1 doc. + res = sessionColl.update({a: 1}, {$set: {b: 1}}, {multi: true}); + assert.eq(1, res.nModified); + res = sessionColl.find({}); + assert.docEq(res.toArray(), [{_id: 0, a: 0}, {_id: 1, a: 0}, {_id: 2, a: 1, b: 1}]); + + session.commitTransaction(); jsTest.log("Do a multiple-result multi-update."); - res = assert.commandWorked(sessionDb.runCommand({ - update: collName, - updates: [{q: {a: 0}, u: {$set: {b: 2}}, multi: true}], - readConcern: {level: "snapshot"}, - txnNumber: NumberLong(txnNumber), - stmtId: NumberInt(0), - startTransaction: true, - autocommit: false - })); - assert.eq(2, res.n); - res = assert.commandWorked(sessionDb.runCommand( - {find: collName, filter: {}, txnNumber: NumberLong(txnNumber), autocommit: false})); - assert.docEq(res.cursor.firstBatch, - [{_id: 0, a: 0, b: 2}, {_id: 1, a: 0, b: 2}, {_id: 2, a: 1, b: 1}]); - - assert.commandWorked(sessionDb.adminCommand({ - commitTransaction: 1, - txnNumber: NumberLong(txnNumber++), - writeConcern: {w: "majority"}, - autocommit: false - })); + session.startTransaction({writeConcern: {w: "majority"}}); + + // Update 2 docs. + res = sessionColl.update({a: 0}, {$set: {b: 2}}, {multi: true}); + assert.eq(2, res.nModified); + res = sessionColl.find({}); + assert.docEq(res.toArray(), [{_id: 0, a: 0, b: 2}, {_id: 1, a: 0, b: 2}, {_id: 2, a: 1, b: 1}]); + + session.commitTransaction(); jsTest.log("Do a multiple-query multi-update."); - res = assert.commandWorked(sessionDb.runCommand({ - update: collName, - updates: [ - {q: {a: 0}, u: {$set: {c: 1}}, multi: true}, - {q: {_id: 2}, u: {$set: {c: 2}}, multi: true} - ], - readConcern: {level: "snapshot"}, - txnNumber: NumberLong(txnNumber), - stmtId: NumberInt(0), - startTransaction: true, - autocommit: false - })); - assert.eq(3, res.n); - res = assert.commandWorked(sessionDb.runCommand( - {find: collName, filter: {}, txnNumber: NumberLong(txnNumber), autocommit: false})); + session.startTransaction({writeConcern: {w: "majority"}}); + + // Bulk update 3 docs. + let bulk = sessionColl.initializeUnorderedBulkOp(); + bulk.find({a: 0}).update({$set: {c: 1}}); + bulk.find({_id: 2}).update({$set: {c: 2}}); + res = assert.commandWorked(bulk.execute()); + assert.eq(3, res.nModified); + + res = sessionColl.find({}); assert.docEq( - res.cursor.firstBatch, + res.toArray(), [{_id: 0, a: 0, b: 2, c: 1}, {_id: 1, a: 0, b: 2, c: 1}, {_id: 2, a: 1, b: 1, c: 2}]); - assert.commandWorked(sessionDb.adminCommand({ - commitTransaction: 1, - txnNumber: NumberLong(txnNumber++), - writeConcern: {w: "majority"}, - autocommit: false - })); + session.commitTransaction(); jsTest.log("Do a multi-update with upsert."); - res = assert.commandWorked(sessionDb.runCommand({ - update: collName, - updates: [{q: {_id: 3}, u: {$set: {d: 1}}, multi: true, upsert: true}], - readConcern: {level: "snapshot"}, - txnNumber: NumberLong(txnNumber), - stmtId: NumberInt(0), - startTransaction: true, - autocommit: false - })); - assert.eq(1, res.n); - assert.eq(res.upserted[0]._id, 3); - res = assert.commandWorked(sessionDb.runCommand( - {find: collName, filter: {}, txnNumber: NumberLong(txnNumber), autocommit: false})); - assert.docEq(res.cursor.firstBatch, [ + session.startTransaction({writeConcern: {w: "majority"}}); + + // Upsert 1 doc. + res = sessionColl.update({_id: 3}, {$set: {d: 1}}, {multi: true, upsert: true}); + assert.eq(1, res.nUpserted); + res = sessionColl.find({}); + assert.docEq(res.toArray(), [ {_id: 0, a: 0, b: 2, c: 1}, {_id: 1, a: 0, b: 2, c: 1}, {_id: 2, a: 1, b: 1, c: 2}, {_id: 3, d: 1} ]); - assert.commandWorked(sessionDb.adminCommand({ - commitTransaction: 1, - txnNumber: NumberLong(txnNumber++), - writeConcern: {w: "majority"}, - autocommit: false - })); + session.commitTransaction(); }()); |