2020-02-25 09:56:55 -07: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 15:16:25 -07:00
|
|
|
from typing import Dict, List
|
2020-02-25 09:56:55 -07:00
|
|
|
|
2021-11-10 13:06:54 -07:00
|
|
|
from twisted.web.resource import Resource
|
2020-02-25 09:56:55 -07:00
|
|
|
|
|
|
|
import synapse
|
|
|
|
import synapse.events
|
|
|
|
from synapse.api.urls import (
|
|
|
|
CLIENT_API_PREFIX,
|
|
|
|
FEDERATION_PREFIX,
|
|
|
|
LEGACY_MEDIA_PREFIX,
|
2021-11-17 08:30:24 -07:00
|
|
|
MEDIA_R0_PREFIX,
|
|
|
|
MEDIA_V3_PREFIX,
|
2022-10-20 09:32:47 -06:00
|
|
|
SERVER_KEY_PREFIX,
|
2020-02-25 09:56:55 -07:00
|
|
|
)
|
|
|
|
from synapse.app import _base
|
2021-06-21 04:41:25 -06:00
|
|
|
from synapse.app._base import (
|
|
|
|
handle_startup_exception,
|
|
|
|
max_request_body_size,
|
|
|
|
redirect_stdio_to_logs,
|
|
|
|
register_start,
|
|
|
|
)
|
2020-02-25 09:56:55 -07:00
|
|
|
from synapse.config._base import ConfigError
|
|
|
|
from synapse.config.homeserver import HomeServerConfig
|
|
|
|
from synapse.config.logger import setup_logging
|
2023-04-03 03:27:51 -06:00
|
|
|
from synapse.config.server import ListenerConfig, TCPListenerConfig
|
2020-02-25 09:56:55 -07:00
|
|
|
from synapse.federation.transport.server import TransportLayerServer
|
2020-05-22 07:30:07 -06:00
|
|
|
from synapse.http.server import JsonResource, OptionsResource
|
2020-04-03 05:29:30 -06:00
|
|
|
from synapse.logging.context import LoggingContext
|
2020-02-25 09:56:55 -07:00
|
|
|
from synapse.metrics import METRICS_PREFIX, MetricsResource, RegistryProxy
|
2020-05-18 05:24:48 -06:00
|
|
|
from synapse.replication.http import REPLICATION_PREFIX, ReplicationRestResource
|
2022-12-02 11:10:05 -07:00
|
|
|
from synapse.rest import ClientRestResource
|
2020-02-25 09:56:55 -07:00
|
|
|
from synapse.rest.admin import register_servlets_for_media_repo
|
2020-08-07 07:21:24 -06:00
|
|
|
from synapse.rest.health import HealthResource
|
2022-10-20 09:32:47 -06:00
|
|
|
from synapse.rest.key.v2 import KeyResource
|
2021-02-01 08:47:59 -07:00
|
|
|
from synapse.rest.synapse.client import build_synapse_client_resource_tree
|
2021-11-01 09:10:16 -06:00
|
|
|
from synapse.rest.well_known import well_known_resource
|
2021-04-14 10:06:06 -06:00
|
|
|
from synapse.server import HomeServer
|
2022-07-21 11:56:45 -06:00
|
|
|
from synapse.storage.databases.main.account_data import AccountDataWorkerStore
|
|
|
|
from synapse.storage.databases.main.appservice import (
|
|
|
|
ApplicationServiceTransactionWorkerStore,
|
|
|
|
ApplicationServiceWorkerStore,
|
|
|
|
)
|
2020-08-05 14:38:57 -06:00
|
|
|
from synapse.storage.databases.main.censor_events import CensorEventsStore
|
2020-10-07 09:27:56 -06:00
|
|
|
from synapse.storage.databases.main.client_ips import ClientIpWorkerStore
|
2022-07-21 11:56:45 -06:00
|
|
|
from synapse.storage.databases.main.deviceinbox import DeviceInboxWorkerStore
|
2022-11-11 03:51:49 -07:00
|
|
|
from synapse.storage.databases.main.devices import DeviceWorkerStore
|
2022-07-21 11:56:45 -06:00
|
|
|
from synapse.storage.databases.main.directory import DirectoryWorkerStore
|
2021-01-11 11:01:27 -07:00
|
|
|
from synapse.storage.databases.main.e2e_room_keys import EndToEndRoomKeyStore
|
2022-11-11 03:51:49 -07: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 12:15:47 -06:00
|
|
|
from synapse.storage.databases.main.lock import LockStore
|
2020-08-05 14:38:57 -06:00
|
|
|
from synapse.storage.databases.main.media_repository import MediaRepositoryStore
|
2020-10-02 06:23:15 -06:00
|
|
|
from synapse.storage.databases.main.metrics import ServerMetricsStore
|
2020-08-05 14:38:57 -06:00
|
|
|
from synapse.storage.databases.main.monthly_active_users import (
|
2020-02-25 09:56:55 -07:00
|
|
|
MonthlyActiveUsersWorkerStore,
|
|
|
|
)
|
2021-04-23 05:21:55 -06:00
|
|
|
from synapse.storage.databases.main.presence import PresenceStore
|
2022-07-21 11:56:45 -06:00
|
|
|
from synapse.storage.databases.main.profile import ProfileWorkerStore
|
2022-11-11 03:51:49 -07:00
|
|
|
from synapse.storage.databases.main.push_rule import PushRulesWorkerStore
|
|
|
|
from synapse.storage.databases.main.pusher import PusherWorkerStore
|
2022-07-21 11:56:45 -06:00
|
|
|
from synapse.storage.databases.main.receipts import ReceiptsWorkerStore
|
|
|
|
from synapse.storage.databases.main.registration import RegistrationWorkerStore
|
2022-11-11 03:51:49 -07:00
|
|
|
from synapse.storage.databases.main.relations import RelationsWorkerStore
|
2021-08-17 07:02:50 -06:00
|
|
|
from synapse.storage.databases.main.room import RoomWorkerStore
|
2022-11-11 03:51:49 -07:00
|
|
|
from synapse.storage.databases.main.roommember import RoomMemberWorkerStore
|
2021-05-27 15:45:43 -06:00
|
|
|
from synapse.storage.databases.main.search import SearchStore
|
2021-08-24 06:14:03 -06:00
|
|
|
from synapse.storage.databases.main.session import SessionStore
|
2022-11-11 03:51:49 -07:00
|
|
|
from synapse.storage.databases.main.signatures import SignatureWorkerStore
|
|
|
|
from synapse.storage.databases.main.state import StateGroupWorkerStore
|
2020-10-02 06:23:15 -06:00
|
|
|
from synapse.storage.databases.main.stats import StatsStore
|
2022-11-11 03:51:49 -07:00
|
|
|
from synapse.storage.databases.main.stream import StreamWorkerStore
|
2022-07-21 11:56:45 -06:00
|
|
|
from synapse.storage.databases.main.tags import TagsWorkerStore
|
2020-10-07 09:27:56 -06:00
|
|
|
from synapse.storage.databases.main.transactions import TransactionWorkerStore
|
2020-08-05 14:38:57 -06:00
|
|
|
from synapse.storage.databases.main.ui_auth import UIAuthWorkerStore
|
|
|
|
from synapse.storage.databases.main.user_directory import UserDirectoryStore
|
2022-11-11 03:51:49 -07:00
|
|
|
from synapse.storage.databases.main.user_erasure_store import UserErasureWorkerStore
|
2022-06-07 08:24:11 -06:00
|
|
|
from synapse.util import SYNAPSE_VERSION
|
2020-02-25 09:56:55 -07:00
|
|
|
from synapse.util.httpresourcetree import create_resource_tree
|
|
|
|
|
|
|
|
logger = logging.getLogger("synapse.app.generic_worker")
|
|
|
|
|
|
|
|
|
2023-05-16 13:56:38 -06:00
|
|
|
class GenericWorkerStore(
|
2020-02-25 09:56:55 -07:00
|
|
|
# FIXME(#3714): We need to add UserDirectoryStore as we write directly
|
|
|
|
# rather than going via the correct worker.
|
|
|
|
UserDirectoryStore,
|
2020-10-02 06:23:15 -06:00
|
|
|
StatsStore,
|
2020-04-30 11:47:49 -06:00
|
|
|
UIAuthWorkerStore,
|
2021-01-11 11:01:27 -07:00
|
|
|
EndToEndRoomKeyStore,
|
2021-04-23 05:21:55 -06:00
|
|
|
PresenceStore,
|
2022-07-21 11:56:45 -06:00
|
|
|
DeviceInboxWorkerStore,
|
2022-11-11 03:51:49 -07:00
|
|
|
DeviceWorkerStore,
|
2022-07-21 11:56:45 -06:00
|
|
|
TagsWorkerStore,
|
|
|
|
AccountDataWorkerStore,
|
2020-05-13 10:15:40 -06:00
|
|
|
CensorEventsStore,
|
2020-10-07 09:27:56 -06:00
|
|
|
ClientIpWorkerStore,
|
2022-11-11 03:51:49 -07: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 07:02:50 -06:00
|
|
|
RoomWorkerStore,
|
2022-07-21 11:56:45 -06:00
|
|
|
DirectoryWorkerStore,
|
2022-11-11 03:51:49 -07:00
|
|
|
PushRulesWorkerStore,
|
2022-07-21 11:56:45 -06:00
|
|
|
ApplicationServiceTransactionWorkerStore,
|
|
|
|
ApplicationServiceWorkerStore,
|
|
|
|
ProfileWorkerStore,
|
2022-11-11 03:51:49 -07:00
|
|
|
FilteringWorkerStore,
|
2020-02-25 09:56:55 -07:00
|
|
|
MonthlyActiveUsersWorkerStore,
|
|
|
|
MediaRepositoryStore,
|
2020-10-02 06:23:15 -06:00
|
|
|
ServerMetricsStore,
|
2022-11-11 03:51:49 -07:00
|
|
|
PusherWorkerStore,
|
|
|
|
RoomMemberWorkerStore,
|
|
|
|
RelationsWorkerStore,
|
|
|
|
EventFederationWorkerStore,
|
|
|
|
EventPushActionsWorkerStore,
|
|
|
|
StateGroupWorkerStore,
|
|
|
|
SignatureWorkerStore,
|
|
|
|
UserErasureWorkerStore,
|
2022-07-21 11:56:45 -06:00
|
|
|
ReceiptsWorkerStore,
|
2022-11-11 03:51:49 -07:00
|
|
|
StreamWorkerStore,
|
|
|
|
EventsWorkerStore,
|
2022-07-21 11:56:45 -06:00
|
|
|
RegistrationWorkerStore,
|
2021-05-27 15:45:43 -06:00
|
|
|
SearchStore,
|
2020-10-07 09:27:56 -06:00
|
|
|
TransactionWorkerStore,
|
2021-06-29 12:15:47 -06:00
|
|
|
LockStore,
|
2021-08-24 06:14:03 -06:00
|
|
|
SessionStore,
|
2020-02-25 09:56:55 -07:00
|
|
|
):
|
2021-11-10 13:06:54 -07:00
|
|
|
# Properties that multiple storage classes define. Tell mypy what the
|
|
|
|
# expected type is.
|
|
|
|
server_name: str
|
|
|
|
config: HomeServerConfig
|
2020-02-25 09:56:55 -07:00
|
|
|
|
|
|
|
|
|
|
|
class GenericWorkerServer(HomeServer):
|
2023-05-16 13:56:38 -06:00
|
|
|
DATASTORE_CLASS = GenericWorkerStore # type: ignore
|
2020-02-25 09:56:55 -07:00
|
|
|
|
2021-11-10 13:06:54 -07:00
|
|
|
def _listen_http(self, listener_config: ListenerConfig) -> None:
|
2020-06-16 05:44:07 -06:00
|
|
|
assert listener_config.http_options is not None
|
|
|
|
|
2020-08-07 07:21:24 -06:00
|
|
|
# We always include a health resource.
|
2021-11-10 13:06:54 -07:00
|
|
|
resources: Dict[str, Resource] = {"/health": HealthResource()}
|
2020-08-07 07:21:24 -06:00
|
|
|
|
2020-06-16 05:44:07 -06:00
|
|
|
for res in listener_config.http_options.resources:
|
|
|
|
for name in res.names:
|
2020-02-25 09:56:55 -07:00
|
|
|
if name == "metrics":
|
|
|
|
resources[METRICS_PREFIX] = MetricsResource(RegistryProxy)
|
|
|
|
elif name == "client":
|
2022-12-02 11:10:05 -07:00
|
|
|
resource: Resource = ClientRestResource(self)
|
2020-02-25 09:56:55 -07:00
|
|
|
|
2022-10-20 09:32:47 -06:00
|
|
|
resources[CLIENT_API_PREFIX] = resource
|
2021-02-01 08:47:59 -07:00
|
|
|
|
|
|
|
resources.update(build_synapse_client_resource_tree(self))
|
2022-10-20 09:32:47 -06:00
|
|
|
resources["/.well-known"] = well_known_resource(self)
|
2021-11-01 09:10:16 -06:00
|
|
|
|
2020-02-25 09:56:55 -07:00
|
|
|
elif name == "federation":
|
2022-10-20 09:32:47 -06:00
|
|
|
resources[FEDERATION_PREFIX] = TransportLayerServer(self)
|
2020-02-25 09:56:55 -07:00
|
|
|
elif name == "media":
|
2021-09-24 05:25:21 -06:00
|
|
|
if self.config.media.can_load_media_repo:
|
2020-02-27 09:33:21 -07: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 08:30:24 -07:00
|
|
|
MEDIA_R0_PREFIX: media_repo,
|
|
|
|
MEDIA_V3_PREFIX: media_repo,
|
2020-02-27 09:33:21 -07: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 05:00:38 -07:00
|
|
|
elif name == "health":
|
|
|
|
# Skip loading, health resource is always included
|
|
|
|
continue
|
2020-02-25 09:56:55 -07:00
|
|
|
|
2020-06-16 05:44:07 -06:00
|
|
|
if name == "openid" and "federation" not in res.names:
|
2020-02-25 09:56:55 -07:00
|
|
|
# Only load the openid resource separately if federation resource
|
|
|
|
# is not specified since federation resource includes openid
|
|
|
|
# resource.
|
2022-10-20 09:32:47 -06:00
|
|
|
resources[FEDERATION_PREFIX] = TransportLayerServer(
|
|
|
|
self, servlet_groups=["openid"]
|
2020-02-25 09:56:55 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
if name in ["keys", "federation"]:
|
2022-10-20 09:32:47 -06:00
|
|
|
resources[SERVER_KEY_PREFIX] = KeyResource(self)
|
2020-02-25 09:56:55 -07:00
|
|
|
|
2020-05-18 05:24:48 -06:00
|
|
|
if name == "replication":
|
|
|
|
resources[REPLICATION_PREFIX] = ReplicationRestResource(self)
|
|
|
|
|
2021-06-18 05:15:52 -06:00
|
|
|
# Attach additional resources registered by modules.
|
|
|
|
resources.update(self._module_web_resources)
|
|
|
|
self._module_web_resources_consumed = True
|
|
|
|
|
2020-05-22 07:30:07 -06:00
|
|
|
root_resource = create_resource_tree(resources, OptionsResource())
|
2020-02-25 09:56:55 -07:00
|
|
|
|
2022-11-15 05:55:00 -07: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 09:56:55 -07:00
|
|
|
reactor=self.get_reactor(),
|
|
|
|
)
|
|
|
|
|
2021-11-10 13:06:54 -07:00
|
|
|
def start_listening(self) -> None:
|
2021-09-24 05:25:21 -06:00
|
|
|
for listener in self.config.worker.worker_listeners:
|
2020-06-16 05:44:07 -06:00
|
|
|
if listener.type == "http":
|
2020-02-25 09:56:55 -07:00
|
|
|
self._listen_http(listener)
|
2020-06-16 05:44:07 -06:00
|
|
|
elif listener.type == "manhole":
|
2023-04-03 03:27:51 -06: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 05:44:07 -06:00
|
|
|
elif listener.type == "metrics":
|
2021-09-23 10:03:01 -06:00
|
|
|
if not self.config.metrics.enable_metrics:
|
2020-02-25 09:56:55 -07:00
|
|
|
logger.warning(
|
2021-07-19 08:28:05 -06:00
|
|
|
"Metrics listener configured, but "
|
|
|
|
"enable_metrics is not True!"
|
2020-02-25 09:56:55 -07:00
|
|
|
)
|
|
|
|
else:
|
2023-04-03 03:27:51 -06: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 09:56:55 -07:00
|
|
|
else:
|
2020-06-16 05:44:07 -06:00
|
|
|
logger.warning("Unsupported listener type: %s", listener.type)
|
2020-02-25 09:56:55 -07:00
|
|
|
|
2022-03-10 06:01:56 -07:00
|
|
|
self.get_replication_command_handler().start_replication(self)
|
2020-02-25 09:56:55 -07:00
|
|
|
|
|
|
|
|
2021-11-10 13:06:54 -07:00
|
|
|
def start(config_options: List[str]) -> None:
|
2020-02-25 09:56:55 -07: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 11:07:12 -06:00
|
|
|
assert config.worker.worker_app in (
|
2020-02-25 09:56:55 -07: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 04:44:15 -06:00
|
|
|
synapse.events.USE_FROZEN_DICTS = config.server.use_frozen_dicts
|
2021-05-05 09:54:36 -06:00
|
|
|
synapse.util.caches.TRACK_MEMORY_USAGE = config.caches.track_memory_usage
|
2020-02-25 09:56:55 -07:00
|
|
|
|
2021-05-05 09:53:45 -06:00
|
|
|
if config.server.gc_seconds:
|
|
|
|
synapse.metrics.MIN_TIME_BETWEEN_GCS = config.server.gc_seconds
|
|
|
|
|
2020-04-28 06:34:12 -06:00
|
|
|
hs = GenericWorkerServer(
|
2021-09-13 11:07:12 -06:00
|
|
|
config.server.server_name,
|
2020-02-25 09:56:55 -07:00
|
|
|
config=config,
|
2022-06-07 08:24:11 -06:00
|
|
|
version_string=f"Synapse/{SYNAPSE_VERSION}",
|
2020-02-25 09:56:55 -07:00
|
|
|
)
|
|
|
|
|
2020-04-28 06:34:12 -06:00
|
|
|
setup_logging(hs, config, use_worker_options=True)
|
|
|
|
|
2021-06-21 04:41:25 -06:00
|
|
|
try:
|
|
|
|
hs.setup()
|
2020-04-28 06:34:12 -06:00
|
|
|
|
2021-06-21 04:41:25 -06: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 09:56:55 -07:00
|
|
|
|
2021-04-23 12:20:44 -06:00
|
|
|
register_start(_base.start, hs)
|
2020-02-25 09:56:55 -07:00
|
|
|
|
2021-06-21 04:41:25 -06:00
|
|
|
# redirect stdio to the logs, if configured.
|
2021-09-23 10:03:01 -06:00
|
|
|
if not hs.config.logging.no_redirect_stdio:
|
2021-06-21 04:41:25 -06:00
|
|
|
redirect_stdio_to_logs()
|
|
|
|
|
2020-02-25 09:56:55 -07:00
|
|
|
_base.start_worker_reactor("synapse-generic-worker", config)
|
|
|
|
|
|
|
|
|
2021-11-30 09:52:45 -07:00
|
|
|
def main() -> None:
|
2020-02-25 09:56:55 -07:00
|
|
|
with LoggingContext("main"):
|
|
|
|
start(sys.argv[1:])
|
2021-11-30 09:52:45 -07:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|