Revision 557838c1

b/daemons/import-export
43 43
from ganeti import errors
44 44
from ganeti import serializer
45 45
from ganeti import objects
46
from ganeti import locking
47 46
from ganeti import impexpd
48 47
from ganeti import netutils
49 48

  
......
256 255
      poller.register(fd, select.POLLIN)
257 256

  
258 257
    if options.connect_timeout and mode == constants.IEM_IMPORT:
259
      listen_timeout = locking.RunningTimeout(options.connect_timeout, True)
258
      listen_timeout = utils.RunningTimeout(options.connect_timeout, True)
260 259
    else:
261 260
      listen_timeout = None
262 261

  
......
277 276
          logging.info("Child process didn't establish connection in time")
278 277
          child.Kill(signal.SIGTERM)
279 278
          exit_timeout = \
280
            locking.RunningTimeout(CHILD_LINGER_TIMEOUT, True)
279
            utils.RunningTimeout(CHILD_LINGER_TIMEOUT, True)
281 280
          # Next block will calculate timeout
282 281
        else:
283 282
          # Not yet connected, check again in a second
......
293 292
        notify_status = child_io_proc.NotifyDd()
294 293
        if notify_status:
295 294
          # Schedule next notification
296
          dd_stats_timeout = locking.RunningTimeout(DD_STATISTICS_INTERVAL,
297
                                                    True)
295
          dd_stats_timeout = utils.RunningTimeout(DD_STATISTICS_INTERVAL, True)
298 296
        else:
299 297
          # Try again soon (dd isn't ready yet)
300
          dd_stats_timeout = locking.RunningTimeout(1.0, True)
298
          dd_stats_timeout = utils.RunningTimeout(1.0, True)
301 299

  
302 300
      if dd_stats_timeout:
303 301
        dd_timeout = max(0, dd_stats_timeout.Remaining() * 1000)
......
327 325
                  logging.info("Giving child process %0.2f seconds to exit",
328 326
                               CHILD_LINGER_TIMEOUT)
329 327
                  exit_timeout = \
330
                    locking.RunningTimeout(CHILD_LINGER_TIMEOUT, True)
328
                    utils.RunningTimeout(CHILD_LINGER_TIMEOUT, True)
331 329
          else:
332 330
            poller.unregister(fd)
333 331
            del fdmap[fd]
b/lib/locking.py
28 28
import os
29 29
import select
30 30
import threading
31
import time
32 31
import errno
33 32
import weakref
34 33
import logging
......
73 72
  return wrap
74 73

  
75 74

  
76
class RunningTimeout(object):
77
  """Class to calculate remaining timeout when doing several operations.
78

  
79
  """
80
  __slots__ = [
81
    "_allow_negative",
82
    "_start_time",
83
    "_time_fn",
84
    "_timeout",
85
    ]
86

  
87
  def __init__(self, timeout, allow_negative, _time_fn=time.time):
88
    """Initializes this class.
89

  
90
    @type timeout: float
91
    @param timeout: Timeout duration
92
    @type allow_negative: bool
93
    @param allow_negative: Whether to return values below zero
94
    @param _time_fn: Time function for unittests
95

  
96
    """
97
    object.__init__(self)
98

  
99
    if timeout is not None and timeout < 0.0:
100
      raise ValueError("Timeout must not be negative")
101

  
102
    self._timeout = timeout
103
    self._allow_negative = allow_negative
104
    self._time_fn = _time_fn
105

  
106
    self._start_time = None
107

  
108
  def Remaining(self):
109
    """Returns the remaining timeout.
110

  
111
    """
112
    if self._timeout is None:
113
      return None
114

  
115
    # Get start time on first calculation
116
    if self._start_time is None:
117
      self._start_time = self._time_fn()
118

  
119
    # Calculate remaining time
120
    remaining_timeout = self._start_time + self._timeout - self._time_fn()
121

  
122
    if not self._allow_negative:
123
      # Ensure timeout is always >= 0
124
      return max(0.0, remaining_timeout)
125

  
126
    return remaining_timeout
127

  
128

  
129 75
class _SingleNotifyPipeConditionWaiter(object):
130 76
  """Helper class for SingleNotifyPipeCondition
131 77

  
......
155 101
    @param timeout: Timeout for waiting (can be None)
156 102

  
157 103
    """
158
    running_timeout = RunningTimeout(timeout, True)
104
    running_timeout = utils.RunningTimeout(timeout, True)
159 105

  
160 106
    while True:
161 107
      remaining_time = running_timeout.Remaining()
......
1033 979

  
1034 980
    # We need to keep track of how long we spent waiting for a lock. The
1035 981
    # timeout passed to this function is over all lock acquires.
1036
    running_timeout = RunningTimeout(timeout, False)
982
    running_timeout = utils.RunningTimeout(timeout, False)
1037 983

  
1038 984
    try:
1039 985
      if names is not None:
b/lib/mcpu.py
38 38
from ganeti import rpc
39 39
from ganeti import cmdlib
40 40
from ganeti import locking
41
from ganeti import utils
41 42

  
42 43

  
43 44
class LockAcquireTimeout(Exception):
......
368 369
    if timeout is None:
369 370
      calc_timeout = lambda: None
370 371
    else:
371
      calc_timeout = locking.RunningTimeout(timeout, False).Remaining
372
      calc_timeout = utils.RunningTimeout(timeout, False).Remaining
372 373

  
373 374
    self._cbs = cbs
374 375
    try:
b/lib/utils.py
3909 3909

  
3910 3910
    """
3911 3911
    return [val for val in items if not self.Matches(val)]
3912

  
3913

  
3914
class RunningTimeout(object):
3915
  """Class to calculate remaining timeout when doing several operations.
3916

  
3917
  """
3918
  __slots__ = [
3919
    "_allow_negative",
3920
    "_start_time",
3921
    "_time_fn",
3922
    "_timeout",
3923
    ]
3924

  
3925
  def __init__(self, timeout, allow_negative, _time_fn=time.time):
3926
    """Initializes this class.
3927

  
3928
    @type timeout: float
3929
    @param timeout: Timeout duration
3930
    @type allow_negative: bool
3931
    @param allow_negative: Whether to return values below zero
3932
    @param _time_fn: Time function for unittests
3933

  
3934
    """
3935
    object.__init__(self)
3936

  
3937
    if timeout is not None and timeout < 0.0:
3938
      raise ValueError("Timeout must not be negative")
3939

  
3940
    self._timeout = timeout
3941
    self._allow_negative = allow_negative
3942
    self._time_fn = _time_fn
3943

  
3944
    self._start_time = None
3945

  
3946
  def Remaining(self):
3947
    """Returns the remaining timeout.
3948

  
3949
    """
3950
    if self._timeout is None:
3951
      return None
3952

  
3953
    # Get start time on first calculation
3954
    if self._start_time is None:
3955
      self._start_time = self._time_fn()
3956

  
3957
    # Calculate remaining time
3958
    remaining_timeout = self._start_time + self._timeout - self._time_fn()
3959

  
3960
    if not self._allow_negative:
3961
      # Ensure timeout is always >= 0
3962
      return max(0.0, remaining_timeout)
3963

  
3964
    return remaining_timeout

Also available in: Unified diff