Replace retrying with tenacity

We are replacing all usages of the 'retrying' package with
'tenacity' as the author of retrying is not actively maintaining
the project. Tenacity is a fork of retrying, but has improved the
interface and extensibility (see [1] for more details). Our end
goal here is removing the retrying package from our requirements.

Tenacity provides the same functionality as retrying, but has the
following major differences to account for:
- Tenacity uses seconds rather than ms as retrying did.
- Tenacity has different kwargs for the decorator and
Retrying class itself.
- Tenacity has a different approach for retrying args by
using classes for its stop/wait/retry kwargs.
- By default tenacity raises a RetryError if a retried callable
times out; retrying raises the last exception from the callable.
Tenacity provides backwards compatibility here by offering
the 'reraise' kwarg.
- Tenacity defines 'time.sleep' as a default value for a kwarg.
That said consumers who need to mock patch time.sleep
need to account for this via mocking of time.sleep before
tenacity is imported.
- For retries that check a result, tenacity will raise if the retried
function raises, whereas retrying retried on all exceptions.

This patch updates all usages of retrying with tenacity.
Unit tests will be included where applicable.

Note: This change is not newton critical so projects are welcome
to hold off on committing until post-newton. Ideally this change
will merge by the first part of Ocata so dependant functionality
can land and have time to solidify for Ocata.

[1] https://github.com/jd/tenacity

Closes-Bug: #1635395

Change-Id: I1c0620894d07d58efbba5226b5244fec950354ca
This commit is contained in:
Boden R 2016-09-02 13:34:06 -06:00
parent cbb7dc0fb2
commit e8bd6424e9
5 changed files with 48 additions and 12 deletions

View File

@ -23,8 +23,8 @@ from neutron_lib import exceptions
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import excutils
import retrying
import six
import tenacity
from neutron._i18n import _, _LE, _LI, _LW
from neutron.agent.common import utils
@ -74,12 +74,12 @@ def _ofport_retry(fn):
@six.wraps(fn)
def wrapped(*args, **kwargs):
self = args[0]
new_fn = retrying.retry(
retry_on_result=_ofport_result_pending,
stop_max_delay=self.vsctl_timeout * 1000,
wait_exponential_multiplier=10,
wait_exponential_max=1000,
retry_on_exception=lambda _: False)(fn)
new_fn = tenacity.retry(
reraise=True,
retry=tenacity.retry_if_result(_ofport_result_pending),
wait=tenacity.wait_exponential(multiplier=0.01, max=1),
stop=tenacity.stop_after_delay(
self.vsctl_timeout))(fn)
return new_fn(*args, **kwargs)
return wrapped
@ -275,7 +275,7 @@ class OVSBridge(BaseOVS):
ofport = INVALID_OFPORT
try:
ofport = self._get_port_ofport(port_name)
except retrying.RetryError:
except tenacity.RetryError:
LOG.exception(_LE("Timed out retrieving ofport on port %s."),
port_name)
return ofport

View File

@ -18,8 +18,8 @@ import traceback
from ovs.db import idl
from ovs import poller
import retrying
from six.moves import queue as Queue
import tenacity
from neutron.agent.ovsdb.native import helpers
from neutron.agent.ovsdb.native import idlutils
@ -96,8 +96,9 @@ class Connection(object):
helpers.enable_connection_uri(self.connection)
# There is a small window for a race, so retry up to a second
@retrying.retry(wait_exponential_multiplier=10,
stop_max_delay=1000)
@tenacity.retry(wait=tenacity.wait_exponential(multiplier=0.01),
stop=tenacity.stop_after_delay(1),
reraise=True)
def do_get_schema_helper():
return idlutils.get_schema_helper(self.connection,
self.schema_name)

View File

@ -18,6 +18,7 @@ import mock
from neutron_lib import exceptions
from oslo_serialization import jsonutils
from oslo_utils import uuidutils
import tenacity
import testtools
from neutron.agent.common import config
@ -805,6 +806,20 @@ class OVS_Lib_Test(base.BaseTestCase):
'tap99id', data, extra_calls_and_values=extra_calls_and_values)
self._assert_vif_port(vif_port, ofport=1337, mac="de:ad:be:ef:13:37")
def test_get_port_ofport_retry(self):
with mock.patch.object(
self.br, 'db_get_val',
side_effect=[[], [], [], [], 1]):
self.assertEqual(1, self.br._get_port_ofport('1'))
def test_get_port_ofport_retry_fails(self):
# after 16 calls the retry will timeout and raise
with mock.patch.object(
self.br, 'db_get_val',
side_effect=[[] for _ in range(16)]):
self.assertRaises(tenacity.RetryError,
self.br._get_port_ofport, '1')
class TestDeferredOVSBridge(base.BaseTestCase):

View File

@ -82,3 +82,23 @@ class TestOVSNativeConnection(base.BaseTestCase):
idl_instance = idl_class.return_value
self.connection.start()
self.assertEqual(idl_instance, self.connection.idl)
@mock.patch.object(connection, 'threading')
@mock.patch.object(connection.idlutils, 'wait_for_change')
@mock.patch.object(connection, 'idl')
@mock.patch.object(connection.helpers, 'enable_connection_uri')
@mock.patch.object(connection.idlutils, 'get_schema_helper')
def test_do_get_schema_helper_retry(self, mock_get_schema_helper,
mock_enable_conn,
mock_idl,
mock_wait_for_change,
mock_threading):
mock_helper = mock.Mock()
# raise until 3rd retry attempt
mock_get_schema_helper.side_effect = [Exception(), Exception(),
mock_helper]
conn = connection.Connection(
mock.Mock(), mock.Mock(), mock.Mock())
conn.start()
self.assertEqual(3, len(mock_get_schema_helper.mock_calls))
mock_helper.register_all.assert_called_once_with()

View File

@ -19,7 +19,7 @@ netaddr!=0.7.16,>=0.7.13 # BSD
netifaces>=0.10.4 # MIT
neutron-lib>=0.4.0 # Apache-2.0
python-neutronclient>=5.1.0 # Apache-2.0
retrying!=1.3.0,>=1.2.3 # Apache-2.0
tenacity>=3.1.1 # Apache-2.0
ryu!=4.1,!=4.2,!=4.2.1,!=4.4,>=3.30 # Apache-2.0
SQLAlchemy<1.1.0,>=1.0.10 # MIT
WebOb>=1.6.0 # MIT