summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/read_only_allow_disk_use.js
blob: f3be69cbd4b16904458533f30a33de21ce5505db (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
/**
 * Test that spilling to disk is prohibited in queryable backup (read-only) mode and when the
 * parameter 'recoverFromOplogAsStandalone' is set to 'true'.
 *
 * The 'requires_persistence' tag exludes the test from running with 'inMemory' and
 * 'ephemeralForTest' storage engines that do not support queryable backup (read-only) mode.
 * @tags: [
 *   requires_persistence,
 *   requires_replication
 * ]
 */

(function() {
"use strict";

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

function prepareData(conn) {
    const testDb = conn.getDB(jsTestName());
    testDb.largeColl.drop();
    // Create a collection exceeding the memory limit.
    for (let i = 0; i < memoryLimitMb + 1; ++i)
        assert.commandWorked(testDb.largeColl.insert({x: i, largeStr: largeStr + i}));
    // Create a view on a large collection containg a sort operation.
    testDb.largeView.drop();
    assert.commandWorked(testDb.createView("largeView", "largeColl", [{$sort: {x: -1}}]));
}

function runTest(conn, allowDiskUseByDefault) {
    const testDb = conn.getDB(jsTestName());
    const coll = testDb.largeColl;
    const view = testDb.largeView;

    function assertFailed(cmdObject) {
        assert.commandFailedWithCode(testDb.runCommand(cmdObject),
                                     ErrorCodes.QueryExceededMemoryLimitNoDiskUseAllowed);
    }

    assert.commandWorked(
        testDb.adminCommand({setParameter: 1, allowDiskUseByDefault: allowDiskUseByDefault}));

    // The 'aggregate' pipelines running within the memory limit must pass.
    assert.eq(1, coll.aggregate([{$match: {x: 1}}]).itcount());
    assert.eq(1,
              coll.aggregate([{$match: {x: 1}}], {allowDiskUse: !allowDiskUseByDefault}).itcount());

    // The 'aggregate' grouping exceeding the memory limit must fail.
    assertFailed({
        aggregate: coll.getName(),
        pipeline: [{$group: {_id: '$largeStr', minId: {$min: '$_id'}}}],
        cursor: {}
    });
    assertFailed({
        aggregate: coll.getName(),
        pipeline: [{$group: {_id: '$largeStr', minId: {$min: '$_id'}}}],
        cursor: {},
        allowDiskUse: !allowDiskUseByDefault
    });

    // The 'aggregate' sort exceeding the memory limit must fail.
    assertFailed({aggregate: coll.getName(), pipeline: [{$sort: {x: -1}}], cursor: {}});
    assertFailed({
        aggregate: coll.getName(),
        pipeline: [{$sort: {x: -1}}],
        cursor: {},
        allowDiskUse: !allowDiskUseByDefault
    });

    // The 'find' queries within the memory limit must pass.
    assert.eq(1, coll.find({x: 1}).itcount());
    assert.eq(1, coll.find({x: 1}).allowDiskUse(!allowDiskUseByDefault).itcount());

    // The 'find' and sort queries exceeding the memory limit must fail.
    assertFailed({find: coll.getName(), sort: {x: -1}});
    assertFailed({find: coll.getName(), sort: {x: -1}, allowDiskUse: !allowDiskUseByDefault});

    // The 'mapReduce' command running within the memory limit must pass.
    assert.eq(coll.mapReduce(
                      function() {
                          emit("x", 1);
                      },
                      function(k, v) {
                          return Array.sum(v);
                      },
                      {out: {inline: 1}})
                  .results[0]
                  .value,
              memoryLimitMb + 1);

    // The 'mapReduce' command exceeding the memory limit must fail.
    assertFailed({
        mapReduce: coll.getName(),
        map: function() {
            emit("x", this.largeStr);
        },
        reduce: function(k, v) {
            return 42;
        },
        out: {inline: 1}
    });

    // The 'count' command within the memory limit must pass.
    assert.eq(coll.count(), memoryLimitMb + 1);

    // The 'count' command exceeding the memory limit must fail.
    assertFailed({count: view.getName()});

    // The 'distinct' command within the memory limit must pass.
    assert.eq(coll.distinct("x"), [0, 1]);

    // The 'distinct' command exceeding the memory limit must fail.
    assertFailed({distinct: view.getName(), key: "largeStr"});
}

// Create a replica set with just one node and add some data.
const rst = new ReplSetTest({nodes: 1});
rst.startSet();
rst.initiate();
const primary = rst.getPrimary();
prepareData(primary);
rst.stopSet(/*signal=*/null, /*forRestart=*/true);

const tightMemoryLimits = {
    internalDocumentSourceGroupMaxMemoryBytes: memoryLimitBytes,
    internalQueryMaxBlockingSortMemoryUsageBytes: memoryLimitBytes,
    internalQuerySlotBasedExecutionHashAggApproxMemoryUseInBytesBeforeSpill: memoryLimitBytes
};

// Start the mongod in a queryable backup mode with the existing dbpath.
const connQueryableBackup = MongoRunner.runMongod({
    dbpath: primary.dbpath,
    noCleanData: true,
    queryableBackupMode: "",
    setParameter: tightMemoryLimits
});
assert.neq(null, connQueryableBackup, "mongod was unable to start up");

runTest(connQueryableBackup, true);
runTest(connQueryableBackup, false);

MongoRunner.stopMongod(connQueryableBackup);

// Recover the mongod from oplog as a standalone with the existing dbpath.
const connRecoverStandalone = MongoRunner.runMongod({
    dbpath: primary.dbpath,
    noCleanData: true,
    setParameter: Object.assign(tightMemoryLimits, {recoverFromOplogAsStandalone: true})
});
assert.neq(null, connRecoverStandalone, "mongod was unable to start up");

runTest(connRecoverStandalone, true);
runTest(connRecoverStandalone, false);

MongoRunner.stopMongod(connRecoverStandalone);
})();