summaryrefslogtreecommitdiff
path: root/jstests/hooks/run_check_repl_dbhash.js
blob: 5a9606e20b1d823b6251482e083ed96fd751c14d (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
// Runner for checkDBHashes() that runs the dbhash command on all replica set nodes
// to ensure all nodes have the same data.
'use strict';

(function() {
    // A thin wrapper around master/slave nodes that provides the getHashes(), getPrimary(),
    // awaitReplication(), and nodeList() methods.
    // DEPRECATED: this wrapper only supports nodes started through resmoke's masterslave.py
    // fixture. Please do not use it with other master/slave clusters.
    var MasterSlaveDBHashTest = function(primaryHost) {
        var master = new Mongo(primaryHost);
        var masterPort = master.host.split(':')[1];
        var slave = new Mongo('localhost:' + String(parseInt(masterPort) + 1));

        this.nodeList = function() {
            return [master.host, slave.host];
        };

        this.getHashes = function(db) {
            var combinedRes = {};
            var res = master.getDB(db).runCommand("dbhash");
            assert.commandWorked(res);
            combinedRes.master = res;

            res = slave.getDB(db).runCommand("dbhash");
            assert.commandWorked(res);
            combinedRes.slaves = [res];

            return combinedRes;
        };

        this.getPrimary = function() {
            slave.setSlaveOk();
            this.liveNodes = {master: master, slaves: [slave]};

            return master;
        };

        this.getSecondaries = function() {
            return [slave];
        };

        this.awaitReplication = function() {
            assert.commandWorked(master.adminCommand({fsyncUnlock: 1}),
                                 'failed to unlock the primary');

            print('Starting fsync on master to flush all pending writes');
            assert.commandWorked(master.adminCommand({fsync: 1}));
            print('fsync on master completed');

            var timeout = 60 * 1000 * 5;  // 5min timeout
            var dbNames = master.getDBNames();
            print('Awaiting replication of inserts into ' + dbNames);
            for (var dbName of dbNames) {
                if (dbName === 'local')
                    continue;
                assert.writeOK(master.getDB(dbName).await_repl.insert(
                                   {awaiting: 'repl'}, {writeConcern: {w: 2, wtimeout: timeout}}),
                               'Awaiting replication failed');
            }
            print('Finished awaiting replication');
            assert.commandWorked(master.adminCommand({fsync: 1, lock: 1}),
                                 'failed to re-lock the primary');
        };

        this.checkReplicatedDataHashes = function() {
            ReplSetTest({nodes: 0}).checkReplicatedDataHashes.apply(this, arguments);
        };

        this.checkReplicaSet = function() {
            ReplSetTest({nodes: 0}).checkReplicaSet.apply(this, arguments);
        };
    };

    var startTime = Date.now();
    assert.neq(typeof db, 'undefined', 'No `db` object, is the shell connected to a mongod?');

    var primaryInfo = db.isMaster();

    assert(primaryInfo.ismaster,
           'shell is not connected to the primary or master node: ' + tojson(primaryInfo));

    var cmdLineOpts = db.adminCommand('getCmdLineOpts');
    assert.commandWorked(cmdLineOpts);
    var isMasterSlave = cmdLineOpts.parsed.master === true;
    var testFixture = isMasterSlave ? new MasterSlaveDBHashTest(db.getMongo().host)
                                    : new ReplSetTest(db.getMongo().host);
    var excludedDBs = jsTest.options().excludedDBsFromDBHash || [];

    // Since UUIDs aren't explicitly replicated in master-slave deployments, we ignore the UUID in
    // the output of the "listCollections" command to avoid reporting a known data inconsistency
    // issue from checkReplicatedDataHashes().
    var ignoreUUIDs = isMasterSlave;
    testFixture.checkReplicatedDataHashes(undefined, excludedDBs, ignoreUUIDs);

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