2010-04-27 18:32:59 +02:00
|
|
|
// shard.cpp
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copyright (C) 2008 10gen 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-04-27 21:27:52 +02:00
|
|
|
#include "pch.h"
|
2010-04-27 18:32:59 +02:00
|
|
|
#include "shard.h"
|
|
|
|
#include "config.h"
|
2010-05-19 23:01:09 +02:00
|
|
|
#include "request.h"
|
2010-04-28 20:36:41 +02:00
|
|
|
#include <set>
|
2010-04-27 18:32:59 +02:00
|
|
|
|
|
|
|
namespace mongo {
|
|
|
|
|
|
|
|
class StaticShardInfo {
|
|
|
|
public:
|
2010-05-26 06:46:49 +02:00
|
|
|
StaticShardInfo() : _mutex("StaticShardInfo") { }
|
2010-05-03 17:18:27 +02:00
|
|
|
void reload(){
|
|
|
|
|
2010-04-27 18:32:59 +02:00
|
|
|
list<BSONObj> all;
|
|
|
|
{
|
2010-05-20 19:36:29 +02:00
|
|
|
ScopedDbConnection conn( configServer.getPrimary() );
|
2010-04-27 18:32:59 +02:00
|
|
|
auto_ptr<DBClientCursor> c = conn->query( ShardNS::shard , Query() );
|
|
|
|
while ( c->more() ){
|
|
|
|
all.push_back( c->next().getOwned() );
|
|
|
|
}
|
|
|
|
conn.done();
|
|
|
|
}
|
|
|
|
|
|
|
|
scoped_lock lk( _mutex );
|
|
|
|
|
|
|
|
for ( list<BSONObj>::iterator i=all.begin(); i!=all.end(); ++i ){
|
|
|
|
BSONObj o = *i;
|
|
|
|
string name = o["_id"].String();
|
|
|
|
string host = o["host"].String();
|
|
|
|
Shard s( name , host );
|
|
|
|
_lookup[name] = s;
|
|
|
|
_lookup[host] = s;
|
|
|
|
}
|
2010-05-03 17:18:27 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const Shard& find( const string& ident ){
|
|
|
|
{
|
|
|
|
scoped_lock lk( _mutex );
|
|
|
|
map<string,Shard>::iterator i = _lookup.find( ident );
|
|
|
|
if ( i != _lookup.end() )
|
|
|
|
return i->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// not in our maps, re-load all
|
|
|
|
reload();
|
|
|
|
|
|
|
|
scoped_lock lk( _mutex );
|
2010-04-27 18:32:59 +02:00
|
|
|
|
|
|
|
map<string,Shard>::iterator i = _lookup.find( ident );
|
|
|
|
uassert( 13129 , (string)"can't find shard for: " + ident , i != _lookup.end() );
|
|
|
|
return i->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set( const string& name , const string& addr , bool setName = true , bool setAddr = true ){
|
|
|
|
Shard s(name,addr);
|
|
|
|
scoped_lock lk( _mutex );
|
|
|
|
if ( setName )
|
|
|
|
_lookup[name] = s;
|
|
|
|
if ( setAddr )
|
|
|
|
_lookup[addr] = s;
|
|
|
|
}
|
2010-04-29 02:56:40 +02:00
|
|
|
|
2010-05-03 17:18:27 +02:00
|
|
|
void getAllShards( vector<Shard>& all ){
|
2010-04-28 20:36:41 +02:00
|
|
|
scoped_lock lk( _mutex );
|
|
|
|
std::set<string> seen;
|
|
|
|
for ( map<string,Shard>::iterator i = _lookup.begin(); i!=_lookup.end(); ++i ){
|
|
|
|
Shard s = i->second;
|
2010-04-29 02:56:40 +02:00
|
|
|
if ( s.getName() == "config" )
|
|
|
|
continue;
|
2010-04-28 20:36:41 +02:00
|
|
|
if ( seen.count( s.getName() ) )
|
|
|
|
continue;
|
|
|
|
seen.insert( s.getName() );
|
|
|
|
all.push_back( s );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-27 18:32:59 +02:00
|
|
|
private:
|
|
|
|
map<string,Shard> _lookup;
|
2010-05-26 06:46:49 +02:00
|
|
|
mongo::mutex _mutex;
|
2010-04-27 18:32:59 +02:00
|
|
|
} staticShardInfo;
|
|
|
|
|
|
|
|
void Shard::setAddress( const string& addr , bool authoritative ){
|
|
|
|
assert( _name.size() );
|
|
|
|
_addr = addr;
|
|
|
|
if ( authoritative )
|
|
|
|
staticShardInfo.set( _name , _addr , true , false );
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shard::reset( const string& ident ){
|
|
|
|
const Shard& s = staticShardInfo.find( ident );
|
|
|
|
uassert( 13128 , (string)"can't find shard for: " + ident , s.ok() );
|
|
|
|
_name = s._name;
|
|
|
|
_addr = s._addr;
|
|
|
|
}
|
2010-04-28 20:36:41 +02:00
|
|
|
|
2010-05-03 17:18:27 +02:00
|
|
|
void Shard::getAllShards( vector<Shard>& all ){
|
2010-04-28 20:36:41 +02:00
|
|
|
staticShardInfo.getAllShards( all );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-03 17:43:12 +02:00
|
|
|
BSONObj Shard::runCommand( const string& db , const BSONObj& cmd ) const {
|
2010-05-20 19:36:29 +02:00
|
|
|
ScopedDbConnection conn( this );
|
2010-04-28 20:36:41 +02:00
|
|
|
BSONObj res;
|
|
|
|
bool ok = conn->runCommand( db , cmd , res );
|
|
|
|
if ( ! ok ){
|
|
|
|
stringstream ss;
|
|
|
|
ss << "runCommand (" << cmd << ") on shard (" << _name << ") failed : " << res;
|
|
|
|
throw UserException( 13136 , ss.str() );
|
|
|
|
}
|
|
|
|
res = res.getOwned();
|
|
|
|
conn.done();
|
|
|
|
return res;
|
|
|
|
}
|
2010-05-03 17:18:27 +02:00
|
|
|
|
2010-05-03 17:43:12 +02:00
|
|
|
ShardStatus Shard::getStatus() const {
|
|
|
|
return ShardStatus( *this , runCommand( "admin" , BSON( "serverStatus" << 1 ) ) );
|
|
|
|
}
|
|
|
|
|
2010-05-03 17:18:27 +02:00
|
|
|
void Shard::reloadShardInfo(){
|
|
|
|
staticShardInfo.reload();
|
|
|
|
}
|
|
|
|
|
|
|
|
Shard Shard::pick(){
|
|
|
|
vector<Shard> all;
|
|
|
|
staticShardInfo.getAllShards( all );
|
|
|
|
if ( all.size() == 0 ){
|
|
|
|
staticShardInfo.reload();
|
|
|
|
staticShardInfo.getAllShards( all );
|
|
|
|
if ( all.size() == 0 )
|
|
|
|
return EMPTY;
|
|
|
|
}
|
2010-05-03 17:43:12 +02:00
|
|
|
|
|
|
|
ShardStatus best = all[0].getStatus();
|
|
|
|
|
|
|
|
for ( size_t i=1; i<all.size(); i++ ){
|
|
|
|
ShardStatus t = all[i].getStatus();
|
|
|
|
if ( t < best )
|
|
|
|
best = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
log(1) << "picking shard: " << best << endl;
|
|
|
|
return best.shard();
|
|
|
|
}
|
|
|
|
|
|
|
|
ShardStatus::ShardStatus( const Shard& shard , const BSONObj& obj )
|
|
|
|
: _shard( shard ) {
|
|
|
|
_mapped = obj.getFieldDotted( "mem.mapped" ).numberLong();
|
|
|
|
_writeLock = 0; // TOOD
|
2010-05-03 17:18:27 +02:00
|
|
|
}
|
2010-05-19 23:01:09 +02:00
|
|
|
|
|
|
|
|
2010-04-27 18:32:59 +02:00
|
|
|
}
|