Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into dmr/status-as-int-…
Browse files Browse the repository at this point in the history
…in-logs
  • Loading branch information
David Robertson committed Jan 26, 2022
2 parents 543fa53 + c581556 commit f46a8a1
Show file tree
Hide file tree
Showing 32 changed files with 696 additions and 69 deletions.
28 changes: 18 additions & 10 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -323,17 +323,22 @@ jobs:
if: ${{ !failure() && !cancelled() }}
needs: linting-done
runs-on: ubuntu-latest
container:
# https://github.com/matrix-org/complement/blob/master/dockerfiles/ComplementCIBuildkite.Dockerfile
image: matrixdotorg/complement:latest
env:
CI: true
ports:
- 8448:8448
volumes:
- /var/run/docker.sock:/var/run/docker.sock

steps:
# The path is set via a file given by $GITHUB_PATH. We need both Go 1.17 and GOPATH on the path to run Complement.
# See https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#adding-a-system-path
- name: "Set Go Version"
run: |
# Add Go 1.17 to the PATH: see https://github.com/actions/virtual-environments/blob/main/images/linux/Ubuntu2004-Readme.md#environment-variables-2
echo "$GOROOT_1_17_X64/bin" >> $GITHUB_PATH
# Add the Go path to the PATH: We need this so we can call gotestfmt
echo "~/go/bin" >> $GITHUB_PATH
- name: "Install Complement Dependencies"
run: |
sudo apt-get update && sudo apt-get install -y libolm3 libolm-dev
go get -v github.com/haveyoudebuggedit/gotestfmt/v2/cmd/gotestfmt@latest
- name: Run actions/checkout@v2 for synapse
uses: actions/checkout@v2
with:
Expand Down Expand Up @@ -376,8 +381,11 @@ jobs:
working-directory: complement/dockerfiles

# Run Complement
- run: set -o pipefail && go test -v -json -tags synapse_blacklist,msc2403 ./tests/... 2>&1 | gotestfmt
- run: |
set -o pipefail
go test -v -json -tags synapse_blacklist,msc2403 ./tests/... 2>&1 | gotestfmt
shell: bash
name: Run Complement Tests
env:
COMPLEMENT_BASE_IMAGE: complement-synapse:latest
working-directory: complement
Expand Down
1 change: 1 addition & 0 deletions changelog.d/11658.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add an admin API to get a list of rooms that federate with a given remote homeserver.
1 change: 1 addition & 0 deletions changelog.d/11743.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a config flag to inhibit M_USER_IN_USE during registration.
1 change: 1 addition & 0 deletions changelog.d/11799.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Preparation for reducing Postgres serialization errors: allow setting transaction isolation level. Contributed by Nick @ Beeper.
1 change: 1 addition & 0 deletions changelog.d/11811.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Run Complement on the Github Actions VM and not inside a Docker container.
1 change: 1 addition & 0 deletions changelog.d/11813.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Log module names at startup.
1 change: 1 addition & 0 deletions changelog.d/11816.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Drop support for Python 3.6, which is EOL.
2 changes: 1 addition & 1 deletion changelog.d/11817.misc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Compatibility with updated type hints for jsonschema 4.4.0.
Correct a type annotation in the event validation logic.
1 change: 1 addition & 0 deletions changelog.d/11821.doc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add missing steps to the contribution submission process in the documentation. Contributed by @sequentialread.
1 change: 1 addition & 0 deletions changelog.d/11823.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Minor updates and documentation for database schema delta files.
1 change: 1 addition & 0 deletions changelog.d/11830.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Correct a type annotation in the event validation logic.
1 change: 1 addition & 0 deletions changelog.d/11834.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Workaround a type annotation problem in `prometheus_client` 0.13.0.
3 changes: 2 additions & 1 deletion docs/development/contributing_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ setup a *virtualenv*, as follows:
cd path/where/you/have/cloned/the/repository
python3 -m venv ./env
source ./env/bin/activate
pip install wheel
pip install -e ".[all,dev]"
pip install tox
```
Expand Down Expand Up @@ -116,7 +117,7 @@ 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.

They're pretty fast, don't hesitate!
The linter takes no time at all to run as soon as you've [downloaded the dependencies into your python virtual environment](#4-install-the-dependencies).

```sh
source ./env/bin/activate
Expand Down
54 changes: 54 additions & 0 deletions docs/development/database_schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,60 @@ Ensure postgres is installed, then run:
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:

```python
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
Expand Down
10 changes: 10 additions & 0 deletions docs/sample_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,16 @@ account_threepid_delegates:
#
#auto_join_rooms_for_guests: false

# Whether to inhibit errors raised when registering a new account if the user ID
# already exists. If turned on, that requests to /register/available will always
# show a user ID as available, and Synapse won't raise an error when starting
# a registration with a user ID that already exists. However, Synapse will still
# raise an error if the registration completes and the username conflicts.
#
# Defaults to false.
#
#inhibit_user_in_use_error: true


## Metrics ###

Expand Down
60 changes: 60 additions & 0 deletions docs/usage/administration/admin_api/federation.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,66 @@ The following parameters should be set in the URL:
The response fields are the same like in the `destinations` array in
[List of destinations](#list-of-destinations) response.

## Destination rooms

This API gets the rooms that federate with a specific remote server.

The API is:

```
GET /_synapse/admin/v1/federation/destinations/<destination>/rooms
```

A response body like the following is returned:

```json
{
"rooms":[
{
"room_id": "!OGEhHVWSdvArJzumhm:matrix.org",
"stream_ordering": 8326
},
{
"room_id": "!xYvNcQPhnkrdUmYczI:matrix.org",
"stream_ordering": 93534
}
],
"total": 2
}
```

To paginate, check for `next_token` and if present, call the endpoint again
with `from` set to the value of `next_token`. This will return a new page.

If the endpoint does not return a `next_token` then there are no more destinations
to paginate through.

**Parameters**

The following parameters should be set in the URL:

- `destination` - Name of the remote server.

The following query parameters are available:

- `from` - Offset in the returned list. Defaults to `0`.
- `limit` - Maximum amount of destinations to return. Defaults to `100`.
- `dir` - Direction of room order by `room_id`. Either `f` for forwards or `b` for
backwards. Defaults to `f`.

**Response**

The following fields are returned in the JSON response body:

- `rooms` - An array of objects, each containing information about a room.
Room objects contain the following fields:
- `room_id` - string - The ID of the room.
- `stream_ordering` - integer - The stream ordering of the most recent
successfully-sent [PDU](understanding_synapse_through_grafana_graphs.md#federation)
to this destination in this room.
- `next_token`: string representing a positive integer - Indication for pagination. See above.
- `total` - integer - Total number of destinations.

## Reset connection timeout

Synapse makes federation requests to other homeservers. If a federation request fails,
Expand Down
17 changes: 10 additions & 7 deletions synapse/app/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,8 @@ def run_sighup(*args: Any, **kwargs: Any) -> None:
# before we start the listeners.
module_api = hs.get_module_api()
for module, config in hs.config.modules.loaded_modules:
module(config=config, api=module_api)
m = module(config=config, api=module_api)
logger.info("Loaded module %s", m)

load_legacy_spam_checkers(hs)
load_legacy_third_party_event_rules(hs)
Expand Down Expand Up @@ -467,12 +468,14 @@ def run_sighup(*args: Any, **kwargs: Any) -> None:
# everything currently allocated are things that will be used for the
# rest of time. Doing so means less work each GC (hopefully).
#
gc.collect()
gc.freeze()

# Speed up shutdowns by freezing all allocated objects. This moves everything
# into the permanent generation and excludes them from the final GC.
atexit.register(gc.freeze)
# PyPy does not (yet?) implement gc.freeze()
if hasattr(gc, "freeze"):
gc.collect()
gc.freeze()

# Speed up shutdowns by freezing all allocated objects. This moves everything
# into the permanent generation and excludes them from the final GC.
atexit.register(gc.freeze)


def setup_sentry(hs: "HomeServer") -> None:
Expand Down
12 changes: 12 additions & 0 deletions synapse/config/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ def read_config(self, config, **kwargs):
# The success template used during fallback auth.
self.fallback_success_template = self.read_template("auth_success.html")

self.inhibit_user_in_use_error = config.get("inhibit_user_in_use_error", False)

def generate_config_section(self, generate_secrets=False, **kwargs):
if generate_secrets:
registration_shared_secret = 'registration_shared_secret: "%s"' % (
Expand Down Expand Up @@ -446,6 +448,16 @@ def generate_config_section(self, generate_secrets=False, **kwargs):
# Defaults to true.
#
#auto_join_rooms_for_guests: false
# Whether to inhibit errors raised when registering a new account if the user ID
# already exists. If turned on, that requests to /register/available will always
# show a user ID as available, and Synapse won't raise an error when starting
# a registration with a user ID that already exists. However, Synapse will still
# raise an error if the registration completes and the username conflicts.
#
# Defaults to false.
#
#inhibit_user_in_use_error: true
"""
% locals()
)
Expand Down
6 changes: 2 additions & 4 deletions synapse/events/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import collections.abc
from typing import Iterable, Union
from typing import Iterable, Type, Union

import jsonschema

Expand Down Expand Up @@ -246,9 +246,7 @@ def _ensure_state_event(self, event: Union[EventBase, EventBuilder]) -> None:

# This could return something newer than Draft 7, but that's the current "latest"
# validator.
#
# See https://github.com/python/typeshed/issues/7028 for the ignored return type.
def _create_power_level_validator() -> jsonschema.Draft7Validator: # type: ignore[valid-type]
def _create_power_level_validator() -> Type[jsonschema.Draft7Validator]:
validator = jsonschema.validators.validator_for(POWER_LEVELS_SCHEMA)

# by default jsonschema does not consider a frozendict to be an object so
Expand Down
26 changes: 14 additions & 12 deletions synapse/handlers/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ async def check_username(
localpart: str,
guest_access_token: Optional[str] = None,
assigned_user_id: Optional[str] = None,
inhibit_user_in_use_error: bool = False,
) -> None:
if types.contains_invalid_mxid_characters(localpart):
raise SynapseError(
Expand Down Expand Up @@ -171,21 +172,22 @@ async def check_username(

users = await self.store.get_users_by_id_case_insensitive(user_id)
if users:
if not guest_access_token:
if not inhibit_user_in_use_error and not guest_access_token:
raise SynapseError(
400, "User ID already taken.", errcode=Codes.USER_IN_USE
)
user_data = await self.auth.get_user_by_access_token(guest_access_token)
if (
not user_data.is_guest
or UserID.from_string(user_data.user_id).localpart != localpart
):
raise AuthError(
403,
"Cannot register taken user ID without valid guest "
"credentials for that user.",
errcode=Codes.FORBIDDEN,
)
if guest_access_token:
user_data = await self.auth.get_user_by_access_token(guest_access_token)
if (
not user_data.is_guest
or UserID.from_string(user_data.user_id).localpart != localpart
):
raise AuthError(
403,
"Cannot register taken user ID without valid guest "
"credentials for that user.",
errcode=Codes.FORBIDDEN,
)

if guest_access_token is None:
try:
Expand Down
3 changes: 2 additions & 1 deletion synapse/python_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@
"msgpack>=0.5.2",
"phonenumbers>=8.2.0",
# we use GaugeHistogramMetric, which was added in prom-client 0.4.0.
"prometheus_client>=0.4.0",
# 0.13.0 has an incorrect type annotation, see #11832.
"prometheus_client>=0.4.0,<0.13.0",
# we use `order`, which arrived in attrs 19.2.0.
# Note: 21.1.0 broke `/sync`, see #9936
"attrs>=19.2.0,!=21.1.0",
Expand Down
2 changes: 2 additions & 0 deletions synapse/rest/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
EventReportsRestServlet,
)
from synapse.rest.admin.federation import (
DestinationMembershipRestServlet,
DestinationResetConnectionRestServlet,
DestinationRestServlet,
ListDestinationsRestServlet,
Expand Down Expand Up @@ -268,6 +269,7 @@ def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
ListRegistrationTokensRestServlet(hs).register(http_server)
NewRegistrationTokenRestServlet(hs).register(http_server)
RegistrationTokenRestServlet(hs).register(http_server)
DestinationMembershipRestServlet(hs).register(http_server)
DestinationResetConnectionRestServlet(hs).register(http_server)
DestinationRestServlet(hs).register(http_server)
ListDestinationsRestServlet(hs).register(http_server)
Expand Down
Loading

0 comments on commit f46a8a1

Please sign in to comment.