From 5a7fde45998e8259e287dc682d7ed77c6752e85e Mon Sep 17 00:00:00 2001 From: Idan Hefetz Date: Mon, 6 Apr 2020 06:04:49 +0000 Subject: [PATCH] Bugfix: Consistency query timestamp format is not the same as vertices Change-Id: I1d2a48874e4d2a17fd559253f86f546b10752ee9 --- .../consistency/consistency_enforcer.py | 14 +++++----- .../consistency/test_consistency.py | 27 ++++++++++--------- vitrage/utils/datetime.py | 12 +++++++++ 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/vitrage/entity_graph/consistency/consistency_enforcer.py b/vitrage/entity_graph/consistency/consistency_enforcer.py index 4fa2d5917..97bea3d1c 100644 --- a/vitrage/entity_graph/consistency/consistency_enforcer.py +++ b/vitrage/entity_graph/consistency/consistency_enforcer.py @@ -12,8 +12,6 @@ # License for the specific language governing permissions and limitations # under the License. -from datetime import timedelta - from oslo_config import cfg from oslo_log import log @@ -27,7 +25,7 @@ from vitrage.datasources import OPENSTACK_CLUSTER from vitrage.datasources import utils from vitrage.evaluator.actions.evaluator_event_transformer \ import VITRAGE_DATASOURCE -from vitrage.utils.datetime import utcnow +from vitrage.utils import datetime CONF = cfg.CONF LOG = log.getLogger(__name__) @@ -71,8 +69,8 @@ class ConsistencyEnforcer(object): LOG.exception('Error in deleting vertices from entity_graph.') def _find_outdated_entities_to_mark_as_deleted(self): - vitrage_sample_tstmp = str(utcnow() - timedelta( - seconds=2 * CONF.datasources.snapshots_interval)) + vitrage_sample_tstmp = str(datetime.datetime_delta( + -2 * CONF.datasources.snapshots_interval)) query = { 'and': [ {'!=': {VProps.VITRAGE_TYPE: VITRAGE_DATASOURCE}}, @@ -85,8 +83,8 @@ class ConsistencyEnforcer(object): return set(self._filter_vertices_to_be_marked_as_deleted(vertices)) def _find_old_deleted_entities(self): - vitrage_sample_tstmp = str(utcnow() - timedelta( - seconds=CONF.consistency.min_time_to_delete)) + vitrage_sample_tstmp = str(datetime.datetime_delta( + -1 * CONF.consistency.min_time_to_delete)) query = { 'and': [ {'==': {VProps.VITRAGE_IS_DELETED: True}}, @@ -103,7 +101,7 @@ class ConsistencyEnforcer(object): event = { DSProps.ENTITY_TYPE: CONSISTENCY_DATASOURCE, DSProps.DATASOURCE_ACTION: DatasourceAction.UPDATE, - DSProps.SAMPLE_DATE: str(utcnow()), + DSProps.SAMPLE_DATE: datetime.format_utcnow(), DSProps.EVENT_TYPE: action, VProps.VITRAGE_ID: vertex[VProps.VITRAGE_ID], VProps.ID: vertex.get(VProps.ID, None), diff --git a/vitrage/tests/functional/entity_graph/consistency/test_consistency.py b/vitrage/tests/functional/entity_graph/consistency/test_consistency.py index 2c5b617fa..d64f9fb8b 100644 --- a/vitrage/tests/functional/entity_graph/consistency/test_consistency.py +++ b/vitrage/tests/functional/entity_graph/consistency/test_consistency.py @@ -12,7 +12,6 @@ # License for the specific language governing permissions and limitations # under the License. -from datetime import timedelta import time from oslo_config import cfg @@ -31,7 +30,7 @@ from vitrage.graph.driver.networkx_graph import NXGraph from vitrage.tests.functional.base import TestFunctionalBase from vitrage.tests.functional.test_configuration import TestConfiguration from vitrage.tests.mocks import utils -from vitrage.utils.datetime import utcnow +from vitrage.utils import datetime # noinspection PyProtectedMember @@ -204,12 +203,17 @@ class TestConsistencyFunctional(TestFunctionalBase, TestConfiguration): def _periodic_process_setup_stage(self, consistency_interval): self._create_processor_with_graph(processor=self.processor) - current_time = utcnow() + current_timestamp = datetime.utcnow() + current_time = str(datetime.datetime_delta(0, current_timestamp)) + time_1_5 = str(datetime.datetime_delta( + 1 * consistency_interval, current_timestamp)) + time_2 = str(datetime.datetime_delta( + 2 * consistency_interval + 1, current_timestamp)) # set all vertices to be have timestamp that consistency won't get - self._update_timestamp(self.processor.entity_graph.get_vertices(), - current_time + - timedelta(seconds=1.5 * consistency_interval)) + self._update_timestamp( + self.processor.entity_graph.get_vertices(), + time_1_5) # check number of instances in graph instance_vertices = self.processor.entity_graph.get_vertices({ @@ -230,8 +234,7 @@ class TestConsistencyFunctional(TestFunctionalBase, TestConfiguration): # set part of the instances as deleted for i in range(6, 9): instance_vertices[i][VProps.VITRAGE_IS_DELETED] = True - instance_vertices[i][VProps.VITRAGE_SAMPLE_TIMESTAMP] = str( - current_time + timedelta(seconds=2 * consistency_interval + 1)) + instance_vertices[i][VProps.VITRAGE_SAMPLE_TIMESTAMP] = time_2 self.processor.entity_graph.update_vertex(instance_vertices[i]) self._add_resources_by_type(consistency_interval=consistency_interval, @@ -309,10 +312,10 @@ class TestConsistencyFunctional(TestFunctionalBase, TestConfiguration): # - outdated_resource with an old timestamp # - deleted_resource with an old timestamp and is_deleted==true - future_timestamp = \ - str(utcnow() + timedelta(seconds=2 * consistency_interval)) - past_timestamp = \ - str(utcnow() - timedelta(seconds=2 * consistency_interval - 1)) + future_timestamp = str(datetime.datetime_delta( + 2 * consistency_interval)) + past_timestamp = str(datetime.datetime_delta( + -2 * consistency_interval + 1)) updated_resource = create_func( v_id=resource_type + '1234', v_type=resource_type, diff --git a/vitrage/utils/datetime.py b/vitrage/utils/datetime.py index 65b560b2b..a48d0cb4d 100644 --- a/vitrage/utils/datetime.py +++ b/vitrage/utils/datetime.py @@ -52,3 +52,15 @@ def format_unix_timestamp(timestamp, date_format=TIMESTAMP_FORMAT): def format_timestamp(timestamp_str, new_format=TIMESTAMP_FORMAT): return parser.parse(timestamp_str).strftime(new_format) if timestamp_str \ else None + + +def datetime_delta(delta_seconds, + time_to_use=None, + with_timezone=True, + date_format=TIMESTAMP_FORMAT): + if time_to_use: + t = time_to_use + else: + t = utcnow(with_timezone) + + return (t + timedelta(seconds=delta_seconds)).strftime(date_format)