summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/spill_to_disk_secondary_read.js
blob: 49b8e9f2aa99ee6f96cffb66b0f847358189e563 (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
/*
 * Test that $group and $setWindowFields spill to the WT RecordStore on secondaries with
 * writeConcern greater than w:1.
 * @tags: [requires_replication, requires_majority_read_concern]
 */
(function() {
"use strict";

const replTest = new ReplSetTest({
    nodes: 3,
});

replTest.startSet();
replTest.initiate();

// Test that spilling '$group' pipeline on a secondary works with a writeConcern greater than w:1.
let primary = replTest.getPrimary();
const insertColl = primary.getDB("test").foo;
for (let i = 0; i < 500; ++i) {
    assert.commandWorked(insertColl.insert({a: i, string: "test test test"}));
}

let secondary = replTest.getSecondary();
assert.commandWorked(secondary.adminCommand(
    {setParameter: 1, internalQuerySlotBasedExecutionHashAggApproxMemoryUseInBytesBeforeSpill: 1}));

const readColl = secondary.getDB("test").foo;

let pipeline = [{$group: {_id: '$a', s: {$addToSet: '$string'}, p: {$push: '$a'}}}];

let res =
    readColl
        .aggregate(
            pipeline,
            {allowDiskUse: true, readConcern: {level: "majority"}, writeConcern: {"w": "majority"}})
        .toArray();

insertColl.drop();

// Test that spilling '$setWindowFields' pipeline on a secondary works with a writeConcern greater
// than w:1.
let avgDocSize = 274;
let smallPartitionSize = 6;
let largePartitionSize = 21;
const insertCollWFs = primary.getDB("test").bar;

// Create small partition.
for (let i = 0; i < smallPartitionSize; i++) {
    assert.commandWorked(insertCollWFs.insert({_id: i, val: i, partition: 1}));
}
// Create large partition.
for (let i = 0; i < largePartitionSize; i++) {
    assert.commandWorked(insertCollWFs.insert({_id: i + smallPartitionSize, val: i, partition: 2}));
}

assert.commandWorked(secondary.adminCommand({
    setParameter: 1,
    internalDocumentSourceSetWindowFieldsMaxMemoryBytes: largePartitionSize * avgDocSize + 1
}));

const readCollWFs = secondary.getDB("test").bar;

pipeline = [
    {
        $setWindowFields: {
            partitionBy: "$partition",
            sortBy: {partition: 1},
            output: {arr: {$push: "$val", window: {documents: [-25, 25]}}}
        }
    },
    {$sort: {_id: 1}}
];

res =
    readCollWFs
        .aggregate(
            pipeline,
            {allowDiskUse: true, readConcern: {level: "majority"}, writeConcern: {"w": "majority"}})
        .toArray();

replTest.stopSet();
})();