summaryrefslogtreecommitdiff
path: root/jstests/libs/override_methods/check_uuids_consistent_across_cluster.js
blob: 800e3f005b8d8bab41331cb9b3b42cbdb320fddd (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
/**
 * Provides a hook to check that shards' storage catalogs and catalog caches are consistent
 * with the sharding catalog on the config server.
 *
 * The hook currently checks that: if the sharding catalog says a shard owns chunks for a sharded
 * collection, then the shard has an entry for the collection
 * - in its storage catalog, with the same UUID as the collection has in the sharding catalog
 * - in its catalog cache, with the same UUID as the collection has in the sharding catalog
 *
 * TODO (SERVER-33252): extend the hook to add consistency checks for databases
 * TODO (SERVER-33253): extend the hook to add consistency checks for collection indexes and options
 */
"use strict";

ShardingTest.prototype.checkUUIDsConsistentAcrossCluster = function() {
    if (jsTest.options().skipCheckingUUIDsConsistentAcrossCluster) {
        // A test may want to skip the consistency checks for a few reasons:
        // 1)  The checks are performed against shard and config primaries, and the connections
        //     cached on ShardingTest are used. So, tests that end with a different (or no) shard or
        //     config primary should skip the checks.
        // 2) The sharding catalog is read from the config server via mongos, so tests that cause
        //    the config primary to be unreachable from mongos should skip the checks.
        print(
            "Skipping checking consistency of the sharding catalog with shards' storage catalogs and catalog caches");
        return;
    }

    if (jsTest.options().skipCheckingCatalogCacheConsistencyWithShardingCatalog) {
        // When a shard takes or loses ownership of a chunk (through shardCollection, moveChunk, or
        // dropCollection), a best-effort is made to make the shard refresh its routing table cache.
        // But since sharding catalog changes are not transactional, it's possible the shard's
        // catalog cache will be stale. A test or suite that induces stepdowns or otherwise makes it
        // likely that this "best-effort" will fail should skip checks for only the catalog caches.
        print(
            "Checking consistency of the sharding catalog with shards' storage catalogs, but not with shards' catalog caches");
    } else {
        print(
            "Checking consistency of the sharding catalog with shards' storage catalogs and catalog caches");
    }

    this.awaitReplicationOnShards = function() {
        var timeout = 1 * 60 * 1000;
        for (var i = 0; i < this._rs.length; i++) {
            // If this shard is standalone, the replica set object will be null. In that case, we
            // will just skip.
            if (!this._rs[i]) {
                continue;
            }
            var rs = this._rs[i].test;

            var keyFile = this._otherParams.keyFile;
            if (keyFile) {
                authutil.asCluster(rs.nodes, keyFile, function() {
                    rs.awaitLastOpCommitted(timeout);
                });
            } else {
                rs.awaitLastOpCommitted(timeout);
            }
        }
    };

    function parseNs(dbDotColl) {
        assert.gt(dbDotColl.indexOf('.'),
                  0,
                  "expected " + dbDotColl + " to represent a full collection name");
        const dbName = dbDotColl.substring(0, dbDotColl.indexOf('.'));
        const collName = dbDotColl.substring(dbDotColl.indexOf('.') + 1, dbDotColl.length);
        return [dbName, collName];
    }

    try {
        // Read from config.collections, config.shards, and config.chunks to construct a picture
        // of which shards own data for which collections, and what the UUIDs for those collections
        // are.
        let authoritativeCollMetadataArr =
            this.s.getDB("config")
                .chunks
                .aggregate([
                    {
                      $lookup: {
                          from: "shards",
                          localField: "shard",
                          foreignField: "_id",
                          as: "shardHost"
                      }
                    },
                    {$unwind: "$shardHost"},
                    {$group: {_id: "$ns", shardConnStrings: {$addToSet: "$shardHost.host"}}},
                    {
                      $lookup: {
                          from: "collections",
                          localField: "_id",
                          foreignField: "_id",
                          as: "collInfo"
                      }
                    },
                    {$unwind: "$collInfo"}
                ])
                .toArray();

        print("Aggregated authoritative metadata on config server for all sharded collections: " +
              tojson(authoritativeCollMetadataArr));

        // The ShardingTest object maintains a connection to each shard in its _connections array,
        // where each connection is tagged with the shard's connection string in a 'host' field.
        // Create a reverse mapping of connection string to connection to efficiently retrieve a
        // connection by connection string.
        let shardConnStringToConn = {};
        this._connections.forEach(function(conn) {
            shardConnStringToConn[conn.host] = conn;
        });

        if (!jsTest.options().skipAwaitingReplicationOnShardsBeforeCheckingUUIDs) {
            // Finish replication on all shards (if they are replica sets).
            this.awaitReplicationOnShards();
        }

        for (let authoritativeCollMetadata of authoritativeCollMetadataArr) {
            const ns = authoritativeCollMetadata._id;
            const [dbName, collName] = parseNs(ns);

            for (let shardConnString of authoritativeCollMetadata.shardConnStrings) {
                // A connection the shard may not be cached in ShardingTest if the shard was added
                // manually to the cluster by the test.
                if (!(shardConnStringToConn.hasOwnProperty(shardConnString))) {
                    print("Creating connection to manually added shard: " + shardConnString);
                    shardConnStringToConn[shardConnString] = new Mongo(shardConnString);
                }
                let shardConn = shardConnStringToConn[shardConnString];

                print("Checking that the UUID for " + ns + " returned by listCollections on " +
                      shardConn +
                      " is consistent with the UUID in config.collections on the config server");

                const actualCollMetadata =
                    shardConn.getDB(dbName).getCollectionInfos({name: collName})[0];
                assert.eq(authoritativeCollMetadata.collInfo.uuid,
                          actualCollMetadata.info.uuid,
                          "authoritative collection info on config server: " +
                              tojson(authoritativeCollMetadata.collInfo) +
                              ", actual collection info on shard " + shardConnString + ": " +
                              tojson(actualCollMetadata));

                if (!jsTest.options().skipCheckingCatalogCacheConsistencyWithShardingCatalog) {
                    print(
                        "Checking that the UUID for " + ns + " in config.cache.collections on " +
                        shardConn +
                        " is consistent with the UUID in config.collections on the config server");

                    // Wait for the shard to finish writing its last refresh to disk.
                    assert.commandWorked(shardConn.adminCommand(
                        {_flushRoutingTableCacheUpdates: ns, syncFromConfig: false}));

                    let actualConfigMetadata = shardConn.getDB("config")
                                                   .getCollection("cache.collections")
                                                   .find({"_id": ns})
                                                   .toArray();
                    assert.eq(
                        actualConfigMetadata.length,
                        1,
                        "Incorrect number of entries in 'cache.collections' have been found for collection '" +
                            ns + "' on node " + shardConn);
                    actualConfigMetadata = actualConfigMetadata[0];

                    assert.eq(authoritativeCollMetadata.collInfo.uuid,
                              actualConfigMetadata.uuid,
                              "authoritative collection info on config server: " +
                                  tojson(authoritativeCollMetadata.collInfo) +
                                  ", actual config info on shard " + shardConnString + ": " +
                                  tojson(actualConfigMetadata));
                }
            }
        }
    } catch (e) {
        if (e.message.indexOf("Unauthorized") < 0) {
            throw e;
        }
        print("ignoring exception " + tojson(e) +
              " while checking UUID consistency across cluster");
    }
};