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

Commit

Permalink
Merge pull request #3497 from matrix-org/rav/measure_fetch_event_loop
Browse files Browse the repository at this point in the history
Add CPU metrics for _fetch_event_list
  • Loading branch information
richvdh authored Jul 10, 2018
2 parents 395fa8d + f3b3b9d commit b1fe697
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
1 change: 1 addition & 0 deletions changelog.d/3497.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add CPU metrics for _fetch_event_list
51 changes: 32 additions & 19 deletions synapse/storage/events_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,25 +222,39 @@ def _do_fetch(self, conn):
"""Takes a database connection and waits for requests for events from
the _event_fetch_list queue.
"""
event_list = []
i = 0
while True:
try:
with self._event_fetch_lock:
event_list = self._event_fetch_list
self._event_fetch_list = []

if not event_list:
single_threaded = self.database_engine.single_threaded
if single_threaded or i > EVENT_QUEUE_ITERATIONS:
self._event_fetch_ongoing -= 1
return
else:
self._event_fetch_lock.wait(EVENT_QUEUE_TIMEOUT_S)
i += 1
continue
i = 0
with self._event_fetch_lock:
event_list = self._event_fetch_list
self._event_fetch_list = []

if not event_list:
single_threaded = self.database_engine.single_threaded
if single_threaded or i > EVENT_QUEUE_ITERATIONS:
self._event_fetch_ongoing -= 1
return
else:
self._event_fetch_lock.wait(EVENT_QUEUE_TIMEOUT_S)
i += 1
continue
i = 0

self._fetch_event_list(conn, event_list)

def _fetch_event_list(self, conn, event_list):
"""Handle a load of requests from the _event_fetch_list queue
Args:
conn (twisted.enterprise.adbapi.Connection): database connection
event_list (list[Tuple[list[str], Deferred]]):
The fetch requests. Each entry consists of a list of event
ids to be fetched, and a deferred to be completed once the
events have been fetched.
"""
with Measure(self._clock, "_fetch_event_list"):
try:
event_id_lists = zip(*event_list)[0]
event_ids = [
item for sublist in event_id_lists for item in sublist
Expand Down Expand Up @@ -280,9 +294,8 @@ def fire(evs):
with PreserveLoggingContext():
d.errback(e)

if event_list:
with PreserveLoggingContext():
self.hs.get_reactor().callFromThread(fire, event_list)
with PreserveLoggingContext():
self.hs.get_reactor().callFromThread(fire, event_list)

@defer.inlineCallbacks
def _enqueue_events(self, events, check_redacted=True, allow_rejected=False):
Expand Down

0 comments on commit b1fe697

Please sign in to comment.