2016-06-16 04:09:15 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
2019-09-23 05:28:01 -06:00
|
|
|
# Copyright 2016 OpenMarket Ltd
|
2016-06-16 04:09:15 -06: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.
|
|
|
|
|
2020-05-14 07:00:58 -06:00
|
|
|
import attr
|
|
|
|
|
2020-05-22 09:11:35 -06:00
|
|
|
from ._base import Config, ConfigError
|
2020-06-16 05:44:07 -06:00
|
|
|
from .server import ListenerConfig, parse_listener_def
|
2016-06-16 04:09:15 -06:00
|
|
|
|
|
|
|
|
2020-05-14 07:00:58 -06:00
|
|
|
@attr.s
|
|
|
|
class InstanceLocationConfig:
|
|
|
|
"""The host and port to talk to an instance via HTTP replication.
|
|
|
|
"""
|
|
|
|
|
|
|
|
host = attr.ib(type=str)
|
|
|
|
port = attr.ib(type=int)
|
|
|
|
|
|
|
|
|
2020-05-22 09:11:35 -06:00
|
|
|
@attr.s
|
|
|
|
class WriterLocations:
|
|
|
|
"""Specifies the instances that write various streams.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
events: The instance that writes to the event and backfill streams.
|
2020-07-16 08:12:54 -06:00
|
|
|
events: The instance that writes to the typing stream.
|
2020-05-22 09:11:35 -06:00
|
|
|
"""
|
|
|
|
|
|
|
|
events = attr.ib(default="master", type=str)
|
2020-07-16 08:12:54 -06:00
|
|
|
typing = attr.ib(default="master", type=str)
|
2020-05-22 09:11:35 -06:00
|
|
|
|
|
|
|
|
2016-06-16 04:09:15 -06: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 10:29:50 -06:00
|
|
|
replication_url to talk to the main synapse process."""
|
2016-06-16 04:09:15 -06:00
|
|
|
|
2019-10-10 02:39:35 -06:00
|
|
|
section = "worker"
|
|
|
|
|
2019-06-24 04:34:45 -06:00
|
|
|
def read_config(self, config, **kwargs):
|
2016-06-16 10:29:50 -06:00
|
|
|
self.worker_app = config.get("worker_app")
|
2018-01-12 03:39:27 -07:00
|
|
|
|
|
|
|
# Canonicalise worker_app so that master always has None
|
|
|
|
if self.worker_app == "synapse.app.homeserver":
|
|
|
|
self.worker_app = None
|
|
|
|
|
2020-06-16 05:44:07 -06:00
|
|
|
self.worker_listeners = [
|
|
|
|
parse_listener_def(x) for x in config.get("worker_listeners", [])
|
|
|
|
]
|
2016-06-16 10:29:50 -06:00
|
|
|
self.worker_daemonize = config.get("worker_daemonize")
|
|
|
|
self.worker_pid_file = config.get("worker_pid_file")
|
2019-07-22 09:05:00 -06:00
|
|
|
self.worker_log_config = config.get("worker_log_config")
|
2018-02-05 10:22:16 -07:00
|
|
|
|
|
|
|
# The host used to connect to the main synapse
|
2017-03-27 09:33:44 -06:00
|
|
|
self.worker_replication_host = config.get("worker_replication_host", None)
|
2018-02-05 10:22:16 -07:00
|
|
|
|
|
|
|
# The port on the main synapse for TCP replication
|
2017-03-27 09:33:44 -06:00
|
|
|
self.worker_replication_port = config.get("worker_replication_port", None)
|
2018-02-05 10:22:16 -07:00
|
|
|
|
|
|
|
# The port on the main synapse for HTTP replication endpoint
|
|
|
|
self.worker_replication_http_port = config.get("worker_replication_http_port")
|
|
|
|
|
2017-03-27 09:33:44 -06:00
|
|
|
self.worker_name = config.get("worker_name", self.worker_app)
|
2017-01-20 04:45:29 -07:00
|
|
|
|
2017-07-07 11:19:46 -06:00
|
|
|
self.worker_main_http_uri = config.get("worker_main_http_uri", None)
|
|
|
|
|
2019-03-14 07:32:14 -06:00
|
|
|
# This option is really only here to support `--manhole` command line
|
|
|
|
# argument.
|
|
|
|
manhole = config.get("worker_manhole")
|
|
|
|
if manhole:
|
2019-06-20 03:32:02 -06:00
|
|
|
self.worker_listeners.append(
|
2020-06-16 05:44:07 -06:00
|
|
|
ListenerConfig(
|
|
|
|
port=manhole, bind_addresses=["127.0.0.1"], type="manhole",
|
|
|
|
)
|
2019-06-20 03:32:02 -06:00
|
|
|
)
|
2019-03-14 07:32:14 -06:00
|
|
|
|
2020-05-14 07:00:58 -06:00
|
|
|
# A map from instance name to host/port of their HTTP replication endpoint.
|
2020-05-22 09:11:35 -06:00
|
|
|
instance_map = config.get("instance_map") or {}
|
2020-05-14 07:00:58 -06:00
|
|
|
self.instance_map = {
|
|
|
|
name: InstanceLocationConfig(**c) for name, c in instance_map.items()
|
|
|
|
}
|
|
|
|
|
2020-05-22 09:11:35 -06:00
|
|
|
# Map from type of streams to source, c.f. WriterLocations.
|
|
|
|
writers = config.get("stream_writers") or {}
|
|
|
|
self.writers = WriterLocations(**writers)
|
|
|
|
|
2020-07-16 08:12:54 -06:00
|
|
|
# Check that the configured writer for events and typing also appears in
|
2020-05-22 09:11:35 -06:00
|
|
|
# `instance_map`.
|
2020-07-16 08:12:54 -06:00
|
|
|
for stream in ("events", "typing"):
|
|
|
|
instance = getattr(self.writers, stream)
|
|
|
|
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)
|
|
|
|
)
|
2020-05-22 09:11:35 -06:00
|
|
|
|
2019-03-13 11:33:54 -06:00
|
|
|
def read_arguments(self, args):
|
2019-03-14 07:32:14 -06: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 11:33:54 -06:00
|
|
|
if args.daemonize is not None:
|
|
|
|
self.worker_daemonize = args.daemonize
|
2019-03-14 07:32:14 -06:00
|
|
|
if args.manhole is not None:
|
|
|
|
self.worker_manhole = args.worker_manhole
|