本文整理汇总了C++中Shard::getConnString方法的典型用法代码示例。如果您正苦于以下问题:C++ Shard::getConnString方法的具体用法?C++ Shard::getConnString怎么用?C++ Shard::getConnString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Shard
的用法示例。
在下文中一共展示了Shard::getConnString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: commandOpUnsharded
Status Strategy::commandOpUnsharded(const std::string& db,
const BSONObj& command,
int options,
const std::string& versionedNS,
CommandResult* cmdResult) {
// Note that this implementation will not handle targeting retries and fails when the
// sharding metadata is too stale
auto status = grid.catalogCache()->getDatabase(db);
if (!status.isOK()) {
mongoutils::str::stream ss;
ss << "Passthrough command failed: " << command.toString()
<< " on ns " << versionedNS << ". Caused by " << causedBy(status.getStatus());
return Status(ErrorCodes::IllegalOperation, ss);
}
shared_ptr<DBConfig> conf = status.getValue();
if (conf->isSharded(versionedNS)) {
mongoutils::str::stream ss;
ss << "Passthrough command failed: " << command.toString()
<< " on ns " << versionedNS << ". Cannot run on sharded namespace.";
return Status(ErrorCodes::IllegalOperation, ss);
}
Shard primaryShard = conf->getPrimary();
BSONObj shardResult;
try {
ShardConnection conn(primaryShard.getConnString(), "");
// TODO: this can throw a stale config when mongos is not up-to-date -- fix.
if (!conn->runCommand(db, command, shardResult, options)) {
conn.done();
return Status(ErrorCodes::OperationFailed,
str::stream() << "Passthrough command failed: " << command
<< " on ns " << versionedNS
<< "; result: " << shardResult);
}
conn.done();
}
catch (const DBException& ex) {
return ex.toStatus();
}
// Fill out the command result.
cmdResult->shardTarget = primaryShard;
cmdResult->result = shardResult;
cmdResult->target = primaryShard.getConnString();
return Status::OK();
}
示例2: chooseWriteHost
Status DBClientShardResolver::chooseWriteHost( const string& shardName,
ConnectionString* shardHost ) const {
// Declare up here for parsing later
string errMsg;
// Special-case for config
if (shardName == "config") {
*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 );
}
return findMaster(shard.getConnString().toString(), shardHost);
}
示例3: getUserObj
bool CmdAuthenticate::getUserObj(const string& dbname, const string& user, BSONObj& userObj, string& pwd) {
if (user == internalSecurity.user) {
uassert(15890, "key file must be used to log in with internal user",
!cmdLine.keyFile.empty());
pwd = internalSecurity.pwd;
}
else {
string systemUsers = dbname + ".system.users";
DBConfigPtr config = grid.getDBConfig( systemUsers );
Shard s = config->getShard( systemUsers );
static BSONObj userPattern = BSON("user" << 1);
scoped_ptr<ScopedDbConnection> conn(
ScopedDbConnection::getInternalScopedDbConnection( s.getConnString(), 30.0 ) );
OCCASIONALLY conn->get()->ensureIndex(systemUsers, userPattern, false, "user_1");
{
BSONObjBuilder b;
b << "user" << user;
BSONObj query = b.done();
userObj = conn->get()->findOne(systemUsers, query, 0, QueryOption_SlaveOk);
if( userObj.isEmpty() ) {
log() << "auth: couldn't find user " << user << ", " << systemUsers << endl;
conn->done(); // return to pool
return false;
}
}
pwd = userObj.getStringField("pwd");
conn->done(); // return to pool
}
return true;
}
示例4: setShardVersion
bool setShardVersion(DBClientBase& conn,
const string& ns,
const string& configServerPrimary,
ChunkVersion version,
ChunkManager* manager,
bool authoritative,
BSONObj& result) {
BSONObjBuilder cmdBuilder;
cmdBuilder.append("setShardVersion", ns);
cmdBuilder.append("configdb", configServerPrimary);
Shard s = Shard::make(conn.getServerAddress());
cmdBuilder.append("shard", s.getName());
cmdBuilder.append("shardHost", s.getConnString());
if (ns.size() > 0) {
version.addToBSON(cmdBuilder);
}
else {
cmdBuilder.append("init", true);
}
if (authoritative) {
cmdBuilder.appendBool("authoritative", 1);
}
BSONObj cmd = cmdBuilder.obj();
LOG(1) << " setShardVersion " << s.getName() << " " << conn.getServerAddress()
<< " " << ns << " " << cmd
<< (manager ? string(str::stream() << " " << manager->getSequenceNumber()) : "");
return conn.runCommand("admin", cmd, result, 0);
}
示例5:
ShardConnection::ShardConnection(const Shard& s, const string& ns, ChunkManagerPtr manager)
: _addr(s.getConnString()),
_ns(ns),
_manager( manager ) {
_init();
}
示例6: set
void set( const string& name , const Shard& s , bool setName = true , bool setAddr = true ) {
scoped_lock lk( _mutex );
ShardPtr ss( new Shard( s ) );
if ( setName )
_lookup[name] = ss;
if ( setAddr )
_installHost( s.getConnString() , ss );
}
示例7: moveAndCommit
bool Chunk::moveAndCommit(const Shard& to,
long long chunkSize /* bytes */,
const WriteConcernOptions* writeConcern,
bool waitForDelete,
int maxTimeMS,
BSONObj& res) const {
uassert( 10167 , "can't move shard to its current location!" , getShard() != to );
log() << "moving chunk ns: " << _manager->getns() << " moving ( " << toString() << ") "
<< _shard.toString() << " -> " << to.toString();
Shard from = _shard;
ScopedDbConnection fromconn(from.getConnString());
BSONObjBuilder builder;
builder.append("moveChunk", _manager->getns());
builder.append("from", from.getAddress().toString());
builder.append("to", to.getAddress().toString());
// NEEDED FOR 2.0 COMPATIBILITY
builder.append("fromShard", from.getName());
builder.append("toShard", to.getName());
///////////////////////////////
builder.append("min", _min);
builder.append("max", _max);
builder.append("maxChunkSizeBytes", chunkSize);
builder.append("shardId", genID());
builder.append("configdb", configServer.modelServer());
// For legacy secondary throttle setting.
bool secondaryThrottle = true;
if (writeConcern &&
writeConcern->wNumNodes <= 1 &&
writeConcern->wMode.empty()) {
secondaryThrottle = false;
}
builder.append("secondaryThrottle", secondaryThrottle);
if (secondaryThrottle && writeConcern) {
builder.append("writeConcern", writeConcern->toBSON());
}
builder.append("waitForDelete", waitForDelete);
builder.append(LiteParsedQuery::cmdOptionMaxTimeMS, maxTimeMS);
builder.append("epoch", _manager->getVersion().epoch());
bool worked = fromconn->runCommand("admin", builder.done(), res);
fromconn.done();
LOG( worked ? 1 : 0 ) << "moveChunk result: " << res;
// if succeeded, needs to reload to pick up the new location
// if failed, mongos may be stale
// reload is excessive here as the failure could be simply because collection metadata is taken
_manager->reload();
return worked;
}
示例8: calcInitSplitsAndShards
void ChunkManager::calcInitSplitsAndShards( const Shard& primary,
const vector<BSONObj>* initPoints,
const vector<Shard>* initShards,
vector<BSONObj>* splitPoints,
vector<Shard>* shards ) const
{
verify( _chunkMap.size() == 0 );
unsigned long long numObjects = 0;
Chunk c(this, _keyPattern.getKeyPattern().globalMin(),
_keyPattern.getKeyPattern().globalMax(), primary);
if ( !initPoints || !initPoints->size() ) {
// discover split points
{
// get stats to see if there is any data
ScopedDbConnection shardConn(primary.getConnString());
numObjects = shardConn->count( getns() );
shardConn.done();
}
if ( numObjects > 0 )
c.pickSplitVector( *splitPoints , Chunk::MaxChunkSize );
// since docs alread exists, must use primary shard
shards->push_back( primary );
} else {
// make sure points are unique and ordered
set<BSONObj> orderedPts;
for ( unsigned i = 0; i < initPoints->size(); ++i ) {
BSONObj pt = (*initPoints)[i];
orderedPts.insert( pt );
}
for ( set<BSONObj>::iterator it = orderedPts.begin(); it != orderedPts.end(); ++it ) {
splitPoints->push_back( *it );
}
if ( !initShards || !initShards->size() ) {
// If not specified, only use the primary shard (note that it's not safe for mongos
// to put initial chunks on other shards without the primary mongod knowing).
shards->push_back( primary );
} else {
std::copy( initShards->begin() , initShards->end() , std::back_inserter(*shards) );
}
}
}
示例9: setShardVersion
bool setShardVersion( DBClientBase & conn , const string& ns , ShardChunkVersion version , bool authoritative , BSONObj& result ){
BSONObjBuilder cmdBuilder;
cmdBuilder.append( "setShardVersion" , ns.c_str() );
cmdBuilder.append( "configdb" , configServer.modelServer() );
cmdBuilder.appendTimestamp( "version" , version );
cmdBuilder.appendOID( "serverID" , &serverID );
if ( authoritative )
cmdBuilder.appendBool( "authoritative" , 1 );
Shard s = Shard::make( conn.getServerAddress() );
cmdBuilder.append( "shard" , s.getName() );
cmdBuilder.append( "shardHost" , s.getConnString() );
BSONObj cmd = cmdBuilder.obj();
log(1) << " setShardVersion " << s.getName() << " " << conn.getServerAddress() << " " << ns << " " << cmd << " " << &conn << endl;
return conn.runCommand( "admin" , cmd , result );
}
示例10: _setupAuth
void ClientInfo::_setupAuth() {
std::string adminNs = "admin";
DBConfigPtr config = grid.getDBConfig(adminNs);
Shard shard = config->getShard(adminNs);
scoped_ptr<ScopedDbConnection> connPtr(
ScopedDbConnection::getInternalScopedDbConnection(shard.getConnString(), 30.0));
ScopedDbConnection& conn = *connPtr;
//
// Note: The connection mechanism here is *not* ideal, and should not be used elsewhere.
// It is safe in this particular case because the admin database is always on the config
// server and does not move.
//
AuthorizationManager* authManager = new AuthorizationManager(new AuthExternalStateImpl());
Status status = authManager->initialize(conn.get());
massert(16479,
mongoutils::str::stream() << "Error initializing AuthorizationManager: "
<< status.reason(),
status == Status::OK());
setAuthorizationManager(authManager);
}
示例11: _checkLocalHostSpecialAdmin
void AuthenticationInfo::_checkLocalHostSpecialAdmin() {
if (noauth || !_isLocalHost || !_isLocalHostAndLocalHostIsAuthorizedForAll) {
return;
}
string adminNs = "admin.system.users";
DBConfigPtr config = grid.getDBConfig( adminNs );
Shard s = config->getShard( adminNs );
//
// Note: The connection mechanism here is *not* ideal, and should not be used elsewhere.
// It is safe in this particular case because the admin database is always on the config
// server and does not move.
//
scoped_ptr<ScopedDbConnection> conn(
ScopedDbConnection::getInternalScopedDbConnection(s.getConnString(), 30.0));
BSONObj result = (*conn)->findOne("admin.system.users", Query());
if( result.isEmpty() ) {
if( ! _warned ) {
// you could get a few of these in a race, but that's ok
_warned = true;
log() << "note: no users configured in admin.system.users, allowing localhost access" << endl;
}
// Must return conn to pool
// TODO: Check for errors during findOne(), or just let the conn die?
conn->done();
_isLocalHostAndLocalHostIsAuthorizedForAll = true;
return;
}
// Must return conn to pool
conn->done();
_isLocalHostAndLocalHostIsAuthorizedForAll = false;
}
示例12:
ShardConnection::ShardConnection( const Shard& s , const string& ns )
: _addr( s.getConnString() ) , _ns( ns ) {
_init();
}
示例13: lockNamespaceOnServer
bool lockNamespaceOnServer( const Shard& shard, const string& ns ){
ScopedDbConnection conn( shard.getConnString() );
bool res = lockNamespaceOnServer( conn.conn() , ns );
conn.done();
return res;
}
示例14:
ShardConnection::ShardConnection( const Shard& s , const string& ns, bool ignoreDirect )
: _addr( s.getConnString() ) , _ns( ns ) {
_init( ignoreDirect );
}
示例15: chooseWriteHost
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, "" );
}