Skip to content

Commit 00f9fc0

Browse files
erikjohnstonphil-flex
authored andcommitted
Reduce auth chains fetched during v2 state res. (matrix-org#6952)
The state res v2 algorithm only cares about the difference between auth chains, so we can pass in the known common state to the `get_auth_chain` storage function so that it can ignore those events.
1 parent 0643424 commit 00f9fc0

File tree

5 files changed

+39
-13
lines changed

5 files changed

+39
-13
lines changed

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)