Jiří Moskovčák has uploaded a new change for review. Change subject: use monotonic time when checking the timeouts ......................................................................
use monotonic time when checking the timeouts When using the system time the timers are influenced by the time changes done by user or ntp. So if the time is shifted backwards, the timers are delayed which can lead to longer downtime of HA VM running the engine. Using monotonic time makes the timers independent on the system time. Change-Id: I9794beb4082f52d29835be5b6a182ab448c248f2 Bug-Url: https://bugzilla.redhat.com/1036683 Signed-off-by: Jiri Moskovcak <jmosk...@redhat.com> --- M ovirt_hosted_engine_ha/agent/state_data.py M ovirt_hosted_engine_ha/agent/state_machine.py M ovirt_hosted_engine_ha/lib/Makefile.am A ovirt_hosted_engine_ha/lib/monotonic.py 4 files changed, 43 insertions(+), 3 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-hosted-engine-ha refs/changes/84/26384/1 diff --git a/ovirt_hosted_engine_ha/agent/state_data.py b/ovirt_hosted_engine_ha/agent/state_data.py index 1b7c915..8877c5c 100644 --- a/ovirt_hosted_engine_ha/agent/state_data.py +++ b/ovirt_hosted_engine_ha/agent/state_data.py @@ -87,7 +87,7 @@ where a,b are timestamps and f(a) and f(b) are values """ - def trapezoid(acc, point): + def trapezoid(acc, point): # point is StatsData area, time, last_load, last_time = acc cur_load = point.local["cpu-load"] cur_time = point.collect_start diff --git a/ovirt_hosted_engine_ha/agent/state_machine.py b/ovirt_hosted_engine_ha/agent/state_machine.py index f3e3567..ff1880e 100644 --- a/ovirt_hosted_engine_ha/agent/state_machine.py +++ b/ovirt_hosted_engine_ha/agent/state_machine.py @@ -7,6 +7,7 @@ import time from states import ReinitializeFSM from state_data import HostedEngineData, StatsData +from lib import monotonic class StartState(BaseState): @@ -66,7 +67,7 @@ """ # Collect stats stats = { - "collect_start": int(time.time()), + "collect_start": int(monotonic.time()), } # Do not refresh if the time has not changed @@ -76,7 +77,7 @@ stats.update(self.hosted_engine.collect_stats()) stats.update({ - "collect_finish": int(time.time()) + "collect_finish": int(monotonic.time()) }) # Convert to read only structure to prevent accidental changes diff --git a/ovirt_hosted_engine_ha/lib/Makefile.am b/ovirt_hosted_engine_ha/lib/Makefile.am index 2f73d0b..07ce89c 100644 --- a/ovirt_hosted_engine_ha/lib/Makefile.am +++ b/ovirt_hosted_engine_ha/lib/Makefile.am @@ -36,6 +36,7 @@ storage_backends_test.py \ util.py \ vds_client.py \ + monotonic.py $(NULL) SUBDIRS = \ diff --git a/ovirt_hosted_engine_ha/lib/monotonic.py b/ovirt_hosted_engine_ha/lib/monotonic.py new file mode 100644 index 0000000..42d3270 --- /dev/null +++ b/ovirt_hosted_engine_ha/lib/monotonic.py @@ -0,0 +1,38 @@ +__all__ = ["time"] + +import ctypes +import os +import logging + +logger = logging.getLogger(__name__) + +CLOCK_MONOTONIC_RAW = 4 # this means time not influenced by ntp corrections - see <linux/time.h> + + +class Timespec(ctypes.Structure): + _fields_ = [ + ('tv_sec', ctypes.c_long), + ('tv_nsec', ctypes.c_long) + ] + +_have_monotonic = False + +try: + librt = ctypes.CDLL('librt.so.1', use_errno=True) + clock_gettime = librt.clock_gettime + clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(Timespec)] + _have_monotonic = True +except OSError as ex: + logger.warning("Can't use monotonic time, timeouts will be influenced by time shifts: '{0}'".format(ex)) + + +def time(): + if _have_monotonic is False: + import time + return int(time.time()) + + t = Timespec() + if clock_gettime(CLOCK_MONOTONIC_RAW, ctypes.pointer(t)) != 0: + errno_ = ctypes.get_errno() + raise OSError(errno_, os.strerror(errno_)) + return t.tv_sec -- To view, visit http://gerrit.ovirt.org/26384 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I9794beb4082f52d29835be5b6a182ab448c248f2 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-hosted-engine-ha Gerrit-Branch: master Gerrit-Owner: Jiří Moskovčák <jmosk...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches