How to use the pymongo.errors.AutoReconnect function in pymongo

To help you get started, we’ve selected a few pymongo examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Georce / lepus / lepus / pymongo-2.7 / pymongo / mongo_replica_set_client.py View on Github external
assert response["number_returned"] == 1
        result = response["data"][0]

        helpers._check_command_response(result, self.disconnect)

        # write commands - skip getLastError checking
        if is_command:
            return result

        # getLastError
        error_msg = result.get("err", "")
        if error_msg is None:
            return result
        if error_msg.startswith("not master"):
            self.disconnect()
            raise AutoReconnect(error_msg)

        code = result.get("code")
        if code in (11000, 11001, 12582):
            raise DuplicateKeyError(result["err"], code, result)
        raise OperationFailure(result["err"], code, result)
github openstack / ceilometer / ceilometer / storage / mongo / utils.py View on Github external
def closure(self, *args, **kwargs):
        # NOTE(idegtiarov) options max_retries and retry_interval have been
        # registered in storage.__init__ in oslo_db.options.set_defaults
        # default values for both options are 10.
        max_retries = self.conf.database.max_retries
        retry_interval = self.conf.database.retry_interval
        attempts = 0
        while True:
            try:
                return call(self, *args, **kwargs)
            except pymongo.errors.AutoReconnect as err:
                if 0 <= max_retries <= attempts:
                    LOG.error('Unable to reconnect to the primary mongodb '
                              'after %(retries)d retries. Giving up.' %
                              {'retries': max_retries})
                    raise
                LOG.warning(_('Unable to reconnect to the primary '
                              'mongodb: %(errmsg)s. Trying again in '
                              '%(retry_interval)d seconds.') %
                            {'errmsg': err, 'retry_interval': retry_interval})
                attempts += 1
                time.sleep(retry_interval)
    return closure
github Georce / lepus / lepus / pymongo-2.7 / pymongo / mongo_replica_set_client.py View on Github external
if node in hosts:
                    members[node] = new_member
                    if response['ismaster']:
                        writer = node

            except (ConnectionFailure, socket.error), why:
                if member:
                    member.pool.discard_socket(sock_info)
                errors.append("%s:%d: %s" % (node[0], node[1], str(why)))
            if hosts:
                break
        else:
            # We've changed nothing. On the next refresh, we'll try the same
            # list of hosts: rs_state.hosts or self.__seeds.
            if errors:
                raise AutoReconnect(', '.join(errors))
            raise ConfigurationError('No suitable hosts found')

        # Ensure we have a pool for each member, and find the primary.
        for host in hosts:
            if host in members:
                # This member was the first we connected to, in the loop above.
                continue

            member, sock_info = rs_state.get(host), None
            try:
                if member:
                    sock_info = self.__socket(member, force=True)
                    res, ping_time = self.__simple_command(
                        sock_info, 'admin', {'ismaster': 1})

                    if res.get('setName') != self.__name:
github mongodb / mongo-snippets / replication / simple-setup.py View on Github external
config = {"_id": options.name,
          "members": []}
for i in range(len(nodes)):
    member = {"_id": i, "host": nodes[i]}
    if i < options.arbiters:
        member["arbiterOnly"] = True
    config["members"].append(member)

sleep(10)
# Last node won't be an arbiter, so use that for initiate
Connection(nodes[-1], slave_okay=True, ssl=SSL).admin.command("replSetInitiate", config)
while (True):
    try:
        print Connection(nodes, ssl=SSL).admin.command("replSetGetStatus")
        break
    except AutoReconnect:
        sleep(1)

print "*** READY ***"
print

try:
    printer_thread.join()
except KeyboardInterrupt:
    pass
github Georce / lepus / lepus / pymongo-2.7 / pymongo / mongo_client.py View on Github external
# wrong replica set name, or incompatible wire protocol.
                raise
            except Exception, why:
                errors.append(str(why))

        if len(mongos_candidates):
            # If we have a mongos seed list, pick the "nearest" member.
            chosen_member = self.__pick_nearest(mongos_candidates)
            mongoses = frozenset(m.host for m in mongos_candidates)

            # The first time, __nodes is empty and mongoses becomes nodes.
            return chosen_member, self.__nodes or mongoses

        if not chosen_member:
            # Couldn't find a suitable host.
            raise AutoReconnect(', '.join(errors))

        return chosen_member, discovered_nodes
github Sefaria / Sefaria-Project / sefaria / search_new.py View on Github external
    @classmethod
    def get_all_versions(tries=0, versions=None, page=0):
        if versions is None:
            versions = []
        try:
            version_limit = 10
            temp_versions = []
            first_run = True
            while first_run or len(temp_versions) > 0:
                temp_versions = VersionSet(limit=version_limit, page=page).array()
                versions += temp_versions
                page += 1
                first_run = False
            return versions
        except pymongo.errors.AutoReconnect as e:
            if tries < 200:
                pytime.sleep(5)
                return get_all_versions(tries+1, versions, page)
            else:
                print "Tried: {} times. Got {} versions".format(tries, len(versions))
                raise e
github sneridagh / osiris / osiris / store / mongodb_store.py View on Github external
def replacement(*args, **kwargs):
        # Handle exceptions in case of database operational error
        try:
            response = fun(*args, **kwargs)
        except AutoReconnect:
            tryin_to_reconnect = True
            while tryin_to_reconnect:
                try:
                    response = fun(*args, **kwargs)
                except AutoReconnect:
                    pass
                else:
                    tryin_to_reconnect = False
        else:
            return response
    return replacement
github naparuba / opsbro / data / global-configuration / packs / mongodb / collectors / pymongo / command_cursor.py View on Github external
def __send_message(self, operation):
        """Send a getmore message and handle the response.
        """
        client = self.__collection.database.client
        listeners = client._event_listeners
        publish = listeners.enabled_for_commands
        try:
            response = client._send_message_with_response(
                operation, address=self.__address)
        except AutoReconnect:
            # Don't try to send kill cursors on another socket
            # or to another server. It can cause a _pinValue
            # assertion on some server releases if we get here
            # due to a socket timeout.
            self.__killed = True
            raise

        cmd_duration = response.duration
        rqst_id = response.request_id
        from_command = response.from_command

        if publish:
            start = datetime.datetime.now()
        try:
            doc = helpers._unpack_response(response.data,
                                           self.__id,
github pulp / pulp / server / pulp / server / db / connection.py View on Github external
            @wraps(method)
            def retry(*args, **kwargs):
                while True:
                    try:
                        return method(*args, **kwargs)

                    except AutoReconnect:
                        msg = _('%(method)s operation failed on %(name)s') % {
                            'method': method.__name__, 'name': full_name}
                        _logger.error(msg)

                        time.sleep(0.3)