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

Commit 92a0c18

Browse files
authored
Improve performance of getting unread counts in rooms (#13119)
1 parent cdc0259 commit 92a0c18

File tree

6 files changed

+40
-4
lines changed

6 files changed

+40
-4
lines changed

changelog.d/13119.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Reduce DB usage of `/sync` when a large number of unread messages have recently been sent in a room.

synapse/_scripts/synapse_port_db.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ def get_reactor(self) -> ISynapseReactor:
270270
def get_instance_name(self) -> str:
271271
return "master"
272272

273+
def should_send_federation(self) -> bool:
274+
return False
275+
273276

274277
class Porter:
275278
def __init__(

synapse/storage/databases/main/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ class DataStore(
8787
RoomStore,
8888
RoomBatchStore,
8989
RegistrationStore,
90-
StreamWorkerStore,
9190
ProfileStore,
9291
PresenceStore,
9392
TransactionWorkerStore,
@@ -112,6 +111,7 @@ class DataStore(
112111
SearchStore,
113112
TagsStore,
114113
AccountDataStore,
114+
StreamWorkerStore,
115115
OpenIdStore,
116116
ClientIpWorkerStore,
117117
DeviceStore,

synapse/storage/databases/main/event_push_actions.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
LoggingDatabaseConnection,
2626
LoggingTransaction,
2727
)
28-
from synapse.storage.databases.main.events_worker import EventsWorkerStore
2928
from synapse.storage.databases.main.receipts import ReceiptsWorkerStore
29+
from synapse.storage.databases.main.stream import StreamWorkerStore
3030
from synapse.util import json_encoder
3131
from synapse.util.caches.descriptors import cached
3232

@@ -122,7 +122,7 @@ def _deserialize_action(actions: str, is_highlight: bool) -> List[Union[dict, st
122122
return DEFAULT_NOTIF_ACTION
123123

124124

125-
class EventPushActionsWorkerStore(ReceiptsWorkerStore, EventsWorkerStore, SQLBaseStore):
125+
class EventPushActionsWorkerStore(ReceiptsWorkerStore, StreamWorkerStore, SQLBaseStore):
126126
def __init__(
127127
self,
128128
database: DatabasePool,
@@ -218,7 +218,7 @@ def _get_unread_counts_by_receipt_txn(
218218
retcol="event_id",
219219
)
220220

221-
stream_ordering = self.get_stream_id_for_event_txn(txn, event_id) # type: ignore[attr-defined]
221+
stream_ordering = self.get_stream_id_for_event_txn(txn, event_id)
222222

223223
return self._get_unread_counts_by_pos_txn(
224224
txn, room_id, user_id, stream_ordering
@@ -307,12 +307,22 @@ def _get_notif_unread_count_for_user_room(
307307
actions that have been deleted from `event_push_actions` table.
308308
"""
309309

310+
# If there have been no events in the room since the stream ordering,
311+
# there can't be any push actions either.
312+
if not self._events_stream_cache.has_entity_changed(room_id, stream_ordering):
313+
return 0, 0
314+
310315
clause = ""
311316
args = [user_id, room_id, stream_ordering]
312317
if max_stream_ordering is not None:
313318
clause = "AND ea.stream_ordering <= ?"
314319
args.append(max_stream_ordering)
315320

321+
# If the max stream ordering is less than the min stream ordering,
322+
# then obviously there are zero push actions in that range.
323+
if max_stream_ordering <= stream_ordering:
324+
return 0, 0
325+
316326
sql = f"""
317327
SELECT
318328
COUNT(CASE WHEN notif = 1 THEN 1 END),

synapse/storage/databases/main/stream.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@
4646
Set,
4747
Tuple,
4848
cast,
49+
overload,
4950
)
5051

5152
import attr
5253
from frozendict import frozendict
54+
from typing_extensions import Literal
5355

5456
from twisted.internet import defer
5557

@@ -795,6 +797,24 @@ async def get_current_room_stream_token_for_room_id(
795797
)
796798
return RoomStreamToken(topo, stream_ordering)
797799

800+
@overload
801+
def get_stream_id_for_event_txn(
802+
self,
803+
txn: LoggingTransaction,
804+
event_id: str,
805+
allow_none: Literal[False] = False,
806+
) -> int:
807+
...
808+
809+
@overload
810+
def get_stream_id_for_event_txn(
811+
self,
812+
txn: LoggingTransaction,
813+
event_id: str,
814+
allow_none: bool = False,
815+
) -> Optional[int]:
816+
...
817+
798818
def get_stream_id_for_event_txn(
799819
self,
800820
txn: LoggingTransaction,

tests/storage/test_event_push_actions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ def _inject_actions(stream: int, action: list) -> None:
8686
event.internal_metadata.is_outlier.return_value = False
8787
event.depth = stream
8888

89+
self.store._events_stream_cache.entity_has_changed(room_id, stream)
90+
8991
self.get_success(
9092
self.store.db_pool.simple_insert(
9193
table="events",

0 commit comments

Comments
 (0)