Bugfix: Consistency query timestamp format is not the same as vertices

Change-Id: I1d2a48874e4d2a17fd559253f86f546b10752ee9
This commit is contained in:
Idan Hefetz 2020-04-06 06:04:49 +00:00
parent 0d9be7cb4d
commit 5a7fde4599
3 changed files with 33 additions and 20 deletions

View File

@ -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),

View File

@ -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,

View File

@ -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)