Skip to content

Commit

Permalink
some abstractions in prep for SHARDING-39
Browse files Browse the repository at this point in the history
  • Loading branch information
erh committed Mar 22, 2010
1 parent f154df0 commit 92dca86
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 28 deletions.
7 changes: 7 additions & 0 deletions client/dbclient.h
Expand Up @@ -723,6 +723,13 @@ namespace mongo {

virtual bool isFailed() const = 0;

static int countCommas( const string& s ){
int n = 0;
for ( unsigned i=0; i<s.size(); i++ )
if ( s[i] == ',' )
n++;
return n;
}
};

class DBClientPaired;
Expand Down
15 changes: 14 additions & 1 deletion client/syncclusterconnection.cpp
Expand Up @@ -197,7 +197,7 @@ namespace mongo {

void SyncClusterConnection::update( const string &ns , Query query , BSONObj obj , bool upsert , bool multi ){ assert(0); }

string SyncClusterConnection::toString(){
string SyncClusterConnection::_toString() const {
stringstream ss;
ss << "SyncClusterConnection [";
for ( size_t i=0; i<_conns.size(); i++ ){
Expand All @@ -209,5 +209,18 @@ namespace mongo {
return ss.str();
}

bool SyncClusterConnection::call( Message &toSend, Message &response, bool assertOk ){
assert(0);
return false;
}

void SyncClusterConnection::say( Message &toSend ){
assert(0);
}

void SyncClusterConnection::sayPiggyBack( Message &toSend ){
assert(0);
}


}
23 changes: 20 additions & 3 deletions client/syncclusterconnection.h
Expand Up @@ -25,7 +25,7 @@ namespace mongo {
* this is a connection to a cluster of servers that operate as one
* for super high durability
*/
class SyncClusterConnection : public DBClientWithCommands {
class SyncClusterConnection : public DBClientBase {
public:
/**
* @param commaSeperated should be 3 hosts comma seperated
Expand Down Expand Up @@ -60,9 +60,26 @@ namespace mongo {

virtual void update( const string &ns , Query query , BSONObj obj , bool upsert , bool multi );

virtual string toString();
private:
virtual string toString(){
return _toString();
}

virtual bool call( Message &toSend, Message &response, bool assertOk );
virtual void say( Message &toSend );
virtual void sayPiggyBack( Message &toSend );

virtual string getServerAddress() const {
return _toString();
}

virtual bool isFailed() const {
return false;
}

private:

string _toString() const;

bool _commandOnActive(const string &dbname, const BSONObj& cmd, BSONObj &info, int options=0);

auto_ptr<DBClientCursor> _queryOnActive(const string &ns, Query query, int nToReturn, int nToSkip,
Expand Down
4 changes: 2 additions & 2 deletions s/config.cpp
Expand Up @@ -381,12 +381,12 @@ namespace mongo {
for ( set<string>::iterator i=hosts.begin(); i!=hosts.end(); i++ ){
string host = *i;
bool ok = false;
for ( int x=0; x<10; x++ ){
for ( int x=10; x>0; x-- ){
if ( ! hostbyname( host.c_str() ).empty() ){
ok = true;
break;
}
log() << "can't resolve DNS for [" << host << "] sleeping and trying " << (10-x) << " more times" << endl;
log() << "can't resolve DNS for [" << host << "] sleeping and trying " << x << " more times" << endl;
sleepsecs( 10 );
}
if ( ! ok )
Expand Down
37 changes: 15 additions & 22 deletions scripting/sm_db.cpp
Expand Up @@ -144,11 +144,13 @@ namespace mongo {
string host = "127.0.0.1";
if ( argc > 0 )
host = c.toString( argv[0] );

int numCommas = DBClientBase::countCommas( host );

shared_ptr< DBClientWithCommands > conn;

string errmsg;
if ( host.find( "," ) == string::npos ){
if ( numCommas == 0 ){
DBClientConnection * c = new DBClientConnection( true );
conn.reset( c );
if ( ! c->connect( host , errmsg ) ){
Expand All @@ -157,30 +159,21 @@ namespace mongo {
}
ScriptEngine::runConnectCallback( *c );
}
else { // paired
int numCommas = 0;
for ( uint i=0; i<host.size(); i++ )
if ( host[i] == ',' )
numCommas++;

assert( numCommas > 0 );

if ( numCommas == 1 ){
DBClientPaired * c = new DBClientPaired();
conn.reset( c );
if ( ! c->connect( host ) ){
JS_ReportError( cx , "couldn't connect to pair" );
else if ( numCommas == 1 ){ // paired
DBClientPaired * c = new DBClientPaired();
conn.reset( c );
if ( ! c->connect( host ) ){
JS_ReportError( cx , "couldn't connect to pair" );
return JS_FALSE;
}
}
else if ( numCommas == 2 ){
conn.reset( new SyncClusterConnection( host ) );
}
else {
JS_ReportError( cx , "1 (paired) or 2(quorum) commas are allowed" );
return JS_FALSE;
}
}
else if ( numCommas == 2 ){
conn.reset( new SyncClusterConnection( host ) );
}
else {
JS_ReportError( cx , "1 (paired) or 2(quorum) commas are allowed" );
return JS_FALSE;
}


assert( JS_SetPrivate( cx , obj , (void*)( new shared_ptr< DBClientWithCommands >( conn ) ) ) );
Expand Down

0 comments on commit 92dca86

Please sign in to comment.