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

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into rav/new_events_col…
Browse files Browse the repository at this point in the history
…umns
  • Loading branch information
richvdh committed Jan 21, 2022
2 parents b8b56d0 + 9f2016e commit f1a0e7d
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 37 deletions.
1 change: 1 addition & 0 deletions changelog.d/11794.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Preparation for database schema simplifications: stop reading from `event_reference_hashes`.
1 change: 1 addition & 0 deletions changelog.d/11795.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Drop unused table `public_room_list_stream`.
2 changes: 1 addition & 1 deletion synapse/replication/slave/storage/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class SlavedEventStore(
EventPushActionsWorkerStore,
StreamWorkerStore,
StateGroupWorkerStore,
EventsWorkerStore,
SignatureWorkerStore,
EventsWorkerStore,
UserErasureWorkerStore,
RelationsWorkerStore,
BaseSlavedStore,
Expand Down
2 changes: 1 addition & 1 deletion synapse/storage/databases/main/event_federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion synapse/storage/databases/main/purge_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,6 @@ def _purge_room_txn(self, txn, room_id: str) -> List[int]:
"event_search",
"events",
"group_rooms",
"public_room_list_stream",
"receipts_graph",
"receipts_linearized",
"room_aliases",
Expand Down
54 changes: 24 additions & 30 deletions synapse/storage/databases/main/signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -41,18 +44,27 @@ 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,
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.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 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]]]:
"""
Expand All @@ -70,24 +82,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"""
9 changes: 6 additions & 3 deletions synapse/storage/schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,12 +53,15 @@
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.
"""


SCHEMA_COMPAT_VERSION = (
# the schema no longer support synapse versions with SCHEMA_VERSION < 66, because
# there are multiple state_key columns.
# we now have `state_key` columns in both `events` and `state_events`, so
# now incompatible with synapses wth SCHEMA_VERSION < 66.
66
)
"""Limit on how far the synapse codebase can be rolled back without breaking db compat
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* Copyright 2022 The Matrix.org Foundation C.I.C
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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.
*/

-- this table is unused as of Synapse 1.41
DROP TABLE public_room_list_stream;

1 change: 0 additions & 1 deletion tests/rest/admin/test_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -2468,7 +2468,6 @@ def _block_room(self, room_id: str) -> None:
"event_search",
"events",
"group_rooms",
"public_room_list_stream",
"receipts_graph",
"receipts_linearized",
"room_aliases",
Expand Down

0 comments on commit f1a0e7d

Please sign in to comment.