From c5a041790b5b35dbacf3eea9dce0db0ad38d790a Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Mon, 15 Feb 2021 18:43:58 +0000 Subject: [PATCH] Move `outlier` out of InternalMetadata._dict Ultimately, this will allow us to stop writing it to the database. For now, we have to grandfather it back in so as to maintain compatibility with older versions of Synapse. --- synapse/events/__init__.py | 9 ++++++--- synapse/events/utils.py | 2 ++ synapse/storage/databases/main/events.py | 12 ++++++++++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/synapse/events/__init__.py b/synapse/events/__init__.py index 3ec4120f85c2..8f6b955d17b7 100644 --- a/synapse/events/__init__.py +++ b/synapse/events/__init__.py @@ -98,7 +98,7 @@ def __get__(self, instance, owner=None): class _EventInternalMetadata: - __slots__ = ["_dict", "stream_ordering"] + __slots__ = ["_dict", "stream_ordering", "outlier"] def __init__(self, internal_metadata_dict: JsonDict): # we have to copy the dict, because it turns out that the same dict is @@ -108,7 +108,10 @@ def __init__(self, internal_metadata_dict: JsonDict): # the stream ordering of this event. None, until it has been persisted. self.stream_ordering = None # type: Optional[int] - outlier = DictProperty("outlier") # type: bool + # whether this event is an outlier (ie, whether we have the state at that point + # in the DAG) + self.outlier = False + out_of_band_membership = DictProperty("out_of_band_membership") # type: bool send_on_behalf_of = DictProperty("send_on_behalf_of") # type: str recheck_redaction = DictProperty("recheck_redaction") # type: bool @@ -129,7 +132,7 @@ def get_dict(self) -> JsonDict: return dict(self._dict) def is_outlier(self) -> bool: - return self._dict.get("outlier", False) + return self.outlier def is_out_of_band_membership(self) -> bool: """Whether this is an out of band membership, like an invite or an invite diff --git a/synapse/events/utils.py b/synapse/events/utils.py index 9c22e3381378..674972629e86 100644 --- a/synapse/events/utils.py +++ b/synapse/events/utils.py @@ -54,6 +54,8 @@ def prune_event(event: EventBase) -> EventBase: event.internal_metadata.stream_ordering ) + pruned_event.internal_metadata.outlier = event.internal_metadata.outlier + # Mark the event as redacted pruned_event.internal_metadata.redacted = True diff --git a/synapse/storage/databases/main/events.py b/synapse/storage/databases/main/events.py index 7abfb9112e0b..061f8efbef4f 100644 --- a/synapse/storage/databases/main/events.py +++ b/synapse/storage/databases/main/events.py @@ -1245,8 +1245,10 @@ def _update_outliers_txn(self, txn, events_and_contexts): logger.exception("") raise + # update the stored internal_metadata to update the "outlier" flag. + # TODO: This is unused as of Synapse 1.29. Remove it once we are happy + # to drop backwards-compatibility with 1.28. metadata_json = json_encoder.encode(event.internal_metadata.get_dict()) - sql = "UPDATE event_json SET internal_metadata = ? WHERE event_id = ?" txn.execute(sql, (metadata_json, event.event_id)) @@ -1294,6 +1296,12 @@ def event_dict(event): d.pop("redacted_because", None) return d + def get_internal_metadata(event): + # temporary hack for compatibility with Synapse 1.28 and earlier + im = event.internal_metadata.get_dict() + im["outlier"] = event.internal_metadata.outlier + return im + self.db_pool.simple_insert_many_txn( txn, table="event_json", @@ -1302,7 +1310,7 @@ def event_dict(event): "event_id": event.event_id, "room_id": event.room_id, "internal_metadata": json_encoder.encode( - event.internal_metadata.get_dict() + get_internal_metadata(event) ), "json": json_encoder.encode(event_dict(event)), "format_version": event.format_version,