From 89ec7a655eebdfd401eba03bb4adec5c4235836f Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Thu, 20 Jan 2022 22:26:24 +0000 Subject: [PATCH 1/3] Stop reading from `event_refrence_hashes` Preparation for dropping this table altogether. Part of #6574. --- changelog.d/11794.misc | 1 + synapse/storage/databases/main/signatures.py | 50 ++++++++------------ synapse/storage/schema/__init__.py | 5 +- 3 files changed, 25 insertions(+), 31 deletions(-) create mode 100644 changelog.d/11794.misc diff --git a/changelog.d/11794.misc b/changelog.d/11794.misc new file mode 100644 index 000000000000..29826bc0e5ab --- /dev/null +++ b/changelog.d/11794.misc @@ -0,0 +1 @@ +Preparation for database schema simplifications: stop reading from `event_reference_hashes`. diff --git a/synapse/storage/databases/main/signatures.py b/synapse/storage/databases/main/signatures.py index 3201623fe415..038bc6489fa9 100644 --- a/synapse/storage/databases/main/signatures.py +++ b/synapse/storage/databases/main/signatures.py @@ -12,16 +12,19 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Dict, Iterable, List, Tuple +from typing import Collection, Dict, List, Tuple from unpaddedbase64 import encode_base64 -from synapse.storage._base import SQLBaseStore -from synapse.storage.types import Cursor +from synapse.crypto.event_signing import compute_event_reference_hash +from synapse.storage.databases.main.events_worker import ( + EventRedactBehaviour, + EventsWorkerStore, +) from synapse.util.caches.descriptors import cached, cachedList -class SignatureWorkerStore(SQLBaseStore): +class SignatureWorkerStore(EventsWorkerStore): @cached() def get_event_reference_hash(self, event_id): # This is a dummy function to allow get_event_reference_hashes @@ -32,7 +35,7 @@ def get_event_reference_hash(self, event_id): cached_method_name="get_event_reference_hash", list_name="event_ids", num_args=1 ) async def get_event_reference_hashes( - self, event_ids: Iterable[str] + self, event_ids: Collection[str] ) -> Dict[str, Dict[str, bytes]]: """Get all hashes for given events. @@ -42,17 +45,22 @@ async def get_event_reference_hashes( Returns: A mapping of event ID to a mapping of algorithm to hash. """ + events = await self.get_events( + event_ids, + redact_behaviour=EventRedactBehaviour.AS_IS, + allow_rejected=True, + ) - def f(txn): - return { - event_id: self._get_event_reference_hashes_txn(txn, event_id) - for event_id in event_ids - } + hashes: Dict[str, Dict[str, bytes]] = {} + for event_id in event_ids: + event = events[event_id] + ref_alg, ref_hash_bytes = compute_event_reference_hash(event) + hashes[event.event_id] = {ref_alg: ref_hash_bytes} - return await self.db_pool.runInteraction("get_event_reference_hashes", f) + return hashes async def add_event_hashes( - self, event_ids: Iterable[str] + self, event_ids: Collection[str] ) -> List[Tuple[str, Dict[str, str]]]: """ @@ -70,24 +78,6 @@ async def add_event_hashes( return list(encoded_hashes.items()) - def _get_event_reference_hashes_txn( - self, txn: Cursor, event_id: str - ) -> Dict[str, bytes]: - """Get all the hashes for a given PDU. - Args: - txn: - event_id: Id for the Event. - Returns: - A mapping of algorithm -> hash. - """ - query = ( - "SELECT algorithm, hash" - " FROM event_reference_hashes" - " WHERE event_id = ?" - ) - txn.execute(query, (event_id,)) - return {k: v for k, v in txn} - class SignatureStore(SignatureWorkerStore): """Persistence for event signatures and hashes""" diff --git a/synapse/storage/schema/__init__.py b/synapse/storage/schema/__init__.py index 2a3d47185ae5..166173ba376c 100644 --- a/synapse/storage/schema/__init__.py +++ b/synapse/storage/schema/__init__.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -SCHEMA_VERSION = 67 # remember to update the list below when updating +SCHEMA_VERSION = 68 # remember to update the list below when updating """Represents the expectations made by the codebase about the database schema This should be incremented whenever the codebase changes its requirements on the @@ -53,6 +53,9 @@ Changes in SCHEMA_VERSION = 67: - state_events.prev_state is no longer written to. + +Changes in SCHEMA_VERSION = 68: + - event_reference_hashes is no longer read. """ From cca16f3cbe18afbf7caa3e1e6dfdc9d2aef7c11b Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Thu, 20 Jan 2022 22:43:59 +0000 Subject: [PATCH 2/3] fix inheritance order --- synapse/replication/slave/storage/events.py | 2 +- synapse/storage/databases/main/event_federation.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/synapse/replication/slave/storage/events.py b/synapse/replication/slave/storage/events.py index 0f0837269486..a72dad7464d7 100644 --- a/synapse/replication/slave/storage/events.py +++ b/synapse/replication/slave/storage/events.py @@ -52,8 +52,8 @@ class SlavedEventStore( EventPushActionsWorkerStore, StreamWorkerStore, StateGroupWorkerStore, - EventsWorkerStore, SignatureWorkerStore, + EventsWorkerStore, UserErasureWorkerStore, RelationsWorkerStore, BaseSlavedStore, diff --git a/synapse/storage/databases/main/event_federation.py b/synapse/storage/databases/main/event_federation.py index 270b30800bf2..0856a9332aa4 100644 --- a/synapse/storage/databases/main/event_federation.py +++ b/synapse/storage/databases/main/event_federation.py @@ -65,7 +65,7 @@ def __init__(self, room_id: str): super().__init__("Unexpectedly no chain cover for events in %s" % (room_id,)) -class EventFederationWorkerStore(EventsWorkerStore, SignatureWorkerStore, SQLBaseStore): +class EventFederationWorkerStore(SignatureWorkerStore, EventsWorkerStore, SQLBaseStore): def __init__( self, database: DatabasePool, From 5d97f738d46c2fdcfd90de995449fa7bc3d3a1fa Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Thu, 20 Jan 2022 23:29:49 +0000 Subject: [PATCH 3/3] Fix unknown-event case --- synapse/storage/databases/main/signatures.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/synapse/storage/databases/main/signatures.py b/synapse/storage/databases/main/signatures.py index 038bc6489fa9..0518b8b910e0 100644 --- a/synapse/storage/databases/main/signatures.py +++ b/synapse/storage/databases/main/signatures.py @@ -44,6 +44,7 @@ async def get_event_reference_hashes( Returns: A mapping of event ID to a mapping of algorithm to hash. + Returns an empty dict for a given event id if that event is unknown. """ events = await self.get_events( event_ids, @@ -53,9 +54,12 @@ async def get_event_reference_hashes( hashes: Dict[str, Dict[str, bytes]] = {} for event_id in event_ids: - event = events[event_id] - ref_alg, ref_hash_bytes = compute_event_reference_hash(event) - hashes[event.event_id] = {ref_alg: ref_hash_bytes} + event = events.get(event_id) + if event is None: + hashes[event_id] = {} + else: + ref_alg, ref_hash_bytes = compute_event_reference_hash(event) + hashes[event_id] = {ref_alg: ref_hash_bytes} return hashes