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

Commit

Permalink
Make _get_state_group_for_events raise if state is unknown
Browse files Browse the repository at this point in the history
It seems like calling `_get_state_group_for_events` for an event where the
state is unknown is an error. Accordingly, let's raise an exception rather than
silently returning an empty result.
  • Loading branch information
richvdh committed Mar 31, 2022
1 parent 68b709a commit 726140b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
16 changes: 12 additions & 4 deletions synapse/storage/databases/main/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
from typing import TYPE_CHECKING, Dict, Iterable, Optional, Set, Tuple
from typing import TYPE_CHECKING, Collection, Dict, Iterable, Optional, Set, Tuple

from frozendict import frozendict

Expand Down Expand Up @@ -309,9 +309,13 @@ async def _get_state_group_for_event(self, event_id: str) -> Optional[int]:
num_args=1,
)
async def _get_state_group_for_events(
self, event_ids: Iterable[str]
self, event_ids: Collection[str]
) -> Dict[str, int]:
"""Returns mapping event_id -> state_group"""
"""Returns mapping event_id -> state_group.
Raises:
RuntimeError if the state is unknown at any of the given events
"""
rows = await self.db_pool.simple_select_many_batch(
table="event_to_state_groups",
column="event_id",
Expand All @@ -321,7 +325,11 @@ async def _get_state_group_for_events(
desc="_get_state_group_for_events",
)

return {row["event_id"]: row["state_group"] for row in rows}
res = {row["event_id"]: row["state_group"] for row in rows}
for e in event_ids:
if e not in res:
raise RuntimeError("No state group for unknown or outlier event %s" % e)
return res

async def get_referenced_state_groups(
self, state_groups: Iterable[int]
Expand Down
20 changes: 20 additions & 0 deletions synapse/storage/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,10 @@ async def get_state_groups_ids(
Returns:
dict of state_group_id -> (dict of (type, state_key) -> event id)
Raises:
RuntimeError if we don't have a state group for one or more of the events
(ie they are outliers or unknown)
"""
if not event_ids:
return {}
Expand Down Expand Up @@ -659,6 +663,10 @@ async def get_state_for_events(
Returns:
A dict of (event_id) -> (type, state_key) -> [state_events]
Raises:
RuntimeError if we don't have a state group for one or more of the events
(ie they are outliers or unknown)
"""
event_to_groups = await self.stores.main._get_state_group_for_events(event_ids)

Expand Down Expand Up @@ -696,6 +704,10 @@ async def get_state_ids_for_events(
Returns:
A dict from event_id -> (type, state_key) -> event_id
Raises:
RuntimeError if we don't have a state group for one or more of the events
(ie they are outliers or unknown)
"""
event_to_groups = await self.stores.main._get_state_group_for_events(event_ids)

Expand Down Expand Up @@ -723,6 +735,10 @@ async def get_state_for_event(
Returns:
A dict from (type, state_key) -> state_event
Raises:
RuntimeError if we don't have a state group for the event (ie it is an
outlier or is unknown)
"""
state_map = await self.get_state_for_events(
[event_id], state_filter or StateFilter.all()
Expand All @@ -741,6 +757,10 @@ async def get_state_ids_for_event(
Returns:
A dict from (type, state_key) -> state_event_id
Raises:
RuntimeError if we don't have a state group for the event (ie it is an
outlier or is unknown)
"""
state_map = await self.get_state_ids_for_events(
[event_id], state_filter or StateFilter.all()
Expand Down

0 comments on commit 726140b

Please sign in to comment.