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

Wait for lazy join to complete when getting current state #12872

Merged
merged 23 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
78faa1a
Rename StateGroupStorage
erikjohnston May 25, 2022
334844d
Add fetching current state funcs to StateStorage
erikjohnston May 25, 2022
fe86915
Use StateStorage.get_current_state_ids
erikjohnston May 25, 2022
a2465b8
Use StateStorage.get_filtered_current_state_ids
erikjohnston May 25, 2022
a2945a5
Use StateStorage.get_current_state_deltas
erikjohnston May 25, 2022
e786f68
Rename to get_partial_current_state_ids
erikjohnston May 25, 2022
3f74b37
Rename to get_partial_filtered_current_state_ids
erikjohnston May 25, 2022
c068581
Rename to get_partial_current_state_deltas
erikjohnston May 25, 2022
6f386d1
Block calls to `get_current_state_ids` and co. when have partial state
erikjohnston May 25, 2022
2b674f8
Add test for `PartialCurrentStateTracker`
erikjohnston May 25, 2022
ecae768
Newsfile
erikjohnston May 25, 2022
d1a1d6a
Don't block sync when lazy joining a room
erikjohnston May 25, 2022
531955f
Fix type annotation
erikjohnston May 25, 2022
6cc7269
Apply suggestions from code review
erikjohnston May 26, 2022
3a20548
s/self.storage/self._storage/
erikjohnston May 27, 2022
6e7625e
s/is_room_got_partial_state/has_room_only_got_partial_state/
erikjohnston May 27, 2022
711ea44
Merge get_filtered and get_current_state_ids
erikjohnston May 27, 2022
1f99ce0
Merge remote-tracking branch 'origin/develop' into erikj/await_curren…
erikjohnston May 27, 2022
b59418f
Merge remote-tracking branch 'origin/develop' into erikj/await_curren…
erikjohnston May 31, 2022
c6fe132
Fix up naming
erikjohnston May 31, 2022
4757b00
Apply suggestions from code review
erikjohnston Jun 1, 2022
b6cb65f
Lint
erikjohnston Jun 1, 2022
c513c20
Rename has_room_only_got_partial_state
erikjohnston Jun 1, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion synapse/handlers/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def __init__(self, hs: "HomeServer"):
self.appservice_handler = hs.get_application_service_handler()
self.event_creation_handler = hs.get_event_creation_handler()
self.store = hs.get_datastores().main
self.storage = hs.get_storage()
self.config = hs.config
self.enable_room_list_search = hs.config.roomdirectory.enable_room_list_search
self.require_membership = hs.config.server.require_membership_for_aliases
Expand Down Expand Up @@ -463,7 +464,9 @@ async def edit_published_room_list(
making_public = visibility == "public"
if making_public:
room_aliases = await self.store.get_aliases_for_room(room_id)
canonical_alias = await self.store.get_canonical_alias_for_room(room_id)
canonical_alias = await self.storage.state.get_canonical_alias_for_room(
room_id
)
if canonical_alias:
room_aliases.append(canonical_alias)

Expand Down
24 changes: 0 additions & 24 deletions synapse/storage/databases/main/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,30 +269,6 @@ def _get_filtered_current_state_ids_txn(
"get_filtered_current_state_ids", _get_filtered_current_state_ids_txn
)

async def get_canonical_alias_for_room(self, room_id: str) -> Optional[str]:
"""Get canonical alias for room, if any

Args:
room_id: The room ID

Returns:
The canonical alias, if any
"""

state = await self.get_filtered_current_state_ids(
room_id, StateFilter.from_types([(EventTypes.CanonicalAlias, "")])
)

event_id = state.get((EventTypes.CanonicalAlias, ""))
if not event_id:
return None

event = await self.get_event(event_id, allow_none=True)
if not event:
return None

return event.content.get("canonical_alias")

@cached(max_entries=50000)
async def _get_state_group_for_event(self, event_id: str) -> Optional[int]:
return await self.db_pool.simple_select_one_onecol(
Expand Down
87 changes: 87 additions & 0 deletions synapse/storage/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import logging
from typing import (
TYPE_CHECKING,
Any,
Awaitable,
Callable,
Collection,
Expand Down Expand Up @@ -895,3 +896,89 @@ async def store_state_group(
return await self.stores.state.store_state_group(
event_id, room_id, prev_group, delta_ids, current_state_ids
)

async def get_current_state_ids(self, room_id: str) -> StateMap[str]:
"""Get the current state event ids for a room based on the
current_state_events table.

Args:
room_id: The room to get the state IDs of.

Returns:
The current state of the room.
"""

return await self.stores.main.get_current_state_ids(room_id)

async def get_filtered_current_state_ids(
self, room_id: str, state_filter: Optional[StateFilter] = None
) -> StateMap[str]:
"""Get the current state event of a given type for a room based on the
current_state_events table. This may not be as up-to-date as the result
of doing a fresh state resolution as per state_handler.get_current_state

Args:
room_id
state_filter: The state filter used to fetch state
from the database.

Returns:
Map from type/state_key to event ID.
"""
return await self.stores.main.get_filtered_current_state_ids(
room_id, state_filter
)

async def get_canonical_alias_for_room(self, room_id: str) -> Optional[str]:
"""Get canonical alias for room, if any

Args:
room_id: The room ID

Returns:
The canonical alias, if any
"""

state = await self.get_filtered_current_state_ids(
room_id, StateFilter.from_types([(EventTypes.CanonicalAlias, "")])
)

event_id = state.get((EventTypes.CanonicalAlias, ""))
if not event_id:
return None

event = await self.stores.main.get_event(event_id, allow_none=True)
if not event:
return None

return event.content.get("canonical_alias")

async def get_current_state_deltas(
self, prev_stream_id: int, max_stream_id: int
) -> Tuple[int, List[Dict[str, Any]]]:
"""Fetch a list of room state changes since the given stream id

Each entry in the result contains the following fields:
- stream_id (int)
- room_id (str)
- type (str): event type
- state_key (str):
- event_id (str|None): new event_id for this state key. None if the
state has been deleted.
- prev_event_id (str|None): previous event_id for this state key. None
if it's new state.

Args:
prev_stream_id: point to get changes since (exclusive)
max_stream_id: the point that we know has been correctly persisted
- ie, an upper limit to return changes from.

Returns:
A tuple consisting of:
- the stream id which these results go up to
- list of current_state_delta_stream rows. If it is empty, we are
up to date.
"""
return await self.stores.main.get_current_state_deltas(
prev_stream_id, max_stream_id
)