diff options
author | Misha Tyulenev <misha@mongodb.com> | 2016-01-06 10:17:01 -0500 |
---|---|---|
committer | Misha Tyulenev <misha@mongodb.com> | 2016-01-06 10:21:34 -0500 |
commit | a05ba516fb340bd7a22480303c8d647b6d6c9874 (patch) | |
tree | 54fe45c04f3f1b6d108f77be4516d7b8a4adfab4 | |
parent | aaf53b13854fde5271bb6e394295bd2ed6c56736 (diff) | |
download | mongo-a05ba516fb340bd7a22480303c8d647b6d6c9874.tar.gz |
SERVER-21906 always use ReplicaSetMonitor when updating ShardRegistry lookup maps
-rw-r--r-- | src/mongo/s/client/shard_registry.cpp | 35 | ||||
-rw-r--r-- | src/mongo/s/client/shard_registry.h | 9 |
2 files changed, 28 insertions, 16 deletions
diff --git a/src/mongo/s/client/shard_registry.cpp b/src/mongo/s/client/shard_registry.cpp index e5b91dda83a..556f4e8de46 100644 --- a/src/mongo/s/client/shard_registry.cpp +++ b/src/mongo/s/client/shard_registry.cpp @@ -207,7 +207,7 @@ void ShardRegistry::reload(OperationContext* txn) { continue; } - _addShard_inlock(shardType); + _addShard_inlock(shardType, false); } } @@ -334,10 +334,10 @@ void ShardRegistry::_addConfigShard_inlock() { ShardType configServerShard; configServerShard.setName("config"); configServerShard.setHost(_configServerCS.toString()); - _addShard_inlock(configServerShard); + _addShard_inlock(configServerShard, true); } -void ShardRegistry::_addShard_inlock(const ShardType& shardType) { +void ShardRegistry::_addShard_inlock(const ShardType& shardType, bool passHostName) { // This validation should ideally go inside the ShardType::validate call. However, doing // it there would prevent us from loading previously faulty shard hosts, which might have // been stored (i.e., the entire getAllShards call would fail). @@ -355,27 +355,38 @@ void ShardRegistry::_addShard_inlock(const ShardType& shardType) { // mechanism and must only be reachable through CatalogManagerLegacy or legacy-style queries // and inserts. Do not create targeter for these connections. This code should go away after // 3.2 is released. + // Config server updates must pass the host name to avoid deadlock shard = std::make_shared<Shard>(shardType.getName(), shardHost, nullptr); + _updateLookupMapsForShard_inlock(std::move(shard), shardHost); } else { // Non-SYNC shards use targeter factory. shard = std::make_shared<Shard>( shardType.getName(), shardHost, _targeterFactory->create(shardHost)); + if (passHostName) { + _updateLookupMapsForShard_inlock(std::move(shard), shardHost); + } else { + _updateLookupMapsForShard_inlock(std::move(shard), + boost::optional<const ConnectionString&>(boost::none)); + } } - - _updateLookupMapsForShard_inlock(std::move(shard), shardHost); } -void ShardRegistry::updateLookupMapsForShard(shared_ptr<Shard> shard, - const ConnectionString& newConnString) { - log() << "Updating ShardRegistry connection string for shard " << shard->getId() - << " from: " << shard->getConnString().toString() << " to: " << newConnString.toString(); +void ShardRegistry::updateLookupMapsForShard( + shared_ptr<Shard> shard, boost::optional<const ConnectionString&> optConnString) { stdx::lock_guard<stdx::mutex> lk(_mutex); - _updateLookupMapsForShard_inlock(std::move(shard), newConnString); + _updateLookupMapsForShard_inlock(std::move(shard), optConnString); } -void ShardRegistry::_updateLookupMapsForShard_inlock(shared_ptr<Shard> shard, - const ConnectionString& newConnString) { +void ShardRegistry::_updateLookupMapsForShard_inlock( + shared_ptr<Shard> shard, boost::optional<const ConnectionString&> optConnString) { auto oldConnString = shard->getConnString(); + auto newConnString = optConnString ? *optConnString : shard->getTargeter()->connectionString(); + + if (oldConnString.toString() != newConnString.toString()) { + log() << "Updating ShardRegistry connection string for shard " << shard->getId() + << " from: " << oldConnString.toString() << " to: " << newConnString.toString(); + } + for (const auto& host : oldConnString.getServers()) { _lookup.erase(host.toString()); _hostLookup.erase(host); diff --git a/src/mongo/s/client/shard_registry.h b/src/mongo/s/client/shard_registry.h index 6c18173505a..dec0f770803 100644 --- a/src/mongo/s/client/shard_registry.h +++ b/src/mongo/s/client/shard_registry.h @@ -150,12 +150,13 @@ public: /** * Updates _lookup and _rsLookup based on the given new version of the given Shard's - * ConnectionString. + * ConnectionString. If ConnectionString is not specified the maps are updated with hosts + * from the ReplicaSetMonitor. * Used to update the ShardRegistry when a change in replica set membership is detected by the * ReplicaSetMonitor. */ void updateLookupMapsForShard(std::shared_ptr<Shard> shard, - const ConnectionString& newConnString); + boost::optional<const ConnectionString&> newConnString); /** * Returns a shared pointer to the shard object with the given shard id. @@ -348,7 +349,7 @@ private: /** * Creates a shard based on the specified information and puts it into the lookup maps. */ - void _addShard_inlock(const ShardType& shardType); + void _addShard_inlock(const ShardType& shardType, bool passHostName); /** * Adds the "config" shard (representing the config server) to the shard registry. @@ -356,7 +357,7 @@ private: void _addConfigShard_inlock(); void _updateLookupMapsForShard_inlock(std::shared_ptr<Shard> shard, - const ConnectionString& newConnString); + boost::optional<const ConnectionString&> newConnString); std::shared_ptr<Shard> _findUsingLookUp(const ShardId& shardId); |