From dd8cbf08bac0e69f1caf00ec1efec6019eb36c29 Mon Sep 17 00:00:00 2001 From: DMRobertson Date: Tue, 10 Jan 2023 12:19:38 +0000 Subject: deploy: 9a4c69f59f1ac54838d3b0f47280d858b7e41073 --- v1.75/development/cas.html | 251 +++++++++ v1.75/development/contributing_guide.html | 615 +++++++++++++++++++++ v1.75/development/database_schema.html | 367 ++++++++++++ v1.75/development/demo.html | 224 ++++++++ v1.75/development/dependencies.html | 394 +++++++++++++ v1.75/development/experimental_features.html | 222 ++++++++ v1.75/development/git.html | 310 +++++++++++ v1.75/development/img/git/branches.jpg | Bin 0 -> 72228 bytes v1.75/development/img/git/clean.png | Bin 0 -> 110840 bytes v1.75/development/img/git/squash.png | Bin 0 -> 29667 bytes .../development/internal_documentation/index.html | 198 +++++++ v1.75/development/releases.html | 219 ++++++++ v1.75/development/reviews.html | 222 ++++++++ v1.75/development/room-dag-concepts.html | 278 ++++++++++ v1.75/development/saml.html | 227 ++++++++ .../synapse_architecture/cancellation.html | 536 ++++++++++++++++++ 16 files changed, 4063 insertions(+) create mode 100644 v1.75/development/cas.html create mode 100644 v1.75/development/contributing_guide.html create mode 100644 v1.75/development/database_schema.html create mode 100644 v1.75/development/demo.html create mode 100644 v1.75/development/dependencies.html create mode 100644 v1.75/development/experimental_features.html create mode 100644 v1.75/development/git.html create mode 100644 v1.75/development/img/git/branches.jpg create mode 100644 v1.75/development/img/git/clean.png create mode 100644 v1.75/development/img/git/squash.png create mode 100644 v1.75/development/internal_documentation/index.html create mode 100644 v1.75/development/releases.html create mode 100644 v1.75/development/reviews.html create mode 100644 v1.75/development/room-dag-concepts.html create mode 100644 v1.75/development/saml.html create mode 100644 v1.75/development/synapse_architecture/cancellation.html (limited to 'v1.75/development') diff --git a/v1.75/development/cas.html b/v1.75/development/cas.html new file mode 100644 index 0000000000..d1afe72f5a --- /dev/null +++ b/v1.75/development/cas.html @@ -0,0 +1,251 @@ + + + + + + CAS - Synapse + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + +
+
+ +
+ +
+ +

How to test CAS as a developer without a server

+

The django-mama-cas project is an +easy to run CAS implementation built on top of Django.

+

Prerequisites

+
    +
  1. Create a new virtualenv: python3 -m venv <your virtualenv>
  2. +
  3. Activate your virtualenv: source /path/to/your/virtualenv/bin/activate
  4. +
  5. Install Django and django-mama-cas: +
    python -m pip install "django<3" "django-mama-cas==2.4.0"
    +
    +
  6. +
  7. Create a Django project in the current directory: +
    django-admin startproject cas_test .
    +
    +
  8. +
  9. Follow the install directions for django-mama-cas
  10. +
  11. Setup the SQLite database: python manage.py migrate
  12. +
  13. Create a user: +
    python manage.py createsuperuser
    +
    +
      +
    1. Use whatever you want as the username and password.
    2. +
    3. Leave the other fields blank.
    4. +
    +
  14. +
  15. Use the built-in Django test server to serve the CAS endpoints on port 8000: +
    python manage.py runserver
    +
    +
  16. +
+

You should now have a Django project configured to serve CAS authentication with +a single user created.

+

Configure Synapse (and Element) to use CAS

+
    +
  1. Modify your homeserver.yaml to enable CAS and point it to your locally +running Django test server: +
    cas_config:
    +  enabled: true
    +  server_url: "http://localhost:8000"
    +  service_url: "http://localhost:8081"
    +  #displayname_attribute: name
    +  #required_attributes:
    +  #    name: value
    +
    +
  2. +
  3. Restart Synapse.
  4. +
+

Note that the above configuration assumes the homeserver is running on port 8081 +and that the CAS server is on port 8000, both on localhost.

+

Testing the configuration

+

Then in Element:

+
    +
  1. Visit the login page with a Element pointing at your homeserver.
  2. +
  3. Click the Single Sign-On button.
  4. +
  5. Login using the credentials created with createsuperuser.
  6. +
  7. You should be logged in.
  8. +
+

If you want to repeat this process you'll need to manually logout first:

+
    +
  1. http://localhost:8000/admin/
  2. +
  3. Click "logout" in the top right.
  4. +
+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + \ No newline at end of file diff --git a/v1.75/development/contributing_guide.html b/v1.75/development/contributing_guide.html new file mode 100644 index 0000000000..e04f8d1488 --- /dev/null +++ b/v1.75/development/contributing_guide.html @@ -0,0 +1,615 @@ + + + + + + Contributing Guide - Synapse + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + +
+
+ +
+ +
+ +

Contributing

+

This document aims to get you started with contributing to Synapse!

+

1. Who can contribute to Synapse?

+

Everyone is welcome to contribute code to matrix.org +projects, provided that they are willing to +license their contributions under the same license as the project itself. We +follow a simple 'inbound=outbound' model for contributions: the act of +submitting an 'inbound' contribution means that the contributor agrees to +license the code under the same terms as the project's overall 'outbound' +license - in our case, this is almost always Apache Software License v2 (see +LICENSE).

+

2. What do I need?

+

If you are running Windows, the Windows Subsystem for Linux (WSL) is strongly +recommended for development. More information about WSL can be found at +https://docs.microsoft.com/en-us/windows/wsl/install. Running Synapse natively +on Windows is not officially supported.

+

The code of Synapse is written in Python 3. To do pretty much anything, you'll need a recent version of Python 3. Your Python also needs support for virtual environments. This is usually built-in, but some Linux distributions like Debian and Ubuntu split it out into its own package. Running sudo apt install python3-venv should be enough.

+

Synapse can connect to PostgreSQL via the psycopg2 Python library. Building this library from source requires access to PostgreSQL's C header files. On Debian or Ubuntu Linux, these can be installed with sudo apt install libpq-dev.

+

Synapse has an optional, improved user search with better Unicode support. For that you need the development package of libicu. On Debian or Ubuntu Linux, this can be installed with sudo apt install libicu-dev.

+

The source code of Synapse is hosted on GitHub. You will also need a recent version of git.

+

For some tests, you will need a recent version of Docker.

+

A recent version of the Rust compiler is needed to build the native modules. The +easiest way of installing the latest version is to use rustup.

+

3. Get the source.

+

The preferred and easiest way to contribute changes is to fork the relevant +project on GitHub, and then create a pull request to ask us to pull your +changes into our repo.

+

Please base your changes on the develop branch.

+
git clone git@github.com:YOUR_GITHUB_USER_NAME/synapse.git
+git checkout develop
+
+

If you need help getting started with git, this is beyond the scope of the document, but you +can find many good git tutorials on the web.

+

4. Install the dependencies

+

Synapse uses the poetry project to manage its dependencies +and development environment. Once you have installed Python 3 and added the +source, you should install poetry. +Of their installation methods, we recommend +installing poetry using pipx,

+
pip install --user pipx
+pipx install poetry
+
+

but see poetry's installation instructions +for other installation methods.

+

Synapse requires Poetry version 1.2.0 or later.

+

Next, open a terminal and install dependencies as follows:

+
cd path/where/you/have/cloned/the/repository
+poetry install --extras all
+
+

This will install the runtime and developer dependencies for the project.

+

5. Get in touch.

+

Join our developer community on Matrix: #synapse-dev:matrix.org!

+

6. Pick an issue.

+

Fix your favorite problem or perhaps find a Good First Issue +to work on.

+

7. Turn coffee into code and documentation!

+

There is a growing amount of documentation located in the +docs +directory, with a rendered version available online. +This documentation is intended primarily for sysadmins running their +own Synapse instance, as well as developers interacting externally with +Synapse. +docs/development +exists primarily to house documentation for +Synapse developers. +docs/admin_api houses documentation +regarding Synapse's Admin API, which is used mostly by sysadmins and external +service developers.

+

Synapse's code style is documented here. Please follow +it, including the conventions for configuration +options and documentation.

+

We welcome improvements and additions to our documentation itself! When +writing new pages, please +build docs to a book +to check that your contributions render correctly. The docs are written in +GitHub-Flavoured Markdown.

+

Some documentation also exists in Synapse's GitHub +Wiki, although this is primarily +contributed to by community authors.

+

When changes are made to any Rust code then you must call either poetry install +or maturin develop (if installed) to rebuild the Rust code. Using maturin +is quicker than poetry install, so is recommended when making frequent +changes to the Rust code.

+

8. Test, test, test!

+

+

While you're developing and before submitting a patch, you'll +want to test your code.

+

Run the linters.

+

The linters look at your code and do two things:

+
    +
  • ensure that your code follows the coding style adopted by the project;
  • +
  • catch a number of errors in your code.
  • +
+

The linter takes no time at all to run as soon as you've downloaded the dependencies.

+
poetry run ./scripts-dev/lint.sh
+
+

Note that this script will modify your files to fix styling errors. +Make sure that you have saved all your files.

+

If you wish to restrict the linters to only the files changed since the last commit +(much faster!), you can instead run:

+
poetry run ./scripts-dev/lint.sh -d
+
+

Or if you know exactly which files you wish to lint, you can instead run:

+
poetry run ./scripts-dev/lint.sh path/to/file1.py path/to/file2.py path/to/folder
+
+

Run the unit tests (Twisted trial).

+

The unit tests run parts of Synapse, including your changes, to see if anything +was broken. They are slower than the linters but will typically catch more errors.

+
poetry run trial tests
+
+

You can run unit tests in parallel by specifying -jX argument to trial where X is the number of parallel runners you want. To use 4 cpu cores, you would run them like:

+
poetry run trial -j4 tests
+
+

If you wish to only run some unit tests, you may specify +another module instead of tests - or a test class or a method:

+
poetry run trial tests.rest.admin.test_room tests.handlers.test_admin.ExfiltrateData.test_invite
+
+

If your tests fail, you may wish to look at the logs (the default log level is ERROR):

+
less _trial_temp/test.log
+
+

To increase the log level for the tests, set SYNAPSE_TEST_LOG_LEVEL:

+
SYNAPSE_TEST_LOG_LEVEL=DEBUG poetry run trial tests
+
+

By default, tests will use an in-memory SQLite database for test data. For additional +help with debugging, one can use an on-disk SQLite database file instead, in order to +review database state during and after running tests. This can be done by setting +the SYNAPSE_TEST_PERSIST_SQLITE_DB environment variable. Doing so will cause the +database state to be stored in a file named test.db under the trial process' +working directory. Typically, this ends up being _trial_temp/test.db. For example:

+
SYNAPSE_TEST_PERSIST_SQLITE_DB=1 poetry run trial tests
+
+

The database file can then be inspected with:

+
sqlite3 _trial_temp/test.db
+
+

Note that the database file is cleared at the beginning of each test run. Thus it +will always only contain the data generated by the last run test. Though generally +when debugging, one is only running a single test anyway.

+

Running tests under PostgreSQL

+

Invoking trial as above will use an in-memory SQLite database. This is great for +quick development and testing. However, we recommend using a PostgreSQL database +in production (and indeed, we have some code paths specific to each database). +This means that we need to run our unit tests against PostgreSQL too. Our CI does +this automatically for pull requests and release candidates, but it's sometimes +useful to reproduce this locally.

+

Using Docker

+

The easiest way to do so is to run Postgres via a docker container. In one +terminal:

+
docker run --rm -e POSTGRES_PASSWORD=mysecretpassword -e POSTGRES_USER=postgres -e POSTGRES_DB=postgress -p 5432:5432 postgres:14
+
+

If you see an error like

+
docker: Error response from daemon: driver failed programming external connectivity on endpoint nice_ride (b57bbe2e251b70015518d00c9981e8cb8346b5c785250341a6c53e3c899875f1): Error starting userland proxy: listen tcp4 0.0.0.0:5432: bind: address already in use.
+
+

then something is already bound to port 5432. You're probably already running postgres locally.

+

Once you have a postgres server running, invoke trial in a second terminal:

+
SYNAPSE_POSTGRES=1 SYNAPSE_POSTGRES_HOST=127.0.0.1 SYNAPSE_POSTGRES_USER=postgres SYNAPSE_POSTGRES_PASSWORD=mysecretpassword poetry run trial tests
+
+

Using an existing Postgres installation

+

If you have postgres already installed on your system, you can run trial with the +following environment variables matching your configuration:

+
    +
  • SYNAPSE_POSTGRES to anything nonempty
  • +
  • SYNAPSE_POSTGRES_HOST (optional if it's the default: UNIX socket)
  • +
  • SYNAPSE_POSTGRES_PORT (optional if it's the default: 5432)
  • +
  • SYNAPSE_POSTGRES_USER (optional if using a UNIX socket)
  • +
  • SYNAPSE_POSTGRES_PASSWORD (optional if using a UNIX socket)
  • +
+

For example:

+
export SYNAPSE_POSTGRES=1
+export SYNAPSE_POSTGRES_HOST=localhost
+export SYNAPSE_POSTGRES_USER=postgres
+export SYNAPSE_POSTGRES_PASSWORD=mydevenvpassword
+trial
+
+

You don't need to specify the host, user, port or password if your Postgres +server is set to authenticate you over the UNIX socket (i.e. if the psql command +works without further arguments).

+

Your Postgres account needs to be able to create databases; see the postgres +docs for ALTER ROLE.

+

Run the integration tests (Sytest).

+

The integration tests are a more comprehensive suite of tests. They +run a full version of Synapse, including your changes, to check if +anything was broken. They are slower than the unit tests but will +typically catch more errors.

+

The following command will let you run the integration test with the most common +configuration:

+
$ docker run --rm -it -v /path/where/you/have/cloned/the/repository\:/src:ro -v /path/to/where/you/want/logs\:/logs matrixdotorg/sytest-synapse:buster
+
+

(Note that the paths must be full paths! You could also write $(realpath relative/path) if needed.)

+

This configuration should generally cover your needs.

+
    +
  • To run with Postgres, supply the -e POSTGRES=1 -e MULTI_POSTGRES=1 environment flags.
  • +
  • To run with Synapse in worker mode, supply the -e WORKERS=1 -e REDIS=1 environment flags (in addition to the Postgres flags).
  • +
+

For more details about other configurations, see the Docker-specific documentation in the SyTest repo.

+

Run the integration tests (Complement).

+

Complement is a suite of black box tests that can be run on any homeserver implementation. It can also be thought of as end-to-end (e2e) tests.

+

It's often nice to develop on Synapse and write Complement tests at the same time. +Here is how to run your local Synapse checkout against your local Complement checkout.

+

(checkout complement alongside your synapse checkout)

+
COMPLEMENT_DIR=../complement ./scripts-dev/complement.sh
+
+

To run a specific test file, you can pass the test name at the end of the command. The name passed comes from the naming structure in your Complement tests. If you're unsure of the name, you can do a full run and copy it from the test output:

+
COMPLEMENT_DIR=../complement ./scripts-dev/complement.sh -run TestImportHistoricalMessages
+
+

To run a specific test, you can specify the whole name structure:

+
COMPLEMENT_DIR=../complement ./scripts-dev/complement.sh -run TestImportHistoricalMessages/parallel/Historical_events_resolve_in_the_correct_order
+
+

The above will run a monolithic (single-process) Synapse with SQLite as the database. For other configurations, try:

+
    +
  • Passing POSTGRES=1 as an environment variable to use the Postgres database instead.
  • +
  • Passing WORKERS=1 as an environment variable to use a workerised setup instead. This option implies the use of Postgres. +
      +
    • If setting WORKERS=1, optionally set WORKER_TYPES= to declare which worker +types you wish to test. A simple comma-delimited string containing the worker types +defined from the WORKERS_CONFIG template in +here. +A safe example would be WORKER_TYPES="federation_inbound, federation_sender, synchrotron". +See the worker documentation for additional information on workers.
    • +
    +
  • +
+

To increase the log level for the tests, set SYNAPSE_TEST_LOG_LEVEL, e.g:

+
SYNAPSE_TEST_LOG_LEVEL=DEBUG COMPLEMENT_DIR=../complement ./scripts-dev/complement.sh -run TestImportHistoricalMessages
+
+

Prettier formatting with gotestfmt

+

If you want to format the output of the tests the same way as it looks in CI, +install gotestfmt.

+

You can then use this incantation to format the tests appropriately:

+
COMPLEMENT_DIR=../complement ./scripts-dev/complement.sh -json | gotestfmt -hide successful-tests
+
+

(Remove -hide successful-tests if you don't want to hide successful tests.)

+

Access database for homeserver after Complement test runs.

+

If you're curious what the database looks like after you run some tests, here are some steps to get you going in Synapse:

+
    +
  1. In your Complement test comment out defer deployment.Destroy(t) and replace with defer time.Sleep(2 * time.Hour) to keep the homeserver running after the tests complete
  2. +
  3. Start the Complement tests
  4. +
  5. Find the name of the container, docker ps -f name=complement_ (this will filter for just the Compelement related Docker containers)
  6. +
  7. Access the container replacing the name with what you found in the previous step: docker exec -it complement_1_hs_with_application_service.hs1_2 /bin/bash
  8. +
  9. Install sqlite (database driver), apt-get update && apt-get install -y sqlite3
  10. +
  11. Then run sqlite3 and open the database .open /conf/homeserver.db (this db path comes from the Synapse homeserver.yaml)
  12. +
+

9. Submit your patch.

+

Once you're happy with your patch, it's time to prepare a Pull Request.

+

To prepare a Pull Request, please:

+
    +
  1. verify that all the tests pass, including the coding style;
  2. +
  3. sign off your contribution;
  4. +
  5. git push your commit to your fork of Synapse;
  6. +
  7. on GitHub, create the Pull Request;
  8. +
  9. add a changelog entry and push it to your Pull Request;
  10. +
  11. that's it for now, a non-draft pull request will automatically request review from the team;
  12. +
  13. if you need to update your PR, please avoid rebasing and just add new commits to your branch.
  14. +
+

Changelog

+

All changes, even minor ones, need a corresponding changelog / newsfragment +entry. These are managed by Towncrier.

+

To create a changelog entry, make a new file in the changelog.d directory named +in the format of PRnumber.type. The type can be one of the following:

+
    +
  • feature
  • +
  • bugfix
  • +
  • docker (for updates to the Docker image)
  • +
  • doc (for updates to the documentation)
  • +
  • removal (also used for deprecations)
  • +
  • misc (for internal-only changes)
  • +
+

This file will become part of our changelog at the next +release, so the content of the file should be a short description of your +change in the same style as the rest of the changelog. The file can contain Markdown +formatting, and must end with a full stop (.) or an exclamation mark (!) for +consistency.

+

Adding credits to the changelog is encouraged, we value your +contributions and would like to have you shouted out in the release notes!

+

For example, a fix in PR #1234 would have its changelog entry in +changelog.d/1234.bugfix, and contain content like:

+
+

The security levels of Florbs are now validated when received +via the /federation/florb endpoint. Contributed by Jane Matrix.

+
+

If there are multiple pull requests involved in a single bugfix/feature/etc, +then the content for each changelog.d file should be the same. Towncrier will +merge the matching files together into a single changelog entry when we come to +release.

+

How do I know what to call the changelog file before I create the PR?

+

Obviously, you don't know if you should call your newsfile +1234.bugfix or 5678.bugfix until you create the PR, which leads to a +chicken-and-egg problem.

+

There are two options for solving this:

+
    +
  1. +

    Open the PR without a changelog file, see what number you got, and then +add the changelog file to your branch, or:

    +
  2. +
  3. +

    Look at the list of all +issues/PRs, add one to the +highest number you see, and quickly open the PR before somebody else claims +your number.

    +

    This +script +might be helpful if you find yourself doing this a lot.

    +
  4. +
+

Sorry, we know it's a bit fiddly, but it's really helpful for us when we come +to put together a release!

+

Debian changelog

+

Changes which affect the debian packaging files (in debian) are an +exception to the rule that all changes require a changelog.d file.

+

In this case, you will need to add an entry to the debian changelog for the +next release. For this, run the following command:

+
dch
+
+

This will make up a new version number (if there isn't already an unreleased +version in flight), and open an editor where you can add a new changelog entry. +(Our release process will ensure that the version number and maintainer name is +corrected for the release.)

+

If your change affects both the debian packaging and files outside the debian +directory, you will need both a regular newsfragment and an entry in the +debian changelog. (Though typically such changes should be submitted as two +separate pull requests.)

+

Sign off

+

In order to have a concrete record that your contribution is intentional +and you agree to license it under the same terms as the project's license, we've adopted the +same lightweight approach that the Linux Kernel +submitting patches process, +Docker, and many other +projects use: the DCO (Developer Certificate of Origin). +This is a simple declaration that you wrote +the contribution or otherwise have the right to contribute it to Matrix:

+
Developer Certificate of Origin
+Version 1.1
+
+Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
+660 York Street, Suite 102,
+San Francisco, CA 94110 USA
+
+Everyone is permitted to copy and distribute verbatim copies of this
+license document, but changing it is not allowed.
+
+Developer's Certificate of Origin 1.1
+
+By making a contribution to this project, I certify that:
+
+(a) The contribution was created in whole or in part by me and I
+    have the right to submit it under the open source license
+    indicated in the file; or
+
+(b) The contribution is based upon previous work that, to the best
+    of my knowledge, is covered under an appropriate open source
+    license and I have the right under that license to submit that
+    work with modifications, whether created in whole or in part
+    by me, under the same open source license (unless I am
+    permitted to submit under a different license), as indicated
+    in the file; or
+
+(c) The contribution was provided directly to me by some other
+    person who certified (a), (b) or (c) and I have not modified
+    it.
+
+(d) I understand and agree that this project and the contribution
+    are public and that a record of the contribution (including all
+    personal information I submit with it, including my sign-off) is
+    maintained indefinitely and may be redistributed consistent with
+    this project or the open source license(s) involved.
+
+

If you agree to this for your contribution, then all that's needed is to +include the line in your commit or pull request comment:

+
Signed-off-by: Your Name <your@email.example.org>
+
+

We accept contributions under a legally identifiable name, such as +your name on government documentation or common-law names (names +claimed by legitimate usage or repute). Unfortunately, we cannot +accept anonymous contributions at this time.

+

Git allows you to add this signoff automatically when using the -s +flag to git commit, which uses the name and email set in your +user.name and user.email git configs.

+

Private Sign off

+

If you would like to provide your legal name privately to the Matrix.org +Foundation (instead of in a public commit or comment), you can do so +by emailing your legal name and a link to the pull request to +dco@matrix.org. +It helps to include "sign off" or similar in the subject line. You will then +be instructed further.

+

Once private sign off is complete, doing so for future contributions will not +be required.

+

10. Turn feedback into better code.

+

Once the Pull Request is opened, you will see a few things:

+
    +
  1. our automated CI (Continuous Integration) pipeline will run (again) the linters, the unit tests, the integration tests and more;
  2. +
  3. one or more of the developers will take a look at your Pull Request and offer feedback.
  4. +
+

From this point, you should:

+
    +
  1. Look at the results of the CI pipeline. +
      +
    • If there is any error, fix the error.
    • +
    +
  2. +
  3. If a developer has requested changes, make these changes and let us know if it is ready for a developer to review again. +
      +
    • A pull request is a conversation, if you disagree with the suggestions, please respond and discuss it.
    • +
    +
  4. +
  5. Create a new commit with the changes. +
      +
    • Please do NOT overwrite the history. New commits make the reviewer's life easier.
    • +
    • Push this commits to your Pull Request.
    • +
    +
  6. +
  7. Back to 1.
  8. +
  9. Once the pull request is ready for review again please re-request review from whichever developer did your initial +review (or leave a comment in the pull request that you believe all required changes have been done).
  10. +
+

Once both the CI and the developers are happy, the patch will be merged into Synapse and released shortly!

+

11. Find a new issue.

+

By now, you know the drill!

+

Notes for maintainers on merging PRs etc

+

There are some notes for those with commit access to the project on how we +manage git here.

+

Conclusion

+

That's it! Matrix is a very open and collaborative project as you might expect +given our obsession with open communication. If we're going to successfully +matrix together all the fragmented communication technologies out there we are +reliant on contributions and collaboration from the community to do so. So +please get involved - and we hope you have as much fun hacking on Matrix as we +do!

+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + \ No newline at end of file diff --git a/v1.75/development/database_schema.html b/v1.75/development/database_schema.html new file mode 100644 index 0000000000..5d830745e0 --- /dev/null +++ b/v1.75/development/database_schema.html @@ -0,0 +1,367 @@ + + + + + + Database Schemas - Synapse + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + +
+
+ +
+ +
+ +

Synapse database schema files

+

Synapse's database schema is stored in the synapse.storage.schema module.

+

Logical databases

+

Synapse supports splitting its datastore across multiple physical databases (which can +be useful for large installations), and the schema files are therefore split according +to the logical database they apply to.

+

At the time of writing, the following "logical" databases are supported:

+
    +
  • state - used to store Matrix room state (more specifically, state_groups, +their relationships and contents).
  • +
  • main - stores everything else.
  • +
+

Additionally, the common directory contains schema files for tables which must be +present on all physical databases.

+

Synapse schema versions

+

Synapse manages its database schema via "schema versions". These are mainly used to +help avoid confusion if the Synapse codebase is rolled back after the database is +updated. They work as follows:

+
    +
  • +

    The Synapse codebase defines a constant synapse.storage.schema.SCHEMA_VERSION +which represents the expectations made about the database by that version. For +example, as of Synapse v1.36, this is 59.

    +
  • +
  • +

    The database stores a "compatibility version" in +schema_compat_version.compat_version which defines the SCHEMA_VERSION of the +oldest version of Synapse which will work with the database. On startup, if +compat_version is found to be newer than SCHEMA_VERSION, Synapse will refuse to +start.

    +

    Synapse automatically updates this field from +synapse.storage.schema.SCHEMA_COMPAT_VERSION.

    +
  • +
  • +

    Whenever a backwards-incompatible change is made to the database format (normally +via a delta file), synapse.storage.schema.SCHEMA_COMPAT_VERSION is also updated +so that administrators can not accidentally roll back to a too-old version of Synapse.

    +
  • +
+

Generally, the goal is to maintain compatibility with at least one or two previous +releases of Synapse, so any substantial change tends to require multiple releases and a +bit of forward-planning to get right.

+

As a worked example: we want to remove the room_stats_historical table. Here is how it +might pan out.

+
    +
  1. +

    Replace any code that reads from room_stats_historical with alternative +implementations, but keep writing to it in case of rollback to an earlier version. +Also, increase synapse.storage.schema.SCHEMA_VERSION. In this +instance, there is no existing code which reads from room_stats_historical, so +our starting point is:

    +

    v1.36.0: SCHEMA_VERSION=59, SCHEMA_COMPAT_VERSION=59

    +
  2. +
  3. +

    Next (say in Synapse v1.37.0): remove the code that writes to +room_stats_historical, but don’t yet remove the table in case of rollback to +v1.36.0. Again, we increase synapse.storage.schema.SCHEMA_VERSION, but +because we have not broken compatibility with v1.36, we do not yet update +SCHEMA_COMPAT_VERSION. We now have:

    +

    v1.37.0: SCHEMA_VERSION=60, SCHEMA_COMPAT_VERSION=59.

    +
  4. +
  5. +

    Later (say in Synapse v1.38.0): we can remove the table altogether. This will +break compatibility with v1.36.0, so we must update SCHEMA_COMPAT_VERSION accordingly. +There is no need to update synapse.storage.schema.SCHEMA_VERSION, since there is no +change to the Synapse codebase here. So we end up with:

    +

    v1.38.0: SCHEMA_VERSION=60, SCHEMA_COMPAT_VERSION=60.

    +
  6. +
+

If in doubt about whether to update SCHEMA_VERSION or not, it is generally best to +lean towards doing so.

+

Full schema dumps

+

In the full_schemas directories, only the most recently-numbered snapshot is used +(54 at the time of writing). Older snapshots (eg, 16) are present for historical +reference only.

+

Building full schema dumps

+

If you want to recreate these schemas, they need to be made from a database that +has had all background updates run.

+

To do so, use scripts-dev/make_full_schema.sh. This will produce new +full.sql.postgres and full.sql.sqlite files.

+

Ensure postgres is installed, then run:

+
./scripts-dev/make_full_schema.sh -p postgres_username -o output_dir/
+
+

NB at the time of writing, this script predates the split into separate state/main +databases so will require updates to handle that correctly.

+

Delta files

+

Delta files define the steps required to upgrade the database from an earlier version. +They can be written as either a file containing a series of SQL statements, or a Python +module.

+

Synapse remembers which delta files it has applied to a database (they are stored in the +applied_schema_deltas table) and will not re-apply them (even if a given file is +subsequently updated).

+

Delta files should be placed in a directory named synapse/storage/schema/<database>/delta/<version>/. +They are applied in alphanumeric order, so by convention the first two characters +of the filename should be an integer such as 01, to put the file in the right order.

+

SQL delta files

+

These should be named *.sql, or — for changes which should only be applied for a +given database engine — *.sql.posgres or *.sql.sqlite. For example, a delta which +adds a new column to the foo table might be called 01add_bar_to_foo.sql.

+

Note that our SQL parser is a bit simple - it understands comments (-- and /*...*/), +but complex statements which require a ; in the middle of them (such as CREATE TRIGGER) are beyond it and you'll have to use a Python delta file.

+

Python delta files

+

For more flexibility, a delta file can take the form of a python module. These should +be named *.py. Note that database-engine-specific modules are not supported here – +instead you can write if isinstance(database_engine, PostgresEngine) or similar.

+

A Python delta module should define either or both of the following functions:

+
import synapse.config.homeserver
+import synapse.storage.engines
+import synapse.storage.types
+
+
+def run_create(
+    cur: synapse.storage.types.Cursor,
+    database_engine: synapse.storage.engines.BaseDatabaseEngine,
+) -> None:
+    """Called whenever an existing or new database is to be upgraded"""
+    ...
+
+def run_upgrade(
+    cur: synapse.storage.types.Cursor,
+    database_engine: synapse.storage.engines.BaseDatabaseEngine,
+    config: synapse.config.homeserver.HomeServerConfig,
+) -> None:
+    """Called whenever an existing database is to be upgraded."""
+    ...
+
+

Boolean columns

+

Boolean columns require special treatment, since SQLite treats booleans the +same as integers.

+

There are three separate aspects to this:

+
    +
  • +

    Any new boolean column must be added to the BOOLEAN_COLUMNS list in +synapse/_scripts/synapse_port_db.py. This tells the port script to cast +the integer value from SQLite to a boolean before writing the value to the +postgres database.

    +
  • +
  • +

    Before SQLite 3.23, TRUE and FALSE were not recognised as constants by +SQLite, and the IS [NOT] TRUE/IS [NOT] FALSE operators were not +supported. This makes it necessary to avoid using TRUE and FALSE +constants in SQL commands.

    +

    For example, to insert a TRUE value into the database, write:

    +
    txn.execute("INSERT INTO tbl(col) VALUES (?)", (True, ))
    +
    +
  • +
  • +

    Default values for new boolean columns present a particular +difficulty. Generally it is best to create separate schema files for +Postgres and SQLite. For example:

    +
    # in 00delta.sql.postgres:
    +ALTER TABLE tbl ADD COLUMN col BOOLEAN DEFAULT FALSE;
    +
    +
    # in 00delta.sql.sqlite:
    +ALTER TABLE tbl ADD COLUMN col BOOLEAN DEFAULT 0;
    +
    +

    Note that there is a particularly insidious failure mode here: the Postgres +flavour will be accepted by SQLite 3.22, but will give a column whose +default value is the string "FALSE" - which, when cast back to a boolean +in Python, evaluates to True.

    +
  • +
+

event_id global uniqueness

+

event_id's can be considered globally unique although there has been a lot of +debate on this topic in places like +MSC2779 and +MSC2848 which +has no resolution yet (as of 2022-09-01). There are several places in Synapse +and even in the Matrix APIs like GET /_matrix/federation/v1/event/{eventId} +where we assume that event IDs are globally unique.

+

When scoping event_id in a database schema, it is often nice to accompany it +with room_id (PRIMARY KEY (room_id, event_id) and a FOREIGN KEY(room_id) REFERENCES rooms(room_id)) which makes flexible lookups easy. For example it +makes it very easy to find and clean up everything in a room when it needs to be +purged (no need to use sub-select query or join from the events table).

+

A note on collisions: In room versions 1 and 2 it's possible to end up with +two events with the same event_id (in the same or different rooms). After room +version 3, that can only happen with a hash collision, which we basically hope +will never happen (SHA256 has a massive big key space).

+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + \ No newline at end of file diff --git a/v1.75/development/demo.html b/v1.75/development/demo.html new file mode 100644 index 0000000000..131c66d4ae --- /dev/null +++ b/v1.75/development/demo.html @@ -0,0 +1,224 @@ + + + + + + Demo scripts - Synapse + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + +
+
+ +
+ +
+ +

Synapse demo setup

+

DO NOT USE THESE DEMO SERVERS IN PRODUCTION

+

Requires you to have a Synapse development environment setup.

+

The demo setup allows running three federation Synapse servers, with server +names localhost:8480, localhost:8481, and localhost:8482.

+

You can access them via any Matrix client over HTTP at localhost:8080, +localhost:8081, and localhost:8082 or over HTTPS at localhost:8480, +localhost:8481, and localhost:8482.

+

To enable the servers to communicate, self-signed SSL certificates are generated +and the servers are configured in a highly insecure way, including:

+
    +
  • Not checking certificates over federation.
  • +
  • Not verifying keys.
  • +
+

The servers are configured to store their data under demo/8080, demo/8081, and +demo/8082. This includes configuration, logs, SQLite databases, and media.

+

Note that when joining a public room on a different homeserver via "#foo:bar.net", +then you are (in the current implementation) joining a room with room_id "foo". +This means that it won't work if your homeserver already has a room with that +name.

+

Using the demo scripts

+

There's three main scripts with straightforward purposes:

+
    +
  • start.sh will start the Synapse servers, generating any missing configuration. +
      +
    • This accepts a single parameter --no-rate-limit to "disable" rate limits +(they actually still exist, but are very high).
    • +
    +
  • +
  • stop.sh will stop the Synapse servers.
  • +
  • clean.sh will delete the configuration, databases, log files, etc.
  • +
+

To start a completely new set of servers, run:

+
./demo/stop.sh; ./demo/clean.sh && ./demo/start.sh
+
+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + \ No newline at end of file diff --git a/v1.75/development/dependencies.html b/v1.75/development/dependencies.html new file mode 100644 index 0000000000..a0823a71c6 --- /dev/null +++ b/v1.75/development/dependencies.html @@ -0,0 +1,394 @@ + + + + + + Dependency management - Synapse + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + +
+
+ +
+ +
+ +

Managing dependencies with Poetry

+

This is a quick cheat sheet for developers on how to use poetry.

+

Background

+

Synapse uses a variety of third-party Python packages to function as a homeserver. +Some of these are direct dependencies, listed in pyproject.toml under the +[tool.poetry.dependencies] section. The rest are transitive dependencies (the +things that our direct dependencies themselves depend on, and so on recursively.)

+

We maintain a locked list of all our dependencies (transitive included) so that +we can track exactly which version of each dependency appears in a given release. +See here +for discussion of why we wanted this for Synapse. We chose to use +poetry to manage this locked list; see +this comment +for the reasoning.

+

The locked dependencies get included in our "self-contained" releases: namely, +our docker images and our debian packages. We also use the locked dependencies +in development and our continuous integration.

+

Separately, our "broad" dependencies—the version ranges specified in +pyproject.toml—are included as metadata in our "sdists" and "wheels" uploaded +to PyPI. Installing from PyPI or from +the Synapse source tree directly will not use the locked dependencies; instead, +they'll pull in the latest version of each package available at install time.

+

Example dependency

+

An example may help. We have a broad dependency on +phonenumbers, as declared in +this snippet from pyproject.toml as of Synapse 1.57:

+
[tool.poetry.dependencies]
+# ...
+phonenumbers = ">=8.2.0"
+
+

In our lockfile this is +pinned +to version 8.12.44, even though +newer versions are available.

+
[[package]]
+name = "phonenumbers"
+version = "8.12.44"
+description = "Python version of Google's common library for parsing, formatting, storing and validating international phone numbers."
+category = "main"
+optional = false
+python-versions = "*"
+
+

The lockfile also includes a +cryptographic checksum +of the sdists and wheels provided for this version of phonenumbers.

+
[metadata.files]
+# ...
+phonenumbers = [
+    {file = "phonenumbers-8.12.44-py2.py3-none-any.whl", hash = "sha256:cc1299cf37b309ecab6214297663ab86cb3d64ae37fd5b88e904fe7983a874a6"},
+    {file = "phonenumbers-8.12.44.tar.gz", hash = "sha256:26cfd0257d1704fe2f88caff2caabb70d16a877b1e65b6aae51f9fbbe10aa8ce"},
+]
+
+

We can see this pinned version inside the docker image for that release:

+
$ docker pull matrixdotorg/synapse:v1.57.0
+...
+$ docker run --entrypoint pip matrixdotorg/synapse:v1.57.0 show phonenumbers
+Name: phonenumbers
+Version: 8.12.44
+Summary: Python version of Google's common library for parsing, formatting, storing and validating international phone numbers.
+Home-page: https://github.com/daviddrysdale/python-phonenumbers
+Author: David Drysdale
+Author-email: dmd@lurklurk.org
+License: Apache License 2.0
+Location: /usr/local/lib/python3.9/site-packages
+Requires:
+Required-by: matrix-synapse
+
+

Whereas the wheel metadata just contains the broad dependencies:

+
$ cd /tmp
+$ wget https://files.pythonhosted.org/packages/ca/5e/d722d572cc5b3092402b783d6b7185901b444427633bd8a6b00ea0dd41b7/matrix_synapse-1.57.0rc1-py3-none-any.whl
+...
+$ unzip -c matrix_synapse-1.57.0rc1-py3-none-any.whl matrix_synapse-1.57.0rc1.dist-info/METADATA | grep phonenumbers
+Requires-Dist: phonenumbers (>=8.2.0)
+
+

Tooling recommendation: direnv

+

direnv is a tool for activating environments in your +shell inside a given directory. Its support for poetry is unofficial (a +community wiki recipe only), but works solidly in our experience. We thoroughly +recommend it for daily use. To use it:

+
    +
  1. Install direnv - it's likely +packaged for your system already.
  2. +
  3. Teach direnv about poetry. The shell config here +needs to be added to ~/.config/direnv/direnvrc (or more generally $XDG_CONFIG_HOME/direnv/direnvrc).
  4. +
  5. Mark the synapse checkout as a poetry project: echo layout poetry > .envrc.
  6. +
  7. Convince yourself that you trust this .envrc configuration and project. +Then formally confirm this to direnv by running direnv allow.
  8. +
+

Then whenever you navigate to the synapse checkout, you should be able to run +e.g. mypy instead of poetry run mypy; python instead of +poetry run python; and your shell commands will automatically run in the +context of poetry's venv, without having to run poetry shell beforehand.

+

How do I...

+

...reset my venv to the locked environment?

+
poetry install --extras all --remove-untracked
+
+

...delete everything and start over from scratch?

+
# Stop the current virtualenv if active
+$ deactivate
+
+# Remove all of the files from the current environment.
+# Don't worry, even though it says "all", this will only
+# remove the Poetry virtualenvs for the current project.
+$ poetry env remove --all
+
+# Reactivate Poetry shell to create the virtualenv again
+$ poetry shell
+# Install everything again
+$ poetry install --extras all
+
+

...run a command in the poetry virtualenv?

+

Use poetry run cmd args when you need the python virtualenv context. +To avoid typing poetry run all the time, you can run poetry shell +to start a new shell in the poetry virtualenv context. Within poetry shell, +python, pip, mypy, trial, etc. are all run inside the project virtualenv +and isolated from the rest o the system.

+

Roughly speaking, the translation from a traditional virtualenv is:

+
    +
  • env/bin/activate -> poetry shell, and
  • +
  • deactivate -> close the terminal (Ctrl-D, exit, etc.)
  • +
+

See also the direnv recommendation above, which makes poetry run and +poetry shell unnecessary.

+

...inspect the poetry virtualenv?

+

Some suggestions:

+
# Current env only
+poetry env info
+# All envs: this allows you to have e.g. a poetry managed venv for Python 3.7,
+# and another for Python 3.10.
+poetry env list --full-path
+poetry run pip list
+
+

Note that poetry show describes the abstract lock file rather than your +on-disk environment. With that said, poetry show --tree can sometimes be +useful.

+

...add a new dependency?

+

Either:

+
    +
  • manually update pyproject.toml; then poetry lock --no-update; or else
  • +
  • poetry add packagename. See poetry add --help; note the --dev, +--extras and --optional flags in particular. +
      +
    • NB: this specifies the new package with a version given by a "caret bound". This won't get forced to its lowest version in the old deps CI job: see this TODO.
    • +
    +
  • +
+

Include the updated pyproject.toml and poetry.lock files in your commit.

+

...remove a dependency?

+

This is not done often and is untested, but

+
poetry remove packagename
+
+

ought to do the trick. Alternatively, manually update pyproject.toml and +poetry lock --no-update. Include the updated pyproject.toml and poetry.lock` +files in your commit.

+

...update the version range for an existing dependency?

+

Best done by manually editing pyproject.toml, then poetry lock --no-update. +Include the updated pyproject.toml and poetry.lock in your commit.

+

...update a dependency in the locked environment?

+

Use

+
poetry update packagename
+
+

to use the latest version of packagename in the locked environment, without +affecting the broad dependencies listed in the wheel.

+

There doesn't seem to be a way to do this whilst locking a specific version of +packagename. We can workaround this (crudely) as follows:

+
poetry add packagename==1.2.3
+# This should update pyproject.lock.
+
+# Now undo the changes to pyproject.toml. For example
+# git restore pyproject.toml
+
+# Get poetry to recompute the content-hash of pyproject.toml without changing
+# the locked package versions.
+poetry lock --no-update
+
+

Either way, include the updated poetry.lock file in your commit.

+

...export a requirements.txt file?

+
poetry export --extras all
+
+

Be wary of bugs in poetry export and pip install -r requirements.txt.

+

Note: poetry export will be made a plugin in Poetry 1.2. Additional config may +be required.

+

...build a test wheel?

+

I usually use

+
poetry run pip install build && poetry run python -m build
+
+

because build is a standardish tool which +doesn't require poetry. (It's what we use in CI too). However, you could try +poetry build too.

+

Troubleshooting

+

Check the version of poetry with poetry --version.

+

The minimum version of poetry supported by Synapse is 1.2.

+

It can also be useful to check the version of poetry-core in use. If you've +installed poetry with pipx, try pipx runpip poetry list | grep poetry-core.

+

Clear caches: poetry cache clear --all pypi.

+

Poetry caches a bunch of information about packages that isn't readily available +from PyPI. (This is what makes poetry seem slow when doing the first +poetry install.) Try poetry cache list and poetry cache clear --all <name of cache> to see if that fixes things.

+

Remove outdated egg-info

+

Delete the matrix_synapse.egg-info/ directory from the root of your Synapse +install.

+

This stores some cached information about dependencies and often conflicts with +letting Poetry do the right thing.

+

Try --verbose or --dry-run arguments.

+

Sometimes useful to see what poetry's internal logic is.

+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + \ No newline at end of file diff --git a/v1.75/development/experimental_features.html b/v1.75/development/experimental_features.html new file mode 100644 index 0000000000..4293989190 --- /dev/null +++ b/v1.75/development/experimental_features.html @@ -0,0 +1,222 @@ + + + + + + Experimental features - Synapse + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + +
+
+ +
+ +
+ +

Implementing experimental features in Synapse

+

It can be desirable to implement "experimental" features which are disabled by +default and must be explicitly enabled via the Synapse configuration. This is +applicable for features which:

+
    +
  • Are unstable in the Matrix spec (e.g. those defined by an MSC that has not yet been merged).
  • +
  • Developers are not confident in their use by general Synapse administrators/users +(e.g. a feature is incomplete, buggy, performs poorly, or needs further testing).
  • +
+

Note that this only really applies to features which are expected to be desirable +to a broad audience. The module infrastructure should +instead be investigated for non-standard features.

+

Guarding experimental features behind configuration flags should help with some +of the following scenarios:

+
    +
  • Ensure that clients do not assume that unstable features exist (failing +gracefully if they do not).
  • +
  • Unstable features do not become de-facto standards and can be removed +aggressively (since only those who have opted-in will be affected).
  • +
  • Ease finding the implementation of unstable features in Synapse (for future +removal or stabilization).
  • +
  • Ease testing a feature (or removal of feature) due to enabling/disabling without +code changes. It also becomes possible to ask for wider testing, if desired.
  • +
+

Experimental configuration flags should be disabled by default (requiring Synapse +administrators to explicitly opt-in), although there are situations where it makes +sense (from a product point-of-view) to enable features by default. This is +expected and not an issue.

+

It is not a requirement for experimental features to be behind a configuration flag, +but one should be used if unsure.

+

New experimental configuration flags should be added under the experimental +configuration key (see the synapse.config.experimental file) and either explain +(briefly) what is being enabled, or include the MSC number.

+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + \ No newline at end of file diff --git a/v1.75/development/git.html b/v1.75/development/git.html new file mode 100644 index 0000000000..1a2a3ed0ed --- /dev/null +++ b/v1.75/development/git.html @@ -0,0 +1,310 @@ + + + + + + Git Usage - Synapse + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + +
+
+ +
+ +
+ +

Some notes on how we use git

+

On keeping the commit history clean

+

In an ideal world, our git commit history would be a linear progression of +commits each of which contains a single change building on what came +before. Here, by way of an arbitrary example, is the top of git log --graph b2dba0607:

+clean git graph +

Note how the commit comment explains clearly what is changing and why. Also +note the absence of merge commits, as well as the absence of commits called +things like (to pick a few culprits): +“pep8”, “fix broken +test”, +“oops”, +“typo”, or “Who's +the president?”.

+

There are a number of reasons why keeping a clean commit history is a good +thing:

+
    +
  • +

    From time to time, after a change lands, it turns out to be necessary to +revert it, or to backport it to a release branch. Those operations are +much easier when the change is contained in a single commit.

    +
  • +
  • +

    Similarly, it's much easier to answer questions like “is the fix for +/publicRooms on the release branch?” if that change consists of a single +commit.

    +
  • +
  • +

    Likewise: “what has changed on this branch in the last week?” is much +clearer without merges and “pep8” commits everywhere.

    +
  • +
  • +

    Sometimes we need to figure out where a bug got introduced, or some +behaviour changed. One way of doing that is with git bisect: pick an +arbitrary commit between the known good point and the known bad point, and +see how the code behaves. However, that strategy fails if the commit you +chose is the middle of someone's epic branch in which they broke the world +before putting it back together again.

    +
  • +
+

One counterargument is that it is sometimes useful to see how a PR evolved as +it went through review cycles. This is true, but that information is always +available via the GitHub UI (or via the little-known refs/pull +namespace).

+

Of course, in reality, things are more complicated than that. We have release +branches as well as develop and master, and we deliberately merge changes +between them. Bugs often slip through and have to be fixed later. That's all +fine: this not a cast-iron rule which must be obeyed, but an ideal to aim +towards.

+

Merges, squashes, rebases: wtf?

+

Ok, so that's what we'd like to achieve. How do we achieve it?

+

The TL;DR is: when you come to merge a pull request, you probably want to +“squash and merge”:

+

squash and merge.

+

(This applies whether you are merging your own PR, or that of another +contributor.)

+

“Squash and merge”1 takes all of the changes in the +PR, and bundles them into a single commit. GitHub gives you the opportunity to +edit the commit message before you confirm, and normally you should do so, +because the default will be useless (again: * woops typo is not a useful +thing to keep in the historical record).

+

The main problem with this approach comes when you have a series of pull +requests which build on top of one another: as soon as you squash-merge the +first PR, you'll end up with a stack of conflicts to resolve in all of the +others. In general, it's best to avoid this situation in the first place by +trying not to have multiple related PRs in flight at the same time. Still, +sometimes that's not possible and doing a regular merge is the lesser evil.

+

Another occasion in which a regular merge makes more sense is a PR where you've +deliberately created a series of commits each of which makes sense in its own +right. For example: a PR which gradually propagates a refactoring operation +through the codebase, or a +PR which is the culmination of several other +PRs. In this case the ability +to figure out when a particular change/bug was introduced could be very useful.

+

Ultimately: this is not a hard-and-fast-rule. If in doubt, ask yourself “do +each of the commits I am about to merge make sense in their own right”, but +remember that we're just doing our best to balance “keeping the commit history +clean” with other factors.

+

Git branching model

+

A lot +of +words have been +written in the past about git branching models (no really, a +lot). I tend to +think the whole thing is overblown. Fundamentally, it's not that +complicated. Here's how we do it.

+

Let's start with a picture:

+

branching model

+

It looks complicated, but it's really not. There's one basic rule: anyone is +free to merge from any more-stable branch to any less-stable branch at +any time2. (The principle behind this is that if a +change is good enough for the more-stable branch, then it's also good enough go +put in a less-stable branch.)

+

Meanwhile, merging (or squashing, as per the above) from a less-stable to a +more-stable branch is a deliberate action in which you want to publish a change +or a set of changes to (some subset of) the world: for example, this happens +when a PR is landed, or as part of our release process.

+

So, what counts as a more- or less-stable branch? A little reflection will show +that our active branches are ordered thus, from more-stable to less-stable:

+
    +
  • master (tracks our last release).
  • +
  • release-vX.Y (the branch where we prepare the next release)3.
  • +
  • PR branches which are targeting the release.
  • +
  • develop (our "mainline" branch containing our bleeding-edge).
  • +
  • regular PR branches.
  • +
+

The corollary is: if you have a bugfix that needs to land in both +release-vX.Y and develop, then you should base your PR on +release-vX.Y, get it merged there, and then merge from release-vX.Y to +develop. (If a fix lands in develop and we later need it in a +release-branch, we can of course cherry-pick it, but landing it in the release +branch first helps reduce the chance of annoying conflicts.)

+
+

[1]: “Squash and merge” is GitHub's term for this +operation. Given that there is no merge involved, I'm not convinced it's the +most intuitive name. ^

+

[2]: Well, anyone with commit access.^

+

[3]: Very, very occasionally (I think this has happened once in +the history of Synapse), we've had two releases in flight at once. Obviously, +release-v1.2 is more-stable than release-v1.3. ^

+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + \ No newline at end of file diff --git a/v1.75/development/img/git/branches.jpg b/v1.75/development/img/git/branches.jpg new file mode 100644 index 0000000000..715ecc8cd0 Binary files /dev/null and b/v1.75/development/img/git/branches.jpg differ diff --git a/v1.75/development/img/git/clean.png b/v1.75/development/img/git/clean.png new file mode 100644 index 0000000000..3accd7ccef Binary files /dev/null and b/v1.75/development/img/git/clean.png differ diff --git a/v1.75/development/img/git/squash.png b/v1.75/development/img/git/squash.png new file mode 100644 index 0000000000..234caca3e4 Binary files /dev/null and b/v1.75/development/img/git/squash.png differ diff --git a/v1.75/development/internal_documentation/index.html b/v1.75/development/internal_documentation/index.html new file mode 100644 index 0000000000..14b78ce1ce --- /dev/null +++ b/v1.75/development/internal_documentation/index.html @@ -0,0 +1,198 @@ + + + + + + Internal Documentation - Synapse + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + +
+
+ +
+ +
+ +

Internal Documentation

+

This section covers implementation documentation for various parts of Synapse.

+

If a developer is planning to make a change to a feature of Synapse, it can be useful for +general documentation of how that feature is implemented to be available. This saves the +developer time in place of needing to understand how the feature works by reading the +code.

+

Documentation that would be more useful for the perspective of a system administrator, +rather than a developer who's intending to change to code, should instead be placed +under the Usage section of the documentation.

+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + \ No newline at end of file diff --git a/v1.75/development/releases.html b/v1.75/development/releases.html new file mode 100644 index 0000000000..8e73776887 --- /dev/null +++ b/v1.75/development/releases.html @@ -0,0 +1,219 @@ + + + + + + Release Cycle - Synapse + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + +
+
+ +
+ +
+ +

Synapse Release Cycle

+

Releases of Synapse follow a two week release cycle with new releases usually +occurring on Tuesdays:

+
    +
  • Day 0: Synapse N - 1 is released.
  • +
  • Day 7: Synapse N release candidate 1 is released.
  • +
  • Days 7 - 13: Synapse N release candidates 2+ are released, if bugs are found.
  • +
  • Day 14: Synapse N is released.
  • +
+

Note that this schedule might be modified depending on the availability of the +Synapse team, e.g. releases may be skipped to avoid holidays.

+

Release announcements can be found in the +release category of the Matrix blog.

+

Bugfix releases

+

If a bug is found after release that is deemed severe enough (by a combination +of the impacted users and the impact on those users) then a bugfix release may +be issued. This may be at any point in the release cycle.

+

Security releases

+

Security will sometimes be backported to the previous version and released +immediately before the next release candidate. An example of this might be:

+
    +
  • Day 0: Synapse N - 1 is released.
  • +
  • Day 7: Synapse (N - 1).1 is released as Synapse N - 1 + the security fix.
  • +
  • Day 7: Synapse N release candidate 1 is released (including the security fix).
  • +
+

Depending on the impact and complexity of security fixes, multiple fixes might +be held to be released together.

+

In some cases, a pre-disclosure of a security release will be issued as a notice +to Synapse operators that there is an upcoming security release. These can be +found in the security category of the Matrix blog.

+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + \ No newline at end of file diff --git a/v1.75/development/reviews.html b/v1.75/development/reviews.html new file mode 100644 index 0000000000..661d0cd7d3 --- /dev/null +++ b/v1.75/development/reviews.html @@ -0,0 +1,222 @@ + + + + + + Reviewing Code - Synapse + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + +
+
+ +
+ +
+ +

Some notes on how we do reviews

+

The Synapse team works off a shared review queue -- any new pull requests for +Synapse (or related projects) has a review requested from the entire team. Team +members should process this queue using the following rules:

+
    +
  • Any high urgency pull requests (e.g. fixes for broken continuous integration +or fixes for release blockers);
  • +
  • Follow-up reviews for pull requests which have previously received reviews;
  • +
  • Any remaining pull requests.
  • +
+

For the latter two categories above, older pull requests should be prioritised.

+

It is explicit that there is no priority given to pull requests from the team +(vs from the community). If a pull request requires a quick turn around, please +explicitly communicate this via #synapse-dev:matrix.org +or as a comment on the pull request.

+

Once an initial review has been completed and the author has made additional changes, +follow-up reviews should go back to the same reviewer. This helps build a shared +context and conversation between author and reviewer.

+

As a team we aim to keep the number of inflight pull requests to a minimum to ensure +that ongoing work is finished before starting new work.

+

Performing a review

+

To communicate to the rest of the team the status of each pull request, team +members should do the following:

+
    +
  • Assign themselves to the pull request (they should be left assigned to the +pull request until it is merged, closed, or are no longer the reviewer);
  • +
  • Review the pull request by leaving comments, questions, and suggestions;
  • +
  • Mark the pull request appropriately (as needing changes or accepted).
  • +
+

If you are unsure about a particular part of the pull request (or are not confident +in your understanding of part of the code) then ask questions or request review +from the team again. When requesting review from the team be sure to leave a comment +with the rationale on why you're putting it back in the queue.

+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + \ No newline at end of file diff --git a/v1.75/development/room-dag-concepts.html b/v1.75/development/room-dag-concepts.html new file mode 100644 index 0000000000..687941d5d2 --- /dev/null +++ b/v1.75/development/room-dag-concepts.html @@ -0,0 +1,278 @@ + + + + + + Room DAG concepts - Synapse + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + +
+
+ +
+ +
+ +

Room DAG concepts

+

Edges

+

The word "edge" comes from graph theory lingo. An edge is just a connection +between two events. In Synapse, we connect events by specifying their +prev_events. A subsequent event points back at a previous event.

+
A (oldest) <---- B <---- C (most recent)
+
+

Depth and stream ordering

+

Events are normally sorted by (topological_ordering, stream_ordering) where +topological_ordering is just depth. In other words, we first sort by depth +and then tie-break based on stream_ordering. depth is incremented as new +messages are added to the DAG. Normally, stream_ordering is an auto +incrementing integer, but backfilled events start with stream_ordering=-1 and decrement.

+
+
    +
  • /sync returns things in the order they arrive at the server (stream_ordering).
  • +
  • /messages (and /backfill in the federation API) return them in the order determined by the event graph (topological_ordering, stream_ordering).
  • +
+

The general idea is that, if you're following a room in real-time (i.e. +/sync), you probably want to see the messages as they arrive at your server, +rather than skipping any that arrived late; whereas if you're looking at a +historical section of timeline (i.e. /messages), you want to see the best +representation of the state of the room as others were seeing it at the time.

+

Outliers

+

We mark an event as an outlier when we haven't figured out the state for the +room at that point in the DAG yet. They are "floating" events that we haven't +yet correlated to the DAG.

+

Outliers typically arise when we fetch the auth chain or state for a given +event. When that happens, we just grab the events in the state/auth chain, +without calculating the state at those events, or backfilling their +prev_events. Since we don't have the state at any events fetched in that +way, we mark them as outliers.

+

So, typically, we won't have the prev_events of an outlier in the database, +(though it's entirely possible that we might have them for some other +reason). Other things that make outliers different from regular events:

+
    +
  • +

    We don't have state for them, so there should be no entry in +event_to_state_groups for an outlier. (In practice this isn't always +the case, though I'm not sure why: see https://github.com/matrix-org/synapse/issues/12201).

    +
  • +
  • +

    We don't record entries for them in the event_edges, +event_forward_extremeties or event_backward_extremities tables.

    +
  • +
+

Since outliers are not tied into the DAG, they do not normally form part of the +timeline sent down to clients via /sync or /messages; however there is an +exception:

+

Out-of-band membership events

+

A special case of outlier events are some membership events for federated rooms +that we aren't full members of. For example:

+
    +
  • invites received over federation, before we join the room
  • +
  • rejections for said invites
  • +
  • knock events for rooms that we would like to join but have not yet joined.
  • +
+

In all the above cases, we don't have the state for the room, which is why they +are treated as outliers. They are a bit special though, in that they are +proactively sent to clients via /sync.

+

Forward extremity

+

Most-recent-in-time events in the DAG which are not referenced by any other +events' prev_events yet. (In this definition, outliers, rejected events, and +soft-failed events don't count.)

+

The forward extremities of a room (or at least, a subset of them, if there are +more than ten) are used as the prev_events when the next event is sent.

+

The "current state" of a room (ie: the state which would be used if we +generated a new event) is, therefore, the resolution of the room states +at each of the forward extremities.

+

Backward extremity

+

The current marker of where we have backfilled up to and will generally be the +prev_events of the oldest-in-time events we have in the DAG. This gives a starting point when +backfilling history.

+

Note that, unlike forward extremities, we typically don't have any backward +extremity events themselves in the database - or, if we do, they will be "outliers" (see +above). Either way, we don't expect to have the room state at a backward extremity.

+

When we persist a non-outlier event, if it was previously a backward extremity, +we clear it as a backward extremity and set all of its prev_events as the new +backward extremities if they aren't already persisted as non-outliers. This +therefore keeps the backward extremities up-to-date.

+

State groups

+

For every non-outlier event we need to know the state at that event. Instead of +storing the full state for each event in the DB (i.e. a event_id -> state +mapping), which is very space inefficient when state doesn't change, we +instead assign each different set of state a "state group" and then have +mappings of event_id -> state_group and state_group -> state.

+

Stage group edges

+

TODO: state_group_edges is a further optimization... +notes from @Azrenbeth, https://pastebin.com/seUGVGeT

+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + \ No newline at end of file diff --git a/v1.75/development/saml.html b/v1.75/development/saml.html new file mode 100644 index 0000000000..e21d487d0a --- /dev/null +++ b/v1.75/development/saml.html @@ -0,0 +1,227 @@ + + + + + + SAML - Synapse + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + +
+
+ +
+ +
+ +

How to test SAML as a developer without a server

+

https://fujifish.github.io/samling/samling.html (https://github.com/fujifish/samling) is a great resource for being able to tinker with the +SAML options within Synapse without needing to deploy and configure a complicated software stack.

+

To make Synapse (and therefore Element) use it:

+
    +
  1. Use the samling.html URL above or deploy your own and visit the IdP Metadata tab.
  2. +
  3. Copy the XML to your clipboard.
  4. +
  5. On your Synapse server, create a new file samling.xml next to your homeserver.yaml with +the XML from step 2 as the contents.
  6. +
  7. Edit your homeserver.yaml to include: +
    saml2_config:
    +  sp_config:
    +    allow_unknown_attributes: true  # Works around a bug with AVA Hashes: https://github.com/IdentityPython/pysaml2/issues/388
    +    metadata:
    +      local: ["samling.xml"]
    +
    +
  8. +
  9. Ensure that your homeserver.yaml has a setting for public_baseurl: +
    public_baseurl: http://localhost:8080/
    +
    +
  10. +
  11. Run apt-get install xmlsec1 and pip install --upgrade --force 'pysaml2>=4.5.0' to ensure +the dependencies are installed and ready to go.
  12. +
  13. Restart Synapse.
  14. +
+

Then in Element:

+
    +
  1. Visit the login page and point Element towards your homeserver using the public_baseurl above.
  2. +
  3. Click the Single Sign-On button.
  4. +
  5. On the samling page, enter a Name Identifier and add a SAML Attribute for uid=your_localpart. +The response must also be signed.
  6. +
  7. Click "Next".
  8. +
  9. Click "Post Response" (change nothing).
  10. +
  11. You should be logged in.
  12. +
+

If you try and repeat this process, you may be automatically logged in using the information you +gave previously. To fix this, open your developer console (F12 or Ctrl+Shift+I) while on the +samling page and clear the site data. In Chrome, this will be a button on the Application tab.

+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + \ No newline at end of file diff --git a/v1.75/development/synapse_architecture/cancellation.html b/v1.75/development/synapse_architecture/cancellation.html new file mode 100644 index 0000000000..43ad7d2b41 --- /dev/null +++ b/v1.75/development/synapse_architecture/cancellation.html @@ -0,0 +1,536 @@ + + + + + + Cancellation - Synapse + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + +
+
+ +
+ +
+ +

Cancellation

+

Sometimes, requests take a long time to service and clients disconnect +before Synapse produces a response. To avoid wasting resources, Synapse +can cancel request processing for select endpoints marked with the +@cancellable decorator.

+

Synapse makes use of Twisted's Deferred.cancel() feature to make +cancellation work. The @cancellable decorator does nothing by itself +and merely acts as a flag, signalling to developers and other code alike +that a method can be cancelled.

+

Enabling cancellation for an endpoint

+
    +
  1. Check that the endpoint method, and any async functions in its call +tree handle cancellation correctly. See +Handling cancellation correctly +for a list of things to look out for.
  2. +
  3. Add the @cancellable decorator to the on_GET/POST/PUT/DELETE +method. It's not recommended to make non-GET methods cancellable, +since cancellation midway through some database updates is less +likely to be handled correctly.
  4. +
+

Mechanics

+

There are two stages to cancellation: downward propagation of a +cancel() call, followed by upwards propagation of a CancelledError +out of a blocked await. +Both Twisted and asyncio have a cancellation mechanism.

+ + + +
MethodExceptionException inherits from
TwistedDeferred.cancel()twisted.internet.defer.CancelledErrorException (!)
asyncioTask.cancel()asyncio.CancelledErrorBaseException
+

Deferred.cancel()

+

When Synapse starts handling a request, it runs the async method +responsible for handling it using defer.ensureDeferred, which returns +a Deferred. For example:

+
def do_something() -> Deferred[None]:
+    ...
+
+@cancellable
+async def on_GET() -> Tuple[int, JsonDict]:
+    d = make_deferred_yieldable(do_something())
+    await d
+    return 200, {}
+
+request = defer.ensureDeferred(on_GET())
+
+

When a client disconnects early, Synapse checks for the presence of the +@cancellable decorator on on_GET. Since on_GET is cancellable, +Deferred.cancel() is called on the Deferred from +defer.ensureDeferred, ie. request. Twisted knows which Deferred +request is waiting on and passes the cancel() call on to d.

+

The Deferred being waited on, d, may have its own handling for +cancel() and pass the call on to other Deferreds.

+

Eventually, a Deferred handles the cancel() call by resolving itself +with a CancelledError.

+

CancelledError

+

The CancelledError gets raised out of the await and bubbles up, as +per normal Python exception handling.

+

Handling cancellation correctly

+

In general, when writing code that might be subject to cancellation, two +things must be considered:

+
    +
  • The effect of CancelledErrors raised out of awaits.
  • +
  • The effect of Deferreds being cancel()ed.
  • +
+

Examples of code that handles cancellation incorrectly include:

+
    +
  • try-except blocks which swallow CancelledErrors.
  • +
  • Code that shares the same Deferred, which may be cancelled, between +multiple requests.
  • +
  • Code that starts some processing that's exempt from cancellation, but +uses a logging context from cancellable code. The logging context +will be finished upon cancellation, while the uncancelled processing +is still using it.
  • +
+

Some common patterns are listed below in more detail.

+

async function calls

+

Most functions in Synapse are relatively straightforward from a +cancellation standpoint: they don't do anything with Deferreds and +purely call and await other async functions.

+

An async function handles cancellation correctly if its own code +handles cancellation correctly and all the async function it calls +handle cancellation correctly. For example:

+
async def do_two_things() -> None:
+    check_something()
+    await do_something()
+    await do_something_else()
+
+

do_two_things handles cancellation correctly if do_something and +do_something_else handle cancellation correctly.

+

That is, when checking whether a function handles cancellation +correctly, its implementation and all its async function calls need to +be checked, recursively.

+

As check_something is not async, it does not need to be checked.

+

CancelledErrors

+

Because Twisted's CancelledErrors are Exceptions, it's easy to +accidentally catch and suppress them. Care must be taken to ensure that +CancelledErrors are allowed to propagate upwards.

+ + + + + + + + + +
+

Bad:

+
try:
+    await do_something()
+except Exception:
+    # `CancelledError` gets swallowed here.
+    logger.info(...)
+
+
+

Good:

+
try:
+    await do_something()
+except CancelledError:
+    raise
+except Exception:
+    logger.info(...)
+
+
+

OK:

+
try:
+    check_something()
+    # A `CancelledError` won't ever be raised here.
+except Exception:
+    logger.info(...)
+
+
+

Good:

+
try:
+    await do_something()
+except ValueError:
+    logger.info(...)
+
+
+

defer.gatherResults

+

defer.gatherResults produces a Deferred which:

+
    +
  • broadcasts cancel() calls to every Deferred being waited on.
  • +
  • wraps the first exception it sees in a FirstError.
  • +
+

Together, this means that CancelledErrors will be wrapped in +a FirstError unless unwrapped. Such FirstErrors are liable to be +swallowed, so they must be unwrapped.

+ + + + + +
+

Bad:

+
async def do_something() -> None:
+    await make_deferred_yieldable(
+        defer.gatherResults([...], consumeErrors=True)
+    )
+
+try:
+    await do_something()
+except CancelledError:
+    raise
+except Exception:
+    # `FirstError(CancelledError)` gets swallowed here.
+    logger.info(...)
+
+
+

Good:

+
async def do_something() -> None:
+    await make_deferred_yieldable(
+        defer.gatherResults([...], consumeErrors=True)
+    ).addErrback(unwrapFirstError)
+
+try:
+    await do_something()
+except CancelledError:
+    raise
+except Exception:
+    logger.info(...)
+
+
+

Creation of Deferreds

+

If a function creates a Deferred, the effect of cancelling it must be considered. Deferreds that get shared are likely to have unintended behaviour when cancelled.

+ + + + + + + + +
+

Bad:

+
cache: Dict[str, Deferred[None]] = {}
+
+def wait_for_room(room_id: str) -> Deferred[None]:
+    deferred = cache.get(room_id)
+    if deferred is None:
+        deferred = Deferred()
+        cache[room_id] = deferred
+    # `deferred` can have multiple waiters.
+    # All of them will observe a `CancelledError`
+    # if any one of them is cancelled.
+    return make_deferred_yieldable(deferred)
+
+# Request 1
+await wait_for_room("!aAAaaAaaaAAAaAaAA:matrix.org")
+# Request 2
+await wait_for_room("!aAAaaAaaaAAAaAaAA:matrix.org")
+
+
+

Good:

+
cache: Dict[str, Deferred[None]] = {}
+
+def wait_for_room(room_id: str) -> Deferred[None]:
+    deferred = cache.get(room_id)
+    if deferred is None:
+        deferred = Deferred()
+        cache[room_id] = deferred
+    # `deferred` will never be cancelled now.
+    # A `CancelledError` will still come out of
+    # the `await`.
+    # `delay_cancellation` may also be used.
+    return make_deferred_yieldable(stop_cancellation(deferred))
+
+# Request 1
+await wait_for_room("!aAAaaAaaaAAAaAaAA:matrix.org")
+# Request 2
+await wait_for_room("!aAAaaAaaaAAAaAaAA:matrix.org")
+
+
+ +

Good:

+
cache: Dict[str, List[Deferred[None]]] = {}
+
+def wait_for_room(room_id: str) -> Deferred[None]:
+    if room_id not in cache:
+        cache[room_id] = []
+    # Each request gets its own `Deferred` to wait on.
+    deferred = Deferred()
+    cache[room_id]].append(deferred)
+    return make_deferred_yieldable(deferred)
+
+# Request 1
+await wait_for_room("!aAAaaAaaaAAAaAaAA:matrix.org")
+# Request 2
+await wait_for_room("!aAAaaAaaaAAAaAaAA:matrix.org")
+
+
+

Uncancelled processing

+

Some async functions may kick off some async processing which is +intentionally protected from cancellation, by stop_cancellation or +other means. If the async processing inherits the logcontext of the +request which initiated it, care must be taken to ensure that the +logcontext is not finished before the async processing completes.

+ + + + + + + + + +
+

Bad:

+
cache: Optional[ObservableDeferred[None]] = None
+
+async def do_something_else(
+    to_resolve: Deferred[None]
+) -> None:
+    await ...
+    logger.info("done!")
+    to_resolve.callback(None)
+
+async def do_something() -> None:
+    if not cache:
+        to_resolve = Deferred()
+        cache = ObservableDeferred(to_resolve)
+        # `do_something_else` will never be cancelled and
+        # can outlive the `request-1` logging context.
+        run_in_background(do_something_else, to_resolve)
+
+    await make_deferred_yieldable(cache.observe())
+
+with LoggingContext("request-1"):
+    await do_something()
+
+
+

Good:

+
cache: Optional[ObservableDeferred[None]] = None
+
+async def do_something_else(
+    to_resolve: Deferred[None]
+) -> None:
+    await ...
+    logger.info("done!")
+    to_resolve.callback(None)
+
+async def do_something() -> None:
+    if not cache:
+        to_resolve = Deferred()
+        cache = ObservableDeferred(to_resolve)
+        run_in_background(do_something_else, to_resolve)
+        # We'll wait until `do_something_else` is
+        # done before raising a `CancelledError`.
+        await make_deferred_yieldable(
+            delay_cancellation(cache.observe())
+        )
+    else:
+        await make_deferred_yieldable(cache.observe())
+
+with LoggingContext("request-1"):
+    await do_something()
+
+
+

OK:

+
cache: Optional[ObservableDeferred[None]] = None
+
+async def do_something_else(
+    to_resolve: Deferred[None]
+) -> None:
+    await ...
+    logger.info("done!")
+    to_resolve.callback(None)
+
+async def do_something() -> None:
+    if not cache:
+        to_resolve = Deferred()
+        cache = ObservableDeferred(to_resolve)
+        # `do_something_else` will get its own independent
+        # logging context. `request-1` will not count any
+        # metrics from `do_something_else`.
+        run_as_background_process(
+            "do_something_else",
+            do_something_else,
+            to_resolve,
+        )
+
+    await make_deferred_yieldable(cache.observe())
+
+with LoggingContext("request-1"):
+    await do_something()
+
+
+
+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + \ No newline at end of file -- cgit 1.5.1