2018-07-04 00:58:47 +02:00
|
|
|
// Tests that pipeline optimization works properly when the failpoint isn't triggered, and is
|
|
|
|
// disabled properly when it is triggered.
|
|
|
|
(function() {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
load("jstests/libs/analyze_plan.js"); // For aggPlan functions.
|
2018-07-30 21:08:06 +02:00
|
|
|
Random.setRandomSeed();
|
2018-07-04 00:58:47 +02:00
|
|
|
|
|
|
|
const conn = MongoRunner.runMongod({});
|
2018-07-30 21:08:06 +02:00
|
|
|
assert.neq(conn, null, "Mongod failed to start up.");
|
2018-07-04 00:58:47 +02:00
|
|
|
const testDb = conn.getDB("test");
|
|
|
|
const coll = testDb.agg_opt;
|
|
|
|
|
2018-07-30 21:08:06 +02:00
|
|
|
const pops = new Set();
|
2018-07-04 00:58:47 +02:00
|
|
|
for (let i = 0; i < 25; ++i) {
|
2018-07-30 21:08:06 +02:00
|
|
|
let pop;
|
|
|
|
do {
|
|
|
|
pop = Random.randInt(100000);
|
|
|
|
} while (pops.has(pop));
|
2018-09-28 16:36:01 +02:00
|
|
|
pops.add(pop);
|
2018-07-30 21:08:06 +02:00
|
|
|
|
|
|
|
assert.commandWorked(coll.insert({_id: i, city: "Cleveland", pop: pop, state: "OH"}));
|
2018-07-04 00:58:47 +02:00
|
|
|
}
|
|
|
|
|
2018-07-30 21:08:06 +02:00
|
|
|
const pipeline = [{$match: {state: "OH"}}, {$sort: {pop: -1}}, {$limit: 10}];
|
|
|
|
|
2018-07-04 00:58:47 +02:00
|
|
|
const enabledPlan = coll.explain().aggregate(pipeline);
|
|
|
|
// Test that sort and the limit were combined.
|
|
|
|
assert.eq(aggPlanHasStage(enabledPlan, "$limit"), false);
|
|
|
|
assert.eq(aggPlanHasStage(enabledPlan, "$sort"), true);
|
|
|
|
assert.eq(enabledPlan.stages.length, 2);
|
|
|
|
|
2018-07-30 21:08:06 +02:00
|
|
|
const enabledResult = coll.aggregate(pipeline).toArray();
|
2018-07-04 00:58:47 +02:00
|
|
|
|
2018-07-30 21:08:06 +02:00
|
|
|
// Enable a failpoint that will cause pipeline optimizations to be skipped.
|
2018-07-04 00:58:47 +02:00
|
|
|
assert.commandWorked(
|
|
|
|
testDb.adminCommand({configureFailPoint: "disablePipelineOptimization", mode: "alwaysOn"}));
|
|
|
|
|
|
|
|
const disabledPlan = coll.explain().aggregate(pipeline);
|
|
|
|
// Test that the $limit still exists and hasn't been optimized away.
|
|
|
|
assert.eq(aggPlanHasStage(disabledPlan, "$limit"), true);
|
|
|
|
assert.eq(aggPlanHasStage(disabledPlan, "$sort"), true);
|
|
|
|
assert.eq(disabledPlan.stages.length, 3);
|
|
|
|
|
2018-07-30 21:08:06 +02:00
|
|
|
const disabledResult = coll.aggregate(pipeline).toArray();
|
2018-07-04 00:58:47 +02:00
|
|
|
|
|
|
|
// Test that the result is the same with and without optimizations enabled.
|
|
|
|
assert.eq(enabledResult, disabledResult);
|
|
|
|
|
|
|
|
MongoRunner.stopMongod(conn);
|
|
|
|
}());
|