summaryrefslogtreecommitdiff
path: root/jstests/sharding/analyze_shard_key/refresh_sample_rates.js
blob: c17bcb31d3045a153d3e5bf944d687b751d646bd (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
 * Tests that the _refreshQueryAnalyzerConfiguration command is only supported on the configsvr
 * primary mongod in a sharded cluster and the primary mongod in standalone replica set, and that
 * it returns correct sample rates.
 *
 * @tags: [requires_fcv_70, requires_persistence]
 */
(function() {
"use strict";

load("jstests/libs/uuid_util.js");  // for 'extractUUIDFromObject'
load("jstests/sharding/analyze_shard_key/libs/query_sampling_util.js");

// Used to prevent a mongos or mongod from running _refreshQueryAnalyzerConfiguration commands by
// themselves in the background.
const setParameterOpts = {
    "failpoint.disableQueryAnalysisSampler": tojson({mode: "alwaysOn"})
};

function testBasic(createConnFn, rst, samplerNames) {
    assert.eq(samplerNames.length, 3);
    const primary = rst.getPrimary();
    const conn = createConnFn();

    const dbName = "testDbBasic-" + extractUUIDFromObject(UUID());

    const collName0 = "testColl0";
    const ns0 = dbName + "." + collName0;
    const sampleRate0 = 5;

    const collName1 = "testColl1";
    const ns1 = dbName + "." + collName1;
    const sampleRate1 = 50;

    const db = conn.getDB(dbName);
    assert.commandWorked(db.createCollection(collName0));
    assert.commandWorked(db.createCollection(collName1));
    const collUuid0 = QuerySamplingUtil.getCollectionUuid(db, collName0);
    const collUuid1 = QuerySamplingUtil.getCollectionUuid(db, collName1);

    jsTest.log("Verifying that refreshing returns the correct configurations");
    assert.commandWorked(
        conn.adminCommand({configureQueryAnalyzer: ns0, mode: "full", sampleRate: sampleRate0}));
    assert.commandWorked(
        conn.adminCommand({configureQueryAnalyzer: ns1, mode: "full", sampleRate: sampleRate1}));
    const configColl = conn.getCollection("config.queryAnalyzers");
    const startTime0 = configColl.findOne({_id: ns0}).startTime;
    const startTime1 = configColl.findOne({_id: ns1}).startTime;

    // Query distribution after: [1, unknown, unknown]. Verify that refreshing returns
    // sampleRate / numSamplers.
    let res0 = assert.commandWorked(primary.adminCommand({
        _refreshQueryAnalyzerConfiguration: 1,
        name: samplerNames[0],
        numQueriesExecutedPerSecond: 1
    }));
    let expectedRatio0 = 1.0 / 3;
    assert.sameMembers(res0.configurations, [
        {
            ns: ns0,
            collectionUuid: collUuid0,
            sampleRate: expectedRatio0 * sampleRate0,
            startTime: startTime0
        },
        {
            ns: ns1,
            collectionUuid: collUuid1,
            sampleRate: expectedRatio0 * sampleRate1,
            startTime: startTime1
        },
    ]);

    // Query distribution after: [1, 0, unknown]. Verify that refreshing returns
    // sampleRate / numSamplers.
    let res1 = assert.commandWorked(primary.adminCommand({
        _refreshQueryAnalyzerConfiguration: 1,
        name: samplerNames[1],
        numQueriesExecutedPerSecond: 0  // zero counts as known.
    }));
    let expectedRatio1 = 1.0 / 3;
    assert.sameMembers(res1.configurations, [
        {
            ns: ns0,
            collectionUuid: collUuid0,
            sampleRate: expectedRatio1 * sampleRate0,
            startTime: startTime0
        },
        {
            ns: ns1,
            collectionUuid: collUuid1,
            sampleRate: expectedRatio1 * sampleRate1,
            startTime: startTime1
        },
    ]);

    // Query distribution after: [1, 0, 1] (no unknowns). Verify that refreshing returns correct
    // weighted sample rates.
    let res2 = assert.commandWorked(primary.adminCommand({
        _refreshQueryAnalyzerConfiguration: 1,
        name: samplerNames[2],
        numQueriesExecutedPerSecond: 1
    }));
    let expectedRatio2 = 1.0 / 2;
    assert.sameMembers(res2.configurations, [
        {
            ns: ns0,
            collectionUuid: collUuid0,
            sampleRate: expectedRatio2 * sampleRate0,
            startTime: startTime0
        },
        {
            ns: ns1,
            collectionUuid: collUuid1,
            sampleRate: expectedRatio2 * sampleRate1,
            startTime: startTime1
        },
    ]);

    // Query distribution after: [4.5, 0, 1] (one is fractional). Verify that refreshing returns
    // correct weighted sample rates.
    res0 = assert.commandWorked(primary.adminCommand({
        _refreshQueryAnalyzerConfiguration: 1,
        name: samplerNames[0],
        numQueriesExecutedPerSecond: 4.5
    }));
    expectedRatio0 = 4.5 / 5.5;
    assert.sameMembers(res0.configurations, [
        {
            ns: ns0,
            collectionUuid: collUuid0,
            sampleRate: expectedRatio0 * sampleRate0,
            startTime: startTime0
        },
        {
            ns: ns1,
            collectionUuid: collUuid1,
            sampleRate: expectedRatio0 * sampleRate1,
            startTime: startTime1
        },
    ]);

    // Query distribution after: [4.5, 0, 1] (no change). Verify that refreshing doesn't
    // return any sample rates since the weight for this mongos is 0.
    res1 = assert.commandWorked(primary.adminCommand({
        _refreshQueryAnalyzerConfiguration: 1,
        name: samplerNames[1],
        numQueriesExecutedPerSecond: 0
    }));
    assert.eq(res1.configurations.length, 2);
    assert.sameMembers(res1.configurations, [
        {ns: ns0, collectionUuid: collUuid0, sampleRate: 0, startTime: startTime0},
        {ns: ns1, collectionUuid: collUuid1, sampleRate: 0, startTime: startTime1},
    ]);

    assert.commandWorked(conn.adminCommand({configureQueryAnalyzer: ns1, mode: "off"}));

    // Query distribution after: [4.5, 0, 1.5]. Verify that refreshing doesn't return a sample
    // rate for the collection with sampling disabled.
    res2 = assert.commandWorked(primary.adminCommand({
        _refreshQueryAnalyzerConfiguration: 1,
        name: samplerNames[2],
        numQueriesExecutedPerSecond: 1.5
    }));
    expectedRatio1 = 1.5 / 6;
    assert.sameMembers(res2.configurations, [
        {
            ns: ns0,
            collectionUuid: collUuid0,
            sampleRate: expectedRatio1 * sampleRate0,
            startTime: startTime0
        },
    ]);

    assert.commandWorked(conn.adminCommand({configureQueryAnalyzer: ns0, mode: "off"}));

    // Query distribution after: [4.5, 0, 1.5] (no change).
    res2 = assert.commandWorked(primary.adminCommand({
        _refreshQueryAnalyzerConfiguration: 1,
        name: samplerNames[2],
        numQueriesExecutedPerSecond: 0
    }));
    assert.eq(0, res2.configurations.length);
}

function testFailover(createConnFn, rst, samplerNames) {
    assert.eq(samplerNames.length, 3);
    let primary = rst.getPrimary();
    let conn = createConnFn();

    const dbName = "testDbFailover-" + extractUUIDFromObject(UUID());
    const collName = "testColl";
    const ns = dbName + "." + collName;
    const sampleRate = 5;

    let db = conn.getDB(dbName);
    assert.commandWorked(db.createCollection(collName));
    const collUuid = QuerySamplingUtil.getCollectionUuid(db, collName);

    jsTest.log("Verify that configurations are persisted and available after failover");
    assert.commandWorked(
        conn.adminCommand({configureQueryAnalyzer: ns, mode: "full", sampleRate: sampleRate}));
    const configColl = conn.getCollection("config.queryAnalyzers");
    const startTime = configColl.findOne({_id: ns}).startTime;

    assert.commandWorked(
        primary.adminCommand({replSetStepDown: ReplSetTest.kForeverSecs, force: true}));
    assert.commandWorked(primary.adminCommand({replSetFreeze: 0}));
    primary = rst.getPrimary();
    conn = createConnFn();
    db = conn.getDB(dbName);

    // Query distribution after: [1, unknown, unknown]. Verify that refreshing returns
    // sampleRate / numSamplers.
    let res = assert.commandWorked(primary.adminCommand({
        _refreshQueryAnalyzerConfiguration: 1,
        name: samplerNames[0],
        numQueriesExecutedPerSecond: 1
    }));
    const expectedRatio = 1.0 / 3;
    assert.sameMembers(
        res.configurations,
        [{ns: ns, collectionUuid: collUuid, sampleRate: expectedRatio * sampleRate, startTime}]);

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

function testRestart(createConnFn, rst, samplerNames) {
    assert.eq(samplerNames.length, 3);
    let primary = rst.getPrimary();
    let conn = createConnFn();

    const dbName = "testDbRestart-" + extractUUIDFromObject(UUID());
    const collName = "testColl";
    const ns = dbName + "." + collName;
    const sampleRate = 5;

    let db = conn.getDB(dbName);
    assert.commandWorked(db.createCollection(collName));
    const collUuid = QuerySamplingUtil.getCollectionUuid(db, collName);

    jsTest.log("Verify that configurations are persisted and available after restart");
    assert.commandWorked(
        conn.adminCommand({configureQueryAnalyzer: ns, mode: "full", sampleRate: sampleRate}));
    const configColl = conn.getCollection("config.queryAnalyzers");
    const startTime = configColl.findOne({_id: ns}).startTime;

    rst.stopSet(null /* signal */, true /*forRestart */);
    rst.startSet({restart: true});
    primary = rst.getPrimary();
    conn = createConnFn();
    db = conn.getDB(dbName);

    // Query distribution after: [1, unknown, unknown]. Verify that refreshing returns
    // sampleRate / numSamplers.
    let res = assert.commandWorked(primary.adminCommand({
        _refreshQueryAnalyzerConfiguration: 1,
        name: samplerNames[0],
        numQueriesExecutedPerSecond: 1
    }));
    const expectedRatio = 1.0 / 3;
    assert.sameMembers(
        res.configurations,
        [{ns: ns, collectionUuid: collUuid, sampleRate: expectedRatio * sampleRate, startTime}]);

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

function runTest(createConnFn, rst, samplerNames) {
    testBasic(createConnFn, rst, samplerNames);
    // Verify that query distribution info is reset upon stepup.
    assert.commandWorked(
        rst.getPrimary().adminCommand({replSetStepDown: ReplSetTest.kForeverSecs, force: true}));
    testBasic(createConnFn, rst, samplerNames);

    testFailover(createConnFn, rst, samplerNames);
    testRestart(createConnFn, rst, samplerNames);
}

{
    const st = new ShardingTest({
        mongos: {
            s0: {setParameter: setParameterOpts},
            s1: {setParameter: setParameterOpts},
            s2: {setParameter: setParameterOpts}
        },
        shards: 1,
        config: 3,
        rs: {nodes: 1, setParameter: setParameterOpts},
        other: {
            configOptions: {setParameter: setParameterOpts},
        }
    });
    st.configRS.isConfigSvr = true;
    const samplerNames = [st.s0.host, st.s1.host, st.s2.host];

    jsTest.log("Wait for the config server to be aware that there are 3 mongoses in the cluster");
    assert.soon(() => {
        return st.s.getCollection("config.mongos").find().itcount() == 3;
    });

    jsTest.log("Test that the _refreshQueryAnalyzerConfiguration command is not supported on " +
               "mongos or shardsvr mongod or configsvr secondary mongod");
    const cmdObj = {
        _refreshQueryAnalyzerConfiguration: 1,
        name: samplerNames[0],
        numQueriesExecutedPerSecond: 1
    };
    assert.commandFailedWithCode(st.s.adminCommand(cmdObj), ErrorCodes.CommandNotFound);
    if (!TestData.configShard) {
        // Shard0 is the config server in config shard mode.
        st.rs0.nodes.forEach(node => {
            assert.commandFailedWithCode(node.adminCommand(cmdObj), ErrorCodes.IllegalOperation);
        });
    }
    st.configRS.getSecondaries(node => {
        assert.commandFailedWithCode(node.adminCommand(cmdObj), ErrorCodes.NotWritablePrimary);
    });

    jsTest.log("Test that the _refreshQueryAnalyzerConfiguration command is supported on " +
               "configsvr primary mongod");
    runTest(() => st.s, st.configRS, samplerNames);

    st.stop();
}

{
    const rst = new ReplSetTest({nodes: 3, nodeOptions: {setParameter: setParameterOpts}});
    rst.startSet();
    rst.initiate();
    const samplerNames = rst.nodes.map(node => node.host);

    jsTest.log("Test that the _refreshQueryAnalyzerConfiguration command is not supported on " +
               "secondary mongod");
    const cmdObj = {
        _refreshQueryAnalyzerConfiguration: 1,
        name: samplerNames[0].host,
        numQueriesExecutedPerSecond: 1
    };
    rst.getSecondaries(node => {
        assert.commandFailedWithCode(node.adminCommand(cmdObj), ErrorCodes.NotWritablePrimary);
    });

    jsTest.log("Test that the _refreshQueryAnalyzerConfiguration command is supported on " +
               "primary mongod");
    runTest(() => rst.getPrimary(), rst, samplerNames);

    rst.stopSet();
}
})();