This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
Add v2 state resolution algorithm #4040
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e238013
Add v2 state res algorithm.
erikjohnston 6bd856c
User event.sender rather than alias event.user_id
erikjohnston 947c744
Add some state res v2 tests
erikjohnston fc95496
Newsfile
erikjohnston 1513347
Fix up use of resolve_events_with_factory
erikjohnston 4a28d3d
Update event_auth table for rejected events
erikjohnston 810715f
Rename resolve_events_with_factory
erikjohnston dacbeb2
Comment
erikjohnston b313b9b
isort
erikjohnston File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 @@ | ||
Add initial implementation of new state resolution algorithm |
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 |
---|---|---|
|
@@ -19,13 +19,14 @@ | |
|
||
from six import iteritems, itervalues | ||
|
||
import attr | ||
from frozendict import frozendict | ||
|
||
from twisted.internet import defer | ||
|
||
from synapse.api.constants import EventTypes, RoomVersions | ||
from synapse.events.snapshot import EventContext | ||
from synapse.state import v1 | ||
from synapse.state import v1, v2 | ||
from synapse.util.async_helpers import Linearizer | ||
from synapse.util.caches import get_cache_factor_for | ||
from synapse.util.caches.expiringcache import ExpiringCache | ||
|
@@ -372,15 +373,10 @@ def resolve_state_groups_for_events(self, room_id, event_ids): | |
|
||
result = yield self._state_resolution_handler.resolve_state_groups( | ||
room_id, room_version, state_groups_ids, None, | ||
self._state_map_factory, | ||
state_res_store=StateResolutionStore(self.store), | ||
) | ||
defer.returnValue(result) | ||
|
||
def _state_map_factory(self, ev_ids): | ||
return self.store.get_events( | ||
ev_ids, get_prev_content=False, check_redacted=False, | ||
) | ||
|
||
@defer.inlineCallbacks | ||
def resolve_events(self, room_version, state_sets, event): | ||
logger.info( | ||
|
@@ -401,7 +397,7 @@ def resolve_events(self, room_version, state_sets, event): | |
new_state = yield resolve_events_with_factory( | ||
room_version, state_set_ids, | ||
event_map=state_map, | ||
state_map_factory=self._state_map_factory | ||
state_res_store=StateResolutionStore(self.store), | ||
) | ||
|
||
new_state = { | ||
|
@@ -436,7 +432,7 @@ def __init__(self, hs): | |
@defer.inlineCallbacks | ||
@log_function | ||
def resolve_state_groups( | ||
self, room_id, room_version, state_groups_ids, event_map, state_map_factory, | ||
self, room_id, room_version, state_groups_ids, event_map, state_res_store, | ||
): | ||
"""Resolves conflicts between a set of state groups | ||
|
||
|
@@ -454,9 +450,11 @@ def resolve_state_groups( | |
a dict from event_id to event, for any events that we happen to | ||
have in flight (eg, those currently being persisted). This will be | ||
used as a starting point fof finding the state we need; any missing | ||
events will be requested via state_map_factory. | ||
events will be requested via state_res_store. | ||
|
||
If None, all events will be fetched via state_res_store. | ||
|
||
If None, all events will be fetched via state_map_factory. | ||
state_res_store (StateResolutionStore) | ||
|
||
Returns: | ||
Deferred[_StateCacheEntry]: resolved state | ||
|
@@ -502,7 +500,7 @@ def resolve_state_groups( | |
room_version, | ||
list(itervalues(state_groups_ids)), | ||
event_map=event_map, | ||
state_map_factory=state_map_factory, | ||
state_res_store=state_res_store, | ||
) | ||
|
||
# if the new state matches any of the input state groups, we can | ||
|
@@ -583,7 +581,7 @@ def _make_state_cache_entry( | |
) | ||
|
||
|
||
def resolve_events_with_factory(room_version, state_sets, event_map, state_map_factory): | ||
def resolve_events_with_factory(room_version, state_sets, event_map, state_res_store): | ||
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. can we rename this now? 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. (and the v2 impl) |
||
""" | ||
Args: | ||
room_version(str): Version of the room | ||
|
@@ -599,21 +597,74 @@ def resolve_events_with_factory(room_version, state_sets, event_map, state_map_f | |
|
||
If None, all events will be fetched via state_map_factory. | ||
|
||
state_map_factory(func): will be called | ||
with a list of event_ids that are needed, and should return with | ||
a Deferred of dict of event_id to event. | ||
state_res_store (StateResolutionStore) | ||
|
||
Returns | ||
Deferred[dict[(str, str), str]]: | ||
a map from (type, state_key) to event_id. | ||
""" | ||
if room_version in (RoomVersions.V1, RoomVersions.VDH_TEST,): | ||
if room_version == RoomVersions.V1: | ||
return v1.resolve_events_with_factory( | ||
state_sets, event_map, state_map_factory, | ||
state_sets, event_map, state_res_store.get_events, | ||
) | ||
elif room_version == RoomVersions.VDH_TEST: | ||
return v2.resolve_events_with_factory( | ||
state_sets, event_map, state_res_store, | ||
) | ||
else: | ||
# This should only happen if we added a version but forgot to add it to | ||
# the list above. | ||
raise Exception( | ||
"No state resolution algorithm defined for version %r" % (room_version,) | ||
) | ||
|
||
|
||
@attr.s | ||
class StateResolutionStore(object): | ||
"""Interface that allows state resolution algorithms to access the database | ||
in well defined way. | ||
|
||
Args: | ||
store (DataStore) | ||
""" | ||
|
||
store = attr.ib() | ||
|
||
def get_events(self, event_ids, allow_rejected=False): | ||
"""Get events from the database | ||
|
||
Args: | ||
event_ids (list): The event_ids of the events to fetch | ||
allow_rejected (bool): If True return rejected events. | ||
|
||
Returns: | ||
Deferred[dict[str, FrozenEvent]]: Dict from event_id to event. | ||
""" | ||
|
||
return self.store.get_events( | ||
event_ids, | ||
check_redacted=False, | ||
get_prev_content=False, | ||
allow_rejected=allow_rejected, | ||
) | ||
|
||
def get_auth_chain(self, event_ids): | ||
"""Gets the full auth chain for a set of events (including rejected | ||
events). | ||
|
||
Includes the given event IDs in the result. | ||
|
||
Note that: | ||
1. All events must be state events. | ||
2. For v1 rooms this may not have the full auth chain in the | ||
presence of rejected events | ||
|
||
Args: | ||
event_ids (list): The event IDs of the events to fetch the auth | ||
chain for. Must be state events. | ||
|
||
Returns: | ||
Deferred[list[str]]: List of event IDs of the auth chain. | ||
""" | ||
|
||
return self.store.get_auth_chain_ids(event_ids, include_given=True) |
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.
this could do with a comment (now that the one at line 393 has gone)