Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

somewhat smarter initial shard selection

  • Loading branch information...
commit e57c74e843d619e02ea1bb9e3f7166ca099a0976 1 parent d1082cf
@erh erh authored
Showing with 49 additions and 6 deletions.
  1. +22 −3 s/shard.cpp
  2. +27 −3 s/shard.h
View
25 s/shard.cpp
@@ -118,7 +118,7 @@ namespace mongo {
}
- BSONObj Shard::runCommand( const string& db , const BSONObj& cmd ){
+ BSONObj Shard::runCommand( const string& db , const BSONObj& cmd ) const {
ShardConnection conn( this );
BSONObj res;
bool ok = conn->runCommand( db , cmd , res );
@@ -132,6 +132,10 @@ namespace mongo {
return res;
}
+ ShardStatus Shard::getStatus() const {
+ return ShardStatus( *this , runCommand( "admin" , BSON( "serverStatus" << 1 ) ) );
+ }
+
void Shard::reloadShardInfo(){
staticShardInfo.reload();
}
@@ -145,7 +149,22 @@ namespace mongo {
if ( all.size() == 0 )
return EMPTY;
}
- Shard temp = all[rand()%all.size()];
- return temp;
+
+ 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
}
}
View
30 s/shard.h
@@ -105,12 +105,12 @@ namespace mongo {
return _addr.size() > 0 && _addr.size() > 0;
}
- BSONObj runCommand( const string& db , const string& simple ){
+ BSONObj runCommand( const string& db , const string& simple ) const {
return runCommand( db , BSON( simple << 1 ) );
}
- BSONObj runCommand( const string& db , const BSONObj& cmd );
+ BSONObj runCommand( const string& db , const BSONObj& cmd ) const ;
- ShardStatus getStatus() const;
+ ShardStatus getStatus() const ;
static void getAllShards( vector<Shard>& all );
@@ -129,7 +129,31 @@ namespace mongo {
};
class ShardStatus {
+ public:
+
+ ShardStatus( const Shard& shard , const BSONObj& obj );
+
+ friend ostream& operator << (ostream& out, const ShardStatus& s) {
+ out << (string)s;
+ return out;
+ }
+
+ operator string() const {
+ stringstream ss;
+ ss << "shard: " << _shard << " mapped: " << _mapped << " writeLock: " << _writeLock;
+ return ss.str();
+ }
+
+ bool operator<( const ShardStatus& other ) const{
+ return _mapped < other._mapped;
+ }
+
+ Shard shard() const {
+ return _shard;
+ }
+
private:
+ Shard _shard;
long long _mapped;
double _writeLock;
};
Please sign in to comment.
Something went wrong with that request. Please try again.