Skip to content

Commit

Permalink
some cleaning and shardingState helper
Browse files Browse the repository at this point in the history
  • Loading branch information
erh committed Jun 15, 2010
1 parent d4d63ed commit e0431b6
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 10 deletions.
7 changes: 6 additions & 1 deletion s/d_logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ using namespace std;

namespace mongo {


bool objectBelongsToMe( const string& ns , const DiskLoc& loc ){
if ( ! shardingState.enabled() )
return true;

return true;
}


bool handlePossibleShardedMessage( Message &m, DbResponse &dbresponse ){
Expand Down
17 changes: 15 additions & 2 deletions s/d_logic.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,26 @@ namespace mongo {
const string& getConfigServer() const { return _configServer; }
void enable( const string& server );

void gotShardName( const string& name );
void gotShardHost( const string& host );

bool hasVersion( const string& ns ) const;
bool hasVersion( const string& ns , ConfigVersion& version ) const;
bool hasVersion( const string& ns );
bool hasVersion( const string& ns , ConfigVersion& version );
ConfigVersion& getVersion( const string& ns ); // TODO: this is dangeroues
void setVersion( const string& ns , const ConfigVersion& version );

void appendInfo( BSONObjBuilder& b );

private:

bool _enabled;

string _configServer;

string _shardName;
string _shardHost;

mongo::mutex _mutex;
NSVersionMap _versions;
};

Expand Down Expand Up @@ -100,6 +110,9 @@ namespace mongo {
*/
bool handlePossibleShardedMessage( Message &m, DbResponse &dbresponse );


bool objectBelongsToMe( const string& ns , const DiskLoc& loc );

// -----------------
// --- writeback ---
// -----------------
Expand Down
87 changes: 81 additions & 6 deletions s/d_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ namespace mongo {

// -----ShardingState START ----

ShardingState::ShardingState(){
_enabled = false;
ShardingState::ShardingState()
: _enabled(false) , _mutex( "ShardingState" ){
}

void ShardingState::enable( const string& server ){
Expand All @@ -58,13 +58,48 @@ namespace mongo {
}
}

void ShardingState::gotShardName( const string& name ){
if ( _shardName.size() == 0 ){
_shardName = name;
return;
}

if ( _shardName == name )
return;

stringstream ss;
ss << "gotShardName different than what i had before "
<< " before [" << _shardName << "] "
<< " got [" << name << "] "
;
uasserted( 13298 , ss.str() );
}

void ShardingState::gotShardHost( const string& host ){
if ( _shardHost.size() == 0 ){
_shardHost = host;
return;
}

if ( _shardHost == host )
return;

stringstream ss;
ss << "gotShardHost different than what i had before "
<< " before [" << _shardHost << "] "
<< " got [" << host << "] "
;
uasserted( 13299 , ss.str() );
}

bool ShardingState::hasVersion( const string& ns ) const {
bool ShardingState::hasVersion( const string& ns ){
scoped_lock lk(_mutex);
NSVersionMap::const_iterator i = _versions.find(ns);
return i != _versions.end();
}

bool ShardingState::hasVersion( const string& ns , ConfigVersion& version ) const {
bool ShardingState::hasVersion( const string& ns , ConfigVersion& version ){
scoped_lock lk(_mutex);
NSVersionMap::const_iterator i = _versions.find(ns);
if ( i == _versions.end() )
return false;
Expand All @@ -73,13 +108,36 @@ namespace mongo {
}

ConfigVersion& ShardingState::getVersion( const string& ns ){
scoped_lock lk(_mutex);
return _versions[ns];
}

void ShardingState::setVersion( const string& ns , const ConfigVersion& version ){
scoped_lock lk(_mutex);
_versions[ns] = version;
}

void ShardingState::appendInfo( BSONObjBuilder& b ){
b.appendBool( "enabled" , _enabled );
if ( ! _enabled )
return;

b.append( "configServer" , _configServer );
b.append( "shardName" , _shardName );
b.append( "shardHost" , _shardHost );

{
BSONObjBuilder bb( b.subobjStart( "versions" ) );

scoped_lock lk(_mutex);
for ( NSVersionMap::iterator i=_versions.begin(); i!=_versions.end(); ++i ){
bb.appendTimestamp( i->first.c_str() , i->second );
}
bb.done();
}

}

ShardingState shardingState;

// -----ShardingState END ----
Expand Down Expand Up @@ -196,6 +254,11 @@ namespace mongo {
}
}

if ( cmdObj["shard"].type() == String ){
shardingState.gotShardName( cmdObj["shard"].String() );
shardingState.gotShardHost( cmdObj["shardHost"].String() );
}

{ // setting up ids
if ( cmdObj["serverID"].type() != jstOID ){
// TODO: fix this
Expand Down Expand Up @@ -289,7 +352,7 @@ namespace mongo {
help << " example: { getShardVersion : 'alleyinsider.foo' } ";
}

virtual LockType locktype() const { return WRITE; } // TODO: figure out how to make this not need to lock
virtual LockType locktype() const { return NONE; }

bool run(const string& , BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
string ns = cmdObj["getShardVersion"].valuestrsafe();
Expand All @@ -312,7 +375,19 @@ namespace mongo {
}

} getShardVersion;


class ShardingStateCmd : public MongodShardCommand {
public:
ShardingStateCmd() : MongodShardCommand( "shardingState" ){}

virtual LockType locktype() const { return WRITE; } // TODO: figure out how to make this not need to lock

bool run(const string& , BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
shardingState.appendInfo( result );
return true;
}

} shardingStateCmd;

/**
* @ return true if not in sharded mode
Expand Down
6 changes: 5 additions & 1 deletion s/strategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,13 @@ namespace mongo {
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 " << conn.getServerAddress() << " " << ns << " " << cmd << " " << &conn << endl;
log(1) << " setShardVersion " << s.getName() << " " << conn.getServerAddress() << " " << ns << " " << cmd << " " << &conn << endl;

return conn.runCommand( "admin" , cmd , result );
}
Expand Down

0 comments on commit e0431b6

Please sign in to comment.