jstests/sharding/sample_timeseries.js
/**
 * This function verifies that $sample correctly obtains only documents in the input 'expectedDocs'
 * and ensures shards are targeted correctly. It does the following:
 *  1. Sample a single document from the collection and verify this targets both shards.
 *  2. Sample the given 'proportion' of documents and verify this targets both shards and uses the
 * specified plan.
 *  3. Sample the given 'proportion' of Dublin documents, which are all colocated on the primary and
 * ensure only the primary shard is targeted when we preface $sample with a $match.
 *  4. Sample the given 'proportion' of non-Dublin (Galway, Cork) documents, which can be found on
 * both shards, and ensure we target both shards.
 */
This function verifies that $sample correctly obtains only documents in the input 'expectedDocs'
and ensures shards are targeted correctly. It does the following:
1. Sample a single document from the collection and verify this targets both shards.
2. Sample the given 'proportion' of documents and verify this targets both shards and uses the
specified plan.
3. Sample the given 'proportion' of Dublin documents, which are all colocated on the primary and
ensure only the primary shard is targeted when we preface $sample with a $match.
4. Sample the given 'proportion' of non-Dublin (Galway, Cork) documents, which can be found on
both shards, and ensure we target both shards.
This function verifies that $sample correctly obtains only documents in the input ' expectedDocs ' and ensures shards are targeted correctly . It does the following : 1 . Sample a single document from the collection and verify this targets both shards . 2 . Sample the given ' proportion ' of documents and verify this targets both shards and uses the specified plan . 3 . Sample the given ' proportion ' of Dublin documents which are all colocated on the primary and ensure only the primary shard is targeted when we preface $sample with a $match . 4 . Sample the given ' proportion ' of non - Dublin ( Galway Cork ) documents which can be found on both shards and ensure we target both shards .
function runTest({proportion, expectedPlan, generateAdditionalData}) {
    const expectedDocs = setUpTestColl(generateAdditionalData);
    let expectedCount = Math.floor(proportion * Object.keys(expectedDocs).length);
    jsTestLog("Running test with proportion: " + proportion + ", expected count: " + expectedCount +
              ", expected plan: " + tojson(expectedPlan));
    let pipeline = [{$sample: {size: expectedCount}}, projection];
    testPipeline({pipeline, expectedDocs, expectedCount, shardsTargetedCount: 2, expectedPlan});
    expectedCount = 1;
    pipeline = [{$sample: {size: expectedCount}}, projection];
    testPipeline({pipeline, expectedDocs, expectedCount, shardsTargetedCount: 2});
    const dublinDocs = {};
    for (let key in expectedDocs) {
        const doc = expectedDocs[key];
        if (doc.city === "Dublin") {
            dublinDocs[key] = doc;
        }
    }
    const matchDublin = {$match: {"location.city": "Dublin"}};
    expectedCount = Math.floor(proportion * Object.keys(dublinDocs).length);
    pipeline = [matchDublin, {$sample: {size: expectedCount}}, projection];
    testPipeline({pipeline, expectedDocs: dublinDocs, expectedCount, shardsTargetedCount: 1});
    pipeline = [{$sample: {size: expectedCount}}, matchDublin, projection];
    testPipeline({pipeline, expectedDocs: dublinDocs, shardsTargetedCount: 2});
    const nonDublinDocs = {};
    for (let key in expectedDocs) {
        const doc = expectedDocs[key];
        if (doc.city !== "Dublin") {
            nonDublinDocs[key] = doc;
        }
    }
    const excludeDublin = {$match: {$expr: {$ne: ["$location.city", "Dublin"]}}};
    expectedCount = Math.floor(proportion * Object.keys(nonDublinDocs).length);
    pipeline = [excludeDublin, {$sample: {size: expectedCount}}, projection];
    testPipeline({pipeline, expectedDocs: nonDublinDocs, expectedCount, shardsTargetedCount: 2});
    pipeline = [{$sample: {size: expectedCount}}, excludeDublin, projection];
    testPipeline({pipeline, expectedDocs: nonDublinDocs, shardsTargetedCount: 2});
    assert(testColl.drop());
}
function runTest ( { proportion , expectedPlan , generateAdditionalData } ) { const expectedDocs = setUpTestColl ( generateAdditionalData ) ; let expectedCount = Math . floor ( proportion * Object . keys ( expectedDocs ) . length ) ; jsTestLog ( "Running test with proportion: " + proportion + ", expected count: " + expectedCount + ", expected plan: " + tojson ( expectedPlan ) ) ; let pipeline = [ { $sample : { size : expectedCount } } , projection ] ; testPipeline ( { pipeline , expectedDocs , expectedCount , shardsTargetedCount : 2 , expectedPlan } ) ; expectedCount = 1 ; pipeline = [ { $sample : { size : expectedCount } } , projection ] ; testPipeline ( { pipeline , expectedDocs , expectedCount , shardsTargetedCount : 2 } ) ; const dublinDocs = { } ; for ( let key in expectedDocs ) { const doc = expectedDocs [ key ] ; if ( doc . city === "Dublin" ) { dublinDocs [ key ] = doc ; } } const matchDublin = { $match : { "location.city" : "Dublin" } } ; expectedCount = Math . floor ( proportion * Object . keys ( dublinDocs ) . length ) ; pipeline = [ matchDublin , { $sample : { size : expectedCount } } , projection ] ; testPipeline ( { pipeline , expectedDocs : dublinDocs , expectedCount , shardsTargetedCount : 1 } ) ; pipeline = [ { $sample : { size : expectedCount } } , matchDublin , projection ] ; testPipeline ( { pipeline , expectedDocs : dublinDocs , shardsTargetedCount : 2 } ) ; const nonDublinDocs = { } ; for ( let key in expectedDocs ) { const doc = expectedDocs [ key ] ; if ( doc . city !== "Dublin" ) { nonDublinDocs [ key ] = doc ; } } const excludeDublin = { $match : { $expr : { $ne : [ "$location.city" , "Dublin" ] } } } ; expectedCount = Math . floor ( proportion * Object . keys ( nonDublinDocs ) . length ) ; pipeline = [ excludeDublin , { $sample : { size : expectedCount } } , projection ] ; testPipeline ( { pipeline , expectedDocs : nonDublinDocs , expectedCount , shardsTargetedCount : 2 } ) ; pipeline = [ { $sample : { size : expectedCount } } , excludeDublin , projection ] ; testPipeline ( { pipeline , expectedDocs : nonDublinDocs , shardsTargetedCount : 2 } ) ; assert ( testColl . drop ( ) ) ; }
This function verifies that $sample correctly obtains only documents in the input 'expectedDocs'
 and ensures shards are targeted correctly.
This function verifies that $sample correctly obtains only documents in the input ' expectedDocs ' and ensures shards are targeted correctly .
// Dublin documents are colocated on one shard, so we should only be targeting that shard. // If the $sample precedes the $match, however, we still need to target both shards. // Don't use an expected count here, since we are filtering for Dublin docs after sampling. // We should target both shards, since Cork and Galway are split across both shards. // Don't use an expected count here, since we are filtering for non-Dublin docs after sampling.