summaryrefslogtreecommitdiff
path: root/jstests/sharding/analyze_shard_key/config_query_analyzer_persistence.js
blob: 94251101ab40279f3f46436fb2eafdecb55b989f (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
 * Tests that the configureQueryAnalyzer command persists the configuration in a document
 * in config.queryAnalyzers and that the document is deleted when the associated collection
 * is dropped.
 *
 * @tags: [requires_fcv_62, featureFlagAnalyzeShardKey]
 */

(function() {
"use strict";

const dbName = "testDb";

/**
 * TestCase: {
 *    command: {ns : "coll namespace",
 *              mode : "full"|"off",
 *              sampleRate : 1.2},
 * }
 *
 */

/**
 * Create documents represting all combinations of options for configureQueryAnalyzer command.
 * @returns array of documents
 */
function optionsAllCombinations() {
    const testCases = [];
    const collName = "collection";
    for (const mode of ["off", "full"]) {
        for (const sampleRate of [null, -1.0, 0.0, 0.2]) {
            let testCase =
                Object.assign({}, {command: {ns: dbName + "." + collName, mode, sampleRate}});
            if (sampleRate == null) {
                delete testCase.command.sampleRate;
            }
            if ((mode == "off" && sampleRate !== null) ||
                (mode == "full" &&
                 (sampleRate == null || typeof sampleRate !== "number" || sampleRate <= 0.0))) {
                continue;  // These cases are tested in configuer_query_analyzer_basic.js.
            }
            testCases.push(testCase);
        }
    }
    return testCases;
}

function assertConfigQueryAnalyzerResponse(res, mode, sampleRate) {
    assert.eq(res.ok, 1);
    assert.eq(res.mode, mode);
    assert.eq(res.sampleRate, sampleRate);
}

function assertQueryAnalyzerConfigDoc(configDb, db, collName, mode, sampleRate) {
    const configColl = configDb.getCollection('queryAnalyzers');
    const listCollRes =
        assert.commandWorked(db.runCommand({listCollections: 1, filter: {name: collName}}));
    const uuid = listCollRes.cursor.firstBatch[0].info.uuid;
    const doc = configColl.findOne({_id: uuid});
    assert.eq(doc.mode, mode, doc);
    if (mode == "off") {
        assert.eq(doc.hasOwnProperty("sampleRate"), false, doc);
    } else if (mode == "full") {
        assert.eq(doc.sampleRate, sampleRate, doc);
    }
}

function assertNoQueryAnalyzerConfigDoc(configDb, db, collName) {
    const configColl = configDb.getCollection('queryAnalyzers');
    const listCollRes =
        assert.commandWorked(db.runCommand({listCollections: 1, filter: {name: collName}}));
    assert.eq(listCollRes.cursor.firstBatch, 0);
}

function testConfigurationOptions(conn, testCases) {
    const collName = "collection";
    const ns = dbName + "." + collName;
    const db = conn.getDB(dbName);
    const coll = db.getCollection(ns);
    let config = conn.getDB('config');
    assert.commandWorked(coll.remove({}));
    assert.commandWorked(db.runCommand({insert: collName, documents: [{x: 1}]}));

    testCases.forEach(testCase => {
        jsTest.log(`Running configureQueryAnalyzer command on test case ${tojson(testCase)}`);

        const res = conn.adminCommand({
            configureQueryAnalyzer: testCase.command.ns,
            mode: testCase.command.mode,
            sampleRate: testCase.command.sampleRate
        });
        assert.commandWorked(res);
        assertConfigQueryAnalyzerResponse(res, testCase.command.mode, testCase.command.sampleRate);
        assertQueryAnalyzerConfigDoc(
            config, db, collName, testCase.command.mode, testCase.command.sampleRate);
    });
}

function testDropCollectionDeletesConfig(conn) {
    const db = conn.getDB(dbName);

    const collNameSh = "collection2DropSh";
    const nsSh = dbName + "." + collNameSh;
    const collSh = db.getCollection(collNameSh);
    const collNameUnsh = "collection2DropUnsh";
    const nsUnsh = dbName + "." + collNameUnsh;
    const collUnsh = db.getCollection(collNameUnsh);

    const config = conn.getDB('config');
    const shardKey = {skey: 1};
    const shardKeySplitPoint = {skey: 2};

    jsTest.log('Testing drop collection deletes query analyzer config doc');

    assert.commandWorked(conn.adminCommand({shardCollection: nsSh, key: shardKey}));
    assert.commandWorked(conn.adminCommand({split: nsSh, middle: shardKeySplitPoint}));

    assert.commandWorked(db.runCommand({insert: collNameSh, documents: [{skey: 1, y: 1}]}));
    assert.commandWorked(db.runCommand({insert: collNameUnsh, documents: [{skey: 1, y: 1}]}));

    // sharded collection

    const mode = "full";
    const sampleRate = 0.5;
    const resSh =
        conn.adminCommand({configureQueryAnalyzer: nsSh, mode: mode, sampleRate: sampleRate});
    assert.commandWorked(resSh);
    assertConfigQueryAnalyzerResponse(resSh, mode, sampleRate);
    assertQueryAnalyzerConfigDoc(config, db, collNameSh, mode, sampleRate);

    collSh.drop();
    assertNoQueryAnalyzerConfigDoc(config, db, collNameSh);

    // unsharded collection

    const resUnsh =
        conn.adminCommand({configureQueryAnalyzer: nsUnsh, mode: mode, sampleRate: sampleRate});
    assert.commandWorked(resUnsh);
    assertConfigQueryAnalyzerResponse(resUnsh, mode, sampleRate);
    assertQueryAnalyzerConfigDoc(config, db, collNameUnsh, mode, sampleRate);

    collUnsh.drop();
    assertNoQueryAnalyzerConfigDoc(config, db, collNameUnsh);
}

function testDropDatabaseDeletesConfig(conn) {
    let db = conn.getDB(dbName);
    const collNameSh = "collection2DropSh";
    const nsSh = dbName + "." + collNameSh;
    const collSh = db.getCollection(nsSh);

    const config = conn.getDB('config');
    const shardKey = {skey: 1};
    const shardKeySplitPoint = {skey: 2};

    jsTest.log('Testing drop database deletes query analyzer config doc');
    assert.commandWorked(conn.adminCommand({shardCollection: nsSh, key: shardKey}));
    assert.commandWorked(conn.adminCommand({split: nsSh, middle: shardKeySplitPoint}));
    assert.commandWorked(db.runCommand({insert: collNameSh, documents: [{skey: 1, y: 1}]}));

    // sharded collection

    const mode = "full";
    const sampleRate = 0.5;
    const resSh =
        conn.adminCommand({configureQueryAnalyzer: nsSh, mode: mode, sampleRate: sampleRate});
    assert.commandWorked(resSh);
    assertConfigQueryAnalyzerResponse(resSh, mode, sampleRate);
    assertQueryAnalyzerConfigDoc(config, db, collNameSh, mode, sampleRate);
    db.dropDatabase();
    assertNoQueryAnalyzerConfigDoc(config, db, collNameSh);

    // unsharded collection

    db = conn.getDB(dbName);
    const collNameUnsh = "collection2DropUnsh";
    const nsUnsh = dbName + "." + collNameUnsh;
    const collUnsh = db.getCollection(nsUnsh);
    assert.commandWorked(db.runCommand({insert: collNameUnsh, documents: [{skey: 1, y: 1}]}));

    const resUnsh =
        conn.adminCommand({configureQueryAnalyzer: nsUnsh, mode: mode, sampleRate: sampleRate});
    assert.commandWorked(resUnsh);
    assertConfigQueryAnalyzerResponse(resUnsh, mode, sampleRate);
    assertQueryAnalyzerConfigDoc(config, db, collNameUnsh, mode, sampleRate);
    db.dropDatabase();
    assertNoQueryAnalyzerConfigDoc(config, db, collNameUnsh);
}

{
    const st = new ShardingTest({shards: 2, rs: {nodes: 2}});

    st.s.adminCommand({enableSharding: dbName, primaryShard: st.shard0.name});

    const AllTestCases = optionsAllCombinations();
    testConfigurationOptions(st.s, AllTestCases);

    testDropCollectionDeletesConfig(st.s);
    testDropDatabaseDeletesConfig(st.s);

    st.stop();
}
})();