summaryrefslogtreecommitdiff
path: root/jstests/sharding/query/pipeline_length_limit.js
blob: aad69742163f6f0c6b7ccb7e09025ee2b193c5a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
 * Confirms that the limit on number of aggregragation pipeline stages is respected.
 */
(function() {
"use strict";

load("jstests/libs/fixture_helpers.js");

function testLimits(testDB, lengthLimit) {
    let maxLength = lengthLimit;
    let tooLarge = lengthLimit + 1;

    assert.commandWorked(testDB.runCommand({
        aggregate: "test",
        cursor: {},
        pipeline: new Array(maxLength).fill({$project: {_id: 1}})
    }));
    assert.commandFailedWithCode(testDB.runCommand({
        aggregate: "test",
        cursor: {},
        pipeline: new Array(tooLarge).fill({$project: {_id: 1}})
    }),
                                 ErrorCodes.FailedToParse);
    testDB.setLogLevel(1);

    assert.commandWorked(testDB.runCommand({
        aggregate: "test",
        cursor: {},
        pipeline: [
            {$unionWith: {coll: "test", pipeline: new Array(maxLength).fill({$project: {_id: 1}})}}
        ]
    }));
    assert.commandFailedWithCode(testDB.runCommand({
        aggregate: "test",
        cursor: {},
        pipeline:
            [{$unionWith: {coll: "test", pipeline: new Array(tooLarge).fill({$project: {_id: 1}})}}]
    }),
                                 ErrorCodes.FailedToParse);

    assert.commandWorked(testDB.runCommand({
        aggregate: "test",
        cursor: {},
        pipeline: [{$facet: {foo: new Array(maxLength).fill({$project: {_id: 1}})}}]
    }));
    assert.commandFailedWithCode(testDB.runCommand({
        aggregate: "test",
        cursor: {},
        pipeline: [{$facet: {foo: new Array(tooLarge).fill({$project: {_id: 1}}), bar: []}}]
    }),
                                 ErrorCodes.FailedToParse);

    assert.commandWorked(testDB.runCommand(
        {update: "test", updates: [{q: {}, u: new Array(maxLength).fill({$project: {_id: 1}})}]}));
    assert.commandFailedWithCode(testDB.runCommand({
        update: "test",
        updates: [{q: {}, u: new Array(tooLarge).fill({$project: {_id: 1}})}]
    }),
                                 ErrorCodes.FailedToParse);

    const collname = "test";

    [  // Long pipelines with many unions.
        new Array(maxLength).fill({$unionWith: collname}),
        // maxLength * 2 total unions.
        new Array(maxLength).fill(
            {$unionWith: {coll: collname, pipeline: [{$unionWith: collname}]}}),
        // maxLength in subPipeline.
        [{
            $unionWith:
                {coll: collname, pipeline: new Array(maxLength).fill({$unionWith: collname})}
        }],
        // maxLength * 50 total unions, should be within max doc size.
        new Array(maxLength).fill(
            {$unionWith: {coll: collname, pipeline: new Array(50).fill({$unionWith: collname})}})]
        .forEach((pipeline) => {
            assert.commandWorked(testDB.runCommand({
                aggregate: collname,
                cursor: {},
                pipeline,
            }));
        });

    // Long pipelines filled with the same stage over and over.
    [{$addFields: {foo: 1}},
     {$bucketAuto: {groupBy: "$nonExistentField", buckets: 1}},
     {
         $graphLookup:
         {from: collname, startWith: "$_id", connectFromField: "_id", connectToField: "_id", as: "foo"}
     },
     {$group: {_id: "$_id"}},
     {$limit: 1},
     {$lookup: {from: collname, localField: "_id", foreignField: "_id", as: "foo"}},
     {$match: {_id: {$exists: true}}},
     {$project: {_id: 1}},
     {$redact: "$$KEEP"},
     {$replaceWith: "$$ROOT"},
     {$skip: 1},
     {$sort: {_id: 1}},
     // unionWith already covered.
     {$unwind: "$_id"}]
        .forEach((stage) => {
            assert.commandWorked(testDB.runCommand({
                aggregate: collname,
                cursor: {},
                pipeline: new Array(maxLength).fill(stage)
            }));
        });

    // Same test, but these to stages get replaced by 2 stages under the hood.
    [{$bucket: {groupBy: "$nonExistentField", boundaries: [0, 1], default: 2}},
     {$sortByCount: "$_id"}]
        .forEach((stage) => assert.commandWorked(testDB.runCommand({
            aggregate: collname,
            cursor: {},
            pipeline: new Array(parseInt(maxLength / 2)).fill(stage)
        })));

    // $lookup inserts a DocumentSourceSequentialDocumentCache stage in the subpipeline to perform
    // cacheing optimizations, so the subpipeline can have at most 'maxLength - 1' user-specified
    // stages. When we connect directly to a shard without mongos, it is treated as a standalone
    // and will not perform pipeline length validation after the cache stage is added.
    if (FixtureHelpers.isMongos(testDB)) {
        maxLength = maxLength - 1;
        tooLarge = tooLarge - 1;
    }

    assert.commandWorked(testDB.runCommand({
        aggregate: "test",
        cursor: {},
        pipeline: [{
            $lookup:
                {from: "test", as: "as", pipeline: new Array(maxLength).fill({$project: {_id: 1}})}
        }]
    }));
    assert.commandFailedWithCode(testDB.runCommand({
        aggregate: "test",
        cursor: {},
        pipeline: [{
            $lookup:
                {from: "test", as: "as", pipeline: new Array(tooLarge).fill({$project: {_id: 1}})}
        }]
    }),
                                 ErrorCodes.FailedToParse);
}

function runTest(lengthLimit, mongosConfig = {}, mongodConfig = {}) {
    const st = new ShardingTest(
        {shards: 2, rs: {nodes: 1}, other: {mongosOptions: mongosConfig, rsOptions: mongodConfig}});

    assert.commandWorked(st.s0.adminCommand({enablesharding: "test"}));
    assert.commandWorked(st.s0.adminCommand({shardCollection: "test.foo", key: {_id: "hashed"}}));

    let mongosDB = st.s0.getDB("test");
    assert.commandWorked(mongosDB.test.insert([{}, {}, {}, {}]));

    // Run test against mongos.
    testLimits(mongosDB, lengthLimit);

    // Run test against shard.
    let shard0DB = st.rs0.getPrimary().getDB("test");
    testLimits(shard0DB, lengthLimit);

    st.stop();
}

// This is a sanity check to make sure that the default value is correct. If the limit is changed,
// it will break for users and this check catches that.
const st = new ShardingTest({shards: 1, rs: {nodes: 1}});
let buildInfo = assert.commandWorked(st.s0.getDB("test").adminCommand("buildInfo"));
let pipelineLimit =
    assert.commandWorked(st.s0.adminCommand({"getParameter": 1, "internalPipelineLengthLimit": 1}));
let expectedPipelineLimit = buildInfo.debug ? 200 : 1000;
assert.eq(expectedPipelineLimit, pipelineLimit["internalPipelineLengthLimit"]);

const shardPrimary = st.rs0.getPrimary().getDB("test");
buildInfo = assert.commandWorked(shardPrimary.adminCommand("buildInfo"));
expectedPipelineLimit = buildInfo.debug ? 200 : 1000;
pipelineLimit = assert.commandWorked(
    shardPrimary.adminCommand({"getParameter": 1, "internalPipelineLengthLimit": 1}));
assert.eq(expectedPipelineLimit, pipelineLimit["internalPipelineLengthLimit"]);
st.stop();

// Test with modified pipeline length limit.
runTest(50,
        {setParameter: {internalPipelineLengthLimit: 50}},
        {setParameter: {internalPipelineLengthLimit: 50}});
})();