summaryrefslogtreecommitdiff
path: root/jstests/sharding/resharding_metrics.js
blob: e141e68df104359e738690e688e68d40a8885f3d (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
/**
 * Tests the basic functionality of the resharding metrics section in server status.
 *
 * @tags: [
 *   requires_fcv_49,
 *   uses_atclustertime,
 * ]
 */

(function() {
'use strict';

load("jstests/libs/discover_topology.js");
load("jstests/sharding/libs/resharding_test_fixture.js");

const kNamespace = "reshardingDb.coll";

function verifyMetrics(metrics, expected) {
    for (var key in expected) {
        assert(metrics.hasOwnProperty(key), `Missing ${key} in ${tojson(metrics)}`);
        const expectedValue = expected[key];
        // The contract for this method is to treat `undefined` as an indication for non-important
        // or non-deterministic values.
        if (expectedValue === undefined)
            continue;
        assert.eq(metrics[key],
                  expectedValue,
                  `Expected the value for ${key} to be ${expectedValue}: ${tojson(metrics)}`);
    }
}

function testMetricsArePresent(mongo, expectedMetrics) {
    const stats = mongo.getDB('admin').serverStatus({});
    assert(stats.hasOwnProperty('shardingStatistics'), stats);
    const shardingStats = stats.shardingStatistics;
    assert(shardingStats.hasOwnProperty('resharding'),
           `Missing resharding section in ${tojson(shardingStats)}`);

    const metrics = shardingStats.resharding;
    verifyMetrics(metrics, expectedMetrics);
}

function verifyParticipantServerStatusOutput(reshardingTest, inputCollection, expectedMetrics) {
    const donorShardNames = reshardingTest.donorShardNames;
    const recipientShardNames = reshardingTest.recipientShardNames;

    const mongos = inputCollection.getMongo();
    const topology = DiscoverTopology.findConnectedNodes(mongos);

    testMetricsArePresent(new Mongo(topology.shards[donorShardNames[0]].primary), expectedMetrics);
    testMetricsArePresent(new Mongo(topology.shards[donorShardNames[1]].primary), expectedMetrics);
    testMetricsArePresent(new Mongo(topology.shards[recipientShardNames[0]].primary),
                          expectedMetrics);
    testMetricsArePresent(new Mongo(topology.shards[recipientShardNames[1]].primary),
                          expectedMetrics);
}

function verifyCoordinatorServerStatusOutput(inputCollection, expectedMetrics) {
    const mongos = inputCollection.getMongo();
    const topology = DiscoverTopology.findConnectedNodes(mongos);

    testMetricsArePresent(new Mongo(topology.configsvr.primary), expectedMetrics);
}

// Tests the currentOp output for each donor, each recipient, and the coordinator.
function checkCurrentOp(mongo, clusterName, role, expected) {
    function getCurrentOpReport(mongo, role) {
        return mongo.getDB("admin").currentOp(
            {ns: kNamespace, desc: {$regex: 'Resharding' + role + 'Service.*'}});
    }

    jsTest.log(`Testing currentOp output for ${role}s on ${clusterName}`);
    assert.soon(() => {
        const report = getCurrentOpReport(mongo, role);
        if (report.inprog.length === 1)
            return true;

        jsTest.log(tojson(report));
        return false;
    }, () => `: was unable to find resharding ${role} service in currentOp output`);

    verifyMetrics(getCurrentOpReport(mongo, role).inprog[0], expected);
}

function verifyCurrentOpOutput(reshardingTest, inputCollection) {
    // Wait for the resharding operation and the donor services to start.
    const mongos = inputCollection.getMongo();
    assert.soon(() => {
        const coordinatorDoc = mongos.getCollection("config.reshardingOperations").findOne({
            ns: inputCollection.getFullName()
        });
        return coordinatorDoc !== null && coordinatorDoc.cloneTimestamp !== undefined;
    });

    const topology = DiscoverTopology.findConnectedNodes(mongos);

    reshardingTest.donorShardNames.forEach(function(shardName) {
        checkCurrentOp(new Mongo(topology.shards[shardName].primary), shardName, "Donor", {
            "type": "op",
            "op": "command",
            "ns": kNamespace,
            "originatingCommand": undefined,
            "totalOperationTimeElapsedSecs": undefined,
            "countWritesDuringCriticalSection": 0,
            "totalCriticalSectionTimeElapsedSecs": undefined,
            "donorState": undefined,
            "opStatus": "running",
        });
    });

    reshardingTest.recipientShardNames.forEach(function(shardName) {
        checkCurrentOp(new Mongo(topology.shards[shardName].primary), shardName, "Recipient", {
            "type": "op",
            "op": "command",
            "ns": kNamespace,
            "originatingCommand": undefined,
            "totalOperationTimeElapsedSecs": undefined,
            "remainingOperationTimeEstimatedSecs": undefined,
            "approxDocumentsToCopy": undefined,
            "approxBytesToCopy": undefined,
            "documentsCopied": undefined,
            "bytesCopied": undefined,
            "totalCopyTimeElapsedSecs": undefined,
            "oplogEntriesFetched": undefined,
            "oplogEntriesApplied": undefined,
            "totalApplyTimeElapsedSecs": undefined,
            "recipientState": undefined,
            "opStatus": "running",
        });
    });

    checkCurrentOp(new Mongo(topology.configsvr.nodes[0]), "configsvr", "Coordinator", {
        "type": "op",
        "op": "command",
        "ns": kNamespace,
        "originatingCommand": undefined,
        "totalOperationTimeElapsedSecs": undefined,
        "coordinatorState": undefined,
        "opStatus": "running",
    });
}

const reshardingTest = new ReshardingTest({numDonors: 2, numRecipients: 2, reshardInPlace: true});
reshardingTest.setup();

const donorShardNames = reshardingTest.donorShardNames;
const inputCollection = reshardingTest.createShardedCollection({
    ns: kNamespace,
    shardKeyPattern: {oldKey: 1},
    chunks: [
        {min: {oldKey: MinKey}, max: {oldKey: 0}, shard: donorShardNames[0]},
        {min: {oldKey: 0}, max: {oldKey: MaxKey}, shard: donorShardNames[1]},
    ],
});

var initialServerStatusMetrics = {
    "countReshardingOperations": 0,
    "countReshardingSuccessful": 0,
    "countReshardingFailures": 0,
    "countReshardingCanceled": 0,
    "documentsCopied": 0,
    "bytesCopied": 0,
    "oplogEntriesApplied": 0,
    "countWritesDuringCriticalSection": 0,
    "lastOpEndingChunkImbalance": 0,
};

verifyParticipantServerStatusOutput(reshardingTest, inputCollection, initialServerStatusMetrics);
verifyCoordinatorServerStatusOutput(inputCollection, {lastOpEndingChunkImbalance: 0});

var documentsInserted = [
    {_id: "stays on shard0", oldKey: -10, newKey: -10},
    {_id: "moves to shard0", oldKey: 10, newKey: -10},
    {_id: "moves to shard1", oldKey: -10, newKey: 10},
    {_id: "stays on shard1", oldKey: 10, newKey: 10},
];

assert.commandWorked(inputCollection.insert(documentsInserted));

const recipientShardNames = reshardingTest.recipientShardNames;
reshardingTest.withReshardingInBackground(  //
    {
        newShardKeyPattern: {newKey: 1},
        newChunks: [
            {min: {newKey: MinKey}, max: {newKey: 0}, shard: recipientShardNames[0]},
            {min: {newKey: 0}, max: {newKey: 10}, shard: recipientShardNames[1]},
            {min: {newKey: 10}, max: {newKey: 20}, shard: recipientShardNames[1]},
            {min: {newKey: 20}, max: {newKey: 30}, shard: recipientShardNames[1]},
            {min: {newKey: 30}, max: {newKey: MaxKey}, shard: recipientShardNames[1]},
        ],
    },
    (tempNs) => {
        verifyCurrentOpOutput(reshardingTest, inputCollection);
    });

var finalServerStatusMetrics = {
    "countReshardingOperations": 1,
    "countReshardingSuccessful": 1,
    "countReshardingFailures": 0,
    "countReshardingCanceled": 0,
    "documentsCopied": 2,
    "bytesCopied": Object.bsonsize(documentsInserted[1]) + Object.bsonsize(documentsInserted[2]),
    "oplogEntriesApplied": 2,
    "countWritesDuringCriticalSection": 0,
    "lastOpEndingChunkImbalance": 0,
};

verifyParticipantServerStatusOutput(reshardingTest, inputCollection, finalServerStatusMetrics);
verifyCoordinatorServerStatusOutput(inputCollection, {lastOpEndingChunkImbalance: 3});

reshardingTest.teardown();
})();