Revert "Revert "SERVER-32983 persist a version field in new config.databases entries in fcv>=4.0""
This reverts commit d7c127cb2c.
This commit is contained in:
@@ -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
|
||||
|
||||
79
jstests/sharding/create_database.js
Normal file
79
jstests/sharding/create_database.js
Normal file
@@ -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);
|
||||
|
||||
})();
|
||||
@@ -33,12 +33,15 @@
|
||||
#include <pcrecpp.h>
|
||||
|
||||
#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<DatabaseVersion> 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));
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ namespace mongo {
|
||||
|
||||
DatabaseVersion Versioning::newDatabaseVersion() {
|
||||
DatabaseVersion dbv;
|
||||
dbv.setLastMod(0);
|
||||
dbv.setLastMod(1);
|
||||
dbv.setUuid(UUID::gen());
|
||||
return dbv;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user