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

Commit

Permalink
Reduce state that push rules pull from DB
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjohnston committed Jun 8, 2022
1 parent 9f3afd2 commit fe6175a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion synapse/push/bulk_push_rule_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ async def action_for_event_by_user(

room_members = await self.store.get_joined_users_from_context(event, context)

room_member_count = await self.store.get_number_joined_users_in_room(
event.room_id
)

(
power_levels,
sender_power_level,
Expand All @@ -278,7 +282,7 @@ async def action_for_event_by_user(

evaluator = PushRuleEvaluatorForEvent(
event,
len(room_members),
room_member_count,
sender_power_level,
power_levels,
relations,
Expand Down
3 changes: 3 additions & 0 deletions synapse/storage/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ def _invalidate_state_caches(
self._attempt_to_invalidate_cache(
"get_users_in_room_with_profiles", (room_id,)
)
self._attempt_to_invalidate_cache(
"get_number_joined_users_in_room.invalidate", (room_id,)
)

# Purge other caches based on room state.
self._attempt_to_invalidate_cache("get_room_summary", (room_id,))
Expand Down
1 change: 1 addition & 0 deletions synapse/storage/databases/main/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ def _invalidate_caches_for_event(
self._membership_stream_cache.entity_has_changed(state_key, stream_ordering)
self.get_invited_rooms_for_local_user.invalidate((state_key,))
self.get_local_users_in_room.invalidate((room_id,))
self.get_number_joined_users_in_room((room_id,))

This comment has been minimized.

Copy link
@DMRobertson

DMRobertson Jun 14, 2022

Contributor

typo here, spotted by @H-Shay and @reivilibre


if relates_to:
self.get_relations_for_event.invalidate((relates_to,))
Expand Down
4 changes: 4 additions & 0 deletions synapse/storage/databases/main/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -1770,6 +1770,10 @@ def _store_room_members_txn(
self.store.get_local_users_in_room.invalidate,
(event.room_id,),
)
txn.call_after(
self.store.get_number_joined_users_in_room.invalidate,
(event.room_id,),
)

# The `_get_membership_from_event_id` is immutable, except for the
# case where we look up an event *before* persisting it.
Expand Down
9 changes: 9 additions & 0 deletions synapse/storage/databases/main/roommember.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,15 @@ def _get_room_summary_txn(
"get_room_summary", _get_room_summary_txn
)

@cached()
async def get_number_joined_users_in_room(self, room_id: str) -> int:
return await self.db_pool.simple_select_one_onecol(
table="current_state_events",
keyvalues={"room_id": room_id, "membership": Membership.JOIN},
retcol="COUNT(*)",
desc="get_number_joined_users_in_room",
)

@cached()
async def get_invited_rooms_for_local_user(
self, user_id: str
Expand Down

0 comments on commit fe6175a

Please sign in to comment.