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

Convert EventContext to attrs #6218

Merged
merged 7 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 changelog.d/6218.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Convert EventContext to an attrs.
100 changes: 39 additions & 61 deletions synapse/events/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@
# 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.

from six import iteritems

import attr
from frozendict import frozendict

from twisted.internet import defer

from synapse.logging.context import make_deferred_yieldable, run_in_background


class EventContext(object):
@attr.s(slots=True)
class EventContext:
"""
Attributes:
state_group (int|None): state group id, if the state has been stored
Expand All @@ -31,9 +32,6 @@ class EventContext(object):
rejected (bool|str): A rejection reason if the event was rejected, else
False

push_actions (list[(str, list[object])]): list of (user_id, actions)
tuples

prev_group (int): Previously persisted state group. ``None`` for an
outlier.
delta_ids (dict[(str, str), str]): Delta from ``prev_group``.
Expand All @@ -42,6 +40,8 @@ class EventContext(object):
prev_state_events (?): XXX: is this ever set to anything other than
the empty list?

app_service: FIXME

_current_state_ids (dict[(str, str), str]|None):
The current state map including the current event. None if outlier
or we haven't fetched the state from DB yet.
Expand All @@ -67,49 +67,33 @@ class EventContext(object):
Only set when state has not been fetched yet.
"""

__slots__ = [
"state_group",
"rejected",
"prev_group",
"delta_ids",
"prev_state_events",
"app_service",
"_current_state_ids",
"_prev_state_ids",
"_prev_state_id",
"_event_type",
"_event_state_key",
"_fetching_state_deferred",
]

def __init__(self):
self.prev_state_events = []
self.rejected = False
self.app_service = None
state_group = attr.ib(default=None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a note: you can annotate the types in this with type= -- for the optional ones you want to use typing.Optional[int] or whatever. mypy can read that info, and also some editor IDEs can as well, so it can be worth it :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm, good to know. I'll consider it next time ;)

rejected = attr.ib(default=False)
prev_group = attr.ib(default=None)
delta_ids = attr.ib(default=None)
prev_state_events = attr.ib(default=attr.Factory(list))
app_service = attr.ib(default=None)

_current_state_ids = attr.ib(default=None)
_prev_state_ids = attr.ib(default=None)
_prev_state_id = attr.ib(default=None)

_event_type = attr.ib(default=None)
_event_state_key = attr.ib(default=None)
_fetching_state_deferred = attr.ib(default=None)

@staticmethod
def with_state(
state_group, current_state_ids, prev_state_ids, prev_group=None, delta_ids=None
):
context = EventContext()

# The current state including the current event
context._current_state_ids = current_state_ids
# The current state excluding the current event
context._prev_state_ids = prev_state_ids
context.state_group = state_group

context._prev_state_id = None
context._event_type = None
context._event_state_key = None
context._fetching_state_deferred = defer.succeed(None)

# A previously persisted state group and a delta between that
# and this state.
context.prev_group = prev_group
context.delta_ids = delta_ids

return context
return EventContext(
current_state_ids=current_state_ids,
prev_state_ids=prev_state_ids,
state_group=state_group,
fetching_state_deferred=defer.succeed(None),
prev_group=prev_group,
delta_ids=delta_ids,
)

@defer.inlineCallbacks
def serialize(self, event, store):
Expand Down Expand Up @@ -157,24 +141,18 @@ def deserialize(store, input):
Returns:
EventContext
"""
context = EventContext()

# We use the state_group and prev_state_id stuff to pull the
# current_state_ids out of the DB and construct prev_state_ids.
context._prev_state_id = input["prev_state_id"]
context._event_type = input["event_type"]
context._event_state_key = input["event_state_key"]

context._current_state_ids = None
context._prev_state_ids = None
context._fetching_state_deferred = None

context.state_group = input["state_group"]
context.prev_group = input["prev_group"]
context.delta_ids = _decode_state_dict(input["delta_ids"])

context.rejected = input["rejected"]
context.prev_state_events = input["prev_state_events"]
context = EventContext(
# We use the state_group and prev_state_id stuff to pull the
# current_state_ids out of the DB and construct prev_state_ids.
prev_state_id=input["prev_state_id"],
event_type=input["event_type"],
event_state_key=input["event_state_key"],
state_group=input["state_group"],
prev_group=input["prev_group"],
delta_ids=_decode_state_dict(input["delta_ids"]),
rejected=input["rejected"],
prev_state_events=input["prev_state_events"],
)

app_service_id = input["app_service_id"]
if app_service_id:
Expand Down
7 changes: 6 additions & 1 deletion synapse/storage/data_stores/main/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import logging
from collections import namedtuple
from typing import Iterable, Tuple

from six import iteritems, itervalues
from six.moves import range
Expand All @@ -23,6 +24,8 @@

from synapse.api.constants import EventTypes
from synapse.api.errors import NotFoundError
from synapse.events import EventBase
from synapse.events.snapshot import EventContext
from synapse.storage._base import SQLBaseStore
from synapse.storage.background_updates import BackgroundUpdateStore
from synapse.storage.data_stores.main.events_worker import EventsWorkerStore
Expand Down Expand Up @@ -1215,7 +1218,9 @@ class StateStore(StateGroupWorkerStore, StateBackgroundUpdateStore):
def __init__(self, db_conn, hs):
super(StateStore, self).__init__(db_conn, hs)

def _store_event_state_mappings_txn(self, txn, events_and_contexts):
def _store_event_state_mappings_txn(
self, txn, events_and_contexts: Iterable[Tuple[EventBase, EventContext]]
):
state_groups = {}
for event, context in events_and_contexts:
if event.internal_metadata.is_outlier():
Expand Down