Skip to content

Commit a18dcc7

Browse files
committed
SERVER-3962 previous commit wasnt right. backport.
1 parent 47f28e9 commit a18dcc7

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

client/dbclient_rs.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -247,23 +247,27 @@ namespace mongo {
247247
}
248248

249249
HostAndPort ReplicaSetMonitor::getSlave() {
250-
LOG(2) << "selecting a slave from replica set " << getServerAddress() << endl;
250+
LOG(2) << "dbclient_rs getSlave " << getServerAddress() << endl;
251251

252252
scoped_lock lk( _lock );
253253

254254
for ( unsigned ii = 0; ii < _nodes.size(); ii++ ) {
255255
_nextSlave = ( _nextSlave + 1 ) % _nodes.size();
256-
if ( _nextSlave == _master ) {
257-
LOG(2) << "not selecting " << _nodes[_nextSlave] << " as it is the current master" << endl;
258-
continue;
256+
if ( _nextSlave != _master ) {
257+
if ( _nodes[ _nextSlave ].okForSecondaryQueries() )
258+
return _nodes[ _nextSlave ].addr;
259+
LOG(2) << "dbclient_rs getSlave not selecting " << _nodes[_nextSlave] << ", not currently okForSecondaryQueries" << endl;
259260
}
260-
if ( _nodes[ _nextSlave ].okForSecondaryQueries() )
261-
return _nodes[ _nextSlave ].addr;
262-
LOG(2) << "not selecting " << _nodes[_nextSlave] << " as it is not ok to use" << endl;
263-
}
264-
265-
LOG(2) << "no suitable slave nodes found, returning default node " << _nodes[ 0 ] << endl;
261+
}
266262

263+
if( _master >= 0 ) {
264+
assert( static_cast<unsigned>(_master) < _nodes.size() );
265+
LOG(2) << "dbclient_rs getSlave no member in secondary state found, returning primary " << _nodes[ _master ] << endl;
266+
return _nodes[_master].addr;
267+
}
268+
269+
LOG(2) << "dbclient_rs getSlave no suitable member found, returning first node " << _nodes[ 0 ] << endl;
270+
assert( _nodes.size() > 0 );
267271
return _nodes[0].addr;
268272
}
269273

0 commit comments

Comments
 (0)