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

Commit

Permalink
Add background update for current_state_events.membership column
Browse files Browse the repository at this point in the history
  • Loading branch information
anoadragon453 committed Feb 19, 2020
2 parents e11e02b + c618a5d commit 1b055eb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
51 changes: 51 additions & 0 deletions synapse/storage/roommember.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
MemberSummary = namedtuple("MemberSummary", ("members", "count"))

_MEMBERSHIP_PROFILE_UPDATE_NAME = "room_membership_profile_update"
_CURRENT_STATE_MEMBERSHIP_UPDATE_NAME = "current_state_events_membership"


class RoomMemberWorkerStore(EventsWorkerStore):
Expand Down Expand Up @@ -602,6 +603,10 @@ def __init__(self, db_conn, hs):
self.register_background_update_handler(
_MEMBERSHIP_PROFILE_UPDATE_NAME, self._background_add_membership_profile
)
self.register_background_update_handler(
_CURRENT_STATE_MEMBERSHIP_UPDATE_NAME,
self._background_current_state_membership,
)

def _store_room_members_txn(self, txn, events, backfilled):
"""Store a room member in the database.
Expand Down Expand Up @@ -781,6 +786,52 @@ def add_membership_profile_txn(txn):

defer.returnValue(result)

@defer.inlineCallbacks
def _background_current_state_membership(self, progress, batch_size):
"""Update the new membership column on current_state_events.
"""

if "rooms" not in progress:
rooms = yield self._simple_select_onecol(
table="current_state_events",
keyvalues={},
retcol="DISTINCT room_id",
desc="_background_current_state_membership_get_rooms",
)
progress["rooms"] = rooms

rooms = progress["rooms"]

def _background_current_state_membership_txn(txn):
processed = 0
while rooms and processed < batch_size:
sql = """
UPDATE current_state_events AS c
SET membership = (
SELECT membership FROM room_memberships
WHERE event_id = c.event_id
)
WHERE room_id = ?
"""
txn.execute(sql, (rooms.pop(),))
processed += txn.rowcount

self._background_update_progress_txn(
txn, _CURRENT_STATE_MEMBERSHIP_UPDATE_NAME, progress
)

return processed

result = yield self.runInteraction(
"_background_current_state_membership_update",
_background_current_state_membership_txn,
)

if not rooms:
yield self._end_background_update(_CURRENT_STATE_MEMBERSHIP_UPDATE_NAME)

defer.returnValue(result)


class _JoinedHostsCache(object):
"""Cache for joined hosts in a room that is optimised to handle updates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@
-- room_memberships, which can be surprisingly costly (we do such queries
-- very frequently).
ALTER TABLE current_state_events ADD membership TEXT;

INSERT INTO background_updates (update_name, progress_json) VALUES
('current_state_events_membership', '{}');

0 comments on commit 1b055eb

Please sign in to comment.