Merge branch 'synapse-workers' into feature/add-worker-support

This commit is contained in:
Max Klenk
2020-08-27 15:22:12 +02:00
10 changed files with 362 additions and 0 deletions

View File

@ -260,6 +260,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

View File

@ -18,6 +18,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"

View File

@ -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"

View File

@ -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 }}"

View File

@ -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

View File

@ -223,6 +223,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
{% endif %}
# Forward extremities can build up in a room due to networking delays between
# homeservers. Once this happens in a large room, calculation of the state of

View File

@ -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
KillMode=process
Restart=always
RestartSec=10
SyslogIdentifier=matrix-synapse-%i
[Install]
WantedBy=matrix-synapse.service

View File

@ -43,6 +43,11 @@ ExecStart={{ matrix_host_command_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_storage_path }}:/matrix-media-store-parent:slave \
{% for volume in matrix_synapse_container_additional_volumes %}

View File

@ -0,0 +1,29 @@
#jinja2: lstrip_blocks: "True"
worker_app: synapse.app.{{ item.worker }}
worker_replication_host: 127.0.0.1
worker_replication_port: {{ matrix_synapse_replication_tcp_port }}
worker_replication_http_port: {{ matrix_synapse_replication_http_port }}
{% if item.worker not in [ 'appservice', 'federation_sender', 'pusher' ] %}
worker_listeners:
- type: http
port: {{ item.port }}
resources:
- names:
{% if item.worker in [ 'synchrotron', 'client_reader', 'event_creator', 'frontend_proxy', 'user_dir' ] %}
- client
{% elif item.worker in [ 'federation_reader' ] %}
- federation
{% elif item.worker in [ 'media_repository' ] %}
- media
{% endif %}
{% endif %}
{% if item.worker == 'frontend_proxy' %}
worker_main_http_uri: http://127.0.0.1:8008
{% endif %}
worker_daemonize: false
worker_pid_file: /matrix-run/{{ item.worker }}.port{{ item.port }}.pid
worker_log_config: /data/{{ matrix_server_fqn_matrix }}.log.config