From 557838c1c5d0cba6950cc24bc3e8986630b149c4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ren=C3=A9=20Nussbaumer?= Date: Tue, 9 Nov 2010 13:43:30 +0100 Subject: [PATCH] Move locking.RunningTimeout to utils MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit As we need this functionality in other places than just locking it makes sense to move it to utils rather than keeping it in locking Signed-off-by: René Nussbaumer Reviewed-by: Michael Hanselmann --- daemons/import-export | 12 +++++----- lib/locking.py | 58 ++----------------------------------------------- lib/mcpu.py | 3 ++- lib/utils.py | 53 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 64 deletions(-) diff --git a/daemons/import-export b/daemons/import-export index ce9c3bd..d137b57 100755 --- a/daemons/import-export +++ b/daemons/import-export @@ -43,7 +43,6 @@ from ganeti import utils from ganeti import errors from ganeti import serializer from ganeti import objects -from ganeti import locking from ganeti import impexpd from ganeti import netutils @@ -256,7 +255,7 @@ def ProcessChildIO(child, socat_stderr_read_fd, dd_stderr_read_fd, poller.register(fd, select.POLLIN) if options.connect_timeout and mode == constants.IEM_IMPORT: - listen_timeout = locking.RunningTimeout(options.connect_timeout, True) + listen_timeout = utils.RunningTimeout(options.connect_timeout, True) else: listen_timeout = None @@ -277,7 +276,7 @@ def ProcessChildIO(child, socat_stderr_read_fd, dd_stderr_read_fd, logging.info("Child process didn't establish connection in time") child.Kill(signal.SIGTERM) exit_timeout = \ - locking.RunningTimeout(CHILD_LINGER_TIMEOUT, True) + utils.RunningTimeout(CHILD_LINGER_TIMEOUT, True) # Next block will calculate timeout else: # Not yet connected, check again in a second @@ -293,11 +292,10 @@ def ProcessChildIO(child, socat_stderr_read_fd, dd_stderr_read_fd, notify_status = child_io_proc.NotifyDd() if notify_status: # Schedule next notification - dd_stats_timeout = locking.RunningTimeout(DD_STATISTICS_INTERVAL, - True) + dd_stats_timeout = utils.RunningTimeout(DD_STATISTICS_INTERVAL, True) else: # Try again soon (dd isn't ready yet) - dd_stats_timeout = locking.RunningTimeout(1.0, True) + dd_stats_timeout = utils.RunningTimeout(1.0, True) if dd_stats_timeout: dd_timeout = max(0, dd_stats_timeout.Remaining() * 1000) @@ -327,7 +325,7 @@ def ProcessChildIO(child, socat_stderr_read_fd, dd_stderr_read_fd, logging.info("Giving child process %0.2f seconds to exit", CHILD_LINGER_TIMEOUT) exit_timeout = \ - locking.RunningTimeout(CHILD_LINGER_TIMEOUT, True) + utils.RunningTimeout(CHILD_LINGER_TIMEOUT, True) else: poller.unregister(fd) del fdmap[fd] diff --git a/lib/locking.py b/lib/locking.py index 934638f..140d834 100644 --- a/lib/locking.py +++ b/lib/locking.py @@ -28,7 +28,6 @@ import os import select import threading -import time import errno import weakref import logging @@ -73,59 +72,6 @@ def ssynchronized(mylock, shared=0): return wrap -class RunningTimeout(object): - """Class to calculate remaining timeout when doing several operations. - - """ - __slots__ = [ - "_allow_negative", - "_start_time", - "_time_fn", - "_timeout", - ] - - def __init__(self, timeout, allow_negative, _time_fn=time.time): - """Initializes this class. - - @type timeout: float - @param timeout: Timeout duration - @type allow_negative: bool - @param allow_negative: Whether to return values below zero - @param _time_fn: Time function for unittests - - """ - object.__init__(self) - - if timeout is not None and timeout < 0.0: - raise ValueError("Timeout must not be negative") - - self._timeout = timeout - self._allow_negative = allow_negative - self._time_fn = _time_fn - - self._start_time = None - - def Remaining(self): - """Returns the remaining timeout. - - """ - if self._timeout is None: - return None - - # Get start time on first calculation - if self._start_time is None: - self._start_time = self._time_fn() - - # Calculate remaining time - remaining_timeout = self._start_time + self._timeout - self._time_fn() - - if not self._allow_negative: - # Ensure timeout is always >= 0 - return max(0.0, remaining_timeout) - - return remaining_timeout - - class _SingleNotifyPipeConditionWaiter(object): """Helper class for SingleNotifyPipeCondition @@ -155,7 +101,7 @@ class _SingleNotifyPipeConditionWaiter(object): @param timeout: Timeout for waiting (can be None) """ - running_timeout = RunningTimeout(timeout, True) + running_timeout = utils.RunningTimeout(timeout, True) while True: remaining_time = running_timeout.Remaining() @@ -1033,7 +979,7 @@ class LockSet: # We need to keep track of how long we spent waiting for a lock. The # timeout passed to this function is over all lock acquires. - running_timeout = RunningTimeout(timeout, False) + running_timeout = utils.RunningTimeout(timeout, False) try: if names is not None: diff --git a/lib/mcpu.py b/lib/mcpu.py index 0cddbc6..277c040 100644 --- a/lib/mcpu.py +++ b/lib/mcpu.py @@ -38,6 +38,7 @@ from ganeti import errors from ganeti import rpc from ganeti import cmdlib from ganeti import locking +from ganeti import utils class LockAcquireTimeout(Exception): @@ -368,7 +369,7 @@ class Processor(object): if timeout is None: calc_timeout = lambda: None else: - calc_timeout = locking.RunningTimeout(timeout, False).Remaining + calc_timeout = utils.RunningTimeout(timeout, False).Remaining self._cbs = cbs try: diff --git a/lib/utils.py b/lib/utils.py index bdd8610..7e4d968 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -3909,3 +3909,56 @@ class FieldSet(object): """ return [val for val in items if not self.Matches(val)] + + +class RunningTimeout(object): + """Class to calculate remaining timeout when doing several operations. + + """ + __slots__ = [ + "_allow_negative", + "_start_time", + "_time_fn", + "_timeout", + ] + + def __init__(self, timeout, allow_negative, _time_fn=time.time): + """Initializes this class. + + @type timeout: float + @param timeout: Timeout duration + @type allow_negative: bool + @param allow_negative: Whether to return values below zero + @param _time_fn: Time function for unittests + + """ + object.__init__(self) + + if timeout is not None and timeout < 0.0: + raise ValueError("Timeout must not be negative") + + self._timeout = timeout + self._allow_negative = allow_negative + self._time_fn = _time_fn + + self._start_time = None + + def Remaining(self): + """Returns the remaining timeout. + + """ + if self._timeout is None: + return None + + # Get start time on first calculation + if self._start_time is None: + self._start_time = self._time_fn() + + # Calculate remaining time + remaining_timeout = self._start_time + self._timeout - self._time_fn() + + if not self._allow_negative: + # Ensure timeout is always >= 0 + return max(0.0, remaining_timeout) + + return remaining_timeout -- 1.7.10.4