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
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions synapse/events/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ class UnpersistedEventContext:
app service.

"""

_storage: "StorageControllers"
state_handler: Optional["StateHandler"] = None
state_group_before_event: Optional[int] = None
Expand Down
4 changes: 2 additions & 2 deletions synapse/events/third_party_rules.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.
import logging
from typing import TYPE_CHECKING, Any, Awaitable, Callable, List, Optional, Tuple
from typing import TYPE_CHECKING, Any, Awaitable, Callable, List, Optional, Tuple, Union

from twisted.internet.defer import CancelledError

Expand Down Expand Up @@ -231,7 +231,7 @@ def register_third_party_rules_callbacks(
self._on_threepid_bind_callbacks.append(on_threepid_bind)

async def check_event_allowed(
self, event: EventBase, context: EventContext
self, event: EventBase, context: Union[EventContext, UnpersistedEventContext]
) -> Tuple[bool, Optional[dict]]:
"""Check if a provided event should be allowed in the given context.

Expand Down
15 changes: 11 additions & 4 deletions synapse/handlers/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,15 +935,20 @@ async def on_make_join_request(
)

try:
event, context = await self.event_creation_handler.create_new_client_event(
(
event,
unpersisted_context,
) = await self.event_creation_handler.create_new_client_event(
builder=builder
)
except SynapseError as e:
logger.warning("Failed to create join to %s because %s", room_id, e)
raise

# Ensure the user can even join the room.
await self._federation_event_handler.check_join_restrictions(context, event)
await self._federation_event_handler.check_join_restrictions(
unpersisted_context, event
)

# The remote hasn't signed it yet, obviously. We'll do the full checks
# when we get the event back in `on_send_join_request`
Expand Down Expand Up @@ -1173,7 +1178,6 @@ async def on_make_knock_request(
},
)


(
event,
unpersisted_context,
Expand Down Expand Up @@ -1345,7 +1349,10 @@ async def exchange_third_party_invite(
)

EventValidator().validate_builder(builder)
event, unpersisted_context = await self.event_creation_handler.create_new_client_event(
(
event,
unpersisted_context,
) = await self.event_creation_handler.create_new_client_event(
builder=builder
)

Expand Down
5 changes: 3 additions & 2 deletions synapse/handlers/federation_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
Sequence,
Set,
Tuple,
Union,
)

from prometheus_client import Counter, Histogram
Expand Down Expand Up @@ -57,7 +58,7 @@
validate_event_for_room_version,
)
from synapse.events import EventBase
from synapse.events.snapshot import EventContext
from synapse.events.snapshot import EventContext, UnpersistedEventContext
from synapse.federation.federation_client import InvalidResponseError, PulledPduInfo
from synapse.logging.context import nested_logging_context
from synapse.logging.opentracing import (
Expand Down Expand Up @@ -424,7 +425,7 @@ async def on_send_membership_event(
return event, context

async def check_join_restrictions(
self, context: EventContext, event: EventBase
self, context: Union[EventContext, UnpersistedEventContext], event: EventBase
) -> None:
"""Check that restrictions in restricted join rules are matched

Expand Down
16 changes: 6 additions & 10 deletions synapse/handlers/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,6 @@ async def create_event(

builder.internal_metadata.historical = historical


event, unpersisted_context = await self.create_new_client_event(
builder=builder,
requester=requester,
Expand Down Expand Up @@ -1073,7 +1072,7 @@ async def create_new_client_event(
state_map: Optional[StateMap[str]] = None,
for_batch: bool = False,
current_state_group: Optional[int] = None,
) -> Tuple[EventBase, EventContext]:
) -> Tuple[EventBase, UnpersistedEventContext]:
"""Create a new event for a local client. If bool for_batch is true, will
create an event using the prev_event_ids, and will create an event context for
the event using the parameters state_map and current_state_group, thus these parameters
Expand Down Expand Up @@ -1201,7 +1200,7 @@ async def create_new_client_event(
# after it is created
if builder.internal_metadata.outlier:
event.internal_metadata.outlier = True
context = EventContext.for_outlier(self._storage_controllers)
context = UnpersistedEventContext.for_outlier(self._storage_controllers)
elif (
event.type == EventTypes.MSC2716_INSERTION
and state_event_ids
Expand Down Expand Up @@ -1287,7 +1286,7 @@ async def create_new_client_event(
elif new_content is not None:
# the third-party rules want to replace the event. We'll need to build a new
# event.
event, context = await self._rebuild_event_after_third_party_rules(
event = await self._rebuild_event_after_third_party_rules(
new_content, event
)

Expand Down Expand Up @@ -2076,9 +2075,9 @@ def _expire_rooms_to_exclude_from_dummy_event_insertion(self) -> None:

async def _rebuild_event_after_third_party_rules(
self, third_party_result: dict, original_event: EventBase
) -> Tuple[EventBase, EventContext]:
) -> EventBase:
# the third_party_event_rules want to replace the event.
# we do some basic checks, and then return the replacement event and context.
# we do some basic checks, and then return the replacement event.

# Construct a new EventBuilder and validate it, which helps with the
# rest of these checks.
Expand Down Expand Up @@ -2130,7 +2129,4 @@ async def _rebuild_event_after_third_party_rules(
auth_event_ids=None,
)

# we rebuild the event context, to be on the safe side. If nothing else,
# delta_ids might need an update.
context = await self.state.compute_event_context(event)
return event, context
return event