diff options
author | Eliot Horowitz <eliot@10gen.com> | 2011-11-20 03:12:55 -0500 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2011-11-21 01:01:43 -0500 |
commit | f48ecaa2c6deceff2df9b8b0d7d266d9ae5b3aef (patch) | |
tree | 0c4b103a04d8863243c52d4bbbf53d17345c69ce | |
parent | 163a0ede78d1dcbde86346248ff993db876f2a03 (diff) | |
download | mongo-f48ecaa2c6deceff2df9b8b0d7d266d9ae5b3aef.tar.gz |
when we get a "not master" error on a command, check for new primary SERVER-4324
-rw-r--r-- | client/dbclient.cpp | 21 | ||||
-rw-r--r-- | client/dbclient.h | 6 |
2 files changed, 25 insertions, 2 deletions
diff --git a/client/dbclient.cpp b/client/dbclient.cpp index dadf7e4f38a..5faeccfc90c 100644 --- a/client/dbclient.cpp +++ b/client/dbclient.cpp @@ -247,6 +247,11 @@ namespace mongo { return o["ok"].trueValue(); } + bool DBClientWithCommands::isNotMasterErrorString( const BSONElement& e ) { + return e.type() == String && str::contains( e.valuestr() , "not master" ); + } + + enum QueryOptions DBClientWithCommands::availableOptions() { if ( !_haveCachedAvailableOptions ) { BSONObj ret; @@ -599,6 +604,19 @@ namespace mongo { return true; } + + inline bool DBClientConnection::runCommand(const string &dbname, const BSONObj& cmd, BSONObj &info, int options) { + if ( DBClientWithCommands::runCommand( dbname , cmd , info , options ) ) + return true; + + if ( clientSet && isNotMasterErrorString( info["errmsg"] ) ) { + clientSet->isntMaster(); + } + + return false; + } + + void DBClientConnection::_checkConnection() { if ( !_failed ) return; @@ -982,8 +1000,7 @@ namespace mongo { if ( clientSet && nReturned ) { assert(data); BSONObj o(data); - BSONElement e = getErrField(o); - if ( e.type() == String && str::contains( e.valuestr() , "not master" ) ) { + if ( isNotMasterErrorString( getErrField(o) ) ) { clientSet->isntMaster(); } } diff --git a/client/dbclient.h b/client/dbclient.h index 2b4bb857e2d..ea55bb474af 100644 --- a/client/dbclient.h +++ b/client/dbclient.h @@ -721,8 +721,12 @@ namespace mongo { } protected: + /** if the result of a command is ok*/ bool isOk(const BSONObj&); + /** if the element contains a not master error */ + bool isNotMasterErrorString( const BSONElement& e ); + BSONObj _countCmd(const string &ns, const BSONObj& query, int options, int limit, int skip ); enum QueryOptions availableOptions(); @@ -892,6 +896,8 @@ namespace mongo { unsigned long long query( boost::function<void(const BSONObj&)> f, const string& ns, Query query, const BSONObj *fieldsToReturn = 0, int queryOptions = 0); unsigned long long query( boost::function<void(DBClientCursorBatchIterator&)> f, const string& ns, Query query, const BSONObj *fieldsToReturn = 0, int queryOptions = 0); + virtual bool runCommand(const string &dbname, const BSONObj& cmd, BSONObj &info, int options=0); + /** @return true if this connection is currently in a failed state. When autoreconnect is on, a connection will transition back to an ok state after reconnecting. |