/** * Copyright (C) 2015 MongoDB Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License, version 3, * as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . * * As a special exception, the copyright holders give permission to link the * code of portions of this program with the OpenSSL library under certain * conditions as described in each individual source file and distribute * linked combinations including the program with the OpenSSL library. You * must comply with the GNU Affero General Public License in all respects for * all of the code used other than as permitted herein. If you modify file(s) * with this exception, you may extend this exception to your version of the * file(s), but you are not obligated to do so. If you do not wish to do so, * delete this exception statement from your version. If you delete this * exception statement from all source files in the program, then also delete * it in the license file. */ #pragma once #include #include #include #include #include #include "mongo/s/client/shard.h" namespace mongo { class BSONObjBuilder; class CatalogManager; class HostAndPort; class NamespaceString; class RemoteCommandRunner; class RemoteCommandTargeterFactory; class Shard; class ShardType; template class StatusWith; namespace executor { class TaskExecutor; } // namespace executor /** * Maintains the set of all shards known to the instance and their connections. Manages polling * the respective replica sets for membership changes. */ class ShardRegistry { public: /** * Instantiates a new shard registry. * * @param targeterFactory Produces targeters for each shard's individual connection string * @param commandRunner Command runner for executing commands against hosts * @param executor Asynchronous task executor to use for making calls to shards. * @param catalogManager Used to retrieve the list of registered shard. TODO: remove. */ ShardRegistry(std::unique_ptr targeterFactory, std::unique_ptr commandRunner, std::unique_ptr executor, CatalogManager* catalogManager); ~ShardRegistry(); RemoteCommandRunner* getCommandRunner() const { return _commandRunner.get(); } executor::TaskExecutor* getExecutor() const { return _executor.get(); } void reload(); /** * Returns shared pointer to shard object with given shard id. */ std::shared_ptr getShard(const ShardId& shardId); /** * Lookup shard by replica set name. Returns nullptr if the name can't be found. * Note: this doesn't refresh the table if the name isn't found, so it's possible that a * newly added shard/Replica Set may not be found. */ std::shared_ptr lookupRSName(const std::string& name) const; void remove(const ShardId& id); void getAllShardIds(std::vector* all) const; void toBSON(BSONObjBuilder* result); /** * Executes 'find' command against the specified host and fetches *all* the results that * the host will return until there are no more or until an error is returned. * * Returns either the complete set of results or an error, never partial results. * * Note: should never be used outside of CatalogManagerReplicaSet or DistLockCatalogImpl. */ StatusWith> exhaustiveFind(const HostAndPort& host, const NamespaceString& nss, const BSONObj& query, boost::optional limit); /** * Runs a command against the specified host and returns the result. */ StatusWith runCommand(const HostAndPort& host, const std::string& dbName, const BSONObj& cmdObj); private: typedef std::map> ShardMap; /** * Creates a shard based on the specified information and puts it into the lookup maps. */ void _addShard_inlock(const ShardType& shardType); /** * Adds the "config" shard (representing the config server) to the shard registry. */ void _addConfigShard_inlock(); std::shared_ptr _findUsingLookUp(const ShardId& shardId); // Factory to obtain remote command targeters for shards const std::unique_ptr _targeterFactory; // API to run remote commands to shards in a synchronous manner const std::unique_ptr _commandRunner; // Executor for scheduling work and remote commands to shards that run in an asynchronous // manner. const std::unique_ptr _executor; // Catalog manager from which to load the shard information. Not owned and must outlive // the shard registry object. CatalogManager* const _catalogManager; // Protects the maps below mutable std::mutex _mutex; // Map of both shardName -> Shard and hostName -> Shard ShardMap _lookup; // TODO: These should eventually disappear and become parts of Shard // Map from all hosts within a replica set to the shard representing this replica set ShardMap _rsLookup; }; } // namespace mongo