Files
mongo/client/connpool.cpp

427 lines
13 KiB
C++
Raw Normal View History

2008-10-31 19:17:54 -05:00
/* connpool.cpp
*/
2008-09-29 18:00:53 -04:00
/* Copyright 2009 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2008-10-31 19:17:54 -05:00
// _ todo: reconnect?
2010-04-27 15:27:52 -04:00
#include "pch.h"
2008-10-31 19:17:54 -05:00
#include "connpool.h"
//#include "../db/commands.h"
#include "syncclusterconnection.h"
2010-05-20 13:36:29 -04:00
#include "../s/shard.h"
2008-10-31 19:17:54 -05:00
2009-01-14 17:09:51 -05:00
namespace mongo {
// ------ PoolForHost ------
2011-01-04 00:40:41 -05:00
PoolForHost::~PoolForHost() {
while ( ! _pool.empty() ) {
StoredConnection sc = _pool.top();
delete sc.conn;
_pool.pop();
}
}
2011-05-19 14:19:40 -04:00
void PoolForHost::done( DBConnectionPool * pool, DBClientBase * c ) {
if ( _pool.size() >= _maxPerHost ) {
2011-10-20 13:44:36 -04:00
pool->onDestroy( c );
delete c;
}
else {
_pool.push(c);
}
}
2011-01-04 00:40:41 -05:00
DBClientBase * PoolForHost::get( DBConnectionPool * pool , double socketTimeout ) {
2011-01-04 00:40:41 -05:00
time_t now = time(0);
2011-01-04 00:40:41 -05:00
while ( ! _pool.empty() ) {
StoredConnection sc = _pool.top();
_pool.pop();
if ( ! sc.ok( now ) ) {
2011-10-20 13:44:36 -04:00
pool->onDestroy( sc.conn );
delete sc.conn;
continue;
}
assert( sc.conn->getSoTimeout() == socketTimeout );
return sc.conn;
}
2011-01-04 00:40:41 -05:00
return NULL;
}
2011-01-04 00:40:41 -05:00
void PoolForHost::flush() {
vector<StoredConnection> all;
2011-01-04 00:40:41 -05:00
while ( ! _pool.empty() ) {
StoredConnection c = _pool.top();
_pool.pop();
all.push_back( c );
bool res;
c.conn->isMaster( res );
}
2011-01-04 00:40:41 -05:00
for ( vector<StoredConnection>::iterator i=all.begin(); i != all.end(); ++i ) {
_pool.push( *i );
}
}
2009-01-14 17:09:51 -05:00
void PoolForHost::getStaleConnections( vector<DBClientBase*>& stale ) {
time_t now = time(0);
vector<StoredConnection> all;
while ( ! _pool.empty() ) {
StoredConnection c = _pool.top();
_pool.pop();
if ( c.ok( now ) )
all.push_back( c );
else
stale.push_back( c.conn );
}
for ( size_t i=0; i<all.size(); i++ ) {
_pool.push( all[i] );
}
}
2011-01-04 00:40:41 -05:00
PoolForHost::StoredConnection::StoredConnection( DBClientBase * c ) {
conn = c;
when = time(0);
}
2011-01-04 00:40:41 -05:00
bool PoolForHost::StoredConnection::ok( time_t now ) {
// if connection has been idle for 30 minutes, kill it
return ( now - when ) < 1800;
}
2010-12-01 23:53:52 -05:00
2011-01-04 00:40:41 -05:00
void PoolForHost::createdOne( DBClientBase * base) {
2010-12-01 23:53:52 -05:00
if ( _created == 0 )
_type = base->type();
2011-01-04 00:40:41 -05:00
_created++;
2010-12-01 23:53:52 -05:00
}
unsigned PoolForHost::_maxPerHost = 50;
// ------ DBConnectionPool ------
DBConnectionPool pool;
2011-01-04 00:40:41 -05:00
DBConnectionPool::DBConnectionPool()
: _mutex("DBConnectionPool") ,
_name( "dbconnectionpool" ) ,
_hooks( new list<DBConnectionHook*>() ) {
}
DBClientBase* DBConnectionPool::_get(const string& ident , double socketTimeout ) {
2011-04-21 11:16:27 -04:00
assert( ! inShutdown() );
2010-04-28 23:59:24 -04:00
scoped_lock L(_mutex);
PoolForHost& p = _pools[PoolKey(ident,socketTimeout)];
return p.get( this , socketTimeout );
}
2010-05-26 21:47:02 -04:00
DBClientBase* DBConnectionPool::_finishCreate( const string& host , double socketTimeout , DBClientBase* conn ) {
{
scoped_lock L(_mutex);
PoolForHost& p = _pools[PoolKey(host,socketTimeout)];
2010-12-01 23:53:52 -05:00
p.createdOne( conn );
}
try {
onCreate( conn );
onHandedOut( conn );
}
2011-11-21 11:24:57 -05:00
catch ( std::exception & ) {
delete conn;
throw;
}
2011-01-04 00:40:41 -05:00
return conn;
}
DBClientBase* DBConnectionPool::get(const ConnectionString& url, double socketTimeout) {
DBClientBase * c = _get( url.toString() , socketTimeout );
2011-01-04 00:40:41 -05:00
if ( c ) {
try {
onHandedOut( c );
}
2011-11-21 11:24:57 -05:00
catch ( std::exception& ) {
delete c;
throw;
}
return c;
2008-10-31 19:17:54 -05:00
}
2011-01-04 00:40:41 -05:00
string errmsg;
c = url.connect( errmsg, socketTimeout );
uassert( 13328 , _name + ": connect failed " + url.toString() + " : " + errmsg , c );
2011-01-04 00:40:41 -05:00
return _finishCreate( url.toString() , socketTimeout , c );
}
2011-01-04 00:40:41 -05:00
DBClientBase* DBConnectionPool::get(const string& host, double socketTimeout) {
DBClientBase * c = _get( host , socketTimeout );
2011-01-04 00:40:41 -05:00
if ( c ) {
try {
onHandedOut( c );
}
2011-11-21 11:24:57 -05:00
catch ( std::exception& ) {
delete c;
throw;
}
return c;
}
2011-01-04 00:40:41 -05:00
string errmsg;
ConnectionString cs = ConnectionString::parse( host , errmsg );
uassert( 13071 , (string)"invalid hostname [" + host + "]" + errmsg , cs.isValid() );
2011-01-04 00:40:41 -05:00
c = cs.connect( errmsg, socketTimeout );
2011-01-25 02:01:07 -05:00
if ( ! c )
throw SocketException( SocketException::CONNECT_ERROR , host , 11002 , str::stream() << _name << " error: " << errmsg );
return _finishCreate( host , socketTimeout , c );
2008-10-31 19:17:54 -05:00
}
2009-01-14 17:09:51 -05:00
2011-05-19 14:19:40 -04:00
void DBConnectionPool::release(const string& host, DBClientBase *c) {
if ( c->isFailed() ) {
2011-10-20 13:44:36 -04:00
onDestroy( c );
2011-05-19 14:19:40 -04:00
delete c;
return;
}
scoped_lock L(_mutex);
_pools[PoolKey(host,c->getSoTimeout())].done(this,c);
2011-05-19 14:19:40 -04:00
}
2011-01-04 00:40:41 -05:00
DBConnectionPool::~DBConnectionPool() {
// connection closing is handled by ~PoolForHost
2010-05-27 17:29:13 -04:00
}
2011-01-04 00:40:41 -05:00
void DBConnectionPool::flush() {
2010-04-28 23:59:24 -04:00
scoped_lock L(_mutex);
for ( PoolMap::iterator i = _pools.begin(); i != _pools.end(); i++ ) {
2010-05-26 21:47:02 -04:00
PoolForHost& p = i->second;
p.flush();
}
}
2011-01-04 00:40:41 -05:00
void DBConnectionPool::addHook( DBConnectionHook * hook ) {
_hooks->push_back( hook );
2009-09-11 16:10:48 -04:00
}
2011-01-04 00:40:41 -05:00
void DBConnectionPool::onCreate( DBClientBase * conn ) {
if ( _hooks->size() == 0 )
2009-09-11 16:10:48 -04:00
return;
2011-01-04 00:40:41 -05:00
for ( list<DBConnectionHook*>::iterator i = _hooks->begin(); i != _hooks->end(); i++ ) {
2009-09-11 16:10:48 -04:00
(*i)->onCreate( conn );
}
}
2011-01-04 00:40:41 -05:00
void DBConnectionPool::onHandedOut( DBClientBase * conn ) {
if ( _hooks->size() == 0 )
2009-09-14 11:33:20 -04:00
return;
2011-01-04 00:40:41 -05:00
for ( list<DBConnectionHook*>::iterator i = _hooks->begin(); i != _hooks->end(); i++ ) {
2009-09-14 11:33:20 -04:00
(*i)->onHandedOut( conn );
}
}
2011-10-20 13:44:36 -04:00
void DBConnectionPool::onDestroy( DBClientBase * conn ) {
2011-05-19 14:19:40 -04:00
if ( _hooks->size() == 0 )
return;
for ( list<DBConnectionHook*>::iterator i = _hooks->begin(); i != _hooks->end(); i++ ) {
2011-10-20 13:44:36 -04:00
(*i)->onDestroy( conn );
2011-05-19 14:19:40 -04:00
}
}
2011-01-04 00:40:41 -05:00
void DBConnectionPool::appendInfo( BSONObjBuilder& b ) {
2010-11-28 15:48:54 -05:00
int avail = 0;
2010-11-28 23:41:59 -05:00
long long created = 0;
2011-01-04 00:40:41 -05:00
2010-12-01 23:53:52 -05:00
map<ConnectionString::ConnectionType,long long> createdByType;
set<string> replicaSets;
BSONObjBuilder bb( b.subobjStart( "hosts" ) );
2010-11-28 15:48:54 -05:00
{
scoped_lock lk( _mutex );
for ( PoolMap::iterator i=_pools.begin(); i!=_pools.end(); ++i ) {
if ( i->second.numCreated() == 0 )
continue;
string s = str::stream() << i->first.ident << "::" << i->first.timeout;
2010-11-28 15:48:54 -05:00
BSONObjBuilder temp( bb.subobjStart( s ) );
temp.append( "available" , i->second.numAvailable() );
temp.appendNumber( "created" , i->second.numCreated() );
temp.done();
avail += i->second.numAvailable();
created += i->second.numCreated();
2010-12-01 23:53:52 -05:00
long long& x = createdByType[i->second.type()];
x += i->second.numCreated();
{
string setName = i->first.ident;
if ( setName.find( "/" ) != string::npos ) {
setName = setName.substr( 0 , setName.find( "/" ) );
replicaSets.insert( setName );
}
}
2010-11-28 15:48:54 -05:00
}
2010-04-28 23:59:24 -04:00
}
bb.done();
BSONObjBuilder setBuilder( b.subobjStart( "replicaSets" ) );
for ( set<string>::iterator i=replicaSets.begin(); i!=replicaSets.end(); ++i ) {
string rs = *i;
ReplicaSetMonitorPtr m = ReplicaSetMonitor::get( rs );
if ( ! m ) {
warning() << "no monitor for set: " << rs << endl;
continue;
}
BSONObjBuilder temp( setBuilder.subobjStart( rs ) );
m->appendInfo( temp );
temp.done();
}
setBuilder.done();
2011-01-04 00:40:41 -05:00
2010-12-01 23:53:52 -05:00
{
BSONObjBuilder temp( bb.subobjStart( "createdByType" ) );
2011-01-04 00:40:41 -05:00
for ( map<ConnectionString::ConnectionType,long long>::iterator i=createdByType.begin(); i!=createdByType.end(); ++i ) {
2010-12-01 23:53:52 -05:00
temp.appendNumber( ConnectionString::typeToString( i->first ) , i->second );
}
temp.done();
}
2010-11-28 15:48:54 -05:00
b.append( "totalAvailable" , avail );
2010-11-28 23:41:59 -05:00
b.appendNumber( "totalCreated" , created );
2010-04-28 23:59:24 -04:00
}
2011-01-18 19:51:09 -05:00
bool DBConnectionPool::serverNameCompare::operator()( const string& a , const string& b ) const{
2011-08-08 18:12:15 -04:00
const char* ap = a.c_str();
const char* bp = b.c_str();
while (true){
if (*ap == '\0' || *ap == '/'){
if (*bp == '\0' || *bp == '/')
return false; // equal strings
else
return true; // a is shorter
}
if (*bp == '\0' || *bp == '/')
return false; // b is shorter
if ( *ap < *bp)
return true;
else if (*ap > *bp)
return false;
++ap;
++bp;
}
assert(false);
}
bool DBConnectionPool::poolKeyCompare::operator()( const PoolKey& a , const PoolKey& b ) const {
2011-08-08 18:12:15 -04:00
if (DBConnectionPool::serverNameCompare()( a.ident , b.ident ))
return true;
2011-08-08 18:12:15 -04:00
if (DBConnectionPool::serverNameCompare()( b.ident , a.ident ))
return false;
return a.timeout < b.timeout;
}
void DBConnectionPool::taskDoWork() {
vector<DBClientBase*> toDelete;
{
// we need to get the connections inside the lock
// but we can actually delete them outside
scoped_lock lk( _mutex );
for ( PoolMap::iterator i=_pools.begin(); i!=_pools.end(); ++i ) {
i->second.getStaleConnections( toDelete );
}
}
for ( size_t i=0; i<toDelete.size(); i++ ) {
try {
2011-10-20 13:44:36 -04:00
onDestroy( toDelete[i] );
delete toDelete[i];
}
catch ( ... ) {
// we don't care if there was a socket error
}
}
}
// ------ ScopedDbConnection ------
2011-01-04 00:40:41 -05:00
ScopedDbConnection * ScopedDbConnection::steal() {
assert( _conn );
ScopedDbConnection * n = new ScopedDbConnection( _host , _conn, _socketTimeout );
_conn = 0;
return n;
}
2011-01-04 00:40:41 -05:00
void ScopedDbConnection::_setSocketTimeout(){
if( ! _conn ) return;
if( _conn->type() == ConnectionString::MASTER )
(( DBClientConnection* ) _conn)->setSoTimeout( _socketTimeout );
else if( _conn->type() == ConnectionString::SYNC )
(( SyncClusterConnection* ) _conn)->setAllSoTimeouts( _socketTimeout );
}
ScopedDbConnection::~ScopedDbConnection() {
2011-01-04 00:40:41 -05:00
if ( _conn ) {
2010-05-03 08:48:15 -04:00
if ( ! _conn->isFailed() ) {
/* see done() comments above for why we log this line */
log() << "~ScopedDbConnection: _conn != null" << endl;
2010-05-03 08:48:15 -04:00
}
kill();
}
}
ScopedDbConnection::ScopedDbConnection(const Shard& shard, double socketTimeout )
: _host( shard.getConnString() ) , _conn( pool.get(_host, socketTimeout) ), _socketTimeout( socketTimeout ) {
_setSocketTimeout();
2010-05-20 13:36:29 -04:00
}
2011-01-04 00:40:41 -05:00
ScopedDbConnection::ScopedDbConnection(const Shard* shard, double socketTimeout )
: _host( shard->getConnString() ) , _conn( pool.get(_host, socketTimeout) ), _socketTimeout( socketTimeout ) {
_setSocketTimeout();
2010-05-20 13:36:29 -04:00
}
2010-11-28 15:48:54 -05:00
AtomicUInt AScopedConnection::_numConnections;
2010-04-28 23:59:24 -04:00
2009-01-14 17:09:51 -05:00
} // namespace mongo