diff options
author | Eliot Horowitz <eliot@10gen.com> | 2011-05-12 16:59:57 -0400 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2011-05-12 18:31:59 -0400 |
commit | 2c201bf63b6e2377ce90298043e32f5902c34659 (patch) | |
tree | 05c5571aef40585c0a0be783349c130d5496a2af | |
parent | 802e514aac3d63110f9d854b9a21141f81f0a787 (diff) | |
download | mongo-2c201bf63b6e2377ce90298043e32f5902c34659.tar.gz |
deterministic slaveok failover
Conflicts:
client/dbclient_rs.cpp
-rw-r--r-- | client/dbclient_rs.cpp | 26 | ||||
-rw-r--r-- | client/dbclient_rs.h | 2 |
2 files changed, 14 insertions, 14 deletions
diff --git a/client/dbclient_rs.cpp b/client/dbclient_rs.cpp index 9f9cc7d6979..04099ee6bd5 100644 --- a/client/dbclient_rs.cpp +++ b/client/dbclient_rs.cpp @@ -74,7 +74,7 @@ namespace mongo { ReplicaSetMonitor::ReplicaSetMonitor( const string& name , const vector<HostAndPort>& servers ) - : _lock( "ReplicaSetMonitor instance" ) , _checkConnectionLock( "ReplicaSetMonitor check connection lock" ), _name( name ) , _master(-1) { + : _lock( "ReplicaSetMonitor instance" ) , _checkConnectionLock( "ReplicaSetMonitor check connection lock" ), _name( name ) , _master(-1), _nextSlave(0) { uassert( 13642 , "need at least 1 node for a replica set" , servers.size() > 0 ); @@ -221,19 +221,19 @@ namespace mongo { } HostAndPort ReplicaSetMonitor::getSlave() { - int x = rand() % _nodes.size(); + { scoped_lock lk( _lock ); for ( unsigned i=0; i<_nodes.size(); i++ ) { - int p = ( i + x ) % _nodes.size(); - if ( p == _master ) + _nextSlave = ( _nextSlave + 1 ) % _nodes.size(); + if ( _nextSlave == _master ) continue; - if ( _nodes[p].ok ) - return _nodes[p].addr; + if ( _nodes[ _nextSlave ].ok ) + return _nodes[ _nextSlave ].addr; } } - return _nodes[0].addr; + return _nodes[ 0 ].addr; } /** @@ -537,8 +537,8 @@ namespace mongo { try { return checkSlave()->query(ns,query,nToReturn,nToSkip,fieldsToReturn,queryOptions,batchSize); } - catch ( DBException & ) { - LOG(1) << "can't query replica set slave: " << _slaveHost << endl; + catch ( DBException &e ) { + LOG(1) << "can't query replica set slave " << i << " : " << _slaveHost << e.what() << endl; } } } @@ -555,8 +555,8 @@ namespace mongo { try { return checkSlave()->findOne(ns,query,fieldsToReturn,queryOptions); } - catch ( DBException & ) { - LOG(1) << "can't query replica set slave: " << _slaveHost << endl; + catch ( DBException &e ) { + LOG(1) << "can't findone replica set slave " << i << " : " << _slaveHost << e.what() << endl; } } } @@ -591,8 +591,8 @@ namespace mongo { *actualServer = s->getServerAddress(); return s->call( toSend , response , assertOk ); } - catch ( DBException & ) { - log(1) << "can't query replica set slave: " << _slaveHost << endl; + catch ( DBException &e ) { + LOG(1) << "can't call replica set slave " << i << " : " << _slaveHost << e.what() << endl; if ( actualServer ) *actualServer = ""; } diff --git a/client/dbclient_rs.h b/client/dbclient_rs.h index fca6e6e2810..3d49fd28c29 100644 --- a/client/dbclient_rs.h +++ b/client/dbclient_rs.h @@ -147,7 +147,7 @@ namespace mongo { vector<Node> _nodes; int _master; // which node is the current master. -1 means no master is known - + int _nextSlave; // which node is the current slave static mongo::mutex _setsLock; // protects _sets static map<string,ReplicaSetMonitorPtr> _sets; // set name to Monitor |