Convert EventContext to attrs (#6218)

* make EventContext use an attr
This commit is contained in:
Richard van der Hoff 2019-10-28 12:29:55 +00:00 committed by Amber Brown
parent da78f61778
commit 9aee28927b
3 changed files with 45 additions and 61 deletions

1
changelog.d/6218.misc Normal file
View File

@ -0,0 +1 @@
Convert EventContext to an attrs.

View File

@ -12,9 +12,9 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from six import iteritems from six import iteritems
import attr
from frozendict import frozendict from frozendict import frozendict
from twisted.internet import defer from twisted.internet import defer
@ -22,7 +22,8 @@ from twisted.internet import defer
from synapse.logging.context import make_deferred_yieldable, run_in_background from synapse.logging.context import make_deferred_yieldable, run_in_background
class EventContext(object): @attr.s(slots=True)
class EventContext:
""" """
Attributes: Attributes:
state_group (int|None): state group id, if the state has been stored state_group (int|None): state group id, if the state has been stored
@ -31,9 +32,6 @@ class EventContext(object):
rejected (bool|str): A rejection reason if the event was rejected, else rejected (bool|str): A rejection reason if the event was rejected, else
False False
push_actions (list[(str, list[object])]): list of (user_id, actions)
tuples
prev_group (int): Previously persisted state group. ``None`` for an prev_group (int): Previously persisted state group. ``None`` for an
outlier. outlier.
delta_ids (dict[(str, str), str]): Delta from ``prev_group``. delta_ids (dict[(str, str), str]): Delta from ``prev_group``.
@ -42,6 +40,8 @@ class EventContext(object):
prev_state_events (?): XXX: is this ever set to anything other than prev_state_events (?): XXX: is this ever set to anything other than
the empty list? the empty list?
app_service: FIXME
_current_state_ids (dict[(str, str), str]|None): _current_state_ids (dict[(str, str), str]|None):
The current state map including the current event. None if outlier The current state map including the current event. None if outlier
or we haven't fetched the state from DB yet. or we haven't fetched the state from DB yet.
@ -67,49 +67,33 @@ class EventContext(object):
Only set when state has not been fetched yet. Only set when state has not been fetched yet.
""" """
__slots__ = [ state_group = attr.ib(default=None)
"state_group", rejected = attr.ib(default=False)
"rejected", prev_group = attr.ib(default=None)
"prev_group", delta_ids = attr.ib(default=None)
"delta_ids", prev_state_events = attr.ib(default=attr.Factory(list))
"prev_state_events", app_service = attr.ib(default=None)
"app_service",
"_current_state_ids",
"_prev_state_ids",
"_prev_state_id",
"_event_type",
"_event_state_key",
"_fetching_state_deferred",
]
def __init__(self): _current_state_ids = attr.ib(default=None)
self.prev_state_events = [] _prev_state_ids = attr.ib(default=None)
self.rejected = False _prev_state_id = attr.ib(default=None)
self.app_service = None
_event_type = attr.ib(default=None)
_event_state_key = attr.ib(default=None)
_fetching_state_deferred = attr.ib(default=None)
@staticmethod @staticmethod
def with_state( def with_state(
state_group, current_state_ids, prev_state_ids, prev_group=None, delta_ids=None state_group, current_state_ids, prev_state_ids, prev_group=None, delta_ids=None
): ):
context = EventContext() return EventContext(
current_state_ids=current_state_ids,
# The current state including the current event prev_state_ids=prev_state_ids,
context._current_state_ids = current_state_ids state_group=state_group,
# The current state excluding the current event fetching_state_deferred=defer.succeed(None),
context._prev_state_ids = prev_state_ids prev_group=prev_group,
context.state_group = state_group delta_ids=delta_ids,
)
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
@defer.inlineCallbacks @defer.inlineCallbacks
def serialize(self, event, store): def serialize(self, event, store):
@ -157,24 +141,18 @@ class EventContext(object):
Returns: Returns:
EventContext EventContext
""" """
context = EventContext() context = EventContext(
# We use the state_group and prev_state_id stuff to pull the
# 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.
# current_state_ids out of the DB and construct prev_state_ids. prev_state_id=input["prev_state_id"],
context._prev_state_id = input["prev_state_id"] event_type=input["event_type"],
context._event_type = input["event_type"] event_state_key=input["event_state_key"],
context._event_state_key = input["event_state_key"] state_group=input["state_group"],
prev_group=input["prev_group"],
context._current_state_ids = None delta_ids=_decode_state_dict(input["delta_ids"]),
context._prev_state_ids = None rejected=input["rejected"],
context._fetching_state_deferred = None prev_state_events=input["prev_state_events"],
)
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"]
app_service_id = input["app_service_id"] app_service_id = input["app_service_id"]
if app_service_id: if app_service_id:

View File

@ -15,6 +15,7 @@
import logging import logging
from collections import namedtuple from collections import namedtuple
from typing import Iterable, Tuple
from six import iteritems, itervalues from six import iteritems, itervalues
from six.moves import range from six.moves import range
@ -23,6 +24,8 @@ from twisted.internet import defer
from synapse.api.constants import EventTypes from synapse.api.constants import EventTypes
from synapse.api.errors import NotFoundError 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._base import SQLBaseStore
from synapse.storage.background_updates import BackgroundUpdateStore from synapse.storage.background_updates import BackgroundUpdateStore
from synapse.storage.data_stores.main.events_worker import EventsWorkerStore from synapse.storage.data_stores.main.events_worker import EventsWorkerStore
@ -1215,7 +1218,9 @@ class StateStore(StateGroupWorkerStore, StateBackgroundUpdateStore):
def __init__(self, db_conn, hs): def __init__(self, db_conn, hs):
super(StateStore, self).__init__(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 = {} state_groups = {}
for event, context in events_and_contexts: for event, context in events_and_contexts:
if event.internal_metadata.is_outlier(): if event.internal_metadata.is_outlier():