File tree Expand file tree Collapse file tree 1 file changed +14
-10
lines changed Expand file tree Collapse file tree 1 file changed +14
-10
lines changed Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments