diff options
author | Jordi Serra Torrens <jordi.serra-torrens@mongodb.com> | 2020-11-26 17:29:26 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-12-01 12:22:39 +0000 |
commit | b85442ba1cc20270a3595bba769438394a66e7e4 (patch) | |
tree | 53f9b0f91a3edb79882ae7fe0a4e5fd024b5a6e0 /src/mongo/db | |
parent | 4b8e802d58cdec6e340c6f8cace09224a64c9aa9 (diff) | |
download | mongo-b85442ba1cc20270a3595bba769438394a66e7e4.tar.gz |
SERVER-52588 Making the upgrade/downgrade process work for the Epoch to ClusterTime migration
Diffstat (limited to 'src/mongo/db')
-rw-r--r-- | src/mongo/db/commands/set_feature_compatibility_version_command.cpp | 21 | ||||
-rw-r--r-- | src/mongo/db/s/config/sharding_catalog_manager.cpp | 77 | ||||
-rw-r--r-- | src/mongo/db/s/config/sharding_catalog_manager.h | 16 | ||||
-rw-r--r-- | src/mongo/db/s/shard_metadata_util.cpp | 23 | ||||
-rw-r--r-- | src/mongo/db/s/shard_metadata_util.h | 3 |
5 files changed, 130 insertions, 10 deletions
diff --git a/src/mongo/db/commands/set_feature_compatibility_version_command.cpp b/src/mongo/db/commands/set_feature_compatibility_version_command.cpp index 92eb7526073..c3474f1caf6 100644 --- a/src/mongo/db/commands/set_feature_compatibility_version_command.cpp +++ b/src/mongo/db/commands/set_feature_compatibility_version_command.cpp @@ -60,6 +60,7 @@ #include "mongo/db/views/view_catalog.h" #include "mongo/logv2/log.h" #include "mongo/rpc/get_status_from_command_result.h" +#include "mongo/s/sharded_collections_ddl_parameters_gen.h" #include "mongo/stdx/unordered_set.h" #include "mongo/util/exit.h" #include "mongo/util/fail_point.h" @@ -252,6 +253,15 @@ public: uassertStatusOK( ShardingCatalogManager::get(opCtx)->setFeatureCompatibilityVersionOnShards( opCtx, CommandHelpers::appendMajorityWriteConcern(request.toBSON({})))); + + // Create a 'timestamp' for the collections that don't have one. This must be done + // after all shards have been upgraded in order to guarantee that when + // createCollectionTimestampsFor49 starts, no new collections without a timestamp + // will be added. + if (requestedVersion >= FeatureCompatibilityParams::Version::kVersion49 && + feature_flags::gShardingFullDDLSupport.isEnabledAndIgnoreFCV()) { + ShardingCatalogManager::get(opCtx)->createCollectionTimestampsFor49(opCtx); + } } hangWhileUpgrading.pauseWhileSet(opCtx); @@ -338,11 +348,20 @@ public: deletePersistedDefaultRWConcernDocument(opCtx); } - // Downgrade shards before config finishes its downgrade. if (serverGlobalParams.clusterRole == ClusterRole::ConfigServer) { + // Downgrade shards before config finishes its downgrade. uassertStatusOK( ShardingCatalogManager::get(opCtx)->setFeatureCompatibilityVersionOnShards( opCtx, CommandHelpers::appendMajorityWriteConcern(request.toBSON({})))); + + // Delete the 'timestamp' field in config.collections entries. This must be done + // after all shards have been downgraded in order to guarantee that when + // downgradeConfigCollectionEntriesToPre49 starts, no new collections with a + // timestamp will be added. + if (requestedVersion < FeatureCompatibilityParams::Version::kVersion49) { + ShardingCatalogManager::get(opCtx)->downgradeConfigCollectionEntriesToPre49( + opCtx); + } } hangWhileDowngrading.pauseWhileSet(opCtx); diff --git a/src/mongo/db/s/config/sharding_catalog_manager.cpp b/src/mongo/db/s/config/sharding_catalog_manager.cpp index 4e9a619db01..6ea65f9ea96 100644 --- a/src/mongo/db/s/config/sharding_catalog_manager.cpp +++ b/src/mongo/db/s/config/sharding_catalog_manager.cpp @@ -39,6 +39,7 @@ #include "mongo/db/ops/write_ops.h" #include "mongo/db/query/query_request.h" #include "mongo/db/s/balancer/type_migration.h" +#include "mongo/db/vector_clock.h" #include "mongo/logv2/log.h" #include "mongo/rpc/get_status_from_command_result.h" #include "mongo/s/catalog/config_server_version.h" @@ -52,6 +53,7 @@ #include "mongo/s/catalog/type_tags.h" #include "mongo/s/client/shard_registry.h" #include "mongo/s/grid.h" +#include "mongo/s/sharded_collections_ddl_parameters_gen.h" #include "mongo/s/write_ops/batched_command_request.h" #include "mongo/s/write_ops/batched_command_response.h" #include "mongo/transport/service_entry_point.h" @@ -484,6 +486,81 @@ void ShardingCatalogManager::removePre49LegacyMetadata(OperationContext* opCtx) uassertStatusOK(getWriteConcernStatusFromCommandResult(commandResult->getCommandReply())); } +void ShardingCatalogManager::createCollectionTimestampsFor49(OperationContext* opCtx) { + LOGV2(5258800, "Starting upgrade of config.collections"); + + const auto catalogClient = Grid::get(opCtx)->catalogClient(); + auto const catalogCache = Grid::get(opCtx)->catalogCache(); + auto const configShard = Grid::get(opCtx)->shardRegistry()->getConfigShard(); + const auto collectionDocs = + uassertStatusOK(configShard->exhaustiveFindOnConfig( + opCtx, + ReadPreferenceSetting{ReadPreference::PrimaryOnly}, + repl::ReadConcernLevel::kLocalReadConcern, + CollectionType::ConfigNS, + BSON(CollectionType::kTimestampFieldName << BSON("$exists" << false)), + BSONObj(), + boost::none)) + .docs; + + + for (const auto& doc : collectionDocs) { + const CollectionType coll(doc); + const auto nss = coll.getNss(); + + auto now = VectorClock::get(opCtx)->getTime(); + auto clusterTime = now.clusterTime().asTimestamp(); + + uassertStatusOK(catalogClient->updateConfigDocument( + opCtx, + CollectionType::ConfigNS, + BSON(CollectionType::kNssFieldName << nss.ns()), + BSON("$set" << BSON(CollectionType::kTimestampFieldName << clusterTime)), + false /* upsert */, + ShardingCatalogClient::kMajorityWriteConcern)); + + catalogCache->invalidateCollectionEntry_LINEARIZABLE(nss); + } + + LOGV2(5258801, "Successfully upgraded config.collections"); +} + +void ShardingCatalogManager::downgradeConfigCollectionEntriesToPre49(OperationContext* opCtx) { + if (feature_flags::gShardingFullDDLSupport.isEnabledAndIgnoreFCV()) { + DBDirectClient client(opCtx); + + // Clear the 'timestamp' fields from config.collections + write_ops::Update unsetTimestamp(CollectionType::ConfigNS, [] { + write_ops::UpdateOpEntry u; + u.setQ({}); + u.setU(write_ops::UpdateModification::parseFromClassicUpdate( + BSON("$unset" << BSON(CollectionType::kTimestampFieldName << "")))); + u.setMulti(true); + return std::vector{u}; + }()); + unsetTimestamp.setWriteCommandBase([] { + write_ops::WriteCommandBase base; + base.setOrdered(false); + return base; + }()); + + auto commandResult = client.runCommand(OpMsgRequest::fromDBAndBody( + CollectionType::ConfigNS.db(), + unsetTimestamp.toBSON(ShardingCatalogClient::kMajorityWriteConcern.toBSON()))); + + uassertStatusOK([&] { + BatchedCommandResponse response; + std::string unusedErrmsg; + response.parseBSON( + commandResult->getCommandReply(), + &unusedErrmsg); // Return value intentionally ignored, because response.toStatus() + // will contain any errors in more detail + return response.toStatus(); + }()); + uassertStatusOK(getWriteConcernStatusFromCommandResult(commandResult->getCommandReply())); + } +} + Lock::ExclusiveLock ShardingCatalogManager::lockZoneMutex(OperationContext* opCtx) { Lock::ExclusiveLock lk(opCtx->lockState(), _kZoneOpLock); return lk; diff --git a/src/mongo/db/s/config/sharding_catalog_manager.h b/src/mongo/db/s/config/sharding_catalog_manager.h index 5b9030f3d84..c8f3b3edf7c 100644 --- a/src/mongo/db/s/config/sharding_catalog_manager.h +++ b/src/mongo/db/s/config/sharding_catalog_manager.h @@ -455,6 +455,22 @@ public: */ void removePre49LegacyMetadata(OperationContext* opCtx); + /** + * Creates a 'timestamp' for each one of the entries in the config server's config.collections, + * where 'timestamp' does not already exist. + * + * It shall be called when upgrading to 4.9. + */ + void createCollectionTimestampsFor49(OperationContext* opCtx); + + /** + * Downgrades the config.collections entries to prior 4.9 version. More specifically, it removes + * the 'timestamp' field from all the documents in config.collections. + * + * It shall be called when downgrading from 4.9 to an earlier version. + */ + void downgradeConfigCollectionEntriesToPre49(OperationContext* opCtx); + // // For Diagnostics // diff --git a/src/mongo/db/s/shard_metadata_util.cpp b/src/mongo/db/s/shard_metadata_util.cpp index dd2a0357293..8da1a50bc10 100644 --- a/src/mongo/db/s/shard_metadata_util.cpp +++ b/src/mongo/db/s/shard_metadata_util.cpp @@ -44,7 +44,9 @@ #include "mongo/rpc/get_status_from_command_result.h" #include "mongo/rpc/unique_message.h" #include "mongo/s/catalog/type_chunk.h" +#include "mongo/s/catalog/type_collection.h" #include "mongo/s/chunk_version.h" +#include "mongo/s/sharded_collections_ddl_parameters_gen.h" #include "mongo/s/write_ops/batched_command_response.h" namespace mongo { @@ -485,30 +487,35 @@ Status deleteDatabasesEntry(OperationContext* opCtx, StringData dbName) { } void downgradeShardConfigCollectionEntriesToPre49(OperationContext* opCtx) { - // Clear the 'allowMigrations' field from config.cache.collections + // Clear the 'allowMigrations' and 'timestamp' fields from config.cache.collections LOGV2(5189100, "Starting downgrade of config.cache.collections"); - write_ops::Update clearAllowMigrations(NamespaceString::kShardConfigCollectionsNamespace, [] { + write_ops::Update clearFields(NamespaceString::kShardConfigCollectionsNamespace, [] { + BSONObj unsetFields = + BSON(ShardCollectionType::kPre50CompatibleAllowMigrationsFieldName << ""); + if (feature_flags::gShardingFullDDLSupport.isEnabledAndIgnoreFCV()) { + unsetFields = unsetFields.addFields(BSON(CollectionType::kTimestampFieldName << "")); + } + write_ops::UpdateOpEntry u; u.setQ({}); - u.setU(write_ops::UpdateModification::parseFromClassicUpdate( - BSON("$unset" << BSON(ShardCollectionType::kPre50CompatibleAllowMigrationsFieldName - << "")))); + u.setU( + write_ops::UpdateModification::parseFromClassicUpdate(BSON("$unset" << unsetFields))); u.setMulti(true); return std::vector{u}; }()); - clearAllowMigrations.setWriteCommandBase([] { + clearFields.setWriteCommandBase([] { write_ops::WriteCommandBase base; base.setOrdered(false); return base; }()); DBDirectClient client(opCtx); - const auto commandResult = client.runCommand(clearAllowMigrations.serialize({})); + const auto commandResult = client.runCommand(clearFields.serialize({})); uassertStatusOK(getStatusFromWriteCommandResponse(commandResult->getCommandReply())); - LOGV2(5189101, "Succesfully downgraded config.cache.collections"); + LOGV2(5189101, "Successfully downgraded config.cache.collections"); } } // namespace shardmetadatautil diff --git a/src/mongo/db/s/shard_metadata_util.h b/src/mongo/db/s/shard_metadata_util.h index 76775b95b7e..f863e406941 100644 --- a/src/mongo/db/s/shard_metadata_util.h +++ b/src/mongo/db/s/shard_metadata_util.h @@ -224,7 +224,8 @@ Status deleteDatabasesEntry(OperationContext* opCtx, StringData dbName); /** * Downgrades the config.cache.collections entries to prior 4.9 version. More specifically, it - * removes the allowMigrations field from all the documents of config.cache.collections + * removes the allowMigrations and timestamp fields from all the documents of + * config.cache.collections */ void downgradeShardConfigCollectionEntriesToPre49(OperationContext* opCtx); |