2020-02-25 17:56:55 +01:00
|
|
|
# Copyright 2016 OpenMarket Ltd
|
|
|
|
# Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
import logging
|
|
|
|
import sys
|
2022-11-16 23:16:25 +01:00
|
|
|
from typing import Dict, List
|
2020-02-25 17:56:55 +01:00
|
|
|
|
2021-11-10 21:06:54 +01:00
|
|
|
from twisted.web.resource import Resource
|
2020-02-25 17:56:55 +01:00
|
|
|
|
|
|
|
import synapse
|
|
|
|
import synapse.events
|
|
|
|
from synapse.api.urls import (
|
|
|
|
CLIENT_API_PREFIX,
|
|
|
|
FEDERATION_PREFIX,
|
|
|
|
LEGACY_MEDIA_PREFIX,
|
2021-11-17 16:30:24 +01:00
|
|
|
MEDIA_R0_PREFIX,
|
|
|
|
MEDIA_V3_PREFIX,
|
2022-10-20 17:32:47 +02:00
|
|
|
SERVER_KEY_PREFIX,
|
2020-02-25 17:56:55 +01:00
|
|
|
)
|
|
|
|
from synapse.app import _base
|
2021-06-21 12:41:25 +02:00
|
|
|
from synapse.app._base import (
|
|
|
|
handle_startup_exception,
|
|
|
|
max_request_body_size,
|
|
|
|
redirect_stdio_to_logs,
|
|
|
|
register_start,
|
|
|
|
)
|
2020-02-25 17:56:55 +01:00
|
|
|
from synapse.config._base import ConfigError
|
|
|
|
from synapse.config.homeserver import HomeServerConfig
|
|
|
|
from synapse.config.logger import setup_logging
|
2023-04-03 11:27:51 +02:00
|
|
|
from synapse.config.server import ListenerConfig, TCPListenerConfig
|
2020-02-25 17:56:55 +01:00
|
|
|
from synapse.federation.transport.server import TransportLayerServer
|
2020-05-22 15:30:07 +02:00
|
|
|
from synapse.http.server import JsonResource, OptionsResource
|
2020-04-03 13:29:30 +02:00
|
|
|
from synapse.logging.context import LoggingContext
|
2020-02-25 17:56:55 +01:00
|
|
|
from synapse.metrics import METRICS_PREFIX, MetricsResource, RegistryProxy
|
2020-05-18 13:24:48 +02:00
|
|
|
from synapse.replication.http import REPLICATION_PREFIX, ReplicationRestResource
|
2022-12-02 19:10:05 +01:00
|
|
|
from synapse.rest import ClientRestResource
|
2020-02-25 17:56:55 +01:00
|
|
|
from synapse.rest.admin import register_servlets_for_media_repo
|
2020-08-07 15:21:24 +02:00
|
|
|
from synapse.rest.health import HealthResource
|
2022-10-20 17:32:47 +02:00
|
|
|
from synapse.rest.key.v2 import KeyResource
|
2021-02-01 16:47:59 +01:00
|
|
|
from synapse.rest.synapse.client import build_synapse_client_resource_tree
|
2021-11-01 16:10:16 +01:00
|
|
|
from synapse.rest.well_known import well_known_resource
|
2021-04-14 18:06:06 +02:00
|
|
|
from synapse.server import HomeServer
|
2022-07-21 19:56:45 +02:00
|
|
|
from synapse.storage.databases.main.account_data import AccountDataWorkerStore
|
|
|
|
from synapse.storage.databases.main.appservice import (
|
|
|
|
ApplicationServiceTransactionWorkerStore,
|
|
|
|
ApplicationServiceWorkerStore,
|
|
|
|
)
|
2020-08-05 22:38:57 +02:00
|
|
|
from synapse.storage.databases.main.censor_events import CensorEventsStore
|
2020-10-07 17:27:56 +02:00
|
|
|
from synapse.storage.databases.main.client_ips import ClientIpWorkerStore
|
2022-07-21 19:56:45 +02:00
|
|
|
from synapse.storage.databases.main.deviceinbox import DeviceInboxWorkerStore
|
2022-11-11 11:51:49 +01:00
|
|
|
from synapse.storage.databases.main.devices import DeviceWorkerStore
|
2022-07-21 19:56:45 +02:00
|
|
|
from synapse.storage.databases.main.directory import DirectoryWorkerStore
|
2021-01-11 19:01:27 +01:00
|
|
|
from synapse.storage.databases.main.e2e_room_keys import EndToEndRoomKeyStore
|
2022-11-11 11:51:49 +01:00
|
|
|
from synapse.storage.databases.main.event_federation import EventFederationWorkerStore
|
|
|
|
from synapse.storage.databases.main.event_push_actions import (
|
|
|
|
EventPushActionsWorkerStore,
|
|
|
|
)
|
|
|
|
from synapse.storage.databases.main.events_worker import EventsWorkerStore
|
|
|
|
from synapse.storage.databases.main.filtering import FilteringWorkerStore
|
|
|
|
from synapse.storage.databases.main.keys import KeyStore
|
2021-06-29 20:15:47 +02:00
|
|
|
from synapse.storage.databases.main.lock import LockStore
|
2020-08-05 22:38:57 +02:00
|
|
|
from synapse.storage.databases.main.media_repository import MediaRepositoryStore
|
2020-10-02 14:23:15 +02:00
|
|
|
from synapse.storage.databases.main.metrics import ServerMetricsStore
|
2020-08-05 22:38:57 +02:00
|
|
|
from synapse.storage.databases.main.monthly_active_users import (
|
2020-02-25 17:56:55 +01:00
|
|
|
MonthlyActiveUsersWorkerStore,
|
|
|
|
)
|
2021-04-23 13:21:55 +02:00
|
|
|
from synapse.storage.databases.main.presence import PresenceStore
|
2022-07-21 19:56:45 +02:00
|
|
|
from synapse.storage.databases.main.profile import ProfileWorkerStore
|
2022-11-11 11:51:49 +01:00
|
|
|
from synapse.storage.databases.main.push_rule import PushRulesWorkerStore
|
|
|
|
from synapse.storage.databases.main.pusher import PusherWorkerStore
|
2022-07-21 19:56:45 +02:00
|
|
|
from synapse.storage.databases.main.receipts import ReceiptsWorkerStore
|
|
|
|
from synapse.storage.databases.main.registration import RegistrationWorkerStore
|
2022-11-11 11:51:49 +01:00
|
|
|
from synapse.storage.databases.main.relations import RelationsWorkerStore
|
2021-08-17 15:02:50 +02:00
|
|
|
from synapse.storage.databases.main.room import RoomWorkerStore
|
2021-11-29 14:13:23 +01:00
|
|
|
from synapse.storage.databases.main.room_batch import RoomBatchStore
|
2022-11-11 11:51:49 +01:00
|
|
|
from synapse.storage.databases.main.roommember import RoomMemberWorkerStore
|
2021-05-27 23:45:43 +02:00
|
|
|
from synapse.storage.databases.main.search import SearchStore
|
2021-08-24 14:14:03 +02:00
|
|
|
from synapse.storage.databases.main.session import SessionStore
|
2022-11-11 11:51:49 +01:00
|
|
|
from synapse.storage.databases.main.signatures import SignatureWorkerStore
|
|
|
|
from synapse.storage.databases.main.state import StateGroupWorkerStore
|
2020-10-02 14:23:15 +02:00
|
|
|
from synapse.storage.databases.main.stats import StatsStore
|
2022-11-11 11:51:49 +01:00
|
|
|
from synapse.storage.databases.main.stream import StreamWorkerStore
|
2022-07-21 19:56:45 +02:00
|
|
|
from synapse.storage.databases.main.tags import TagsWorkerStore
|
2020-10-07 17:27:56 +02:00
|
|
|
from synapse.storage.databases.main.transactions import TransactionWorkerStore
|
2020-08-05 22:38:57 +02:00
|
|
|
from synapse.storage.databases.main.ui_auth import UIAuthWorkerStore
|
|
|
|
from synapse.storage.databases.main.user_directory import UserDirectoryStore
|
2022-11-11 11:51:49 +01:00
|
|
|
from synapse.storage.databases.main.user_erasure_store import UserErasureWorkerStore
|
2022-06-07 16:24:11 +02:00
|
|
|
from synapse.util import SYNAPSE_VERSION
|
2020-02-25 17:56:55 +01:00
|
|
|
from synapse.util.httpresourcetree import create_resource_tree
|
|
|
|
|
|
|
|
logger = logging.getLogger("synapse.app.generic_worker")
|
|
|
|
|
|
|
|
|
|
|
|
class GenericWorkerSlavedStore(
|
|
|
|
# FIXME(#3714): We need to add UserDirectoryStore as we write directly
|
|
|
|
# rather than going via the correct worker.
|
|
|
|
UserDirectoryStore,
|
2020-10-02 14:23:15 +02:00
|
|
|
StatsStore,
|
2020-04-30 19:47:49 +02:00
|
|
|
UIAuthWorkerStore,
|
2021-01-11 19:01:27 +01:00
|
|
|
EndToEndRoomKeyStore,
|
2021-04-23 13:21:55 +02:00
|
|
|
PresenceStore,
|
2022-07-21 19:56:45 +02:00
|
|
|
DeviceInboxWorkerStore,
|
2022-11-11 11:51:49 +01:00
|
|
|
DeviceWorkerStore,
|
2022-07-21 19:56:45 +02:00
|
|
|
TagsWorkerStore,
|
|
|
|
AccountDataWorkerStore,
|
2020-05-13 18:15:40 +02:00
|
|
|
CensorEventsStore,
|
2020-10-07 17:27:56 +02:00
|
|
|
ClientIpWorkerStore,
|
2022-11-11 11:51:49 +01:00
|
|
|
# KeyStore isn't really safe to use from a worker, but for now we do so and hope that
|
|
|
|
# the races it creates aren't too bad.
|
|
|
|
KeyStore,
|
2021-08-17 15:02:50 +02:00
|
|
|
RoomWorkerStore,
|
2021-11-29 14:13:23 +01:00
|
|
|
RoomBatchStore,
|
2022-07-21 19:56:45 +02:00
|
|
|
DirectoryWorkerStore,
|
2022-11-11 11:51:49 +01:00
|
|
|
PushRulesWorkerStore,
|
2022-07-21 19:56:45 +02:00
|
|
|
ApplicationServiceTransactionWorkerStore,
|
|
|
|
ApplicationServiceWorkerStore,
|
|
|
|
ProfileWorkerStore,
|
2022-11-11 11:51:49 +01:00
|
|
|
FilteringWorkerStore,
|
2020-02-25 17:56:55 +01:00
|
|
|
MonthlyActiveUsersWorkerStore,
|
|
|
|
MediaRepositoryStore,
|
2020-10-02 14:23:15 +02:00
|
|
|
ServerMetricsStore,
|
2022-11-11 11:51:49 +01:00
|
|
|
PusherWorkerStore,
|
|
|
|
RoomMemberWorkerStore,
|
|
|
|
RelationsWorkerStore,
|
|
|
|
EventFederationWorkerStore,
|
|
|
|
EventPushActionsWorkerStore,
|
|
|
|
StateGroupWorkerStore,
|
|
|
|
SignatureWorkerStore,
|
|
|
|
UserErasureWorkerStore,
|
2022-07-21 19:56:45 +02:00
|
|
|
ReceiptsWorkerStore,
|
2022-11-11 11:51:49 +01:00
|
|
|
StreamWorkerStore,
|
|
|
|
EventsWorkerStore,
|
2022-07-21 19:56:45 +02:00
|
|
|
RegistrationWorkerStore,
|
2021-05-27 23:45:43 +02:00
|
|
|
SearchStore,
|
2020-10-07 17:27:56 +02:00
|
|
|
TransactionWorkerStore,
|
2021-06-29 20:15:47 +02:00
|
|
|
LockStore,
|
2021-08-24 14:14:03 +02:00
|
|
|
SessionStore,
|
2020-02-25 17:56:55 +01:00
|
|
|
):
|
2021-11-10 21:06:54 +01:00
|
|
|
# Properties that multiple storage classes define. Tell mypy what the
|
|
|
|
# expected type is.
|
|
|
|
server_name: str
|
|
|
|
config: HomeServerConfig
|
2020-02-25 17:56:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
class GenericWorkerServer(HomeServer):
|
2021-11-10 21:06:54 +01:00
|
|
|
DATASTORE_CLASS = GenericWorkerSlavedStore # type: ignore
|
2020-02-25 17:56:55 +01:00
|
|
|
|
2021-11-10 21:06:54 +01:00
|
|
|
def _listen_http(self, listener_config: ListenerConfig) -> None:
|
2020-06-16 13:44:07 +02:00
|
|
|
assert listener_config.http_options is not None
|
|
|
|
|
2020-08-07 15:21:24 +02:00
|
|
|
# We always include a health resource.
|
2021-11-10 21:06:54 +01:00
|
|
|
resources: Dict[str, Resource] = {"/health": HealthResource()}
|
2020-08-07 15:21:24 +02:00
|
|
|
|
2020-06-16 13:44:07 +02:00
|
|
|
for res in listener_config.http_options.resources:
|
|
|
|
for name in res.names:
|
2020-02-25 17:56:55 +01:00
|
|
|
if name == "metrics":
|
|
|
|
resources[METRICS_PREFIX] = MetricsResource(RegistryProxy)
|
|
|
|
elif name == "client":
|
2022-12-02 19:10:05 +01:00
|
|
|
resource: Resource = ClientRestResource(self)
|
2020-02-25 17:56:55 +01:00
|
|
|
|
2022-10-20 17:32:47 +02:00
|
|
|
resources[CLIENT_API_PREFIX] = resource
|
2021-02-01 16:47:59 +01:00
|
|
|
|
|
|
|
resources.update(build_synapse_client_resource_tree(self))
|
2022-10-20 17:32:47 +02:00
|
|
|
resources["/.well-known"] = well_known_resource(self)
|
2021-11-01 16:10:16 +01:00
|
|
|
|
2020-02-25 17:56:55 +01:00
|
|
|
elif name == "federation":
|
2022-10-20 17:32:47 +02:00
|
|
|
resources[FEDERATION_PREFIX] = TransportLayerServer(self)
|
2020-02-25 17:56:55 +01:00
|
|
|
elif name == "media":
|
2021-09-24 13:25:21 +02:00
|
|
|
if self.config.media.can_load_media_repo:
|
2020-02-27 17:33:21 +01:00
|
|
|
media_repo = self.get_media_repository_resource()
|
|
|
|
|
|
|
|
# We need to serve the admin servlets for media on the
|
|
|
|
# worker.
|
|
|
|
admin_resource = JsonResource(self, canonical_json=False)
|
|
|
|
register_servlets_for_media_repo(self, admin_resource)
|
|
|
|
|
|
|
|
resources.update(
|
|
|
|
{
|
2021-11-17 16:30:24 +01:00
|
|
|
MEDIA_R0_PREFIX: media_repo,
|
|
|
|
MEDIA_V3_PREFIX: media_repo,
|
2020-02-27 17:33:21 +01:00
|
|
|
LEGACY_MEDIA_PREFIX: media_repo,
|
|
|
|
"/_synapse/admin": admin_resource,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
logger.warning(
|
|
|
|
"A 'media' listener is configured but the media"
|
|
|
|
" repository is disabled. Ignoring."
|
|
|
|
)
|
2023-01-11 13:00:38 +01:00
|
|
|
elif name == "health":
|
|
|
|
# Skip loading, health resource is always included
|
|
|
|
continue
|
2020-02-25 17:56:55 +01:00
|
|
|
|
2020-06-16 13:44:07 +02:00
|
|
|
if name == "openid" and "federation" not in res.names:
|
2020-02-25 17:56:55 +01:00
|
|
|
# Only load the openid resource separately if federation resource
|
|
|
|
# is not specified since federation resource includes openid
|
|
|
|
# resource.
|
2022-10-20 17:32:47 +02:00
|
|
|
resources[FEDERATION_PREFIX] = TransportLayerServer(
|
|
|
|
self, servlet_groups=["openid"]
|
2020-02-25 17:56:55 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
if name in ["keys", "federation"]:
|
2022-10-20 17:32:47 +02:00
|
|
|
resources[SERVER_KEY_PREFIX] = KeyResource(self)
|
2020-02-25 17:56:55 +01:00
|
|
|
|
2020-05-18 13:24:48 +02:00
|
|
|
if name == "replication":
|
|
|
|
resources[REPLICATION_PREFIX] = ReplicationRestResource(self)
|
|
|
|
|
2021-06-18 13:15:52 +02:00
|
|
|
# Attach additional resources registered by modules.
|
|
|
|
resources.update(self._module_web_resources)
|
|
|
|
self._module_web_resources_consumed = True
|
|
|
|
|
2020-05-22 15:30:07 +02:00
|
|
|
root_resource = create_resource_tree(resources, OptionsResource())
|
2020-02-25 17:56:55 +01:00
|
|
|
|
2022-11-15 13:55:00 +01:00
|
|
|
_base.listen_http(
|
|
|
|
listener_config,
|
|
|
|
root_resource,
|
|
|
|
self.version_string,
|
|
|
|
max_request_body_size(self.config),
|
|
|
|
self.tls_server_context_factory,
|
2020-02-25 17:56:55 +01:00
|
|
|
reactor=self.get_reactor(),
|
|
|
|
)
|
|
|
|
|
2021-11-10 21:06:54 +01:00
|
|
|
def start_listening(self) -> None:
|
2021-09-24 13:25:21 +02:00
|
|
|
for listener in self.config.worker.worker_listeners:
|
2020-06-16 13:44:07 +02:00
|
|
|
if listener.type == "http":
|
2020-02-25 17:56:55 +01:00
|
|
|
self._listen_http(listener)
|
2020-06-16 13:44:07 +02:00
|
|
|
elif listener.type == "manhole":
|
2023-04-03 11:27:51 +02:00
|
|
|
if isinstance(listener, TCPListenerConfig):
|
|
|
|
_base.listen_manhole(
|
|
|
|
listener.bind_addresses,
|
|
|
|
listener.port,
|
|
|
|
manhole_settings=self.config.server.manhole_settings,
|
|
|
|
manhole_globals={"hs": self},
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
raise ConfigError(
|
|
|
|
"Can not using a unix socket for manhole at this time."
|
|
|
|
)
|
|
|
|
|
2020-06-16 13:44:07 +02:00
|
|
|
elif listener.type == "metrics":
|
2021-09-23 18:03:01 +02:00
|
|
|
if not self.config.metrics.enable_metrics:
|
2020-02-25 17:56:55 +01:00
|
|
|
logger.warning(
|
2021-07-19 16:28:05 +02:00
|
|
|
"Metrics listener configured, but "
|
|
|
|
"enable_metrics is not True!"
|
2020-02-25 17:56:55 +01:00
|
|
|
)
|
|
|
|
else:
|
2023-04-03 11:27:51 +02:00
|
|
|
if isinstance(listener, TCPListenerConfig):
|
|
|
|
_base.listen_metrics(
|
|
|
|
listener.bind_addresses,
|
|
|
|
listener.port,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
raise ConfigError(
|
|
|
|
"Can not use a unix socket for metrics at this time."
|
|
|
|
)
|
|
|
|
|
2020-02-25 17:56:55 +01:00
|
|
|
else:
|
2020-06-16 13:44:07 +02:00
|
|
|
logger.warning("Unsupported listener type: %s", listener.type)
|
2020-02-25 17:56:55 +01:00
|
|
|
|
2022-03-10 14:01:56 +01:00
|
|
|
self.get_replication_command_handler().start_replication(self)
|
2020-02-25 17:56:55 +01:00
|
|
|
|
|
|
|
|
2021-11-10 21:06:54 +01:00
|
|
|
def start(config_options: List[str]) -> None:
|
2020-02-25 17:56:55 +01:00
|
|
|
try:
|
|
|
|
config = HomeServerConfig.load_config("Synapse worker", config_options)
|
|
|
|
except ConfigError as e:
|
|
|
|
sys.stderr.write("\n" + str(e) + "\n")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# For backwards compatibility let any of the old app names.
|
2021-09-13 19:07:12 +02:00
|
|
|
assert config.worker.worker_app in (
|
2020-02-25 17:56:55 +01:00
|
|
|
"synapse.app.appservice",
|
|
|
|
"synapse.app.client_reader",
|
|
|
|
"synapse.app.event_creator",
|
|
|
|
"synapse.app.federation_reader",
|
|
|
|
"synapse.app.federation_sender",
|
|
|
|
"synapse.app.frontend_proxy",
|
|
|
|
"synapse.app.generic_worker",
|
|
|
|
"synapse.app.media_repository",
|
|
|
|
"synapse.app.pusher",
|
|
|
|
"synapse.app.synchrotron",
|
|
|
|
"synapse.app.user_dir",
|
|
|
|
)
|
|
|
|
|
2021-09-29 12:44:15 +02:00
|
|
|
synapse.events.USE_FROZEN_DICTS = config.server.use_frozen_dicts
|
2021-05-05 17:54:36 +02:00
|
|
|
synapse.util.caches.TRACK_MEMORY_USAGE = config.caches.track_memory_usage
|
2020-02-25 17:56:55 +01:00
|
|
|
|
2021-05-05 17:53:45 +02:00
|
|
|
if config.server.gc_seconds:
|
|
|
|
synapse.metrics.MIN_TIME_BETWEEN_GCS = config.server.gc_seconds
|
|
|
|
|
2020-04-28 14:34:12 +02:00
|
|
|
hs = GenericWorkerServer(
|
2021-09-13 19:07:12 +02:00
|
|
|
config.server.server_name,
|
2020-02-25 17:56:55 +01:00
|
|
|
config=config,
|
2022-06-07 16:24:11 +02:00
|
|
|
version_string=f"Synapse/{SYNAPSE_VERSION}",
|
2020-02-25 17:56:55 +01:00
|
|
|
)
|
|
|
|
|
2020-04-28 14:34:12 +02:00
|
|
|
setup_logging(hs, config, use_worker_options=True)
|
|
|
|
|
2021-06-21 12:41:25 +02:00
|
|
|
try:
|
|
|
|
hs.setup()
|
2020-04-28 14:34:12 +02:00
|
|
|
|
2021-06-21 12:41:25 +02:00
|
|
|
# Ensure the replication streamer is always started in case we write to any
|
|
|
|
# streams. Will no-op if no streams can be written to by this worker.
|
|
|
|
hs.get_replication_streamer()
|
|
|
|
except Exception as e:
|
|
|
|
handle_startup_exception(e)
|
2020-02-25 17:56:55 +01:00
|
|
|
|
2021-04-23 20:20:44 +02:00
|
|
|
register_start(_base.start, hs)
|
2020-02-25 17:56:55 +01:00
|
|
|
|
2021-06-21 12:41:25 +02:00
|
|
|
# redirect stdio to the logs, if configured.
|
2021-09-23 18:03:01 +02:00
|
|
|
if not hs.config.logging.no_redirect_stdio:
|
2021-06-21 12:41:25 +02:00
|
|
|
redirect_stdio_to_logs()
|
|
|
|
|
2020-02-25 17:56:55 +01:00
|
|
|
_base.start_worker_reactor("synapse-generic-worker", config)
|
|
|
|
|
|
|
|
|
2021-11-30 17:52:45 +01:00
|
|
|
def main() -> None:
|
2020-02-25 17:56:55 +01:00
|
|
|
with LoggingContext("main"):
|
|
|
|
start(sys.argv[1:])
|
2021-11-30 17:52:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|