Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synapse workers #456

Merged
merged 88 commits into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
88 commits
Select commit Hold shift + click to select a range
353bc7c
Add initial support for synapse workers
eMPee584 Apr 10, 2020
a14b9c0
Add to synapse nginx template conditional URL rewrites for workers
eMPee584 Apr 11, 2020
cf452fd
Fix corner-cases found through testing (aka ansible is nuts)
eMPee584 Apr 19, 2020
66a4073
Publish synapse worker ports, need to be accessible to nginx
eMPee584 Apr 19, 2020
e4763c2
nginx config: route traffic to workers on matrix-synapse
eMPee584 Apr 19, 2020
5f63d28
Move synapse worker ports up 10k
eMPee584 Apr 19, 2020
765c046
add missing ; to matrix-synapse.conf.j2
JohannesKleine Apr 19, 2020
46984a4
Nginx conf: more testing less b0rk
eMPee584 Apr 19, 2020
567d031
Merge branch 'synapse-workers' into feature/add-worker-support
maxklenk Aug 27, 2020
59d1fb7
only apply worker redirects if workers are enabled
maxklenk Aug 27, 2020
53ccc78
remove duplicated key
maxklenk Aug 27, 2020
06bc430
refactor to use new workers and routes they serve
maxklenk Aug 28, 2020
a25a429
add redis support
maxklenk Sep 10, 2020
9a3d84b
Merge branch 'master' into feature/add-worker-support
maxklenk Sep 10, 2020
4fdfc0a
add missing ratelimiting options required for load testing
maxklenk Sep 11, 2020
8800253
fix redis config if no password is set
maxklenk Sep 11, 2020
1e68d8b
allow to pass arguments to the postgres process
maxklenk Sep 11, 2020
132daba
fix worker routes
maxklenk Sep 18, 2020
fc2edcb
fix media routing
maxklenk Sep 18, 2020
93a8ea7
Merge remote-tracking branch 'master' into feature/add-worker-support
eMPee584 Oct 11, 2020
40024e9
Prevent workers failing if their config doesn't exist
eMPee584 Sep 29, 2020
e9241f5
Improve synapse-workers systemd service template
eMPee584 Oct 11, 2020
36e9be6
matrix_synapse_workers_{avail,enabled}_list: sort non-generic workers
eMPee584 Oct 11, 2020
d2e61af
Add worker_name to synapse worker config template
eMPee584 Sep 9, 2020
501efee
synapse workers: supply systemd with actual worker PIDs (requires jq)
eMPee584 Oct 22, 2020
a4125d5
synapse workers: polishing, cleansing and installation of jq dependency
eMPee584 Oct 23, 2020
87bd64c
Merge remote-tracking branch 'origin/master' into synapse-workers
eMPee584 Oct 23, 2020
2d1b9f2
synapse workers: reworkings + get endpoints from upstream docs via awk
eMPee584 Oct 28, 2020
e078e29
synapse workers: fix self name in workers-doc-to-yaml.awk script
eMPee584 Oct 28, 2020
cce90b1
synapse workers: fix undefined variable cases when removing workers
eMPee584 Oct 28, 2020
e5072c2
synapse workers/nginx: handle media_repository worker endpoints on fe…
eMPee584 Nov 10, 2020
1e97131
synapse workers: handle auth fallback endpoint on main process only
eMPee584 Nov 10, 2020
b05d298
synapse workers nginx rule: add client_max_body_size on media endpoints
eMPee584 Nov 10, 2020
dd402be
synapse workers: add rudimentary documentation on worker support
eMPee584 Nov 10, 2020
f3d2797
synapse workers: make awk script invocation handle paths with spaces
eMPee584 Nov 10, 2020
5598a89
synapse workers doc: link to relevant synapse issue list search
eMPee584 Nov 10, 2020
4678c5d
Merge remote-tracking branch 'origin/master' into synapse-workers
eMPee584 Nov 11, 2020
b73ac96
Merge remote-tracking branch 'origin/master' into synapse-workers
eMPee584 Dec 1, 2020
e314613
Add files created by workers-doc-to-yaml.awk to .gitignore
eMPee584 Dec 1, 2020
851c25c
matrix-synapse nginx template: fix invalid jinja comment syntax
eMPee584 Dec 1, 2020
d5932ca
synapse role workers setup: execute the endpoint extraction locally
eMPee584 Dec 1, 2020
414b812
synapse role workers setup: make configs clean action remote compatible
eMPee584 Dec 1, 2020
af08f18
synapse workers default config: disable user_dir worker for now
eMPee584 Dec 1, 2020
f201bca
synapse workers: define and expose METRICS port for each worker
eMPee584 Dec 1, 2020
e892ac4
synapse workers: untangle config template and specify bind address
eMPee584 Dec 1, 2020
3156d96
synapse workers-doc-to-yaml.awk: escape slash for non-gnu awk versions
eMPee584 Dec 1, 2020
b6b95fe
synapse workers-doc-to-yaml script: compatibility++ with non-gnu awk
eMPee584 Dec 2, 2020
cd81005
Merge remote-tracking branch 'origin/master' into synapse-workers
eMPee584 Jan 8, 2021
f2c7d79
Drop probably incorrect comment from synapse homeserver.yaml.j2
eMPee584 Jan 23, 2021
c8f051a
Track workers endpoint list in repo instead of regenerating on user side
eMPee584 Jan 23, 2021
183adec
Merge remote-tracking branch 'origin/master' into synapse-workers
eMPee584 Jan 23, 2021
edc21f1
Restrict publishing worker (metrics) ports to localhost
eMPee584 Jan 24, 2021
92ee3d7
Fix matrix-remove-all for when Synapse workers are enabled
spantaleev Jan 24, 2021
cc5cf0d
Load roles/matrix-synapse/vars/workers.yml earlier to not break --tag…
spantaleev Jan 24, 2021
5ca6821
Do not handle /_matrix/federation on client-server port, nor /_matrix…
spantaleev Jan 24, 2021
f66a6b0
Be more specific with the Redis version being used
spantaleev Jan 24, 2021
63301b0
Improvements around Synapse worker/metrics ports exposure
spantaleev Jan 25, 2021
c05d3d0
Disable systemd services while stopping them
spantaleev Jan 25, 2021
4d62a75
Get matrix-corporal to play nicely with a Synapse worker setup
spantaleev Jan 25, 2021
da50fb2
Whitelist /_matrix/key requests for going to generic workers on the f…
spantaleev Jan 25, 2021
7079670
Run Synapse workers in their own containers
spantaleev Jan 25, 2021
01747c8
Prevent Synapse warning about enabling metric listeners with enable_m…
spantaleev Jan 25, 2021
1462409
Fix worker listening addresses
spantaleev Jan 25, 2021
66cdc7b
Clean up worker.yaml generation a bit and make it more flexible
spantaleev Jan 25, 2021
d3ecc6f
Fix bridges failing to upload media when Synapse workers are enabled
spantaleev Jan 25, 2021
70dcdd4
Simplify matrix-remove-all
spantaleev Jan 25, 2021
778b668
Merge branch 'master' into synapse-workers
spantaleev Jan 25, 2021
a535226
Stop/disable unnecessary worker services before deleting them
spantaleev Jan 25, 2021
a9af368
Merge branch 'master' into synapse-workers
spantaleev Jan 27, 2021
d98a1ce
Merge branch 'master' into synapse-workers
spantaleev Jan 27, 2021
a31c960
Merge branch 'master' into synapse-workers
spantaleev Jan 27, 2021
a49dab7
Merge branch 'master' into synapse-workers
spantaleev Jan 27, 2021
39c2d72
Merge branch 'master' into synapse-workers
spantaleev Jan 27, 2021
1cd2a21
Merge branch 'master' into synapse-workers
spantaleev Jan 27, 2021
e7f3f7c
Enable /devices endpoint for generic workers
spantaleev Jan 27, 2021
5cfeae8
Merge branch 'master' into synapse-workers
spantaleev Feb 14, 2021
453a4ec
Relocate tasks related to Synapse workers
spantaleev Feb 15, 2021
43059bb
Fix metrics listeners for Synapse workers
spantaleev Feb 15, 2021
85a05f3
Allow Synapse worker list to be generated dynamically
spantaleev Feb 15, 2021
61e427d
Do not let people enable more than 1 federation_sender worker
spantaleev Feb 15, 2021
daae74b
Merge branch 'master' into synapse-workers
spantaleev Feb 16, 2021
d33483b
Document that Synapse pusher worker instances are shardable
spantaleev Feb 16, 2021
d6c4d41
Define instanceId property on workers
spantaleev Feb 18, 2021
1789620
Merge branch 'master' into synapse-workers
spantaleev Feb 18, 2021
2f732e4
Update Synapse worker endpoints
spantaleev Feb 19, 2021
eaea215
Allow Synapse workers to be used with an external nginx webserver
spantaleev Feb 19, 2021
9dc87bb
Add Synapse worker presets for easier configuration
spantaleev Feb 19, 2021
b754c27
Announce Synapse workers support
spantaleev Feb 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions roles/matrix-nginx-proxy/templates/nginx/conf.d/matrix-synapse.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,114 @@
}
{% endif %}

{% if synchrotron_workers %}
{# c.f. https://github.com/matrix-org/synapse/blame/master/docs/workers.md#L134 #}
location /_matrix/client/r0/sync {
proxy_pass http://synchrotron$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /_matrix/client/r0/events {
proxy_pass http://synchrotron$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /_matrix/client/r0/initialSync {
proxy_pass http://synchrotron$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location ~ ^/_matrix/client/r0/rooms/[^/]+/initialSync$ {
proxy_pass http://synchrotron$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
{% endif %}

{% set client_reader_worker = matrix_synapse_workers_enabled_list|selectattr('worker', 'equalto', 'client_reader')|first %}
{% if client_reader_worker %}
{# c.f. https://github.com/matrix-org/synapse/blame/master/docs/workers.md#L252 #}
location ^/_matrix/client/(versions$|(api/v1|r0|unstable)/(publicRooms$|rooms/.*/joined_me|rooms/.*/context/.|rooms/.*/members$|rooms/.*/messages$|rooms/.*/state$|login$|account/3pid$|keys/query$|keys/changes$|voip/turnServer$|joined_groups$|publicised_groups$|publicised_groups/|pushrules/.*$|groups/.*$|register$|auth/.*/fallback/web$)) {
Copy link

@lovelaced lovelaced Jul 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two of the endpoints here can only handle GET requests, and there are also two more not included here. The following list is GET-only for the client_reader worker:

^/_matrix/client/(api/v1|r0|unstable)/pushrules/.*$
^/_matrix/client/(api/v1|r0|unstable)/groups/.*$
^/_matrix/client/(api/v1|r0|unstable)/user/[^/]*/account_data/
^/_matrix/client/(api/v1|r0|unstable)/user/[^/]*/rooms/[^/]*/account_data/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "location ^/..." here, for a regex match needs to be "location ~ ^..." doesn't it?

{# Use the embedded DNS resolver in Docker containers to discover the service #}
resolver 127.0.0.11 valid=5s;
set $backend "matrix-synapse:{{ client_reader_worker.port }}";
proxy_pass http://$backend$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
{% endif %}

{% set media_repository_worker = matrix_synapse_workers_enabled_list|selectattr('worker', 'equalto', 'media_repository')|first %}
{% if media_repository_worker %}
{# c.f. https://github.com/matrix-org/synapse/blame/master/docs/workers.md#L219 #}
location /_matrix/media/ {
{# Use the embedded DNS resolver in Docker containers to discover the service #}
resolver 127.0.0.11 valid=5s;
set $backend "matrix-synapse:{{ media_repository_worker.port }}";
proxy_pass http://$backend$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
{# c.f. https://github.com/matrix-org/synapse/blame/master/docs/workers.md#L223 #}
location ~ ^/_synapse/admin/v1/(purge_media_cache|room/.*/media.*|user/.*/media.*|media/.*|quarantine_media/.*)$ {
{# Use the embedded DNS resolver in Docker containers to discover the service #}
resolver 127.0.0.11 valid=5s;
set $backend "matrix-synapse:{{ media_repository_worker.port }}";
proxy_pass http://$backend$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
{% endif %}

{% set event_creator_worker = matrix_synapse_workers_enabled_list|selectattr('worker', 'equalto', 'event_creator')|first %}
{% if event_creator_worker %}
{# c.f. https://github.com/matrix-org/synapse/blame/master/docs/workers.md#L323 #}
location ~ ^/_matrix/client/(api/v1|r0|unstable)/(rooms/.*/send|rooms/.*/state/|rooms/.*/(join|invite|leave|ban|unban|kick)$|join/|profile/) {
{# Use the embedded DNS resolver in Docker containers to discover the service #}
resolver 127.0.0.11 valid=5s;
set $backend "matrix-synapse:{{ event_creator_worker.port }}";
proxy_pass http://$backend$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
{% endif %}

{% set frontend_proxy_worker = matrix_synapse_workers_enabled_list|selectattr('worker', 'equalto', 'frontend_proxy')|first %}
{% if frontend_proxy_worker %}
{# c.f. https://github.com/matrix-org/synapse/blame/master/docs/workers.md#L302 #}
location ~ ^/_matrix/client/(api/v1|r0|unstable)/keys/upload {
{# Use the embedded DNS resolver in Docker containers to discover the service #}
resolver 127.0.0.11 valid=5s;
set $backend "matrix-synapse:{{ frontend_proxy_worker.port }}";
proxy_pass http://$backend$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
{% if not matrix_synapse_use_presence %}
location ~ ^/_matrix/client/(api/v1|r0|unstable)/presence/[^/]+/status {
{# Use the embedded DNS resolver in Docker containers to discover the service #}
resolver 127.0.0.11 valid=5s;
set $backend "matrix-synapse:{{ frontend_proxy_worker.port }}";
proxy_pass http://$backend$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
{% endif %}
{% endif %}

{% set user_dir_worker = matrix_synapse_workers_enabled_list|selectattr('worker', 'equalto', 'user_dir')|first %}
{% if user_dir_worker %}
{# c.f. https://github.com/matrix-org/synapse/blame/master/docs/workers.md#L290 #}
location ~ ^/_matrix/client/(api/v1|r0|unstable)/user_directory/search$ {
{# Use the embedded DNS resolver in Docker containers to discover the service #}
resolver 127.0.0.11 valid=5s;
set $backend "matrix-synapse:{{ user_dir_worker.port }}";
proxy_pass http://$backend$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
{% endif %}

{% for configuration_block in matrix_nginx_proxy_proxy_matrix_additional_server_configuration_blocks %}
{{- configuration_block }}
{% endfor %}
Expand Down Expand Up @@ -174,6 +282,19 @@
}
{% endmacro %}

{% set synchrotron_workers = matrix_synapse_workers_enabled_list|selectattr('worker', 'equalto', 'synchrotron')|list %}
{% if synchrotron_workers %}
upstream synchrotron {
# ensures that requests from the same client will always be passed
# to the same server (except when this server is unavailable)
ip_hash;

{% for synchrotron_worker in synchrotron_workers %}
server "matrix-synapse:{{ synchrotron_worker.port }}";
{% endfor %}
}
{% endif %}

server {
listen {{ 8080 if matrix_nginx_proxy_enabled else 80 }};
server_name {{ matrix_nginx_proxy_proxy_matrix_hostname }};
Expand Down Expand Up @@ -255,6 +376,19 @@ server {
ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
{% endif %}

{% set federation_reader_worker = matrix_synapse_workers_enabled_list|selectattr('worker', 'equalto', 'federation_reader')|first %}
{% if federation_reader_worker %}
{# c.f. https://github.com/matrix-org/synapse/blame/master/docs/workers.md#L160 #}
location ~ ^(/_matrix/federation/v1/event/|/_matrix/federation/v1/state/|/_matrix/federation/v1/state_ids/|/_matrix/federation/v1/backfill/|/_matrix/federation/v1/get_missing_events/|/_matrix/federation/v1/publicRooms|/_matrix/federation/v1/query/|/_matrix/federation/v1/make_join/|/_matrix/federation/v1/make_leave/|/_matrix/federation/v1/send_join/|/_matrix/federation/v2/send_join/|/_matrix/federation/v1/send_leave/|/_matrix/federation/v2/send_leave/|/_matrix/federation/v1/invite/|/_matrix/federation/v2/invite/|/_matrix/federation/v1/query_auth/|/_matrix/federation/v1/event_auth/|/_matrix/federation/v1/exchange_third_party_invite/|/_matrix/federation/v1/user/devices/|/_matrix/federation/v1/send/|/_matrix/federation/v1/get_groups_publicised$|/_matrix/key/v2/query|/_matrix/federation/v1/groups/) {
Copy link

@lovelaced lovelaced Jul 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/_matrix/federation/v1/groups/ should only be allowed GET requests; this configuration will cause federated community invites to fail. reference here: https://github.com/matrix-org/synapse/blob/master/docs/workers.md#synapseappfederation_reader

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback.. Is this theoretical or have you experienced it on an actual system?
I think there probably is a different mistake in the config to cause this.. we are also having some minor troubles with our setup.
I've just created an awk script to parse the endpoints directly from the upstream synapse workers documentation.. but it remains kinda nightmarish to work with.. 🤔

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I experienced it myself when trying to set up a fresh homeserver so I just removed the groups endpoint so the main synapse thread is handling it instead for now.

{# Use the embedded DNS resolver in Docker containers to discover the service #}
resolver 127.0.0.11 valid=5s;
set $backend "matrix-synapse:{{ federation_reader_worker.port }}";
proxy_pass http://$backend$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
{% endif %}

location / {
{% if matrix_nginx_proxy_enabled %}
{# Use the embedded DNS resolver in Docker containers to discover the service #}
Expand Down
37 changes: 37 additions & 0 deletions roles/matrix-synapse/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,43 @@ matrix_synapse_metrics_port: 9100
# See https://github.com/matrix-org/synapse/blob/master/docs/manhole.md
matrix_synapse_manhole_enabled: false

# Enable support for Synapse workers
matrix_synapse_workers_enabled: false

# List of workers to spawn
matrix_synapse_workers_enabled_list: []

# Default list of workers to spawn
matrix_synapse_workers_enabled_list:
- { worker: synchrotron, port: 18082 }
- { worker: synchrotron, port: 18083 }
- { worker: synchrotron, port: 18084 }
- { worker: appservice, port: 18085 }
- { worker: client_reader, port: 18086 }
- { worker: event_creator, port: 18087 }
- { worker: federation_reader, port: 18088 }
- { worker: federation_sender, port: 18089 }
- { worker: frontend_proxy, port: 18090 }
- { worker: media_repository, port: 18091 }
- { worker: pusher, port: 18092 }
- { worker: user_dir, port: 18093 }

# The list of available workers (2020-04-14)
matrix_synapse_workers_avail_list:
- appservice
- client_reader
- event_creator
- federation_reader
- federation_sender
- frontend_proxy
- media_repository
- pusher
- synchrotron
- user_dir

# Ports used for communication between main synapse process and workers
matrix_synapse_replication_tcp_port: 9092
matrix_synapse_replication_http_port: 9093

# Send ERROR logs to sentry.io for easier tracking
# To set this up: go to sentry.io, create a python project, and set
Expand Down
2 changes: 2 additions & 0 deletions roles/matrix-synapse/tasks/setup_synapse.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

- import_tasks: "{{ role_path }}/tasks/ext/setup.yml"

- import_tasks: "{{ role_path }}/tasks/workers/setup.yml"

- import_tasks: "{{ role_path }}/tasks/synapse/setup.yml"

- import_tasks: "{{ role_path }}/tasks/goofys/setup.yml"
8 changes: 8 additions & 0 deletions roles/matrix-synapse/tasks/workers/setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# a negative when condition will not actually prevent ansible from executing loops in imported tasks!

- import_tasks: "{{ role_path }}/tasks/workers/setup_install.yml"
when: "matrix_synapse_enabled|bool and matrix_synapse_workers_enabled|bool"

- import_tasks: "{{ role_path }}/tasks/workers/setup_uninstall.yml"
when: "not matrix_synapse_workers_enabled|bool"
42 changes: 42 additions & 0 deletions roles/matrix-synapse/tasks/workers/setup_install.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---

- name: Ensure synapse worker base service file installed
template:
src: "{{ role_path }}/templates/synapse/systemd/matrix-synapse-worker@.service.j2"
dest: "{{ matrix_systemd_path }}/matrix-synapse-worker@.service"
mode: 0644
register: matrix_synapse_worker_systemd_service_result

- name: Ensure previous worker service symlinks are cleaned (FIXME)
file:
path: "{{ item.root + '/' + item.path }}"
state: absent
when:
- matrix_synapse_workers_enabled|bool
- item.state == 'link'
- item.path is match('matrix-synapse-worker@.*\\.service')
with_filetree:
- "{{ matrix_systemd_path }}/matrix-synapse.service.wants"

- name: Ensure systemd reloaded the worker service unit
service:
daemon_reload: yes

- name: Ensure individual worker service symlinks exist
service:
name: "matrix-synapse-worker@{{ item.worker }}:{{ item.port }}.service"
enabled: true
with_items: "{{ matrix_synapse_workers_enabled_list }}"

- name: Ensure creation of specific worker configs
template:
src: "{{ role_path }}/templates/synapse/worker.yaml.j2"
dest: "{{ matrix_synapse_config_dir_path }}/worker.{{ item.worker }}:{{ item.port }}.yaml"
with_list: "{{ matrix_synapse_workers_enabled_list }}"

- name: Add workers to synapse.wants list
set_fact:
matrix_synapse_systemd_wanted_services_list: >
{{ matrix_synapse_systemd_wanted_services_list +
['matrix-synapse-worker@' + item.worker + ':' + item.port|string + '.service'] }}
with_items: "{{ matrix_synapse_workers_enabled_list }}"
38 changes: 38 additions & 0 deletions roles/matrix-synapse/tasks/workers/setup_uninstall.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---

- name: Populate service facts
service_facts:

- name: Ensure any worker services are stopped
service:
name: "{{ item.key }}"
state: stopped
with_dict: "{{ ansible_facts.services|default({})|dict2items|selectattr('key', 'match', 'matrix-synapse-worker@.+\\.service')|list|items2dict }}"

# As we cannot know the ports of workers removed from the enabled_list..
# => .. just kill them all (FIXME?)
- name: Ensure previous worker service symlinks are cleaned
file:
path: "{{ item.root + '/' + item.path }}"
state: absent
when:
- not matrix_synapse_workers_enabled|bool
- item.state == 'link'
- item.path is match('matrix-synapse-worker@.*\\.service')
with_filetree:
- "{{ matrix_systemd_path }}/matrix-synapse.service.wants"

- name: Ensure synapse worker base service file gets removed
file:
path: "{{ matrix_systemd_path }}/matrix-synapse-worker@.service"
state: absent
register: matrix_synapse_worker_systemd_service_result

- name: Remove workers from synapse.wants list
set_fact:
matrix_synapse_systemd_wanted_services_list: "{{ matrix_synapse_systemd_wanted_services_list | reject('search', item) | list }}"
with_items: "{{ matrix_synapse_workers_avail_list }}"

- name: Ensure systemd noticed removal of worker service units
service:
daemon_reload: yes
38 changes: 38 additions & 0 deletions roles/matrix-synapse/templates/synapse/homeserver.yaml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,44 @@ listeners:
type: manhole
{% endif %}

{% if matrix_synapse_workers_enabled %}
# c.f. https://github.com/matrix-org/synapse/tree/master/docs/workers.md
# TCP replication: streaming data from the master to the workers
- port: {{ matrix_synapse_replication_tcp_port }}
bind_addresses: ['0.0.0.0']
type: replication

# HTTP replication: for the workers to send data to the main synapse process
- port: {{ matrix_synapse_replication_http_port }}
bind_addresses: ['0.0.0.0']
type: http
resources:
- names: [replication]

# c.f. https://github.com/matrix-org/synapse/tree/master/contrib/systemd-with-workers/README.md
worker_app: synapse.app.homeserver

# thx https://oznetnerd.com/2017/04/18/jinja2-selectattr-filter/
# reduce the main worker's offerings to core homeserver business
{% if matrix_synapse_workers_enabled_list|selectattr('worker', 'equalto', 'appservice')|list %}
notify_appservices: false
{% endif %}
{% if matrix_synapse_workers_enabled_list|selectattr('worker', 'equalto', 'federation_sender')|list %}
send_federation: false
{% endif %}
{% if matrix_synapse_workers_enabled_list|selectattr('worker', 'equalto', 'media_repository')|list %}
enable_media_repo: false
{% endif %}
{% if matrix_synapse_workers_enabled_list|selectattr('worker', 'equalto', 'pusher')|list %}
start_pushers: false
{% endif %}
{% if matrix_synapse_workers_enabled_list|selectattr('worker', 'equalto', 'user_dir')|list %}
update_user_directory: false
{% endif %}

# rather let systemd handle the forking
daemonize: false
eMPee584 marked this conversation as resolved.
Show resolved Hide resolved
{% endif %}

## Homeserver blocking ##

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#jinja2: lstrip_blocks: "True"
# c.f. https://github.com/matrix-org/synapse/pull/4662
[Unit]
Description=Synapse Matrix Worker
After=matrix-synapse.service
BindsTo=matrix-synapse.service

[Service]
Type=simple

# Intentional delay, so that the homeserver (we likely depend on) can manage to start.
ExecStartPre=/bin/sleep 5

# systemd ftw 🤦‍♂️
# https://github.com/systemd/systemd/issues/14895#issuecomment-594123923
ExecStart=/bin/sh -c "WORKER=%i; WORKER=$${WORKER%%:*}; \
exec /usr/bin/docker exec \
--user={{ matrix_user_uid }}:{{ matrix_user_gid }} \
matrix-synapse \
python -m synapse.app.$${WORKER} -c /data/homeserver.yaml -c /data/worker.%i.yaml"

ExecStop=/usr/bin/docker exec matrix-synapse pkill -f %i
spantaleev marked this conversation as resolved.
Show resolved Hide resolved
KillMode=process
Restart=always
RestartSec=10
SyslogIdentifier=matrix-synapse-%i

[Install]
WantedBy=matrix-synapse.service
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ ExecStart=/usr/bin/docker run --rm --name matrix-synapse \
{% if matrix_synapse_manhole_enabled and matrix_synapse_container_manhole_api_host_bind_port %}
-p {{ matrix_synapse_container_manhole_api_host_bind_port }}:9000 \
{% endif %}
{% if matrix_synapse_workers_enabled %}
{% for worker in matrix_synapse_workers_enabled_list %}
-p {{ worker.port }}:{{ worker.port }} \
{% endfor %}
{% endif %}
-v {{ matrix_synapse_config_dir_path }}:/data:ro \
-v {{ matrix_synapse_run_path }}:/matrix-run:rw \
-v {{ matrix_synapse_storage_path }}:/matrix-media-store-parent:slave \
Expand Down
Loading