jstests/sharding/transactions_snapshot_errors_first_statement.js
// Verify that all commands that can start a transaction are able to retry on snapshot errors.
Verify that all commands that can start a transaction are able to retry on snapshot errors.
Verify that all commands that can start a transaction are able to retry on snapshot errors .
function runTest(st, collName, numShardsToError, errorCode, isSharded) {
    const session = st.s.startSession();
    const sessionDB = session.getDatabase(dbName);
    for (let commandTestCase of kCommandTestCases) {
        const commandName = commandTestCase.name;
        const commandBody = commandTestCase.command;
        if (isSharded && commandName === "distinct") {
            print("Skipping distinct test case for sharded collection");
            continue;
        }
         Retry on a single error.
        setFailCommandOnShards(st, {times: 1}, [commandName], errorCode, numShardsToError);
        session.startTransaction({readConcern: {level: "snapshot"}});
        assert.commandWorked(sessionDB.runCommand(commandBody));
        assert.commandWorked(session.commitTransaction_forTesting());
        unsetFailCommandOnEachShard(st, numShardsToError);
         Clean up after insert to avoid duplicate key errors.
        if (commandName === "insert") {
            assert.commandWorked(sessionDB[collName].remove({_id: {$in: [1, 11]}}));
        }
         Retry on multiple errors.
        setFailCommandOnShards(st, {times: 3}, [commandName], errorCode, numShardsToError);
        session.startTransaction({readConcern: {level: "snapshot"}});
        assert.commandWorked(sessionDB.runCommand(commandBody));
        assert.commandWorked(session.commitTransaction_forTesting());
        unsetFailCommandOnEachShard(st, numShardsToError);
         Clean up after insert to avoid duplicate key errors.
        if (commandName === "insert") {
            assert.commandWorked(sessionDB[collName].remove({_id: {$in: [1, 11]}}));
        }
         Exhaust retry attempts.
        setFailCommandOnShards(st, "alwaysOn", [commandName], errorCode, numShardsToError);
        session.startTransaction({readConcern: {level: "snapshot"}});
        const res = assert.commandFailedWithCode(sessionDB.runCommand(commandBody), errorCode);
        assert.eq(res.errorLabels, ["TransientTransactionError"]);
        unsetFailCommandOnEachShard(st, numShardsToError);
        assertNoSuchTransactionOnAllShards(
            st, session.getSessionId(), session.getTxnNumber_forTesting());
        assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                     ErrorCodes.NoSuchTransaction);
    }
}
function runTest ( st , collName , numShardsToError , errorCode , isSharded ) { const session = st . s . startSession ( ) ; const sessionDB = session . getDatabase ( dbName ) ; for ( let commandTestCase of kCommandTestCases ) { const commandName = commandTestCase . name ; const commandBody = commandTestCase . command ; if ( isSharded && commandName === "distinct" ) { print ( "Skipping distinct test case for sharded collection" ) ; continue ; } setFailCommandOnShards ( st , { times : 1 } , [ commandName ] , errorCode , numShardsToError ) ; session . startTransaction ( { readConcern : { level : "snapshot" } } ) ; assert . commandWorked ( sessionDB . runCommand ( commandBody ) ) ; assert . commandWorked ( session . commitTransaction_forTesting ( ) ) ; unsetFailCommandOnEachShard ( st , numShardsToError ) ; if ( commandName === "insert" ) { assert . commandWorked ( sessionDB [ collName ] . remove ( { _id : { $in : [ 1 , 11 ] } } ) ) ; } setFailCommandOnShards ( st , { times : 3 } , [ commandName ] , errorCode , numShardsToError ) ; session . startTransaction ( { readConcern : { level : "snapshot" } } ) ; assert . commandWorked ( sessionDB . runCommand ( commandBody ) ) ; assert . commandWorked ( session . commitTransaction_forTesting ( ) ) ; unsetFailCommandOnEachShard ( st , numShardsToError ) ; if ( commandName === "insert" ) { assert . commandWorked ( sessionDB [ collName ] . remove ( { _id : { $in : [ 1 , 11 ] } } ) ) ; } setFailCommandOnShards ( st , "alwaysOn" , [ commandName ] , errorCode , numShardsToError ) ; session . startTransaction ( { readConcern : { level : "snapshot" } } ) ; const res = assert . commandFailedWithCode ( sessionDB . runCommand ( commandBody ) , errorCode ) ; assert . eq ( res . errorLabels , [ "TransientTransactionError" ] ) ; unsetFailCommandOnEachShard ( st , numShardsToError ) ; assertNoSuchTransactionOnAllShards ( st , session . getSessionId ( ) , session . getTxnNumber_forTesting ( ) ) ; assert . commandFailedWithCode ( session . abortTransaction_forTesting ( ) , ErrorCodes . NoSuchTransaction ) ; } }
Verify that all commands that can start a transaction are able to retry on snapshot errors.
Verify that all commands that can start a transaction are able to retry on snapshot errors .
// Distinct isn't allowed on sharded collections in a multi-document transaction. // // Retry on a single error. // // Clean up after insert to avoid duplicate key errors. // // Retry on multiple errors. // // Clean up after insert to avoid duplicate key errors. // // Exhaust retry attempts. //