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

repl pairs support in conn pool

This commit is contained in:
dwight 2009-02-07 09:21:29 -05:00
parent c2cde25267
commit 9590e60f21
4 changed files with 55 additions and 17 deletions

View File

@ -26,7 +26,7 @@ namespace mongo {
DBConnectionPool pool;
DBClientConnection* DBConnectionPool::get(const string& host) {
DBClientBase* DBConnectionPool::get(const string& host) {
boostlock L(poolMutex);
PoolForHost *&p = pools[host];
@ -34,15 +34,28 @@ namespace mongo {
p = new PoolForHost();
if ( p->pool.empty() ) {
string errmsg;
DBClientConnection *c = new DBClientConnection();
if ( !c->connect(host.c_str(), errmsg) ) {
delete c;
uassert( (string)"dbconnectionpool: connect failed" + host , false);
return 0;
DBClientBase *c;
if( host.find(',') == string::npos ) {
DBClientConnection *cc = new DBClientConnection(true);
if ( !cc->connect(host.c_str(), errmsg) ) {
delete cc;
uassert( (string)"dbconnectionpool: connect failed" + host , false);
return 0;
}
c = cc;
}
else {
DBClientPaired *p = new DBClientPaired();
if( !p->connect(host) ) {
delete p;
uassert( (string)"dbconnectionpool: connect failed [2] " + host , false);
return 0;
}
c = p;
}
return c;
}
DBClientConnection *c = p->pool.front();
DBClientBase *c = p->pool.front();
p->pool.pop();
return c;
}

View File

@ -24,7 +24,7 @@
namespace mongo {
struct PoolForHost {
std::queue<DBClientConnection*> pool;
std::queue<DBClientBase*> pool;
};
/** Database connection pool.
@ -45,8 +45,8 @@ namespace mongo {
boost::mutex poolMutex;
map<string,PoolForHost*> pools; // servername -> pool
public:
DBClientConnection *get(const string& host);
void release(const string& host, DBClientConnection *c) {
DBClientBase *get(const string& host);
void release(const string& host, DBClientBase *c) {
boostlock L(poolMutex);
pools[host]->pool.push(c);
}
@ -59,9 +59,9 @@ namespace mongo {
*/
class ScopedDbConnection {
const string host;
DBClientConnection *_conn;
DBClientBase *_conn;
public:
DBClientConnection& conn() {
DBClientBase& conn() {
uassert( "did you call done already" , _conn );
return *_conn;
}
@ -78,16 +78,18 @@ namespace mongo {
_conn = 0;
}
/* Call this when you are done with the ocnnection.
/* Call this when you are done with the connection.
Why? See note in the destructor below.
*/
void done() {
if ( ! _conn )
return;
/* we could do this, but instead of assume one is using autoreconnect mode on the connection
if ( _conn->isFailed() )
kill();
else
*/
pool.release(host, _conn);
_conn = 0;
}

View File

@ -614,7 +614,7 @@ namespace mongo {
On a failover situation, expect at least one operation to return an error (throw
an exception) before the failover is complete. Operations are not retried.
*/
class DBClientPaired : public DBClientWithCommands {
class DBClientPaired : public DBClientBase {
DBClientConnection left,right;
enum State {
NotSetL=0,
@ -635,6 +635,15 @@ namespace mongo {
*/
bool connect(const char *serverHostname1, const char *serverHostname2);
/** Connect to a server pair using a host pair string of the form
hostname[:port],hostname[:port]
*/
bool connect(string hostpairstring) {
uassert("bad hostpairstring", hostpairstring.find(',') != string::npos);
massert("not yet implemented", false);
return false;
}
bool auth(const char *dbname, const char *username, const char *pwd, string& errmsg);
/* throws userassertion "no master found" */
@ -673,6 +682,13 @@ namespace mongo {
void isntMaster() {
master = ( ( master == Left ) ? NotSetR : NotSetL );
}
/* TODO */
virtual bool call( Message &toSend, Message &response, bool assertOk=true ) { assert(false); return false; }
virtual void say( Message &toSend ) { assert(false); }
virtual void sayPiggyBack( Message &toSend ) { assert(false); }
virtual void checkResponse( const char *data, int nReturned ) { assert(false); }
};

View File

@ -29,7 +29,9 @@ namespace mongo {
}
ScopedDbConnection dbcon( r.primaryName() );
DBClientConnection &c = dbcon.conn();
DBClientBase &_c = dbcon.conn();
/** Todo: This will not work with Paired connections. Fix. */
DBClientConnection&c = dynamic_cast<DBClientConnection&>(_c);
Message response;
bool ok = c.port().call( r.m(), response);
uassert("dbgrid: error calling db", ok);
@ -54,7 +56,9 @@ namespace mongo {
log(3) << "getmore: " << ns << endl;
ScopedDbConnection dbcon( r.primaryName() );
DBClientConnection &c = dbcon.conn();
DBClientBase& _c = dbcon.conn();
/* TODO */
DBClientConnection &c = dynamic_cast<DBClientConnection&>(_c);
Message response;
bool ok = c.port().call( r.m() , response);
@ -70,7 +74,10 @@ namespace mongo {
log(3) << "write: " << ns << endl;
ScopedDbConnection dbcon( r.primaryName() );
DBClientConnection &c = dbcon.conn();
DBClientBase &_c = dbcon.conn();
/* TODO FIX - do not case and call DBClientBase::say() */
DBClientConnection&c = dynamic_cast<DBClientConnection&>(_c);
c.port().say( r.m() );