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

Commit 8045ad5

Browse files
committed
Make _get_state_group_for_events raise if state is unknown
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.
1 parent fdcd35d commit 8045ad5

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

synapse/storage/databases/main/state.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
# limitations under the License.
1515
import collections.abc
1616
import logging
17-
from typing import TYPE_CHECKING, Iterable, Optional, Set
17+
from typing import TYPE_CHECKING, Collection, Dict, Iterable, Optional, Set
1818

1919
from synapse.api.constants import EventTypes, Membership
20-
from synapse.api.errors import NotFoundError, UnsupportedRoomVersionError
20+
from synapse.api.errors import NotFoundError, StoreError, UnsupportedRoomVersionError
2121
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS, RoomVersion
2222
from synapse.events import EventBase
2323
from synapse.storage._base import SQLBaseStore
@@ -304,8 +304,13 @@ async def _get_state_group_for_event(self, event_id: str) -> Optional[int]:
304304
list_name="event_ids",
305305
num_args=1,
306306
)
307-
async def _get_state_group_for_events(self, event_ids):
308-
"""Returns mapping event_id -> state_group"""
307+
async def _get_state_group_for_events(
308+
self, event_ids: Collection[str]
309+
) -> Dict[str, int]:
310+
"""Returns mapping event_id -> state_group.
311+
312+
Raises a StoreError if the state is unknown at any of the given events
313+
"""
309314
rows = await self.db_pool.simple_select_many_batch(
310315
table="event_to_state_groups",
311316
column="event_id",
@@ -315,7 +320,13 @@ async def _get_state_group_for_events(self, event_ids):
315320
desc="_get_state_group_for_events",
316321
)
317322

318-
return {row["event_id"]: row["state_group"] for row in rows}
323+
res = {row["event_id"]: row["state_group"] for row in rows}
324+
for e in event_ids:
325+
if e not in res:
326+
raise StoreError(
327+
404, "No state group for unknown or outlier event %s" % e
328+
)
329+
return res
319330

320331
async def get_referenced_state_groups(
321332
self, state_groups: Iterable[int]

synapse/storage/state.py

+16
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,10 @@ async def get_state_groups_ids(
571571
572572
Returns:
573573
dict of state_group_id -> (dict of (type, state_key) -> event id)
574+
575+
Raises:
576+
StoreError if we don't have a state group for any of the events (ie they
577+
are outliers or unknown)
574578
"""
575579
if not event_ids:
576580
return {}
@@ -659,6 +663,10 @@ async def get_state_for_events(
659663
660664
Returns:
661665
A dict of (event_id) -> (type, state_key) -> [state_events]
666+
667+
Raises:
668+
StoreError if we don't have a state group for any of the events (ie they
669+
are outliers or unknown)
662670
"""
663671
event_to_groups = await self.stores.main._get_state_group_for_events(event_ids)
664672

@@ -696,6 +704,10 @@ async def get_state_ids_for_events(
696704
697705
Returns:
698706
A dict from event_id -> (type, state_key) -> event_id
707+
708+
Raises:
709+
StoreError if we don't have a state group for any of the events (ie they
710+
are outliers or unknown)
699711
"""
700712
event_to_groups = await self.stores.main._get_state_group_for_events(event_ids)
701713

@@ -723,6 +735,10 @@ async def get_state_for_event(
723735
724736
Returns:
725737
A dict from (type, state_key) -> state_event
738+
739+
Raises:
740+
StoreError if we don't have a state group for the event (ie it is an
741+
outlier or is unknown)
726742
"""
727743
state_map = await self.get_state_for_events(
728744
[event_id], state_filter or StateFilter.all()

0 commit comments

Comments
 (0)