Status DBClientShardResolver::findMaster( const std::string connString, ConnectionString* resolvedHost ) { std::string errMsg; ConnectionString rawHost = ConnectionString::parse( connString, errMsg ); dassert( errMsg == "" ); dassert( rawHost.type() == ConnectionString::SET || rawHost.type() == ConnectionString::MASTER ); if ( rawHost.type() == ConnectionString::MASTER ) { *resolvedHost = rawHost; return Status::OK(); } // // If we need to, then get the particular node we're targeting in the replica set // // Don't create the monitor unless we need to - fast path ReplicaSetMonitorPtr replMonitor = ReplicaSetMonitor::get(rawHost.getSetName()); if (!replMonitor) { // Slow path std::set<HostAndPort> seedServers(rawHost.getServers().begin(), rawHost.getServers().end()); ReplicaSetMonitor::createIfNeeded(rawHost.getSetName(), seedServers); replMonitor = ReplicaSetMonitor::get(rawHost.getSetName()); } if (!replMonitor) { return Status( ErrorCodes::ReplicaSetNotFound, string("unknown replica set ") + rawHost.getSetName() ); } try { // This can throw when we don't find a master! HostAndPort masterHostAndPort = replMonitor->getMasterOrUassert(); *resolvedHost = ConnectionString::parse( masterHostAndPort.toString(), errMsg ); dassert( errMsg == "" ); return Status::OK(); } catch ( const DBException& ) { return Status( ErrorCodes::HostNotFound, string("could not contact primary for replica set ") + replMonitor->getName() ); } // Unreachable dassert( false ); return Status( ErrorCodes::UnknownError, "" ); }
Status DBClientShardResolver::findMaster( const std::string connString, ConnectionString* resolvedHost ) { std::string errMsg; ConnectionString rawHost = ConnectionString::parse( connString, errMsg ); dassert( errMsg == "" ); dassert( rawHost.type() == ConnectionString::SET || rawHost.type() == ConnectionString::MASTER ); if ( rawHost.type() == ConnectionString::MASTER ) { *resolvedHost = rawHost; return Status::OK(); } // // If we need to, then get the particular node we're targeting in the replica set // // Does not reload the monitor if it doesn't currently exist ReplicaSetMonitorPtr replMonitor = ReplicaSetMonitor::get( rawHost.getSetName(), false ); if ( !replMonitor ) { return Status( ErrorCodes::ReplicaSetNotFound, string("unknown replica set ") + rawHost.getSetName() ); } try { // This can throw when we don't find a master! HostAndPort masterHostAndPort = replMonitor->getMasterOrUassert(); *resolvedHost = ConnectionString::parse( masterHostAndPort.toString( true ), errMsg ); dassert( errMsg == "" ); return Status::OK(); } catch ( const DBException& ) { return Status( ErrorCodes::HostNotFound, string("could not contact primary for replica set ") + replMonitor->getName() ); } // Unreachable dassert( false ); return Status( ErrorCodes::UnknownError, "" ); }
Status DBClientShardResolver::chooseWriteHost( const string& shardName, ConnectionString* shardHost ) const { // Declare up here for parsing later string errMsg; // Special-case for config and admin if ( shardName == "config" || shardName == "admin" ) { *shardHost = ConnectionString::parse( configServer.modelServer(), errMsg ); dassert( errMsg == "" ); return Status::OK(); } // // First get the information about the shard from the shard cache // // Internally uses our shard cache, does no reload Shard shard = Shard::findIfExists( shardName ); if ( shard.getName() == "" ) { return Status( ErrorCodes::ShardNotFound, string("unknown shard name ") + shardName ); } ConnectionString rawShardHost = ConnectionString::parse( shard.getConnString(), errMsg ); dassert( errMsg == "" ); dassert( rawShardHost.type() == ConnectionString::SET || rawShardHost.type() == ConnectionString::MASTER ); if ( rawShardHost.type() == ConnectionString::MASTER ) { *shardHost = rawShardHost; return Status::OK(); } // // If we need to, then get the particular node we're targeting in the replica set // // Does not reload the monitor if it doesn't currently exist ReplicaSetMonitorPtr replMonitor = ReplicaSetMonitor::get( rawShardHost.getSetName(), false ); if ( !replMonitor ) { return Status( ErrorCodes::ReplicaSetNotFound, string("unknown replica set ") + rawShardHost.getSetName() ); } try { // This can throw when we don't find a master! HostAndPort masterHostAndPort = replMonitor->getMaster(); *shardHost = ConnectionString::parse( masterHostAndPort.toString( true ), errMsg ); dassert( errMsg == "" ); return Status::OK(); } catch ( const DBException& ) { return Status( ErrorCodes::HostNotFound, string("could not contact primary for replica set ") + replMonitor->getName() ); } // Unreachable dassert( false ); return Status( ErrorCodes::UnknownError, "" ); }