2019-09-23 07:28:01 -04:00
|
|
|
# Copyright 2016 OpenMarket Ltd
|
2021-12-01 07:28:23 -05:00
|
|
|
# Copyright 2021 The Matrix.org Foundation C.I.C.
|
2016-06-16 06:09:15 -04:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2021-12-01 07:28:23 -05:00
|
|
|
import argparse
|
2020-09-14 05:16:41 -04:00
|
|
|
from typing import List, Union
|
|
|
|
|
2020-05-14 09:00:58 -04:00
|
|
|
import attr
|
|
|
|
|
2021-02-24 08:23:18 -05:00
|
|
|
from ._base import (
|
|
|
|
Config,
|
|
|
|
ConfigError,
|
|
|
|
RoutableShardedWorkerHandlingConfig,
|
|
|
|
ShardedWorkerHandlingConfig,
|
|
|
|
)
|
2020-06-16 07:44:07 -04:00
|
|
|
from .server import ListenerConfig, parse_listener_def
|
2016-06-16 06:09:15 -04:00
|
|
|
|
2021-02-24 08:23:18 -05:00
|
|
|
_FEDERATION_SENDER_WITH_SEND_FEDERATION_ENABLED_ERROR = """
|
|
|
|
The send_federation config option must be disabled in the main
|
|
|
|
synapse process before they can be run in a separate worker.
|
|
|
|
|
|
|
|
Please add ``send_federation: false`` to the main config
|
|
|
|
"""
|
|
|
|
|
|
|
|
_PUSHER_WITH_START_PUSHERS_ENABLED_ERROR = """
|
|
|
|
The start_pushers config option must be disabled in the main
|
|
|
|
synapse process before they can be run in a separate worker.
|
|
|
|
|
|
|
|
Please add ``start_pushers: false`` to the main config
|
|
|
|
"""
|
|
|
|
|
2016-06-16 06:09:15 -04:00
|
|
|
|
2020-09-14 05:16:41 -04:00
|
|
|
def _instance_to_list_converter(obj: Union[str, List[str]]) -> List[str]:
|
|
|
|
"""Helper for allowing parsing a string or list of strings to a config
|
|
|
|
option expecting a list of strings.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if isinstance(obj, str):
|
|
|
|
return [obj]
|
|
|
|
return obj
|
|
|
|
|
|
|
|
|
2020-05-14 09:00:58 -04:00
|
|
|
@attr.s
|
|
|
|
class InstanceLocationConfig:
|
2021-02-16 17:32:34 -05:00
|
|
|
"""The host and port to talk to an instance via HTTP replication."""
|
2020-05-14 09:00:58 -04:00
|
|
|
|
|
|
|
host = attr.ib(type=str)
|
|
|
|
port = attr.ib(type=int)
|
|
|
|
|
|
|
|
|
2020-05-22 11:11:35 -04:00
|
|
|
@attr.s
|
|
|
|
class WriterLocations:
|
|
|
|
"""Specifies the instances that write various streams.
|
|
|
|
|
|
|
|
Attributes:
|
2020-09-14 05:16:41 -04:00
|
|
|
events: The instances that write to the event and backfill streams.
|
2021-11-03 10:25:47 -04:00
|
|
|
typing: The instances that write to the typing stream. Currently
|
|
|
|
can only be a single instance.
|
2021-04-23 07:21:55 -04:00
|
|
|
to_device: The instances that write to the to_device stream. Currently
|
|
|
|
can only be a single instance.
|
|
|
|
account_data: The instances that write to the account data streams. Currently
|
|
|
|
can only be a single instance.
|
|
|
|
receipts: The instances that write to the receipts stream. Currently
|
|
|
|
can only be a single instance.
|
|
|
|
presence: The instances that write to the presence stream. Currently
|
|
|
|
can only be a single instance.
|
2020-05-22 11:11:35 -04:00
|
|
|
"""
|
|
|
|
|
2020-09-14 05:16:41 -04:00
|
|
|
events = attr.ib(
|
2021-11-03 10:25:47 -04:00
|
|
|
default=["master"],
|
|
|
|
type=List[str],
|
|
|
|
converter=_instance_to_list_converter,
|
|
|
|
)
|
|
|
|
typing = attr.ib(
|
|
|
|
default=["master"],
|
|
|
|
type=List[str],
|
|
|
|
converter=_instance_to_list_converter,
|
2020-09-14 05:16:41 -04:00
|
|
|
)
|
2021-01-07 15:19:26 -05:00
|
|
|
to_device = attr.ib(
|
2021-02-16 17:32:34 -05:00
|
|
|
default=["master"],
|
|
|
|
type=List[str],
|
|
|
|
converter=_instance_to_list_converter,
|
2021-01-07 15:19:26 -05:00
|
|
|
)
|
2021-01-18 10:47:59 -05:00
|
|
|
account_data = attr.ib(
|
2021-02-16 17:32:34 -05:00
|
|
|
default=["master"],
|
|
|
|
type=List[str],
|
|
|
|
converter=_instance_to_list_converter,
|
2021-01-18 10:47:59 -05:00
|
|
|
)
|
|
|
|
receipts = attr.ib(
|
2021-02-16 17:32:34 -05:00
|
|
|
default=["master"],
|
|
|
|
type=List[str],
|
|
|
|
converter=_instance_to_list_converter,
|
2021-01-18 10:47:59 -05:00
|
|
|
)
|
2021-04-23 07:21:55 -04:00
|
|
|
presence = attr.ib(
|
|
|
|
default=["master"],
|
|
|
|
type=List[str],
|
|
|
|
converter=_instance_to_list_converter,
|
|
|
|
)
|
2020-05-22 11:11:35 -04:00
|
|
|
|
|
|
|
|
2016-06-16 06:09:15 -04:00
|
|
|
class WorkerConfig(Config):
|
|
|
|
"""The workers are processes run separately to the main synapse process.
|
|
|
|
They have their own pid_file and listener configuration. They use the
|
2016-06-16 12:29:50 -04:00
|
|
|
replication_url to talk to the main synapse process."""
|
2016-06-16 06:09:15 -04:00
|
|
|
|
2019-10-10 04:39:35 -04:00
|
|
|
section = "worker"
|
|
|
|
|
2019-06-24 06:34:45 -04:00
|
|
|
def read_config(self, config, **kwargs):
|
2016-06-16 12:29:50 -04:00
|
|
|
self.worker_app = config.get("worker_app")
|
2018-01-12 05:39:27 -05:00
|
|
|
|
|
|
|
# Canonicalise worker_app so that master always has None
|
|
|
|
if self.worker_app == "synapse.app.homeserver":
|
|
|
|
self.worker_app = None
|
|
|
|
|
2020-06-16 07:44:07 -04:00
|
|
|
self.worker_listeners = [
|
|
|
|
parse_listener_def(x) for x in config.get("worker_listeners", [])
|
|
|
|
]
|
2016-06-16 12:29:50 -04:00
|
|
|
self.worker_daemonize = config.get("worker_daemonize")
|
|
|
|
self.worker_pid_file = config.get("worker_pid_file")
|
2019-07-22 11:05:00 -04:00
|
|
|
self.worker_log_config = config.get("worker_log_config")
|
2018-02-05 12:22:16 -05:00
|
|
|
|
|
|
|
# The host used to connect to the main synapse
|
2017-03-27 11:33:44 -04:00
|
|
|
self.worker_replication_host = config.get("worker_replication_host", None)
|
2018-02-05 12:22:16 -05:00
|
|
|
|
|
|
|
# The port on the main synapse for TCP replication
|
2017-03-27 11:33:44 -04:00
|
|
|
self.worker_replication_port = config.get("worker_replication_port", None)
|
2018-02-05 12:22:16 -05:00
|
|
|
|
|
|
|
# The port on the main synapse for HTTP replication endpoint
|
|
|
|
self.worker_replication_http_port = config.get("worker_replication_http_port")
|
|
|
|
|
2020-12-04 10:56:28 -05:00
|
|
|
# The shared secret used for authentication when connecting to the main synapse.
|
|
|
|
self.worker_replication_secret = config.get("worker_replication_secret", None)
|
|
|
|
|
2017-03-27 11:33:44 -04:00
|
|
|
self.worker_name = config.get("worker_name", self.worker_app)
|
2021-02-24 08:23:18 -05:00
|
|
|
self.instance_name = self.worker_name or "master"
|
2017-01-20 06:45:29 -05:00
|
|
|
|
2017-07-07 13:19:46 -04:00
|
|
|
self.worker_main_http_uri = config.get("worker_main_http_uri", None)
|
|
|
|
|
2019-03-14 09:32:14 -04:00
|
|
|
# This option is really only here to support `--manhole` command line
|
|
|
|
# argument.
|
|
|
|
manhole = config.get("worker_manhole")
|
|
|
|
if manhole:
|
2019-06-20 05:32:02 -04:00
|
|
|
self.worker_listeners.append(
|
2020-06-16 07:44:07 -04:00
|
|
|
ListenerConfig(
|
2021-02-16 17:32:34 -05:00
|
|
|
port=manhole,
|
|
|
|
bind_addresses=["127.0.0.1"],
|
|
|
|
type="manhole",
|
2020-06-16 07:44:07 -04:00
|
|
|
)
|
2019-06-20 05:32:02 -04:00
|
|
|
)
|
2019-03-14 09:32:14 -04:00
|
|
|
|
2021-02-24 08:23:18 -05:00
|
|
|
# Handle federation sender configuration.
|
|
|
|
#
|
|
|
|
# There are two ways of configuring which instances handle federation
|
|
|
|
# sending:
|
|
|
|
# 1. The old way where "send_federation" is set to false and running a
|
|
|
|
# `synapse.app.federation_sender` worker app.
|
|
|
|
# 2. Specifying the workers sending federation in
|
|
|
|
# `federation_sender_instances`.
|
|
|
|
#
|
|
|
|
|
|
|
|
send_federation = config.get("send_federation", True)
|
|
|
|
|
|
|
|
federation_sender_instances = config.get("federation_sender_instances")
|
|
|
|
if federation_sender_instances is None:
|
|
|
|
# Default to an empty list, which means "another, unknown, worker is
|
|
|
|
# responsible for it".
|
|
|
|
federation_sender_instances = []
|
2020-07-29 18:22:13 -04:00
|
|
|
|
2021-02-24 08:23:18 -05:00
|
|
|
# If no federation sender instances are set we check if
|
|
|
|
# `send_federation` is set, which means use master
|
|
|
|
if send_federation:
|
|
|
|
federation_sender_instances = ["master"]
|
|
|
|
|
|
|
|
if self.worker_app == "synapse.app.federation_sender":
|
|
|
|
if send_federation:
|
|
|
|
# If we're running federation senders, and not using
|
|
|
|
# `federation_sender_instances`, then we should have
|
|
|
|
# explicitly set `send_federation` to false.
|
|
|
|
raise ConfigError(
|
|
|
|
_FEDERATION_SENDER_WITH_SEND_FEDERATION_ENABLED_ERROR
|
|
|
|
)
|
|
|
|
|
|
|
|
federation_sender_instances = [self.worker_name]
|
|
|
|
|
|
|
|
self.send_federation = self.instance_name in federation_sender_instances
|
2020-07-29 18:22:13 -04:00
|
|
|
self.federation_shard_config = ShardedWorkerHandlingConfig(
|
|
|
|
federation_sender_instances
|
|
|
|
)
|
|
|
|
|
2020-05-14 09:00:58 -04:00
|
|
|
# A map from instance name to host/port of their HTTP replication endpoint.
|
2020-05-22 11:11:35 -04:00
|
|
|
instance_map = config.get("instance_map") or {}
|
2020-05-14 09:00:58 -04:00
|
|
|
self.instance_map = {
|
|
|
|
name: InstanceLocationConfig(**c) for name, c in instance_map.items()
|
|
|
|
}
|
|
|
|
|
2020-05-22 11:11:35 -04:00
|
|
|
# Map from type of streams to source, c.f. WriterLocations.
|
|
|
|
writers = config.get("stream_writers") or {}
|
|
|
|
self.writers = WriterLocations(**writers)
|
|
|
|
|
2020-09-14 05:16:41 -04:00
|
|
|
# Check that the configured writers for events and typing also appears in
|
2020-05-22 11:11:35 -04:00
|
|
|
# `instance_map`.
|
2021-04-23 07:21:55 -04:00
|
|
|
for stream in (
|
|
|
|
"events",
|
|
|
|
"typing",
|
|
|
|
"to_device",
|
|
|
|
"account_data",
|
|
|
|
"receipts",
|
|
|
|
"presence",
|
|
|
|
):
|
2020-09-14 05:16:41 -04:00
|
|
|
instances = _instance_to_list_converter(getattr(self.writers, stream))
|
|
|
|
for instance in instances:
|
|
|
|
if instance != "master" and instance not in self.instance_map:
|
|
|
|
raise ConfigError(
|
|
|
|
"Instance %r is configured to write %s but does not appear in `instance_map` config."
|
|
|
|
% (instance, stream)
|
|
|
|
)
|
|
|
|
|
2021-11-03 10:25:47 -04:00
|
|
|
if len(self.writers.typing) != 1:
|
|
|
|
raise ConfigError(
|
|
|
|
"Must only specify one instance to handle `typing` messages."
|
|
|
|
)
|
|
|
|
|
2021-01-07 15:19:26 -05:00
|
|
|
if len(self.writers.to_device) != 1:
|
|
|
|
raise ConfigError(
|
|
|
|
"Must only specify one instance to handle `to_device` messages."
|
|
|
|
)
|
|
|
|
|
2021-01-18 10:47:59 -05:00
|
|
|
if len(self.writers.account_data) != 1:
|
|
|
|
raise ConfigError(
|
|
|
|
"Must only specify one instance to handle `account_data` messages."
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(self.writers.receipts) != 1:
|
|
|
|
raise ConfigError(
|
|
|
|
"Must only specify one instance to handle `receipts` messages."
|
|
|
|
)
|
|
|
|
|
2021-02-24 08:23:18 -05:00
|
|
|
if len(self.writers.events) == 0:
|
|
|
|
raise ConfigError("Must specify at least one instance to handle `events`.")
|
|
|
|
|
2021-04-23 07:21:55 -04:00
|
|
|
if len(self.writers.presence) != 1:
|
|
|
|
raise ConfigError(
|
|
|
|
"Must only specify one instance to handle `presence` messages."
|
|
|
|
)
|
|
|
|
|
2021-02-24 08:23:18 -05:00
|
|
|
self.events_shard_config = RoutableShardedWorkerHandlingConfig(
|
|
|
|
self.writers.events
|
|
|
|
)
|
|
|
|
|
|
|
|
# Handle sharded push
|
|
|
|
start_pushers = config.get("start_pushers", True)
|
|
|
|
pusher_instances = config.get("pusher_instances")
|
|
|
|
if pusher_instances is None:
|
|
|
|
# Default to an empty list, which means "another, unknown, worker is
|
|
|
|
# responsible for it".
|
|
|
|
pusher_instances = []
|
|
|
|
|
|
|
|
# If no pushers instances are set we check if `start_pushers` is
|
|
|
|
# set, which means use master
|
|
|
|
if start_pushers:
|
|
|
|
pusher_instances = ["master"]
|
|
|
|
|
|
|
|
if self.worker_app == "synapse.app.pusher":
|
|
|
|
if start_pushers:
|
|
|
|
# If we're running pushers, and not using
|
|
|
|
# `pusher_instances`, then we should have explicitly set
|
|
|
|
# `start_pushers` to false.
|
|
|
|
raise ConfigError(_PUSHER_WITH_START_PUSHERS_ENABLED_ERROR)
|
|
|
|
|
|
|
|
pusher_instances = [self.instance_name]
|
|
|
|
|
|
|
|
self.start_pushers = self.instance_name in pusher_instances
|
|
|
|
self.pusher_shard_config = ShardedWorkerHandlingConfig(pusher_instances)
|
2020-05-22 11:11:35 -04:00
|
|
|
|
2020-10-02 08:23:15 -04:00
|
|
|
# Whether this worker should run background tasks or not.
|
|
|
|
#
|
|
|
|
# As a note for developers, the background tasks guarded by this should
|
|
|
|
# be able to run on only a single instance (meaning that they don't
|
|
|
|
# depend on any in-memory state of a particular worker).
|
|
|
|
#
|
|
|
|
# No effort is made to ensure only a single instance of these tasks is
|
|
|
|
# running.
|
|
|
|
background_tasks_instance = config.get("run_background_tasks_on") or "master"
|
|
|
|
self.run_background_tasks = (
|
|
|
|
self.worker_name is None and background_tasks_instance == "master"
|
|
|
|
) or self.worker_name == background_tasks_instance
|
|
|
|
|
2020-07-29 18:22:13 -04:00
|
|
|
def generate_config_section(self, config_dir_path, server_name, **kwargs):
|
|
|
|
return """\
|
|
|
|
## Workers ##
|
|
|
|
|
|
|
|
# Disables sending of outbound federation transactions on the main process.
|
|
|
|
# Uncomment if using a federation sender worker.
|
|
|
|
#
|
|
|
|
#send_federation: false
|
|
|
|
|
|
|
|
# It is possible to run multiple federation sender workers, in which case the
|
|
|
|
# work is balanced across them.
|
|
|
|
#
|
|
|
|
# This configuration must be shared between all federation sender workers, and if
|
|
|
|
# changed all federation sender workers must be stopped at the same time and then
|
|
|
|
# started, to ensure that all instances are running with the same config (otherwise
|
|
|
|
# events may be dropped).
|
|
|
|
#
|
|
|
|
#federation_sender_instances:
|
|
|
|
# - federation_sender1
|
|
|
|
|
|
|
|
# When using workers this should be a map from `worker_name` to the
|
|
|
|
# HTTP replication listener of the worker, if configured.
|
|
|
|
#
|
|
|
|
#instance_map:
|
|
|
|
# worker1:
|
|
|
|
# host: localhost
|
|
|
|
# port: 8034
|
|
|
|
|
|
|
|
# Experimental: When using workers you can define which workers should
|
|
|
|
# handle event persistence and typing notifications. Any worker
|
|
|
|
# specified here must also be in the `instance_map`.
|
|
|
|
#
|
|
|
|
#stream_writers:
|
|
|
|
# events: worker1
|
|
|
|
# typing: worker1
|
2020-10-02 08:23:15 -04:00
|
|
|
|
|
|
|
# The worker that is used to run background tasks (e.g. cleaning up expired
|
|
|
|
# data). If not provided this defaults to the main process.
|
|
|
|
#
|
|
|
|
#run_background_tasks_on: worker1
|
2020-12-04 10:56:28 -05:00
|
|
|
|
|
|
|
# A shared secret used by the replication APIs to authenticate HTTP requests
|
|
|
|
# from workers.
|
|
|
|
#
|
|
|
|
# By default this is unused and traffic is not authenticated.
|
|
|
|
#
|
|
|
|
#worker_replication_secret: ""
|
2020-07-29 18:22:13 -04:00
|
|
|
"""
|
|
|
|
|
2021-12-01 07:28:23 -05:00
|
|
|
def read_arguments(self, args: argparse.Namespace) -> None:
|
2019-03-14 09:32:14 -04:00
|
|
|
# We support a bunch of command line arguments that override options in
|
|
|
|
# the config. A lot of these options have a worker_* prefix when running
|
|
|
|
# on workers so we also have to override them when command line options
|
|
|
|
# are specified.
|
|
|
|
|
2019-03-13 13:33:54 -04:00
|
|
|
if args.daemonize is not None:
|
|
|
|
self.worker_daemonize = args.daemonize
|
2019-03-14 09:32:14 -04:00
|
|
|
if args.manhole is not None:
|
|
|
|
self.worker_manhole = args.worker_manhole
|