summaryrefslogtreecommitdiff
path: root/jstests/libs/check_routing_table_consistency_helpers.js
blob: 9d8ff22074bddc91b349749778bfcd73acec201d (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
'use strict';

load("jstests/libs/feature_flag_util.js");

var RoutingTableConsistencyChecker = (function() {
    const sameObjectFields = (lhsObjFields, rhsObjFields) => {
        if (lhsObjFields.length !== rhsObjFields.length) {
            return false;
        }

        for (let i = 0; i != lhsObjFields.length; ++i) {
            if (lhsObjFields[i] !== rhsObjFields[i]) {
                return false;
            }
        }

        return true;
    };

    const fetchRoutingTableData =
        (mongos) => {
            // Group docs in config.chunks by coll UUID (sorting by minKey), then join with docs in
            // config.collections.
            return mongos.getDB('config')
                                .chunks
                                .aggregate([
                                    {$sort: {min: 1}},
                                    {
                                        $group: {
                                            _id: '$uuid',
                                            routingTable: {$push: '$$ROOT'},
                                        }
                                    },
                                    {$lookup: {
                                        from: 'collections',
                                        localField: '_id',
                                        foreignField: 'uuid',
                                        as: 'details'
                                    },
                                    }
                                ]);
        };

    const checkCollRoutingTable = (nss, shardKeyPattern, routingTable) => {
        if (!routingTable) {
            jsTest.log(`config.collections entry '${nss}' has no chunks`);
            return false;
        }

        const shardKeyfields = Object.keys(shardKeyPattern);
        let lastLowerBound = {};
        for (const key of shardKeyfields) {
            lastLowerBound[key] = MinKey;
        }
        let expectedLastBound = {};
        for (const key of shardKeyfields) {
            expectedLastBound[key] = MaxKey;
        }
        for (const chunk of routingTable) {
            if (!sameObjectFields(Object.keys(chunk.min), shardKeyfields) ||
                !sameObjectFields(Object.keys(chunk.max), shardKeyfields)) {
                jsTest.log(`Shard key pattern violation found in config.chunks for ${
                    nss}! Expected: ${tojson(shardKeyPattern)}, found chunk ${tojson(chunk)}`);
                return false;
            }

            if (bsonWoCompare(chunk.min, lastLowerBound) !== 0) {
                jsTest.log(`Found gap or range overlap in config.chunks for collection ${
                    nss}, chunk ${tojson(chunk._id)}! Expected ${tojson(lastLowerBound)}, found ${
                    tojson(chunk.min)}`);
                return false;
            }
            lastLowerBound = chunk.max;
        }

        if (bsonWoCompare(routingTable[routingTable.length - 1].max, expectedLastBound) !== 0) {
            jsTest.log(
                `Incomplete range key detected in config.chunks for ${nss} (MaxKey missing)`);
            return false;
        }

        jsTest.log(`${nss} with ${routingTable.length} chunks passed the check`);
        return true;
    };

    /**
     * Reproduces the logic implemented in ShardingCatalogManager::initializePlacementHistory()
     * to compute the placement of each existing collection and database by reading the content of
     * - config.collections + config.chunks
     * - config.databases.
     * The output format follows the same schema of config.placementHistory; results are ordered by
     * namespace.
     **/
    const buildCurrentPlacementData = (mongos) => {
        const pipeline = [
            {
                $lookup: {
                from: "chunks",
                localField: "uuid",
                foreignField: "uuid",
                as: "timestampByShard",
                pipeline: [
                    {
                     $group: {
                        _id: "$shard",
                        value: {
                        $max: "$onCurrentShardSince"
                        }
                    }
                    }
                ],
                }
            },
            {
                $project: {
                _id: 0,
                nss: "$_id",
                shards: "$timestampByShard._id",
                uuid: 1,
                timestamp: {
                    $max: "$timestampByShard.value"
                },
                }
            },
            {
                $unionWith: {
                 coll: "databases",
                 pipeline: [
                    {
                    $project: {
                        _id: 0,
                        nss: "$_id",
                        shards: [
                        "$primary"
                        ],
                        timestamp: "$version.timestamp"
                    }
                    }
                ]
                }
            },
            {
                $sort: {
                    nss: 1
                }
            }
            ];

        return mongos.getDB('config').collections.aggregate(pipeline);
    };

    /**
     * Extracts from config.placementHistory the most recent document for each collection
     * and database. Results are ordered by namespace.
     */
    const getHistoricalPlacementData = (mongos, atClusterTime) => {
        const kConfigPlacementHistoryInitializationMarker = '';
        const pipeline = [
            {
                $match: {
                    // Skip documents containing initialization metadata
                    nss: {$ne: kConfigPlacementHistoryInitializationMarker},
                    timestamp: {$lte: atClusterTime}
                }
            },
            {
                $group: {
                    _id: "$nss",
                    placement: {$top: {output: "$$CURRENT", sortBy: {"timestamp": -1}}}
                }
            },
            // Disregard placement entries on dropped namespaces
            {$match: {"placement.shards": {$not: {$size: 0}}}},
            {$replaceRoot: {newRoot: "$placement"}},
            {$sort: {nss: 1}}
        ];
        return mongos.getDB('config').placementHistory.aggregate(pipeline);
    };

    const checkHistoricalPlacementMetadataConsistency = (mongos) => {
        const placementDataFromRoutingTable = buildCurrentPlacementData(mongos);
        const now = mongos.getDB('admin').runCommand({isMaster: 1}).operationTime;
        const historicalPlacementData = getHistoricalPlacementData(mongos, now);

        placementDataFromRoutingTable.forEach(function(nssPlacementFromRoutingTable) {
            assert(historicalPlacementData.hasNext(),
                   `Historical placement data on ${nssPlacementFromRoutingTable.nss} is missing`);
            const historicalNssPlacement = historicalPlacementData.next();
            assert.eq(nssPlacementFromRoutingTable.nss,
                      historicalNssPlacement.nss,
                      'Historical placement data does not contain the expected number of entries');
            assert.sameMembers(nssPlacementFromRoutingTable.shards,
                               historicalNssPlacement.shards,
                               `Inconsistent placement info detected: routing table ${
                                   tojson(nssPlacementFromRoutingTable)} VS placement history ${
                                   tojson(historicalNssPlacement)}`);

            assert.eq(nssPlacementFromRoutingTable.uuid,
                      historicalNssPlacement.uuid,
                      `Inconsistent placement info detected: routing table ${
                          tojson(nssPlacementFromRoutingTable)} VS placement history ${
                          tojson(historicalNssPlacement)}`);
            // Timestamps are not compared, since they are expected to diverge if a chunk
            // migration, collection rename or a movePrimary request have been executed during
            // the test.
        });

        if (historicalPlacementData.hasNext()) {
            assert(false,
                   `Unexpected historical placement entries: ${
                       tojson(historicalPlacementData.toArray())}`);
        }
    };

    const run = (mongos) => {
        try {
            jsTest.log('Checking routing table consistency');

            // Group docs in config.chunks by coll UUID (sorting by minKey), then join with docs in
            // config.collections.
            const testCollectionsWithRoutingTable = fetchRoutingTableData(mongos);

            testCollectionsWithRoutingTable.forEach(function(collData) {
                assert.eq(
                    1,
                    collData.details.length,
                    `There are entries in config.chunks which are either not linked to a collection or are linked to more than one collection! Details: ${
                        tojson(collData)}`);

                assert(checkCollRoutingTable(
                           collData.details[0]._id, collData.details[0].key, collData.routingTable),
                       `Corrupted routing table detected for ${collData._id}! Details: ${
                           tojson(collData)}`);
            });
            jsTest.log('Routing table consistency check completed');
        } catch (e) {
            if (e.code !== ErrorCodes.Unauthorized) {
                throw e;
            }
            jsTest.log(
                'Skipping check of routing table consistency - access to admin collections is not authorized');
        }

        // TODO (SERVER-68217): Remove this try/catch block once 7.0 becomes last LTS.
        try {
            const historicalPlacementMetadataDataAvailable = FeatureFlagUtil.isPresentAndEnabled(
                mongos.getDB('config'), "HistoricalPlacementShardingCatalog");
            if (!historicalPlacementMetadataDataAvailable) {
                jsTest.log(
                    'Skipping consistency check of config.placementHistory: feature disabled');
                return;
            }
        } catch (err) {
            jsTest.log(`Skipping consistency check of config.placementHistory: "${err}"`);
            return;
        }

        try {
            jsTest.log('Checking consistency of config.placementHistory against the routing table');
            checkHistoricalPlacementMetadataConsistency(mongos);
            jsTest.log('config.placementHistory consistency check completed');

        } catch (e) {
            if (e.code !== ErrorCodes.Unauthorized) {
                throw e;
            }
            jsTest.log(
                'Skipping consistency check of config.placementHistory - access to admin collections is not authorized');
        }
    };

    return {
        run: run,
    };
})();