Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Use monotonic clock where possible for metrics #5498

Merged
merged 1 commit into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/5498.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix intermittent exceptions on Apple hardware. Also fix bug that caused database activity times to be under-reported in log lines.
20 changes: 14 additions & 6 deletions synapse/storage/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
from synapse.util.logcontext import LoggingContext, PreserveLoggingContext
from synapse.util.stringutils import exception_to_unicode

# import a function which will return a monotonic time, in seconds
try:
# on python 3, use time.monotonic, since time.clock can go backwards
from time import monotonic as monotonic_time
except ImportError:
# ... but python 2 doesn't have it
from time import clock as monotonic_time

logger = logging.getLogger(__name__)

try:
Expand Down Expand Up @@ -352,14 +360,14 @@ def set_expiration_date_for_user_txn(self, txn, user_id, use_delta=False):
)

def start_profiling(self):
self._previous_loop_ts = self._clock.time_msec()
self._previous_loop_ts = monotonic_time()

def loop():
curr = self._current_txn_total_time
prev = self._previous_txn_total_time
self._previous_txn_total_time = curr

time_now = self._clock.time_msec()
time_now = monotonic_time()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so yes. this function turned out to be passing milliseconds into a function that expected seconds.

time_then = self._previous_loop_ts
self._previous_loop_ts = time_now

Expand All @@ -385,7 +393,7 @@ def loop():
def _new_transaction(
self, conn, desc, after_callbacks, exception_callbacks, func, *args, **kwargs
):
start = time.time()
start = monotonic_time()
txn_id = self._TXN_ID

# We don't really need these to be unique, so lets stop it from
Expand Down Expand Up @@ -451,7 +459,7 @@ def _new_transaction(
logger.debug("[TXN FAIL] {%s} %s", name, e)
raise
finally:
end = time.time()
end = monotonic_time()
duration = end - start

LoggingContext.current_context().add_database_transaction(duration)
Expand Down Expand Up @@ -525,11 +533,11 @@ def runWithConnection(self, func, *args, **kwargs):
)
parent_context = None

start_time = time.time()
start_time = monotonic_time()

def inner_func(conn, *args, **kwargs):
with LoggingContext("runWithConnection", parent_context) as context:
sched_duration_sec = time.time() - start_time
sched_duration_sec = monotonic_time() - start_time
sql_scheduling_timer.observe(sched_duration_sec)
context.add_database_scheduled(sched_duration_sec)

Expand Down