summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/lookup_metrics.js
blob: 9f2aec0bfdb7d03bc1d29579c02a84eca0c1b866 (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
/**
 * Tests that the lookup metrics are recorded correctly in serverStatus.
 */

(function() {
"use strict";

load("jstests/libs/sbe_util.js");      // For 'checkSBEEnabled()'.
load("jstests/libs/analyze_plan.js");  // For 'getAggPlanStages' and other explain helpers.

const conn = MongoRunner.runMongod({setParameter: {allowDiskUseByDefault: true}});
assert.neq(null, conn, "mongod was unable to start up");

const db = conn.getDB(jsTestName());

if (!checkSBEEnabled(db)) {
    jsTestLog("Skipping test because either the sbe lookup pushdown feature flag is disabled or" +
              " sbe itself is disabled");
    MongoRunner.stopMongod(conn);
    return;
}

assert.commandWorked(db.dropDatabase());

// Set up the database.
assert.commandWorked(db.students.insertMany([
    {sID: 22001, name: "Alex", year: 1, score: 4.0},
    {sID: 21001, name: "Bernie", year: 2, score: 3.7},
    {sID: 20010, name: "Chris", year: 3, score: 2.5},
    {sID: 22021, name: "Drew", year: 1, score: 3.2},
    {sID: 17301, name: "Harley", year: 6, score: 3.1},
    {sID: 21022, name: "Farmer", year: 1, score: 2.2},
    {sID: 20020, name: "George", year: 3, score: 2.8},
    {sID: 18020, name: "Harley", year: 5, score: 2.8},
]));

assert.commandWorked(db.createView("firstYears", "students", [{$match: {year: 1}}]));

assert.commandWorked(db.people.insertMany([
    {pID: 1000, name: "Alex"},
    {pID: 1001, name: "Drew"},
    {pID: 1002, name: "Justin"},
    {pID: 1003, name: "Parker"},
]));

const lookupStrategy = {
    nestedLoopJoin: "nestedLoopJoin",
    indexedLoopJoin: "indexedLoopJoin",
    hashLookup: "hashLookup",
    nonSbe: "nonSbe"
};

// Create an object with the correct lookup counter values after the specified type of query.
function generateExpectedCounters(joinStrategy = lookupStrategy.nonSbe, spillToDisk = 0) {
    let counters = db.serverStatus().metrics.query.lookup;
    assert(counters, "counters did not exist");
    let expected = Object.assign(counters);
    switch (joinStrategy) {
        case lookupStrategy.nestedLoopJoin:
            expected.nestedLoopJoin = NumberLong(expected.nestedLoopJoin + 1);
            break;
        case lookupStrategy.indexedLoopJoin:
            expected.indexedLoopJoin = NumberLong(expected.indexedLoopJoin + 1);
            break;
        case lookupStrategy.hashLookup:
            expected.hashLookup = NumberLong(expected.hashLookup + 1);
            expected.hashLookupSpillToDisk =
                NumberLong(expected.hashLookupSpillToDisk + spillToDisk);
            break;
    }
    return expected;
}

// Compare the values of the lookup counters to an object that represents the expected values.
function compareLookupCounters(expectedCounters) {
    let counters = db.serverStatus().metrics.query.lookup;
    assert.docEq(expectedCounters, counters);
}

// Run a lookup pipeline that does not get pushed down to SBE because it's querying against a view.
let expectedCounters = generateExpectedCounters();
assert.eq(
    db.people
        .aggregate([
            {$lookup: {from: "firstYears", localField: "name", foreignField: "name", as: "matches"}}
        ])
        .itcount(),
    4 /* Matching results */);
compareLookupCounters(expectedCounters);

// Run a lookup pipeline with a hash lookup that gets pushed down to SBE.
expectedCounters = generateExpectedCounters(lookupStrategy.hashLookup);
assert.eq(
    db.people
        .aggregate([
            {$lookup: {from: "students", localField: "name", foreignField: "name", as: "matches"}}
        ])
        .itcount(),
    4 /* Matching results */);
compareLookupCounters(expectedCounters);

// Run a lookup pipeline without disk use so that it will use NLJ.
expectedCounters = generateExpectedCounters(lookupStrategy.nestedLoopJoin);
assert.eq(
    db.people
        .aggregate(
            [{
                $lookup: {from: "students", localField: "name", foreignField: "name", as: "matches"}
            }],
            {allowDiskUse: false})
        .itcount(),
    4 /* Matching results */);
compareLookupCounters(expectedCounters);

// Create an index for the foreign collection so the query uses INLJ.
assert.commandWorked(db["students"].createIndex({name: 1}));
expectedCounters = generateExpectedCounters(lookupStrategy.indexedLoopJoin);
assert.eq(
    db.people
        .aggregate([
            {$lookup: {from: "students", localField: "name", foreignField: "name", as: "matches"}}
        ])
        .itcount(),
    4 /* Matching results */);
compareLookupCounters(expectedCounters);

assert.commandWorked(db["students"].dropIndexes());

// Reduce the threshold for spilling with hash lookup and then run a query that will spill.
assert.commandWorked(db.adminCommand({
    setParameter: 1,
    internalQuerySlotBasedExecutionHashLookupApproxMemoryUseInBytesBeforeSpill: 1,
}));
expectedCounters = generateExpectedCounters(lookupStrategy.hashLookup,
                                            16 /* 2 spills per foreign collection row */);
assert.eq(
    db.people
        .aggregate([
            {$lookup: {from: "students", localField: "name", foreignField: "name", as: "matches"}}
        ])
        .itcount(),
    4 /* Matching results */);
compareLookupCounters(expectedCounters);

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