summaryrefslogtreecommitdiff
path: root/jstests/sharding/analyze_shard_key/sample_write_queries_unsharded.js
blob: ba9ff505babece5954f9fdd6d5a2ad550f05229a (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
 * Tests basic support for sampling write queries against an unsharded collection on a sharded
 * cluster.
 *
 * @tags: [requires_fcv_63, featureFlagAnalyzeShardKey]
 */
(function() {
"use strict";

load("jstests/sharding/analyze_shard_key/libs/query_sampling_util.js");

// Make the periodic jobs for refreshing sample rates and writing sampled queries and diffs have a
// period of 1 second to speed up the test.
const st = new ShardingTest({
    shards: 2,
    rs: {
        nodes: 2,
        setParameter:
            {queryAnalysisWriterIntervalSecs: 1, logComponentVerbosity: tojson({verbosity: 2})}
    },
    mongosOptions: {setParameter: {queryAnalysisSamplerConfigurationRefreshSecs: 1}}
});

const dbName = "testDb";
const collName = "testColl";
const ns = dbName + "." + collName;
const mongosDB = st.s.getDB(dbName);
const mongosColl = mongosDB.getCollection(collName);

assert.commandWorked(st.s.adminCommand({enableSharding: dbName}));
st.ensurePrimaryShard(dbName, st.shard0.name);
assert.commandWorked(mongosDB.createCollection(collName));
const collectionUuid = QuerySamplingUtil.getCollectionUuid(mongosDB, collName);

assert.commandWorked(
    st.s.adminCommand({configureQueryAnalyzer: ns, mode: "full", sampleRate: 1000}));
QuerySamplingUtil.waitForActiveSampling(st.s);

const expectedSampledQueryDocs = [];
// This is an unsharded collection so all documents are on the primary shard.
const shardNames = [st.rs0.name];

// Make each write below have a unique filter and use that to look up the corresponding
// config.sampledQueries document later.

{
    // Perform some updates.
    assert.commandWorked(mongosColl.insert([{x: 1, y: 1, z: [1, 0, 1]}, {x: 2, y: 2, z: [2]}]));

    const cmdName = "update";
    const updateOp0 = {
        q: {x: 1},
        u: {$mul: {y: 10}, $set: {"z.$[element]": 10}},
        arrayFilters: [{"element": {$gte: 1}}],
        multi: false,
        upsert: false,
        collation: QuerySamplingUtil.generateRandomCollation(),
    };
    const diff0 = {y: 'u', z: 'u'};
    const updateOp1 = {
        q: {x: 2},
        u: [{$set: {y: 20, w: 200}}],
        c: {var0: 1},
        multi: true,
    };
    const diff1 = {y: 'u', w: 'i'};
    const originalCmdObj = {
        update: collName,
        updates: [updateOp0, updateOp1],
        let : {var1: 1},
    };

    assert.commandWorked(mongosDB.runCommand(originalCmdObj));
    assert.neq(mongosColl.findOne({x: 1, y: 10, z: [10, 0, 10]}), null);
    assert.neq(mongosColl.findOne({x: 2, y: 20, z: [2], w: 200}), null);

    expectedSampledQueryDocs.push({
        filter: {"cmd.updates.0.q": updateOp0.q},
        cmdName: cmdName,
        cmdObj: Object.assign({}, originalCmdObj, {updates: [updateOp0]}),
        diff: diff0,
        shardNames
    });
    expectedSampledQueryDocs.push({
        filter: {"cmd.updates.0.q": updateOp1.q},
        cmdName: cmdName,
        cmdObj: Object.assign({}, originalCmdObj, {updates: [updateOp1]}),
        diff: diff1,
        shardNames
    });

    // 'explain' queries should not get sampled.
    assert.commandWorked(mongosDB.runCommand(
        {explain: {update: collName, updates: [{q: {x: 101}, u: [{$set: {y: 101}}]}]}}));
}

{
    // Perform some deletes.
    assert.commandWorked(mongosColl.insert([{x: 3}, {x: 4}]));

    const cmdName = "delete";
    const deleteOp0 = {
        q: {x: 3},
        limit: 1,
        collation: QuerySamplingUtil.generateRandomCollation(),
    };
    const deleteOp1 = {q: {x: 4}, limit: 0};
    const originalCmdObj = {
        delete: collName,
        deletes: [deleteOp0, deleteOp1],

    };

    assert.commandWorked(mongosDB.runCommand(originalCmdObj));
    assert.eq(mongosColl.findOne({x: 3}), null);
    assert.eq(mongosColl.findOne({x: 4}), null);

    expectedSampledQueryDocs.push({
        filter: {"cmd.deletes.0.q": deleteOp0.q},
        cmdName: cmdName,
        cmdObj: Object.assign({}, originalCmdObj, {deletes: [deleteOp0]}),
        shardNames
    });
    expectedSampledQueryDocs.push({
        filter: {"cmd.deletes.0.q": deleteOp1.q},
        cmdName: cmdName,
        cmdObj: Object.assign({}, originalCmdObj, {deletes: [deleteOp1]}),
        shardNames
    });

    // 'explain' queries should not get sampled.
    assert.commandWorked(
        mongosDB.runCommand({explain: {delete: collName, deletes: [{q: {x: 301}, limit: 1}]}}));
}

{
    // Perform some findAndModify.
    assert.commandWorked(mongosColl.insert([{x: 5, y: 5, z: [5, 0, 5]}]));

    const cmdName = "findAndModify";
    const originalCmdObj = {
        findAndModify: collName,
        query: {x: 5},
        update: {$mul: {y: 10}, $set: {"z.$[element]": 50}},
        arrayFilters: [{"element": {$gte: 5}}],
        sort: {_id: 1},
        collation: QuerySamplingUtil.generateRandomCollation(),
        new: true,
        upsert: false,
        let : {var0: 1}
    };
    const diff = {y: 'u', z: 'u'};

    assert.commandWorked(mongosDB.runCommand(originalCmdObj));
    assert.neq(mongosColl.findOne({x: 5, y: 50, z: [50, 0, 50]}), null);

    expectedSampledQueryDocs.push({
        filter: {"cmd.query": originalCmdObj.query},
        cmdName: cmdName,
        cmdObj: Object.assign({}, originalCmdObj),
        diff,
        shardNames
    });

    // 'explain' queries should not get sampled.
    assert.commandWorked(mongosDB.runCommand(
        {explain: {findAndModify: collName, query: {x: 501}, update: {$set: {y: 501}}}}));
}

const cmdNames = ["update", "delete", "findAndModify"];
QuerySamplingUtil.assertSoonSampledQueryDocumentsAcrossShards(
    st, ns, collectionUuid, cmdNames, expectedSampledQueryDocs);

assert.commandWorked(st.s.adminCommand({configureQueryAnalyzer: ns, mode: "off"}));

st.stop();
})();