Do not wait in _get_realized_id_using_search upon ERROR

Using the search api to get the realized id will take the maximum number of
attempts in case the policy object is in realization ERROR.
To avoid waiting so long, every once in a while (currently after 3 attempts)
the realization api will be checked for errors.

Change-Id: I6c32907eb3e91ecd251475fd7459bcdde7e9bef0
This commit is contained in:
Adit Sarfaty 2019-09-01 10:15:19 +03:00
parent 54393a235c
commit 289be2fc06
1 changed files with 30 additions and 16 deletions

View File

@ -274,7 +274,7 @@ class NsxPolicyResourceBase(object):
@check_allowed_passthrough
def _get_realized_id_using_search(self, policy_resource_path,
mp_resource_type,
mp_resource_type, resource_def=None,
sleep=None, max_attempts=None):
"""Wait until the policy path will be found using search api
@ -284,26 +284,38 @@ class NsxPolicyResourceBase(object):
sleep = self.nsxlib_config.realization_wait_sec
if max_attempts is None:
max_attempts = self.nsxlib_config.realization_max_attempts
check_status = 3
tag = [{'scope': 'policyPath',
'tag': utils.escape_tag_data(policy_resource_path)}]
@utils.retry_upon_none_result(max_attempts, delay=sleep, random=True)
def get_info():
test_num = 0
while test_num < max_attempts:
# Use the search api to find the realization id of this entity.
resources = self.nsx_api.search_by_tags(
tags=tag, resource_type=mp_resource_type)['results']
if resources:
return resources[0]['id']
try:
return get_info()
except Exception:
# max retries reached
raise exceptions.RealizationTimeoutError(
resource_type=mp_resource_type,
resource_id=policy_resource_path,
attempts=max_attempts,
sleep=sleep)
# From time to time also check the Policy realization state,
# as if it is in ERROR waiting should be avoided.
if resource_def and test_num % check_status == (check_status - 1):
info = self._get_realization_info(resource_def)
if info and info['state'] == constants.STATE_ERROR:
error_msg = self._get_realization_error_message(info)
raise exceptions.RealizationErrorStateError(
resource_type=resource_def.resource_type(),
resource_id=resource_def.get_id(),
error=error_msg)
eventlet.sleep(sleep)
test_num += 1
# max retries reached
raise exceptions.RealizationTimeoutError(
resource_type=mp_resource_type,
resource_id=policy_resource_path,
attempts=max_attempts,
sleep=sleep)
def _get_extended_attr_from_realized_info(self, realization_info,
requested_attr):
@ -1193,12 +1205,13 @@ class NsxPolicyTier1Api(NsxPolicyResourceBase):
def get_realized_id(self, tier1_id, entity_type=None,
tenant=constants.POLICY_INFRA_TENANT,
realization_info=None):
tier1_def = self.entry_def(tier1_id=tier1_id, tenant=tenant)
if self.nsx_api:
# Use MP search api to find the LR ID as it is faster
return self._get_realized_id_using_search(
self.get_path(tier1_id, tenant=tenant),
self.nsx_api.logical_router.resource_type)
tier1_def = self.entry_def(tier1_id=tier1_id, tenant=tenant)
self.nsx_api.logical_router.resource_type,
resource_def=tier1_id)
return self._get_realized_id(tier1_def, entity_type=entity_type,
realization_info=realization_info)
@ -1919,13 +1932,14 @@ class NsxPolicySegmentApi(NsxPolicyResourceBase):
def get_realized_logical_switch_id(self, segment_id,
tenant=constants.POLICY_INFRA_TENANT):
segment_def = self.entry_def(segment_id=segment_id, tenant=tenant)
if self.nsx_api:
# Use MP search api to find the LS ID as it is faster
return self._get_realized_id_using_search(
self.get_path(segment_id, tenant=tenant),
self.nsx_api.logical_switch.resource_type)
self.nsx_api.logical_switch.resource_type,
resource_def=segment_def)
segment_def = self.entry_def(segment_id=segment_id, tenant=tenant)
realization_info = self._wait_until_realized(
segment_def, entity_type='RealizedLogicalSwitch')
return self._get_realized_id(segment_def,