This repository was 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
Speed up rebuilding of the user directory for local users #15529
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bc57470
Reduce number of transactions
erikjohnston 6d813e1
Split out to separate function
erikjohnston ab90615
Make user dir insertion take collection
erikjohnston b39f5de
Batch up queries to user dir
erikjohnston 030205a
Newsfile
erikjohnston ec420cf
Move non_null_str_or_none check
erikjohnston 532f4b3
Rename
erikjohnston 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 hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -27,6 +27,8 @@ | |
| cast, | ||
| ) | ||
|
|
||
| import attr | ||
|
|
||
| try: | ||
| # Figure out if ICU support is available for searching users. | ||
| import icu | ||
|
|
@@ -66,6 +68,17 @@ | |
| TEMP_TABLE = "_temp_populate_user_directory" | ||
|
|
||
|
|
||
| @attr.s(auto_attribs=True, frozen=True) | ||
| class _UserDirProfile: | ||
| """Helper type for the user directory code for an entry to be inserted into | ||
| the directory. | ||
| """ | ||
|
|
||
| user_id: str | ||
| display_name: Optional[str] = None | ||
| avatar_url: Optional[str] = None | ||
|
|
||
|
|
||
| class UserDirectoryBackgroundUpdateStore(StateDeltasStore): | ||
| # How many records do we calculate before sending it to | ||
| # add_users_who_share_private_rooms? | ||
|
|
@@ -388,18 +401,23 @@ def _get_next_batch(txn: LoggingTransaction) -> Optional[List[str]]: | |
| user_id, profile.display_name, profile.avatar_url | ||
| ) | ||
|
|
||
| # We've finished processing a user. Delete it from the table. | ||
| await self.db_pool.simple_delete_one( | ||
| TEMP_TABLE + "_users", {"user_id": user_id} | ||
| ) | ||
| # Update the remaining counter. | ||
| progress["remaining"] -= 1 | ||
| await self.db_pool.runInteraction( | ||
| "populate_user_directory", | ||
| self.db_pool.updates._background_update_progress_txn, | ||
| "populate_user_directory_process_users", | ||
| progress, | ||
| ) | ||
| # We've finished processing the users. Delete it from the table. | ||
| await self.db_pool.simple_delete_many( | ||
| table=TEMP_TABLE + "_users", | ||
| column="user_id", | ||
| iterable=users_to_work_on, | ||
| keyvalues={}, | ||
| desc="populate_user_directory_process_users_delete", | ||
| ) | ||
|
|
||
| # Update the remaining counter. | ||
| progress["remaining"] -= len(users_to_work_on) | ||
| await self.db_pool.runInteraction( | ||
| "populate_user_directory", | ||
| self.db_pool.updates._background_update_progress_txn, | ||
| "populate_user_directory_process_users", | ||
| progress, | ||
| ) | ||
|
|
||
| return len(users_to_work_on) | ||
|
|
||
|
|
@@ -588,68 +606,102 @@ async def update_profile_in_user_dir( | |
| display_name = non_null_str_or_none(display_name) | ||
| avatar_url = non_null_str_or_none(avatar_url) | ||
|
|
||
| def _update_profile_in_user_dir_txn(txn: LoggingTransaction) -> None: | ||
| self.db_pool.simple_upsert_txn( | ||
| await self.db_pool.runInteraction( | ||
| "update_profile_in_user_dir", | ||
| self._update_profile_in_user_dir_txn, | ||
| [_UserDirProfile(user_id, display_name, avatar_url)], | ||
| ) | ||
|
|
||
| def _update_profile_in_user_dir_txn( | ||
| self, | ||
| txn: LoggingTransaction, | ||
| profiles: Sequence[_UserDirProfile], | ||
| ) -> None: | ||
| self.db_pool.simple_upsert_many_txn( | ||
| txn, | ||
| table="user_directory", | ||
| key_names=("user_id",), | ||
| key_values=[(p.user_id,) for p in profiles], | ||
| value_names=("display_name", "avatar_url"), | ||
| value_values=[ | ||
| ( | ||
| non_null_str_or_none(p.display_name), | ||
| non_null_str_or_none(p.avatar_url), | ||
|
||
| ) | ||
| for p in profiles | ||
| ], | ||
| ) | ||
|
|
||
| # Remote users: Make sure the profile is not marked as stale anymore. | ||
| remote_users = [ | ||
| p.user_id for p in profiles if not self.hs.is_mine_id(p.user_id) | ||
| ] | ||
| if remote_users: | ||
| self.db_pool.simple_delete_many_txn( | ||
| txn, | ||
| table="user_directory", | ||
| keyvalues={"user_id": user_id}, | ||
| values={"display_name": display_name, "avatar_url": avatar_url}, | ||
| table="user_directory_stale_remote_users", | ||
| column="user_id", | ||
| values=remote_users, | ||
| keyvalues={}, | ||
| ) | ||
|
|
||
| if not self.hs.is_mine_id(user_id): | ||
| # Remote users: Make sure the profile is not marked as stale anymore. | ||
| self.db_pool.simple_delete_txn( | ||
| txn, | ||
| table="user_directory_stale_remote_users", | ||
| keyvalues={"user_id": user_id}, | ||
| if isinstance(self.database_engine, PostgresEngine): | ||
| # We weight the localpart most highly, then display name and finally | ||
| # server name | ||
| template = """ | ||
| ( | ||
| %s, | ||
| setweight(to_tsvector('simple', %s), 'A') | ||
| || setweight(to_tsvector('simple', %s), 'D') | ||
| || setweight(to_tsvector('simple', COALESCE(%s, '')), 'B') | ||
| ) | ||
| """ | ||
|
|
||
| # The display name that goes into the database index. | ||
| index_display_name = display_name | ||
| if index_display_name is not None: | ||
| index_display_name = _filter_text_for_index(index_display_name) | ||
|
|
||
| if isinstance(self.database_engine, PostgresEngine): | ||
| # We weight the localpart most highly, then display name and finally | ||
| # server name | ||
| sql = """ | ||
| INSERT INTO user_directory_search(user_id, vector) | ||
| VALUES (?, | ||
| setweight(to_tsvector('simple', ?), 'A') | ||
| || setweight(to_tsvector('simple', ?), 'D') | ||
| || setweight(to_tsvector('simple', COALESCE(?, '')), 'B') | ||
| ) ON CONFLICT (user_id) DO UPDATE SET vector=EXCLUDED.vector | ||
| """ | ||
| txn.execute( | ||
| sql, | ||
| sql = """ | ||
| INSERT INTO user_directory_search(user_id, vector) | ||
| VALUES ? ON CONFLICT (user_id) DO UPDATE SET vector=EXCLUDED.vector | ||
| """ | ||
| txn.execute_values( | ||
| sql, | ||
| [ | ||
| ( | ||
| user_id, | ||
| get_localpart_from_id(user_id), | ||
| get_domain_from_id(user_id), | ||
| index_display_name, | ||
| ), | ||
| ) | ||
| elif isinstance(self.database_engine, Sqlite3Engine): | ||
| value = ( | ||
| "%s %s" % (user_id, index_display_name) | ||
| if index_display_name | ||
| else user_id | ||
| ) | ||
| self.db_pool.simple_upsert_txn( | ||
| txn, | ||
| table="user_directory_search", | ||
| keyvalues={"user_id": user_id}, | ||
| values={"value": value}, | ||
| ) | ||
| else: | ||
| # This should be unreachable. | ||
| raise Exception("Unrecognized database engine") | ||
| p.user_id, | ||
| get_localpart_from_id(p.user_id), | ||
| get_domain_from_id(p.user_id), | ||
| _filter_text_for_index(p.display_name) | ||
| if p.display_name | ||
| else None, | ||
| ) | ||
| for p in profiles | ||
| ], | ||
| template=template, | ||
| fetch=False, | ||
| ) | ||
| elif isinstance(self.database_engine, Sqlite3Engine): | ||
| values = [] | ||
| for p in profiles: | ||
| if p.display_name is not None: | ||
| index_display_name = _filter_text_for_index(p.display_name) | ||
| value = f"{p.user_id} {index_display_name}" | ||
| else: | ||
| value = p.user_id | ||
|
|
||
| txn.call_after(self.get_user_in_directory.invalidate, (user_id,)) | ||
| values.append((value,)) | ||
|
|
||
| await self.db_pool.runInteraction( | ||
| "update_profile_in_user_dir", _update_profile_in_user_dir_txn | ||
| ) | ||
| self.db_pool.simple_upsert_many_txn( | ||
| txn, | ||
| table="user_directory_search", | ||
| key_names=("user_id",), | ||
| key_values=[(p.user_id,) for p in profiles], | ||
| value_names=("value",), | ||
| value_values=values, | ||
| ) | ||
| else: | ||
| # This should be unreachable. | ||
| raise Exception("Unrecognized database engine") | ||
|
|
||
| for p in profiles: | ||
| txn.call_after(self.get_user_in_directory.invalidate, (p.user_id,)) | ||
|
|
||
| async def add_users_who_share_private_room( | ||
| self, room_id: str, user_id_tuples: Iterable[Tuple[str, str]] | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.