bool SyncSourceFeedback::_connect(const std::string& hostName) {
        if (hasConnection()) {
            return true;
        }
        log() << "replset setting syncSourceFeedback to " << hostName << rsLog;
        _connection.reset(new DBClientConnection(false, 0, OplogReader::tcp_timeout));
        string errmsg;
        try {
            if (!_connection->connect(hostName.c_str(), errmsg) ||
                (getGlobalAuthorizationManager()->isAuthEnabled() && !replAuthenticate())) {
                resetConnection();
                log() << "repl: " << errmsg << endl;
                return false;
            }
        }
        catch (const DBException& e) {
            log() << "Error connecting to " << hostName << ": " << e.what();
            resetConnection();
            return false;
        }

        if (!replHandshake()) {
            if (!supportsUpdater()) {
                return connectOplogReader(hostName);
            }
            return false;
        }
        return true;
    }
    void SyncSourceFeedback::run() {
        Client::initThread("SyncSourceFeedbackThread");
        OperationContextImpl txn;

        bool positionChanged = false;
        bool handshakeNeeded = false;
        ReplicationCoordinator* replCoord = getGlobalReplicationCoordinator();
        while (!inShutdown()) { // TODO(spencer): Remove once legacy repl coordinator is gone.
            {
                boost::unique_lock<boost::mutex> lock(_mtx);
                while (!_positionChanged && !_handshakeNeeded && !_shutdownSignaled) {
                    _cond.wait(lock);
                }

                if (_shutdownSignaled) {
                    break;
                }

                positionChanged = _positionChanged;
                handshakeNeeded = _handshakeNeeded;
                _positionChanged = false;
                _handshakeNeeded = false;
            }

            MemberState state = replCoord->getCurrentMemberState();
            if (state.primary() || state.fatal() || state.startup()) {
                continue;
            }
            const Member* target = BackgroundSync::get()->getSyncTarget();
            if (_syncTarget != target) {
                _resetConnection();
                _syncTarget = target;
            }
            if (!hasConnection()) {
                // fix connection if need be
                if (!target) {
                    sleepmillis(500);
                    continue;
                }
                if (!_connect(&txn, target->fullName())) {
                    sleepmillis(500);
                    continue;
                }
            }
            if (handshakeNeeded) {
                if (!replHandshake(&txn)) {
                    boost::unique_lock<boost::mutex> lock(_mtx);
                    _handshakeNeeded = true;
                    continue;
                }
            }
            if (positionChanged) {
                if (!updateUpstream(&txn)) {
                    boost::unique_lock<boost::mutex> lock(_mtx);
                    _positionChanged = true;
                }
            }
        }
        cc().shutdown();
    }
 void SyncSourceFeedback::run() {
     Client::initThread("SyncSourceFeedbackThread");
     while (true) {
         {
             boost::unique_lock<boost::mutex> lock(_mtx);
             while (!_positionChanged && !_handshakeNeeded) {
                 _cond.wait(lock);
             }
             boost::unique_lock<boost::mutex> conlock(_connmtx);
             const Member* target = replset::BackgroundSync::get()->getSyncTarget();
             if (_syncTarget != target) {
                 resetConnection();
                 _syncTarget = target;
             }
             if (!hasConnection()) {
                 // fix connection if need be
                 if (!target) {
                     continue;
                 }
                 if (!_connect(target->fullName())) {
                     continue;
                 }
                 else if (!supportsUpdater()) {
                     _handshakeNeeded = false;
                     _positionChanged = false;
                     continue;
                 }
             }
             if (_handshakeNeeded) {
                 if (!replHandshake()) {
                     _handshakeNeeded = true;
                     continue;
                 }
                 else {
                     _handshakeNeeded = false;
                 }
             }
             if (_positionChanged) {
                 if (!updateUpstream()) {
                     _positionChanged = true;
                     continue;
                 }
                 else {
                     _positionChanged = false;
                 }
             }
         }
     }
 }
Exemple #4
0
 bool OplogReader::connect(string hostName) {
     if( conn() == 0 ) {
         _conn = auto_ptr<DBClientConnection>(new DBClientConnection( false, 0, 0 /* tcp timeout */));
         string errmsg;
         ReplInfo r("trying to connect to sync source");
         if ( !_conn->connect(hostName.c_str(), errmsg) ||
                 (!noauth && !replAuthenticate(_conn.get())) ||
                 !replHandshake(_conn.get()) ) {
             resetConnection();
             log() << "repl:  " << errmsg << endl;
             return false;
         }
     }
     return true;
 }
Exemple #5
0
    bool OplogReader::connect(const std::string& hostName, const BSONObj& me) {
        if (conn()) {
            return true;
        }

        if (!commonConnect(hostName)) {
            return false;
        }

        if (!replHandshake(_conn.get(), me)) {
            return false;
        }

        return true;
    }
Exemple #6
0
    bool OplogReader::connect(const std::string& hostName, const double default_timeout) {
        if (conn() != 0) {
            return true;
        }

        if ( ! commonConnect(hostName, default_timeout) ) {
            return false;
        }
        
        
        if ( _doHandshake && ! replHandshake(_conn.get() ) ) {
            return false;
        }

        return true;
    }
    bool SyncSourceFeedback::_connect(const std::string& hostName) {
        if (hasConnection()) {
            return true;
        }
        _connection.reset(new DBClientConnection(false, 0, OplogReader::tcp_timeout));
        string errmsg;
        if (!_connection->connect(hostName.c_str(), errmsg) ||
                (AuthorizationManager::isAuthEnabled() && !replAuthenticate(true))) {
            resetConnection();
            log() << "repl: " << errmsg << endl;
            return false;
        }

        if (!replHandshake()) {
            if (!supportsUpdater()) {
                return connectOplogReader(hostName);
            }
            return false;
        }
        return true;
    }
 void SyncSourceFeedback::run() {
     Client::initThread("SyncSourceFeedbackThread");
     bool sleepNeeded = false;
     while (true) {
         if (sleepNeeded) {
             sleepmillis(500);
             sleepNeeded = false;
         }
         {
             boost::unique_lock<boost::mutex> lock(_mtx);
             while (!_positionChanged && !_handshakeNeeded) {
                 _cond.wait(lock);
             }
             if (theReplSet->isPrimary()) {
                 _positionChanged = false;
                 _handshakeNeeded = false;
                 continue;
             }
             const Member* target = replset::BackgroundSync::get()->getSyncTarget();
             boost::unique_lock<boost::mutex> connlock(_connmtx);
             if (_syncTarget != target) {
                 resetConnection();
                 _syncTarget = target;
             }
             if (!hasConnection()) {
                 // fix connection if need be
                 if (!target) {
                     sleepNeeded = true;
                     continue;
                 }
                 if (!_connect(target->fullName())) {
                     sleepNeeded = true;
                     continue;
                 }
                 else if (!supportsUpdater()) {
                     _handshakeNeeded = false;
                     _positionChanged = false;
                     continue;
                 }
             }
             if (_handshakeNeeded) {
                 if (!replHandshake()) {
                     _handshakeNeeded = true;
                     continue;
                 }
                 else {
                     _handshakeNeeded = false;
                     _positionChanged = true;
                 }
             }
             if (_positionChanged) {
                 if (!updateUpstream()) {
                     // no need to set _handshakeNeeded to true as a failed updateUpstream() call
                     // will call resetConnection() and when the new connection is established
                     // the handshake process will be run
                     _positionChanged = true;
                     continue;
                 }
                 else {
                     _positionChanged = false;
                 }
             }
         }
     }
 }