jstests/replsets/libs/tenant_migration_util.js
/**
     * Runs the given tenant migration command against the primary of the given replica set until
     * the command succeeds or fails with a non-retryable error (if 'retryOnRetryableErrors' is
     * true) or until 'shouldStopFunc' returns true. Returns the last response.
     */
Runs the given tenant migration command against the primary of the given replica set until
the command succeeds or fails with a non-retryable error (if 'retryOnRetryableErrors' is
true) or until 'shouldStopFunc' returns true. Returns the last response.
Runs the given tenant migration command against the primary of the given replica set until the command succeeds or fails with a non - retryable error ( if ' retryOnRetryableErrors ' is true ) or until ' shouldStopFunc ' returns true . Returns the last response .
function runTenantMigrationCommand(cmdObj, rst, {
        retryOnRetryableErrors = false,
        shouldStopFunc = () => true,
        enableDonorStartMigrationFsync = false
    } = {}) {
        let primary = rst.getPrimary();
        let localCmdObj = cmdObj;
        let run = () => primary.adminCommand(localCmdObj);
        if (Object.keys(cmdObj)[0] === "donorStartMigration") {
            run = () => {
                const adminDB = primary.getDB("admin");
                localCmdObj = donorStartMigrationWithProtocol(cmdObj, adminDB);
                if (enableDonorStartMigrationFsync) {
                    rst.awaitLastOpCommitted();
                    assert.commandWorked(primary.adminCommand({fsync: 1}));
                }
                return primary.adminCommand(localCmdObj);
            };
        }
        let res;
        assert.soon(() => {
            try {
                res = undefined;
                res = run();
                assert.commandWorked(res);
                return shouldStopFunc(res);
            } catch (e) {
                if (retryOnRetryableErrors && isRetryableError(e)) {
                    jsTestLog(`Retryable error runing runTenantMigrationCommand. Command: ${
                        tojson(localCmdObj)}, Error: ${tojson(e)}`);
                    primary = rst.getPrimary();
                    return false;
                }
                jsTestLog(`Error running runTenantMigrationCommand. Command: ${
                    tojson(localCmdObj)}, Error: ${tojson(e)}`);
                if (res)
                    return true;
                throw e;
            }
        });
        return res;
    }
function runTenantMigrationCommand ( cmdObj , rst , { retryOnRetryableErrors = false , shouldStopFunc = ( ) => true , enableDonorStartMigrationFsync = false } = { } ) { let primary = rst . getPrimary ( ) ; let localCmdObj = cmdObj ; let run = ( ) => primary . adminCommand ( localCmdObj ) ; if ( Object . keys ( cmdObj ) [ 0 ] === "donorStartMigration" ) { run = ( ) => { const adminDB = primary . getDB ( "admin" ) ; localCmdObj = donorStartMigrationWithProtocol ( cmdObj , adminDB ) ; if ( enableDonorStartMigrationFsync ) { rst . awaitLastOpCommitted ( ) ; assert . commandWorked ( primary . adminCommand ( { fsync : 1 } ) ) ; } return primary . adminCommand ( localCmdObj ) ; } ; } let res ; assert . soon ( ( ) => { try { res = undefined ; res = run ( ) ; assert . commandWorked ( res ) ; return shouldStopFunc ( res ) ; } catch ( e ) { if ( retryOnRetryableErrors && isRetryableError ( e ) ) { jsTestLog ( ` ${ tojson ( localCmdObj ) } ${ tojson ( e ) } ` ) ; primary = rst . getPrimary ( ) ; return false ; } jsTestLog ( ` ${ tojson ( localCmdObj ) } ${ tojson ( e ) } ` ) ; if ( res ) return true ; throw e ; } } ) ; return res ; }
Runs the given tenant migration command against the primary of the given replica set until
 the command succeeds or fails with a non-retryable error (if 'retryOnRetryableErrors' is
 true) or until 'shouldStopFunc' returns true.
Runs the given tenant migration command against the primary of the given replica set until the command succeeds or fails with a non - retryable error ( if ' retryOnRetryableErrors ' is true ) or until ' shouldStopFunc ' returns true .
// Note: assert.commandWorked() considers command responses with embedded // writeErrors and WriteConcernErrors as a failure even if the command returned // "ok: 1". And, admin commands(like, donorStartMigration) // doesn't generate writeConcernErros or WriteErrors. So, it's safe to wrap up // run() with assert.commandWorked() here. However, in few scenarios, like // Mongo.prototype.recordRerouteDueToTenantMigration(), it's not safe to wrap up // run() with commandWorked() as retrying on retryable writeConcernErrors can // cause the retry attempt to fail with writeErrors.