This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Micro-optimisations to get_auth_chain_ids #8132
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Micro-optimisations to get_auth_chain_ids. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,27 +15,31 @@ | |
import itertools | ||
import logging | ||
from queue import Empty, PriorityQueue | ||
from typing import Dict, Iterable, List, Optional, Set, Tuple | ||
from typing import Dict, Iterable, List, Set, Tuple | ||
|
||
from synapse.api.errors import StoreError | ||
from synapse.events import EventBase | ||
from synapse.metrics.background_process_metrics import run_as_background_process | ||
from synapse.storage._base import SQLBaseStore, make_in_list_sql_clause | ||
from synapse.storage.database import DatabasePool | ||
from synapse.storage.database import DatabasePool, LoggingTransaction | ||
from synapse.storage.databases.main.events_worker import EventsWorkerStore | ||
from synapse.storage.databases.main.signatures import SignatureWorkerStore | ||
from synapse.types import Collection | ||
from synapse.util.caches.descriptors import cached | ||
from synapse.util.iterutils import batch_iter | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class EventFederationWorkerStore(EventsWorkerStore, SignatureWorkerStore, SQLBaseStore): | ||
async def get_auth_chain(self, event_ids, include_given=False): | ||
async def get_auth_chain( | ||
self, event_ids: Collection[str], include_given: bool = False | ||
) -> List[EventBase]: | ||
"""Get auth events for given event_ids. The events *must* be state events. | ||
|
||
Args: | ||
event_ids (list): state events | ||
include_given (bool): include the given events in result | ||
event_ids: state events | ||
include_given: include the given events in result | ||
|
||
Returns: | ||
list of events | ||
|
@@ -45,43 +49,34 @@ async def get_auth_chain(self, event_ids, include_given=False): | |
) | ||
return await self.get_events_as_list(event_ids) | ||
|
||
def get_auth_chain_ids( | ||
self, | ||
event_ids: List[str], | ||
include_given: bool = False, | ||
ignore_events: Optional[Set[str]] = None, | ||
): | ||
async def get_auth_chain_ids( | ||
self, event_ids: Collection[str], include_given: bool = False, | ||
) -> List[str]: | ||
"""Get auth events for given event_ids. The events *must* be state events. | ||
|
||
Args: | ||
event_ids: state events | ||
include_given: include the given events in result | ||
ignore_events: Set of events to exclude from the returned auth | ||
chain. This is useful if the caller will just discard the | ||
given events anyway, and saves us from figuring out their auth | ||
chains if not required. | ||
|
||
Returns: | ||
list of event_ids | ||
""" | ||
return self.db_pool.runInteraction( | ||
return await self.db_pool.runInteraction( | ||
"get_auth_chain_ids", | ||
self._get_auth_chain_ids_txn, | ||
event_ids, | ||
include_given, | ||
ignore_events, | ||
) | ||
|
||
def _get_auth_chain_ids_txn(self, txn, event_ids, include_given, ignore_events): | ||
if ignore_events is None: | ||
ignore_events = set() | ||
|
||
def _get_auth_chain_ids_txn( | ||
self, txn: LoggingTransaction, event_ids: Collection[str], include_given: bool | ||
) -> List[str]: | ||
if include_given: | ||
results = set(event_ids) | ||
else: | ||
results = set() | ||
|
||
base_sql = "SELECT auth_id FROM event_auth WHERE " | ||
base_sql = "SELECT DISTINCT auth_id FROM event_auth WHERE " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm expecting a fair bit of duplication here, so we may as well have the db server deduplicate. |
||
|
||
front = set(event_ids) | ||
while front: | ||
|
@@ -93,7 +88,6 @@ def _get_auth_chain_ids_txn(self, txn, event_ids, include_given, ignore_events): | |
txn.execute(base_sql + clause, args) | ||
new_front.update(r[0] for r in txn) | ||
|
||
new_front -= ignore_events | ||
new_front -= results | ||
|
||
front = new_front | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was no longer used: may as well strip it out