summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/views_count_distinct_disk_use.js
blob: cd8d3046637c2681fe19b116cb2c9b2250fed2ae (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
// Test count and distinct on views use with different values of the allowDiskUseByDefault
// parameter.

(function() {
"use strict";

load("jstests/libs/sbe_util.js");  // For checkSBEEnabled.

const conn = MongoRunner.runMongod();
assert.neq(null, conn, "mongod was unable to start up");

const viewsDB = conn.getDB(jsTestName());
viewsDB.largeColl.drop();

const memoryLimitMb = 1;
const largeStr = "A".repeat(1024 * 1024);  // 1MB string

// Create a collection exceeding the memory limit.
for (let i = 0; i < memoryLimitMb + 1; ++i)
    assert.commandWorked(viewsDB.largeColl.insert({x: i, largeStr: largeStr}));

viewsDB.largeView.drop();
assert.commandWorked(viewsDB.createView("largeView", "largeColl", [{$sort: {x: -1}}]));

function testDiskUse(cmd) {
    assert.commandWorked(viewsDB.adminCommand({setParameter: 1, allowDiskUseByDefault: false}));
    assert.commandFailedWithCode(viewsDB.runCommand(cmd),
                                 ErrorCodes.QueryExceededMemoryLimitNoDiskUseAllowed);

    assert.commandWorked(viewsDB.adminCommand({setParameter: 1, allowDiskUseByDefault: true}));
    assert.commandWorked(viewsDB.runCommand(cmd));
}

// The 'count' command executes the view definition pipeline containing the '$sort' stage. This
// stage needs to spill to disk if the memory limit is reached.
assert.commandWorked(viewsDB.adminCommand(
    {setParameter: 1, internalQueryMaxBlockingSortMemoryUsageBytes: memoryLimitMb * 1024 * 1024}));

// In SBE the $sort will not cause spilling because it's only the integers being sorted on.
if (!checkSBEEnabled(viewsDB)) {
    testDiskUse({count: "largeView"});
}

// The 'distinct' command executes the view definition pipeline containing the '$sort' stage. This
// stage needs to spill to disk if the memory limit is reached.
testDiskUse({distinct: "largeView", key: "largeStr"});

MongoRunner.stopMongod(conn);
})();