summaryrefslogtreecommitdiff
path: root/jstests/aggregation/bugs/server9444.js
blob: d8ab778168179615e19239ce9c0041b519cd5e50 (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
// server-9444 support disk storage of intermediate results in aggregation

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

var sharded = (typeof(RUNNING_IN_SHARDED_AGG_TEST) != 'undefined'); // see end of testshard1.js
if (sharded) {
    db.adminCommand( { shardcollection : t.getFullName(), key : { "_id" : 'hashed' } } );
}

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});
    assert.commandFailed(res);
    assert.eq(res.code, outOfMemoryCode);

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

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

    // ensure we work when allowDiskUse === true
    var 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: {$first: '$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: {$first: '$bigStr'}}}, {$sort: {_id:1}}], groupCode);
test([{$group: {_id: '$_id', bigStr: {$first: '$bigStr'}}}, {$sort: {_id:-1}}], groupCode);
test([{$group: {_id: '$_id', bigStr: {$first: '$bigStr'}}}, {$sort: {random:1}}], groupCode);
test([{$sort: {random:1}}, {$group: {_id: '$_id', bigStr: {$first: '$bigStr'}}}], sortCode);

var origDB = db;
if (sharded) {
    // Stop balancer first before dropping so there will be no contention on the ns lock.
    // It's alright to modify the global db variable since sharding tests never run in parallel.
    db = db.getSiblingDB('config');
    sh.stopBalancer();
}

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

if (sharded) {
    sh.startBalancer();
    db = origDB;
}