jstests/sharding/internal_txns/libs/resharding_test.js
// Helpers for defining transaction test cases.
Helpers for defining transaction test cases.
Helpers for defining transaction test cases .
function makeTransactionOptionsForInsertUpdateDeleteTest(
        {isPreparedTxn, isLargeTxn, abortOnInitialTry}) {
        const testId = ++lastTestId;
        const testCase = makeSessionOptionsForTest(testId);
        testCase.id = testId;
        testCase.isPreparedTxn = isPreparedTxn;
        testCase.isLargeTxn = isLargeTxn;
        testCase.abortOnInitialTry = abortOnInitialTry;
        testCase.lastUsedStmtId = -1;
        testCase.commands = [];
        const docToInsert = {insertOp: -testId, oldShardKey: -testId, newShardKey: -testId};
        testCase.commands.push({
            cmdObj: {
                insert: kCollName,
                documents: [docToInsert],
                stmtId: NumberInt(++testCase.lastUsedStmtId)
            },
            checkResponseFunc: (res) => {
                assert.eq(res.n, 1);
            }
        });
        const docToUpdate = {updateOp: -testId, oldShardKey: -testId, newShardKey: -testId};
        testCase.commands.push({
            cmdObj: {
                update: kCollName,
                updates: [{q: docToUpdate, u: {$mul: {updateOp: 100}}}],
                stmtId: NumberInt(++testCase.lastUsedStmtId)
            },
            checkResponseFunc: (res) => {
                assert.eq(res.n, 1);
                assert.eq(res.nModified, 1);
            }
        });
        const updatedDoc = {updateOp: -testId * 100, oldShardKey: -testId, newShardKey: -testId};
        const docToDelete = {deleteOp: -testId, oldShardKey: -testId, newShardKey: -testId};
        testCase.commands.push({
            cmdObj: {
                delete: kCollName,
                deletes: [{q: docToDelete, limit: 1}],
                stmtId: NumberInt(++testCase.lastUsedStmtId)
            },
            checkResponseFunc: (res) => {
                assert.eq(res.n, 1);
            }
        });
        const additionalDocsToInsert =
            makeInsertCommandsIfTestingPreparedOrLargeTransaction(testId, testCase);
        testCase.setUpFunc = () => {
            const coll = mongosConn.getCollection(kNs);
            assert.commandWorked(coll.insert(docToUpdate));
            assert.commandWorked(coll.insert(docToDelete));
        };
        testCase.checkDocsFunc = (isTxnCommitted) => {
            const coll = mongosConn.getCollection(kNs);
            const docs = coll.find().toArray();
            assert.eq(coll.find(docToInsert).itcount(), isTxnCommitted ? 1 : 0, tojson(docs));
            assert.eq(coll.find(docToUpdate).itcount(), isTxnCommitted ? 0 : 1, tojson(docs));
            assert.eq(coll.find(updatedDoc).itcount(), isTxnCommitted ? 1 : 0, tojson(docs));
            assert.eq(coll.find(docToDelete).itcount(), isTxnCommitted ? 0 : 1, tojson(docs));
            additionalDocsToInsert.forEach(docToInsert => {
                assert.eq(coll.find(docToInsert).itcount(), isTxnCommitted ? 1 : 0, tojson(docs));
            });
        };
        return testCase;
    }
function makeTransactionOptionsForInsertUpdateDeleteTest ( { isPreparedTxn , isLargeTxn , abortOnInitialTry } ) { const testId = ++ lastTestId ; const testCase = makeSessionOptionsForTest ( testId ) ; testCase . id = testId ; testCase . isPreparedTxn = isPreparedTxn ; testCase . isLargeTxn = isLargeTxn ; testCase . abortOnInitialTry = abortOnInitialTry ; testCase . lastUsedStmtId = - 1 ; testCase . commands = [ ] ; const docToInsert = { insertOp : - testId , oldShardKey : - testId , newShardKey : - testId } ; testCase . commands . push ( { cmdObj : { insert : kCollName , documents : [ docToInsert ] , stmtId : NumberInt ( ++ testCase . lastUsedStmtId ) } , checkResponseFunc : ( res ) => { assert . eq ( res . n , 1 ) ; } } ) ; const docToUpdate = { updateOp : - testId , oldShardKey : - testId , newShardKey : - testId } ; testCase . commands . push ( { cmdObj : { update : kCollName , updates : [ { q : docToUpdate , u : { $mul : { updateOp : 100 } } } ] , stmtId : NumberInt ( ++ testCase . lastUsedStmtId ) } , checkResponseFunc : ( res ) => { assert . eq ( res . n , 1 ) ; assert . eq ( res . nModified , 1 ) ; } } ) ; const updatedDoc = { updateOp : - testId * 100 , oldShardKey : - testId , newShardKey : - testId } ; const docToDelete = { deleteOp : - testId , oldShardKey : - testId , newShardKey : - testId } ; testCase . commands . push ( { cmdObj : { delete : kCollName , deletes : [ { q : docToDelete , limit : 1 } ] , stmtId : NumberInt ( ++ testCase . lastUsedStmtId ) } , checkResponseFunc : ( res ) => { assert . eq ( res . n , 1 ) ; } } ) ; const additionalDocsToInsert = makeInsertCommandsIfTestingPreparedOrLargeTransaction ( testId , testCase ) ; testCase . setUpFunc = ( ) => { const coll = mongosConn . getCollection ( kNs ) ; assert . commandWorked ( coll . insert ( docToUpdate ) ) ; assert . commandWorked ( coll . insert ( docToDelete ) ) ; } ; testCase . checkDocsFunc = ( isTxnCommitted ) => { const coll = mongosConn . getCollection ( kNs ) ; const docs = coll . find ( ) . toArray ( ) ; assert . eq ( coll . find ( docToInsert ) . itcount ( ) , isTxnCommitted ? 1 : 0 , tojson ( docs ) ) ; assert . eq ( coll . find ( docToUpdate ) . itcount ( ) , isTxnCommitted ? 0 : 1 , tojson ( docs ) ) ; assert . eq ( coll . find ( updatedDoc ) . itcount ( ) , isTxnCommitted ? 1 : 0 , tojson ( docs ) ) ; assert . eq ( coll . find ( docToDelete ) . itcount ( ) , isTxnCommitted ? 0 : 1 , tojson ( docs ) ) ; additionalDocsToInsert . forEach ( docToInsert => { assert . eq ( coll . find ( docToInsert ) . itcount ( ) , isTxnCommitted ? 1 : 0 , tojson ( docs ) ) ; } ) ; } ; return testCase ; }
Helpers for defining transaction test cases.
Helpers for defining transaction test cases .
// Define the retryable insert, update and delete statements to be run in the test internal // transaction. Prior to resharding, these write statements will be routed to donor0. // If testing a prepared and/or large transaction, define additional insert statements to // make the transaction a prepared and/or large transaction.