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
Make background updates controllable via a plugin #11306
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
4a1a832
Store whether a BG update is oneshot or not
erikjohnston 957da6f
Add a `BackgroundUpdateController` class.
erikjohnston 0c3ba88
Add a `register_background_update_controller`
erikjohnston 0ace9f8
Expose a `sleep(..)` func on `ModuleApi`
erikjohnston dccddf1
Add tests
erikjohnston c7f1498
Newsfile
erikjohnston c77bad8
Convert API to use callbacks
erikjohnston dddfdca
Merge remote-tracking branch 'origin/develop' into erikj/bg_update_co…
erikjohnston 3dcda89
Merge branch 'develop' into erikj/bg_update_controller
babolivier 4a1d77e
Remove callback wrapping
babolivier 31a4897
Lint and docstrings
babolivier d89cadd
Don't ignore module callbacks if we don't want to sleep
babolivier df863fb
Rename update handler to avoid name clashes
babolivier 66aae92
Add docs
babolivier f5d551a
Fixup changelog
babolivier 82e880e
Let more time for the update to complete
babolivier 99ef30c
Merge branch 'develop' of github.com:matrix-org/synapse into erikj/bg…
babolivier 26a61b4
Fix test
babolivier 1a99abe
Allow modules to run a function in a thread
babolivier 08bb9b1
Lint
babolivier 8c7ec0f
Retune test_do_background_update
babolivier 589d8ea
Incorporate review comments
babolivier e05809d
Merge branch 'develop' of github.com:matrix-org/synapse into erikj/bg…
babolivier 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 @@ | ||
Add plugin support for controlling database background updates. |
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,71 @@ | ||
# Background update controller callbacks | ||
|
||
Background update controller callbacks allow module developers to control (e.g. rate-limit) | ||
how database background updates are run. A database background update is an operation | ||
Synapse runs on its database in the background after it starts. It's usually used to run | ||
database operations that would take too long if they were run at the same time as schema | ||
updates (which are run on startup) and delay Synapse's startup too much: populating a | ||
table with a big amount of data, adding an index on a big table, deleting superfluous data, | ||
etc. | ||
|
||
Background update controller callbacks can be registered using the module API's | ||
`register_background_update_controller_callbacks` method. Only the first module (in order | ||
of appearance in Synapse's configuration file) calling this method can register background | ||
update controller callbacks, subsequent calls are ignored. | ||
|
||
The available background update controller callbacks are: | ||
|
||
### `on_update` | ||
|
||
_First introduced in Synapse v1.49.0_ | ||
|
||
```python | ||
def on_update(update_name: str, database_name: str, one_shot: bool) -> AsyncContextManager[int] | ||
``` | ||
|
||
Called when about to do an iteration of a background update. The module is given the name | ||
of the update, the name of the database, and a flag to indicate whether the background | ||
update will happen in one go and may take a long time (e.g. creating indices). If this last | ||
argument is set to `False`, the update will be run in batches. | ||
|
||
The module must return an async context manager. It will be entered before Synapse runs a | ||
background update; this should return the desired duration of the iteration, in | ||
milliseconds. | ||
|
||
The context manager will be exited when the iteration completes. Note that the duration | ||
returned by the context manager is a target, and an iteration may take substantially longer | ||
or shorter. If the `one_shot` flag is set to `True`, the duration returned is ignored. | ||
|
||
__Note__: Unlike most module callbacks in Synapse, this one is _synchronous_. This is | ||
because asynchronous operations are expected to be run by the async context manager. | ||
|
||
This callback is required when registering any other background update controller callback. | ||
|
||
### `default_batch_size` | ||
|
||
_First introduced in Synapse v1.49.0_ | ||
|
||
```python | ||
async def default_batch_size(update_name: str, database_name: str) -> int | ||
``` | ||
|
||
Called before the first iteration of a background update, with the name of the update and | ||
of the database. The module must return the number of elements to process in this first | ||
iteration. | ||
|
||
If this callback is not defined, Synapse will use a default value of 100. | ||
|
||
### `min_batch_size` | ||
|
||
_First introduced in Synapse v1.49.0_ | ||
|
||
```python | ||
async def min_batch_size(update_name: str, database_name: str) -> int | ||
``` | ||
|
||
Called before running a new batch for a background update, with the name of the update and | ||
of the database. The module must return an integer representing the minimum number of | ||
elements to process in this iteration. This number must be at least 1, and is used to | ||
ensure that progress is always made. | ||
|
||
If this callback is not defined, Synapse will use a default value of 100. |
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
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
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
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.
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.
Does the plugin author get to know if their callback was ignored?
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.
Not currently.