summaryrefslogtreecommitdiff
path: root/src/mongo/db/catalog/drop_indexes.cpp
blob: 7eaf3d6bd4a4a63413f4f9cf9c657be5afcae04a (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kCommand

#include "mongo/platform/basic.h"

#include "mongo/db/catalog/drop_indexes.h"

#include "mongo/db/background.h"
#include "mongo/db/catalog/index_catalog.h"
#include "mongo/db/client.h"
#include "mongo/db/concurrency/write_conflict_exception.h"
#include "mongo/db/curop.h"
#include "mongo/db/db_raii.h"
#include "mongo/db/index/index_descriptor.h"
#include "mongo/db/index_builds_coordinator.h"
#include "mongo/db/op_observer.h"
#include "mongo/db/repl/replication_coordinator.h"
#include "mongo/db/repl_set_member_in_standalone_mode.h"
#include "mongo/db/service_context.h"
#include "mongo/db/views/view_catalog.h"
#include "mongo/logv2/log.h"
#include "mongo/util/log.h"

namespace mongo {
namespace {

// Field name in dropIndexes command for indexes to drop.
constexpr auto kIndexFieldName = "index"_sd;

Status checkForViewOrMissingNS(OperationContext* opCtx,
                               const NamespaceString& nss,
                               Database* db,
                               Collection* collection) {
    if (!collection) {
        if (db && ViewCatalog::get(db)->lookup(opCtx, nss.ns())) {
            return Status(ErrorCodes::CommandNotSupportedOnView,
                          str::stream() << "Cannot drop indexes on view " << nss);
        }
        return Status(ErrorCodes::NamespaceNotFound, str::stream() << "ns not found " << nss);
    }
    return Status::OK();
}

/**
 * Validates the key pattern passed through the command.
 */
StatusWith<const IndexDescriptor*> getDescriptorByKeyPattern(OperationContext* opCtx,
                                                             IndexCatalog* indexCatalog,
                                                             const BSONElement& keyPattern) {
    const bool includeUnfinished = true;
    std::vector<const IndexDescriptor*> indexes;
    indexCatalog->findIndexesByKeyPattern(
        opCtx, keyPattern.embeddedObject(), includeUnfinished, &indexes);
    if (indexes.empty()) {
        return Status(ErrorCodes::IndexNotFound,
                      str::stream()
                          << "can't find index with key: " << keyPattern.embeddedObject());
    } else if (indexes.size() > 1) {
        return Status(ErrorCodes::AmbiguousIndexKeyPattern,
                      str::stream() << indexes.size() << " indexes found for key: "
                                    << keyPattern.embeddedObject() << ", identify by name instead."
                                    << " Conflicting indexes: " << indexes[0]->infoObj() << ", "
                                    << indexes[1]->infoObj());
    }

    const IndexDescriptor* desc = indexes[0];
    if (desc->isIdIndex()) {
        return Status(ErrorCodes::InvalidOptions, "cannot drop _id index");
    }

    if (desc->indexName() == "*") {
        // Dropping an index named '*' results in an drop-index oplog entry with a name of '*',
        // which in 3.6 and later is interpreted by replication as meaning "drop all indexes on
        // this collection".
        return Status(ErrorCodes::InvalidOptions,
                      "cannot drop an index named '*' by key pattern.  You must drop the "
                      "entire collection, drop all indexes on the collection by using an index "
                      "name of '*', or downgrade to 3.4 to drop only this index.");
    }

    return desc;
}

/**
 * Returns a list of index names that the caller requested to abort/drop. Requires a collection lock
 * to be held to look up the index name from the key pattern.
 */
StatusWith<std::vector<std::string>> getIndexNames(OperationContext* opCtx,
                                                   Collection* collection,
                                                   const BSONElement& indexElem) {
    invariant(opCtx->lockState()->isCollectionLockedForMode(collection->ns(), MODE_IX));

    std::vector<std::string> indexNames;
    if (indexElem.type() == String) {
        std::string indexToAbort = indexElem.valuestr();
        indexNames.push_back(indexToAbort);
    } else if (indexElem.type() == Object) {
        auto swDescriptor =
            getDescriptorByKeyPattern(opCtx, collection->getIndexCatalog(), indexElem);
        if (!swDescriptor.isOK()) {
            return swDescriptor.getStatus();
        }
        indexNames.push_back(swDescriptor.getValue()->indexName());
    } else if (indexElem.type() == Array) {
        for (auto indexNameElem : indexElem.Array()) {
            invariant(indexNameElem.type() == String);
            indexNames.push_back(indexNameElem.valuestr());
        }
    }

    return indexNames;
}

/**
 * Attempts to abort a single index builder that is responsible for all the index names passed in.
 */
std::vector<UUID> abortIndexBuildByIndexNamesNoWait(OperationContext* opCtx,
                                                    Collection* collection,
                                                    std::vector<std::string> indexNames) {

    boost::optional<UUID> buildUUID =
        IndexBuildsCoordinator::get(opCtx)->abortIndexBuildByIndexNamesNoWait(
            opCtx, collection->uuid(), indexNames, Timestamp(), "dropIndexes command");
    if (buildUUID) {
        return {*buildUUID};
    }
    return {};
}

/**
 * Drops single index given a descriptor.
 */
Status dropIndexByDescriptor(OperationContext* opCtx,
                             Collection* collection,
                             IndexCatalog* indexCatalog,
                             const IndexDescriptor* desc) {
    if (desc->isIdIndex()) {
        return Status(ErrorCodes::InvalidOptions, "cannot drop _id index");
    }

    // Support dropping unfinished indexes, but only if the index is 'frozen'. These indexes only
    // exist in standalone mode.
    auto entry = indexCatalog->getEntry(desc);
    if (entry->isFrozen()) {
        invariant(!entry->isReady(opCtx));
        invariant(getReplSetMemberInStandaloneMode(opCtx->getServiceContext()));
        // Return here. No need to fall through to op observer on standalone.
        return indexCatalog->dropUnfinishedIndex(opCtx, desc);
    }

    // Do not allow dropping unfinished indexes that are not frozen.
    if (!entry->isReady(opCtx)) {
        return Status(ErrorCodes::IndexNotFound,
                      str::stream()
                          << "can't drop unfinished index with name: " << desc->indexName());
    }

    auto s = indexCatalog->dropIndex(opCtx, desc);
    if (!s.isOK()) {
        return s;
    }

    opCtx->getServiceContext()->getOpObserver()->onDropIndex(
        opCtx, collection->ns(), collection->uuid(), desc->indexName(), desc->infoObj());

    return Status::OK();
}

/**
 * Aborts all the index builders on the collection if the first element in 'indexesToAbort' is "*",
 * otherwise this attempts to abort a single index builder building the given index names.
 */
std::vector<UUID> abortActiveIndexBuilders(OperationContext* opCtx,
                                           Collection* collection,
                                           const std::vector<std::string>& indexNames) {
    invariant(opCtx->lockState()->isCollectionLockedForMode(collection->ns(), MODE_IX));

    if (indexNames.empty()) {
        return {};
    }

    if (indexNames.front() == "*") {
        return IndexBuildsCoordinator::get(opCtx)->abortCollectionIndexBuildsNoWait(
            collection->uuid(), "dropIndexes command");
    }

    return abortIndexBuildByIndexNamesNoWait(opCtx, collection, indexNames);
}

Status dropReadyIndexes(OperationContext* opCtx,
                        Collection* collection,
                        const std::vector<std::string>& indexNames,
                        BSONObjBuilder* anObjBuilder) {
    invariant(opCtx->lockState()->isCollectionLockedForMode(collection->ns(), MODE_X));

    if (indexNames.empty()) {
        return Status::OK();
    }

    IndexCatalog* indexCatalog = collection->getIndexCatalog();
    if (indexNames.front() == "*") {
        indexCatalog->dropAllIndexes(
            opCtx, false, [opCtx, collection](const IndexDescriptor* desc) {
                opCtx->getServiceContext()->getOpObserver()->onDropIndex(opCtx,
                                                                         collection->ns(),
                                                                         collection->uuid(),
                                                                         desc->indexName(),
                                                                         desc->infoObj());
            });

        anObjBuilder->append("msg", "non-_id indexes dropped for collection");
        return Status::OK();
    }

    bool includeUnfinished = true;
    for (const auto& indexName : indexNames) {
        auto desc = indexCatalog->findIndexByName(opCtx, indexName, includeUnfinished);
        if (!desc) {
            return Status(ErrorCodes::IndexNotFound,
                          str::stream() << "index not found with name [" << indexName << "]");
        }
        Status status = dropIndexByDescriptor(opCtx, collection, indexCatalog, desc);
        if (!status.isOK()) {
            return status;
        }
    }
    return Status::OK();
}

}  // namespace

Status dropIndexes(OperationContext* opCtx,
                   const NamespaceString& nss,
                   const BSONObj& cmdObj,
                   BSONObjBuilder* result) {
    // Protects the command from replica set state changes during its execution.
    Lock::GlobalLock globalLk(opCtx, MODE_IX);

    // We only need to hold an intent lock to send abort signals to the active index builder(s) we
    // intend to abort.
    boost::optional<AutoGetCollection> autoColl;
    autoColl.emplace(opCtx, nss, MODE_IX);

    bool writesAreReplicatedAndNotPrimary = opCtx->writesAreReplicated() &&
        !repl::ReplicationCoordinator::get(opCtx)->canAcceptWritesFor(opCtx, nss);

    if (writesAreReplicatedAndNotPrimary) {
        return Status(ErrorCodes::NotMaster,
                      str::stream() << "Not primary while dropping indexes in " << nss);
    }

    Database* db = autoColl->getDb();
    Collection* collection = autoColl->getCollection();
    Status status = checkForViewOrMissingNS(opCtx, nss, db, collection);
    if (!status.isOK()) {
        return status;
    }

    const UUID collectionUUID = collection->uuid();
    const NamespaceStringOrUUID nssOrUUID = {nss.db().toString(), collectionUUID};

    if (!serverGlobalParams.quiet.load()) {
        LOGV2(51806,
              "CMD: dropIndexes {nss}: {cmdObj_kIndexFieldName_false}",
              "nss"_attr = nss,
              "cmdObj_kIndexFieldName_false"_attr = cmdObj[kIndexFieldName].toString(false));
    }

    result->appendNumber("nIndexesWas", collection->getIndexCatalog()->numIndexesTotal(opCtx));

    // Validate basic user input.
    BSONElement indexElem = cmdObj.getField(kIndexFieldName);
    const bool isWildcard = indexElem.type() == String && indexElem.String() == "*";

    // If an Array was passed in, verify that all the elements are of type String.
    if (indexElem.type() == Array) {
        for (auto indexNameElem : indexElem.Array()) {
            if (indexNameElem.type() != String) {
                return Status(ErrorCodes::TypeMismatch,
                              str::stream()
                                  << "dropIndexes " << collection->ns() << " (" << collectionUUID
                                  << ") failed to drop multiple indexes "
                                  << indexElem.toString(false) << ": index name must be a string");
            }
        }
    }

    IndexBuildsCoordinator* indexBuildsCoord = IndexBuildsCoordinator::get(opCtx);

    // When releasing the collection lock to send the abort signal to the index builders, it's
    // possible for new index builds to start. Keep aborting in-progress index builds if they
    // satisfy the caller's input.
    std::vector<UUID> abortedIndexBuilders;
    std::vector<std::string> indexNames;
    while (true) {
        auto swIndexNames = getIndexNames(opCtx, collection, indexElem);
        if (!swIndexNames.isOK()) {
            return swIndexNames.getStatus();
        }

        indexNames = swIndexNames.getValue();

        // Send the abort signal to any index builders that match the users request.
        abortedIndexBuilders = abortActiveIndexBuilders(opCtx, collection, indexNames);

        // Now that the abort signals were sent to the intended index builders, release our lock
        // temporarily to allow the index builders to process the abort signal. Holding a lock here
        // will cause the index builders to block indefinitely.
        autoColl = boost::none;
        if (abortedIndexBuilders.size() == 1) {
            indexBuildsCoord->awaitIndexBuildFinished(collectionUUID, abortedIndexBuilders.front());
        } else if (abortedIndexBuilders.size() > 1) {
            // Only the "*" wildcard can abort multiple index builders.
            invariant(isWildcard);
            indexBuildsCoord->awaitNoIndexBuildInProgressForCollection(collectionUUID);
        }

        // Take an exclusive lock on the collection now to be able to perform index catalog writes
        // when removing ready indexes from disk.
        autoColl.emplace(opCtx, nssOrUUID, MODE_X);

        // Abandon the snapshot as the index catalog will compare the in-memory state to the disk
        // state, which may have changed when we released the lock temporarily.
        opCtx->recoveryUnit()->abandonSnapshot();

        db = autoColl->getDb();
        collection = autoColl->getCollection();
        if (!collection) {
            return {ErrorCodes::NamespaceNotFound,
                    str::stream() << "Collection not found on database " << nss.db()
                                  << " with UUID " << collectionUUID};
        }

        // Check to see if a new index build was started that the caller requested to be aborted.
        bool abortAgain = false;
        if (isWildcard) {
            abortAgain = indexBuildsCoord->inProgForCollection(collectionUUID);
        } else {
            abortAgain = indexBuildsCoord->hasIndexBuilder(opCtx, collectionUUID, indexNames);
        }

        if (!abortAgain) {
            break;
        }

        // We only need to hold an intent lock to send abort signals to the active index
        // builder(s) we intend to abort.
        autoColl = boost::none;
        autoColl.emplace(opCtx, nssOrUUID, MODE_IX);

        // Abandon the snapshot as the index catalog will compare the in-memory state to the
        // disk state, which may have changed when we released the lock temporarily.
        opCtx->recoveryUnit()->abandonSnapshot();

        db = autoColl->getDb();
        collection = autoColl->getCollection();
        if (!collection) {
            return {ErrorCodes::NamespaceNotFound,
                    str::stream() << "Collection not found on database " << nss.db()
                                  << " with UUID " << collectionUUID};
        }
    }

    // If the "*" wildcard was not specified, verify that all the index names belonging to the
    // index builder were aborted. If not, they must be ready, so we drop them.
    if (!isWildcard && !abortedIndexBuilders.empty()) {
        invariant(abortedIndexBuilders.size() == 1);

        return writeConflictRetry(
            opCtx, "dropIndexes", nss.db(), [opCtx, &collection, &indexNames, result] {
                WriteUnitOfWork wunit(opCtx);

                // This is necessary to check shard version.
                OldClientContext ctx(opCtx, collection->ns().ns());

                size_t numReady = 0;
                const bool includeUnfinished = false;
                IndexCatalog* indexCatalog = collection->getIndexCatalog();
                for (const auto& indexName : indexNames) {
                    const IndexDescriptor* desc =
                        indexCatalog->findIndexByName(opCtx, indexName, includeUnfinished);
                    if (!desc) {
                        // The given index name was successfully aborted.
                        continue;
                    }

                    Status status = dropIndexByDescriptor(opCtx, collection, indexCatalog, desc);
                    if (!status.isOK()) {
                        return status;
                    }

                    numReady++;
                }

                invariant(numReady == 0 || numReady == indexNames.size());

                wunit.commit();
                return Status::OK();
            });
    }

    if (!abortedIndexBuilders.empty()) {
        // All the index builders were sent the abort signal, remove all the remaining indexes in
        // the index catalog.
        invariant(isWildcard);
        invariant(indexNames.size() == 1);
        invariant(indexNames.front() == "*");
        invariant(collection->getIndexCatalog()->numIndexesInProgress(opCtx) == 0);
    } else {
        // The index catalog requires that no active index builders are running when dropping
        // indexes.
        BackgroundOperation::assertNoBgOpInProgForNs(collection->ns());
        IndexBuildsCoordinator::get(opCtx)->assertNoIndexBuildInProgForCollection(collectionUUID);
    }

    return writeConflictRetry(
        opCtx, "dropIndexes", nss.db(), [opCtx, &collection, &indexNames, result] {
            WriteUnitOfWork wunit(opCtx);

            // This is necessary to check shard version.
            OldClientContext ctx(opCtx, collection->ns().ns());

            // Use an empty BSONObjBuilder to avoid duplicate appends to result on retry loops.
            BSONObjBuilder tempObjBuilder;
            Status status = dropReadyIndexes(opCtx, collection, indexNames, &tempObjBuilder);
            if (!status.isOK()) {
                return status;
            }

            wunit.commit();

            result->appendElementsUnique(
                tempObjBuilder.done());  // This append will only happen once.
            return Status::OK();
        });
}

Status dropIndexesForApplyOps(OperationContext* opCtx,
                              const NamespaceString& nss,
                              const BSONObj& cmdObj,
                              BSONObjBuilder* result) {
    return writeConflictRetry(opCtx, "dropIndexes", nss.db(), [opCtx, &nss, &cmdObj, result] {
        AutoGetCollection autoColl(opCtx, nss, MODE_X);

        // If db/collection does not exist, short circuit and return.
        Database* db = autoColl.getDb();
        Collection* collection = autoColl.getCollection();
        Status status = checkForViewOrMissingNS(opCtx, nss, db, collection);
        if (!status.isOK()) {
            return status;
        }

        if (!serverGlobalParams.quiet.load()) {
            LOGV2(20344,
                  "CMD: dropIndexes {nss}: {cmdObj_kIndexFieldName_false}",
                  "nss"_attr = nss,
                  "cmdObj_kIndexFieldName_false"_attr = cmdObj[kIndexFieldName].toString(false));
        }

        BackgroundOperation::assertNoBgOpInProgForNs(nss);
        IndexBuildsCoordinator::get(opCtx)->assertNoIndexBuildInProgForCollection(
            collection->uuid());

        BSONElement indexElem = cmdObj.getField(kIndexFieldName);
        auto swIndexNames = getIndexNames(opCtx, collection, indexElem);
        if (!swIndexNames.isOK()) {
            return swIndexNames.getStatus();
        }

        WriteUnitOfWork wunit(opCtx);

        // This is necessary to check shard version.
        OldClientContext ctx(opCtx, nss.ns());

        // Use an empty BSONObjBuilder to avoid duplicate appends to result on retry loops.
        BSONObjBuilder tempObjBuilder;
        status = dropReadyIndexes(opCtx, collection, swIndexNames.getValue(), &tempObjBuilder);
        if (!status.isOK()) {
            return status;
        }

        wunit.commit();

        result->appendElementsUnique(tempObjBuilder.done());  // This append will only happen once.
        return Status::OK();
    });
}

}  // namespace mongo