From 92dca86601a5f54c76e748e04ecb3a96d35ca0b4 Mon Sep 17 00:00:00 2001 From: Eliot Horowitz Date: Mon, 22 Mar 2010 09:51:06 -0400 Subject: [PATCH] some abstractions in prep for SHARDING-39 --- client/dbclient.h | 7 ++++++ client/syncclusterconnection.cpp | 15 ++++++++++++- client/syncclusterconnection.h | 23 +++++++++++++++++--- s/config.cpp | 4 ++-- scripting/sm_db.cpp | 37 +++++++++++++------------------- 5 files changed, 58 insertions(+), 28 deletions(-) diff --git a/client/dbclient.h b/client/dbclient.h index cc5aff2234d..3141d4588b6 100644 --- a/client/dbclient.h +++ b/client/dbclient.h @@ -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 _queryOnActive(const string &ns, Query query, int nToReturn, int nToSkip, diff --git a/s/config.cpp b/s/config.cpp index 4cc8ff25f01..467b5d8e597 100644 --- a/s/config.cpp +++ b/s/config.cpp @@ -381,12 +381,12 @@ namespace mongo { for ( set::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 ) diff --git a/scripting/sm_db.cpp b/scripting/sm_db.cpp index 1d0fe119511..c74baf96ba1 100644 --- a/scripting/sm_db.cpp +++ b/scripting/sm_db.cpp @@ -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 ) ){ @@ -157,30 +159,21 @@ namespace mongo { } ScriptEngine::runConnectCallback( *c ); } - else { // paired - int numCommas = 0; - for ( uint i=0; i 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 ) ) ) );