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

Commit

Permalink
Fixed removal of new presence stream states (#10014)
Browse files Browse the repository at this point in the history
Fixes: #9962

This is a fix for above problem.

I fixed it by swaping the order of insertion of new records and deletion of old ones. This ensures that we don't delete fresh database records as we do deletes before inserts.

Signed-off-by: Marek Matys <themarcq@gmail.com>
  • Loading branch information
thermaq authored May 21, 2021
1 parent 7958ead commit 6a8643f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
1 change: 1 addition & 0 deletions changelog.d/10014.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed deletion of new presence stream states from database.
18 changes: 9 additions & 9 deletions synapse/storage/databases/main/presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ def _update_presence_txn(self, txn, stream_orderings, presence_states):
)
txn.call_after(self._get_presence_for_user.invalidate, (state.user_id,))

# Delete old rows to stop database from getting really big
sql = "DELETE FROM presence_stream WHERE stream_id < ? AND "

for states in batch_iter(presence_states, 50):
clause, args = make_in_list_sql_clause(
self.database_engine, "user_id", [s.user_id for s in states]
)
txn.execute(sql + clause, [stream_id] + list(args))

# Actually insert new rows
self.db_pool.simple_insert_many_txn(
txn,
Expand All @@ -117,15 +126,6 @@ def _update_presence_txn(self, txn, stream_orderings, presence_states):
],
)

# Delete old rows to stop database from getting really big
sql = "DELETE FROM presence_stream WHERE stream_id < ? AND "

for states in batch_iter(presence_states, 50):
clause, args = make_in_list_sql_clause(
self.database_engine, "user_id", [s.user_id for s in states]
)
txn.execute(sql + clause, [stream_id] + list(args))

async def get_all_presence_updates(
self, instance_name: str, last_id: int, current_id: int, limit: int
) -> Tuple[List[Tuple[int, list]], int, bool]:
Expand Down

0 comments on commit 6a8643f

Please sign in to comment.