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

Add a class UnpersistedEventContext to allow for the batching up of storing state groups #14675

Merged
merged 32 commits into from
Feb 9, 2023
Merged
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
286469f
add class UnpersistedEventContext
H-Shay Dec 13, 2022
6322dac
modify create new client event to create unpersistedeventcontexts
H-Shay Dec 13, 2022
54fa7f0
persist event contexts after creation
H-Shay Dec 13, 2022
d23d1be
fix tests to persist unpersisted event contexts
H-Shay Dec 13, 2022
b483243
cleanup
H-Shay Dec 13, 2022
c008b6f
misc lints + cleanup
H-Shay Dec 13, 2022
c6ef7bb
Merge branch 'develop' into shay/rework_event_context
H-Shay Dec 13, 2022
0f07c0e
changelog + fix comments
H-Shay Dec 13, 2022
a66cc71
Merge branch 'shay/rework_event_context' of https://github.com/matrix…
H-Shay Dec 13, 2022
78a71ed
lints
H-Shay Dec 13, 2022
6321576
fix batch insertion?
H-Shay Dec 14, 2022
d6454ce
reduce redundant calculation
H-Shay Dec 14, 2022
8b99436
add unpersisted event classes
H-Shay Jan 9, 2023
1668fd2
rework compute_event_context, split into function that returns unpers…
H-Shay Jan 9, 2023
7e494fc
use calculate_context_info to create unpersisted event contexts
H-Shay Jan 9, 2023
61c8de0
update typing
H-Shay Jan 9, 2023
ed2e305
Merge branch 'develop' into shay/rework_event_context
H-Shay Jan 9, 2023
f7eafcf
$%#^&*
H-Shay Jan 10, 2023
0402917
black
H-Shay Jan 10, 2023
373e331
fix comments and consolidate classes, use attr.s for class
H-Shay Jan 12, 2023
408fa21
requested changes
H-Shay Jan 12, 2023
345d746
lint
H-Shay Jan 12, 2023
b9ee9d3
Merge branch 'develop' into shay/rework_event_context
H-Shay Jan 20, 2023
cdce9a2
requested changes
H-Shay Jan 20, 2023
b69a87c
requested changes
H-Shay Jan 23, 2023
8830c94
refactor to be stupidly explicit
H-Shay Jan 26, 2023
d4c380d
Merge branch 'develop' into shay/rework_event_context
H-Shay Jan 26, 2023
f439b23
Merge branch 'develop' into shay/rework_event_context
erikjohnston Feb 7, 2023
3614ed0
clearer renaming and flow
H-Shay Feb 8, 2023
a0bcc0a
Merge branch 'shay/rework_event_context' of https://github.com/matrix…
H-Shay Feb 8, 2023
427d007
make partial state non-optional
H-Shay Feb 8, 2023
c3bdb6e
update docstrings
H-Shay Feb 9, 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
79 changes: 79 additions & 0 deletions synapse/events/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
import attr
from frozendict import frozendict

from synapse.api.constants import EventTypes
from synapse.appservice import ApplicationService
from synapse.events import EventBase
from synapse.types import JsonDict, StateMap

if TYPE_CHECKING:
from synapse.state import StateHandler
from synapse.storage.controllers import StorageControllers
from synapse.storage.databases.main import DataStore
from synapse.storage.state import StateFilter
Expand Down Expand Up @@ -254,6 +256,83 @@ async def get_prev_state_ids(
)


@attr.s(slots=True, auto_attribs=True)
class UnpersistedEventContext:
"""
This is a version of an EventContext before the new state group (if any) has been
computed and stored. It contains information about the state before the event (which
also may be the information after the event, if the event is not a state event). The
UnpersistedEventContext must be converted into an EventContext by calling the method
'persist' on it before it is suitable to be sent to the DB for processing.

state_handler:
an instance of the class StateHandler, this is required for all
but outlier instances
state_group_before_event:
the state group at/before the event. This is required if `for_batch` is True.
state_map:
the current state of the room
for_batch:
whether the context is part of a group of events being created for batch persisting
to the DB.
app_service: If this event is being sent by a (local) application service, that
app service.

"""
_storage: "StorageControllers"
state_handler: Optional["StateHandler"] = None
state_group_before_event: Optional[int] = None
state_map: Optional[StateMap[str]] = None
for_batch: bool = False
app_service: Optional[ApplicationService] = None

@staticmethod
def for_outlier(
storage: "StorageControllers",
) -> "UnpersistedEventContext":
"""Returns an UnpersistedEventContext instance suitable for an outlier event"""
return UnpersistedEventContext(storage=storage)

async def persist(
self,
event: EventBase,
) -> "EventContext":
"""
Converts an UnpersistedEventContext into an EventContext, which is now suitable for
sending to the DB along with the event.
"""
if event.internal_metadata.is_outlier():
return EventContext.for_outlier(self._storage)

assert self.state_handler is not None
if self.for_batch:
assert self.state_group_before_event is not None
assert self.state_map is not None
return await self.state_handler.compute_event_context_for_batched(
event, self.state_map, self.state_group_before_event
)
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
elif event.type == EventTypes.MSC2716_INSERTION:
return await self.state_handler.compute_event_context(
event, state_ids_before_event=self.state_map, partial_state=False
)
return await self.state_handler.compute_event_context(event)

async def get_prev_state_ids(
self, state_filter: Optional["StateFilter"] = None
) -> StateMap[str]:
"""
Returns the state at/before the event (which also may be the same as after the event
if the event is not a state event.)
"""
if self.state_map is not None:
return self.state_map
else:
assert self.state_group_before_event is not None
return await self._storage.state.get_state_ids_for_group(
self.state_group_before_event, state_filter
)
H-Shay marked this conversation as resolved.
Show resolved Hide resolved


def _encode_state_dict(
state_dict: Optional[StateMap[str]],
) -> Optional[List[Tuple[str, str, str]]]:
Expand Down