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

Commit c6a3dad

Browse files
committed
Don't store prev state IDs and current state IDs
1 parent ee61e78 commit c6a3dad

5 files changed

Lines changed: 29 additions & 44 deletions

File tree

synapse/events/snapshot.py

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ class EventContext:
5757
If ``state_group`` is None (ie, the event is an outlier),
5858
``state_group_before_event`` will always also be ``None``.
5959
60+
delta_before_after: If `state_group` and `state_group_before_event` are not None
61+
then this is the delta of the state between the two groups.
62+
6063
prev_group: If it is known, ``state_group``'s prev_group. Note that this being
6164
None does not necessarily mean that ``state_group`` does not have
6265
a prev_group!
@@ -75,30 +78,6 @@ class EventContext:
7578
app_service: If this event is being sent by a (local) application service, that
7679
app service.
7780
78-
_current_state_ids: The room state map, including this event - ie, the state
79-
in ``state_group``.
80-
81-
(type, state_key) -> event_id
82-
83-
For an outlier, this is {}
84-
85-
Note that this is a private attribute: it should be accessed via
86-
``get_current_state_ids``. _AsyncEventContext impl calculates this
87-
on-demand: it will be None until that happens.
88-
89-
_prev_state_ids: The room state map, excluding this event - ie, the state
90-
in ``state_group_before_event``. For a non-state
91-
event, this will be the same as _current_state_events.
92-
93-
Note that it is a completely different thing to prev_group!
94-
95-
(type, state_key) -> event_id
96-
97-
For an outlier, this is {}
98-
99-
As with _current_state_ids, this is a private attribute. It should be
100-
accessed via get_prev_state_ids.
101-
10281
partial_state: if True, we may be storing this event with a temporary,
10382
incomplete state.
10483
"""
@@ -107,22 +86,19 @@ class EventContext:
10786
rejected: Union[bool, str] = False
10887
_state_group: Optional[int] = None
10988
state_group_before_event: Optional[int] = None
89+
_delta_before_after: Optional[StateMap[str]] = None
11090
prev_group: Optional[int] = None
11191
delta_ids: Optional[StateMap[str]] = None
11292
app_service: Optional[ApplicationService] = None
11393

114-
_current_state_ids: Optional[StateMap[str]] = None
115-
_prev_state_ids: Optional[StateMap[str]] = None
116-
11794
partial_state: bool = False
11895

11996
@staticmethod
12097
def with_state(
12198
storage: "Storage",
12299
state_group: Optional[int],
123100
state_group_before_event: Optional[int],
124-
current_state_ids: Optional[StateMap[str]],
125-
prev_state_ids: Optional[StateMap[str]],
101+
delta_before_after: Optional[StateMap[str]],
126102
partial_state: bool,
127103
prev_group: Optional[int] = None,
128104
delta_ids: Optional[StateMap[str]] = None,
@@ -131,6 +107,7 @@ def with_state(
131107
storage=storage,
132108
state_group=state_group,
133109
state_group_before_event=state_group_before_event,
110+
delta_before_after=delta_before_after,
134111
prev_group=prev_group,
135112
delta_ids=delta_ids,
136113
partial_state=partial_state,
@@ -141,11 +118,7 @@ def for_outlier(
141118
storage: "Storage",
142119
) -> "EventContext":
143120
"""Return an EventContext instance suitable for persisting an outlier event"""
144-
return EventContext(
145-
storage=storage,
146-
current_state_ids={},
147-
prev_state_ids={},
148-
)
121+
return EventContext(storage=storage)
149122

150123
async def serialize(self, event: EventBase, store: "DataStore") -> JsonDict:
151124
"""Converts self to a type that can be serialized as JSON, and then
@@ -163,6 +136,7 @@ async def serialize(self, event: EventBase, store: "DataStore") -> JsonDict:
163136
"state_group_before_event": self.state_group_before_event,
164137
"rejected": self.rejected,
165138
"prev_group": self.prev_group,
139+
"delta_before_after": _encode_state_dict(self._delta_before_after),
166140
"delta_ids": _encode_state_dict(self.delta_ids),
167141
"app_service_id": self.app_service.id if self.app_service else None,
168142
"partial_state": self.partial_state,
@@ -187,6 +161,7 @@ def deserialize(storage: "Storage", input: JsonDict) -> "EventContext":
187161
state_group=input["state_group"],
188162
state_group_before_event=input["state_group_before_event"],
189163
prev_group=input["prev_group"],
164+
delta_before_after=_decode_state_dict(input["delta_before_after"]),
190165
delta_ids=_decode_state_dict(input["delta_ids"]),
191166
rejected=input["rejected"],
192167
partial_state=input.get("partial_state", False),
@@ -234,10 +209,15 @@ async def get_current_state_ids(self) -> Optional[StateMap[str]]:
234209
if self.rejected:
235210
raise RuntimeError("Attempt to access state_ids of rejected event")
236211

237-
if self._state_group is None:
238-
return None
212+
assert self._delta_before_after is not None
213+
214+
prev_state_ids = await self.get_prev_state_ids()
215+
216+
if self._delta_before_after:
217+
prev_state_ids = dict(prev_state_ids)
218+
prev_state_ids.update(self._delta_before_after)
239219

240-
return await self._storage.state.get_state_ids_for_group(self._state_group)
220+
return prev_state_ids
241221

242222
async def get_prev_state_ids(self) -> StateMap[str]:
243223
"""
@@ -252,7 +232,7 @@ async def get_prev_state_ids(self) -> StateMap[str]:
252232
Maps a (type, state_key) to the event ID of the state event matching
253233
this tuple.
254234
"""
255-
assert self.state_group_before_event
235+
assert self.state_group_before_event is not None
256236
return await self._storage.state.get_state_ids_for_group(
257237
self.state_group_before_event
258238
)

synapse/handlers/federation_event.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,8 +1877,7 @@ async def _update_context_for_auth_events(
18771877
storage=self._storage,
18781878
state_group=state_group,
18791879
state_group_before_event=context.state_group_before_event,
1880-
current_state_ids=current_state_ids,
1881-
prev_state_ids=prev_state_ids,
1880+
delta_before_after=state_updates,
18821881
prev_group=prev_group,
18831882
delta_ids=state_updates,
18841883
partial_state=context.partial_state,

synapse/handlers/message.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,10 @@ async def deduplicate_state_event(
757757
The previous version of the event is returned, if it is found in the
758758
event context. Otherwise, None is returned.
759759
"""
760+
if event.internal_metadata.is_outlier():
761+
# This can happen due to out of band memberships
762+
return None
763+
760764
prev_state_ids = await context.get_prev_state_ids()
761765
prev_event_id = prev_state_ids.get((event.type, event.state_key))
762766
if not prev_event_id:

synapse/push/action_generator.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,9 @@ def __init__(self, hs: "HomeServer"):
4040
async def handle_push_actions_for_event(
4141
self, event: EventBase, context: EventContext
4242
) -> None:
43+
if event.internal_metadata.is_outlier():
44+
# This can happen due to out of band memberships
45+
return
46+
4347
with Measure(self.clock, "action_for_event_by_user"):
4448
await self.bulk_evaluator.action_for_event_by_user(event, context)

synapse/state/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,7 @@ async def compute_event_context(
365365
storage=self._storage,
366366
state_group_before_event=state_group_before_event,
367367
state_group=state_group_before_event,
368-
current_state_ids=state_ids_before_event,
369-
prev_state_ids=state_ids_before_event,
368+
delta_before_after={},
370369
prev_group=state_group_before_event_prev_group,
371370
delta_ids=deltas_to_state_group_before_event,
372371
partial_state=partial_state,
@@ -398,8 +397,7 @@ async def compute_event_context(
398397
storage=self._storage,
399398
state_group=state_group_after_event,
400399
state_group_before_event=state_group_before_event,
401-
current_state_ids=state_ids_after_event,
402-
prev_state_ids=state_ids_before_event,
400+
delta_before_after=delta_ids,
403401
prev_group=state_group_before_event,
404402
delta_ids=delta_ids,
405403
partial_state=partial_state,

0 commit comments

Comments
 (0)