summaryrefslogtreecommitdiff
path: root/jstests/core/find_and_modify_metrics.js
blob: 4fb500b81f03a9623edd7db239e0a213c5f8dd84 (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
/**
 * Tests "metrics.commands.findAndModify.pipeline" and "metrics.commands.findAndModify.arrayFilters"
 * counters of the findAndModify command.
 *
 * @tags: [
 * requires_fcv_42,
 * # The test relies on the precise number of executions of commands.
 * requires_non_retryable_writes,
 * # The test is designed to work with an unsharded collection.
 * assumes_unsharded_collection,
 * ]
 */
(function() {
"use strict";

const testDB = db.getSiblingDB(jsTestName());
assert.commandWorked(testDB.dropDatabase());
const coll = testDB.findAndModify_metrics;
assert.commandWorked(testDB.createCollection(coll.getName()));

assert.commandWorked(coll.insert([{key: 1, value: 1, array: [5, 10]}]));

// "Initialize" the counters for the findAndModify command.
let result = coll.findAndModify({query: {key: 1}, update: {$set: {value: 0}}});
assert.eq(1, result.key);

let serverStatusBeforeTest = testDB.serverStatus();

// Verify that the metrics.commands.findAndModify.pipeline counter is present.
assert.gte(serverStatusBeforeTest.metrics.commands.findAndModify.pipeline,
           0,
           tojson(serverStatusBeforeTest));

// Verify that that findAndModify command without aggregation pipeline-style update does not
// increment the counter.
result = coll.findAndModify({query: {key: 1}, update: {$set: {value: 5}}});
assert.eq(1, result.key);
let serverStatusAfterTest = testDB.serverStatus();
assert.eq(serverStatusBeforeTest.metrics.commands.findAndModify.pipeline,
          serverStatusAfterTest.metrics.commands.findAndModify.pipeline,
          `Before:  ${tojson(serverStatusBeforeTest)}, after: ${tojson(serverStatusAfterTest)}`);

// Verify that that findAndModify command with aggregation pipeline-style update increments the
// counter.
result = coll.findAndModify({query: {key: 1}, update: [{$set: {value: 10}}]});
assert.eq(1, result.key);
serverStatusAfterTest = testDB.serverStatus();
assert.eq(serverStatusBeforeTest.metrics.commands.findAndModify.pipeline + 1,
          serverStatusAfterTest.metrics.commands.findAndModify.pipeline,
          `Before:  ${tojson(serverStatusBeforeTest)}, after: ${tojson(serverStatusAfterTest)}`);

serverStatusBeforeTest = testDB.serverStatus();

// Verify that the metrics.commands.findAndModify.arrayFilters counter is present.
assert.gte(serverStatusBeforeTest.metrics.commands.findAndModify.arrayFilters,
           0,
           tojson(serverStatusBeforeTest));

// Verify that that findAndModify command without arrayFilters does not increment the counter.
result = coll.findAndModify({query: {key: 1}, update: {$set: {value: 5}}});
assert.eq(1, result.key);
serverStatusAfterTest = testDB.serverStatus();
assert.eq(serverStatusBeforeTest.metrics.commands.findAndModify.arrayFilters,
          serverStatusAfterTest.metrics.commands.findAndModify.arrayFilters,
          `Before:  ${tojson(serverStatusBeforeTest)}, after: ${tojson(serverStatusAfterTest)}`);

// Verify that that findAndModify command with arrayFilters increments the counter.
result = coll.findAndModify({
    query: {key: 1},
    update: {$set: {"array.$[element]": 20}},
    arrayFilters: [{"element": {$gt: 6}}]
});
assert.eq(1, result.key);
serverStatusAfterTest = testDB.serverStatus();
assert.eq(serverStatusBeforeTest.metrics.commands.findAndModify.arrayFilters + 1,
          serverStatusAfterTest.metrics.commands.findAndModify.arrayFilters,
          `Before:  ${tojson(serverStatusBeforeTest)}, after: ${tojson(serverStatusAfterTest)}`);
})();