diff options
author | Kaloian Manassiev <kaloian.manassiev@mongodb.com> | 2016-03-04 14:43:27 -0500 |
---|---|---|
committer | Kaloian Manassiev <kaloian.manassiev@mongodb.com> | 2016-03-08 15:29:06 -0500 |
commit | 7df00746cc59c9df635402f531abd18481b9a034 (patch) | |
tree | 3c4526b9887ee0b58ac09927795fac0c313ea0d1 /src | |
parent | 8f1e89803e2239bd31ecb53c85245a249b7e04ce (diff) | |
download | mongo-7df00746cc59c9df635402f531abd18481b9a034.tar.gz |
SERVER-22972 Remove usages of OldClientContext which reference database
Some of these are doing shard version checking against the name of the
database, which is useless and breaks sharding assertions, which we are
about to add.
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/db/catalog/database.cpp | 10 | ||||
-rw-r--r-- | src/mongo/db/catalog/database.h | 11 | ||||
-rw-r--r-- | src/mongo/db/catalog/drop_database.cpp | 14 | ||||
-rw-r--r-- | src/mongo/db/commands/cpuprofile.cpp | 7 | ||||
-rw-r--r-- | src/mongo/db/db_raii.h | 3 | ||||
-rw-r--r-- | src/mongo/db/dbcommands.cpp | 20 | ||||
-rw-r--r-- | src/mongo/db/dbeval.cpp | 2 | ||||
-rw-r--r-- | src/mongo/db/exec/stagedebug_cmd.cpp | 16 | ||||
-rw-r--r-- | src/mongo/db/repl/master_slave.cpp | 14 | ||||
-rw-r--r-- | src/mongo/db/repl/master_slave.h | 2 | ||||
-rw-r--r-- | src/mongo/db/s/collection_sharding_state.cpp | 5 | ||||
-rw-r--r-- | src/mongo/db/s/collection_sharding_state.h | 1 | ||||
-rw-r--r-- | src/mongo/dbtests/rollbacktests.cpp | 3 | ||||
-rw-r--r-- | src/mongo/s/d_state.cpp | 31 | ||||
-rw-r--r-- | src/mongo/s/stale_exception.h | 3 |
15 files changed, 89 insertions, 53 deletions
diff --git a/src/mongo/db/catalog/database.cpp b/src/mongo/db/catalog/database.cpp index b94c465a4e4..876179b9f49 100644 --- a/src/mongo/db/catalog/database.cpp +++ b/src/mongo/db/catalog/database.cpp @@ -560,7 +560,7 @@ void dropAllDatabasesExceptLocal(OperationContext* txn) { if (db == nullptr) { log() << "database disappeared after listDatabases but before drop: " << *i; } else { - dropDatabase(txn, db); + Database::dropDatabase(txn, db); } } MONGO_WRITE_CONFLICT_RETRY_LOOP_END(txn, "dropAllDatabasesExceptLocal", *i); @@ -568,18 +568,18 @@ void dropAllDatabasesExceptLocal(OperationContext* txn) { } } -void dropDatabase(OperationContext* txn, Database* db) { +void Database::dropDatabase(OperationContext* txn, Database* db) { invariant(db); // Store the name so we have if for after the db object is deleted const string name = db->name(); - LOG(1) << "dropDatabase " << name << endl; + LOG(1) << "dropDatabase " << name; invariant(txn->lockState()->isDbLockedForMode(name, MODE_X)); - BackgroundOperation::assertNoBgOpInProgForDb(name.c_str()); + BackgroundOperation::assertNoBgOpInProgForDb(name); - audit::logDropDatabase(&cc(), name); + audit::logDropDatabase(txn->getClient(), name); dbHolder().close(txn, name); db = NULL; // d is now deleted diff --git a/src/mongo/db/catalog/database.h b/src/mongo/db/catalog/database.h index 53c3cb5712d..113862ec1c0 100644 --- a/src/mongo/db/catalog/database.h +++ b/src/mongo/db/catalog/database.h @@ -168,6 +168,15 @@ public: bool stayTemp); /** + * Physically drops the specified opened database and removes it from the server's metadata. It + * doesn't notify the replication subsystem or do any other consistency checks, so it should + * not be used directly from user commands. + * + * Must be called with the specified database locked in X mode. + */ + static void dropDatabase(OperationContext* txn, Database* db); + + /** * @return name of an existing database with same text name but different * casing, if one exists. Otherwise the empty std::string is returned. If * 'duplicates' is specified, it is filled with all duplicate names. @@ -217,8 +226,6 @@ private: friend class IndexCatalog; }; -void dropDatabase(OperationContext* txn, Database* db); - void dropAllDatabasesExceptLocal(OperationContext* txn); Status userCreateNS(OperationContext* txn, diff --git a/src/mongo/db/catalog/drop_database.cpp b/src/mongo/db/catalog/drop_database.cpp index f45d2596ebe..eaf168a1b2a 100644 --- a/src/mongo/db/catalog/drop_database.cpp +++ b/src/mongo/db/catalog/drop_database.cpp @@ -48,7 +48,15 @@ #include "mongo/util/log.h" namespace mongo { + Status dropDatabase(OperationContext* txn, const std::string& dbName) { + // TODO (Kal): OldClientContext legacy, needs to be removed + { + CurOp::get(txn)->ensureStarted(); + stdx::lock_guard<Client> lk(*txn->getClient()); + CurOp::get(txn)->setNS_inlock(dbName); + } + MONGO_WRITE_CONFLICT_RETRY_LOOP_BEGIN { ScopedTransaction transaction(txn, MODE_X); Lock::GlobalWrite lk(txn->lockState()); @@ -59,7 +67,6 @@ Status dropDatabase(OperationContext* txn, const std::string& dbName) { str::stream() << "Could not drop database " << dbName << " because it does not exist"); } - OldClientContext context(txn, dbName); bool userInitiatedWritesAndNotPrimary = txn->writesAreReplicated() && !repl::getGlobalReplicationCoordinator()->canAcceptWritesForDatabase(dbName); @@ -70,10 +77,7 @@ Status dropDatabase(OperationContext* txn, const std::string& dbName) { } log() << "dropDatabase " << dbName << " starting"; - - BackgroundOperation::assertNoBgOpInProgForDb(dbName); - mongo::dropDatabase(txn, db); - + Database::dropDatabase(txn, db); log() << "dropDatabase " << dbName << " finished"; WriteUnitOfWork wunit(txn); diff --git a/src/mongo/db/commands/cpuprofile.cpp b/src/mongo/db/commands/cpuprofile.cpp index 4273666a70f..10be4af8978 100644 --- a/src/mongo/db/commands/cpuprofile.cpp +++ b/src/mongo/db/commands/cpuprofile.cpp @@ -139,10 +139,10 @@ bool CpuProfilerStartCommand::run(OperationContext* txn, int options, std::string& errmsg, BSONObjBuilder& result) { + // The DB lock here is just so we have IX on the global lock in order to prevent shutdown ScopedTransaction transaction(txn, MODE_IX); Lock::DBLock dbXLock(txn->lockState(), db, MODE_X); - // The lock here is just to prevent concurrency, nothing will write. - OldClientContext ctx(txn, db); + OldClientContext ctx(txn, db, false /* no shard version checking */); std::string profileFilename = cmdObj[commandName]["profileFilename"].String(); if (!::ProfilerStart(profileFilename.c_str())) { @@ -158,9 +158,10 @@ bool CpuProfilerStopCommand::run(OperationContext* txn, int options, std::string& errmsg, BSONObjBuilder& result) { + // The DB lock here is just so we have IX on the global lock in order to prevent shutdown ScopedTransaction transaction(txn, MODE_IX); Lock::DBLock dbXLock(txn->lockState(), db, MODE_X); - OldClientContext ctx(txn, db); + OldClientContext ctx(txn, db, false /* no shard version checking */); ::ProfilerStop(); return true; diff --git a/src/mongo/db/db_raii.h b/src/mongo/db/db_raii.h index 59a43d67d64..948cf014c90 100644 --- a/src/mongo/db/db_raii.h +++ b/src/mongo/db/db_raii.h @@ -182,9 +182,6 @@ public: Database* db() const { return _db; } - const char* ns() const { - return _ns.c_str(); - } /** @return if the db was created by this OldClientContext */ bool justCreated() const { diff --git a/src/mongo/db/dbcommands.cpp b/src/mongo/db/dbcommands.cpp index 4ad9cd2439b..79cdd098674 100644 --- a/src/mongo/db/dbcommands.cpp +++ b/src/mongo/db/dbcommands.cpp @@ -266,7 +266,13 @@ public: // TODO: SERVER-4328 Don't lock globally ScopedTransaction transaction(txn, MODE_X); Lock::GlobalWrite lk(txn->lockState()); - OldClientContext context(txn, dbname); + + // TODO (Kal): OldClientContext legacy, needs to be removed + { + CurOp::get(txn)->ensureStarted(); + stdx::lock_guard<Client> lk(*txn->getClient()); + CurOp::get(txn)->setNS_inlock(dbname); + } log() << "repairDatabase " << dbname; BackgroundOperation::assertNoBgOpInProgForDb(dbname); @@ -426,7 +432,13 @@ public: // ScopedTransaction transaction(txn, MODE_IX); Lock::DBLock dbXLock(txn->lockState(), dbname, MODE_X); - OldClientContext ctx(txn, dbname); + + // TODO (Kal): OldClientContext legacy, needs to be removed + { + CurOp::get(txn)->ensureStarted(); + stdx::lock_guard<Client> lk(*txn->getClient()); + CurOp::get(txn)->setNS_inlock(dbname); + } int was = _diaglog.setLevel(cmdObj.firstElement().numberInt()); _diaglog.flush(); @@ -1064,9 +1076,9 @@ public: const string ns = parseNs(dbname, jsobj); - // TODO: OldClientContext legacy, needs to be removed - CurOp::get(txn)->ensureStarted(); + // TODO (Kal): OldClientContext legacy, needs to be removed { + CurOp::get(txn)->ensureStarted(); stdx::lock_guard<Client> lk(*txn->getClient()); CurOp::get(txn)->setNS_inlock(dbname); } diff --git a/src/mongo/db/dbeval.cpp b/src/mongo/db/dbeval.cpp index ada1495be36..5fefb31cfd0 100644 --- a/src/mongo/db/dbeval.cpp +++ b/src/mongo/db/dbeval.cpp @@ -184,7 +184,7 @@ public: ScopedTransaction transaction(txn, MODE_X); Lock::GlobalWrite lk(txn->lockState()); - OldClientContext ctx(txn, dbname); + OldClientContext ctx(txn, dbname, false /* no shard version checking */); return dbEval(txn, dbname, cmdObj, result, errmsg); } diff --git a/src/mongo/db/exec/stagedebug_cmd.cpp b/src/mongo/db/exec/stagedebug_cmd.cpp index 4f24f7fb43d..3cacf015fcc 100644 --- a/src/mongo/db/exec/stagedebug_cmd.cpp +++ b/src/mongo/db/exec/stagedebug_cmd.cpp @@ -154,20 +154,24 @@ public: if (collElt.eoo() || (String != collElt.type())) { return false; } - string collName = collElt.String(); + + const NamespaceString nss(dbname, collElt.String()); + uassert(ErrorCodes::InvalidNamespace, + str::stream() << nss.toString() << " is not a valid namespace", + nss.isValid()); // Need a context to get the actual Collection* // TODO A write lock is currently taken here to accommodate stages that perform writes // (e.g. DeleteStage). This should be changed to use a read lock for read-only // execution trees. ScopedTransaction transaction(txn, MODE_IX); - Lock::DBLock lk(txn->lockState(), dbname, MODE_X); - OldClientContext ctx(txn, dbname); + AutoGetCollection autoColl(txn, nss, MODE_IX); // Make sure the collection is valid. - Database* db = ctx.db(); - Collection* collection = db->getCollection(db->name() + '.' + collName); - uassert(17446, "Couldn't find the collection " + collName, NULL != collection); + Collection* collection = autoColl.getCollection(); + uassert(ErrorCodes::NamespaceNotFound, + str::stream() << "Couldn't find collection " << nss.ns(), + collection); // Pull out the plan BSONElement planElt = argObj["plan"]; diff --git a/src/mongo/db/repl/master_slave.cpp b/src/mongo/db/repl/master_slave.cpp index 51c471802aa..e5c50e53321 100644 --- a/src/mongo/db/repl/master_slave.cpp +++ b/src/mongo/db/repl/master_slave.cpp @@ -459,10 +459,12 @@ void ReplSource::forceResync(OperationContext* txn, const char* requester) { save(txn); } -void ReplSource::resyncDrop(OperationContext* txn, const string& db) { - log() << "resync: dropping database " << db; - OldClientContext ctx(txn, db); - dropDatabase(txn, ctx.db()); +void ReplSource::resyncDrop(OperationContext* txn, const string& dbName) { + log() << "resync: dropping database " << dbName; + invariant(txn->lockState()->isW()); + + Database* const db = dbHolder().get(txn, dbName); + Database::dropDatabase(txn, db); } /* grab initial copy of a database from the master */ @@ -604,8 +606,8 @@ bool ReplSource::handleDuplicateDbName(OperationContext* txn, incompleteCloneDbs.erase(*i); addDbNextPass.erase(*i); - OldClientContext ctx(txn, *i); - dropDatabase(txn, ctx.db()); + AutoGetDb autoDb(txn, *i, MODE_X); + Database::dropDatabase(txn, autoDb.getDb()); } massert(14034, diff --git a/src/mongo/db/repl/master_slave.h b/src/mongo/db/repl/master_slave.h index e26fd3c0f56..8ff41e85e39 100644 --- a/src/mongo/db/repl/master_slave.h +++ b/src/mongo/db/repl/master_slave.h @@ -100,7 +100,7 @@ class ReplSource { /// TODO(spencer): Remove this once the LegacyReplicationCoordinator is gone. BSONObj _me; - void resyncDrop(OperationContext* txn, const std::string& db); + void resyncDrop(OperationContext* txn, const std::string& dbName); // call without the db mutex void syncToTailOfRemoteLog(); std::string ns() const { diff --git a/src/mongo/db/s/collection_sharding_state.cpp b/src/mongo/db/s/collection_sharding_state.cpp index 23955390096..e08c17ab4f4 100644 --- a/src/mongo/db/s/collection_sharding_state.cpp +++ b/src/mongo/db/s/collection_sharding_state.cpp @@ -47,6 +47,11 @@ CollectionShardingState::CollectionShardingState( CollectionShardingState::~CollectionShardingState() = default; CollectionShardingState* CollectionShardingState::get(OperationContext* txn, + const NamespaceString& nss) { + return CollectionShardingState::get(txn, nss.ns()); +} + +CollectionShardingState* CollectionShardingState::get(OperationContext* txn, const std::string& ns) { // Collection lock must be held to have a reference to the collection's sharding state dassert(txn->lockState()->isCollectionLockedForMode(ns, MODE_IS)); diff --git a/src/mongo/db/s/collection_sharding_state.h b/src/mongo/db/s/collection_sharding_state.h index 4c51a2a0460..73f076fb37b 100644 --- a/src/mongo/db/s/collection_sharding_state.h +++ b/src/mongo/db/s/collection_sharding_state.h @@ -66,6 +66,7 @@ public: * Must be called with some lock held on the specific collection being looked up and the * returned pointer should never be stored. */ + static CollectionShardingState* get(OperationContext* txn, const NamespaceString& nss); static CollectionShardingState* get(OperationContext* txn, const std::string& ns); /** diff --git a/src/mongo/dbtests/rollbacktests.cpp b/src/mongo/dbtests/rollbacktests.cpp index b0ebd0d825f..0432b8b30e1 100644 --- a/src/mongo/dbtests/rollbacktests.cpp +++ b/src/mongo/dbtests/rollbacktests.cpp @@ -26,6 +26,7 @@ * then also delete it in the license file. */ +#include "mongo/platform/basic.h" #include "mongo/bson/bsonobj.h" #include "mongo/db/catalog/collection.h" @@ -52,7 +53,7 @@ void dropDatabase(OperationContext* txn, const NamespaceString& nss) { Database* db = dbHolder().get(txn, nss.db()); if (db) { - dropDatabase(txn, db); + Database::dropDatabase(txn, db); } } bool collectionExists(OldClientContext* ctx, const string& ns) { diff --git a/src/mongo/s/d_state.cpp b/src/mongo/s/d_state.cpp index 630e5687612..9206a6808b6 100644 --- a/src/mongo/s/d_state.cpp +++ b/src/mongo/s/d_state.cpp @@ -44,6 +44,7 @@ #include "mongo/db/operation_context.h" #include "mongo/db/repl/replication_coordinator_global.h" #include "mongo/db/s/collection_metadata.h" +#include "mongo/db/s/collection_sharding_state.h" #include "mongo/db/s/operation_shard_version.h" #include "mongo/db/s/sharded_connection_info.h" #include "mongo/db/s/sharding_state.h" @@ -141,13 +142,12 @@ public: int, string& errmsg, BSONObjBuilder& result) { - const string ns = cmdObj["getShardVersion"].valuestrsafe(); - if (ns.size() == 0) { - errmsg = "need to specify full namespace"; - return false; + const NamespaceString nss(cmdObj["getShardVersion"].valuestrsafe()); + if (!nss.isValid()) { + uasserted(ErrorCodes::InvalidNamespace, "Command requires valid collection namespace"); } - ShardingState* shardingState = ShardingState::get(txn); + ShardingState* const shardingState = ShardingState::get(txn); if (shardingState->enabled()) { result.append("configServer", shardingState->getConfigServer(txn).toString()); @@ -155,18 +155,27 @@ public: result.append("configServer", ""); } - result.appendTimestamp("global", shardingState->getVersion(ns).toLong()); + AutoGetCollection autoColl(txn, nss, MODE_IS); + CollectionShardingState* const css = CollectionShardingState::get(txn, nss); + + shared_ptr<CollectionMetadata> metadata(css ? css->getMetadata() : nullptr); + if (metadata) { + result.appendTimestamp("global", metadata->getShardVersion().toLong()); + } else { + result.appendTimestamp("global", ChunkVersion(0, 0, OID()).toLong()); + } ShardedConnectionInfo* const info = ShardedConnectionInfo::get(txn->getClient(), false); result.appendBool("inShardedMode", info != NULL); if (info) { - result.appendTimestamp("mine", info->getVersion(ns).toLong()); + result.appendTimestamp("mine", info->getVersion(nss.ns()).toLong()); } else { result.appendTimestamp("mine", 0); } if (cmdObj["fullMetadata"].trueValue()) { - shared_ptr<CollectionMetadata> metadata = shardingState->getCollectionMetadata(ns); + shared_ptr<CollectionMetadata> metadata = + shardingState->getCollectionMetadata(nss.ns()); if (metadata) { result.append("metadata", metadata->toBSON()); } else { @@ -177,7 +186,7 @@ public: return true; } -} getShardVersion; +} getShardVersionCmd; class ShardingStateCmd : public Command { public: @@ -209,10 +218,6 @@ public: int, string& errmsg, BSONObjBuilder& result) { - ScopedTransaction transaction(txn, MODE_IX); - Lock::DBLock dbXLock(txn->lockState(), dbname, MODE_X); - OldClientContext ctx(txn, dbname); - ShardingState::get(txn)->appendInfo(txn, result); return true; } diff --git a/src/mongo/s/stale_exception.h b/src/mongo/s/stale_exception.h index c9d3827572d..8769da77bbe 100644 --- a/src/mongo/s/stale_exception.h +++ b/src/mongo/s/stale_exception.h @@ -139,9 +139,6 @@ public: ChunkVersion received, ChunkVersion wanted) : StaleConfigException(ns, raw, ErrorCodes::SendStaleConfig, received, wanted) {} - - SendStaleConfigException(const std::string& raw, const BSONObj& error) - : StaleConfigException(raw, ErrorCodes::SendStaleConfig, error) {} }; class RecvStaleConfigException : public StaleConfigException { |