summaryrefslogtreecommitdiff
path: root/jstests/sharding/query/explain_exec_stats_on_shards.js
blob: 253b47a32389b47375224719f77e5dccb0f3dded (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
// Tests for the mongos explain command to ensure that the 'executionStats' section of the explain
// output is populated correctly for each shard.
(function() {
'use strict';

// Verifies that the explain output for the given shard contains all expected fields and that
// field values match the values specified in the 'expected*' arguments. This function also
// updates the 'totals' object which holds accumulated values for certain fields from each
// shard.
function verifyExecStatsOnShard({
    explain,
    expectedShardName,
    expectedNReturned,
    expectedKeysExamined,
    expectedDocsExamined,
    totals
}) {
    assert(explain.executionSuccess, tojson(explain));
    assert.eq(explain.shardName, expectedShardName, tojson(explain));
    assert.eq(explain.nReturned, expectedNReturned, tojson(explain));
    assert.gte(explain.executionTimeMillis, 0, tojson(explain));
    assert.eq(explain.totalKeysExamined, expectedKeysExamined, tojson(explain));
    assert.eq(explain.totalDocsExamined, expectedDocsExamined, tojson(explain));
    assert(explain.hasOwnProperty("executionStages"), tojson(explain));

    totals.nReturned += explain.nReturned;
    totals.executionTimeMillis += explain.executionTimeMillis;
    totals.keysExamined += explain.totalKeysExamined;
    totals.docsExamined += explain.totalDocsExamined;
}

// Create a cluster with 2 shards.
const numShards = 2;
const st = new ShardingTest({shards: numShards});
const db = st.s.getDB(`${jsTest.name()}_db`);

// Enable sharding on the database and use shard0 as the primary shard.
assert.commandWorked(db.adminCommand({enableSharding: db.getName()}));
st.ensurePrimaryShard(db.getName(), st.shard0.shardName);

// Test that the explain's 'executionStats' section includes all relevant fields for each shard
// when the 'explain' command is executed against a sharded collection.
(function testExplainExecutionStatsWithShardedCollection() {
    // Set up a collection, shard on {a:1}, split at {a:4}, and move the {a:4} chunk to shard1.
    const shardedColl = db.getCollection(`${jsTest.name()}_sharded`);
    shardedColl.drop();
    st.shardColl(shardedColl, {a: 1}, {a: 4}, {a: 4});

    // Put documents on each shard.
    const numDocs = 10;
    for (let i = 0; i < numDocs; i++) {
        assert.commandWorked(shardedColl.insert({_id: i, a: i}));
    }
    assert.eq(shardedColl.find().itcount(), numDocs);

    // Explain the find command and check that all expected explain fields are present in the
    // output.
    const explain = shardedColl.find({}).explain("executionStats");
    assert(explain.hasOwnProperty("executionStats"), tojson(explain));
    assert(explain.executionStats.hasOwnProperty("executionStages"), tojson(explain));
    assert(explain.executionStats.executionStages.hasOwnProperty("shards"), tojson(explain));
    assert.eq(explain.executionStats.executionStages.shards.length, numShards);

    // Verify execution stats on each shards and accumulate totals.
    const totals = {nReturned: 0, executionTimeMillis: 0, keysExamined: 0, docsExamined: 0};
    const executionStages = explain.executionStats.executionStages;
    // Sort the shards array by the 'shardName' to guarantee consistent results, as explain
    // outputs from the shards may arrive in an arbitrary order.
    executionStages.shards.sort((a, b) => a.shardName.localeCompare(b.shardName));
    verifyExecStatsOnShard({
        explain: executionStages.shards[0],
        expectedShardName: st.shard0.shardName,
        expectedNReturned: 4,
        expectedKeysExamined: 0,
        expectedDocsExamined: 4,
        totals: totals
    });
    verifyExecStatsOnShard({
        explain: executionStages.shards[1],
        expectedShardName: st.shard1.shardName,
        expectedNReturned: 6,
        expectedKeysExamined: 0,
        expectedDocsExamined: 6,
        totals: totals
    });

    // Ensure that execution stats accumulated across all shards matches the values in the
    // top-level 'executionStages' section of the explain output.
    assert.eq(executionStages.nReturned, totals.nReturned, tojson(explain));
    assert.eq(executionStages.totalChildMillis, totals.executionTimeMillis, tojson(explain));
    assert.eq(executionStages.totalKeysExamined, totals.keysExamined, tojson(explain));
    assert.eq(executionStages.totalDocsExamined, totals.docsExamined, tojson(explain));
})();

// Test that the explain's 'executionStats' section includes all relevant fields when the
// 'explain' command is executed against an unsharded collection.
(function testExplainExecutionStatsWithUnshardedCollection() {
    // Setup an unsharded collection.
    const unshardedColl = db.getCollection(`${jsTest.name()}_unsharded`);
    unshardedColl.drop();
    assert.commandWorked(unshardedColl.createIndex({a: 1}));

    // Add documents to the collection.
    const numDocs = 10;
    for (let i = 0; i < numDocs; i++) {
        assert.commandWorked(unshardedColl.insert({_id: i, a: i}));
    }
    assert.eq(unshardedColl.count(), numDocs);

    // Explain the find command and check that all expected explain fields are present in the
    // output.
    const explain = unshardedColl.find({}).explain("executionStats");
    assert(explain.hasOwnProperty("executionStats"), tojson(explain));
    assert(explain.executionStats.hasOwnProperty("executionStages"), tojson(explain));
    assert(explain.executionStats.executionStages.hasOwnProperty("shards"), tojson(explain));
    assert.eq(explain.executionStats.executionStages.shards.length, 1);

    // Verify execution stats on the primary shard and accumulate totals.
    const totals = {nReturned: 0, executionTimeMillis: 0, keysExamined: 0, docsExamined: 0};
    const executionStages = explain.executionStats.executionStages;
    verifyExecStatsOnShard({
        explain: executionStages.shards[0],
        expectedShardName: st.shard0.shardName,
        expectedNReturned: 10,
        expectedKeysExamined: 0,
        expectedDocsExamined: 10,
        totals: totals
    });

    // Ensure that execution stats on the primary shard matches the values in the top-level
    // 'executionStages' section of the explain output.
    assert.eq(executionStages.nReturned, totals.nReturned, tojson(explain));
    assert.eq(executionStages.totalChildMillis, totals.executionTimeMillis, tojson(explain));
    assert.eq(executionStages.totalKeysExamined, totals.keysExamined, tojson(explain));
    assert.eq(executionStages.totalDocsExamined, totals.docsExamined, tojson(explain));
})();

st.stop();
})();