Commit 557838c1 authored by René Nussbaumer's avatar René Nussbaumer
Browse files

Move locking.RunningTimeout to utils



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: default avatarRené Nussbaumer <rn@google.com>
Reviewed-by: default avatarMichael Hanselmann <hansmi@google.com>
parent 56956bcb
......@@ -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]
......
......@@ -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:
......
......@@ -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:
......
......@@ -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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment