jstests/sharding/query/agg_mongos_merge.js
/**
 * Runs a series of test cases which will consistently merge on mongoS or mongoD regardless of
 * whether 'allowDiskUse' is true, false or omitted.
 */
Runs a series of test cases which will consistently merge on mongoS or mongoD regardless of
whether 'allowDiskUse' is true, false or omitted.
Runs a series of test cases which will consistently merge on mongoS or mongoD regardless of whether ' allowDiskUse ' is true false or omitted .
function runTestCasesWhoseMergeLocationIsConsistentRegardlessOfAllowDiskUse(allowDiskUse) {
    assertMergeOnMongoS({
        testName: "agg_mongos_merge_match_only",
        pipeline: [{$match: {_id: {$gte: -200, $lte: 200}}}],
        allowDiskUse: allowDiskUse,
        expectedCount: 400
    });
    assertMergeOnMongoS({
        testName: "agg_mongos_merge_sort_presorted",
        pipeline: [{$match: {_id: {$gte: -200, $lte: 200}}}, {$sort: {_id: -1}}],
        allowDiskUse: allowDiskUse,
        expectedCount: 400
    });
    assertMergeOnMongoS({
        testName: "agg_mongos_merge_skip",
        pipeline: [{$match: {_id: {$gte: -200, $lte: 200}}}, {$sort: {_id: -1}}, {$skip: 300}],
        allowDiskUse: allowDiskUse,
        expectedCount: 100
    });
    assertMergeOnMongoS({
        testName: "agg_mongos_merge_limit",
        pipeline: [{$match: {_id: {$gte: -200, $lte: 200}}}, {$limit: 300}],
        allowDiskUse: allowDiskUse,
        expectedCount: 300
    });
    assertMergeOnMongoS({
        testName: "agg_mongos_merge_sample_splitpoint",
        pipeline: [{$match: {_id: {$gte: -200, $lte: 200}}}, {$sample: {size: 300}}],
        allowDiskUse: allowDiskUse,
        expectedCount: 300
    });
    assertMergeOnMongoS({
        testName: "agg_mongos_merge_geo_near",
        pipeline:
            [{$geoNear: {near: [0, 0], distanceField: "distance", spherical: true}}, {$limit: 300}],
        allowDiskUse: allowDiskUse,
        expectedCount: 300
    });
    assertMergeOnMongoS({
        testName: "agg_mongos_merge_facet_all_pipes_eligible_for_mongos",
        pipeline: [
            {$match: {_id: {$gte: -200, $lte: 200}}},
            {
                $facet: {
                    pipe1: [{$match: {_id: {$gt: 0}}}, {$skip: 10}, {$limit: 150}],
                    pipe2: [{$match: {_id: {$lt: 0}}}, {$project: {_id: 0, a: 1}}]
                }
            }
        ],
        allowDiskUse: allowDiskUse,
        expectedCount: 1
    });
    assertMergeOnMongoD({
            testName: "agg_mongos_merge_facet_pipe_needs_primary_shard_disk_use_" + allowDiskUse,
            pipeline: [
                {$match: {_id: {$gte: -200, $lte: 200}}},
                {
                  $facet: {
                      pipe1: [{$match: {_id: {$gt: 0}}}, {$skip: 10}, {$limit: 150}],
                      pipe2: [
                          {$match: {_id: {$lt: 0}}},
                          {
                            $lookup: {
                                from: unshardedColl.getName(),
                                localField: "_id",
                                foreignField: "_id",
                                as: "lookupField"
                            }
                          }
                      ]
                  }
                }
            ],
            mergeType: "primaryShard",
            allowDiskUse: allowDiskUse,
            expectedCount: 1
        });
    assertMergeOnMongoS({
        testName: "agg_mongos_merge_all_mongos_runnable_skip_and_limit_stages",
        pipeline: [
            {$match: {_id: {$gte: -200, $lte: 200}}},
            {$sort: {_id: -1}},
            {$skip: 150},
            {$limit: 150},
            {$skip: 5},
            {$limit: 1},
        ],
        allowDiskUse: allowDiskUse,
        expectedCount: 1
    });
    assertMergeOnMongoD({
        testName: "agg_mongos_merge_primary_shard_disk_use_" + allowDiskUse,
        pipeline: [
            {$match: {_id: {$gte: -200, $lte: 200}}},
            {$_internalSplitPipeline: {mergeType: "anyShard"}}
        ],
        mergeType: "anyShard",
        allowDiskUse: allowDiskUse,
        expectedCount: 400
    });
    if (!isShardedLookupEnabled) {
        assertMergeOnMongoD({
            testName: "agg_mongos_merge_lookup_unsharded_disk_use_" + allowDiskUse,
            pipeline: [
                {$match: {_id: {$gte: -200, $lte: 200}}},
                {
                    $lookup: {
                        from: unshardedColl.getName(),
                        localField: "_id",
                        foreignField: "_id",
                        as: "lookupField"
                    }
                }
            ],
            mergeType: "primaryShard",
            allowDiskUse: allowDiskUse,
            expectedCount: 400
        });
    } else {
        assertMergeOnMongoD({
            testName: "agg_mongos_merge_lookup_unsharded_flag_on_disk_use_" + allowDiskUse,
            pipeline: [
                {$match: {_id: {$gte: -200, $lte: 200}}},
                {$_internalSplitPipeline: {}},
                {
                $lookup: {
                    from: unshardedColl.getName(),
                    localField: "_id",
                    foreignField: "_id",
                    as: "lookupField"
                }
                }
            ],
            mergeType: "primaryShard",
            allowDiskUse: allowDiskUse,
            expectedCount: 400
        });
        assertMergeOnMongoS({
            testName: "agg_mongos_merge_lookup_sharded_flag_on_disk_use_" + allowDiskUse,
            pipeline: [
                {$match: {_id: {$gte: -200, $lte: 200}}},
                {$_internalSplitPipeline: {}},
                {
                $lookup: {
                    from: mongosColl.getName(),
                    localField: "_id",
                    foreignField: "_id",
                    as: "lookupField"
                }
                }
            ],
            mergeType: "mongos",
            allowDiskUse: allowDiskUse,
            expectedCount: 400
        });
    }
}
function runTestCasesWhoseMergeLocationIsConsistentRegardlessOfAllowDiskUse ( allowDiskUse ) { assertMergeOnMongoS ( { testName : "agg_mongos_merge_match_only" , pipeline : [ { $match : { _id : { $gte : - 200 , $lte : 200 } } } ] , allowDiskUse : allowDiskUse , expectedCount : 400 } ) ; assertMergeOnMongoS ( { testName : "agg_mongos_merge_sort_presorted" , pipeline : [ { $match : { _id : { $gte : - 200 , $lte : 200 } } } , { $sort : { _id : - 1 } } ] , allowDiskUse : allowDiskUse , expectedCount : 400 } ) ; assertMergeOnMongoS ( { testName : "agg_mongos_merge_skip" , pipeline : [ { $match : { _id : { $gte : - 200 , $lte : 200 } } } , { $sort : { _id : - 1 } } , { $skip : 300 } ] , allowDiskUse : allowDiskUse , expectedCount : 100 } ) ; assertMergeOnMongoS ( { testName : "agg_mongos_merge_limit" , pipeline : [ { $match : { _id : { $gte : - 200 , $lte : 200 } } } , { $limit : 300 } ] , allowDiskUse : allowDiskUse , expectedCount : 300 } ) ; assertMergeOnMongoS ( { testName : "agg_mongos_merge_sample_splitpoint" , pipeline : [ { $match : { _id : { $gte : - 200 , $lte : 200 } } } , { $sample : { size : 300 } } ] , allowDiskUse : allowDiskUse , expectedCount : 300 } ) ; assertMergeOnMongoS ( { testName : "agg_mongos_merge_geo_near" , pipeline : [ { $geoNear : { near : [ 0 , 0 ] , distanceField : "distance" , spherical : true } } , { $limit : 300 } ] , allowDiskUse : allowDiskUse , expectedCount : 300 } ) ; assertMergeOnMongoS ( { testName : "agg_mongos_merge_facet_all_pipes_eligible_for_mongos" , pipeline : [ { $match : { _id : { $gte : - 200 , $lte : 200 } } } , { $facet : { pipe1 : [ { $match : { _id : { $gt : 0 } } } , { $skip : 10 } , { $limit : 150 } ] , pipe2 : [ { $match : { _id : { $lt : 0 } } } , { $project : { _id : 0 , a : 1 } } ] } } ] , allowDiskUse : allowDiskUse , expectedCount : 1 } ) ; assertMergeOnMongoD ( { testName : "agg_mongos_merge_facet_pipe_needs_primary_shard_disk_use_" + allowDiskUse , pipeline : [ { $match : { _id : { $gte : - 200 , $lte : 200 } } } , { $facet : { pipe1 : [ { $match : { _id : { $gt : 0 } } } , { $skip : 10 } , { $limit : 150 } ] , pipe2 : [ { $match : { _id : { $lt : 0 } } } , { $lookup : { from : unshardedColl . getName ( ) , localField : "_id" , foreignField : "_id" , as : "lookupField" } } ] } } ] , mergeType : "primaryShard" , allowDiskUse : allowDiskUse , expectedCount : 1 } ) ; assertMergeOnMongoS ( { testName : "agg_mongos_merge_all_mongos_runnable_skip_and_limit_stages" , pipeline : [ { $match : { _id : { $gte : - 200 , $lte : 200 } } } , { $sort : { _id : - 1 } } , { $skip : 150 } , { $limit : 150 } , { $skip : 5 } , { $limit : 1 } , ] , allowDiskUse : allowDiskUse , expectedCount : 1 } ) ; assertMergeOnMongoD ( { testName : "agg_mongos_merge_primary_shard_disk_use_" + allowDiskUse , pipeline : [ { $match : { _id : { $gte : - 200 , $lte : 200 } } } , { $_internalSplitPipeline : { mergeType : "anyShard" } } ] , mergeType : "anyShard" , allowDiskUse : allowDiskUse , expectedCount : 400 } ) ; if ( ! isShardedLookupEnabled ) { assertMergeOnMongoD ( { testName : "agg_mongos_merge_lookup_unsharded_disk_use_" + allowDiskUse , pipeline : [ { $match : { _id : { $gte : - 200 , $lte : 200 } } } , { $lookup : { from : unshardedColl . getName ( ) , localField : "_id" , foreignField : "_id" , as : "lookupField" } } ] , mergeType : "primaryShard" , allowDiskUse : allowDiskUse , expectedCount : 400 } ) ; } else { assertMergeOnMongoD ( { testName : "agg_mongos_merge_lookup_unsharded_flag_on_disk_use_" + allowDiskUse , pipeline : [ { $match : { _id : { $gte : - 200 , $lte : 200 } } } , { $_internalSplitPipeline : { } } , { $lookup : { from : unshardedColl . getName ( ) , localField : "_id" , foreignField : "_id" , as : "lookupField" } } ] , mergeType : "primaryShard" , allowDiskUse : allowDiskUse , expectedCount : 400 } ) ; assertMergeOnMongoS ( { testName : "agg_mongos_merge_lookup_sharded_flag_on_disk_use_" + allowDiskUse , pipeline : [ { $match : { _id : { $gte : - 200 , $lte : 200 } } } , { $_internalSplitPipeline : { } } , { $lookup : { from : mongosColl . getName ( ) , localField : "_id" , foreignField : "_id" , as : "lookupField" } } ] , mergeType : "mongos" , allowDiskUse : allowDiskUse , expectedCount : 400 } ) ; } }
Runs a series of test cases which will consistently merge on mongoS or mongoD regardless of
 whether 'allowDiskUse' is true, false or omitted.
Runs a series of test cases which will consistently merge on mongoS or mongoD regardless of whether ' allowDiskUse ' is true false or omitted .
// Test that a $match pipeline with an empty merge stage is merged on mongoS. // Test that a $sort stage which merges pre-sorted streams is run on mongoS. // Test that $skip is merged on mongoS. // Test that $limit is merged on mongoS. // Test that $sample is merged on mongoS if it is the splitpoint, since this will result in // a merging $sort of presorted streams in the merge pipeline. // Test that $geoNear is merged on mongoS. // Test that $facet is merged on mongoS if all pipelines are mongoS-mergeable regardless of // 'allowDiskUse'. // Test that $facet is merged on mongoD if any pipeline requires a primary shard merge, // regardless of 'allowDiskUse'. // Test that a pipeline whose merging half can be run on mongos using only the mongos // execution machinery returns the correct results. // TODO SERVER-30882 Find a way to assert that all stages get absorbed by mongos. // Test that a merge pipeline which needs to run on a shard is NOT merged on mongoS // regardless of 'allowDiskUse'. // Test that $lookup is merged on the primary shard when the foreign collection is // unsharded. // Test that equality $lookup continues to be merged on the primary shard when the foreign // collection is unsharded. // $lookup is now allowed on the shards pipeline, but we should force it to be part // of the merge pipeline for this test. // Test that equality $lookup is merged on mongoS when the foreign collection is sharded. // $lookup is now allowed on the shards pipeline, but we should force it to be part // of the merge pipeline for this test.