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

Replace EventContext fields prev_group and delta_ids with field state_group_deltas #15233

Merged
merged 28 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
bbaf807
remove fields prev_group and delta_ids and add field state_group_deltas
H-Shay Mar 9, 2023
e89cbb6
check context.state_group_deltas for possible state delta
H-Shay Mar 9, 2023
82ea621
update tests
H-Shay Mar 9, 2023
e8b8186
newsfragment
H-Shay Mar 9, 2023
d6e858c
fix lints
H-Shay Mar 9, 2023
6486e1f
properly serialize/deserialize context.state_group_deltas
H-Shay Mar 9, 2023
430ea51
lint
H-Shay Mar 9, 2023
59cbe10
fix changelog
H-Shay Mar 15, 2023
1f19617
Merge branch 'develop' into shay/kill_prev_with_fire
H-Shay May 2, 2023
75ad4a1
Merge branch 'develop' into shay/kill_prev_with_fire
H-Shay May 10, 2023
492968c
clarify state_group_deltas and create function to build them
H-Shay May 16, 2023
60765d9
fix mypy
H-Shay May 16, 2023
3a2ea21
remove extraneous space
H-Shay May 16, 2023
10c853e
Update synapse/events/snapshot.py
H-Shay May 17, 2023
0784641
Update synapse/events/snapshot.py
H-Shay May 17, 2023
91504ae
refactors
H-Shay May 17, 2023
839af7a
Merge branch 'shay/kill_prev_with_fire' of https://github.com/matrix-…
H-Shay May 17, 2023
9e9df0c
make state group deltas non-optional
H-Shay May 17, 2023
962ec1c
add upgrade notes about incompatible versions over replication
H-Shay May 17, 2023
effa597
Merge branch 'develop' into shay/kill_prev_with_fire
H-Shay May 17, 2023
28b025b
Update upgrade.md
H-Shay May 17, 2023
6ac912a
Update upgrade.md
H-Shay May 17, 2023
b5ed756
fix stupid errors
H-Shay May 18, 2023
77a58db
formatting/doc change
H-Shay May 18, 2023
5277a75
Merge branch 'develop' into shay/kill_prev_with_fire
H-Shay Jun 6, 2023
fa0a671
fix numbers
H-Shay Jun 6, 2023
49866ea
make serialization backwards/forwards compatible
H-Shay Jun 12, 2023
1b8769a
fix stray space upgrade.md
H-Shay Jun 12, 2023
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
1 change: 1 addition & 0 deletions changelog.d/15233.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Replace EventContext fields prev_group and delta_ids with field state_group_deltas.
H-Shay marked this conversation as resolved.
Show resolved Hide resolved
133 changes: 80 additions & 53 deletions synapse/events/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, List, Optional, Tuple
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple

import attr
from frozendict import frozendict
Expand Down Expand Up @@ -106,21 +106,9 @@ class EventContext(UnpersistedEventContextBase):
state_delta_due_to_event: If `state_group` and `state_group_before_event` are not None
then this is the delta of the state between the two groups.

prev_group: If it is known, ``state_group``'s prev_group. Note that this being
None does not necessarily mean that ``state_group`` does not have
a prev_group!

If the event is a state event, this is normally the same as
``state_group_before_event``.

If ``state_group`` is None (ie, the event is an outlier), ``prev_group``
will always also be ``None``.

Note that this *not* (necessarily) the state group associated with
``_prev_state_ids``.

delta_ids: If ``prev_group`` is not None, the state delta between ``prev_group``
and ``state_group``.
state_group_deltas: a dict containing information about the state groups associated with
the event and the state delta between these groups, ie a map from
(prev state group, new state group) -> delta state dict
clokep marked this conversation as resolved.
Show resolved Hide resolved

partial_state: if True, we may be storing this event with a temporary,
incomplete state.
Expand All @@ -131,8 +119,9 @@ class EventContext(UnpersistedEventContextBase):
_state_group: Optional[int] = None
state_group_before_event: Optional[int] = None
_state_delta_due_to_event: Optional[StateMap[str]] = None
prev_group: Optional[int] = None
delta_ids: Optional[StateMap[str]] = None
state_group_deltas: Optional[
Dict[Tuple[Optional[int], Optional[int]], Optional[StateMap[str]]]
H-Shay marked this conversation as resolved.
Show resolved Hide resolved
] = None
app_service: Optional[ApplicationService] = None

partial_state: bool = False
Expand All @@ -144,16 +133,16 @@ def with_state(
state_group_before_event: Optional[int],
state_delta_due_to_event: Optional[StateMap[str]],
partial_state: bool,
prev_group: Optional[int] = None,
delta_ids: Optional[StateMap[str]] = None,
state_group_deltas: Optional[
Dict[Tuple[Optional[int], Optional[int]], Optional[StateMap[str]]]
],
) -> "EventContext":
return EventContext(
storage=storage,
state_group=state_group,
state_group_before_event=state_group_before_event,
state_delta_due_to_event=state_delta_due_to_event,
prev_group=prev_group,
delta_ids=delta_ids,
state_group_deltas=state_group_deltas,
partial_state=partial_state,
)

Expand All @@ -177,16 +166,14 @@ async def serialize(self, event: EventBase, store: "DataStore") -> JsonDict:
Returns:
The serialized event.
"""

return {
"state_group": self._state_group,
"state_group_before_event": self.state_group_before_event,
"rejected": self.rejected,
"prev_group": self.prev_group,
"state_group_deltas": _encode_state_group_delta(self.state_group_deltas),
"state_delta_due_to_event": _encode_state_dict(
self._state_delta_due_to_event
),
"delta_ids": _encode_state_dict(self.delta_ids),
"app_service_id": self.app_service.id if self.app_service else None,
"partial_state": self.partial_state,
}
Expand All @@ -209,11 +196,10 @@ def deserialize(storage: "StorageControllers", input: JsonDict) -> "EventContext
storage=storage,
state_group=input["state_group"],
state_group_before_event=input["state_group_before_event"],
prev_group=input["prev_group"],
state_group_deltas=_decode_state_group_delta(input["state_group_deltas"]),
clokep marked this conversation as resolved.
Show resolved Hide resolved
state_delta_due_to_event=_decode_state_dict(
input["state_delta_due_to_event"]
),
delta_ids=_decode_state_dict(input["delta_ids"]),
rejected=input["rejected"],
partial_state=input.get("partial_state", False),
)
Expand Down Expand Up @@ -344,7 +330,7 @@ class UnpersistedEventContext(UnpersistedEventContextBase):
_storage: "StorageControllers"
state_group_before_event: Optional[int]
state_group_after_event: Optional[int]
state_delta_due_to_event: Optional[dict]
state_delta_due_to_event: Optional[StateMap[str]]
prev_group_for_state_group_before_event: Optional[int]
delta_ids_to_state_group_before_event: Optional[StateMap[str]]
partial_state: bool
Expand Down Expand Up @@ -375,26 +361,24 @@ async def batch_persist_unpersisted_contexts(

events_and_persisted_context = []
for event, unpersisted_context in amended_events_and_context:
if event.is_state():
context = EventContext(
storage=unpersisted_context._storage,
state_group=unpersisted_context.state_group_after_event,
state_group_before_event=unpersisted_context.state_group_before_event,
state_delta_due_to_event=unpersisted_context.state_delta_due_to_event,
partial_state=unpersisted_context.partial_state,
prev_group=unpersisted_context.state_group_before_event,
delta_ids=unpersisted_context.state_delta_due_to_event,
)
else:
context = EventContext(
storage=unpersisted_context._storage,
state_group=unpersisted_context.state_group_after_event,
state_group_before_event=unpersisted_context.state_group_before_event,
state_delta_due_to_event=unpersisted_context.state_delta_due_to_event,
partial_state=unpersisted_context.partial_state,
prev_group=unpersisted_context.prev_group_for_state_group_before_event,
delta_ids=unpersisted_context.delta_ids_to_state_group_before_event,
)
state_group_deltas = {
(
unpersisted_context.state_group_before_event,
unpersisted_context.state_group_after_event,
): unpersisted_context.state_delta_due_to_event,
(
unpersisted_context.prev_group_for_state_group_before_event,
unpersisted_context.state_group_before_event,
): unpersisted_context.delta_ids_to_state_group_before_event,
}
context = EventContext(
storage=unpersisted_context._storage,
state_group=unpersisted_context.state_group_after_event,
state_group_before_event=unpersisted_context.state_group_before_event,
state_delta_due_to_event=unpersisted_context.state_delta_due_to_event,
partial_state=unpersisted_context.partial_state,
state_group_deltas=state_group_deltas,
)
events_and_persisted_context.append((event, context))
return events_and_persisted_context

Expand Down Expand Up @@ -447,26 +431,37 @@ async def persist(self, event: EventBase) -> EventContext:

# if the event isn't a state event the state group doesn't change
if not self.state_delta_due_to_event:
state_group_after_event = self.state_group_before_event
self.state_group_after_event = self.state_group_before_event

# otherwise if it is a state event we need to get a state group for it
else:
state_group_after_event = await self._storage.state.store_state_group(
self.state_group_after_event = await self._storage.state.store_state_group(
event.event_id,
event.room_id,
prev_group=self.state_group_before_event,
delta_ids=self.state_delta_due_to_event,
current_state_ids=None,
)
H-Shay marked this conversation as resolved.
Show resolved Hide resolved
state_group_deltas: Optional[
Dict[Tuple[Optional[int], Optional[int]], Optional[StateMap[str]]]
] = {
(
self.state_group_before_event,
self.state_group_after_event,
): self.state_delta_due_to_event,
(
self.prev_group_for_state_group_before_event,
self.state_group_before_event,
): self.delta_ids_to_state_group_before_event,
}

return EventContext.with_state(
storage=self._storage,
state_group=state_group_after_event,
state_group=self.state_group_after_event,
state_group_before_event=self.state_group_before_event,
state_delta_due_to_event=self.state_delta_due_to_event,
state_group_deltas=state_group_deltas,
partial_state=self.partial_state,
prev_group=self.state_group_before_event,
delta_ids=self.state_delta_due_to_event,
)


Expand All @@ -482,6 +477,38 @@ def _encode_state_dict(
return [(etype, state_key, v) for (etype, state_key), v in state_dict.items()]


def _encode_state_group_delta(
state_group_delta: Optional[
Dict[Tuple[Optional[int], Optional[int]], Optional[StateMap[str]]]
]
) -> Optional[
List[Tuple[Optional[int], Optional[int], Optional[List[Tuple[str, str, str]]]]]
]:
if state_group_delta is None:
return None

state_group_delta_encoded = []
for key, value in state_group_delta.items():
state_group_delta_encoded.append((key[0], key[1], _encode_state_dict(value)))

return state_group_delta_encoded


def _decode_state_group_delta(
input: Optional[
List[Tuple[Optional[int], Optional[int], Optional[List[Tuple[str, str, str]]]]]
]
) -> Optional[Dict[Tuple[Optional[int], Optional[int]], Optional[StateMap[str]]]]:
if input is None:
return None

state_group_deltas = {}
for element in input:
state_group_deltas[(element[0], element[1])] = _decode_state_dict(element[2])

return state_group_deltas


def _decode_state_dict(
H-Shay marked this conversation as resolved.
Show resolved Hide resolved
input: Optional[List[Tuple[str, str, str]]]
) -> Optional[StateMap[str]]:
Expand Down
10 changes: 4 additions & 6 deletions synapse/storage/controllers/persist_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,8 +827,6 @@ async def _get_new_state_after_events(
the new current state is only returned if we've already calculated
it.
"""
# Map from (prev state group, new state group) -> delta state dict
state_group_deltas = {}

for ev, ctx in events_context:
if ctx.state_group is None:
Expand All @@ -840,9 +838,6 @@ async def _get_new_state_after_events(
)
continue

if ctx.prev_group:
state_group_deltas[(ctx.prev_group, ctx.state_group)] = ctx.delta_ids

# We need to map the event_ids to their state groups. First, let's
# check if the event is one we're persisting, in which case we can
# pull the state group from its context.
Expand Down Expand Up @@ -894,7 +889,10 @@ async def _get_new_state_after_events(
new_state_group = next(iter(new_state_groups))
old_state_group = next(iter(old_state_groups))

delta_ids = state_group_deltas.get((old_state_group, new_state_group), None)
assert ctx.state_group_deltas is not None
delta_ids = ctx.state_group_deltas.get(
(old_state_group, new_state_group), None
)
if delta_ids is not None:
# We have a delta from the existing to new current state,
# so lets just return that.
Expand Down
3 changes: 1 addition & 2 deletions tests/events/test_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ def _check_serialize_deserialize(
self.assertEqual(
context.state_group_before_event, d_context.state_group_before_event
)
self.assertEqual(context.prev_group, d_context.prev_group)
self.assertEqual(context.delta_ids, d_context.delta_ids)
self.assertEqual(context.state_group_deltas, d_context.state_group_deltas)
self.assertEqual(context.app_service, d_context.app_service)

self.assertEqual(
Expand Down
9 changes: 7 additions & 2 deletions tests/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,9 +555,14 @@ def test_annotate_with_old_state(
)

self.assertIsNotNone(context.state_group_before_event)
H-Shay marked this conversation as resolved.
Show resolved Hide resolved
assert context.state_group_deltas is not None
self.assertEqual(
context.state_group_deltas.get(
(context.state_group_before_event, context.state_group)
),
{(event.type, event.state_key): event.event_id},
)
self.assertNotEqual(context.state_group_before_event, context.state_group)
self.assertEqual(context.state_group_before_event, context.prev_group)
self.assertEqual({("state", ""): event.event_id}, context.delta_ids)

@defer.inlineCallbacks
def test_trivial_annotate_message(
Expand Down