0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00

connPoolStats

This commit is contained in:
Eliot Horowitz 2010-04-28 23:59:24 -04:00
parent a155ee2932
commit 0d7e6b4913
2 changed files with 44 additions and 11 deletions

View File

@ -28,9 +28,9 @@ namespace mongo {
DBConnectionPool pool; DBConnectionPool pool;
DBClientBase* DBConnectionPool::get(const string& host) { DBClientBase* DBConnectionPool::get(const string& host) {
scoped_lock L(poolMutex); scoped_lock L(_mutex);
PoolForHost *&p = pools[host]; PoolForHost *&p = _pools[host];
if ( p == 0 ) if ( p == 0 )
p = new PoolForHost(); p = new PoolForHost();
if ( p->pool.empty() ) { if ( p->pool.empty() ) {
@ -65,6 +65,7 @@ namespace mongo {
uassert( 13071 , (string)"invalid hostname [" + host + "]" , 0 ); uassert( 13071 , (string)"invalid hostname [" + host + "]" , 0 );
c = 0; // prevents compiler warning c = 0; // prevents compiler warning
} }
p->created++;
return c; return c;
} }
DBClientBase *c = p->pool.top(); DBClientBase *c = p->pool.top();
@ -74,8 +75,8 @@ namespace mongo {
} }
void DBConnectionPool::flush(){ void DBConnectionPool::flush(){
scoped_lock L(poolMutex); scoped_lock L(_mutex);
for ( map<string,PoolForHost*>::iterator i = pools.begin(); i != pools.end(); i++ ){ for ( map<string,PoolForHost*>::iterator i = _pools.begin(); i != _pools.end(); i++ ){
PoolForHost* p = i->second; PoolForHost* p = i->second;
vector<DBClientBase*> all; vector<DBClientBase*> all;
@ -115,6 +116,19 @@ namespace mongo {
} }
} }
void DBConnectionPool::appendInfo( BSONObjBuilder& b ){
scoped_lock lk( _mutex );
BSONObjBuilder bb( b.subobjStart( "hosts" ) );
for ( map<string,PoolForHost*>::iterator i=_pools.begin(); i!=_pools.end(); ++i ){
string s = i->first;
BSONObjBuilder temp( bb.subobjStart( s.c_str() ) );
temp.append( "available" , (int)(i->second->pool.size()) );
temp.appendNumber( "created" , i->second->created );
temp.done();
}
bb.done();
}
ScopedDbConnection * ScopedDbConnection::steal(){ ScopedDbConnection * ScopedDbConnection::steal(){
assert( _conn ); assert( _conn );
ScopedDbConnection * n = new ScopedDbConnection( _host , _conn ); ScopedDbConnection * n = new ScopedDbConnection( _host , _conn );
@ -133,12 +147,11 @@ namespace mongo {
class PoolFlushCmd : public Command { class PoolFlushCmd : public Command {
public: public:
PoolFlushCmd() : Command( "connpoolsync" ){} PoolFlushCmd() : Command( "connPoolSync" , false , "connpoolsync" ){}
virtual void help( stringstream &help ) const { help<<"internal"; } virtual void help( stringstream &help ) const { help<<"internal"; }
virtual LockType locktype() const { return NONE; } virtual LockType locktype() const { return NONE; }
virtual bool run(const char*, mongo::BSONObj&, std::string&, mongo::BSONObjBuilder& result, bool){ virtual bool run(const char*, mongo::BSONObj&, std::string&, mongo::BSONObjBuilder& result, bool){
pool.flush(); pool.flush();
result << "ok" << 1;
return true; return true;
} }
virtual bool slaveOk() const { virtual bool slaveOk() const {
@ -147,4 +160,20 @@ namespace mongo {
} poolFlushCmd; } poolFlushCmd;
class PoolStats : public Command {
public:
PoolStats() : Command( "connPoolStats" ){}
virtual void help( stringstream &help ) const { help<<"stats about connection pool"; }
virtual LockType locktype() const { return NONE; }
virtual bool run(const char*, mongo::BSONObj&, std::string&, mongo::BSONObjBuilder& result, bool){
pool.appendInfo( result );
return true;
}
virtual bool slaveOk() const {
return true;
}
} poolStatsCmd;
} // namespace mongo } // namespace mongo

View File

@ -24,7 +24,10 @@
namespace mongo { namespace mongo {
struct PoolForHost { struct PoolForHost {
PoolForHost()
: created(0){}
std::stack<DBClientBase*> pool; std::stack<DBClientBase*> pool;
long long created;
}; };
class DBConnectionHook { class DBConnectionHook {
@ -52,8 +55,8 @@ namespace mongo {
} }
*/ */
class DBConnectionPool { class DBConnectionPool {
mongo::mutex poolMutex; mongo::mutex _mutex;
map<string,PoolForHost*> pools; // servername -> pool map<string,PoolForHost*> _pools; // servername -> pool
list<DBConnectionHook*> _hooks; list<DBConnectionHook*> _hooks;
void onCreate( DBClientBase * conn ); void onCreate( DBClientBase * conn );
@ -64,12 +67,13 @@ namespace mongo {
void release(const string& host, DBClientBase *c) { void release(const string& host, DBClientBase *c) {
if ( c->isFailed() ) if ( c->isFailed() )
return; return;
scoped_lock L(poolMutex); scoped_lock L(_mutex);
pools[host]->pool.push(c); _pools[host]->pool.push(c);
} }
void addHook( DBConnectionHook * hook ); void addHook( DBConnectionHook * hook );
void appendInfo( BSONObjBuilder& b );
}; };
extern DBConnectionPool pool; extern DBConnectionPool pool;
/** Use to get a connection from the pool. On exceptions things /** Use to get a connection from the pool. On exceptions things