Exemplo n.º 1
0
 def test_wrap_db_retry_get_interval(self):
     x = api.wrap_db_retry(max_retries=5, retry_on_deadlock=True,
                           max_retry_interval=11)
     self.assertEqual(11, x.max_retry_interval)
     for i in (1, 2, 4):
         # With jitter: sleep_time = [0, 2 ** retry_times)
         sleep_time, n = x._get_inc_interval(i, True)
         self.assertEqual(2 * i, n)
         self.assertTrue(2 * i > sleep_time)
         # Without jitter: sleep_time = 2 ** retry_times
         sleep_time, n = x._get_inc_interval(i, False)
         self.assertEqual(2 * i, n)
         self.assertEqual(2 * i, sleep_time)
     for i in (8, 16, 32):
         sleep_time, n = x._get_inc_interval(i, False)
         self.assertEqual(x.max_retry_interval, sleep_time)
         self.assertEqual(2 * i, n)
         sleep_time, n = x._get_inc_interval(i, True)
         self.assertTrue(x.max_retry_interval >= sleep_time)
         self.assertEqual(2 * i, n)
Exemplo n.º 2
0
 def test_wrap_db_retry_get_interval(self):
     x = api.wrap_db_retry(max_retries=5,
                           retry_on_deadlock=True,
                           max_retry_interval=11)
     self.assertEqual(11, x.max_retry_interval)
     for i in (1, 2, 4):
         # With jitter: sleep_time = [0, 2 ** retry_times)
         sleep_time, n = x._get_inc_interval(i, True)
         self.assertEqual(2 * i, n)
         self.assertTrue(2 * i > sleep_time)
         # Without jitter: sleep_time = 2 ** retry_times
         sleep_time, n = x._get_inc_interval(i, False)
         self.assertEqual(2 * i, n)
         self.assertEqual(2 * i, sleep_time)
     for i in (8, 16, 32):
         sleep_time, n = x._get_inc_interval(i, False)
         self.assertEqual(x.max_retry_interval, sleep_time)
         self.assertEqual(2 * i, n)
         sleep_time, n = x._get_inc_interval(i, True)
         self.assertTrue(x.max_retry_interval >= sleep_time)
         self.assertEqual(2 * i, n)
Exemplo n.º 3
0
MAX_RETRIES = 10


def is_retriable(e):
    if _is_nested_instance(e, (db_exc.DBDeadlock, exc.StaleDataError)):
        return True
    # looking savepoints mangled by deadlocks. see bug/1590298 for details.
    return _is_nested_instance(e, db_exc.DBError) and '1305' in str(e)

is_deadlock = moves.moved_function(is_retriable, 'is_deadlock', __name__,
                                   message='use "is_retriable" instead',
                                   version='newton', removal_version='ocata')
retry_db_errors = oslo_db_api.wrap_db_retry(
    max_retries=MAX_RETRIES,
    retry_interval=0.1,
    inc_retry_interval=True,
    retry_on_request=True,
    exception_checker=is_retriable
)


def _is_nested_instance(e, etypes):
    """Check if exception or its inner excepts are an instance of etypes."""
    return (isinstance(e, etypes) or
            isinstance(e, exceptions.MultipleExceptions) and
            any(_is_nested_instance(i, etypes) for i in e.inner_exceptions))


@contextlib.contextmanager
def exc_to_retry(exceptions):
    try:
Exemplo n.º 4
0

def is_retriable(e):
    if getattr(e, '_RETRY_EXCEEDED', False):
        return False
    if _is_nested_instance(
            e, (db_exc.DBDeadlock, exc.StaleDataError,
                db_exc.DBConnectionError, db_exc.DBDuplicateEntry,
                db_exc.RetryRequest, obj_exc.NeutronDbObjectDuplicateEntry)):
        return True
    # looking savepoints mangled by deadlocks. see bug/1590298 for details.
    return _is_nested_instance(e, db_exc.DBError) and '1305' in str(e)


_retry_db_errors = oslo_db_api.wrap_db_retry(max_retries=MAX_RETRIES,
                                             retry_interval=0.1,
                                             inc_retry_interval=True,
                                             exception_checker=is_retriable)


def _tag_retriables_as_unretriable(f):
    """Puts a flag on retriable exceptions so is_retriable returns False.

    This decorator can be used outside of a retry decorator to prevent
    decorators higher up from retrying again.
    """
    @six.wraps(f)
    def wrapped(*args, **kwargs):
        try:
            return f(*args, **kwargs)
        except Exception as e:
            with excutils.save_and_reraise_exception():
Exemplo n.º 5
0
from oslo_db import exception as db_exc
from oslo_db.sqlalchemy import session
from oslo_utils import excutils
from oslo_utils import uuidutils

from neutron.common import exceptions as n_exc
from neutron.db import common_db_mixin


_FACADE = None

MAX_RETRIES = 10
is_deadlock = lambda e: isinstance(e, db_exc.DBDeadlock)
retry_db_errors = oslo_db_api.wrap_db_retry(
    max_retries=MAX_RETRIES,
    retry_on_request=True,
    exception_checker=is_deadlock
)


@contextlib.contextmanager
def exc_to_retry(exceptions):
    try:
        yield
    except Exception as e:
        with excutils.save_and_reraise_exception() as ctx:
            if isinstance(e, exceptions):
                ctx.reraise = False
                raise db_exc.RetryRequest(e)

Exemplo n.º 6
0
from oslo_db.sqlalchemy import session
from oslo_utils import excutils
from oslo_utils import uuidutils
from sqlalchemy import exc

from neutron.common import exceptions as n_exc
from neutron.db import common_db_mixin


_FACADE = None

MAX_RETRIES = 10
is_deadlock = lambda e: isinstance(e, db_exc.DBDeadlock)
retry_db_errors = oslo_db_api.wrap_db_retry(
    max_retries=MAX_RETRIES,
    retry_on_request=True,
    exception_checker=is_deadlock
)


@contextlib.contextmanager
def exc_to_retry(exceptions):
    try:
        yield
    except Exception as e:
        with excutils.save_and_reraise_exception() as ctx:
            if isinstance(e, exceptions):
                ctx.reraise = False
                raise db_exc.RetryRequest(e)

Exemplo n.º 7
0
from oslo_config import cfg
from oslo_db import api as oslo_db_api
from oslo_db import exception as os_db_exception
from oslo_db.sqlalchemy import session
from oslo_utils import uuidutils
from sqlalchemy import exc
from sqlalchemy import orm

from neutron.common import exceptions as n_exc
from neutron.db import common_db_mixin

_FACADE = None

MAX_RETRIES = 10
retry_db_errors = oslo_db_api.wrap_db_retry(max_retries=MAX_RETRIES,
                                            retry_on_deadlock=True)


def _create_facade_lazily():
    global _FACADE

    if _FACADE is None:
        _FACADE = session.EngineFacade.from_config(cfg.CONF, sqlite_fk=True)

    return _FACADE


def get_engine():
    """Helper method to grab engine."""
    facade = _create_facade_lazily()
    return facade.get_engine()
Exemplo n.º 8
0
from oslo_db import api as oslo_db_api
from oslo_log import log
from sqlalchemy.orm import exc

from networking_ovn.common import constants as ovn_const
from networking_ovn.common import exceptions as ovn_exc
from networking_ovn.common import utils
from networking_ovn.db import models

LOG = log.getLogger(__name__)

STD_ATTR_MAP = standard_attr.get_standard_attr_resource_model_map()

_wrap_db_retry = oslo_db_api.wrap_db_retry(
    max_retries=ovn_const.DB_MAX_RETRIES,
    retry_interval=ovn_const.DB_INITIAL_RETRY_INTERVAL,
    max_retry_interval=ovn_const.DB_MAX_RETRY_INTERVAL,
    inc_retry_interval=True,
    retry_on_deadlock=True)


def _get_standard_attr_id(session, resource_uuid, resource_type):
    try:
        row = session.query(
            STD_ATTR_MAP[resource_type]).filter_by(id=resource_uuid).one()
        return row.standard_attr_id
    except exc.NoResultFound:
        raise ovn_exc.StandardAttributeIDNotFound(resource_uuid=resource_uuid)


@_wrap_db_retry
def create_initial_revision(resource_uuid,
Exemplo n.º 9
0
import contextlib

from oslo_config import cfg
from oslo_db import api as oslo_db_api
from oslo_db.sqlalchemy import session
from oslo_utils import uuidutils
from sqlalchemy import exc

from neutron.common import exceptions as n_exc
from neutron.db import common_db_mixin


_FACADE = None

MAX_RETRIES = 10
retry_db_errors = oslo_db_api.wrap_db_retry(max_retries=MAX_RETRIES,
                                            retry_on_deadlock=True)


def _create_facade_lazily():
    global _FACADE

    if _FACADE is None:
        _FACADE = session.EngineFacade.from_config(cfg.CONF, sqlite_fk=True)

    return _FACADE


def get_engine():
    """Helper method to grab engine."""
    facade = _create_facade_lazily()
    return facade.get_engine()