Use the oslo_utils.timeutils 'StopWatch' class

Instead of using time.time() to keep track of durations
the stop watch class provides this same functionality in a
nicely made helper class. It also internally uses monotonically
increasing time so that drifts/going backwards should not be
possible so it seems like a nice change to just use it vs computing
similar information.

Change-Id: Ic8906f42f6997412fffe950d4833dc2ad88ecf28
This commit is contained in:
Joshua Harlow 2015-08-10 16:23:01 -07:00
parent 1d82c93652
commit 8821f42b20
2 changed files with 9 additions and 8 deletions

View File

@ -95,11 +95,11 @@ raised in the background thread.):
"""
import functools
import time
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import excutils
from oslo_utils import timeutils
import retrying
from ironic.common import driver_factory
@ -193,6 +193,7 @@ class TaskManager(object):
self.fsm = states.machine.copy()
self._purpose = purpose
self._debug_timer = timeutils.StopWatch()
try:
LOG.debug("Attempting to get %(type)s lock on node %(node)s (for "
@ -202,7 +203,7 @@ class TaskManager(object):
if not self.shared:
self._lock()
else:
self._debug_timer = time.time()
self._debug_timer.restart()
self.node = objects.Node.get(context, node_id)
self.ports = objects.Port.list_by_node_id(context, self.node.id)
self.driver = driver_factory.get_driver(driver_name or
@ -221,7 +222,7 @@ class TaskManager(object):
self.release_resources()
def _lock(self):
self._debug_timer = time.time()
self._debug_timer.restart()
# NodeLocked exceptions can be annoying. Let's try to alleviate
# some of that pain by retrying our lock attempts. The retrying
@ -236,8 +237,8 @@ class TaskManager(object):
LOG.debug("Node %(node)s successfully reserved for %(purpose)s "
"(took %(time).2f seconds)",
{'node': self.node_id, 'purpose': self._purpose,
'time': time.time() - self._debug_timer})
self._debug_timer = time.time()
'time': self._debug_timer.elapsed()})
self._debug_timer.restart()
reserve_node()
@ -252,7 +253,7 @@ class TaskManager(object):
'to an exclusive one (shared lock was held %(time).2f '
'seconds)',
{'uuid': self.node.uuid, 'purpose': self._purpose,
'time': time.time() - self._debug_timer})
'time': self._debug_timer.elapsed()})
self._lock()
self.shared = False
@ -308,7 +309,7 @@ class TaskManager(object):
"on node %(node)s (lock was held %(time).2f sec)",
{'type': 'shared' if self.shared else 'exclusive',
'purpose': self._purpose, 'node': self.node.uuid,
'time': time.time() - self._debug_timer})
'time': self._debug_timer.elapsed()})
self.node = None
self.driver = None
self.ports = None

View File

@ -481,7 +481,7 @@ class TaskManagerStateModelTestCases(tests_base.TestCase):
t.ports = mock.Mock()
t.shared = True
t._purpose = 'purpose'
t._debug_timer = 3.14
t._debug_timer = mock.Mock()
t.release_resources(t)
self.assertIsNone(t.node)