diff options
author | Geert Bosch <geert@mongodb.com> | 2017-03-11 06:17:41 -0800 |
---|---|---|
committer | Geert Bosch <geert@mongodb.com> | 2017-04-02 09:32:57 -0400 |
commit | 576e157c2753b9bc61c36002e323421c09bc62dc (patch) | |
tree | bc123ef55e9a325bdbfd1cae38288284278466cf /src | |
parent | 53022d457a0610b40fea600d9d546f410be3d7ed (diff) | |
download | mongo-576e157c2753b9bc61c36002e323421c09bc62dc.tar.gz |
SERVER-28534 Pass collection names around as NamespaceStrings more often
Before this patch 'ns' values were often passed around as std::string or
char* containing either a dbname (no '.'), a dbname with '.$cmd', or a fully qualified
collection name. Instead pass either plain 'dbName' value (as string) or a fully
qualified name using the actual NamespaceString type.
Diffstat (limited to 'src')
42 files changed, 212 insertions, 214 deletions
diff --git a/src/mongo/db/auth/authorization_manager.cpp b/src/mongo/db/auth/authorization_manager.cpp index bca15e13223..66c232813b0 100644 --- a/src/mongo/db/auth/authorization_manager.cpp +++ b/src/mongo/db/auth/authorization_manager.cpp @@ -629,10 +629,10 @@ Status AuthorizationManager::initialize(OperationContext* opCtx) { } namespace { -bool isAuthzNamespace(StringData ns) { - return (ns == AuthorizationManager::rolesCollectionNamespace.ns() || - ns == AuthorizationManager::usersCollectionNamespace.ns() || - ns == AuthorizationManager::versionCollectionNamespace.ns()); +bool isAuthzNamespace(const NamespaceString& nss) { + return (nss == AuthorizationManager::rolesCollectionNamespace || + nss == AuthorizationManager::usersCollectionNamespace || + nss == AuthorizationManager::versionCollectionNamespace); } bool isAuthzCollection(StringData coll) { @@ -641,8 +641,8 @@ bool isAuthzCollection(StringData coll) { coll == AuthorizationManager::versionCollectionNamespace.coll()); } -bool loggedCommandOperatesOnAuthzData(const char* ns, const BSONObj& cmdObj) { - if (ns != AuthorizationManager::adminCommandNamespace.ns()) +bool loggedCommandOperatesOnAuthzData(const NamespaceString& nss, const BSONObj& cmdObj) { + if (nss != AuthorizationManager::adminCommandNamespace) return false; const StringData cmdName(cmdObj.firstElement().fieldNameStringData()); if (cmdName == "drop") { @@ -661,16 +661,16 @@ bool loggedCommandOperatesOnAuthzData(const char* ns, const BSONObj& cmdObj) { } } -bool appliesToAuthzData(const char* op, const char* ns, const BSONObj& o) { +bool appliesToAuthzData(const char* op, const NamespaceString& nss, const BSONObj& o) { switch (*op) { case 'i': case 'u': case 'd': if (op[1] != '\0') return false; // "db" op type - return isAuthzNamespace(ns); + return isAuthzNamespace(nss); case 'c': - return loggedCommandOperatesOnAuthzData(ns, o); + return loggedCommandOperatesOnAuthzData(nss, o); break; case 'n': return false; @@ -701,11 +701,11 @@ void AuthorizationManager::_updateCacheGeneration_inlock() { } void AuthorizationManager::_invalidateRelevantCacheData(const char* op, - const char* ns, + const NamespaceString& ns, const BSONObj& o, const BSONObj* o2) { - if (ns == AuthorizationManager::rolesCollectionNamespace.ns() || - ns == AuthorizationManager::versionCollectionNamespace.ns()) { + if (ns == AuthorizationManager::rolesCollectionNamespace || + ns == AuthorizationManager::versionCollectionNamespace) { invalidateUserCache(); return; } @@ -713,7 +713,7 @@ void AuthorizationManager::_invalidateRelevantCacheData(const char* op, if (*op == 'i' || *op == 'd' || *op == 'u') { // If you got into this function isAuthzNamespace() must have returned true, and we've // already checked that it's not the roles or version collection. - invariant(ns == AuthorizationManager::usersCollectionNamespace.ns()); + invariant(ns == AuthorizationManager::usersCollectionNamespace); StatusWith<UserName> userName = (*op == 'u') ? extractUserNameFromIdString((*o2)["_id"].str()) @@ -732,11 +732,14 @@ void AuthorizationManager::_invalidateRelevantCacheData(const char* op, } } -void AuthorizationManager::logOp( - OperationContext* opCtx, const char* op, const char* ns, const BSONObj& o, const BSONObj* o2) { - if (appliesToAuthzData(op, ns, o)) { - _externalState->logOp(opCtx, op, ns, o, o2); - _invalidateRelevantCacheData(op, ns, o, o2); +void AuthorizationManager::logOp(OperationContext* opCtx, + const char* op, + const NamespaceString& nss, + const BSONObj& o, + const BSONObj* o2) { + if (appliesToAuthzData(op, nss, o)) { + _externalState->logOp(opCtx, op, nss, o, o2); + _invalidateRelevantCacheData(op, nss, o, o2); } } diff --git a/src/mongo/db/auth/authorization_manager.h b/src/mongo/db/auth/authorization_manager.h index 2532bd5217e..495bcc0aa81 100644 --- a/src/mongo/db/auth/authorization_manager.h +++ b/src/mongo/db/auth/authorization_manager.h @@ -303,7 +303,7 @@ public: */ void logOp(OperationContext* opCtx, const char* opstr, - const char* ns, + const NamespaceString& nss, const BSONObj& obj, const BSONObj* patt); @@ -326,7 +326,7 @@ private: * with oplog entries that have been pre-verified to actually affect authorization data. */ void _invalidateRelevantCacheData(const char* op, - const char* ns, + const NamespaceString& ns, const BSONObj& o, const BSONObj* o2); diff --git a/src/mongo/db/auth/authorization_manager_test.cpp b/src/mongo/db/auth/authorization_manager_test.cpp index 83edae4ddef..7eb3c980e3b 100644 --- a/src/mongo/db/auth/authorization_manager_test.cpp +++ b/src/mongo/db/auth/authorization_manager_test.cpp @@ -456,7 +456,7 @@ public: TEST_F(AuthorizationManagerLogOpTest, testDropDatabaseAddsRecoveryUnits) { authzManager->logOp(&opCtx, "c", - "admin.$cmd", + {"admin", "$cmd"}, BSON("dropDatabase" << "1"), nullptr); @@ -466,7 +466,7 @@ TEST_F(AuthorizationManagerLogOpTest, testDropDatabaseAddsRecoveryUnits) { TEST_F(AuthorizationManagerLogOpTest, testDropAuthCollectionAddsRecoveryUnits) { authzManager->logOp(&opCtx, "c", - "admin.$cmd", + {"admin", "$cmd"}, BSON("drop" << "system.users"), nullptr); @@ -474,7 +474,7 @@ TEST_F(AuthorizationManagerLogOpTest, testDropAuthCollectionAddsRecoveryUnits) { authzManager->logOp(&opCtx, "c", - "admin.$cmd", + {"admin", "$cmd"}, BSON("drop" << "system.roles"), nullptr); @@ -482,7 +482,7 @@ TEST_F(AuthorizationManagerLogOpTest, testDropAuthCollectionAddsRecoveryUnits) { authzManager->logOp(&opCtx, "c", - "admin.$cmd", + {"admin", "$cmd"}, BSON("drop" << "system.version"), nullptr); @@ -490,7 +490,7 @@ TEST_F(AuthorizationManagerLogOpTest, testDropAuthCollectionAddsRecoveryUnits) { authzManager->logOp(&opCtx, "c", - "admin.$cmd", + {"admin", "$cmd"}, BSON("drop" << "system.profile"), nullptr); @@ -500,21 +500,21 @@ TEST_F(AuthorizationManagerLogOpTest, testDropAuthCollectionAddsRecoveryUnits) { TEST_F(AuthorizationManagerLogOpTest, testCreateAnyCollectionAddsNoRecoveryUnits) { authzManager->logOp(&opCtx, "c", - "admin.$cmd", + {"admin", "$cmd"}, BSON("create" << "system.users"), nullptr); authzManager->logOp(&opCtx, "c", - "admin.$cmd", + {"admin", "$cmd"}, BSON("create" << "system.profile"), nullptr); authzManager->logOp(&opCtx, "c", - "admin.$cmd", + {"admin", "$cmd"}, BSON("create" << "system.other"), nullptr); @@ -525,7 +525,7 @@ TEST_F(AuthorizationManagerLogOpTest, testCreateAnyCollectionAddsNoRecoveryUnits TEST_F(AuthorizationManagerLogOpTest, testRawInsertToRolesCollectionAddsRecoveryUnits) { authzManager->logOp(&opCtx, "i", - "admin.system.profile", + {"admin", "system.profile"}, BSON("_id" << "admin.user"), nullptr); @@ -533,7 +533,7 @@ TEST_F(AuthorizationManagerLogOpTest, testRawInsertToRolesCollectionAddsRecovery authzManager->logOp(&opCtx, "i", - "admin.system.users", + {"admin", "system.users"}, BSON("_id" << "admin.user"), nullptr); @@ -541,7 +541,7 @@ TEST_F(AuthorizationManagerLogOpTest, testRawInsertToRolesCollectionAddsRecovery authzManager->logOp(&opCtx, "i", - "admin.system.roles", + {"admin", "system.roles"}, BSON("_id" << "admin.user"), nullptr); diff --git a/src/mongo/db/auth/authz_manager_external_state.h b/src/mongo/db/auth/authz_manager_external_state.h index 18277c272b2..f63e05b818f 100644 --- a/src/mongo/db/auth/authz_manager_external_state.h +++ b/src/mongo/db/auth/authz_manager_external_state.h @@ -157,7 +157,7 @@ public: virtual void logOp(OperationContext* opCtx, const char* op, - const char* ns, + const NamespaceString& ns, const BSONObj& o, const BSONObj* o2) {} diff --git a/src/mongo/db/auth/authz_manager_external_state_local.cpp b/src/mongo/db/auth/authz_manager_external_state_local.cpp index de706dcd9da..9227e1d2dd2 100644 --- a/src/mongo/db/auth/authz_manager_external_state_local.cpp +++ b/src/mongo/db/auth/authz_manager_external_state_local.cpp @@ -445,13 +445,13 @@ public: AuthzManagerLogOpHandler(OperationContext* opCtx, AuthzManagerExternalStateLocal* externalState, const char* op, - const char* ns, + const NamespaceString& nss, const BSONObj& o, const BSONObj* o2) : _opCtx(opCtx), _externalState(externalState), _op(op), - _ns(ns), + _nss(nss), _o(o.getOwned()), _isO2Set(o2 ? true : false), @@ -460,13 +460,13 @@ public: virtual void commit() { stdx::lock_guard<stdx::mutex> lk(_externalState->_roleGraphMutex); Status status = _externalState->_roleGraph.handleLogOp( - _opCtx, _op.c_str(), NamespaceString(_ns.c_str()), _o, _isO2Set ? &_o2 : NULL); + _opCtx, _op.c_str(), _nss, _o, _isO2Set ? &_o2 : NULL); if (status == ErrorCodes::OplogOperationUnsupported) { _externalState->_roleGraph = RoleGraph(); _externalState->_roleGraphState = _externalState->roleGraphStateInitial; BSONObjBuilder oplogEntryBuilder; - oplogEntryBuilder << "op" << _op << "ns" << _ns << "o" << _o; + oplogEntryBuilder << "op" << _op << "ns" << _nss.ns() << "o" << _o; if (_isO2Set) oplogEntryBuilder << "o2" << _o2; error() << "Unsupported modification to roles collection in oplog; " @@ -494,19 +494,22 @@ private: OperationContext* _opCtx; AuthzManagerExternalStateLocal* _externalState; const std::string _op; - const std::string _ns; + const NamespaceString _nss; const BSONObj _o; const bool _isO2Set; const BSONObj _o2; }; -void AuthzManagerExternalStateLocal::logOp( - OperationContext* opCtx, const char* op, const char* ns, const BSONObj& o, const BSONObj* o2) { - if (ns == AuthorizationManager::rolesCollectionNamespace.ns() || - ns == AuthorizationManager::adminCommandNamespace.ns()) { +void AuthzManagerExternalStateLocal::logOp(OperationContext* opCtx, + const char* op, + const NamespaceString& nss, + const BSONObj& o, + const BSONObj* o2) { + if (nss == AuthorizationManager::rolesCollectionNamespace || + nss == AuthorizationManager::adminCommandNamespace) { opCtx->recoveryUnit()->registerChange( - new AuthzManagerLogOpHandler(opCtx, this, op, ns, o, o2)); + new AuthzManagerLogOpHandler(opCtx, this, op, nss, o, o2)); } } diff --git a/src/mongo/db/auth/authz_manager_external_state_local.h b/src/mongo/db/auth/authz_manager_external_state_local.h index ccaa7e2d452..d33b8059084 100644 --- a/src/mongo/db/auth/authz_manager_external_state_local.h +++ b/src/mongo/db/auth/authz_manager_external_state_local.h @@ -101,7 +101,7 @@ public: virtual void logOp(OperationContext* opCtx, const char* op, - const char* ns, + const NamespaceString& ns, const BSONObj& o, const BSONObj* o2); diff --git a/src/mongo/db/auth/authz_manager_external_state_mock.cpp b/src/mongo/db/auth/authz_manager_external_state_mock.cpp index ff58770d230..7b8d72ad1f7 100644 --- a/src/mongo/db/auth/authz_manager_external_state_mock.cpp +++ b/src/mongo/db/auth/authz_manager_external_state_mock.cpp @@ -158,7 +158,7 @@ Status AuthzManagerExternalStateMock::insert(OperationContext* opCtx, _documents[collectionName].push_back(toInsert); if (_authzManager) { - _authzManager->logOp(opCtx, "i", collectionName.ns().c_str(), toInsert, NULL); + _authzManager->logOp(opCtx, "i", collectionName, toInsert, NULL); } return Status::OK(); @@ -197,7 +197,7 @@ Status AuthzManagerExternalStateMock::updateOne(OperationContext* opCtx, BSONObj idQuery = driver.makeOplogEntryQuery(newObj, false); if (_authzManager) { - _authzManager->logOp(opCtx, "u", collectionName.ns().c_str(), logObj, &idQuery); + _authzManager->logOp(opCtx, "u", collectionName, logObj, &idQuery); } return Status::OK(); @@ -244,7 +244,7 @@ Status AuthzManagerExternalStateMock::remove(OperationContext* opCtx, ++n; if (_authzManager) { - _authzManager->logOp(opCtx, "d", collectionName.ns().c_str(), idQuery, NULL); + _authzManager->logOp(opCtx, "d", collectionName, idQuery, NULL); } } *numRemoved = n; diff --git a/src/mongo/db/catalog/apply_ops.cpp b/src/mongo/db/catalog/apply_ops.cpp index ca20b2b848a..b138b1f7d74 100644 --- a/src/mongo/db/catalog/apply_ops.cpp +++ b/src/mongo/db/catalog/apply_ops.cpp @@ -122,10 +122,11 @@ Status _applyOps(OperationContext* opCtx, continue; const std::string ns = opObj["ns"].String(); + const NamespaceString nss{ns}; // Need to check this here, or OldClientContext may fail an invariant. - if (*opType != 'c' && !NamespaceString(ns).isValid()) - return {ErrorCodes::InvalidNamespace, "invalid ns: " + ns}; + if (*opType != 'c' && !nss.isValid()) + return {ErrorCodes::InvalidNamespace, "invalid ns: " + nss.ns()}; Status status(ErrorCodes::InternalError, ""); @@ -143,7 +144,7 @@ Status _applyOps(OperationContext* opCtx, status = repl::applyOperation_inlock(opCtx, ctx.db(), opObj, alwaysUpsert); if (!status.isOK()) return status; - logOpForDbHash(opCtx, ns.c_str()); + logOpForDbHash(opCtx, nss); } else { try { // Run operations under a nested lock as a hack to prevent yielding. @@ -186,7 +187,7 @@ Status _applyOps(OperationContext* opCtx, return Status(ErrorCodes::UnknownError, ex.what()); } WriteUnitOfWork wuow(opCtx); - logOpForDbHash(opCtx, ns.c_str()); + logOpForDbHash(opCtx, nss); wuow.commit(); } @@ -207,8 +208,6 @@ Status _applyOps(OperationContext* opCtx, // We want this applied atomically on slaves // so we re-wrap without the pre-condition for speed - std::string tempNS = str::stream() << dbName << ".$cmd"; - // TODO: possibly use mutable BSON to remove preCondition field // once it is available BSONObjBuilder cmdBuilder; @@ -227,7 +226,7 @@ Status _applyOps(OperationContext* opCtx, auto opObserver = getGlobalServiceContext()->getOpObserver(); invariant(opObserver); if (haveWrappingWUOW) { - opObserver->onApplyOps(opCtx, tempNS, cmdRewritten); + opObserver->onApplyOps(opCtx, dbName, cmdRewritten); } else { // When executing applyOps outside of a wrapping WriteUnitOfWOrk, always logOp the // command regardless of whether the individial ops succeeded and rely on any @@ -236,7 +235,7 @@ Status _applyOps(OperationContext* opCtx, while (true) { try { WriteUnitOfWork wunit(opCtx); - opObserver->onApplyOps(opCtx, tempNS, cmdRewritten); + opObserver->onApplyOps(opCtx, dbName, cmdRewritten); wunit.commit(); break; diff --git a/src/mongo/db/catalog/coll_mod.cpp b/src/mongo/db/catalog/coll_mod.cpp index a2df7e2c791..56b0287149d 100644 --- a/src/mongo/db/catalog/coll_mod.cpp +++ b/src/mongo/db/catalog/coll_mod.cpp @@ -345,8 +345,7 @@ Status collMod(OperationContext* opCtx, // Only observe non-view collMods, as view operations are observed as operations on the // system.views collection. - getGlobalServiceContext()->getOpObserver()->onCollMod( - opCtx, (dbName.toString() + ".$cmd").c_str(), cmdObj); + getGlobalServiceContext()->getOpObserver()->onCollMod(opCtx, nss, cmdObj); } wunit.commit(); diff --git a/src/mongo/db/catalog/database.h b/src/mongo/db/catalog/database.h index a41d9dcdffb..6eedbf6b3e7 100644 --- a/src/mongo/db/catalog/database.h +++ b/src/mongo/db/catalog/database.h @@ -201,7 +201,7 @@ public: static Status validateDBName(StringData dbname); - const std::string& getSystemIndexesName() const { + const NamespaceString& getSystemIndexesName() const { return _indexesName; } @@ -239,9 +239,9 @@ private: DatabaseCatalogEntry* _dbEntry; // not owned here - const std::string _profileName; // "dbname.system.profile" - const std::string _indexesName; // "dbname.system.indexes" - const std::string _viewsName; // "dbname.system.views" + const std::string _profileName; // "dbname.system.profile" + const NamespaceString _indexesName; // "dbname.system.indexes" + const std::string _viewsName; // "dbname.system.views" int _profile; // 0=off. diff --git a/src/mongo/db/catalog/drop_database.cpp b/src/mongo/db/catalog/drop_database.cpp index 1819f05b72d..024b097bd56 100644 --- a/src/mongo/db/catalog/drop_database.cpp +++ b/src/mongo/db/catalog/drop_database.cpp @@ -83,7 +83,7 @@ Status dropDatabase(OperationContext* opCtx, const std::string& dbName) { WriteUnitOfWork wunit(opCtx); - getGlobalServiceContext()->getOpObserver()->onDropDatabase(opCtx, dbName + ".$cmd"); + getGlobalServiceContext()->getOpObserver()->onDropDatabase(opCtx, dbName); wunit.commit(); } diff --git a/src/mongo/db/cloner.cpp b/src/mongo/db/cloner.cpp index 2fc1f6f0eaf..01d03fbc7bf 100644 --- a/src/mongo/db/cloner.cpp +++ b/src/mongo/db/cloner.cpp @@ -410,7 +410,7 @@ void Cloner::copyIndexes(OperationContext* opCtx, indexer.commit(); if (opCtx->writesAreReplicated()) { const string targetSystemIndexesCollectionName = to_collection.getSystemIndexesCollection(); - const char* createIndexNs = targetSystemIndexesCollectionName.c_str(); + const NamespaceString createIndexNs{targetSystemIndexesCollectionName}; for (auto&& infoObj : indexInfoObjs) { getGlobalServiceContext()->getOpObserver()->onCreateIndex( opCtx, createIndexNs, infoObj, false); diff --git a/src/mongo/db/commands.h b/src/mongo/db/commands.h index d185c0cf6d6..0c9ffca316b 100644 --- a/src/mongo/db/commands.h +++ b/src/mongo/db/commands.h @@ -509,7 +509,7 @@ private: friend void mongo::execCommandClient(OperationContext* opCtx, Command* c, int queryOptions, - const char* ns, + StringData dbname, BSONObj& cmdObj, BSONObjBuilder& result); diff --git a/src/mongo/db/commands/create_indexes.cpp b/src/mongo/db/commands/create_indexes.cpp index 93833f09b8e..9b821a15843 100644 --- a/src/mongo/db/commands/create_indexes.cpp +++ b/src/mongo/db/commands/create_indexes.cpp @@ -392,7 +392,7 @@ public: indexer.commit(); for (auto&& infoObj : indexInfoObjs) { - std::string systemIndexes = ns.getSystemIndexesCollection(); + NamespaceString systemIndexes{ns.getSystemIndexesCollection()}; getGlobalServiceContext()->getOpObserver()->onCreateIndex( opCtx, systemIndexes, infoObj, false); } diff --git a/src/mongo/db/commands/dbhash.cpp b/src/mongo/db/commands/dbhash.cpp index d1761d1f103..d633cfecd65 100644 --- a/src/mongo/db/commands/dbhash.cpp +++ b/src/mongo/db/commands/dbhash.cpp @@ -273,9 +273,8 @@ private: } // namespace -void logOpForDbHash(OperationContext* opCtx, const char* ns) { - NamespaceString nsString(ns); - dbhashCmd.wipeCacheForCollection(opCtx, nsString); +void logOpForDbHash(OperationContext* opCtx, const NamespaceString& nss) { + dbhashCmd.wipeCacheForCollection(opCtx, nss); } } // namespace mongo diff --git a/src/mongo/db/commands/dbhash.h b/src/mongo/db/commands/dbhash.h index 09db7e97e0e..29ec66ef9cf 100644 --- a/src/mongo/db/commands/dbhash.h +++ b/src/mongo/db/commands/dbhash.h @@ -31,7 +31,8 @@ namespace mongo { class OperationContext; +class NamespaceString; -void logOpForDbHash(OperationContext* opCtx, const char* ns); +void logOpForDbHash(OperationContext* opCtx, const NamespaceString& nss); } // namespace mongo diff --git a/src/mongo/db/commands/mr.cpp b/src/mongo/db/commands/mr.cpp index 50e6edc44a5..89668c818e7 100644 --- a/src/mongo/db/commands/mr.cpp +++ b/src/mongo/db/commands/mr.cpp @@ -510,7 +510,7 @@ void State::prepTempCollection() { uassertStatusOK(status); } // Log the createIndex operation. - string logNs = _config.tempNamespace.db() + ".system.indexes"; + NamespaceString logNs{_config.tempNamespace.db(), "system.indexes"}; getGlobalServiceContext()->getOpObserver()->onCreateIndex(_opCtx, logNs, *it, false); } wuow.commit(); diff --git a/src/mongo/db/commands_helpers.h b/src/mongo/db/commands_helpers.h index 0f559a3af86..5880c184cdf 100644 --- a/src/mongo/db/commands_helpers.h +++ b/src/mongo/db/commands_helpers.h @@ -48,7 +48,7 @@ class ReplyBuilderInterface; void execCommandClient(OperationContext* opCtx, Command* c, int queryOptions, - const char* ns, + StringData dbname, BSONObj& cmdObj, BSONObjBuilder& result); diff --git a/src/mongo/db/dbhelpers.cpp b/src/mongo/db/dbhelpers.cpp index ae26b591272..c247d31a6d1 100644 --- a/src/mongo/db/dbhelpers.cpp +++ b/src/mongo/db/dbhelpers.cpp @@ -167,7 +167,7 @@ RecordId Helpers::findOne(OperationContext* opCtx, bool Helpers::findById(OperationContext* opCtx, Database* database, - const char* ns, + StringData ns, BSONObj query, BSONObj& result, bool* nsFound, @@ -493,11 +493,11 @@ long long Helpers::removeRange(OperationContext* opCtx, return numDeleted; } -void Helpers::emptyCollection(OperationContext* opCtx, const char* ns) { - OldClientContext context(opCtx, ns); +void Helpers::emptyCollection(OperationContext* opCtx, const NamespaceString& nss) { + OldClientContext context(opCtx, nss.ns()); repl::UnreplicatedWritesBlock uwb(opCtx); - Collection* collection = context.db() ? context.db()->getCollection(ns) : nullptr; - deleteObjects(opCtx, collection, ns, BSONObj(), PlanExecutor::YIELD_MANUAL, false); + Collection* collection = context.db() ? context.db()->getCollection(nss.ns()) : nullptr; + deleteObjects(opCtx, collection, nss, BSONObj(), PlanExecutor::YIELD_MANUAL, false); } Helpers::RemoveSaver::RemoveSaver(const string& a, const string& b, const string& why) { diff --git a/src/mongo/db/dbhelpers.h b/src/mongo/db/dbhelpers.h index 3ff0318b687..d143c418dbf 100644 --- a/src/mongo/db/dbhelpers.h +++ b/src/mongo/db/dbhelpers.h @@ -99,7 +99,7 @@ struct Helpers { */ static bool findById(OperationContext* opCtx, Database* db, - const char* ns, + StringData ns, BSONObj query, BSONObj& result, bool* nsFound = 0, @@ -184,7 +184,7 @@ struct Helpers { * You do not need to set the database before calling. * Does not oplog the operation. */ - static void emptyCollection(OperationContext* opCtx, const char* ns); + static void emptyCollection(OperationContext* opCtx, const NamespaceString& nss); /** * for saving deleted bson objects to a flat file diff --git a/src/mongo/db/exec/update.cpp b/src/mongo/db/exec/update.cpp index 9a9b6c36a07..3dca5184810 100644 --- a/src/mongo/db/exec/update.cpp +++ b/src/mongo/db/exec/update.cpp @@ -666,7 +666,7 @@ BSONObj UpdateStage::transformAndUpdate(const Snapshotted<BSONObj>& oldObj, Reco const RecordData oldRec(oldObj.value().objdata(), oldObj.value().objsize()); BSONObj idQuery = driver->makeOplogEntryQuery(newObj, request->isMulti()); OplogUpdateEntryArgs args; - args.ns = _collection->ns().ns(); + args.nss = _collection->ns(); args.update = logObj; args.criteria = idQuery; args.fromMigrate = request->isFromMigration(); @@ -695,7 +695,7 @@ BSONObj UpdateStage::transformAndUpdate(const Snapshotted<BSONObj>& oldObj, Reco invariant(_collection); BSONObj idQuery = driver->makeOplogEntryQuery(newObj, request->isMulti()); OplogUpdateEntryArgs args; - args.ns = _collection->ns().ns(); + args.nss = _collection->ns(); args.update = logObj; args.criteria = idQuery; args.fromMigrate = request->isFromMigration(); diff --git a/src/mongo/db/namespace_string-inl.h b/src/mongo/db/namespace_string-inl.h index 1377d38ce41..c3bb251b98f 100644 --- a/src/mongo/db/namespace_string-inl.h +++ b/src/mongo/db/namespace_string-inl.h @@ -167,8 +167,8 @@ inline std::string NamespaceString::getSystemIndexesCollection() const { return db().toString() + ".system.indexes"; } -inline std::string NamespaceString::getCommandNS() const { - return db().toString() + ".$cmd"; +inline NamespaceString NamespaceString::getCommandNS() const { + return {db(), "$cmd"}; } } // namespace mongo diff --git a/src/mongo/db/namespace_string.h b/src/mongo/db/namespace_string.h index 58f26d76b2c..9a88f67a9a3 100644 --- a/src/mongo/db/namespace_string.h +++ b/src/mongo/db/namespace_string.h @@ -263,8 +263,8 @@ public: // @return db() + ".system.indexes" std::string getSystemIndexesCollection() const; - // @return db() + ".$cmd" - std::string getCommandNS() const; + // @return {db(), "$cmd"} + NamespaceString getCommandNS() const; /** * Function to escape most non-alpha characters from file names diff --git a/src/mongo/db/op_observer.h b/src/mongo/db/op_observer.h index 43142ea5cee..2cdc981bb74 100644 --- a/src/mongo/db/op_observer.h +++ b/src/mongo/db/op_observer.h @@ -44,7 +44,7 @@ class OperationContext; */ struct OplogUpdateEntryArgs { // Name of the collection in which document is being updated. - std::string ns; + NamespaceString nss; // Fully updated document with damages (update modifiers) applied. BSONObj updatedDoc; @@ -67,7 +67,7 @@ public: virtual ~OpObserver() = default; virtual void onCreateIndex(OperationContext* opCtx, - const std::string& ns, + const NamespaceString& ns, BSONObj indexDoc, bool fromMigrate) = 0; virtual void onInserts(OperationContext* opCtx, @@ -98,7 +98,7 @@ public: const CollectionOptions& options, const BSONObj& idIndex) = 0; virtual void onCollMod(OperationContext* opCtx, - const std::string& dbName, + const NamespaceString& nss, const BSONObj& collModCmd) = 0; virtual void onDropDatabase(OperationContext* opCtx, const std::string& dbName) = 0; virtual void onDropCollection(OperationContext* opCtx, diff --git a/src/mongo/db/op_observer_impl.cpp b/src/mongo/db/op_observer_impl.cpp index ea0d64f6f09..19f16cb368c 100644 --- a/src/mongo/db/op_observer_impl.cpp +++ b/src/mongo/db/op_observer_impl.cpp @@ -46,19 +46,18 @@ namespace mongo { void OpObserverImpl::onCreateIndex(OperationContext* opCtx, - const std::string& ns, + const NamespaceString& ns, BSONObj indexDoc, bool fromMigrate) { - repl::logOp(opCtx, "i", ns.c_str(), indexDoc, nullptr, fromMigrate); - AuthorizationManager::get(opCtx->getServiceContext()) - ->logOp(opCtx, "i", ns.c_str(), indexDoc, nullptr); + repl::logOp(opCtx, "i", ns, indexDoc, nullptr, fromMigrate); + AuthorizationManager::get(opCtx->getServiceContext())->logOp(opCtx, "i", ns, indexDoc, nullptr); auto css = CollectionShardingState::get(opCtx, ns); if (!fromMigrate) { css->onInsertOp(opCtx, indexDoc); } - logOpForDbHash(opCtx, ns.c_str()); + logOpForDbHash(opCtx, ns); } void OpObserverImpl::onInserts(OperationContext* opCtx, @@ -69,10 +68,9 @@ void OpObserverImpl::onInserts(OperationContext* opCtx, repl::logOps(opCtx, "i", nss, begin, end, fromMigrate); auto css = CollectionShardingState::get(opCtx, nss.ns()); - const char* ns = nss.ns().c_str(); for (auto it = begin; it != end; it++) { - AuthorizationManager::get(opCtx->getServiceContext())->logOp(opCtx, "i", ns, *it, nullptr); + AuthorizationManager::get(opCtx->getServiceContext())->logOp(opCtx, "i", nss, *it, nullptr); if (!fromMigrate) { css->onInsertOp(opCtx, *it); } @@ -84,8 +82,8 @@ void OpObserverImpl::onInserts(OperationContext* opCtx, } } - logOpForDbHash(opCtx, ns); - if (strstr(ns, ".system.js")) { + logOpForDbHash(opCtx, nss); + if (strstr(nss.ns().c_str(), ".system.js")) { Scope::storedFuncMod(opCtx); } if (nss.coll() == DurableViewCatalog::viewsCollectionName()) { @@ -99,26 +97,25 @@ void OpObserverImpl::onUpdate(OperationContext* opCtx, const OplogUpdateEntryArg return; } - repl::logOp(opCtx, "u", args.ns.c_str(), args.update, &args.criteria, args.fromMigrate); + repl::logOp(opCtx, "u", args.nss, args.update, &args.criteria, args.fromMigrate); AuthorizationManager::get(opCtx->getServiceContext()) - ->logOp(opCtx, "u", args.ns.c_str(), args.update, &args.criteria); + ->logOp(opCtx, "u", args.nss, args.update, &args.criteria); - auto css = CollectionShardingState::get(opCtx, args.ns); + auto css = CollectionShardingState::get(opCtx, args.nss); if (!args.fromMigrate) { css->onUpdateOp(opCtx, args.updatedDoc); } - logOpForDbHash(opCtx, args.ns.c_str()); - if (strstr(args.ns.c_str(), ".system.js")) { + logOpForDbHash(opCtx, args.nss); + if (strstr(args.nss.ns().c_str(), ".system.js")) { Scope::storedFuncMod(opCtx); } - NamespaceString nss(args.ns); - if (nss.coll() == DurableViewCatalog::viewsCollectionName()) { - DurableViewCatalog::onExternalChange(opCtx, nss); + if (args.nss.coll() == DurableViewCatalog::viewsCollectionName()) { + DurableViewCatalog::onExternalChange(opCtx, args.nss); } - if (args.ns == FeatureCompatibilityVersion::kCollection) { + if (args.nss.ns() == FeatureCompatibilityVersion::kCollection) { FeatureCompatibilityVersion::onInsertOrUpdate(args.updatedDoc); } } @@ -139,42 +136,42 @@ CollectionShardingState::DeleteState OpObserverImpl::aboutToDelete(OperationCont } void OpObserverImpl::onDelete(OperationContext* opCtx, - const NamespaceString& ns, + const NamespaceString& nss, CollectionShardingState::DeleteState deleteState, bool fromMigrate) { if (deleteState.idDoc.isEmpty()) return; - repl::logOp(opCtx, "d", ns.ns().c_str(), deleteState.idDoc, nullptr, fromMigrate); + repl::logOp(opCtx, "d", nss, deleteState.idDoc, nullptr, fromMigrate); AuthorizationManager::get(opCtx->getServiceContext()) - ->logOp(opCtx, "d", ns.ns().c_str(), deleteState.idDoc, nullptr); + ->logOp(opCtx, "d", nss, deleteState.idDoc, nullptr); - auto css = CollectionShardingState::get(opCtx, ns.ns()); + auto css = CollectionShardingState::get(opCtx, nss.ns()); if (!fromMigrate) { css->onDeleteOp(opCtx, deleteState); } - logOpForDbHash(opCtx, ns.ns().c_str()); - if (ns.coll() == "system.js") { + logOpForDbHash(opCtx, nss); + if (nss.coll() == "system.js") { Scope::storedFuncMod(opCtx); } - if (ns.coll() == DurableViewCatalog::viewsCollectionName()) { - DurableViewCatalog::onExternalChange(opCtx, ns); + if (nss.coll() == DurableViewCatalog::viewsCollectionName()) { + DurableViewCatalog::onExternalChange(opCtx, nss); } - if (ns.ns() == FeatureCompatibilityVersion::kCollection) { + if (nss.ns() == FeatureCompatibilityVersion::kCollection) { FeatureCompatibilityVersion::onDelete(deleteState.idDoc); } } void OpObserverImpl::onOpMessage(OperationContext* opCtx, const BSONObj& msgObj) { - repl::logOp(opCtx, "n", "", msgObj, nullptr, false); + repl::logOp(opCtx, "n", {}, msgObj, nullptr, false); } void OpObserverImpl::onCreateCollection(OperationContext* opCtx, const NamespaceString& collectionName, const CollectionOptions& options, const BSONObj& idIndex) { - std::string dbName = collectionName.db().toString() + ".$cmd"; + const NamespaceString dbName = collectionName.getCommandNS(); BSONObjBuilder b; b.append("create", collectionName.coll().toString()); b.appendElements(options.toBSON()); @@ -193,49 +190,49 @@ void OpObserverImpl::onCreateCollection(OperationContext* opCtx, if (!collectionName.isSystemDotProfile()) { // do not replicate system.profile modifications - repl::logOp(opCtx, "c", dbName.c_str(), cmdObj, nullptr, false); + repl::logOp(opCtx, "c", dbName, cmdObj, nullptr, false); } - getGlobalAuthorizationManager()->logOp(opCtx, "c", dbName.c_str(), cmdObj, nullptr); - logOpForDbHash(opCtx, dbName.c_str()); + getGlobalAuthorizationManager()->logOp(opCtx, "c", dbName, cmdObj, nullptr); + logOpForDbHash(opCtx, dbName); } void OpObserverImpl::onCollMod(OperationContext* opCtx, - const std::string& dbName, + const NamespaceString& nss, const BSONObj& collModCmd) { - BSONElement first = collModCmd.firstElement(); - std::string coll = first.valuestr(); + const NamespaceString cmdNss = nss.getCommandNS(); - if (!NamespaceString(NamespaceString(dbName).db(), coll).isSystemDotProfile()) { + if (!nss.isSystemDotProfile()) { // do not replicate system.profile modifications - repl::logOp(opCtx, "c", dbName.c_str(), collModCmd, nullptr, false); + repl::logOp(opCtx, "c", cmdNss, collModCmd, nullptr, false); } - getGlobalAuthorizationManager()->logOp(opCtx, "c", dbName.c_str(), collModCmd, nullptr); - logOpForDbHash(opCtx, dbName.c_str()); + getGlobalAuthorizationManager()->logOp(opCtx, "c", cmdNss, collModCmd, nullptr); + logOpForDbHash(opCtx, cmdNss); } void OpObserverImpl::onDropDatabase(OperationContext* opCtx, const std::string& dbName) { BSONObj cmdObj = BSON("dropDatabase" << 1); + const NamespaceString cmdNss{dbName, "$cmd"}; - repl::logOp(opCtx, "c", dbName.c_str(), cmdObj, nullptr, false); + repl::logOp(opCtx, "c", cmdNss, cmdObj, nullptr, false); - if (NamespaceString(dbName).db() == FeatureCompatibilityVersion::kDatabase) { + if (dbName == FeatureCompatibilityVersion::kDatabase) { FeatureCompatibilityVersion::onDropCollection(); } - getGlobalAuthorizationManager()->logOp(opCtx, "c", dbName.c_str(), cmdObj, nullptr); - logOpForDbHash(opCtx, dbName.c_str()); + getGlobalAuthorizationManager()->logOp(opCtx, "c", cmdNss, cmdObj, nullptr); + logOpForDbHash(opCtx, cmdNss); } void OpObserverImpl::onDropCollection(OperationContext* opCtx, const NamespaceString& collectionName) { - std::string dbName = collectionName.db().toString() + ".$cmd"; + const NamespaceString dbName = collectionName.getCommandNS(); BSONObj cmdObj = BSON("drop" << collectionName.coll().toString()); if (!collectionName.isSystemDotProfile()) { // do not replicate system.profile modifications - repl::logOp(opCtx, "c", dbName.c_str(), cmdObj, nullptr, false); + repl::logOp(opCtx, "c", dbName, cmdObj, nullptr, false); } if (collectionName.coll() == DurableViewCatalog::viewsCollectionName()) { @@ -246,12 +243,12 @@ void OpObserverImpl::onDropCollection(OperationContext* opCtx, FeatureCompatibilityVersion::onDropCollection(); } - getGlobalAuthorizationManager()->logOp(opCtx, "c", dbName.c_str(), cmdObj, nullptr); + getGlobalAuthorizationManager()->logOp(opCtx, "c", dbName, cmdObj, nullptr); auto css = CollectionShardingState::get(opCtx, collectionName); css->onDropCollection(opCtx, collectionName); - logOpForDbHash(opCtx, dbName.c_str()); + logOpForDbHash(opCtx, dbName); } void OpObserverImpl::onDropIndex(OperationContext* opCtx, @@ -260,10 +257,10 @@ void OpObserverImpl::onDropIndex(OperationContext* opCtx, const BSONObj& indexInfo) { BSONObj cmdObj = BSON("dropIndexes" << ns.coll() << "index" << indexName); auto commandNS = ns.getCommandNS(); - repl::logOp(opCtx, "c", commandNS.c_str(), cmdObj, &indexInfo, false); + repl::logOp(opCtx, "c", commandNS, cmdObj, &indexInfo, false); - getGlobalAuthorizationManager()->logOp(opCtx, "c", commandNS.c_str(), cmdObj, &indexInfo); - logOpForDbHash(opCtx, commandNS.c_str()); + getGlobalAuthorizationManager()->logOp(opCtx, "c", commandNS, cmdObj, &indexInfo); + logOpForDbHash(opCtx, commandNS); } void OpObserverImpl::onRenameCollection(OperationContext* opCtx, @@ -271,59 +268,60 @@ void OpObserverImpl::onRenameCollection(OperationContext* opCtx, const NamespaceString& toCollection, bool dropTarget, bool stayTemp) { - std::string dbName = fromCollection.db().toString() + ".$cmd"; + const NamespaceString cmdNss = fromCollection.getCommandNS(); BSONObj cmdObj = BSON("renameCollection" << fromCollection.ns() << "to" << toCollection.ns() << "stayTemp" << stayTemp << "dropTarget" << dropTarget); - repl::logOp(opCtx, "c", dbName.c_str(), cmdObj, nullptr, false); + repl::logOp(opCtx, "c", cmdNss, cmdObj, nullptr, false); if (fromCollection.coll() == DurableViewCatalog::viewsCollectionName() || toCollection.coll() == DurableViewCatalog::viewsCollectionName()) { DurableViewCatalog::onExternalChange( opCtx, NamespaceString(DurableViewCatalog::viewsCollectionName())); } - getGlobalAuthorizationManager()->logOp(opCtx, "c", dbName.c_str(), cmdObj, nullptr); - logOpForDbHash(opCtx, dbName.c_str()); + getGlobalAuthorizationManager()->logOp(opCtx, "c", cmdNss, cmdObj, nullptr); + logOpForDbHash(opCtx, cmdNss); } void OpObserverImpl::onApplyOps(OperationContext* opCtx, const std::string& dbName, const BSONObj& applyOpCmd) { - repl::logOp(opCtx, "c", dbName.c_str(), applyOpCmd, nullptr, false); + const NamespaceString cmdNss{dbName, "$cmd"}; + repl::logOp(opCtx, "c", cmdNss, applyOpCmd, nullptr, false); - getGlobalAuthorizationManager()->logOp(opCtx, "c", dbName.c_str(), applyOpCmd, nullptr); - logOpForDbHash(opCtx, dbName.c_str()); + getGlobalAuthorizationManager()->logOp(opCtx, "c", cmdNss, applyOpCmd, nullptr); + logOpForDbHash(opCtx, cmdNss); } void OpObserverImpl::onConvertToCapped(OperationContext* opCtx, const NamespaceString& collectionName, double size) { - std::string dbName = collectionName.db().toString() + ".$cmd"; + const NamespaceString cmdNss = collectionName.getCommandNS(); BSONObj cmdObj = BSON("convertToCapped" << collectionName.coll() << "size" << size); if (!collectionName.isSystemDotProfile()) { // do not replicate system.profile modifications - repl::logOp(opCtx, "c", dbName.c_str(), cmdObj, nullptr, false); + repl::logOp(opCtx, "c", cmdNss, cmdObj, nullptr, false); } - getGlobalAuthorizationManager()->logOp(opCtx, "c", dbName.c_str(), cmdObj, nullptr); - logOpForDbHash(opCtx, dbName.c_str()); + getGlobalAuthorizationManager()->logOp(opCtx, "c", cmdNss, cmdObj, nullptr); + logOpForDbHash(opCtx, cmdNss); } void OpObserverImpl::onEmptyCapped(OperationContext* opCtx, const NamespaceString& collectionName) { - std::string dbName = collectionName.db().toString() + ".$cmd"; + const NamespaceString cmdNss = collectionName.getCommandNS(); BSONObj cmdObj = BSON("emptycapped" << collectionName.coll()); if (!collectionName.isSystemDotProfile()) { // do not replicate system.profile modifications - repl::logOp(opCtx, "c", dbName.c_str(), cmdObj, nullptr, false); + repl::logOp(opCtx, "c", cmdNss, cmdObj, nullptr, false); } - getGlobalAuthorizationManager()->logOp(opCtx, "c", dbName.c_str(), cmdObj, nullptr); - logOpForDbHash(opCtx, dbName.c_str()); + getGlobalAuthorizationManager()->logOp(opCtx, "c", cmdNss, cmdObj, nullptr); + logOpForDbHash(opCtx, cmdNss); } } // namespace mongo diff --git a/src/mongo/db/op_observer_impl.h b/src/mongo/db/op_observer_impl.h index b02b7c96955..28e08a3f0b1 100644 --- a/src/mongo/db/op_observer_impl.h +++ b/src/mongo/db/op_observer_impl.h @@ -40,7 +40,7 @@ public: virtual ~OpObserverImpl() = default; void onCreateIndex(OperationContext* opCtx, - const std::string& ns, + const NamespaceString& ns, BSONObj indexDoc, bool fromMigrate) override; void onInserts(OperationContext* opCtx, @@ -62,7 +62,7 @@ public: const CollectionOptions& options, const BSONObj& idIndex) override; void onCollMod(OperationContext* opCtx, - const std::string& dbName, + const NamespaceString& nss, const BSONObj& collModCmd) override; void onDropDatabase(OperationContext* opCtx, const std::string& dbName) override; void onDropCollection(OperationContext* opCtx, const NamespaceString& collectionName) override; diff --git a/src/mongo/db/op_observer_noop.cpp b/src/mongo/db/op_observer_noop.cpp index 93404bd0ccc..426df480b13 100644 --- a/src/mongo/db/op_observer_noop.cpp +++ b/src/mongo/db/op_observer_noop.cpp @@ -32,7 +32,7 @@ namespace mongo { -void OpObserverNoop::onCreateIndex(OperationContext*, const std::string&, BSONObj, bool) {} +void OpObserverNoop::onCreateIndex(OperationContext*, const NamespaceString&, BSONObj, bool) {} void OpObserverNoop::onInserts(OperationContext*, const NamespaceString&, @@ -60,7 +60,7 @@ void OpObserverNoop::onCreateCollection(OperationContext*, const CollectionOptions&, const BSONObj&) {} -void OpObserverNoop::onCollMod(OperationContext*, const std::string&, const BSONObj&) {} +void OpObserverNoop::onCollMod(OperationContext*, const NamespaceString&, const BSONObj&) {} void OpObserverNoop::onDropDatabase(OperationContext*, const std::string&) {} diff --git a/src/mongo/db/op_observer_noop.h b/src/mongo/db/op_observer_noop.h index 675fff217f5..490c7457cdd 100644 --- a/src/mongo/db/op_observer_noop.h +++ b/src/mongo/db/op_observer_noop.h @@ -40,7 +40,7 @@ public: virtual ~OpObserverNoop() = default; void onCreateIndex(OperationContext* opCtx, - const std::string& ns, + const NamespaceString& ns, BSONObj indexDoc, bool fromMigrate) override; void onInserts(OperationContext* opCtx, @@ -62,7 +62,7 @@ public: const CollectionOptions& options, const BSONObj& idIndex) override; void onCollMod(OperationContext* opCtx, - const std::string& dbName, + const NamespaceString& dbName, const BSONObj& collModCmd) override; void onDropDatabase(OperationContext* opCtx, const std::string& dbName) override; void onDropCollection(OperationContext* opCtx, const NamespaceString& collectionName) override; diff --git a/src/mongo/db/ops/delete.cpp b/src/mongo/db/ops/delete.cpp index 636a9aa17ee..19fc3eb7458 100644 --- a/src/mongo/db/ops/delete.cpp +++ b/src/mongo/db/ops/delete.cpp @@ -46,14 +46,13 @@ namespace mongo { */ long long deleteObjects(OperationContext* opCtx, Collection* collection, - StringData ns, + const NamespaceString& ns, BSONObj pattern, PlanExecutor::YieldPolicy policy, bool justOne, bool god, bool fromMigrate) { - NamespaceString nsString(ns); - DeleteRequest request(nsString); + DeleteRequest request(ns); request.setQuery(pattern); request.setMulti(!justOne); request.setGod(god); diff --git a/src/mongo/db/ops/delete.h b/src/mongo/db/ops/delete.h index bead641a0b8..bb7427f5bb5 100644 --- a/src/mongo/db/ops/delete.h +++ b/src/mongo/db/ops/delete.h @@ -41,7 +41,7 @@ class OperationContext; long long deleteObjects(OperationContext* opCtx, Collection* collection, - StringData ns, + const NamespaceString& ns, BSONObj pattern, PlanExecutor::YieldPolicy policy, bool justOne, diff --git a/src/mongo/db/repl/master_slave.cpp b/src/mongo/db/repl/master_slave.cpp index e6e4f99a883..163de670c0d 100644 --- a/src/mongo/db/repl/master_slave.cpp +++ b/src/mongo/db/repl/master_slave.cpp @@ -199,7 +199,7 @@ void ReplSource::ensureMe(OperationContext* opCtx) { Lock::DBLock dblk(opCtx, "local", MODE_X); WriteUnitOfWork wunit(opCtx); // clean out local.me - Helpers::emptyCollection(opCtx, "local.me"); + Helpers::emptyCollection(opCtx, NamespaceString("local.me")); // repopulate BSONObjBuilder b; diff --git a/src/mongo/db/repl/oplog.cpp b/src/mongo/db/repl/oplog.cpp index 51a44a672ed..fd32b0beae7 100644 --- a/src/mongo/db/repl/oplog.cpp +++ b/src/mongo/db/repl/oplog.cpp @@ -390,13 +390,12 @@ void _logOpsInner(OperationContext* opCtx, void logOp(OperationContext* opCtx, const char* opstr, - const char* ns, + const NamespaceString& nss, const BSONObj& obj, const BSONObj* o2, bool fromMigrate) { ReplicationCoordinator::Mode replMode = ReplicationCoordinator::get(opCtx)->getReplicationMode(); - NamespaceString nss(ns); if (oplogDisabled(opCtx, replMode, nss)) return; @@ -705,6 +704,7 @@ Status applyOperation_inlock(OperationContext* opCtx, "'ns' must be of type String", fieldNs.type() == BSONType::String); const StringData ns = fieldNs.valueStringData(); + NamespaceString requestNss{ns}; BSONObj o2; if (fieldO2.isABSONObj()) @@ -885,13 +885,12 @@ Status applyOperation_inlock(OperationContext* opCtx, BSONObjBuilder b; b.append(o.getField("_id")); - const NamespaceString requestNs(ns); - UpdateRequest request(requestNs); + UpdateRequest request(requestNss); request.setQuery(b.done()); request.setUpdates(o); request.setUpsert(); - UpdateLifecycleImpl updateLifecycle(requestNs); + UpdateLifecycleImpl updateLifecycle(requestNss); request.setLifecycle(&updateLifecycle); UpdateResult res = update(opCtx, db, request); @@ -916,13 +915,12 @@ Status applyOperation_inlock(OperationContext* opCtx, str::stream() << "Failed to apply update due to missing _id: " << op.toString(), updateCriteria.hasField("_id")); - const NamespaceString requestNs(ns); - UpdateRequest request(requestNs); + UpdateRequest request(requestNss); request.setQuery(updateCriteria); request.setUpdates(o); request.setUpsert(upsert); - UpdateLifecycleImpl updateLifecycle(requestNs); + UpdateLifecycleImpl updateLifecycle(requestNss); request.setLifecycle(&updateLifecycle); UpdateResult ur = update(opCtx, db, request); @@ -976,7 +974,8 @@ Status applyOperation_inlock(OperationContext* opCtx, o.hasField("_id")); if (opType[1] == 0) { - deleteObjects(opCtx, collection, ns, o, PlanExecutor::YIELD_MANUAL, /*justOne*/ valueB); + deleteObjects( + opCtx, collection, requestNss, o, PlanExecutor::YIELD_MANUAL, /*justOne*/ valueB); } else verify(opType[1] == 'b'); // "db" advertisement if (incrementOpsAppliedStats) { @@ -999,7 +998,7 @@ Status applyOperation_inlock(OperationContext* opCtx, // observers. WriteUnitOfWork wuow(opCtx); getGlobalAuthorizationManager()->logOp( - opCtx, opType, ns.toString().c_str(), o, fieldO2.isABSONObj() ? &o2 : NULL); + opCtx, opType, requestNss, o, fieldO2.isABSONObj() ? &o2 : NULL); wuow.commit(); return Status::OK(); @@ -1115,7 +1114,7 @@ Status applyCommand_inlock(OperationContext* opCtx, // AuthorizationManager's logOp method registers a RecoveryUnit::Change // and to do so we need to have begun a UnitOfWork WriteUnitOfWork wuow(opCtx); - getGlobalAuthorizationManager()->logOp(opCtx, opType, nss.ns().c_str(), o, nullptr); + getGlobalAuthorizationManager()->logOp(opCtx, opType, nss, o, nullptr); wuow.commit(); return Status::OK(); diff --git a/src/mongo/db/repl/oplog.h b/src/mongo/db/repl/oplog.h index fcff70fb607..782a15a1725 100644 --- a/src/mongo/db/repl/oplog.h +++ b/src/mongo/db/repl/oplog.h @@ -93,7 +93,7 @@ void logOps(OperationContext* opCtx, */ void logOp(OperationContext* opCtx, const char* opstr, - const char* ns, + const NamespaceString& ns, const BSONObj& obj, const BSONObj* o2, bool fromMigrate); diff --git a/src/mongo/db/repl/replication_coordinator_external_state_impl.cpp b/src/mongo/db/repl/replication_coordinator_external_state_impl.cpp index 5025269fa4e..925704e7f84 100644 --- a/src/mongo/db/repl/replication_coordinator_external_state_impl.cpp +++ b/src/mongo/db/repl/replication_coordinator_external_state_impl.cpp @@ -427,7 +427,7 @@ OID ReplicationCoordinatorExternalStateImpl::ensureMe(OperationContext* opCtx) { myRID = OID::gen(); // clean out local.me - Helpers::emptyCollection(opCtx, meCollectionName); + Helpers::emptyCollection(opCtx, NamespaceString(meCollectionName)); // repopulate BSONObjBuilder b; diff --git a/src/mongo/db/repl/rs_rollback.cpp b/src/mongo/db/repl/rs_rollback.cpp index d32404132e4..b17e80c6035 100644 --- a/src/mongo/db/repl/rs_rollback.cpp +++ b/src/mongo/db/repl/rs_rollback.cpp @@ -718,7 +718,7 @@ void syncFixUp(OperationContext* opCtx, } else { deleteObjects(opCtx, collection, - doc.ns, + docNss, pattern, PlanExecutor::YIELD_MANUAL, true, // justone @@ -729,14 +729,13 @@ void syncFixUp(OperationContext* opCtx, // TODO faster... updates++; - const NamespaceString requestNs(doc.ns); - UpdateRequest request(requestNs); + UpdateRequest request(docNss); request.setQuery(pattern); request.setUpdates(idAndDoc.second); request.setGod(); request.setUpsert(); - UpdateLifecycleImpl updateLifecycle(requestNs); + UpdateLifecycleImpl updateLifecycle(docNss); request.setLifecycle(&updateLifecycle); update(opCtx, ctx.db(), request); diff --git a/src/mongo/db/repl/sync_tail_test.cpp b/src/mongo/db/repl/sync_tail_test.cpp index 2fe5ea8b8d4..5ce737f5d11 100644 --- a/src/mongo/db/repl/sync_tail_test.cpp +++ b/src/mongo/db/repl/sync_tail_test.cpp @@ -200,7 +200,7 @@ OplogEntry makeCommandOplogEntry(OpTime opTime, bob.append("h", 1LL); bob.append("v", 2); bob.append("op", "c"); - bob.append("ns", nss.getCommandNS()); + bob.append("ns", nss.getCommandNS().ns()); bob.append("o", command); return OplogEntry(bob.obj()); } diff --git a/src/mongo/db/s/migration_destination_manager.cpp b/src/mongo/db/s/migration_destination_manager.cpp index e9cc1fff46b..2f9355dcdd4 100644 --- a/src/mongo/db/s/migration_destination_manager.cpp +++ b/src/mongo/db/s/migration_destination_manager.cpp @@ -121,7 +121,7 @@ bool isInRange(const BSONObj& obj, * TODO: Could optimize this check out if sharding on _id. */ bool willOverrideLocalId(OperationContext* opCtx, - const string& ns, + const NamespaceString& nss, BSONObj min, BSONObj max, BSONObj shardKeyPattern, @@ -129,7 +129,7 @@ bool willOverrideLocalId(OperationContext* opCtx, BSONObj remoteDoc, BSONObj* localDoc) { *localDoc = BSONObj(); - if (Helpers::findById(opCtx, db, ns.c_str(), remoteDoc, *localDoc)) { + if (Helpers::findById(opCtx, db, nss.ns(), remoteDoc, *localDoc)) { return !isInRange(*localDoc, min, max, shardKeyPattern); } @@ -670,7 +670,7 @@ void MigrationDestinationManager::_migrateDriver(OperationContext* opCtx, BSONObj localDoc; if (willOverrideLocalId(opCtx, - _nss.ns(), + _nss, min, max, shardKeyPattern, @@ -746,7 +746,7 @@ void MigrationDestinationManager::_migrateDriver(OperationContext* opCtx, break; } - _applyMigrateOp(opCtx, _nss.ns(), min, max, shardKeyPattern, res, &lastOpApplied); + _applyMigrateOp(opCtx, _nss, min, max, shardKeyPattern, res, &lastOpApplied); const int maxIterations = 3600 * 50; @@ -837,7 +837,7 @@ void MigrationDestinationManager::_migrateDriver(OperationContext* opCtx, } if (res["size"].number() > 0 && - _applyMigrateOp(opCtx, _nss.ns(), min, max, shardKeyPattern, res, &lastOpApplied)) { + _applyMigrateOp(opCtx, _nss, min, max, shardKeyPattern, res, &lastOpApplied)) { continue; } @@ -879,7 +879,7 @@ void MigrationDestinationManager::_migrateDriver(OperationContext* opCtx, } bool MigrationDestinationManager::_applyMigrateOp(OperationContext* opCtx, - const string& ns, + const NamespaceString& nss, const BSONObj& min, const BSONObj& max, const BSONObj& shardKeyPattern, @@ -893,19 +893,19 @@ bool MigrationDestinationManager::_applyMigrateOp(OperationContext* opCtx, bool didAnything = false; if (xfer["deleted"].isABSONObj()) { - Lock::DBLock dlk(opCtx, nsToDatabaseSubstring(ns), MODE_IX); - Helpers::RemoveSaver rs("moveChunk", ns, "removedDuring"); + Lock::DBLock dlk(opCtx, nss.db(), MODE_IX); + Helpers::RemoveSaver rs("moveChunk", nss.ns(), "removedDuring"); BSONObjIterator i(xfer["deleted"].Obj()); // deleted documents while (i.more()) { - Lock::CollectionLock clk(opCtx->lockState(), ns, MODE_X); - OldClientContext ctx(opCtx, ns); + Lock::CollectionLock clk(opCtx->lockState(), nss.ns(), MODE_X); + OldClientContext ctx(opCtx, nss.ns()); BSONObj id = i.next().Obj(); // do not apply delete if doc does not belong to the chunk being migrated BSONObj fullObj; - if (Helpers::findById(opCtx, ctx.db(), ns.c_str(), id, fullObj)) { + if (Helpers::findById(opCtx, ctx.db(), nss.ns(), id, fullObj)) { if (!isInRange(fullObj, min, max, shardKeyPattern)) { if (MONGO_FAIL_POINT(failMigrationReceivedOutOfRangeOperation)) { invariant(0); @@ -919,8 +919,8 @@ bool MigrationDestinationManager::_applyMigrateOp(OperationContext* opCtx, } deleteObjects(opCtx, - ctx.db() ? ctx.db()->getCollection(ns) : nullptr, - ns, + ctx.db() ? ctx.db()->getCollection(nss.ns()) : nullptr, + nss, id, PlanExecutor::YIELD_MANUAL, true /* justOne */, @@ -935,7 +935,7 @@ bool MigrationDestinationManager::_applyMigrateOp(OperationContext* opCtx, if (xfer["reload"].isABSONObj()) { // modified documents (insert/update) BSONObjIterator i(xfer["reload"].Obj()); while (i.more()) { - OldClientWriteContext cx(opCtx, ns); + OldClientWriteContext cx(opCtx, nss.ns()); BSONObj updatedDoc = i.next().Obj(); @@ -949,7 +949,7 @@ bool MigrationDestinationManager::_applyMigrateOp(OperationContext* opCtx, BSONObj localDoc; if (willOverrideLocalId( - opCtx, ns, min, max, shardKeyPattern, cx.db(), updatedDoc, &localDoc)) { + opCtx, nss, min, max, shardKeyPattern, cx.db(), updatedDoc, &localDoc)) { string errMsg = str::stream() << "cannot migrate chunk, local document " << localDoc << " has same _id as reloaded remote document " << updatedDoc; @@ -961,7 +961,7 @@ bool MigrationDestinationManager::_applyMigrateOp(OperationContext* opCtx, } // We are in write lock here, so sure we aren't killing - Helpers::upsert(opCtx, ns, updatedDoc, true); + Helpers::upsert(opCtx, nss.ns(), updatedDoc, true); *lastOpApplied = repl::ReplClientInfo::forClient(opCtx->getClient()).getLastOp(); didAnything = true; diff --git a/src/mongo/db/s/migration_destination_manager.h b/src/mongo/db/s/migration_destination_manager.h index 700e9284159..c42ce1cdcb2 100644 --- a/src/mongo/db/s/migration_destination_manager.h +++ b/src/mongo/db/s/migration_destination_manager.h @@ -134,7 +134,7 @@ private: const WriteConcernOptions& writeConcern); bool _applyMigrateOp(OperationContext* opCtx, - const std::string& ns, + const NamespaceString& ns, const BSONObj& min, const BSONObj& max, const BSONObj& shardKeyPattern, diff --git a/src/mongo/db/views/durable_view_catalog.cpp b/src/mongo/db/views/durable_view_catalog.cpp index 757952ec598..dff65bbb9a5 100644 --- a/src/mongo/db/views/durable_view_catalog.cpp +++ b/src/mongo/db/views/durable_view_catalog.cpp @@ -148,7 +148,7 @@ void DurableViewCatalogImpl::upsert(OperationContext* opCtx, systemViews->insertDocument(opCtx, view, &CurOp::get(opCtx)->debug(), enforceQuota)); } else { OplogUpdateEntryArgs args; - args.ns = systemViewsNs.ns(); + args.nss = systemViewsNs; args.update = view; args.criteria = BSON("_id" << name.ns()); args.fromMigrate = false; diff --git a/src/mongo/dbtests/query_stage_count.cpp b/src/mongo/dbtests/query_stage_count.cpp index 3f6e8fc3dc0..c202c8308c6 100644 --- a/src/mongo/dbtests/query_stage_count.cpp +++ b/src/mongo/dbtests/query_stage_count.cpp @@ -122,7 +122,7 @@ public: WriteUnitOfWork wunit(&_opCtx); BSONObj oldDoc = _coll->getRecordStore()->dataFor(&_opCtx, oldrecordId).releaseToBson(); OplogUpdateEntryArgs args; - args.ns = _coll->ns().ns(); + args.nss = _coll->ns(); _coll->updateDocument(&_opCtx, oldrecordId, Snapshotted<BSONObj>(_opCtx.recoveryUnit()->getSnapshotId(), oldDoc), diff --git a/src/mongo/dbtests/query_stage_sort.cpp b/src/mongo/dbtests/query_stage_sort.cpp index f5ffa69d1e7..1bc24a40290 100644 --- a/src/mongo/dbtests/query_stage_sort.cpp +++ b/src/mongo/dbtests/query_stage_sort.cpp @@ -356,7 +356,7 @@ public: // foo < limit(). BSONObj newDoc = BSON("_id" << updatedId << "foo" << limit() + 10); OplogUpdateEntryArgs args; - args.ns = coll->ns().ns(); + args.nss = coll->ns(); { WriteUnitOfWork wuow(&_opCtx); coll->updateDocument(&_opCtx, *it, oldDoc, newDoc, false, false, NULL, &args); diff --git a/src/mongo/s/commands/strategy.cpp b/src/mongo/s/commands/strategy.cpp index b586acfc57d..228703c26f6 100644 --- a/src/mongo/s/commands/strategy.cpp +++ b/src/mongo/s/commands/strategy.cpp @@ -86,7 +86,7 @@ namespace { const std::string kOperationTime = "operationTime"; void runAgainstRegistered(OperationContext* opCtx, - const char* ns, + const NamespaceString& nss, BSONObj& jsobj, BSONObjBuilder& anObjBuilder, int queryOptions) { @@ -94,7 +94,7 @@ void runAgainstRegistered(OperationContext* opCtx, // into this function with any other collection name. uassert(16618, "Illegal attempt to run a command against a namespace other than $cmd.", - nsToCollectionSubstring(ns) == "$cmd"); + nss.isCommand()); BSONElement e = jsobj.firstElement(); std::string commandName = e.fieldName(); @@ -107,7 +107,7 @@ void runAgainstRegistered(OperationContext* opCtx, return; } - execCommandClient(opCtx, c, queryOptions, ns, jsobj, anObjBuilder); + execCommandClient(opCtx, c, queryOptions, nss.db(), jsobj, anObjBuilder); } /** @@ -331,8 +331,7 @@ void Strategy::clientCommandOp(OperationContext* opCtx, // Rewrite upgraded pseudoCommands to run on the 'admin' database. const NamespaceString interposedNss("admin", "$cmd"); BSONObjBuilder reply; - runAgainstRegistered( - opCtx, interposedNss.ns().c_str(), interposedCmd, reply, q.queryOptions); + runAgainstRegistered(opCtx, interposedNss, interposedCmd, reply, q.queryOptions); replyToQuery(0, client->session(), dbm->msg(), reply.done()); }; @@ -396,7 +395,7 @@ void Strategy::clientCommandOp(OperationContext* opCtx, OpQueryReplyBuilder reply; { BSONObjBuilder builder(reply.bufBuilderForResults()); - runAgainstRegistered(opCtx, q.ns, cmdObj, builder, q.queryOptions); + runAgainstRegistered(opCtx, NamespaceString(q.ns), cmdObj, builder, q.queryOptions); } reply.sendCommandReply(client->session(), dbm->msg()); return; @@ -589,12 +588,12 @@ void Strategy::writeOp(OperationContext* opCtx, DbMessage* dbm) { // Adjust namespace for command const NamespaceString& fullNS(commandRequest->getNS()); - const std::string cmdNS = fullNS.getCommandNS(); + const NamespaceString& cmdNS = fullNS.getCommandNS(); BSONObj commandBSON = commandRequest->toBSON(); BSONObjBuilder builder; - runAgainstRegistered(opCtx, cmdNS.c_str(), commandBSON, builder, 0); + runAgainstRegistered(opCtx, cmdNS, commandBSON, builder, 0); bool parsed = commandResponse.parseBSON(builder.done(), nullptr); (void)parsed; // for compile @@ -658,13 +657,13 @@ Status Strategy::explainFind(OperationContext* opCtx, void execCommandClient(OperationContext* opCtx, Command* c, int queryOptions, - const char* ns, + StringData dbname, BSONObj& cmdObj, BSONObjBuilder& result) { - const std::string dbname = nsToDatabase(ns); ON_BLOCK_EXIT([opCtx, &result] { appendRequiredFieldsToResponse(opCtx, &result); }); + dassert(dbname == nsToDatabase(dbname)); StringMap<int> topLevelFields; for (auto&& element : cmdObj) { StringData fieldName = element.fieldNameStringData(); @@ -683,7 +682,7 @@ void execCommandClient(OperationContext* opCtx, topLevelFields[fieldName]++ == 0); } - Status status = Command::checkAuthorization(c, opCtx, dbname, cmdObj); + Status status = Command::checkAuthorization(c, opCtx, dbname.toString(), cmdObj); if (!status.isOK()) { Command::appendCommandStatus(result, status); return; @@ -696,7 +695,7 @@ void execCommandClient(OperationContext* opCtx, } StatusWith<WriteConcernOptions> wcResult = - WriteConcernOptions::extractWCFromCommand(cmdObj, dbname); + WriteConcernOptions::extractWCFromCommand(cmdObj, dbname.toString()); if (!wcResult.isOK()) { Command::appendCommandStatus(result, wcResult.getStatus()); return; @@ -727,14 +726,14 @@ void execCommandClient(OperationContext* opCtx, bool ok = false; try { if (!supportsWriteConcern) { - ok = c->run(opCtx, dbname, cmdObj, queryOptions, errmsg, result); + ok = c->run(opCtx, dbname.toString(), cmdObj, queryOptions, errmsg, result); } else { // Change the write concern while running the command. const auto oldWC = opCtx->getWriteConcern(); ON_BLOCK_EXIT([&] { opCtx->setWriteConcern(oldWC); }); opCtx->setWriteConcern(wcResult.getValue()); - ok = c->run(opCtx, dbname, cmdObj, queryOptions, errmsg, result); + ok = c->run(opCtx, dbname.toString(), cmdObj, queryOptions, errmsg, result); } } catch (const DBException& e) { result.resetToEmpty(); |