summaryrefslogtreecommitdiff
path: root/jstests/hooks/run_initial_sync_node_validation.js
blob: 562f3f0007609c5419be59ef8b0e55e2f3d52344 (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
// Runner that runs full validation on all collections of the initial sync node and checks the
// dbhashes of all of the nodes including the initial sync node.
'use strict';

(function() {
    var startTime = Date.now();

    var primaryInfo = db.isMaster();
    assert(primaryInfo.ismaster,
           'shell is not connected to the primary node: ' + tojson(primaryInfo));

    var cmdLineOpts = db.adminCommand('getCmdLineOpts');
    assert.commandWorked(cmdLineOpts);
    var isMasterSlave = cmdLineOpts.parsed.master === true;
    assert(!isMasterSlave, 'Master/Slave is not supported with initial sync hooks');

    // The initial sync hooks only work for replica sets.
    var rst = new ReplSetTest(db.getMongo().host);

    // Call getPrimary to populate rst with information about the nodes.
    var primary = rst.getPrimary();
    assert(primary, 'calling getPrimary() failed');

    // Find the hidden node.
    var hiddenNode;
    for (var secondary of rst.liveNodes.slaves) {
        var isMasterRes = secondary.getDB('admin').isMaster();
        if (isMasterRes.hidden) {
            hiddenNode = secondary;
            break;
        }
    }

    assert(hiddenNode, 'No hidden initial sync node was found in the replica set');

    // Confirm that the hidden node is in SECONDARY state.
    var res = assert.commandWorked(hiddenNode.adminCommand({replSetGetStatus: 1}));
    assert.eq(res.myState, ReplSetTest.State.SECONDARY, tojson(res));

    /* The checkReplicatedDataHashes call waits until all operations have replicated to and
       have been applied on the secondaries, so we run the validation script after it
       to ensure we're validating the entire contents of the collection */

    // For checkDBHashes
    const excludedDBs = jsTest.options().excludedDBsFromDBHash;
    rst.checkReplicatedDataHashes(undefined, excludedDBs);

    load('jstests/hooks/run_validate_collections.js');

    var totalTime = Date.now() - startTime;
    print('Finished consistency checks of initial sync node in ' + totalTime + ' ms.');
})();