jstests/replsets/kill_prepared_transaction_commit_abort.js
/**
 * Commit or abort transactions on all the given sessions until all transactions are complete. We
 * choose to randomly commit or abort a given transaction with equal probability.
 */
Commit or abort transactions on all the given sessions until all transactions are complete. We
choose to randomly commit or abort a given transaction with equal probability.
Commit or abort transactions on all the given sessions until all transactions are complete . We choose to randomly commit or abort a given transaction with equal probability .
function commitOrAbortAllTransactions(sessions) {
    while (sessions.find(s => (s.state === TxnState.InProgress)) !== undefined) {
        for (let i = 0; i < sessions.length; i++) {
            if (sessions[i].state !== TxnState.InProgress) {
                continue;
            }
            let sess = sessions[i];
            let terminalStates = [TxnState.Committed, TxnState.Aborted];
            let terminalState = terminalStates[Math.round(Math.random())];
            let cmd = (terminalState === TxnState.Committed)
                ? {commitTransaction: 1, commitTimestamp: sess.commitTs}
                : {abortTransaction: 1};
            let res = sess.session.getDatabase("admin").adminCommand(cmd);
            if (res.ok === 1) {
                sessions[i].state = terminalState;
            }
            if (res.ok === 0) {
                assert.commandFailedWithCode(res, ErrorCodes.Interrupted);
                print("Transaction " + i + " was interrupted.");
            }
        }
    }
}
function commitOrAbortAllTransactions ( sessions ) { while ( sessions . find ( s => ( s . state === TxnState . InProgress ) ) !== undefined ) { for ( let i = 0 ; i < sessions . length ; i ++ ) { if ( sessions [ i ] . state !== TxnState . InProgress ) { continue ; } let sess = sessions [ i ] ; let terminalStates = [ TxnState . Committed , TxnState . Aborted ] ; let terminalState = terminalStates [ Math . round ( Math . random ( ) ) ] ; let cmd = ( terminalState === TxnState . Committed ) ? { commitTransaction : 1 , commitTimestamp : sess . commitTs } : { abortTransaction : 1 } ; let res = sess . session . getDatabase ( "admin" ) . adminCommand ( cmd ) ; if ( res . ok === 1 ) { sessions [ i ] . state = terminalState ; } if ( res . ok === 0 ) { assert . commandFailedWithCode ( res , ErrorCodes . Interrupted ) ; print ( "Transaction " + i + " was interrupted." ) ; } } } }
Commit or abort transactions on all the given sessions until all transactions are complete.
Commit or abort transactions on all the given sessions until all transactions are complete .
// Until all transactions have definitively completed, try to abort/commit the open, // prepared transactions. // We don't need to commit an already completed transaction. // Randomly choose to commit or abort the transaction. // Mark the transaction's terminal state. // We assume that transaction commit/abort should not fail for any reason other than // interruption in this test. If the commit/abort was interrupted, then the command // should have failed, and the transaction state should be unaffected.