diff options
author | Spencer T Brody <spencer@mongodb.com> | 2015-04-15 15:01:04 -0400 |
---|---|---|
committer | Spencer T Brody <spencer@mongodb.com> | 2015-04-16 16:02:49 -0400 |
commit | cd6f9bd7744324e725368db67934c5fa23e85347 (patch) | |
tree | dc6d0f2af2adfcd7252079b08dbe6b3eefbc943b /src/mongo/s | |
parent | 492e26db0702abd8b44794fb718f76b26afecefd (diff) | |
download | mongo-cd6f9bd7744324e725368db67934c5fa23e85347.tar.gz |
SERVER-18071 Parse mongos 'configdb' command line parameter into a ConnectionString rather than a vector of strings
Diffstat (limited to 'src/mongo/s')
-rw-r--r-- | src/mongo/s/catalog/legacy/catalog_manager_legacy.cpp | 29 | ||||
-rw-r--r-- | src/mongo/s/catalog/legacy/catalog_manager_legacy.h | 2 | ||||
-rw-r--r-- | src/mongo/s/config.cpp | 18 | ||||
-rw-r--r-- | src/mongo/s/config.h | 4 | ||||
-rw-r--r-- | src/mongo/s/d_state.cpp | 11 | ||||
-rw-r--r-- | src/mongo/s/grid.cpp | 4 | ||||
-rw-r--r-- | src/mongo/s/grid.h | 2 | ||||
-rw-r--r-- | src/mongo/s/mongos_options.cpp | 24 | ||||
-rw-r--r-- | src/mongo/s/mongos_options.h | 3 | ||||
-rw-r--r-- | src/mongo/s/server.cpp | 28 |
10 files changed, 61 insertions, 64 deletions
diff --git a/src/mongo/s/catalog/legacy/catalog_manager_legacy.cpp b/src/mongo/s/catalog/legacy/catalog_manager_legacy.cpp index 77502e783ea..2ed5203069e 100644 --- a/src/mongo/s/catalog/legacy/catalog_manager_legacy.cpp +++ b/src/mongo/s/catalog/legacy/catalog_manager_legacy.cpp @@ -260,33 +260,25 @@ namespace { } // namespace - Status CatalogManagerLegacy::init(const vector<string>& configHosts) { + Status CatalogManagerLegacy::init(const ConnectionString& configDBCS) { // Initialization should not happen more than once invariant(!_configServerConnectionString.isValid()); invariant(_configServers.empty()); - - if (configHosts.empty()) { - return Status(ErrorCodes::InvalidOptions, "No config server hosts specified"); - } + invariant(configDBCS.isValid()); // Extract the hosts in HOST:PORT format - set<HostAndPort> configHostsAndPorts; + set<HostAndPort> configHostsAndPortsSet; set<string> configHostsOnly; - for (size_t i = 0; i < configHosts.size(); i++) { - // Parse the config host string - StatusWith<HostAndPort> status = HostAndPort::parse(configHosts[i]); - if (!status.isOK()) { - return status.getStatus(); - } - + std::vector<HostAndPort> configHostAndPorts = configDBCS.getServers(); + for (size_t i = 0; i < configHostAndPorts.size(); i++) { // Append the default port, if not specified - HostAndPort configHost = status.getValue(); + HostAndPort configHost = configHostAndPorts[i]; if (!configHost.hasPort()) { configHost = HostAndPort(configHost.host(), ServerGlobalParams::ConfigServerPort); } // Make sure there are no duplicates - if (!configHostsAndPorts.insert(configHost).second) { + if (!configHostsAndPortsSet.insert(configHost).second) { StringBuilder sb; sb << "Host " << configHost.toString() << " exists twice in the config servers listing."; @@ -329,15 +321,12 @@ namespace { } } - string fullString; - joinStringDelim(configHosts, &fullString, ','); - - LOG(1) << " config string : " << fullString; + LOG(1) << " config string : " << configDBCS.toString(); // Now that the config hosts are verified, initialize the catalog manager. The code below // should never fail. - _configServerConnectionString = ConnectionString(fullString, ConnectionString::SYNC); + _configServerConnectionString = configDBCS; if (_configServerConnectionString.type() == ConnectionString::MASTER) { _configServers.push_back(_configServerConnectionString); diff --git a/src/mongo/s/catalog/legacy/catalog_manager_legacy.h b/src/mongo/s/catalog/legacy/catalog_manager_legacy.h index 2dc01ae4121..00f49e14c46 100644 --- a/src/mongo/s/catalog/legacy/catalog_manager_legacy.h +++ b/src/mongo/s/catalog/legacy/catalog_manager_legacy.h @@ -49,7 +49,7 @@ namespace mongo { * Initializes the catalog manager with the hosts, which will be used as a configuration * server. Can only be called once for the lifetime. */ - Status init(const std::vector<std::string>& configHosts); + Status init(const ConnectionString& configCS); virtual Status enableSharding(const std::string& dbName); diff --git a/src/mongo/s/config.cpp b/src/mongo/s/config.cpp index 644b0aaac42..501012e6375 100644 --- a/src/mongo/s/config.cpp +++ b/src/mongo/s/config.cpp @@ -703,20 +703,18 @@ namespace mongo { return ConnectionString(_primary.getConnString(), ConnectionString::SYNC); } - bool ConfigServer::init( const std::string& s ) { - vector<string> configdbs; - splitStringDelim( s, &configdbs, ',' ); - return init( configdbs ); - } + bool ConfigServer::init( const ConnectionString& configCS ) { + invariant(configCS.isValid()); - bool ConfigServer::init( vector<string> configHosts ) { - uassert( 10187 , "need configdbs" , configHosts.size() ); + std::vector<HostAndPort> configHostAndPorts = configCS.getServers(); + uassert( 10187 , "need configdbs" , configHostAndPorts.size() ); + std::vector<std::string> configHosts; set<string> hosts; - for ( size_t i=0; i<configHosts.size(); i++ ) { - string host = configHosts[i]; + for ( size_t i=0; i<configHostAndPorts.size(); i++ ) { + string host = configHostAndPorts[i].toString(); hosts.insert( getHost( host , false ) ); - configHosts[i] = getHost( host , true ); + configHosts.push_back(getHost( host , true )); } for ( set<string>::iterator i=hosts.begin(); i!=hosts.end(); i++ ) { diff --git a/src/mongo/s/config.h b/src/mongo/s/config.h index 2f990812568..9c89e7d93ff 100644 --- a/src/mongo/s/config.h +++ b/src/mongo/s/config.h @@ -192,9 +192,7 @@ namespace mongo { /** call at startup, this will initiate connection to the grid db */ - bool init( std::vector<std::string> configHosts ); - - bool init( const std::string& s ); + bool init( const ConnectionString& configCS ); /** * Check hosts are unique. Returns true if all configHosts diff --git a/src/mongo/s/d_state.cpp b/src/mongo/s/d_state.cpp index 626881edafc..960ede1413e 100644 --- a/src/mongo/s/d_state.cpp +++ b/src/mongo/s/d_state.cpp @@ -477,13 +477,16 @@ namespace mongo { ShardedConnectionInfo::addHook(); - vector<string> configdbs; - splitStringDelim(server, &configdbs, ','); + std::string errmsg; + ConnectionString configServerCS = ConnectionString::parse(server, errmsg); + uassert(28631, + str::stream() << "Invalid config server connection string: " << errmsg, + configServerCS.isValid()); - configServer.init(configdbs); + configServer.init(configServerCS); uassert(28627, "failed to initialize catalog manager", - grid.initCatalogManager(configdbs)); + grid.initCatalogManager(configServerCS)); _enabled = true; } diff --git a/src/mongo/s/grid.cpp b/src/mongo/s/grid.cpp index fac1af9d0e5..b8110b7a4ad 100644 --- a/src/mongo/s/grid.cpp +++ b/src/mongo/s/grid.cpp @@ -60,9 +60,9 @@ namespace mongo { } - bool Grid::initCatalogManager(const std::vector<std::string>& configHosts) { + bool Grid::initCatalogManager(const ConnectionString& configDBString) { std::auto_ptr<CatalogManagerLegacy> cm(new CatalogManagerLegacy()); - Status status = cm->init(configHosts); + Status status = cm->init(configDBString); if (!status.isOK()) { severe() << "Catalog manager failed to initialize " << status; return false; diff --git a/src/mongo/s/grid.h b/src/mongo/s/grid.h index 3504a3a7e37..ec9fa10967e 100644 --- a/src/mongo/s/grid.h +++ b/src/mongo/s/grid.h @@ -61,7 +61,7 @@ namespace mongo { * Returns whether the catalog manager has been initialized successfully. Must be called * only once. */ - bool initCatalogManager(const std::vector<std::string>& configHosts); + bool initCatalogManager(const ConnectionString& configDBString); /** * Implicitly creates the specified database as non-sharded. diff --git a/src/mongo/s/mongos_options.cpp b/src/mongo/s/mongos_options.cpp index 05d2ecf9f07..ab111163d92 100644 --- a/src/mongo/s/mongos_options.cpp +++ b/src/mongo/s/mongos_options.cpp @@ -255,15 +255,29 @@ namespace mongo { return Status(ErrorCodes::BadValue, "error: no args for --configdb"); } - splitStringDelim(params["sharding.configDB"].as<std::string>(), - &mongosGlobalParams.configdbs, ','); - if (mongosGlobalParams.configdbs.size() != 1 && mongosGlobalParams.configdbs.size() != 3) { + std::string configdbString = params["sharding.configDB"].as<std::string>(); + try { + std::string errmsg; + mongosGlobalParams.configdbs = ConnectionString::parse(configdbString, errmsg); + + if (!mongosGlobalParams.configdbs.isValid()) { + return Status(ErrorCodes::BadValue, + str::stream() << "Invalid configdb connection string: " << errmsg); + } + } catch (const DBException& e) { + return Status(ErrorCodes::BadValue, + str::stream() << "Invalid configdb connection string: " << e.what()); + } + + std::vector<HostAndPort> configServers = mongosGlobalParams.configdbs.getServers(); + + if (configServers.size() != 1 && configServers.size() != 3) { return Status(ErrorCodes::BadValue, "need either 1 or 3 configdbs"); } - if (mongosGlobalParams.configdbs.size() == 1) { + if (configServers.size() == 1) { warning() << "running with 1 config server should be done only for testing purposes " - << "and is not recommended for production" << endl; + << "and is not recommended for production" << endl; } if (params.count("upgrade")) { diff --git a/src/mongo/s/mongos_options.h b/src/mongo/s/mongos_options.h index 30dfd354874..ee38ce51064 100644 --- a/src/mongo/s/mongos_options.h +++ b/src/mongo/s/mongos_options.h @@ -29,6 +29,7 @@ #pragma once #include "mongo/base/status.h" +#include "mongo/client/connection_string.h" #include "mongo/db/server_options.h" #include "mongo/util/options_parser/environment.h" #include "mongo/util/options_parser/option_section.h" @@ -43,7 +44,7 @@ namespace mongo { namespace moe = mongo::optionenvironment; struct MongosGlobalParams { - std::vector<std::string> configdbs; + ConnectionString configdbs; bool upgrade; MongosGlobalParams() : diff --git a/src/mongo/s/server.cpp b/src/mongo/s/server.cpp index df31706bd0e..145746646ca 100644 --- a/src/mongo/s/server.cpp +++ b/src/mongo/s/server.cpp @@ -347,26 +347,20 @@ static int _main() { startSignalProcessingThread(); // we either have a setting where all processes are in localhost or none are - for (std::vector<std::string>::const_iterator it = mongosGlobalParams.configdbs.begin(); - it != mongosGlobalParams.configdbs.end(); ++it) { - try { + std::vector<HostAndPort> configServers = mongosGlobalParams.configdbs.getServers(); + for (std::vector<HostAndPort>::const_iterator it = configServers.begin(); + it != configServers.end(); ++it) { - HostAndPort configAddr( *it ); // will throw if address format is invalid - - if (it == mongosGlobalParams.configdbs.begin()) { - grid.setAllowLocalHost( configAddr.isLocalHost() ); - } - - if ( configAddr.isLocalHost() != grid.allowLocalHost() ) { - mongo::log(LogComponent::kDefault) - << "cannot mix localhost and ip addresses in configdbs" << endl; - return 10; - } + const HostAndPort& configAddr = *it; + if (it == configServers.begin()) { + grid.setAllowLocalHost( configAddr.isLocalHost() ); } - catch ( DBException& e) { - mongo::log(LogComponent::kDefault) << "configdb: " << e.what() << endl; - return 9; + + if ( configAddr.isLocalHost() != grid.allowLocalHost() ) { + mongo::log(LogComponent::kDefault) + << "cannot mix localhost and ip addresses in configdbs" << endl; + return 10; } } |