summaryrefslogtreecommitdiff
path: root/jstests/libs/override_methods/check_orphans_are_deleted.js
blob: fc6f8198d8652b9f1cde38f1a25ba538e76d05c2 (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
load('jstests/libs/check_orphans_are_deleted_helpers.js');  // For CheckOrphansAreDeletedHelpers.

/**
 * Asserts that all shards in the sharded cluster doesn't own any orphan documents.
 * Requires all shards and config server to have primary that is reachable.
 *
 * Note: Doesn't catch documents in the shard that doesn't have the full shard key.
 * Assumes that all shards have the index that matches the shard key.
 */
ShardingTest.prototype.checkOrphansAreDeleted = function() {
    if (jsTest.options().skipCheckOrphans) {
        print("Skipping orphan check across the cluster");
        return;
    }

    print('Running check orphans against cluster with mongos: ' + this.s.host);

    let getConn = function(connStr) {
        try {
            return new Mongo(connStr);
        } catch (exp) {
            jsTest.log('Unable to connect to ' + connStr + ' while trying to check for orphans');
            return null;
        }
    };

    // Use a new connection so we don't have to worry about existing users logged in to the
    // connection.
    let mongosConn = new Mongo(this.s.host);
    mongosConn.fullOptions = Object.merge(this.s.fullOptions, {});

    const keyFile = this.keyFile;
    if (keyFile || mongosConn.fullOptions.clusterAuthMode == 'x509') {
        authutil.asCluster(mongosConn, keyFile, () => {
            assert.commandWorked(mongosConn.adminCommand({balancerStop: 1}));

            // Use config.shards so we will not miss shards added outside of ShardingTest.
            mongosConn.getDB('config').shards.find().forEach(shardDoc => {
                let shardConn = getConn(shardDoc.host);

                // Inherit connection options from mongos connection.
                shardConn.fullOptions = Object.merge(this.s.fullOptions, {});

                if (shardConn != null) {
                    authutil.asCluster(shardConn, keyFile, () => {
                        CheckOrphansAreDeletedHelpers.runCheck(mongosConn, shardConn, shardDoc._id);
                    });
                }
            });
        });
    } else {
        assert.commandWorked(mongosConn.adminCommand({balancerStop: 1}));

        try {
            waitForOngoingChunkSplits(this);
        } catch (e) {
            print("Unable to wait for ongoing chunk splits: " + e);
        }

        // Use config.shards so we will not miss shards added outside of ShardingTest.
        mongosConn.getDB('config').shards.find().forEach(shardDoc => {
            let shardConn = getConn(shardDoc.host);

            if (shardConn != null) {
                shardConn.host = shardDoc.host;
                CheckOrphansAreDeletedHelpers.runCheck(mongosConn, shardConn, shardDoc._id);
            }
        });
    }
};