-
Notifications
You must be signed in to change notification settings - Fork 564
Sliding Sync: Speed up background updates to populate Sliding Sync tables #17676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
MadLittleMods
wants to merge
26
commits into
develop
from
madlittlemods/sliding-sync-faster-background-updates
Closed
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
944661a
Pull from `room_stats_state`
MadLittleMods 5894fc2
`concurrently_execute` data fetching
MadLittleMods b4fcbc9
`concurrently_execute` inserting
MadLittleMods ce7e1b4
Look for previous snapshot
MadLittleMods 27caa47
Work on snapshots background update
MadLittleMods 333b472
TODO and asserts
MadLittleMods ef2f553
Merge branch 'develop' into madlittlemods/sliding-sync-faster-backgro…
MadLittleMods f74106b
Implement `insertion_value_names`/`insertion_value_values` for `simpl…
MadLittleMods 836bcae
Fix nothing being upserted
MadLittleMods 90371d4
Sort by `event_stream_ordering` so we can re-use snapshots along the way
MadLittleMods 2c2146d
Only re-use for join memberships
MadLittleMods 1078163
Make sure re-use doesn't blow up
MadLittleMods a4cc5eb
Get snapshots info concurrently
MadLittleMods 815ef2f
Read from the `room_stats_state` table
MadLittleMods 1688212
Uncomment and remove todo
MadLittleMods f045574
Better comments
MadLittleMods a2da537
Add changelog
MadLittleMods ca4afad
Be more precise
MadLittleMods e2dca5d
Merge branch 'develop' into madlittlemods/sliding-sync-faster-backgro…
MadLittleMods bb5784b
Need to sort by room_id to make progress
MadLittleMods 6f3e8be
Add assertion message to better debug
MadLittleMods 8477df4
Fix f-string
MadLittleMods 76cc662
Use `events.stream_ordering` since `local_current_membership.event_st…
MadLittleMods 2e8f5d6
Use correct table
MadLittleMods 6565380
Rely on `events.stream_ordering`
MadLittleMods e5f133d
Merge branch 'develop' into madlittlemods/sliding-sync-faster-backgro…
MadLittleMods 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Pre-populate room data used in experimental [MSC3575](https://github.com/matrix-org/matrix-spec-proposals/pull/3575) Sliding Sync `/sync` endpoint for quick filtering/sorting. | ||
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 |
|---|---|---|
|
|
@@ -1472,6 +1472,11 @@ async def simple_upsert_many( | |
| key_values: Collection[Collection[Any]], | ||
| value_names: Collection[str], | ||
| value_values: Collection[Collection[Any]], | ||
| # Given these are the same type as the normal values, force keyword-only so that | ||
| # they can't be confused. | ||
| *, | ||
| insertion_value_names: Collection[str] = [], | ||
| insertion_value_values: Collection[Iterable[Any]] = [], | ||
|
Contributor
Author
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. Adding support for insertion only values on upsert. Same as we already have for |
||
| desc: str, | ||
| ) -> None: | ||
| """ | ||
|
|
@@ -1497,6 +1502,8 @@ async def simple_upsert_many( | |
| key_values, | ||
| value_names, | ||
| value_values, | ||
| insertion_value_names=insertion_value_names, | ||
| insertion_value_values=insertion_value_values, | ||
| db_autocommit=autocommit, | ||
| ) | ||
|
|
||
|
|
@@ -1508,6 +1515,11 @@ def simple_upsert_many_txn( | |
| key_values: Collection[Iterable[Any]], | ||
| value_names: Collection[str], | ||
| value_values: Collection[Iterable[Any]], | ||
| # Given these are the same type as the normal values, force keyword-only so that | ||
| # they can't be confused. | ||
| *, | ||
| insertion_value_names: Collection[str] = [], | ||
| insertion_value_values: Collection[Iterable[Any]] = [], | ||
| ) -> None: | ||
| """ | ||
| Upsert, many times. | ||
|
|
@@ -1519,6 +1531,9 @@ def simple_upsert_many_txn( | |
| value_names: The value column names | ||
| value_values: A list of each row's value column values. | ||
| Ignored if value_names is empty. | ||
| insertion_value_names: The value column names to use only when inserting | ||
| insertion_value_values: A list of each row's value column values to use only | ||
| when inserting. Ignored if `insertion_value_names` is empty. | ||
| """ | ||
| # If there's nothing to upsert, then skip executing the query. | ||
| if not key_values: | ||
|
|
@@ -1528,14 +1543,30 @@ def simple_upsert_many_txn( | |
| # zip() works correctly. | ||
| if not value_names: | ||
| value_values = [() for x in range(len(key_values))] | ||
| elif len(value_values) != len(key_values): | ||
| elif len(key_values) != len(value_values): | ||
| raise ValueError( | ||
| f"{len(key_values)} key rows and {len(value_values)} value rows: should be the same number." | ||
| ) | ||
|
|
||
| # No value columns, therefore make a blank list so that the following | ||
| # zip() works correctly. | ||
| if not insertion_value_names: | ||
| insertion_value_values = [() for x in range(len(key_values))] | ||
| elif len(key_values) != len(insertion_value_values): | ||
| raise ValueError( | ||
| f"{len(key_values)} key rows and {len(insertion_value_values)} insertion value rows: should be the same number." | ||
| ) | ||
|
|
||
| if table not in self._unsafe_to_upsert_tables: | ||
| return self.simple_upsert_many_txn_native_upsert( | ||
| txn, table, key_names, key_values, value_names, value_values | ||
| txn, | ||
| table, | ||
| key_names, | ||
| key_values, | ||
| value_names, | ||
| value_values, | ||
| insertion_value_names=insertion_value_names, | ||
| insertion_value_values=insertion_value_values, | ||
| ) | ||
| else: | ||
| return self.simple_upsert_many_txn_emulated( | ||
|
|
@@ -1545,6 +1576,8 @@ def simple_upsert_many_txn( | |
| key_values, | ||
| value_names, | ||
| value_values, | ||
| insertion_value_names=insertion_value_names, | ||
| insertion_value_values=insertion_value_values, | ||
| ) | ||
|
|
||
| def simple_upsert_many_txn_emulated( | ||
|
|
@@ -1555,6 +1588,11 @@ def simple_upsert_many_txn_emulated( | |
| key_values: Collection[Iterable[Any]], | ||
| value_names: Collection[str], | ||
| value_values: Iterable[Iterable[Any]], | ||
| # Given these are the same type as the normal values, force keyword-only so that | ||
| # they can't be confused. | ||
| *, | ||
| insertion_value_names: Collection[str] = [], | ||
| insertion_value_values: Collection[Iterable[Any]] = [], | ||
| ) -> None: | ||
| """ | ||
| Upsert, many times, but without native UPSERT support or batching. | ||
|
|
@@ -1566,18 +1604,26 @@ def simple_upsert_many_txn_emulated( | |
| value_names: The value column names | ||
| value_values: A list of each row's value column values. | ||
| Ignored if value_names is empty. | ||
| insertion_value_names: The value column names to use only when inserting | ||
| insertion_value_values: A list of each row's value column values to use only | ||
| when inserting. Ignored if `insertion_value_names` is empty. | ||
| """ | ||
|
|
||
| # Lock the table just once, to prevent it being done once per row. | ||
| # Note that, according to Postgres' documentation, once obtained, | ||
| # the lock is held for the remainder of the current transaction. | ||
| self.engine.lock_table(txn, table) | ||
|
|
||
| for keyv, valv in zip(key_values, value_values): | ||
| for keyv, valv, insertionv in zip( | ||
| key_values, value_values, insertion_value_values | ||
| ): | ||
| _keys = dict(zip(key_names, keyv)) | ||
| _vals = dict(zip(value_names, valv)) | ||
| _insertion_vals = dict(zip(insertion_value_names, insertionv)) | ||
|
|
||
| self.simple_upsert_txn_emulated(txn, table, _keys, _vals, lock=False) | ||
| self.simple_upsert_txn_emulated( | ||
| txn, table, _keys, _vals, insertion_values=_insertion_vals, lock=False | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def simple_upsert_many_txn_native_upsert( | ||
|
|
@@ -1587,6 +1633,11 @@ def simple_upsert_many_txn_native_upsert( | |
| key_values: Collection[Iterable[Any]], | ||
| value_names: Collection[str], | ||
| value_values: Iterable[Iterable[Any]], | ||
| # Given these are the same type as the normal values, force keyword-only so that | ||
| # they can't be confused. | ||
| *, | ||
| insertion_value_names: Collection[str] = [], | ||
| insertion_value_values: Collection[Iterable[Any]] = [], | ||
| ) -> None: | ||
| """ | ||
| Upsert, many times, using batching where possible. | ||
|
|
@@ -1598,10 +1649,14 @@ def simple_upsert_many_txn_native_upsert( | |
| value_names: The value column names | ||
| value_values: A list of each row's value column values. | ||
| Ignored if value_names is empty. | ||
| insertion_value_names: The value column names to use only when inserting | ||
| insertion_value_values: A list of each row's value column values to use only | ||
| when inserting. Ignored if `insertion_value_names` is empty. | ||
| """ | ||
| allnames: List[str] = [] | ||
| allnames.extend(key_names) | ||
| allnames.extend(value_names) | ||
| allnames.extend(insertion_value_names) | ||
|
|
||
| if not value_names: | ||
| latter = "NOTHING" | ||
|
|
@@ -1612,8 +1667,8 @@ def simple_upsert_many_txn_native_upsert( | |
|
|
||
| args = [] | ||
|
|
||
| for x, y in zip(key_values, value_values): | ||
| args.append(tuple(x) + tuple(y)) | ||
| for x, y, z in zip(key_values, value_values, insertion_value_values): | ||
| args.append(tuple(x) + tuple(y) + tuple(z)) | ||
|
|
||
| if isinstance(txn.database_engine, PostgresEngine): | ||
| # We use `execute_values` as it can be a lot faster than `execute_batch`, | ||
|
|
||
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
Oops, something went wrong.
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.