This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Faster room joins: fix race in recalculation of current room state #13151
Merged
squahtx
merged 9 commits into
develop
from
squah/faster_room_joins_fix_current_state_recalculation_race
Jul 7, 2022
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d8a37a9
Bounce recalculation of current state to the correct event persister
db0430a
Move recalculation of current state into the event persistence queue
6738bbc
Give recalculation of a room's current state a real stream ordering
af5735c
Add newsfile
2479743
Fix tests
e03565e
Rename replication client to _update_current_state_client
7588ab5
Factor out persistence queue task merging logic into its own method
c7d6d6e
Underscore unused parameters
f8743a9
Merge branch 'develop' into squah/faster_room_joins_fix_current_state…
squahtx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Faster room joins: fix race in recalculation of current room state. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Copyright 2022 The Matrix.org Foundation C.I.C. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import logging | ||
from typing import TYPE_CHECKING, Tuple | ||
|
||
from twisted.web.server import Request | ||
|
||
from synapse.api.errors import SynapseError | ||
from synapse.http.server import HttpServer | ||
from synapse.replication.http._base import ReplicationEndpoint | ||
from synapse.types import JsonDict | ||
|
||
if TYPE_CHECKING: | ||
from synapse.server import HomeServer | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class ReplicationUpdateCurrentStateRestServlet(ReplicationEndpoint): | ||
"""Recalculates the current state for a room, and persists it. | ||
|
||
The API looks like: | ||
|
||
POST /_synapse/replication/update_current_state/:room_id | ||
|
||
{} | ||
|
||
200 OK | ||
|
||
{} | ||
""" | ||
|
||
NAME = "update_current_state" | ||
PATH_ARGS = ("room_id",) | ||
|
||
def __init__(self, hs: "HomeServer"): | ||
super().__init__(hs) | ||
|
||
self._state_handler = hs.get_state_handler() | ||
self._events_shard_config = hs.config.worker.events_shard_config | ||
self._instance_name = hs.get_instance_name() | ||
|
||
@staticmethod | ||
async def _serialize_payload(room_id: str) -> JsonDict: # type: ignore[override] | ||
return {} | ||
|
||
async def _handle_request( # type: ignore[override] | ||
self, request: Request, room_id: str | ||
) -> Tuple[int, JsonDict]: | ||
writer_instance = self._events_shard_config.get_instance(room_id) | ||
if writer_instance != self._instance_name: | ||
raise SynapseError( | ||
400, "/update_current_state request was routed to the wrong worker" | ||
) | ||
|
||
await self._state_handler.update_current_state(room_id) | ||
|
||
return 200, {} | ||
|
||
|
||
def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None: | ||
if hs.get_instance_name() in hs.config.worker.writers.events: | ||
ReplicationUpdateCurrentStateRestServlet(hs).register(http_server) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ | |
from synapse.events import EventBase | ||
from synapse.events.snapshot import EventContext | ||
from synapse.logging.context import ContextResourceUsage | ||
from synapse.replication.http.state import ReplicationUpdateCurrentStateRestServlet | ||
from synapse.state import v1, v2 | ||
from synapse.storage.databases.main.events_worker import EventRedactBehaviour | ||
from synapse.storage.roommember import ProfileInfo | ||
|
@@ -129,6 +130,12 @@ def __init__(self, hs: "HomeServer"): | |
self.hs = hs | ||
self._state_resolution_handler = hs.get_state_resolution_handler() | ||
self._storage_controllers = hs.get_storage_controllers() | ||
self._events_shard_config = hs.config.worker.events_shard_config | ||
self._instance_name = hs.get_instance_name() | ||
|
||
self._update_current_state = ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be worth naming this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renamed! |
||
ReplicationUpdateCurrentStateRestServlet.make_client(hs) | ||
) | ||
|
||
async def get_current_state_ids( | ||
self, | ||
|
@@ -417,6 +424,24 @@ async def resolve_events( | |
|
||
return {key: state_map[ev_id] for key, ev_id in new_state.items()} | ||
|
||
async def update_current_state(self, room_id: str) -> None: | ||
"""Recalculates the current state for a room, and persists it. | ||
|
||
Raises: | ||
SynapseError(502): if all attempts to connect to the event persister worker | ||
fail | ||
""" | ||
writer_instance = self._events_shard_config.get_instance(room_id) | ||
if writer_instance != self._instance_name: | ||
await self._update_current_state( | ||
instance_name=writer_instance, | ||
room_id=room_id, | ||
) | ||
return | ||
|
||
assert self._storage_controllers.persistence is not None | ||
await self._storage_controllers.persistence.update_current_state(room_id) | ||
|
||
|
||
@attr.s(slots=True, auto_attribs=True) | ||
class _StateResMetrics: | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can drop this since returning an empty dict is the default implementation in the base class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Python complains because the base implementation is an
abstractmethod
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh boo