Permalink
Browse files

some refactoring for SERVER-946

  • Loading branch information...
1 parent f68e9e0 commit d1082cf676e372970ce62254fabb61b9c6fca96b @erh erh committed May 3, 2010
Showing with 64 additions and 44 deletions.
  1. +2 −2 s/balance.cpp
  2. +1 −1 s/chunk.cpp
  3. +5 −2 s/commands_admin.cpp
  4. +1 −21 s/config.cpp
  5. +0 −2 s/config.h
  6. +36 −13 s/shard.cpp
  7. +19 −3 s/shard.h
View
4 s/balance.cpp
@@ -111,9 +111,9 @@ namespace mongo {
return;
{
- list<Shard> all;
+ vector<Shard> all;
Shard::getAllShards( all );
- for ( list<Shard>::iterator i=all.begin(); i!=all.end(); ++i ){
+ for ( vector<Shard>::iterator i=all.begin(); i!=all.end(); ++i ){
// this just makes sure there is an entry in the map for every shard
Shard s = *i;
shards[s.getName()].size();
View
2 s/chunk.cpp
@@ -302,7 +302,7 @@ namespace mongo {
if ( ! toMove )
return false;
- Shard newLocation = grid.pickShardForNewDB();
+ Shard newLocation = Shard::pick();
if ( getShard() == newLocation ){
// if this is the best server, then we shouldn't do anything!
log(1) << "not moving chunk: " << toString() << " b/c would move to same place " << newLocation.toString() << " -> " << getShard().toString() << endl;
View
7 s/commands_admin.cpp
@@ -654,6 +654,9 @@ namespace mongo {
}
result.append( "added" , shard["host"].valuestrsafe() );
conn.done();
+
+ Shard::reloadShardInfo();
+
return true;
}
} addServer;
@@ -840,13 +843,13 @@ namespace mongo {
virtual void help( stringstream& help ) const { help << "list databases on cluster"; }
bool run(const char *ns, BSONObj& jsobj, string& errmsg, BSONObjBuilder& result, bool /*fromRepl*/) {
- list<Shard> shards;
+ vector<Shard> shards;
Shard::getAllShards( shards );
map<string,long long> sizes;
map< string,shared_ptr<BSONObjBuilder> > dbShardInfo;
- for ( list<Shard>::iterator i=shards.begin(); i!=shards.end(); i++ ){
+ for ( vector<Shard>::iterator i=shards.begin(); i!=shards.end(); i++ ){
Shard s = *i;
BSONObj x = s.runCommand( "admin" , "listDatabases" );
cout << s.toString() << "\t" << x.jsonString() << endl;
View
22 s/config.cpp
@@ -286,26 +286,6 @@ namespace mongo {
/* --- Grid --- */
- Shard Grid::pickShardForNewDB(){
- ShardConnection conn( configServer.getPrimary() );
-
- // TODO: this is temporary
-
- vector<string> all;
- auto_ptr<DBClientCursor> c = conn->query( "config.shards" , Query() );
- while ( c->more() ){
- BSONObj s = c->next();
- all.push_back( s["host"].valuestrsafe() );
- // look at s["maxSize"] if exists
- }
- conn.done();
-
- if ( all.size() == 0 )
- return Shard::EMPTY;
-
- return Shard::make( all[ rand() % all.size() ] );
- }
-
bool Grid::knowAboutShard( string name ) const{
ShardConnection conn( configServer.getPrimary() );
BSONObj shard = conn->findOne( "config.shards" , BSON( "host" << name ) );
@@ -336,7 +316,7 @@ namespace mongo {
if ( database == "admin" )
cc->_primary = configServer.getPrimary();
else
- cc->_primary = pickShardForNewDB();
+ cc->_primary = Shard::pick();
if ( cc->_primary.ok() ){
cc->save();
View
2 s/config.h
@@ -159,8 +159,6 @@ namespace mongo {
* on next getDBConfig call will fetch from db
*/
void removeDB( string db );
-
- Shard pickShardForNewDB();
bool knowAboutShard( string name ) const;
View
49 s/shard.cpp
@@ -26,16 +26,8 @@ namespace mongo {
class StaticShardInfo {
public:
- 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
-
+ void reload(){
+
list<BSONObj> all;
{
ShardConnection conn( configServer.getPrimary() );
@@ -46,7 +38,6 @@ namespace mongo {
conn.done();
}
-
scoped_lock lk( _mutex );
for ( list<BSONObj>::iterator i=all.begin(); i!=all.end(); ++i ){
@@ -57,6 +48,21 @@ namespace mongo {
_lookup[name] = s;
_lookup[host] = s;
}
+
+ }
+
+ 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 );
map<string,Shard>::iterator i = _lookup.find( ident );
if ( i == _lookup.end() ) printStackTrace();
@@ -73,7 +79,7 @@ namespace mongo {
_lookup[addr] = s;
}
- void getAllShards( list<Shard>& all ){
+ void getAllShards( vector<Shard>& all ){
scoped_lock lk( _mutex );
std::set<string> seen;
for ( map<string,Shard>::iterator i = _lookup.begin(); i!=_lookup.end(); ++i ){
@@ -107,7 +113,7 @@ namespace mongo {
_addr = s._addr;
}
- void Shard::getAllShards( list<Shard>& all ){
+ void Shard::getAllShards( vector<Shard>& all ){
staticShardInfo.getAllShards( all );
}
@@ -125,4 +131,21 @@ namespace mongo {
conn.done();
return res;
}
+
+ 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;
+ }
+ Shard temp = all[rand()%all.size()];
+ return temp;
+ }
}
View
22 s/shard.h
@@ -24,7 +24,8 @@
namespace mongo {
class ShardConnection;
-
+ class ShardStatus;
+
class Shard {
public:
Shard()
@@ -108,8 +109,17 @@ namespace mongo {
return runCommand( db , BSON( simple << 1 ) );
}
BSONObj runCommand( const string& db , const BSONObj& cmd );
-
- static void getAllShards( list<Shard>& all );
+
+ ShardStatus getStatus() const;
+
+ static void getAllShards( vector<Shard>& all );
+
+ /**
+ * picks a Shard for more load
+ */
+ static Shard pick();
+
+ static void reloadShardInfo();
static Shard EMPTY;
@@ -118,6 +128,12 @@ namespace mongo {
string _addr;
};
+ class ShardStatus {
+ private:
+ long long _mapped;
+ double _writeLock;
+ };
+
class ShardConnection {
public:
ShardConnection( const Shard * s )

0 comments on commit d1082cf

Please sign in to comment.