Skip to content

Commit cd6f9bd

Browse files
committed
SERVER-18071 Parse mongos 'configdb' command line parameter into a ConnectionString rather than a vector of strings
1 parent 492e26d commit cd6f9bd

File tree

10 files changed

+61
-64
lines changed

10 files changed

+61
-64
lines changed

src/mongo/s/catalog/legacy/catalog_manager_legacy.cpp

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -260,33 +260,25 @@ namespace {
260260
} // namespace
261261

262262

263-
Status CatalogManagerLegacy::init(const vector<string>& configHosts) {
263+
Status CatalogManagerLegacy::init(const ConnectionString& configDBCS) {
264264
// Initialization should not happen more than once
265265
invariant(!_configServerConnectionString.isValid());
266266
invariant(_configServers.empty());
267-
268-
if (configHosts.empty()) {
269-
return Status(ErrorCodes::InvalidOptions, "No config server hosts specified");
270-
}
267+
invariant(configDBCS.isValid());
271268

272269
// Extract the hosts in HOST:PORT format
273-
set<HostAndPort> configHostsAndPorts;
270+
set<HostAndPort> configHostsAndPortsSet;
274271
set<string> configHostsOnly;
275-
for (size_t i = 0; i < configHosts.size(); i++) {
276-
// Parse the config host string
277-
StatusWith<HostAndPort> status = HostAndPort::parse(configHosts[i]);
278-
if (!status.isOK()) {
279-
return status.getStatus();
280-
}
281-
272+
std::vector<HostAndPort> configHostAndPorts = configDBCS.getServers();
273+
for (size_t i = 0; i < configHostAndPorts.size(); i++) {
282274
// Append the default port, if not specified
283-
HostAndPort configHost = status.getValue();
275+
HostAndPort configHost = configHostAndPorts[i];
284276
if (!configHost.hasPort()) {
285277
configHost = HostAndPort(configHost.host(), ServerGlobalParams::ConfigServerPort);
286278
}
287279

288280
// Make sure there are no duplicates
289-
if (!configHostsAndPorts.insert(configHost).second) {
281+
if (!configHostsAndPortsSet.insert(configHost).second) {
290282
StringBuilder sb;
291283
sb << "Host " << configHost.toString()
292284
<< " exists twice in the config servers listing.";
@@ -329,15 +321,12 @@ namespace {
329321
}
330322
}
331323

332-
string fullString;
333-
joinStringDelim(configHosts, &fullString, ',');
334-
335-
LOG(1) << " config string : " << fullString;
324+
LOG(1) << " config string : " << configDBCS.toString();
336325

337326
// Now that the config hosts are verified, initialize the catalog manager. The code below
338327
// should never fail.
339328

340-
_configServerConnectionString = ConnectionString(fullString, ConnectionString::SYNC);
329+
_configServerConnectionString = configDBCS;
341330

342331
if (_configServerConnectionString.type() == ConnectionString::MASTER) {
343332
_configServers.push_back(_configServerConnectionString);

src/mongo/s/catalog/legacy/catalog_manager_legacy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace mongo {
4949
* Initializes the catalog manager with the hosts, which will be used as a configuration
5050
* server. Can only be called once for the lifetime.
5151
*/
52-
Status init(const std::vector<std::string>& configHosts);
52+
Status init(const ConnectionString& configCS);
5353

5454
virtual Status enableSharding(const std::string& dbName);
5555

src/mongo/s/config.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -703,20 +703,18 @@ namespace mongo {
703703
return ConnectionString(_primary.getConnString(), ConnectionString::SYNC);
704704
}
705705

706-
bool ConfigServer::init( const std::string& s ) {
707-
vector<string> configdbs;
708-
splitStringDelim( s, &configdbs, ',' );
709-
return init( configdbs );
710-
}
706+
bool ConfigServer::init( const ConnectionString& configCS ) {
707+
invariant(configCS.isValid());
711708

712-
bool ConfigServer::init( vector<string> configHosts ) {
713-
uassert( 10187 , "need configdbs" , configHosts.size() );
709+
std::vector<HostAndPort> configHostAndPorts = configCS.getServers();
710+
uassert( 10187 , "need configdbs" , configHostAndPorts.size() );
714711

712+
std::vector<std::string> configHosts;
715713
set<string> hosts;
716-
for ( size_t i=0; i<configHosts.size(); i++ ) {
717-
string host = configHosts[i];
714+
for ( size_t i=0; i<configHostAndPorts.size(); i++ ) {
715+
string host = configHostAndPorts[i].toString();
718716
hosts.insert( getHost( host , false ) );
719-
configHosts[i] = getHost( host , true );
717+
configHosts.push_back(getHost( host , true ));
720718
}
721719

722720
for ( set<string>::iterator i=hosts.begin(); i!=hosts.end(); i++ ) {

src/mongo/s/config.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,7 @@ namespace mongo {
192192
/**
193193
call at startup, this will initiate connection to the grid db
194194
*/
195-
bool init( std::vector<std::string> configHosts );
196-
197-
bool init( const std::string& s );
195+
bool init( const ConnectionString& configCS );
198196

199197
/**
200198
* Check hosts are unique. Returns true if all configHosts

src/mongo/s/d_state.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,13 +477,16 @@ namespace mongo {
477477

478478
ShardedConnectionInfo::addHook();
479479

480-
vector<string> configdbs;
481-
splitStringDelim(server, &configdbs, ',');
480+
std::string errmsg;
481+
ConnectionString configServerCS = ConnectionString::parse(server, errmsg);
482+
uassert(28631,
483+
str::stream() << "Invalid config server connection string: " << errmsg,
484+
configServerCS.isValid());
482485

483-
configServer.init(configdbs);
486+
configServer.init(configServerCS);
484487
uassert(28627,
485488
"failed to initialize catalog manager",
486-
grid.initCatalogManager(configdbs));
489+
grid.initCatalogManager(configServerCS));
487490
_enabled = true;
488491
}
489492

src/mongo/s/grid.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ namespace mongo {
6060

6161
}
6262

63-
bool Grid::initCatalogManager(const std::vector<std::string>& configHosts) {
63+
bool Grid::initCatalogManager(const ConnectionString& configDBString) {
6464
std::auto_ptr<CatalogManagerLegacy> cm(new CatalogManagerLegacy());
65-
Status status = cm->init(configHosts);
65+
Status status = cm->init(configDBString);
6666
if (!status.isOK()) {
6767
severe() << "Catalog manager failed to initialize " << status;
6868
return false;

src/mongo/s/grid.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ namespace mongo {
6161
* Returns whether the catalog manager has been initialized successfully. Must be called
6262
* only once.
6363
*/
64-
bool initCatalogManager(const std::vector<std::string>& configHosts);
64+
bool initCatalogManager(const ConnectionString& configDBString);
6565

6666
/**
6767
* Implicitly creates the specified database as non-sharded.

src/mongo/s/mongos_options.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,15 +255,29 @@ namespace mongo {
255255
return Status(ErrorCodes::BadValue, "error: no args for --configdb");
256256
}
257257

258-
splitStringDelim(params["sharding.configDB"].as<std::string>(),
259-
&mongosGlobalParams.configdbs, ',');
260-
if (mongosGlobalParams.configdbs.size() != 1 && mongosGlobalParams.configdbs.size() != 3) {
258+
std::string configdbString = params["sharding.configDB"].as<std::string>();
259+
try {
260+
std::string errmsg;
261+
mongosGlobalParams.configdbs = ConnectionString::parse(configdbString, errmsg);
262+
263+
if (!mongosGlobalParams.configdbs.isValid()) {
264+
return Status(ErrorCodes::BadValue,
265+
str::stream() << "Invalid configdb connection string: " << errmsg);
266+
}
267+
} catch (const DBException& e) {
268+
return Status(ErrorCodes::BadValue,
269+
str::stream() << "Invalid configdb connection string: " << e.what());
270+
}
271+
272+
std::vector<HostAndPort> configServers = mongosGlobalParams.configdbs.getServers();
273+
274+
if (configServers.size() != 1 && configServers.size() != 3) {
261275
return Status(ErrorCodes::BadValue, "need either 1 or 3 configdbs");
262276
}
263277

264-
if (mongosGlobalParams.configdbs.size() == 1) {
278+
if (configServers.size() == 1) {
265279
warning() << "running with 1 config server should be done only for testing purposes "
266-
<< "and is not recommended for production" << endl;
280+
<< "and is not recommended for production" << endl;
267281
}
268282

269283
if (params.count("upgrade")) {

src/mongo/s/mongos_options.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#pragma once
3030

3131
#include "mongo/base/status.h"
32+
#include "mongo/client/connection_string.h"
3233
#include "mongo/db/server_options.h"
3334
#include "mongo/util/options_parser/environment.h"
3435
#include "mongo/util/options_parser/option_section.h"
@@ -43,7 +44,7 @@ namespace mongo {
4344
namespace moe = mongo::optionenvironment;
4445

4546
struct MongosGlobalParams {
46-
std::vector<std::string> configdbs;
47+
ConnectionString configdbs;
4748
bool upgrade;
4849

4950
MongosGlobalParams() :

src/mongo/s/server.cpp

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -347,26 +347,20 @@ static int _main() {
347347
startSignalProcessingThread();
348348

349349
// we either have a setting where all processes are in localhost or none are
350-
for (std::vector<std::string>::const_iterator it = mongosGlobalParams.configdbs.begin();
351-
it != mongosGlobalParams.configdbs.end(); ++it) {
352-
try {
350+
std::vector<HostAndPort> configServers = mongosGlobalParams.configdbs.getServers();
351+
for (std::vector<HostAndPort>::const_iterator it = configServers.begin();
352+
it != configServers.end(); ++it) {
353353

354-
HostAndPort configAddr( *it ); // will throw if address format is invalid
355-
356-
if (it == mongosGlobalParams.configdbs.begin()) {
357-
grid.setAllowLocalHost( configAddr.isLocalHost() );
358-
}
359-
360-
if ( configAddr.isLocalHost() != grid.allowLocalHost() ) {
361-
mongo::log(LogComponent::kDefault)
362-
<< "cannot mix localhost and ip addresses in configdbs" << endl;
363-
return 10;
364-
}
354+
const HostAndPort& configAddr = *it;
365355

356+
if (it == configServers.begin()) {
357+
grid.setAllowLocalHost( configAddr.isLocalHost() );
366358
}
367-
catch ( DBException& e) {
368-
mongo::log(LogComponent::kDefault) << "configdb: " << e.what() << endl;
369-
return 9;
359+
360+
if ( configAddr.isLocalHost() != grid.allowLocalHost() ) {
361+
mongo::log(LogComponent::kDefault)
362+
<< "cannot mix localhost and ip addresses in configdbs" << endl;
363+
return 10;
370364
}
371365
}
372366

0 commit comments

Comments
 (0)