From 99d61895e1c7516b26d1409da64113174cb23701 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Thu, 12 Sep 2019 17:16:18 +0100 Subject: [PATCH 1/3] Fix race condition in room stats. --- synapse/handlers/stats.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/synapse/handlers/stats.py b/synapse/handlers/stats.py index 3c265f3718e0..7b8b6ce33e2f 100644 --- a/synapse/handlers/stats.py +++ b/synapse/handlers/stats.py @@ -84,6 +84,10 @@ def _unsafe_process(self): # Loop round handling deltas until we're up to date while True: + room_max_stream_ordering = yield self.store.get_room_max_stream_ordering() + if self.pos == room_max_stream_ordering: + break + deltas = yield self.store.get_current_state_deltas(self.pos) if deltas: @@ -94,7 +98,7 @@ def _unsafe_process(self): else: room_deltas = {} user_deltas = {} - max_pos = yield self.store.get_room_max_stream_ordering() + max_pos = room_max_stream_ordering # Then count deltas for total_events and total_event_bytes. room_count, user_count = yield self.store.get_changes_room_total_events_and_bytes( @@ -117,10 +121,9 @@ def _unsafe_process(self): stream_id=max_pos, ) - event_processing_positions.labels("stats").set(max_pos) + logger.debug("Handled room stats to %s -> %s", self.pos, max_pos) - if self.pos == max_pos: - break + event_processing_positions.labels("stats").set(max_pos) self.pos = max_pos From f09dbd1a423f5c122ed3d3ec9f1d9f5a798a49cd Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Thu, 12 Sep 2019 17:17:32 +0100 Subject: [PATCH 2/3] Newsfile --- changelog.d/6029.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/6029.bugfix diff --git a/changelog.d/6029.bugfix b/changelog.d/6029.bugfix new file mode 100644 index 000000000000..9ea095103b45 --- /dev/null +++ b/changelog.d/6029.bugfix @@ -0,0 +1 @@ +Fix room and user stats tracking. From 976c1ce5f7386225ecbb0fcd7f30138f55750c49 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Tue, 17 Sep 2019 12:23:59 +0100 Subject: [PATCH 3/3] Update synapse/handlers/stats.py --- synapse/handlers/stats.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/synapse/handlers/stats.py b/synapse/handlers/stats.py index 7b8b6ce33e2f..cbac7c347aab 100644 --- a/synapse/handlers/stats.py +++ b/synapse/handlers/stats.py @@ -84,6 +84,9 @@ def _unsafe_process(self): # Loop round handling deltas until we're up to date while True: + # Be sure to read the max stream_ordering *before* checking if there are any outstanding + # deltas, since there is otherwise a chance that we could miss updates which arrive + # after we check the deltas. room_max_stream_ordering = yield self.store.get_room_max_stream_ordering() if self.pos == room_max_stream_ordering: break