summaryrefslogtreecommitdiff
path: root/src/mongo/db/catalog/database_holder_impl.cpp
blob: 31275bfe51430af4e0b7ea504720aa79990b7b09 (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
/**
 *    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.
 */


#include "mongo/platform/basic.h"

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

#include "mongo/db/audit.h"
#include "mongo/db/catalog/collection_catalog.h"
#include "mongo/db/catalog/collection_impl.h"
#include "mongo/db/catalog/database_impl.h"
#include "mongo/db/concurrency/exception_util.h"
#include "mongo/db/index_builds_coordinator.h"
#include "mongo/db/op_observer.h"
#include "mongo/db/operation_context.h"
#include "mongo/db/service_context.h"
#include "mongo/db/stats/top.h"
#include "mongo/db/storage/storage_engine.h"
#include "mongo/logv2/log.h"

#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kStorage


namespace mongo {

Database* DatabaseHolderImpl::getDb(OperationContext* opCtx, const DatabaseName& dbName) const {
    uassert(
        13280,
        "invalid db name: " + dbName.db(),
        NamespaceString::validDBName(dbName.db(), NamespaceString::DollarInDbNameBehavior::Allow));

    invariant(opCtx->lockState()->isDbLockedForMode(dbName.toString(), MODE_IS) ||
              (dbName.db().compare("local") == 0 && opCtx->lockState()->isLocked()));

    stdx::lock_guard<SimpleMutex> lk(_m);
    DBs::const_iterator it = _dbs.find(dbName);
    if (it != _dbs.end()) {
        return it->second;
    }

    return nullptr;
}

bool DatabaseHolderImpl::dbExists(OperationContext* opCtx, const DatabaseName& dbName) const {
    uassert(
        6198702,
        "invalid db name: " + dbName.db(),
        NamespaceString::validDBName(dbName.db(), NamespaceString::DollarInDbNameBehavior::Allow));
    stdx::lock_guard<SimpleMutex> lk(_m);
    auto it = _dbs.find(dbName);
    return it != _dbs.end() && it->second != nullptr;
}

std::set<DatabaseName> DatabaseHolderImpl::_getNamesWithConflictingCasing_inlock(
    const DatabaseName& dbName) {
    std::set<DatabaseName> duplicates;

    for (const auto& nameAndPointer : _dbs) {
        // A name that's equal with case-insensitive match must be identical, or it's a duplicate.
        if (dbName.equalCaseInsensitive(nameAndPointer.first) && dbName != nameAndPointer.first)
            duplicates.insert(nameAndPointer.first);
    }
    return duplicates;
}

std::set<DatabaseName> DatabaseHolderImpl::getNamesWithConflictingCasing(
    const DatabaseName& dbName) {
    stdx::lock_guard<SimpleMutex> lk(_m);
    return _getNamesWithConflictingCasing_inlock(dbName);
}

std::vector<DatabaseName> DatabaseHolderImpl::getNames() {
    stdx::lock_guard<SimpleMutex> lk(_m);
    std::vector<DatabaseName> dbNames;
    for (const auto& nameAndPointer : _dbs) {
        dbNames.push_back(nameAndPointer.first);
    }
    return dbNames;
}

Database* DatabaseHolderImpl::openDb(OperationContext* opCtx,
                                     const DatabaseName& dbName,
                                     bool* justCreated) {
    uassert(
        6198701,
        "invalid db name: " + dbName.db(),
        NamespaceString::validDBName(dbName.db(), NamespaceString::DollarInDbNameBehavior::Allow));
    invariant(opCtx->lockState()->isDbLockedForMode(dbName.db(), MODE_IX));

    if (justCreated)
        *justCreated = false;  // Until proven otherwise.

    stdx::unique_lock<SimpleMutex> lk(_m);

    // The following will insert a nullptr for dbname, which will treated the same as a non-
    // existant database by the get method, yet still counts in getNamesWithConflictingCasing.
    if (auto db = _dbs[dbName])
        return db;

    // We've inserted a nullptr entry for dbname: make sure to remove it on unsuccessful exit.
    ScopeGuard removeDbGuard([this, &lk, opCtx, dbName] {
        if (!lk.owns_lock())
            lk.lock();
        auto it = _dbs.find(dbName);
        // If someone else hasn't either already removed it or already set it successfully, remove.
        if (it != _dbs.end() && !it->second) {
            _dbs.erase(it);
        }

        // In case anyone else is trying to open the same DB simultaneously and waiting on our
        // result, we should notify them we failed and let them try in our place.
        _c.notify_all();
    });

    // Check casing in lock to avoid transient duplicates.
    auto duplicates = _getNamesWithConflictingCasing_inlock(dbName);
    uassert(ErrorCodes::DatabaseDifferCase,
            str::stream() << "db already exists with different case already have: ["
                          << (*duplicates.cbegin()) << "] trying to create [" << dbName.toString()
                          << "]",
            duplicates.empty());

    // Do the catalog lookup and database creation outside of the scoped lock, because these may
    // block.
    lk.unlock();

    if (CollectionCatalog::get(opCtx)->getAllCollectionUUIDsFromDb(dbName).empty()) {
        audit::logCreateDatabase(opCtx->getClient(), dbName.toString());
        if (justCreated)
            *justCreated = true;
    }

    std::unique_ptr<DatabaseImpl> newDb = std::make_unique<DatabaseImpl>(dbName);
    Status status = newDb->init(opCtx);
    while (!status.isOK()) {
        // If we get here, then initializing the database failed because another concurrent writer
        // already registered their own Database instance with the ViewCatalog. We need to wait for
        // them to finish.
        lk.lock();

        auto it = _dbs.find(dbName);
        if (it != _dbs.end() && it->second) {
            // Creating databases only requires a DB lock in MODE_IX. Thus databases can be created
            // concurrently. If this thread "lost the race", return the database object that was
            // persisted in the `_dbs` map.
            removeDbGuard.dismiss();
            return it->second;
        }

        // Consider using OperationContext::waitForConditionOrInterrupt if the logic here changes
        // in such a way that we can easily express it as a predicate for that function.
        _c.wait_for(lk, stdx::chrono::milliseconds(1));

        it = _dbs.find(dbName);
        if (it != _dbs.end() && it->second) {
            // As above, another writer finished successfully, return the persisted object.
            removeDbGuard.dismiss();
            return it->second;
        }

        lk.unlock();

        // Before we continue make sure we haven't been killed
        opCtx->checkForInterrupt();

        // At this point it's possible that the other writer just hasn't finished yet, or that they
        // failed. In either case, we should check and see if we can initialize the database now.
        status = newDb->init(opCtx);
    }

    // Finally replace our nullptr entry with the new Database pointer.
    removeDbGuard.dismiss();
    lk.lock();

    invariant(!_dbs[dbName]);
    auto* db = newDb.release();
    _dbs[dbName] = db;
    invariant(_getNamesWithConflictingCasing_inlock(dbName).empty());
    _c.notify_all();

    return db;
}

void DatabaseHolderImpl::dropDb(OperationContext* opCtx, Database* db) {
    invariant(db);

    // Store the name so we have if for after the db object is deleted
    auto name = db->name();

    LOGV2_DEBUG(20310, 1, "dropDatabase {name}", "name"_attr = name);

    invariant(opCtx->lockState()->isDbLockedForMode(name.db(), MODE_X));

    auto catalog = CollectionCatalog::get(opCtx);
    for (auto collIt = catalog->begin(opCtx, name); collIt != catalog->end(opCtx); ++collIt) {
        auto coll = *collIt;
        if (!coll) {
            break;
        }

        // It is the caller's responsibility to ensure that no index builds are active in the
        // database.
        invariant(!coll->getIndexCatalog()->haveAnyIndexesInProgress(),
                  str::stream() << "An index is building on collection '" << coll->ns() << "'.");
    }

    audit::logDropDatabase(opCtx->getClient(), name.toString());

    auto const serviceContext = opCtx->getServiceContext();

    for (auto collIt = catalog->begin(opCtx, name); collIt != catalog->end(opCtx); ++collIt) {
        auto coll = *collIt;
        if (!coll) {
            break;
        }

        // The in-memory ViewCatalog gets cleared when opObserver::onDropCollection() is called for
        // the system.views collection. Since it is a replicated collection, this call occurs in
        // dropCollectionEvenIfSystem(). For standalones, `system.views` and the ViewCatalog are
        // dropped/cleared here.
        auto replCoord = repl::ReplicationCoordinator::get(opCtx);
        if (!replCoord->isReplEnabled() && coll->ns().isSystemDotViews()) {
            opCtx->getServiceContext()->getOpObserver()->onDropCollection(
                opCtx,
                coll->ns(),
                coll->uuid(),
                coll->numRecords(opCtx),
                OpObserver::CollectionDropType::kOnePhase);
        }

        Top::get(serviceContext).collectionDropped(coll->ns());
    }

    // Clean up the in-memory database state.
    CollectionCatalog::write(
        opCtx, [&](CollectionCatalog& catalog) { catalog.clearDatabaseProfileSettings(name); });
    close(opCtx, name);

    auto const storageEngine = serviceContext->getStorageEngine();
    writeConflictRetry(opCtx, "dropDatabase", name.toString(), [&] {
        storageEngine->dropDatabase(opCtx, name).transitional_ignore();
    });
}

void DatabaseHolderImpl::close(OperationContext* opCtx, const DatabaseName& dbName) {
    uassert(
        6198700,
        "invalid db name: " + dbName.db(),
        NamespaceString::validDBName(dbName.db(), NamespaceString::DollarInDbNameBehavior::Allow));
    invariant(opCtx->lockState()->isDbLockedForMode(dbName.db(), MODE_X));

    stdx::lock_guard<SimpleMutex> lk(_m);

    DBs::const_iterator it = _dbs.find(dbName);
    if (it == _dbs.end()) {
        return;
    }
    auto db = it->second;

    LOGV2_DEBUG(20311, 2, "DatabaseHolder::close", "db"_attr = dbName);

    CollectionCatalog::write(
        opCtx, [&](CollectionCatalog& catalog) { catalog.onCloseDatabase(opCtx, dbName); });

    delete db;
    db = nullptr;

    _dbs.erase(it);

    auto* const storageEngine = opCtx->getServiceContext()->getStorageEngine();
    storageEngine->closeDatabase(opCtx, dbName).transitional_ignore();
}

void DatabaseHolderImpl::closeAll(OperationContext* opCtx) {
    invariant(opCtx->lockState()->isW());

    while (true) {
        std::vector<DatabaseName> dbs;
        {
            stdx::lock_guard<SimpleMutex> lk(_m);
            for (DBs::const_iterator i = _dbs.begin(); i != _dbs.end(); ++i) {
                // It is the caller's responsibility to ensure that no index builds are active in
                // the database.
                IndexBuildsCoordinator::get(opCtx)->assertNoBgOpInProgForDb(i->first.toString());
                dbs.push_back(i->first);
            }
        }

        if (dbs.empty()) {
            break;
        }

        for (const auto& name : dbs) {
            close(opCtx, name);
        }
    }
}

void DatabaseHolderImpl::setDbInfo(OperationContext* opCtx,
                                   const DatabaseName& dbName,
                                   const DatabaseType& dbInfo) {
    uassert(
        6420900,
        "Invalid database name: " + dbName.db(),
        NamespaceString::validDBName(dbName.db(), NamespaceString::DollarInDbNameBehavior::Allow));
    invariant(opCtx->lockState()->isDbLockedForMode(dbName.db(), MODE_X));

    stdx::lock_guard<SimpleMutex> lk(_m);

    const auto it = _dbs.find(dbName);
    if (it == _dbs.end() || !it->second) {
        return;
    }

    LOGV2(6420901,
          "Setting this node's cached database info",
          "db"_attr = dbName.db(),
          "version"_attr = dbInfo.getVersion());

    auto db = static_cast<DatabaseImpl*>(it->second);
    db->_info.emplace(dbInfo);
}

void DatabaseHolderImpl::clearDbInfo(OperationContext* opCtx, const DatabaseName& dbName) {
    uassert(
        6420902,
        "Invalid database name: " + dbName.db(),
        NamespaceString::validDBName(dbName.db(), NamespaceString::DollarInDbNameBehavior::Allow));
    invariant(opCtx->lockState()->isDbLockedForMode(dbName.db(), MODE_IX));

    stdx::lock_guard<SimpleMutex> lk(_m);

    const auto it = _dbs.find(dbName);
    if (it == _dbs.end() || !it->second) {
        return;
    }

    LOGV2(6420903, "Clearing this node's cached database info", "db"_attr = dbName.db());

    auto db = static_cast<DatabaseImpl*>(it->second);
    db->_info = boost::none;
}

boost::optional<DatabaseVersion> DatabaseHolderImpl::getDbVersion(
    OperationContext* opCtx, const DatabaseName& dbName) const {
    uassert(
        6420904,
        "Invalid database name: " + dbName.db(),
        NamespaceString::validDBName(dbName.db(), NamespaceString::DollarInDbNameBehavior::Allow));
    // The database should be required to be locked in IS mode, however this function is also called
    // by the `AutoGet*ForReadLockFree` constructor, which only holds the global lock in IS mode.

    stdx::lock_guard<SimpleMutex> lk(_m);

    const auto it = _dbs.find(dbName);
    if (it == _dbs.end() || !it->second) {
        return boost::none;
    }

    auto db = static_cast<DatabaseImpl*>(it->second);
    return db->_info ? boost::optional<DatabaseVersion>(db->_info->getVersion()) : boost::none;
}

boost::optional<ShardId> DatabaseHolderImpl::getDbPrimary(OperationContext* opCtx,
                                                          const DatabaseName& dbName) const {
    uassert(
        6420905,
        "Invalid database name: " + dbName.db(),
        NamespaceString::validDBName(dbName.db(), NamespaceString::DollarInDbNameBehavior::Allow));
    invariant(opCtx->lockState()->isDbLockedForMode(dbName.db(), MODE_IS));

    stdx::lock_guard<SimpleMutex> lk(_m);

    const auto it = _dbs.find(dbName);
    if (it == _dbs.end() || !it->second) {
        return boost::none;
    }

    auto db = static_cast<DatabaseImpl*>(it->second);
    return db->_info ? boost::optional<ShardId>(db->_info->getPrimary()) : boost::none;
}

}  // namespace mongo