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

Don't pull out the full state when storing state #13274

Merged
merged 14 commits into from
Jul 15, 2022
6 changes: 4 additions & 2 deletions synapse/state/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,11 @@ async def compute_event_context(
deltas_to_state_group_before_event = entry.delta_ids

# We make sure that we have a state group assigned to the state.
state_ids_before_event = None
erikjohnston marked this conversation as resolved.
Show resolved Hide resolved
if entry.state_group is None:
state_ids_before_event = None
# store_state_group requires us to have either a previous state group
# (with deltas) or the complete state map. So, if we don't have a
# previous state group, load the complete state map now.
if state_group_before_event_prev_group is None:
erikjohnston marked this conversation as resolved.
Show resolved Hide resolved
state_ids_before_event = await entry.get_state(
self._state_storage_controller, StateFilter.all()
Expand All @@ -312,7 +315,6 @@ async def compute_event_context(
entry.state_group = state_group_before_event
else:
state_group_before_event = entry.state_group
state_ids_before_event = None

#
# now if it's not a state event, we're done
Expand Down
13 changes: 11 additions & 2 deletions synapse/storage/databases/state/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,9 @@ async def store_state_group(
) -> int:
"""Store a new set of state, returning a newly assigned state group.

If `current_state_ids` is None then `prev_group` and `delta_ids` must
not be None.
erikjohnston marked this conversation as resolved.
Show resolved Hide resolved

Args:
event_id: The event ID for which the state was calculated
room_id
erikjohnston marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -427,7 +430,9 @@ async def store_state_group(
def insert_delta_group_txn(
txn: LoggingTransaction, prev_group: int, delta_ids: StateMap[str]
) -> Optional[int]:
"""If we have a delta we try and persist the new group as a delta.
"""Try and persist the new group as a delta.

Requires that we have the state as a delta from a previous state group.

Returns:
The state group if successfully created, or None if the state
Expand All @@ -446,8 +451,9 @@ def insert_delta_group_txn(
% (prev_group,)
)

# if the chain of state group deltas is going too long, we fall back to
# persisting a complete state group.
potential_hops = self._count_state_group_hops_txn(txn, prev_group)

if potential_hops >= MAX_STATE_DELTA_HOPS:
return None

Expand Down Expand Up @@ -540,6 +546,9 @@ def insert_full_state_txn(
if state_group is not None:
return state_group

# We're going to persist the state as a complete group rather than
# a delta, so first we need to ensure we have loaded the state map
# from the database.
if current_state_ids is None:
erikjohnston marked this conversation as resolved.
Show resolved Hide resolved
assert prev_group is not None
assert delta_ids is not None
Expand Down