ReplicaSetMonitor::ReplicaSetMonitor( const string& name , const vector<HostAndPort>& servers ) : _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 ); if ( _name.size() == 0 ) { warning() << "replica set name empty, first node: " << servers[0] << endl; } string errmsg; for ( unsigned i=0; i<servers.size(); i++ ) { bool haveAlready = false; for ( unsigned n = 0; n < _nodes.size() && ! haveAlready; n++ ) haveAlready = ( _nodes[n].addr == servers[i] ); if( haveAlready ) continue; auto_ptr<DBClientConnection> conn( new DBClientConnection( true , 0, 5.0 ) ); if (!conn->connect( servers[i] , errmsg ) ) { log(1) << "error connecting to seed " << servers[i] << ": " << errmsg << endl; // skip seeds that don't work continue; } _nodes.push_back( Node( servers[i] , conn.release() ) ); string maybePrimary; if (_checkConnection( _nodes[_nodes.size()-1].conn , maybePrimary, false)) { break; } } }
void ReplicaSetMonitor::_check() { bool triedQuickCheck = false; LOG(1) << "_check : " << getServerAddress() << endl; for ( int retry = 0; retry < 2; retry++ ) { for ( unsigned i=0; i<_nodes.size(); i++ ) { DBClientConnection * c; { scoped_lock lk( _lock ); c = _nodes[i].conn; } string maybePrimary; if ( _checkConnection( c , maybePrimary , retry ) ) { _master = i; return; } if ( ! triedQuickCheck && maybePrimary.size() ) { int x = _find( maybePrimary ); if ( x >= 0 ) { triedQuickCheck = true; string dummy; DBClientConnection * testConn; { scoped_lock lk( _lock ); testConn = _nodes[x].conn; } if ( _checkConnection( testConn , dummy , false ) ) { _master = x; return; } } } } sleepsecs(1); } }
void ReplicaSetMonitor::check() { // first see if the current master is fine if ( _master >= 0 ) { string temp; if ( _checkConnection( _nodes[_master].conn , temp , false ) ) { // current master is fine, so we're done return; } } // we either have no master, or the current is dead _check(); }
ReplicaSetMonitor::ReplicaSetMonitor( const string& name , const vector<HostAndPort>& servers ) : _lock( "ReplicaSetMonitor instance" ) , _name( name ) , _master(-1) { string errmsg; for ( unsigned i=0; i<servers.size(); i++ ) { auto_ptr<DBClientConnection> conn( new DBClientConnection( true , 0, 5.0 ) ); if (!conn->connect( servers[i] , errmsg ) ) { log(1) << "error connecting to seed " << servers[i] << ": " << errmsg << endl; // skip seeds that don't work continue; } _nodes.push_back( Node( servers[i] , conn.release() ) ); string maybePrimary; if (_checkConnection( _nodes[_nodes.size()-1].conn , maybePrimary, false)) { break; } } }