diff options
author | Esha Maharishi <esha.maharishi@mongodb.com> | 2018-04-26 13:21:09 -0400 |
---|---|---|
committer | Esha Maharishi <esha.maharishi@mongodb.com> | 2018-04-26 13:26:08 -0400 |
commit | 751e985f0e1ecc30e2d49c3c2cb01c046dd5f8bb (patch) | |
tree | 71f5815be75ee946114ccdc0eedfe498d542050a | |
parent | a39601ed33bbcf138d8ba644ff680bf1ff23564f (diff) | |
download | mongo-751e985f0e1ecc30e2d49c3c2cb01c046dd5f8bb.tar.gz |
SERVER-34483 Avoid taking DBLocks when clearing in-memory database cache in FCV op observer
4 files changed, 22 insertions, 26 deletions
diff --git a/jstests/sharding/database_versioning_upgrade_downgrade.js b/jstests/sharding/database_versioning_upgrade_downgrade.js index 57764563638..3a84b2e0723 100644 --- a/jstests/sharding/database_versioning_upgrade_downgrade.js +++ b/jstests/sharding/database_versioning_upgrade_downgrade.js @@ -32,7 +32,7 @@ assert.eq(null, db1EntryOriginal.version); assert.eq(null, db2EntryOriginal.version); - // Ensure the shard does not have a cached entries in-memory or on-disk. + // Ensure the shard does not have cached entries in-memory or on-disk. checkInMemoryDatabaseVersion(st.rs0.getPrimary(), db1Name, {}); checkInMemoryDatabaseVersion(st.rs0.getSecondary(), db1Name, {}); checkInMemoryDatabaseVersion(st.rs0.getPrimary(), db2Name, {}); @@ -104,10 +104,9 @@ assert.docEq(db1EntryOriginal, db1EntryFCV36); assert.docEq(db2EntryOriginal, db2EntryFCV36); - // The shard's in-memory database cache should have been cleared, but its on-disk cache left - // untouched. - checkInMemoryDatabaseVersion(st.rs0.getPrimary(), db1Name, {}); - checkInMemoryDatabaseVersion(st.rs0.getSecondary(), db1Name, {}); + // The shard's in-memory and on-disk database caches should have been left untouched. + checkInMemoryDatabaseVersion(st.rs0.getPrimary(), db1Name, db1EntryFCV40.version); + checkInMemoryDatabaseVersion(st.rs0.getSecondary(), db1Name, db1EntryFCV40.version); checkInMemoryDatabaseVersion(st.rs0.getPrimary(), db2Name, {}); checkInMemoryDatabaseVersion(st.rs0.getSecondary(), db2Name, {}); checkOnDiskDatabaseVersion(st.shard0, db1Name, db1EntryFCV40); diff --git a/src/mongo/db/commands/feature_compatibility_version.cpp b/src/mongo/db/commands/feature_compatibility_version.cpp index a387b69d2a0..c33031abc8d 100644 --- a/src/mongo/db/commands/feature_compatibility_version.cpp +++ b/src/mongo/db/commands/feature_compatibility_version.cpp @@ -42,7 +42,7 @@ #include "mongo/db/operation_context.h" #include "mongo/db/repl/optime.h" #include "mongo/db/repl/storage_interface.h" -#include "mongo/db/s/database_sharding_state.h" +#include "mongo/db/s/collection_sharding_state.h" #include "mongo/db/s/sharding_state.h" #include "mongo/db/server_parameters.h" #include "mongo/db/service_context.h" @@ -167,26 +167,7 @@ void FeatureCompatibilityVersion::onInsertOrUpdate(OperationContext* opCtx, cons (newVersion == ServerGlobalParams::FeatureCompatibility::Version::kFullyDowngradedTo36 || newVersion == ServerGlobalParams::FeatureCompatibility::Version::kFullyUpgradedTo40)) { - // Clear the in-memory cached database versions and collections metadata. - // TODO: Once SERVER-34431 goes in, just clear the DatabaseShardingStateMap. - std::vector<std::string> dbNames; - getGlobalServiceContext()->getGlobalStorageEngine()->listDatabases(&dbNames); - for (const auto& dbName : dbNames) { - if (dbName == "admin") { - // The 'admin' database is already locked, since the FCV document is in - // admin.system.version. Just skip 'admin', since it is not versioned. - continue; - } - AutoGetDb autoDb(opCtx, dbName, MODE_X); - if (autoDb.getDb()) { - DatabaseShardingState::get(autoDb.getDb()).setDbVersion(opCtx, boost::none); - for (const auto& collection : *autoDb.getDb()) { - CollectionShardingState::get(opCtx, collection->ns()) - ->refreshMetadata(opCtx, nullptr); - } - } - } - + CollectionShardingState::resetAll(opCtx); Grid::get(opCtx)->catalogCache()->purgeAllDatabases(); } diff --git a/src/mongo/db/s/collection_sharding_state.cpp b/src/mongo/db/s/collection_sharding_state.cpp index fdbedadbe51..424ce3ce548 100644 --- a/src/mongo/db/s/collection_sharding_state.cpp +++ b/src/mongo/db/s/collection_sharding_state.cpp @@ -124,6 +124,16 @@ public: return *it->second; } + void resetAll() { + stdx::lock_guard<stdx::mutex> lg(_mutex); + for (auto it = _collections.begin(); it != _collections.end(); ++it) { + // This is a hack to get around CollectionShardingState::refreshMetadata() requiring + // the X lock: markNotShardedAtStepdown() doesn't have a lock check. Temporary + // measure until SERVER-31595 removes the X lock requirement. + it->second->markNotShardedAtStepdown(); + } + } + void report(OperationContext* opCtx, BSONObjBuilder* builder) { BSONObjBuilder versionB(builder->subobjStart("versions")); @@ -175,6 +185,11 @@ CollectionShardingState* CollectionShardingState::get(OperationContext* opCtx, return &collectionsMap.getOrCreate(ns); } +void CollectionShardingState::resetAll(OperationContext* opCtx) { + auto& collectionsMap = CollectionShardingStateMap::get(opCtx->getServiceContext()); + collectionsMap.resetAll(); +} + void CollectionShardingState::report(OperationContext* opCtx, BSONObjBuilder* builder) { auto& collectionsMap = CollectionShardingStateMap::get(opCtx->getServiceContext()); collectionsMap.report(opCtx, builder); diff --git a/src/mongo/db/s/collection_sharding_state.h b/src/mongo/db/s/collection_sharding_state.h index 2df1699c450..398c4833b29 100644 --- a/src/mongo/db/s/collection_sharding_state.h +++ b/src/mongo/db/s/collection_sharding_state.h @@ -71,6 +71,7 @@ public: static CollectionShardingState* get(OperationContext* opCtx, const NamespaceString& nss); static CollectionShardingState* get(OperationContext* opCtx, const std::string& ns); + static void resetAll(OperationContext* opCtx); static void report(OperationContext* opCtx, BSONObjBuilder* builder); /** |