Add retry on StaleRevision errors for PATCH

Change-Id: I5249ce89c6684868b9e6f47256725093669c5150
This commit is contained in:
asarfaty 2020-04-05 09:23:49 +02:00 committed by Adit Sarfaty
parent 9efc77c3be
commit 36e69f0717
3 changed files with 59 additions and 3 deletions

View File

@ -16,6 +16,7 @@
import mock
from vmware_nsxlib.tests.unit.v3 import nsxlib_testcase
from vmware_nsxlib.tests.unit.v3.policy import test_resources
from vmware_nsxlib.v3 import exceptions as nsxlib_exc
from vmware_nsxlib.v3.policy import constants
@ -1218,6 +1219,60 @@ class TestPolicyLBPoolApi(test_resources.NsxPolicyLibTestCase):
self.assert_called_with_def(api_call, expected_def)
self.assertIsNotNone(result)
def test_create_with_retry_stale_revision(self):
name = 'd1'
description = 'desc'
obj_id = '111'
members = [
lb_defs.LBPoolMemberDef(ip_address='10.0.0.1')]
algorithm = 'algo'
active_monitor_paths = 'path1'
member_group = 'group1'
snat_translation = False
with mock.patch.object(self.policy_api, "create_or_update",
side_effect=nsxlib_exc.StaleRevision
) as api_call:
with self.assertRaises(nsxlib_exc.StaleRevision):
self.resourceApi.create_or_overwrite(
name,
lb_pool_id=obj_id,
description=description,
members=members,
active_monitor_paths=active_monitor_paths,
algorithm=algorithm,
member_group=member_group,
snat_translation=snat_translation,
tenant=TEST_TENANT)
self.assertEqual(nsxlib_testcase.NSX_MAX_ATTEMPTS,
api_call.call_count)
def test_create_with_retry_pending_delete(self):
name = 'd1'
description = 'desc'
obj_id = '111'
members = [
lb_defs.LBPoolMemberDef(ip_address='10.0.0.1')]
algorithm = 'algo'
active_monitor_paths = 'path1'
member_group = 'group1'
snat_translation = False
with mock.patch.object(self.policy_api, "create_or_update",
side_effect=nsxlib_exc.NsxPendingDelete
) as api_call:
with self.assertRaises(nsxlib_exc.NsxPendingDelete):
self.resourceApi.create_or_overwrite(
name,
lb_pool_id=obj_id,
description=description,
members=members,
active_monitor_paths=active_monitor_paths,
algorithm=algorithm,
member_group=member_group,
snat_translation=snat_translation,
tenant=TEST_TENANT)
self.assertEqual(nsxlib_testcase.NSX_MAX_ATTEMPTS,
api_call.call_count)
def test_delete(self):
obj_id = '111'
with mock.patch.object(self.policy_api, "delete") as api_call:

View File

@ -358,10 +358,10 @@ class NsxPolicyResourceBase(object):
transaction.store_def(child_def, self.policy_api.client)
else:
# No transaction - apply now
# in case the same object was just deleted, create may need to
# be retried
# In case the same object was just deleted, or depends on another
# resource, create may need to be retried.
@utils.retry_upon_exception(
exceptions.NsxPendingDelete,
(exceptions.NsxPendingDelete, exceptions.StaleRevision),
max_attempts=self.policy_api.client.max_attempts)
def _do_create_with_retry():
if child_def:

View File

@ -168,6 +168,7 @@ def _log_after_retry(retry_state):
def retry_upon_exception(exc, delay=0.5, max_delay=2,
max_attempts=DEFAULT_MAX_ATTEMPTS):
# exc can be a single exception or a tuple of exceptions
return tenacity.retry(reraise=True,
retry=tenacity.retry_if_exception_type(exc),
wait=tenacity.wait_exponential(