From d213908da9ddb54cfddfde0d5d88a4e4e1f70bd6 Mon Sep 17 00:00:00 2001 From: Esha Maharishi Date: Tue, 6 Feb 2018 11:36:27 -0500 Subject: [PATCH] Revert "Revert "SERVER-32983 persist a version field in new config.databases entries in fcv>=4.0"" This reverts commit d7c127cb2c98eab7ca9ea0ef8405126e675ed5d7. --- ...ng_last_stable_mongos_and_mixed_shards.yml | 1 + jstests/sharding/create_database.js | 79 +++++++++++++++++++ ...ng_catalog_manager_database_operations.cpp | 30 ++++++- src/mongo/s/versioning.cpp | 2 +- 4 files changed, 107 insertions(+), 5 deletions(-) create mode 100644 jstests/sharding/create_database.js diff --git a/buildscripts/resmokeconfig/suites/sharding_last_stable_mongos_and_mixed_shards.yml b/buildscripts/resmokeconfig/suites/sharding_last_stable_mongos_and_mixed_shards.yml index a2a39365987..d5296d508bd 100644 --- a/buildscripts/resmokeconfig/suites/sharding_last_stable_mongos_and_mixed_shards.yml +++ b/buildscripts/resmokeconfig/suites/sharding_last_stable_mongos_and_mixed_shards.yml @@ -19,6 +19,7 @@ selector: # 3.6 becomes 'last-stable'. - jstests/sharding/covered_shard_key_indexes.js # Enable when 3.8 becomes last-stable. + - jstests/sharding/create_database.js - jstests/sharding/dump_coll_metadata.js - jstests/sharding/geo_near_random1.js - jstests/sharding/geo_near_random2.js diff --git a/jstests/sharding/create_database.js b/jstests/sharding/create_database.js new file mode 100644 index 00000000000..4f31141f830 --- /dev/null +++ b/jstests/sharding/create_database.js @@ -0,0 +1,79 @@ +/** + * Tests that creating a database causes it to be written to the sharding catalog with a + * databaseVersion if FCV > 3.6, but not if FCV <= 3.6. + */ +(function() { + 'use strict'; + + function createDatabase(st, dbName) { + // A database is implicitly created when a collection inside it is created. + assert.commandWorked(st.s.getDB(dbName).runCommand({create: collName})); + } + + function cleanUp(st, dbName) { + assert.commandWorked(st.s.getDB(dbName).runCommand({dropDatabase: 1})); + } + + function assertHasDbVersion(dbEntry) { + assert.neq(null, dbEntry); + assert.neq(null, dbEntry.version); + assert.neq(null, dbEntry.version.uuid); + assert.eq(1, dbEntry.version.lastMod); + } + + function assertDoesntHaveDbVersion(dbEntry) { + assert.neq(null, dbEntry); + assert.eq(null, dbEntry.version); + } + + const dbName = "db1"; + const collName = "foo"; + + var st = new ShardingTest({shards: 1}); + + let dbEntry; + + // + // FCV 4.0 + // + + assert.commandWorked(st.s.adminCommand({setFeatureCompatibilityVersion: "4.0"})); + + // A new database is given a databaseVersion. + createDatabase(st, dbName); + let dbEntry1 = st.s.getDB("config").getCollection("databases").findOne({_id: dbName}); + assertHasDbVersion(dbEntry1); + cleanUp(st, dbName); + + // A new incarnation of a database that was previously dropped is given a fresh databaseVersion. + createDatabase(st, dbName); + let dbEntry2 = st.s.getDB("config").getCollection("databases").findOne({_id: dbName}); + assertHasDbVersion(dbEntry2); + assert.neq(dbEntry1.version.uuid, dbEntry2.version.uuid); + cleanUp(st, dbName); + + // + // FCV 3.6 + // + + assert.commandWorked(st.s.adminCommand({setFeatureCompatibilityVersion: "3.6"})); + + // A new database is not given a databaseVersion. + createDatabase(st, dbName); + dbEntry = st.s.getDB("config").getCollection("databases").findOne({_id: dbName}); + assertDoesntHaveDbVersion(dbEntry); + cleanUp(st, dbName); + + // + // FCV 3.4 (This section can be deleted once FCV 3.4 is removed). + // + + assert.commandWorked(st.s.adminCommand({setFeatureCompatibilityVersion: "3.4"})); + + // A new database is not given a databaseVersion. + createDatabase(st, dbName); + dbEntry = st.s.getDB("config").getCollection("databases").findOne({_id: dbName}); + assertDoesntHaveDbVersion(dbEntry); + cleanUp(st, dbName); + +})(); diff --git a/src/mongo/db/s/config/sharding_catalog_manager_database_operations.cpp b/src/mongo/db/s/config/sharding_catalog_manager_database_operations.cpp index d84ea03282f..8435d155428 100644 --- a/src/mongo/db/s/config/sharding_catalog_manager_database_operations.cpp +++ b/src/mongo/db/s/config/sharding_catalog_manager_database_operations.cpp @@ -33,12 +33,15 @@ #include #include "mongo/bson/util/bson_extract.h" +#include "mongo/db/commands/feature_compatibility_version.h" #include "mongo/db/namespace_string.h" #include "mongo/db/repl/repl_client_info.h" +#include "mongo/db/server_options.h" #include "mongo/s/catalog/sharding_catalog_client_impl.h" #include "mongo/s/catalog/type_database.h" #include "mongo/s/client/shard.h" #include "mongo/s/grid.h" +#include "mongo/s/versioning.h" #include "mongo/util/log.h" namespace mongo { @@ -101,13 +104,32 @@ DatabaseType ShardingCatalogManager::createDatabase(OperationContext* opCtx, return uassertStatusOK(DatabaseType::fromBSON(dbObj)); } - // The database does not exist. Pick a primary shard to place it on. - auto const primaryShardId = + // The database does not exist. Insert an entry for the new database into the sharding catalog. + + // Pick a primary shard for the new database. + const auto primaryShardId = uassertStatusOK(_selectShardForNewDatabase(opCtx, Grid::get(opCtx)->shardRegistry())); - log() << "Placing [" << dbName << "] on: " << primaryShardId; + + // Take the fcvLock to prevent the fcv from changing from the point we decide whether to include + // a databaseVersion until after we finish writing the database entry. Otherwise, we may end up + // in fcv>3.6, but without a databaseVersion. + invariant(!opCtx->lockState()->isLocked()); + Lock::SharedLock lk(opCtx->lockState(), FeatureCompatibilityVersion::fcvLock); + + // If in FCV>3.6, generate a databaseVersion, including a UUID, for the new database. + boost::optional dbVersion = boost::none; + if (serverGlobalParams.featureCompatibility.getVersion() == + ServerGlobalParams::FeatureCompatibility::Version::kUpgradingTo40 || + serverGlobalParams.featureCompatibility.getVersion() == + ServerGlobalParams::FeatureCompatibility::Version::kFullyUpgradedTo40) { + dbVersion = Versioning::newDatabaseVersion(); + } // Insert an entry for the new database into the sharding catalog. - DatabaseType db(dbName, primaryShardId, false); + DatabaseType db(dbName, std::move(primaryShardId), false, std::move(dbVersion)); + + log() << "Registering new database " << db << " in sharding catalog"; + uassertStatusOK(Grid::get(opCtx)->catalogClient()->insertConfigDocument( opCtx, DatabaseType::ConfigNS, db.toBSON(), ShardingCatalogClient::kMajorityWriteConcern)); diff --git a/src/mongo/s/versioning.cpp b/src/mongo/s/versioning.cpp index 11b2b4ccb06..0d0c27eeb6c 100644 --- a/src/mongo/s/versioning.cpp +++ b/src/mongo/s/versioning.cpp @@ -36,7 +36,7 @@ namespace mongo { DatabaseVersion Versioning::newDatabaseVersion() { DatabaseVersion dbv; - dbv.setLastMod(0); + dbv.setLastMod(1); dbv.setUuid(UUID::gen()); return dbv; }