|
| 1 | +# Copyright 2013-2016 DataStax, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +This module contains utilities for generating timestamps for client-side |
| 17 | +timestamp specification. |
| 18 | +""" |
| 19 | + |
| 20 | +import logging |
| 21 | +import time |
| 22 | +from threading import Lock |
| 23 | + |
| 24 | +log = logging.getLogger(__name__) |
| 25 | + |
| 26 | +class MonotonicTimestampGenerator(object): |
| 27 | + """ |
| 28 | + An object that, when called, returns ``int(time.time() * 1e6)`` when |
| 29 | + possible, but, if the value returned by ``time.time`` doesn't increase, |
| 30 | + drifts into the future and logs warnings. |
| 31 | + Exposed configuration attributes can be configured with arguments to |
| 32 | + ``__init__`` or by changing attributes on an initialized object. |
| 33 | +
|
| 34 | + .. versionadded:: 3.8.0 |
| 35 | + """ |
| 36 | + |
| 37 | + warn_on_drift = True |
| 38 | + """ |
| 39 | + If true, log warnings when timestamps drift into the future as allowed by |
| 40 | + :attr:`warning_threshold` and :attr:`warning_interval`. |
| 41 | + """ |
| 42 | + |
| 43 | + warning_threshold = 0 |
| 44 | + """ |
| 45 | + This object will only issue warnings when the returned timestamp drifts |
| 46 | + more than ``warning_threshold`` seconds into the future. |
| 47 | + """ |
| 48 | + |
| 49 | + warning_interval = 0 |
| 50 | + """ |
| 51 | + This object will only issue warnings every ``warning_interval`` seconds. |
| 52 | + """ |
| 53 | + |
| 54 | + def __init__(self, warn_on_drift=True, warning_threshold=0, warning_interval=0): |
| 55 | + self.lock = Lock() |
| 56 | + with self.lock: |
| 57 | + self.last = 0 |
| 58 | + self._last_warn = 0 |
| 59 | + self.warn_on_drift = warn_on_drift |
| 60 | + self.warning_threshold = warning_threshold |
| 61 | + self.warning_interval = warning_interval |
| 62 | + |
| 63 | + def _next_timestamp(self, now, last): |
| 64 | + """ |
| 65 | + Returns the timestamp that should be used if ``now`` is the current |
| 66 | + time and ``last`` is the last timestamp returned by this object. |
| 67 | + Intended for internal and testing use only; to generate timestamps, |
| 68 | + call an instantiated ``MonotonicTimestampGenerator`` object. |
| 69 | +
|
| 70 | + :param int now: an integer to be used as the current time, typically |
| 71 | + representing the current time in seconds since the UNIX epoch |
| 72 | + :param int last: an integer representing the last timestamp returned by |
| 73 | + this object |
| 74 | + """ |
| 75 | + if now > last: |
| 76 | + self.last = now |
| 77 | + return now |
| 78 | + else: |
| 79 | + self._maybe_warn(now=now) |
| 80 | + self.last = last + 1 |
| 81 | + return self.last |
| 82 | + |
| 83 | + def __call__(self): |
| 84 | + """ |
| 85 | + Makes ``MonotonicTimestampGenerator`` objects callable; defers |
| 86 | + internally to _next_timestamp. |
| 87 | + """ |
| 88 | + with self.lock: |
| 89 | + return self._next_timestamp(now=int(time.time() * 1e6), |
| 90 | + last=self.last) |
| 91 | + |
| 92 | + def _maybe_warn(self, now): |
| 93 | + # should be called from inside the self.lock. |
| 94 | + diff = self.last - now |
| 95 | + since_last_warn = now - self._last_warn |
| 96 | + |
| 97 | + warn = (self.warn_on_drift and |
| 98 | + (diff > self.warning_threshold * 1e6) and |
| 99 | + (since_last_warn >= self.warning_interval * 1e6)) |
| 100 | + if warn: |
| 101 | + log.warn( |
| 102 | + "Clock skew detected: current tick ({now}) was {diff} " |
| 103 | + "microseconds behind the last generated timestamp " |
| 104 | + "({last}), returned timestamps will be artificially " |
| 105 | + "incremented to guarantee monotonicity.".format( |
| 106 | + now=now, diff=diff, last=self.last)) |
| 107 | + self._last_warn = now |
0 commit comments