Merge "Remove oslo.utils dependency"

This commit is contained in:
Jenkins 2017-05-19 14:57:54 +00:00 committed by Gerrit Code Review
commit 176c1332f6
10 changed files with 151 additions and 46 deletions

View File

@ -16,7 +16,6 @@
import datetime
import numpy
from oslo_utils import timeutils
import pandas
import six
@ -80,8 +79,8 @@ class MovingAverage(aggregates.CustomAggregator):
msec = datetime.timedelta(milliseconds=1)
zero = datetime.timedelta(seconds=0)
half_span = datetime.timedelta(seconds=window / 2)
start = timeutils.normalize_time(data.index[0])
stop = timeutils.normalize_time(
start = utils.normalize_time(data.index[0])
stop = utils.normalize_time(
data.index[-1] + datetime.timedelta(seconds=min_grain))
# min_grain addition necessary since each bin of rolled-up data
# is indexed by leftmost timestamp of bin.
@ -90,7 +89,7 @@ class MovingAverage(aggregates.CustomAggregator):
right = 2 * half_span - left - msec
# msec subtraction is so we don't include right endpoint in slice.
x = timeutils.normalize_time(x)
x = utils.normalize_time(x)
if x - left >= start and x + right <= stop:
dslice = data[x - left: x + right]

View File

@ -22,7 +22,6 @@ from cotyledon import oslo_config_glue
from futurist import periodics
from oslo_config import cfg
from oslo_log import log
from oslo_utils import timeutils
import six
import tenacity
import tooz
@ -120,10 +119,9 @@ class MetricProcessBase(cotyledon.Service):
time.sleep(self.startup_delay)
while not self._shutdown.is_set():
with timeutils.StopWatch() as timer:
with utils.StopWatch() as timer:
self._run_job()
self._shutdown.wait(max(0, self.interval_delay -
timer.elapsed()))
self._shutdown.wait(max(0, self.interval_delay - timer.elapsed()))
self._shutdown_done.set()
def terminate(self):

View File

@ -21,7 +21,6 @@ import decimal
import iso8601
from oslo_db.sqlalchemy import models
from oslo_utils import timeutils
import six
import sqlalchemy
from sqlalchemy.dialects import mysql
@ -91,7 +90,7 @@ class PreciseTimestamp(types.TypeDecorator):
def process_bind_param(self, value, dialect):
if value is not None:
value = timeutils.normalize_time(value)
value = utils.normalize_time(value)
if dialect.name == 'mysql':
return self._dt_to_decimal(value)
return value
@ -100,7 +99,7 @@ class PreciseTimestamp(types.TypeDecorator):
if dialect.name == 'mysql':
value = self._decimal_to_dt(value)
if value is not None:
return timeutils.normalize_time(value).replace(
return utils.normalize_time(value).replace(
tzinfo=iso8601.iso8601.UTC)
@ -116,7 +115,7 @@ class TimestampUTC(types.TypeDecorator):
def process_bind_param(self, value, dialect):
if value is not None:
return timeutils.normalize_time(value)
return utils.normalize_time(value)
def process_result_value(self, value, dialect):
if value is not None:

View File

@ -23,7 +23,6 @@ from concurrent import futures
import iso8601
from oslo_config import cfg
from oslo_log import log
from oslo_utils import timeutils
import six
import six.moves
@ -91,17 +90,17 @@ class CarbonaraBasedStorage(storage.StorageDriver):
be retrieved, returns None.
"""
with timeutils.StopWatch() as sw:
with utils.StopWatch() as sw:
raw_measures = (
self._get_unaggregated_timeserie(
metric)
)
if not raw_measures:
return
LOG.debug(
"Retrieve unaggregated measures "
"for %s in %.2fs",
metric.id, sw.elapsed())
if not raw_measures:
return
LOG.debug(
"Retrieve unaggregated measures "
"for %s in %.2fs",
metric.id, sw.elapsed())
try:
return carbonara.BoundTimeSerie.unserialize(
raw_measures, block_size, back_window)
@ -447,23 +446,23 @@ class CarbonaraBasedStorage(storage.StorageDriver):
new_first_block_timestamp)
for aggregation in agg_methods))
with timeutils.StopWatch() as sw:
with utils.StopWatch() as sw:
ts.set_values(measures,
before_truncate_callback=_map_add_measures,
ignore_too_old_timestamps=True)
elapsed = sw.elapsed()
number_of_operations = (len(agg_methods) * len(definition))
perf = ""
if elapsed > 0:
perf = " (%d points/s, %d measures/s)" % (
((number_of_operations * computed_points['number']) /
elapsed),
((number_of_operations * len(measures)) / elapsed)
)
LOG.debug("Computed new metric %s with %d new measures "
"in %.2f seconds%s",
metric.id, len(measures), elapsed, perf)
number_of_operations = (len(agg_methods) * len(definition))
perf = ""
elapsed = sw.elapsed()
if elapsed > 0:
perf = " (%d points/s, %d measures/s)" % (
((number_of_operations * computed_points['number']) /
elapsed),
((number_of_operations * len(measures)) / elapsed)
)
LOG.debug("Computed new metric %s with %d new measures "
"in %.2f seconds%s",
metric.id, len(measures), elapsed, perf)
self._store_unaggregated_timeserie(metric, ts.serialize())

View File

@ -18,7 +18,7 @@ import functools
import math
import fixtures
from oslo_utils import timeutils
import iso8601
from oslotest import base
import pandas
import six
@ -138,13 +138,13 @@ class TestAggregatedTimeSerie(base.BaseTestCase):
[(datetime.datetime(2014, 1, 1, 12, 0, 4), 1, 5),
(datetime.datetime(2014, 1, 1, 12, 0, 9), 1, 6)],
ts.fetch(
from_timestamp=timeutils.parse_isotime(
from_timestamp=iso8601.parse_date(
"2014-01-01 12:00:04")))
self.assertEqual(
[(datetime.datetime(2014, 1, 1, 12, 0, 4), 1, 5),
(datetime.datetime(2014, 1, 1, 12, 0, 9), 1, 6)],
ts.fetch(
from_timestamp=timeutils.parse_isotime(
from_timestamp=iso8601.parse_date(
"2014-01-01 13:00:04+01:00")))
def test_before_epoch(self):

View File

@ -23,9 +23,9 @@ import hashlib
import json
import uuid
import iso8601
from keystonemiddleware import fixture as ksm_fixture
import mock
from oslo_utils import timeutils
import six
from stevedore import extension
import testscenarios
@ -417,7 +417,7 @@ class MetricTest(RestTest):
self.app.get("/v1/metric/%s/measures" % metric['id'],
status=403)
@mock.patch.object(timeutils, 'utcnow')
@mock.patch.object(utils, 'utcnow')
def test_get_measure_start_relative(self, utcnow):
"""Make sure the timestamps can be relative to now."""
utcnow.return_value = datetime.datetime(2014, 1, 1, 10, 23)
@ -732,7 +732,7 @@ class ResourceTest(RestTest):
@staticmethod
def _strtime_to_httpdate(dt):
return email_utils.formatdate(calendar.timegm(
timeutils.parse_isotime(dt).timetuple()), usegmt=True)
iso8601.parse_date(dt).timetuple()), usegmt=True)
def _check_etag(self, response, resource):
lastmodified = self._strtime_to_httpdate(resource['revision_start'])

View File

@ -16,8 +16,8 @@
import datetime
import uuid
import iso8601
import mock
from oslo_utils import timeutils
from oslotest import base
import six.moves
@ -649,7 +649,7 @@ class TestStorageDriver(tests_base.TestCase):
(utils.datetime_utc(2014, 1, 1, 12), 300.0, 69.0),
], self.storage.get_measures(
self.metric,
from_timestamp=timeutils.parse_isotime("2014-1-1 13:00:00+01:00"),
from_timestamp=iso8601.parse_date("2014-1-1 13:00:00+01:00"),
to_timestamp=datetime.datetime(2014, 1, 1, 12, 0, 2)))
self.assertEqual([

View File

@ -76,3 +76,30 @@ class TestResourceUUID(tests_base.TestCase):
self.assertEqual(
uuid.UUID('853e5c64-f45e-58b2-999c-96df856fbe3d'),
utils.ResourceUUID("foo", ""))
class StopWatchTest(tests_base.TestCase):
def test_no_states(self):
watch = utils.StopWatch()
self.assertRaises(RuntimeError, watch.stop)
def test_start_stop(self):
watch = utils.StopWatch()
watch.start()
watch.stop()
def test_no_elapsed(self):
watch = utils.StopWatch()
self.assertRaises(RuntimeError, watch.elapsed)
def test_elapsed(self):
watch = utils.StopWatch()
watch.start()
watch.stop()
elapsed = watch.elapsed()
self.assertAlmostEqual(elapsed, watch.elapsed())
def test_context_manager(self):
with utils.StopWatch() as watch:
pass
self.assertGreater(watch.elapsed(), 0)

View File

@ -24,15 +24,14 @@ import os
import uuid
import iso8601
import monotonic
import numpy
from oslo_log import log
from oslo_utils import timeutils
import pandas as pd
import six
import tenacity
from tooz import coordination
LOG = log.getLogger(__name__)
# uuid5 namespace for id transformation.
@ -160,8 +159,16 @@ def to_timespan(value):
def utcnow():
"""Better version of utcnow() that returns utcnow with a correct TZ."""
return timeutils.utcnow(True)
"""Version of utcnow() that returns utcnow with a correct TZ."""
return datetime.datetime.now(tz=iso8601.iso8601.UTC)
def normalize_time(timestamp):
"""Normalize time in arbitrary timezone to UTC naive object."""
offset = timestamp.utcoffset()
if offset is None:
return timestamp
return timestamp.replace(tzinfo=None) - offset
def datetime_utc(*args):
@ -214,3 +221,79 @@ def strtobool(v):
if isinstance(v, bool):
return v
return bool(distutils.util.strtobool(v))
class StopWatch(object):
"""A simple timer/stopwatch helper class.
Inspired by: apache-commons-lang java stopwatch.
Not thread-safe (when a single watch is mutated by multiple threads at
the same time). Thread-safe when used by a single thread (not shared) or
when operations are performed in a thread-safe manner on these objects by
wrapping those operations with locks.
It will use the `monotonic`_ pypi library to find an appropriate
monotonically increasing time providing function (which typically varies
depending on operating system and python version).
.. _monotonic: https://pypi.python.org/pypi/monotonic/
"""
_STARTED = object()
_STOPPED = object()
def __init__(self):
self._started_at = None
self._stopped_at = None
self._state = None
def start(self):
"""Starts the watch (if not already started).
NOTE(harlowja): resets any splits previously captured (if any).
"""
if self._state == self._STARTED:
return self
self._started_at = monotonic.monotonic()
self._state = self._STARTED
return self
@staticmethod
def _delta_seconds(earlier, later):
# Uses max to avoid the delta/time going backwards (and thus negative).
return max(0.0, later - earlier)
def elapsed(self):
"""Returns how many seconds have elapsed."""
if self._state not in (self._STARTED, self._STOPPED):
raise RuntimeError("Can not get the elapsed time of a stopwatch"
" if it has not been started/stopped")
if self._state == self._STOPPED:
elapsed = self._delta_seconds(self._started_at, self._stopped_at)
else:
elapsed = self._delta_seconds(
self._started_at, monotonic.monotonic())
return elapsed
def __enter__(self):
"""Starts the watch."""
self.start()
return self
def __exit__(self, type, value, traceback):
"""Stops the watch (ignoring errors if stop fails)."""
try:
self.stop()
except RuntimeError:
pass
def stop(self):
"""Stops the watch."""
if self._state == self._STOPPED:
return self
if self._state != self._STARTED:
raise RuntimeError("Can not stop a stopwatch that has not been"
" started")
self._stopped_at = monotonic.monotonic()
self._state = self._STOPPED
return self

View File

@ -4,7 +4,6 @@ iso8601
oslo.config>=3.22.0
oslo.log>=2.3.0
oslo.policy>=0.3.0
oslo.utils>=3.18.0
oslo.middleware>=3.22.0
pandas>=0.18.0
scipy>=0.18.1 # BSD
@ -22,3 +21,4 @@ tenacity>=3.1.0 # Apache-2.0
WebOb>=1.4.1
Paste
PasteDeploy
monotonic