summaryrefslogtreecommitdiff
path: root/jstests/aggregation/bugs/server9444.js
blob: f3c6a449fad4c0410130b8bb9711b7f91836f825 (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
// server-9444 support disk storage of intermediate results in aggregation
(function() {
    'use strict';

    load('jstests/libs/fixture_helpers.js');  // For 'FixtureHelpers'

    const t = db.server9444;
    t.drop();

    const sharded = FixtureHelpers.isSharded(t);

    var memoryLimitMB = sharded ? 200 : 100;

    function loadData() {
        var bigStr = Array(1024 * 1024 + 1).toString();  // 1MB of ','
        for (var i = 0; i < memoryLimitMB + 1; i++)
            t.insert({_id: i, bigStr: i + bigStr, random: Math.random()});

        assert.gt(t.stats().size, memoryLimitMB * 1024 * 1024);
    }
    loadData();

    function test(pipeline, outOfMemoryCode) {
        // ensure by default we error out if exceeding memory limit
        var res = t.runCommand('aggregate', {pipeline: pipeline, cursor: {}});
        assert.commandFailed(res);
        assert.eq(res.code, outOfMemoryCode);

        // ensure allowDiskUse: false does what it says
        res = t.runCommand('aggregate', {pipeline: pipeline, cursor: {}, allowDiskUse: false});
        assert.commandFailed(res);
        assert.eq(res.code, outOfMemoryCode);

        // allowDiskUse only supports bool. In particular, numbers aren't allowed.
        res = t.runCommand('aggregate', {pipeline: pipeline, cursor: {}, allowDiskUse: 1});
        assert.commandFailed(res);

        // ensure we work when allowDiskUse === true
        res = t.aggregate(pipeline, {allowDiskUse: true});
        assert.eq(res.itcount(), t.count());  // all tests output one doc per input doc
    }

    var groupCode = 16945;
    var sortCode = 16819;
    var sortLimitCode = 16820;

    test([{$group: {_id: '$_id', bigStr: {$min: '$bigStr'}}}], groupCode);

    // sorting with _id would use index which doesn't require extsort
    test([{$sort: {random: 1}}], sortCode);
    test([{$sort: {bigStr: 1}}], sortCode);  // big key and value

    // make sure sort + large limit won't crash the server (SERVER-10136)
    test([{$sort: {bigStr: 1}}, {$limit: 1000 * 1000 * 1000}], sortLimitCode);

    // test combining two extSorts in both same and different orders
    test([{$group: {_id: '$_id', bigStr: {$min: '$bigStr'}}}, {$sort: {_id: 1}}], groupCode);
    test([{$group: {_id: '$_id', bigStr: {$min: '$bigStr'}}}, {$sort: {_id: -1}}], groupCode);
    test([{$group: {_id: '$_id', bigStr: {$min: '$bigStr'}}}, {$sort: {random: 1}}], groupCode);
    test([{$sort: {random: 1}}, {$group: {_id: '$_id', bigStr: {$first: '$bigStr'}}}], sortCode);

    // don't leave large collection laying around
    t.drop();
})();