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

Commit 8fbacc7

Browse files
committed
Reduce auth chains fetched during v2 state res. (#6952)
* commit '2b37eabca': Reduce auth chains fetched during v2 state res. (#6952) update changes.md
2 parents 4b9c44b + 2b37eab commit 8fbacc7

File tree

6 files changed

+44
-19
lines changed

6 files changed

+44
-19
lines changed

CHANGES.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ Features
77
- Admin API to add or modify threepids of user accounts. ([\#6769](https://github.com/matrix-org/synapse/issues/6769))
88
- Limit the number of events that can be requested by the backfill federation API to 100. ([\#6864](https://github.com/matrix-org/synapse/issues/6864))
99
- Add ability to run some group APIs on workers. ([\#6866](https://github.com/matrix-org/synapse/issues/6866))
10-
- Reject device display names over 100 characters in length. ([\#6882](https://github.com/matrix-org/synapse/issues/6882))
10+
- Reject device display names over 100 characters in length to prevent abuse. ([\#6882](https://github.com/matrix-org/synapse/issues/6882))
1111
- Add ability to route federation user device queries to workers. ([\#6873](https://github.com/matrix-org/synapse/issues/6873))
1212
- The result of a user directory search can now be filtered via the spam checker. ([\#6888](https://github.com/matrix-org/synapse/issues/6888))
13-
- Implement `GET /_matrix/client/r0/rooms/{roomId}/aliases` endpoint as per [MSC2432](https://github.com/matrix-org/matrix-doc/pull/2432). ([\#6939](https://github.com/matrix-org/synapse/issues/6939), [\#6948](https://github.com/matrix-org/synapse/issues/6948), [\#6949](https://github.com/matrix-org/synapse/issues/6949))
13+
- Implement new `GET /_matrix/client/unstable/org.matrix.msc2432/rooms/{roomId}/aliases` endpoint as per [MSC2432](https://github.com/matrix-org/matrix-doc/pull/2432). ([\#6939](https://github.com/matrix-org/synapse/issues/6939), [\#6948](https://github.com/matrix-org/synapse/issues/6948), [\#6949](https://github.com/matrix-org/synapse/issues/6949))
1414
- Stop sending `m.room.alias` events wheng adding / removing aliases. Check `alt_aliases` in the latest `m.room.canonical_alias` event when deleting an alias. ([\#6904](https://github.com/matrix-org/synapse/issues/6904))
15-
15+
- Change the default power levels of invites, tombstones and server ACLs for new rooms. ([\#6834](https://github.com/matrix-org/synapse/issues/6834))
1616

1717
Bugfixes
1818
--------
@@ -41,6 +41,7 @@ Improved Documentation
4141
- Clean up and update docs on setting up federation. ([\#6940](https://github.com/matrix-org/synapse/issues/6940))
4242
- Add a warning about indentation to generated configuration files. ([\#6920](https://github.com/matrix-org/synapse/issues/6920))
4343
- Databases created using the compose file in contrib/docker will now always have correct encoding and locale settings. Contributed by Fridtjof Mund. ([\#6921](https://github.com/matrix-org/synapse/issues/6921))
44+
- Update pip install directions in readme to avoid error when using zsh. ([\#6855](https://github.com/matrix-org/synapse/issues/6855))
4445

4546

4647
Deprecations and Removals
@@ -54,15 +55,13 @@ Internal Changes
5455

5556
- Add type hints to `SyncHandler`. ([\#6821](https://github.com/matrix-org/synapse/issues/6821))
5657
- Refactoring work in preparation for changing the event redaction algorithm. ([\#6823](https://github.com/matrix-org/synapse/issues/6823), [\#6827](https://github.com/matrix-org/synapse/issues/6827), [\#6854](https://github.com/matrix-org/synapse/issues/6854), [\#6856](https://github.com/matrix-org/synapse/issues/6856), [\#6857](https://github.com/matrix-org/synapse/issues/6857), [\#6858](https://github.com/matrix-org/synapse/issues/6858))
57-
- Change the default power levels of invites, tombstones and server ACLs for new rooms. ([\#6834](https://github.com/matrix-org/synapse/issues/6834))
5858
- Fix stacktraces when using `ObservableDeferred` and async/await. ([\#6836](https://github.com/matrix-org/synapse/issues/6836))
5959
- Port much of `synapse.handlers.federation` to async/await. ([\#6837](https://github.com/matrix-org/synapse/issues/6837), [\#6840](https://github.com/matrix-org/synapse/issues/6840))
6060
- Populate `rooms.room_version` database column at startup, rather than in a background update. ([\#6847](https://github.com/matrix-org/synapse/issues/6847))
61-
- Update pip install directions in readme to avoid error when using zsh. ([\#6855](https://github.com/matrix-org/synapse/issues/6855))
6261
- Reduce amount we log at `INFO` level. ([\#6833](https://github.com/matrix-org/synapse/issues/6833), [\#6862](https://github.com/matrix-org/synapse/issues/6862))
6362
- Remove unused `get_room_stats_state` method. ([\#6869](https://github.com/matrix-org/synapse/issues/6869))
6463
- Add typing to `synapse.federation.sender` and port to async/await. ([\#6871](https://github.com/matrix-org/synapse/issues/6871))
65-
- Refactor _EventInternalMetadata object to improve type safety. ([\#6872](https://github.com/matrix-org/synapse/issues/6872))
64+
- Refactor `_EventInternalMetadata` object to improve type safety. ([\#6872](https://github.com/matrix-org/synapse/issues/6872))
6665
- Add an additional entry to the SyTest blacklist for worker mode. ([\#6883](https://github.com/matrix-org/synapse/issues/6883))
6766
- Fix the use of sed in the linting scripts when using BSD sed. ([\#6887](https://github.com/matrix-org/synapse/issues/6887))
6867
- Add type hints to the spam checker module. ([\#6915](https://github.com/matrix-org/synapse/issues/6915))

changelog.d/6952.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve perf of v2 state res for large rooms.

synapse/state/__init__.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import logging
1818
from collections import namedtuple
19-
from typing import Dict, Iterable, List, Optional
19+
from typing import Dict, Iterable, List, Optional, Set
2020

2121
from six import iteritems, itervalues
2222

@@ -662,7 +662,7 @@ def get_events(self, event_ids, allow_rejected=False):
662662
allow_rejected=allow_rejected,
663663
)
664664

665-
def get_auth_chain(self, event_ids):
665+
def get_auth_chain(self, event_ids: List[str], ignore_events: Set[str]):
666666
"""Gets the full auth chain for a set of events (including rejected
667667
events).
668668
@@ -674,11 +674,16 @@ def get_auth_chain(self, event_ids):
674674
presence of rejected events
675675
676676
Args:
677-
event_ids (list): The event IDs of the events to fetch the auth
678-
chain for. Must be state events.
677+
event_ids: The event IDs of the events to fetch the auth chain for.
678+
Must be state events.
679+
ignore_events: Set of events to exclude from the returned auth
680+
chain.
681+
679682
680683
Returns:
681684
Deferred[list[str]]: List of event IDs of the auth chain.
682685
"""
683686

684-
return self.store.get_auth_chain_ids(event_ids, include_given=True)
687+
return self.store.get_auth_chain_ids(
688+
event_ids, include_given=True, ignore_events=ignore_events,
689+
)

synapse/state/v2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def _get_auth_chain_difference(state_sets, event_map, state_res_store):
248248
and eid not in common
249249
)
250250

251-
auth_chain = yield state_res_store.get_auth_chain(auth_ids)
251+
auth_chain = yield state_res_store.get_auth_chain(auth_ids, common)
252252
auth_ids.update(auth_chain)
253253

254254
auth_sets.append(auth_ids)

synapse/storage/data_stores/main/event_federation.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# limitations under the License.
1515
import itertools
1616
import logging
17+
from typing import List, Optional, Set
1718

1819
from six.moves import range
1920
from six.moves.queue import Empty, PriorityQueue
@@ -46,21 +47,37 @@ def get_auth_chain(self, event_ids, include_given=False):
4647
event_ids, include_given=include_given
4748
).addCallback(self.get_events_as_list)
4849

49-
def get_auth_chain_ids(self, event_ids, include_given=False):
50+
def get_auth_chain_ids(
51+
self,
52+
event_ids: List[str],
53+
include_given: bool = False,
54+
ignore_events: Optional[Set[str]] = None,
55+
):
5056
"""Get auth events for given event_ids. The events *must* be state events.
5157
5258
Args:
53-
event_ids (list): state events
54-
include_given (bool): include the given events in result
59+
event_ids: state events
60+
include_given: include the given events in result
61+
ignore_events: Set of events to exclude from the returned auth
62+
chain. This is useful if the caller will just discard the
63+
given events anyway, and saves us from figuring out their auth
64+
chains if not required.
5565
5666
Returns:
5767
list of event_ids
5868
"""
5969
return self.db.runInteraction(
60-
"get_auth_chain_ids", self._get_auth_chain_ids_txn, event_ids, include_given
70+
"get_auth_chain_ids",
71+
self._get_auth_chain_ids_txn,
72+
event_ids,
73+
include_given,
74+
ignore_events,
6175
)
6276

63-
def _get_auth_chain_ids_txn(self, txn, event_ids, include_given):
77+
def _get_auth_chain_ids_txn(self, txn, event_ids, include_given, ignore_events):
78+
if ignore_events is None:
79+
ignore_events = set()
80+
6481
if include_given:
6582
results = set(event_ids)
6683
else:
@@ -80,6 +97,7 @@ def _get_auth_chain_ids_txn(self, txn, event_ids, include_given):
8097
txn.execute(base_sql + clause, list(args))
8198
new_front.update([r[0] for r in txn])
8299

100+
new_front -= ignore_events
83101
new_front -= results
84102

85103
front = new_front

tests/state/test_v2.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ def get_events(self, event_ids, allow_rejected=False):
603603

604604
return {eid: self.event_map[eid] for eid in event_ids if eid in self.event_map}
605605

606-
def get_auth_chain(self, event_ids):
606+
def get_auth_chain(self, event_ids, ignore_events):
607607
"""Gets the full auth chain for a set of events (including rejected
608608
events).
609609
@@ -617,6 +617,8 @@ def get_auth_chain(self, event_ids):
617617
Args:
618618
event_ids (list): The event IDs of the events to fetch the auth
619619
chain for. Must be state events.
620+
ignore_events: Set of events to exclude from the returned auth
621+
chain.
620622
621623
Returns:
622624
Deferred[list[str]]: List of event IDs of the auth chain.
@@ -627,7 +629,7 @@ def get_auth_chain(self, event_ids):
627629
stack = list(event_ids)
628630
while stack:
629631
event_id = stack.pop()
630-
if event_id in result:
632+
if event_id in result or event_id in ignore_events:
631633
continue
632634

633635
result.add(event_id)

0 commit comments

Comments
 (0)