summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/initial_sync_operation_metrics.js
blob: 2a4d745208050a969522c94ec9c7fb65db1e42ad (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
/**
 * Test that initial sync does not contribute to operation metrics on the sync source when cloning
 * data.
 *
 * @tags: [
 *   requires_replication,
 *   requites_wiredtiger,
 *   sbe_incompatible,
 * ]
 */

(function() {
"use strict";

const assertMetricsExist = function(metrics) {
    try {
        assert.neq(metrics, undefined);
        let primaryMetrics = metrics.primaryMetrics;
        let secondaryMetrics = metrics.secondaryMetrics;
        [primaryMetrics, secondaryMetrics].forEach((readMetrics) => {
            assert.gte(readMetrics.docBytesRead, 0);
            assert.gte(readMetrics.docUnitsRead, 0);
            assert.gte(readMetrics.idxEntryBytesRead, 0);
            assert.gte(readMetrics.idxEntryUnitsRead, 0);
            assert.gte(readMetrics.keysSorted, 0);
            assert.gte(readMetrics.docUnitsReturned, 0);
            assert.gte(readMetrics.cursorSeeks, 0);
        });

        assert.gte(metrics.cpuNanos, 0);
        assert.gte(metrics.docBytesWritten, 0);
        assert.gte(metrics.docUnitsWritten, 0);
        assert.gte(metrics.idxEntryBytesWritten, 0);
        assert.gte(metrics.idxEntryUnitsWritten, 0);
    } catch (e) {
        print("caught exception while checking metrics output: " + tojson(metrics));
        throw e;
    }
};

// Returns metrics keyed database name.
const getDBMetrics = (adminDB) => {
    const cursor = adminDB.aggregate([{$operationMetrics: {}}]);
    let allMetrics = {};
    while (cursor.hasNext()) {
        let doc = cursor.next();
        allMetrics[doc.db] = doc;
    }
    return allMetrics;
};

const setParams = {
    "aggregateOperationResourceConsumptionMetrics": true,
};

const replSet = new ReplSetTest({nodes: 1, nodeOptions: {setParameter: setParams}});
replSet.startSet();
replSet.initiate();

const primary = replSet.getPrimary();
const adminDB = primary.getDB('admin');

const db1Name = "db1";
const db1 = primary.getDB(db1Name);
assert.commandWorked(db1.coll1.insert({a: 1}));
assert.commandWorked(db1.coll2.insert({a: 1}));

const db2Name = 'db2';
const db2 = primary.getDB(db2Name);
assert.commandWorked(db2.coll1.insert({a: 1}));
assert.commandWorked(db2.coll2.insert({a: 1}));

const metricsBefore = getDBMetrics(adminDB);
assertMetricsExist(metricsBefore[db1Name]);
assertMetricsExist(metricsBefore[db2Name]);

const newNode = replSet.add({setParameter: setParams});
replSet.reInitiate();
replSet.waitForState(newNode, ReplSetTest.State.SECONDARY);
replSet.awaitReplication();

// Ensure that the initial syncing node has not collected any metrics.
{
    const metrics = getDBMetrics(newNode.getDB('admin'));
    assert(!metrics.hasOwnProperty(db1Name), metrics);
    assert(!metrics.hasOwnProperty(db2Name), metrics);
}

// Ensure the initial syncing node did not accumulate metrics on the primary by reading.
{
    const metrics = getDBMetrics(adminDB);
    assert.eq(metricsBefore[db1Name], metrics[db1Name]);
    assert.eq(metricsBefore[db2Name], metrics[db2Name]);
}

replSet.stopSet();
})();