def __init__(self, hs): self.hs = hs self.auth = hs.get_auth() self.store = hs.get_datastore() self.storage = hs.get_storage() self.state_store = self.storage.state self.clock = hs.get_clock() self._server_name = hs.hostname self.pagination_lock = ReadWriteLock() self._purges_in_progress_by_room = set() # map from purge id to PurgeStatus self._purges_by_id = {} self._event_serializer = hs.get_event_client_serializer() self._retention_default_max_lifetime = hs.config.retention_default_max_lifetime if hs.config.retention_enabled: # Run the purge jobs described in the configuration file. for job in hs.config.retention_purge_jobs: logger.info("Setting up purge job with config: %s", job) self.clock.looping_call( run_as_background_process, job["interval"], "purge_history_for_rooms_in_range", self.purge_history_for_rooms_in_range, job["shortest_max_lifetime"], job["longest_max_lifetime"], )
def __init__(self, hs): self.hs = hs self.auth = hs.get_auth() self.store = hs.get_datastore() self.clock = hs.get_clock() self.pagination_lock = ReadWriteLock() self._purges_in_progress_by_room = set() # map from purge id to PurgeStatus self._purges_by_id = {}
def __init__(self, hs): self.hs = hs self.auth = hs.get_auth() self.store = hs.get_datastore() self.storage = hs.get_storage() self.state_store = self.storage.state self.clock = hs.get_clock() self._server_name = hs.hostname self.pagination_lock = ReadWriteLock() self._purges_in_progress_by_room = set() # map from purge id to PurgeStatus self._purges_by_id = {} self._event_serializer = hs.get_event_client_serializer()
def test_cancellation_while_holding_write_lock(self): """Test cancellation while holding a write lock. A waiting reader should be given the lock when the writer holding the lock is cancelled. """ rwlock = ReadWriteLock() key = "key" # 1. A writer takes the lock and blocks. writer_d, _, _ = self._start_blocking_writer(rwlock, key, "write completed") # 2. A reader waits for the writer to complete. reader_d, _ = self._start_nonblocking_reader(rwlock, key, "read completed") self.assertFalse(reader_d.called) # 3. The writer is cancelled. writer_d.cancel() self.failureResultOf(writer_d, CancelledError) # 4. The reader should take the lock and complete. self.assertTrue(reader_d.called, "Reader is stuck waiting for a cancelled writer") self.assertEqual("read completed", self.successResultOf(reader_d))
def __init__(self, hs: "HomeServer"): self.hs = hs self.auth = hs.get_auth() self.store = hs.get_datastores().main self._storage_controllers = hs.get_storage_controllers() self._state_storage_controller = self._storage_controllers.state self.clock = hs.get_clock() self._server_name = hs.hostname self._room_shutdown_handler = hs.get_room_shutdown_handler() self._relations_handler = hs.get_relations_handler() self.pagination_lock = ReadWriteLock() # IDs of rooms in which there currently an active purge *or delete* operation. self._purges_in_progress_by_room: Set[str] = set() # map from purge id to PurgeStatus self._purges_by_id: Dict[str, PurgeStatus] = {} # map from purge id to DeleteStatus self._delete_by_id: Dict[str, DeleteStatus] = {} # map from room id to delete ids # Dict[`room_id`, List[`delete_id`]] self._delete_by_room: Dict[str, List[str]] = {} self._event_serializer = hs.get_event_client_serializer() self._retention_default_max_lifetime = ( hs.config.retention.retention_default_max_lifetime) self._retention_allowed_lifetime_min = ( hs.config.retention.retention_allowed_lifetime_min) self._retention_allowed_lifetime_max = ( hs.config.retention.retention_allowed_lifetime_max) if (hs.config.worker.run_background_tasks and hs.config.retention.retention_enabled): # Run the purge jobs described in the configuration file. for job in hs.config.retention.retention_purge_jobs: logger.info("Setting up purge job with config: %s", job) self.clock.looping_call( run_as_background_process, job.interval, "purge_history_for_rooms_in_range", self.purge_history_for_rooms_in_range, job.shortest_max_lifetime, job.longest_max_lifetime, )
def test_rwlock(self): rwlock = ReadWriteLock() key = object() ds = [ rwlock.read(key), # 0 rwlock.read(key), # 1 rwlock.write(key), # 2 rwlock.write(key), # 3 rwlock.read(key), # 4 rwlock.read(key), # 5 rwlock.write(key), # 6 ] ds = [defer.ensureDeferred(d) for d in ds] self._assert_called_before_not_after(ds, 2) with ds[0].result: self._assert_called_before_not_after(ds, 2) self._assert_called_before_not_after(ds, 2) with ds[1].result: self._assert_called_before_not_after(ds, 2) self._assert_called_before_not_after(ds, 3) with ds[2].result: self._assert_called_before_not_after(ds, 3) self._assert_called_before_not_after(ds, 4) with ds[3].result: self._assert_called_before_not_after(ds, 4) self._assert_called_before_not_after(ds, 6) with ds[5].result: self._assert_called_before_not_after(ds, 6) self._assert_called_before_not_after(ds, 6) with ds[4].result: self._assert_called_before_not_after(ds, 6) self._assert_called_before_not_after(ds, 7) with ds[6].result: pass d = defer.ensureDeferred(rwlock.write(key)) self.assertTrue(d.called) with d.result: pass d = defer.ensureDeferred(rwlock.read(key)) self.assertTrue(d.called) with d.result: pass
def __init__(self, hs): self.hs = hs self.auth = hs.get_auth() self.store = hs.get_datastore() self.clock = hs.get_clock() self.pagination_lock = ReadWriteLock() self._purges_in_progress_by_room = set() # map from purge id to PurgeStatus self._purges_by_id = {}
def test_cancellation_while_waiting_for_read_lock(self): """Test cancellation while waiting for a read lock. Tests that cancelling a waiting reader: * does not cancel the writer it is waiting on * does not cancel the next writer waiting on it * does not allow the next writer to acquire the lock before an earlier writer has finished * does not keep the next writer waiting indefinitely These correspond to the asserts with explicit messages. """ rwlock = ReadWriteLock() key = "key" # 1. A writer takes the lock and blocks. writer1_d, _, unblock_writer1 = self._start_blocking_writer( rwlock, key, "write 1 completed") # 2. A reader waits for the first writer to complete. # This reader will be cancelled later. reader_d, _ = self._start_nonblocking_reader(rwlock, key, "read completed") self.assertFalse(reader_d.called) # 3. A second writer waits for both the first writer and the reader to complete. writer2_d, _ = self._start_nonblocking_writer(rwlock, key, "write 2 completed") self.assertFalse(writer2_d.called) # 4. The waiting reader is cancelled. # Neither of the writers should be cancelled. # The second writer should still be waiting, but only on the first writer. reader_d.cancel() self.failureResultOf(reader_d, CancelledError) self.assertFalse(writer1_d.called, "First writer was unexpectedly cancelled") self.assertFalse( writer2_d.called, "Second writer was unexpectedly cancelled or given the lock before the " "first writer finished", ) # 5. Unblock the first writer, which should complete. unblock_writer1.callback(None) self.assertEqual("write 1 completed", self.successResultOf(writer1_d)) # 6. The second writer should take the lock and complete. self.assertTrue( writer2_d.called, "Second writer is stuck waiting for a cancelled reader") self.assertEqual("write 2 completed", self.successResultOf(writer2_d))
def test_rwlock(self): rwlock = ReadWriteLock() key = object() ds = [ rwlock.read(key), # 0 rwlock.read(key), # 1 rwlock.write(key), # 2 rwlock.write(key), # 3 rwlock.read(key), # 4 rwlock.read(key), # 5 rwlock.write(key), # 6 ] self._assert_called_before_not_after(ds, 2) with ds[0].result: self._assert_called_before_not_after(ds, 2) self._assert_called_before_not_after(ds, 2) with ds[1].result: self._assert_called_before_not_after(ds, 2) self._assert_called_before_not_after(ds, 3) with ds[2].result: self._assert_called_before_not_after(ds, 3) self._assert_called_before_not_after(ds, 4) with ds[3].result: self._assert_called_before_not_after(ds, 4) self._assert_called_before_not_after(ds, 6) with ds[5].result: self._assert_called_before_not_after(ds, 6) self._assert_called_before_not_after(ds, 6) with ds[4].result: self._assert_called_before_not_after(ds, 6) self._assert_called_before_not_after(ds, 7) with ds[6].result: pass d = rwlock.write(key) self.assertTrue(d.called) with d.result: pass d = rwlock.read(key) self.assertTrue(d.called) with d.result: pass
def test_lock_handoff_to_nonblocking_writer(self): """Test a writer handing the lock to another writer that completes instantly.""" rwlock = ReadWriteLock() key = "key" d1, _, unblock = self._start_blocking_writer(rwlock, key, "write 1 completed") d2, _ = self._start_nonblocking_writer(rwlock, key, "write 2 completed") self.assertFalse(d1.called) self.assertFalse(d2.called) # Unblock the first writer. The second writer will complete without blocking. unblock.callback(None) self.assertTrue(d1.called) self.assertTrue(d2.called) # The `ReadWriteLock` should operate as normal. d3, _ = self._start_nonblocking_writer(rwlock, key, "write 3 completed") self.assertTrue(d3.called)
def __init__(self, hs: "HomeServer"): self.hs = hs self.auth = hs.get_auth() self.store = hs.get_datastore() self.storage = hs.get_storage() self.state_store = self.storage.state self.clock = hs.get_clock() self._server_name = hs.hostname self.pagination_lock = ReadWriteLock() self._purges_in_progress_by_room: Set[str] = set() # map from purge id to PurgeStatus self._purges_by_id: Dict[str, PurgeStatus] = {} self._event_serializer = hs.get_event_client_serializer() self._retention_default_max_lifetime = ( hs.config.server.retention_default_max_lifetime) self._retention_allowed_lifetime_min = ( hs.config.server.retention_allowed_lifetime_min) self._retention_allowed_lifetime_max = ( hs.config.server.retention_allowed_lifetime_max) if hs.config.worker.run_background_tasks and hs.config.server.retention_enabled: # Run the purge jobs described in the configuration file. for job in hs.config.server.retention_purge_jobs: logger.info("Setting up purge job with config: %s", job) self.clock.looping_call( run_as_background_process, job.interval, "purge_history_for_rooms_in_range", self.purge_history_for_rooms_in_range, job.shortest_max_lifetime, job.longest_max_lifetime, )
def test_cancellation_while_waiting_for_write_lock(self): """Test cancellation while waiting for a write lock. Tests that cancelling a waiting writer: * does not cancel the reader or writer it is waiting on * does not cancel the next writer waiting on it * does not allow the next writer to acquire the lock before an earlier reader and writer have finished * does not keep the next writer waiting indefinitely These correspond to the asserts with explicit messages. """ rwlock = ReadWriteLock() key = "key" # 1. A reader takes the lock and blocks. reader_d, _, unblock_reader = self._start_blocking_reader( rwlock, key, "read completed") # 2. A writer waits for the reader to complete. writer1_d, _, unblock_writer1 = self._start_blocking_writer( rwlock, key, "write 1 completed") # 3. A second writer waits for both the reader and first writer to complete. # This writer will be cancelled later. writer2_d, _ = self._start_nonblocking_writer(rwlock, key, "write 2 completed") self.assertFalse(writer2_d.called) # 4. A third writer waits for the second writer to complete. writer3_d, _ = self._start_nonblocking_writer(rwlock, key, "write 3 completed") self.assertFalse(writer3_d.called) # 5. The second writer is cancelled, but continues waiting for the lock. # The reader, first writer and third writer should not be cancelled. # The first writer should still be waiting on the reader. # The third writer should still be waiting on the second writer. writer2_d.cancel() self.assertNoResult(writer2_d) self.assertFalse(reader_d.called, "Reader was unexpectedly cancelled") self.assertFalse(writer1_d.called, "First writer was unexpectedly cancelled") self.assertFalse( writer3_d.called, "Third writer was unexpectedly cancelled or given the lock before the first " "writer finished", ) # 6. Unblock the reader, which should complete. # The first writer should be given the lock and block. # The third writer should still be waiting on the second writer. unblock_reader.callback(None) self.assertEqual("read completed", self.successResultOf(reader_d)) self.assertNoResult(writer2_d) self.assertFalse( writer3_d.called, "Third writer was unexpectedly given the lock before the first writer " "finished", ) # 7. Unblock the first writer, which should complete. unblock_writer1.callback(None) self.assertEqual("write 1 completed", self.successResultOf(writer1_d)) # 8. The second writer should take the lock and release it immediately, since it # has been cancelled. self.failureResultOf(writer2_d, CancelledError) # 9. The third writer should take the lock and complete. self.assertTrue( writer3_d.called, "Third writer is stuck waiting for a cancelled writer") self.assertEqual("write 3 completed", self.successResultOf(writer3_d))
class PaginationHandler(object): """Handles pagination and purge history requests. These are in the same handler due to the fact we need to block clients paginating during a purge. """ def __init__(self, hs): self.hs = hs self.auth = hs.get_auth() self.store = hs.get_datastore() self.storage = hs.get_storage() self.state_store = self.storage.state self.clock = hs.get_clock() self._server_name = hs.hostname self.pagination_lock = ReadWriteLock() self._purges_in_progress_by_room = set() # map from purge id to PurgeStatus self._purges_by_id = {} self._event_serializer = hs.get_event_client_serializer() self._retention_default_max_lifetime = hs.config.retention_default_max_lifetime if hs.config.retention_enabled: # Run the purge jobs described in the configuration file. for job in hs.config.retention_purge_jobs: logger.info("Setting up purge job with config: %s", job) self.clock.looping_call( run_as_background_process, job["interval"], "purge_history_for_rooms_in_range", self.purge_history_for_rooms_in_range, job["shortest_max_lifetime"], job["longest_max_lifetime"], ) @defer.inlineCallbacks def purge_history_for_rooms_in_range(self, min_ms, max_ms): """Purge outdated events from rooms within the given retention range. If a default retention policy is defined in the server's configuration and its 'max_lifetime' is within this range, also targets rooms which don't have a retention policy. Args: min_ms (int|None): Duration in milliseconds that define the lower limit of the range to handle (exclusive). If None, it means that the range has no lower limit. max_ms (int|None): Duration in milliseconds that define the upper limit of the range to handle (inclusive). If None, it means that the range has no upper limit. """ # We want the storage layer to to include rooms with no retention policy in its # return value only if a default retention policy is defined in the server's # configuration and that policy's 'max_lifetime' is either lower (or equal) than # max_ms or higher than min_ms (or both). if self._retention_default_max_lifetime is not None: include_null = True if min_ms is not None and min_ms >= self._retention_default_max_lifetime: # The default max_lifetime is lower than (or equal to) min_ms. include_null = False if max_ms is not None and max_ms < self._retention_default_max_lifetime: # The default max_lifetime is higher than max_ms. include_null = False else: include_null = False logger.info( "[purge] Running purge job for %d < max_lifetime <= %d (include NULLs = %s)", min_ms, max_ms, include_null, ) rooms = yield self.store.get_rooms_for_retention_period_in_range( min_ms, max_ms, include_null) logger.debug("[purge] Rooms to purge: %s", rooms) for room_id, retention_policy in iteritems(rooms): logger.info("[purge] Attempting to purge messages in room %s", room_id) if room_id in self._purges_in_progress_by_room: logger.warning( "[purge] not purging room %s as there's an ongoing purge running" " for this room", room_id, ) continue max_lifetime = retention_policy["max_lifetime"] if max_lifetime is None: # If max_lifetime is None, it means that include_null equals True, # therefore we can safely assume that there is a default policy defined # in the server's configuration. max_lifetime = self._retention_default_max_lifetime # Figure out what token we should start purging at. ts = self.clock.time_msec() - max_lifetime stream_ordering = yield self.store.find_first_stream_ordering_after_ts( ts) r = yield self.store.get_room_event_before_stream_ordering( room_id, stream_ordering, ) if not r: logger.warning( "[purge] purging events not possible: No event found " "(ts %i => stream_ordering %i)", ts, stream_ordering, ) continue (stream, topo, _event_id) = r token = "t%d-%d" % (topo, stream) purge_id = random_string(16) self._purges_by_id[purge_id] = PurgeStatus() logger.info("Starting purging events in room %s (purge_id %s)" % (room_id, purge_id)) # We want to purge everything, including local events, and to run the purge in # the background so that it's not blocking any other operation apart from # other purges in the same room. run_as_background_process( "_purge_history", self._purge_history, purge_id, room_id, token, True, ) def start_purge_history(self, room_id, token, delete_local_events=False): """Start off a history purge on a room. Args: room_id (str): The room to purge from token (str): topological token to delete events before delete_local_events (bool): True to delete local events as well as remote ones Returns: str: unique ID for this purge transaction. """ if room_id in self._purges_in_progress_by_room: raise SynapseError( 400, "History purge already in progress for %s" % (room_id, )) purge_id = random_string(16) # we log the purge_id here so that it can be tied back to the # request id in the log lines. logger.info("[purge] starting purge_id %s", purge_id) self._purges_by_id[purge_id] = PurgeStatus() run_in_background(self._purge_history, purge_id, room_id, token, delete_local_events) return purge_id @defer.inlineCallbacks def _purge_history(self, purge_id, room_id, token, delete_local_events): """Carry out a history purge on a room. Args: purge_id (str): The id for this purge room_id (str): The room to purge from token (str): topological token to delete events before delete_local_events (bool): True to delete local events as well as remote ones Returns: Deferred """ self._purges_in_progress_by_room.add(room_id) try: with (yield self.pagination_lock.write(room_id)): yield self.storage.purge_events.purge_history( room_id, token, delete_local_events) logger.info("[purge] complete") self._purges_by_id[purge_id].status = PurgeStatus.STATUS_COMPLETE except Exception: f = Failure() logger.error("[purge] failed", exc_info=(f.type, f.value, f.getTracebackObject())) self._purges_by_id[purge_id].status = PurgeStatus.STATUS_FAILED finally: self._purges_in_progress_by_room.discard(room_id) # remove the purge from the list 24 hours after it completes def clear_purge(): del self._purges_by_id[purge_id] self.hs.get_reactor().callLater(24 * 3600, clear_purge) def get_purge_status(self, purge_id): """Get the current status of an active purge Args: purge_id (str): purge_id returned by start_purge_history Returns: PurgeStatus|None """ return self._purges_by_id.get(purge_id) async def purge_room(self, room_id): """Purge the given room from the database""" with (await self.pagination_lock.write(room_id)): # check we know about the room await self.store.get_room_version(room_id) # first check that we have no users in this room joined = await defer.maybeDeferred(self.store.is_host_joined, room_id, self._server_name) if joined: raise SynapseError(400, "Users are still joined to this room") await self.storage.purge_events.purge_room(room_id) async def get_messages( self, requester, room_id=None, pagin_config=None, as_client_event=True, event_filter=None, ): """Get messages in a room. Args: requester (Requester): The user requesting messages. room_id (str): The room they want messages from. pagin_config (synapse.api.streams.PaginationConfig): The pagination config rules to apply, if any. as_client_event (bool): True to get events in client-server format. event_filter (Filter): Filter to apply to results or None Returns: dict: Pagination API results """ user_id = requester.user.to_string() if pagin_config.from_token: room_token = pagin_config.from_token.room_key else: pagin_config.from_token = ( await self.hs.get_event_sources().get_current_token_for_pagination()) room_token = pagin_config.from_token.room_key room_token = RoomStreamToken.parse(room_token) pagin_config.from_token = pagin_config.from_token.copy_and_replace( "room_key", str(room_token)) source_config = pagin_config.get_source_config("room") with (await self.pagination_lock.read(room_id)): ( membership, member_event_id, ) = await self.auth.check_in_room_or_world_readable( room_id, user_id) if source_config.direction == "b": # if we're going backwards, we might need to backfill. This # requires that we have a topo token. if room_token.topological: max_topo = room_token.topological else: max_topo = await self.store.get_max_topological_token( room_id, room_token.stream) if membership == Membership.LEAVE: # If they have left the room then clamp the token to be before # they left the room, to save the effort of loading from the # database. leave_token = await self.store.get_topological_token_for_event( member_event_id) leave_token = RoomStreamToken.parse(leave_token) if leave_token.topological < max_topo: source_config.from_key = str(leave_token) await self.hs.get_handlers().federation_handler.maybe_backfill( room_id, max_topo) events, next_key = await self.store.paginate_room_events( room_id=room_id, from_key=source_config.from_key, to_key=source_config.to_key, direction=source_config.direction, limit=source_config.limit, event_filter=event_filter, ) next_token = pagin_config.from_token.copy_and_replace( "room_key", next_key) if events: if event_filter: events = event_filter.filter(events) events = await filter_events_for_client( self.storage, user_id, events, is_peeking=(member_event_id is None)) if not events: return { "chunk": [], "start": pagin_config.from_token.to_string(), "end": next_token.to_string(), } state = None if event_filter and event_filter.lazy_load_members( ) and len(events) > 0: # TODO: remove redundant members # FIXME: we also care about invite targets etc. state_filter = StateFilter.from_types( (EventTypes.Member, event.sender) for event in events) state_ids = await self.state_store.get_state_ids_for_event( events[0].event_id, state_filter=state_filter) if state_ids: state = await self.store.get_events(list(state_ids.values())) state = state.values() time_now = self.clock.time_msec() chunk = { "chunk": (await self._event_serializer.serialize_events( events, time_now, as_client_event=as_client_event)), "start": pagin_config.from_token.to_string(), "end": next_token.to_string(), } if state: chunk["state"] = await self._event_serializer.serialize_events( state, time_now, as_client_event=as_client_event) return chunk
class PaginationHandler(object): """Handles pagination and purge history requests. These are in the same handler due to the fact we need to block clients paginating during a purge. """ def __init__(self, hs): self.hs = hs self.auth = hs.get_auth() self.store = hs.get_datastore() self.clock = hs.get_clock() self.pagination_lock = ReadWriteLock() self._purges_in_progress_by_room = set() # map from purge id to PurgeStatus self._purges_by_id = {} def start_purge_history(self, room_id, token, delete_local_events=False): """Start off a history purge on a room. Args: room_id (str): The room to purge from token (str): topological token to delete events before delete_local_events (bool): True to delete local events as well as remote ones Returns: str: unique ID for this purge transaction. """ if room_id in self._purges_in_progress_by_room: raise SynapseError( 400, "History purge already in progress for %s" % (room_id, ), ) purge_id = random_string(16) # we log the purge_id here so that it can be tied back to the # request id in the log lines. logger.info("[purge] starting purge_id %s", purge_id) self._purges_by_id[purge_id] = PurgeStatus() run_in_background( self._purge_history, purge_id, room_id, token, delete_local_events, ) return purge_id @defer.inlineCallbacks def _purge_history(self, purge_id, room_id, token, delete_local_events): """Carry out a history purge on a room. Args: purge_id (str): The id for this purge room_id (str): The room to purge from token (str): topological token to delete events before delete_local_events (bool): True to delete local events as well as remote ones Returns: Deferred """ self._purges_in_progress_by_room.add(room_id) try: with (yield self.pagination_lock.write(room_id)): yield self.store.purge_history( room_id, token, delete_local_events, ) logger.info("[purge] complete") self._purges_by_id[purge_id].status = PurgeStatus.STATUS_COMPLETE except Exception: logger.error("[purge] failed: %s", Failure().getTraceback().rstrip()) self._purges_by_id[purge_id].status = PurgeStatus.STATUS_FAILED finally: self._purges_in_progress_by_room.discard(room_id) # remove the purge from the list 24 hours after it completes def clear_purge(): del self._purges_by_id[purge_id] self.hs.get_reactor().callLater(24 * 3600, clear_purge) def get_purge_status(self, purge_id): """Get the current status of an active purge Args: purge_id (str): purge_id returned by start_purge_history Returns: PurgeStatus|None """ return self._purges_by_id.get(purge_id) @defer.inlineCallbacks def get_messages(self, requester, room_id=None, pagin_config=None, as_client_event=True, event_filter=None): """Get messages in a room. Args: requester (Requester): The user requesting messages. room_id (str): The room they want messages from. pagin_config (synapse.api.streams.PaginationConfig): The pagination config rules to apply, if any. as_client_event (bool): True to get events in client-server format. event_filter (Filter): Filter to apply to results or None Returns: dict: Pagination API results """ user_id = requester.user.to_string() if pagin_config.from_token: room_token = pagin_config.from_token.room_key else: pagin_config.from_token = ( yield self.hs.get_event_sources().get_current_token_for_room( room_id=room_id ) ) room_token = pagin_config.from_token.room_key room_token = RoomStreamToken.parse(room_token) pagin_config.from_token = pagin_config.from_token.copy_and_replace( "room_key", str(room_token) ) source_config = pagin_config.get_source_config("room") with (yield self.pagination_lock.read(room_id)): membership, member_event_id = yield self.auth.check_in_room_or_world_readable( room_id, user_id ) if source_config.direction == 'b': # if we're going backwards, we might need to backfill. This # requires that we have a topo token. if room_token.topological: max_topo = room_token.topological else: max_topo = yield self.store.get_max_topological_token( room_id, room_token.stream ) if membership == Membership.LEAVE: # If they have left the room then clamp the token to be before # they left the room, to save the effort of loading from the # database. leave_token = yield self.store.get_topological_token_for_event( member_event_id ) leave_token = RoomStreamToken.parse(leave_token) if leave_token.topological < max_topo: source_config.from_key = str(leave_token) yield self.hs.get_handlers().federation_handler.maybe_backfill( room_id, max_topo ) events, next_key = yield self.store.paginate_room_events( room_id=room_id, from_key=source_config.from_key, to_key=source_config.to_key, direction=source_config.direction, limit=source_config.limit, event_filter=event_filter, ) next_token = pagin_config.from_token.copy_and_replace( "room_key", next_key ) if events: if event_filter: events = event_filter.filter(events) events = yield filter_events_for_client( self.store, user_id, events, is_peeking=(member_event_id is None), ) if not events: defer.returnValue({ "chunk": [], "start": pagin_config.from_token.to_string(), "end": next_token.to_string(), }) state = None if event_filter and event_filter.lazy_load_members(): # TODO: remove redundant members # FIXME: we also care about invite targets etc. state_filter = StateFilter.from_types( (EventTypes.Member, event.sender) for event in events ) state_ids = yield self.store.get_state_ids_for_event( events[0].event_id, state_filter=state_filter, ) if state_ids: state = yield self.store.get_events(list(state_ids.values())) state = state.values() time_now = self.clock.time_msec() chunk = { "chunk": [ serialize_event(e, time_now, as_client_event) for e in events ], "start": pagin_config.from_token.to_string(), "end": next_token.to_string(), } if state: chunk["state"] = [ serialize_event(e, time_now, as_client_event) for e in state ] defer.returnValue(chunk)
class PaginationHandler: """Handles pagination and purge history requests. These are in the same handler due to the fact we need to block clients paginating during a purge. """ # when to remove a completed deletion/purge from the results map CLEAR_PURGE_AFTER_MS = 1000 * 3600 * 24 # 24 hours def __init__(self, hs: "HomeServer"): self.hs = hs self.auth = hs.get_auth() self.store = hs.get_datastores().main self._storage_controllers = hs.get_storage_controllers() self._state_storage_controller = self._storage_controllers.state self.clock = hs.get_clock() self._server_name = hs.hostname self._room_shutdown_handler = hs.get_room_shutdown_handler() self._relations_handler = hs.get_relations_handler() self.pagination_lock = ReadWriteLock() # IDs of rooms in which there currently an active purge *or delete* operation. self._purges_in_progress_by_room: Set[str] = set() # map from purge id to PurgeStatus self._purges_by_id: Dict[str, PurgeStatus] = {} # map from purge id to DeleteStatus self._delete_by_id: Dict[str, DeleteStatus] = {} # map from room id to delete ids # Dict[`room_id`, List[`delete_id`]] self._delete_by_room: Dict[str, List[str]] = {} self._event_serializer = hs.get_event_client_serializer() self._retention_default_max_lifetime = ( hs.config.retention.retention_default_max_lifetime) self._retention_allowed_lifetime_min = ( hs.config.retention.retention_allowed_lifetime_min) self._retention_allowed_lifetime_max = ( hs.config.retention.retention_allowed_lifetime_max) if (hs.config.worker.run_background_tasks and hs.config.retention.retention_enabled): # Run the purge jobs described in the configuration file. for job in hs.config.retention.retention_purge_jobs: logger.info("Setting up purge job with config: %s", job) self.clock.looping_call( run_as_background_process, job.interval, "purge_history_for_rooms_in_range", self.purge_history_for_rooms_in_range, job.shortest_max_lifetime, job.longest_max_lifetime, ) async def purge_history_for_rooms_in_range(self, min_ms: Optional[int], max_ms: Optional[int]) -> None: """Purge outdated events from rooms within the given retention range. If a default retention policy is defined in the server's configuration and its 'max_lifetime' is within this range, also targets rooms which don't have a retention policy. Args: min_ms: Duration in milliseconds that define the lower limit of the range to handle (exclusive). If None, it means that the range has no lower limit. max_ms: Duration in milliseconds that define the upper limit of the range to handle (inclusive). If None, it means that the range has no upper limit. """ # We want the storage layer to include rooms with no retention policy in its # return value only if a default retention policy is defined in the server's # configuration and that policy's 'max_lifetime' is either lower (or equal) than # max_ms or higher than min_ms (or both). if self._retention_default_max_lifetime is not None: include_null = True if min_ms is not None and min_ms >= self._retention_default_max_lifetime: # The default max_lifetime is lower than (or equal to) min_ms. include_null = False if max_ms is not None and max_ms < self._retention_default_max_lifetime: # The default max_lifetime is higher than max_ms. include_null = False else: include_null = False logger.info( "[purge] Running purge job for %s < max_lifetime <= %s (include NULLs = %s)", min_ms, max_ms, include_null, ) rooms = await self.store.get_rooms_for_retention_period_in_range( min_ms, max_ms, include_null) logger.debug("[purge] Rooms to purge: %s", rooms) for room_id, retention_policy in rooms.items(): logger.info("[purge] Attempting to purge messages in room %s", room_id) if room_id in self._purges_in_progress_by_room: logger.warning( "[purge] not purging room %s as there's an ongoing purge running" " for this room", room_id, ) continue # If max_lifetime is None, it means that the room has no retention policy. # Given we only retrieve such rooms when there's a default retention policy # defined in the server's configuration, we can safely assume that's the # case and use it for this room. max_lifetime = (retention_policy.max_lifetime or self._retention_default_max_lifetime) # Cap the effective max_lifetime to be within the range allowed in the # config. # We do this in two steps: # 1. Make sure it's higher or equal to the minimum allowed value, and if # it's not replace it with that value. This is because the server # operator can be required to not delete information before a given # time, e.g. to comply with freedom of information laws. # 2. Make sure the resulting value is lower or equal to the maximum allowed # value, and if it's not replace it with that value. This is because the # server operator can be required to delete any data after a specific # amount of time. if self._retention_allowed_lifetime_min is not None: max_lifetime = max(self._retention_allowed_lifetime_min, max_lifetime) if self._retention_allowed_lifetime_max is not None: max_lifetime = min(max_lifetime, self._retention_allowed_lifetime_max) logger.debug("[purge] max_lifetime for room %s: %s", room_id, max_lifetime) # Figure out what token we should start purging at. ts = self.clock.time_msec() - max_lifetime stream_ordering = await self.store.find_first_stream_ordering_after_ts( ts) r = await self.store.get_room_event_before_stream_ordering( room_id, stream_ordering, ) if not r: logger.warning( "[purge] purging events not possible: No event found " "(ts %i => stream_ordering %i)", ts, stream_ordering, ) continue (stream, topo, _event_id) = r token = "t%d-%d" % (topo, stream) purge_id = random_string(16) self._purges_by_id[purge_id] = PurgeStatus() logger.info("Starting purging events in room %s (purge_id %s)" % (room_id, purge_id)) # We want to purge everything, including local events, and to run the purge in # the background so that it's not blocking any other operation apart from # other purges in the same room. run_as_background_process( "_purge_history", self._purge_history, purge_id, room_id, token, True, ) def start_purge_history(self, room_id: str, token: str, delete_local_events: bool = False) -> str: """Start off a history purge on a room. Args: room_id: The room to purge from token: topological token to delete events before delete_local_events: True to delete local events as well as remote ones Returns: unique ID for this purge transaction. """ if room_id in self._purges_in_progress_by_room: raise SynapseError( 400, "History purge already in progress for %s" % (room_id, )) purge_id = random_string(16) # we log the purge_id here so that it can be tied back to the # request id in the log lines. logger.info("[purge] starting purge_id %s", purge_id) self._purges_by_id[purge_id] = PurgeStatus() run_as_background_process( "purge_history", self._purge_history, purge_id, room_id, token, delete_local_events, ) return purge_id async def _purge_history(self, purge_id: str, room_id: str, token: str, delete_local_events: bool) -> None: """Carry out a history purge on a room. Args: purge_id: The ID for this purge. room_id: The room to purge from token: topological token to delete events before delete_local_events: True to delete local events as well as remote ones """ self._purges_in_progress_by_room.add(room_id) try: async with self.pagination_lock.write(room_id): await self._storage_controllers.purge_events.purge_history( room_id, token, delete_local_events) logger.info("[purge] complete") self._purges_by_id[purge_id].status = PurgeStatus.STATUS_COMPLETE except Exception: f = Failure() logger.error( "[purge] failed", exc_info=(f.type, f.value, f.getTracebackObject() ) # type: ignore ) self._purges_by_id[purge_id].status = PurgeStatus.STATUS_FAILED self._purges_by_id[purge_id].error = f.getErrorMessage() finally: self._purges_in_progress_by_room.discard(room_id) # remove the purge from the list 24 hours after it completes def clear_purge() -> None: del self._purges_by_id[purge_id] self.hs.get_reactor().callLater( PaginationHandler.CLEAR_PURGE_AFTER_MS / 1000, clear_purge) def get_purge_status(self, purge_id: str) -> Optional[PurgeStatus]: """Get the current status of an active purge Args: purge_id: purge_id returned by start_purge_history """ return self._purges_by_id.get(purge_id) def get_delete_status(self, delete_id: str) -> Optional[DeleteStatus]: """Get the current status of an active deleting Args: delete_id: delete_id returned by start_shutdown_and_purge_room """ return self._delete_by_id.get(delete_id) def get_delete_ids_by_room(self, room_id: str) -> Optional[Collection[str]]: """Get all active delete ids by room Args: room_id: room_id that is deleted """ return self._delete_by_room.get(room_id) async def purge_room(self, room_id: str, force: bool = False) -> None: """Purge the given room from the database. This function is part the delete room v1 API. Args: room_id: room to be purged force: set true to skip checking for joined users. """ async with self.pagination_lock.write(room_id): # first check that we have no users in this room if not force: joined = await self.store.is_host_joined( room_id, self._server_name) if joined: raise SynapseError(400, "Users are still joined to this room") await self._storage_controllers.purge_events.purge_room(room_id) async def get_messages( self, requester: Requester, room_id: str, pagin_config: PaginationConfig, as_client_event: bool = True, event_filter: Optional[Filter] = None, ) -> JsonDict: """Get messages in a room. Args: requester: The user requesting messages. room_id: The room they want messages from. pagin_config: The pagination config rules to apply, if any. as_client_event: True to get events in client-server format. event_filter: Filter to apply to results or None Returns: Pagination API results """ user_id = requester.user.to_string() if pagin_config.from_token: from_token = pagin_config.from_token else: from_token = (await self.hs.get_event_sources(). get_current_token_for_pagination(room_id)) # We expect `/messages` to use historic pagination tokens by default but # `/messages` should still works with live tokens when manually provided. assert from_token.room_key.topological is not None if pagin_config.limit is None: # This shouldn't happen as we've set a default limit before this # gets called. raise Exception("limit not set") room_token = from_token.room_key async with self.pagination_lock.read(room_id): ( membership, member_event_id, ) = await self.auth.check_user_in_room_or_world_readable( room_id, user_id, allow_departed_users=True) if pagin_config.direction == "b": # if we're going backwards, we might need to backfill. This # requires that we have a topo token. if room_token.topological: curr_topo = room_token.topological else: curr_topo = await self.store.get_current_topological_token( room_id, room_token.stream) if membership == Membership.LEAVE: # If they have left the room then clamp the token to be before # they left the room, to save the effort of loading from the # database. # This is only None if the room is world_readable, in which # case "JOIN" would have been returned. assert member_event_id leave_token = await self.store.get_topological_token_for_event( member_event_id) assert leave_token.topological is not None if leave_token.topological < curr_topo: from_token = from_token.copy_and_replace( StreamKeyType.ROOM, leave_token) await self.hs.get_federation_handler().maybe_backfill( room_id, curr_topo, limit=pagin_config.limit, ) to_room_key = None if pagin_config.to_token: to_room_key = pagin_config.to_token.room_key events, next_key = await self.store.paginate_room_events( room_id=room_id, from_key=from_token.room_key, to_key=to_room_key, direction=pagin_config.direction, limit=pagin_config.limit, event_filter=event_filter, ) next_token = from_token.copy_and_replace(StreamKeyType.ROOM, next_key) # if no events are returned from pagination, that implies # we have reached the end of the available events. # In that case we do not return end, to tell the client # there is no need for further queries. if not events: return { "chunk": [], "start": await from_token.to_string(self.store), } if event_filter: events = await event_filter.filter(events) events = await filter_events_for_client( self._storage_controllers, user_id, events, is_peeking=(member_event_id is None), ) # if after the filter applied there are no more events # return immediately - but there might be more in next_token batch if not events: return { "chunk": [], "start": await from_token.to_string(self.store), "end": await next_token.to_string(self.store), } state = None if event_filter and event_filter.lazy_load_members and len(events) > 0: # TODO: remove redundant members # FIXME: we also care about invite targets etc. state_filter = StateFilter.from_types( (EventTypes.Member, event.sender) for event in events) state_ids = await self._state_storage_controller.get_state_ids_for_event( events[0].event_id, state_filter=state_filter) if state_ids: state_dict = await self.store.get_events( list(state_ids.values())) state = state_dict.values() aggregations = await self._relations_handler.get_bundled_aggregations( events, user_id) time_now = self.clock.time_msec() serialize_options = SerializeEventConfig( as_client_event=as_client_event) chunk = { "chunk": (self._event_serializer.serialize_events( events, time_now, config=serialize_options, bundle_aggregations=aggregations, )), "start": await from_token.to_string(self.store), "end": await next_token.to_string(self.store), } if state: chunk["state"] = self._event_serializer.serialize_events( state, time_now, config=serialize_options) return chunk async def _shutdown_and_purge_room( self, delete_id: str, room_id: str, requester_user_id: str, new_room_user_id: Optional[str] = None, new_room_name: Optional[str] = None, message: Optional[str] = None, block: bool = False, purge: bool = True, force_purge: bool = False, ) -> None: """ Shuts down and purges a room. See `RoomShutdownHandler.shutdown_room` for details of creation of the new room Args: delete_id: The ID for this delete. room_id: The ID of the room to shut down. requester_user_id: User who requested the action. Will be recorded as putting the room on the blocking list. new_room_user_id: If set, a new room will be created with this user ID as the creator and admin, and all users in the old room will be moved into that room. If not set, no new room will be created and the users will just be removed from the old room. new_room_name: A string representing the name of the room that new users will be invited to. Defaults to `Content Violation Notification` message: A string containing the first message that will be sent as `new_room_user_id` in the new room. Ideally this will clearly convey why the original room was shut down. Defaults to `Sharing illegal content on this server is not permitted and rooms in violation will be blocked.` block: If set to `true`, this room will be added to a blocking list, preventing future attempts to join the room. Defaults to `false`. purge: If set to `true`, purge the given room from the database. force_purge: If set to `true`, the room will be purged from database also if it fails to remove some users from room. Saves a `RoomShutdownHandler.ShutdownRoomResponse` in `DeleteStatus`: """ self._purges_in_progress_by_room.add(room_id) try: async with self.pagination_lock.write(room_id): self._delete_by_id[ delete_id].status = DeleteStatus.STATUS_SHUTTING_DOWN self._delete_by_id[ delete_id].shutdown_room = await self._room_shutdown_handler.shutdown_room( room_id=room_id, requester_user_id=requester_user_id, new_room_user_id=new_room_user_id, new_room_name=new_room_name, message=message, block=block, ) self._delete_by_id[ delete_id].status = DeleteStatus.STATUS_PURGING if purge: logger.info("starting purge room_id %s", room_id) # first check that we have no users in this room if not force_purge: joined = await self.store.is_host_joined( room_id, self._server_name) if joined: raise SynapseError( 400, "Users are still joined to this room") await self._storage_controllers.purge_events.purge_room( room_id) logger.info("complete") self._delete_by_id[delete_id].status = DeleteStatus.STATUS_COMPLETE except Exception: f = Failure() logger.error( "failed", exc_info=(f.type, f.value, f.getTracebackObject()), # type: ignore ) self._delete_by_id[delete_id].status = DeleteStatus.STATUS_FAILED self._delete_by_id[delete_id].error = f.getErrorMessage() finally: self._purges_in_progress_by_room.discard(room_id) # remove the delete from the list 24 hours after it completes def clear_delete() -> None: del self._delete_by_id[delete_id] self._delete_by_room[room_id].remove(delete_id) if not self._delete_by_room[room_id]: del self._delete_by_room[room_id] self.hs.get_reactor().callLater( PaginationHandler.CLEAR_PURGE_AFTER_MS / 1000, clear_delete) def start_shutdown_and_purge_room( self, room_id: str, requester_user_id: str, new_room_user_id: Optional[str] = None, new_room_name: Optional[str] = None, message: Optional[str] = None, block: bool = False, purge: bool = True, force_purge: bool = False, ) -> str: """Start off shut down and purge on a room. Args: room_id: The ID of the room to shut down. requester_user_id: User who requested the action and put the room on the blocking list. new_room_user_id: If set, a new room will be created with this user ID as the creator and admin, and all users in the old room will be moved into that room. If not set, no new room will be created and the users will just be removed from the old room. new_room_name: A string representing the name of the room that new users will be invited to. Defaults to `Content Violation Notification` message: A string containing the first message that will be sent as `new_room_user_id` in the new room. Ideally this will clearly convey why the original room was shut down. Defaults to `Sharing illegal content on this server is not permitted and rooms in violation will be blocked.` block: If set to `true`, this room will be added to a blocking list, preventing future attempts to join the room. Defaults to `false`. purge: If set to `true`, purge the given room from the database. force_purge: If set to `true`, the room will be purged from database also if it fails to remove some users from room. Returns: unique ID for this delete transaction. """ if room_id in self._purges_in_progress_by_room: raise SynapseError( 400, "History purge already in progress for %s" % (room_id, )) # This check is double to `RoomShutdownHandler.shutdown_room` # But here the requester get a direct response / error with HTTP request # and do not have to check the purge status if new_room_user_id is not None: if not self.hs.is_mine_id(new_room_user_id): raise SynapseError( 400, "User must be our own: %s" % (new_room_user_id, )) delete_id = random_string(16) # we log the delete_id here so that it can be tied back to the # request id in the log lines. logger.info( "starting shutdown room_id %s with delete_id %s", room_id, delete_id, ) self._delete_by_id[delete_id] = DeleteStatus() self._delete_by_room.setdefault(room_id, []).append(delete_id) run_as_background_process( "shutdown_and_purge_room", self._shutdown_and_purge_room, delete_id, room_id, requester_user_id, new_room_user_id, new_room_name, message, block, purge, force_purge, ) return delete_id
def test_rwlock(self): rwlock = ReadWriteLock() key = "key" ds = [ self._start_blocking_reader(rwlock, key, "0"), self._start_blocking_reader(rwlock, key, "1"), self._start_blocking_writer(rwlock, key, "2"), self._start_blocking_writer(rwlock, key, "3"), self._start_blocking_reader(rwlock, key, "4"), self._start_blocking_reader(rwlock, key, "5"), self._start_blocking_writer(rwlock, key, "6"), ] # `Deferred`s that resolve when each reader or writer acquires the lock. acquired_ds = [acquired_d for _, acquired_d, _ in ds] # `Deferred`s that will trigger the release of locks when resolved. release_ds = [release_d for _, _, release_d in ds] # The first two readers should acquire their locks. self._assert_first_n_resolved(acquired_ds, 2) # Release one of the read locks. The next writer should not acquire the lock, # because there is another reader holding the lock. self._assert_first_n_resolved(acquired_ds, 2) release_ds[0].callback(None) self._assert_first_n_resolved(acquired_ds, 2) # Release the other read lock. The next writer should acquire the lock. self._assert_first_n_resolved(acquired_ds, 2) release_ds[1].callback(None) self._assert_first_n_resolved(acquired_ds, 3) # Release the write lock. The next writer should acquire the lock. self._assert_first_n_resolved(acquired_ds, 3) release_ds[2].callback(None) self._assert_first_n_resolved(acquired_ds, 4) # Release the write lock. The next two readers should acquire locks. self._assert_first_n_resolved(acquired_ds, 4) release_ds[3].callback(None) self._assert_first_n_resolved(acquired_ds, 6) # Release one of the read locks. The next writer should not acquire the lock, # because there is another reader holding the lock. self._assert_first_n_resolved(acquired_ds, 6) release_ds[5].callback(None) self._assert_first_n_resolved(acquired_ds, 6) # Release the other read lock. The next writer should acquire the lock. self._assert_first_n_resolved(acquired_ds, 6) release_ds[4].callback(None) self._assert_first_n_resolved(acquired_ds, 7) # Release the write lock. release_ds[6].callback(None) # Acquire and release the write and read locks one last time for good measure. _, acquired_d = self._start_nonblocking_writer(rwlock, key, "last writer") self.assertTrue(acquired_d.called) _, acquired_d = self._start_nonblocking_reader(rwlock, key, "last reader") self.assertTrue(acquired_d.called)
class PaginationHandler(object): """Handles pagination and purge history requests. These are in the same handler due to the fact we need to block clients paginating during a purge. """ def __init__(self, hs): self.hs = hs self.auth = hs.get_auth() self.store = hs.get_datastore() self.clock = hs.get_clock() self.pagination_lock = ReadWriteLock() self._purges_in_progress_by_room = set() # map from purge id to PurgeStatus self._purges_by_id = {} def start_purge_history(self, room_id, token, delete_local_events=False): """Start off a history purge on a room. Args: room_id (str): The room to purge from token (str): topological token to delete events before delete_local_events (bool): True to delete local events as well as remote ones Returns: str: unique ID for this purge transaction. """ if room_id in self._purges_in_progress_by_room: raise SynapseError( 400, "History purge already in progress for %s" % (room_id, ), ) purge_id = random_string(16) # we log the purge_id here so that it can be tied back to the # request id in the log lines. logger.info("[purge] starting purge_id %s", purge_id) self._purges_by_id[purge_id] = PurgeStatus() run_in_background( self._purge_history, purge_id, room_id, token, delete_local_events, ) return purge_id @defer.inlineCallbacks def _purge_history(self, purge_id, room_id, token, delete_local_events): """Carry out a history purge on a room. Args: purge_id (str): The id for this purge room_id (str): The room to purge from token (str): topological token to delete events before delete_local_events (bool): True to delete local events as well as remote ones Returns: Deferred """ self._purges_in_progress_by_room.add(room_id) try: with (yield self.pagination_lock.write(room_id)): yield self.store.purge_history( room_id, token, delete_local_events, ) logger.info("[purge] complete") self._purges_by_id[purge_id].status = PurgeStatus.STATUS_COMPLETE except Exception: f = Failure() logger.error( "[purge] failed", exc_info=(f.type, f.value, f.getTracebackObject()), ) self._purges_by_id[purge_id].status = PurgeStatus.STATUS_FAILED finally: self._purges_in_progress_by_room.discard(room_id) # remove the purge from the list 24 hours after it completes def clear_purge(): del self._purges_by_id[purge_id] self.hs.get_reactor().callLater(24 * 3600, clear_purge) def get_purge_status(self, purge_id): """Get the current status of an active purge Args: purge_id (str): purge_id returned by start_purge_history Returns: PurgeStatus|None """ return self._purges_by_id.get(purge_id) @defer.inlineCallbacks def get_messages(self, requester, room_id=None, pagin_config=None, as_client_event=True, event_filter=None): """Get messages in a room. Args: requester (Requester): The user requesting messages. room_id (str): The room they want messages from. pagin_config (synapse.api.streams.PaginationConfig): The pagination config rules to apply, if any. as_client_event (bool): True to get events in client-server format. event_filter (Filter): Filter to apply to results or None Returns: dict: Pagination API results """ user_id = requester.user.to_string() if pagin_config.from_token: room_token = pagin_config.from_token.room_key else: pagin_config.from_token = ( yield self.hs.get_event_sources().get_current_token_for_room( room_id=room_id)) room_token = pagin_config.from_token.room_key room_token = RoomStreamToken.parse(room_token) pagin_config.from_token = pagin_config.from_token.copy_and_replace( "room_key", str(room_token)) source_config = pagin_config.get_source_config("room") with (yield self.pagination_lock.read(room_id)): membership, member_event_id = yield self.auth.check_in_room_or_world_readable( room_id, user_id) if source_config.direction == 'b': # if we're going backwards, we might need to backfill. This # requires that we have a topo token. if room_token.topological: max_topo = room_token.topological else: max_topo = yield self.store.get_max_topological_token( room_id, room_token.stream) if membership == Membership.LEAVE: # If they have left the room then clamp the token to be before # they left the room, to save the effort of loading from the # database. leave_token = yield self.store.get_topological_token_for_event( member_event_id) leave_token = RoomStreamToken.parse(leave_token) if leave_token.topological < max_topo: source_config.from_key = str(leave_token) yield self.hs.get_handlers().federation_handler.maybe_backfill( room_id, max_topo) events, next_key = yield self.store.paginate_room_events( room_id=room_id, from_key=source_config.from_key, to_key=source_config.to_key, direction=source_config.direction, limit=source_config.limit, event_filter=event_filter, ) next_token = pagin_config.from_token.copy_and_replace( "room_key", next_key) if events: if event_filter: events = event_filter.filter(events) events = yield filter_events_for_client( self.store, user_id, events, is_peeking=(member_event_id is None), ) if not events: defer.returnValue({ "chunk": [], "start": pagin_config.from_token.to_string(), "end": next_token.to_string(), }) state = None if event_filter and event_filter.lazy_load_members( ) and len(events) > 0: # TODO: remove redundant members # FIXME: we also care about invite targets etc. state_filter = StateFilter.from_types( (EventTypes.Member, event.sender) for event in events) state_ids = yield self.store.get_state_ids_for_event( events[0].event_id, state_filter=state_filter, ) if state_ids: state = yield self.store.get_events(list(state_ids.values())) state = state.values() time_now = self.clock.time_msec() chunk = { "chunk": [serialize_event(e, time_now, as_client_event) for e in events], "start": pagin_config.from_token.to_string(), "end": next_token.to_string(), } if state: chunk["state"] = [ serialize_event(e, time_now, as_client_event) for e in state ] defer.returnValue(chunk)