2020-02-25 11:56:55 -05:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# 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 contextlib
|
|
|
|
import logging
|
|
|
|
import sys
|
2020-05-26 06:41:38 -04:00
|
|
|
from typing import Dict, Iterable, Optional, Set
|
2020-04-22 17:39:04 -04:00
|
|
|
|
|
|
|
from typing_extensions import ContextManager
|
2020-02-25 11:56:55 -05:00
|
|
|
|
2020-07-17 07:08:56 -04:00
|
|
|
from twisted.internet import address, reactor
|
2020-02-25 11:56:55 -05:00
|
|
|
|
|
|
|
import synapse
|
|
|
|
import synapse.events
|
2020-05-22 06:39:20 -04:00
|
|
|
from synapse.api.errors import HttpResponseException, RequestSendFailed, SynapseError
|
2020-02-25 11:56:55 -05:00
|
|
|
from synapse.api.urls import (
|
|
|
|
CLIENT_API_PREFIX,
|
|
|
|
FEDERATION_PREFIX,
|
|
|
|
LEGACY_MEDIA_PREFIX,
|
|
|
|
MEDIA_PREFIX,
|
|
|
|
SERVER_KEY_V2_PREFIX,
|
|
|
|
)
|
|
|
|
from synapse.app import _base
|
|
|
|
from synapse.config._base import ConfigError
|
|
|
|
from synapse.config.homeserver import HomeServerConfig
|
|
|
|
from synapse.config.logger import setup_logging
|
2020-06-16 07:44:07 -04:00
|
|
|
from synapse.config.server import ListenerConfig
|
2020-02-25 11:56:55 -05:00
|
|
|
from synapse.federation import send_queue
|
|
|
|
from synapse.federation.transport.server import TransportLayerServer
|
2020-05-22 11:11:35 -04:00
|
|
|
from synapse.handlers.presence import (
|
|
|
|
BasePresenceHandler,
|
|
|
|
PresenceState,
|
|
|
|
get_interested_parties,
|
|
|
|
)
|
2020-05-22 09:30:07 -04:00
|
|
|
from synapse.http.server import JsonResource, OptionsResource
|
2020-02-25 11:56:55 -05:00
|
|
|
from synapse.http.servlet import RestServlet, parse_json_object_from_request
|
|
|
|
from synapse.http.site import SynapseSite
|
2020-04-03 07:29:30 -04:00
|
|
|
from synapse.logging.context import LoggingContext
|
2020-02-25 11:56:55 -05:00
|
|
|
from synapse.metrics import METRICS_PREFIX, MetricsResource, RegistryProxy
|
|
|
|
from synapse.metrics.background_process_metrics import run_as_background_process
|
2020-05-18 07:24:48 -04:00
|
|
|
from synapse.replication.http import REPLICATION_PREFIX, ReplicationRestResource
|
2020-05-22 11:11:35 -04:00
|
|
|
from synapse.replication.http.presence import (
|
|
|
|
ReplicationBumpPresenceActiveTime,
|
|
|
|
ReplicationPresenceSetState,
|
|
|
|
)
|
2020-04-22 17:39:04 -04:00
|
|
|
from synapse.replication.slave.storage._base import BaseSlavedStore
|
2020-02-25 11:56:55 -05:00
|
|
|
from synapse.replication.slave.storage.account_data import SlavedAccountDataStore
|
|
|
|
from synapse.replication.slave.storage.appservice import SlavedApplicationServiceStore
|
|
|
|
from synapse.replication.slave.storage.client_ips import SlavedClientIpStore
|
|
|
|
from synapse.replication.slave.storage.deviceinbox import SlavedDeviceInboxStore
|
|
|
|
from synapse.replication.slave.storage.devices import SlavedDeviceStore
|
|
|
|
from synapse.replication.slave.storage.directory import DirectoryStore
|
|
|
|
from synapse.replication.slave.storage.events import SlavedEventStore
|
|
|
|
from synapse.replication.slave.storage.filtering import SlavedFilteringStore
|
|
|
|
from synapse.replication.slave.storage.groups import SlavedGroupServerStore
|
|
|
|
from synapse.replication.slave.storage.keys import SlavedKeyStore
|
|
|
|
from synapse.replication.slave.storage.presence import SlavedPresenceStore
|
|
|
|
from synapse.replication.slave.storage.profile import SlavedProfileStore
|
|
|
|
from synapse.replication.slave.storage.push_rule import SlavedPushRuleStore
|
|
|
|
from synapse.replication.slave.storage.pushers import SlavedPusherStore
|
|
|
|
from synapse.replication.slave.storage.receipts import SlavedReceiptsStore
|
|
|
|
from synapse.replication.slave.storage.registration import SlavedRegistrationStore
|
|
|
|
from synapse.replication.slave.storage.room import RoomStore
|
|
|
|
from synapse.replication.slave.storage.transactions import SlavedTransactionStore
|
2020-04-06 04:58:42 -04:00
|
|
|
from synapse.replication.tcp.client import ReplicationDataHandler
|
2020-03-30 11:37:24 -04:00
|
|
|
from synapse.replication.tcp.commands import ClearUserSyncsCommand
|
2020-03-23 07:39:36 -04:00
|
|
|
from synapse.replication.tcp.streams import (
|
|
|
|
AccountDataStream,
|
2020-02-25 11:56:55 -05:00
|
|
|
DeviceListsStream,
|
2020-03-23 07:39:36 -04:00
|
|
|
GroupServerStream,
|
|
|
|
PresenceStream,
|
|
|
|
PushersStream,
|
|
|
|
PushRulesStream,
|
2020-02-25 11:56:55 -05:00
|
|
|
ReceiptsStream,
|
2020-03-23 07:39:36 -04:00
|
|
|
TagAccountDataStream,
|
2020-02-25 11:56:55 -05:00
|
|
|
ToDeviceStream,
|
2020-03-23 07:39:36 -04:00
|
|
|
)
|
2020-02-25 11:56:55 -05:00
|
|
|
from synapse.rest.admin import register_servlets_for_media_repo
|
|
|
|
from synapse.rest.client.v1 import events
|
|
|
|
from synapse.rest.client.v1.initial_sync import InitialSyncRestServlet
|
|
|
|
from synapse.rest.client.v1.login import LoginRestServlet
|
|
|
|
from synapse.rest.client.v1.profile import (
|
|
|
|
ProfileAvatarURLRestServlet,
|
|
|
|
ProfileDisplaynameRestServlet,
|
|
|
|
ProfileRestServlet,
|
|
|
|
)
|
|
|
|
from synapse.rest.client.v1.push_rule import PushRuleRestServlet
|
|
|
|
from synapse.rest.client.v1.room import (
|
|
|
|
JoinedRoomMemberListRestServlet,
|
|
|
|
JoinRoomAliasServlet,
|
|
|
|
PublicRoomListRestServlet,
|
|
|
|
RoomEventContextServlet,
|
|
|
|
RoomInitialSyncRestServlet,
|
|
|
|
RoomMemberListRestServlet,
|
|
|
|
RoomMembershipRestServlet,
|
|
|
|
RoomMessageListRestServlet,
|
|
|
|
RoomSendEventRestServlet,
|
|
|
|
RoomStateEventRestServlet,
|
|
|
|
RoomStateRestServlet,
|
2020-07-16 10:12:54 -04:00
|
|
|
RoomTypingRestServlet,
|
2020-02-25 11:56:55 -05:00
|
|
|
)
|
|
|
|
from synapse.rest.client.v1.voip import VoipRestServlet
|
|
|
|
from synapse.rest.client.v2_alpha import groups, sync, user_directory
|
|
|
|
from synapse.rest.client.v2_alpha._base import client_patterns
|
|
|
|
from synapse.rest.client.v2_alpha.account import ThreepidRestServlet
|
2020-04-21 05:46:30 -04:00
|
|
|
from synapse.rest.client.v2_alpha.account_data import (
|
|
|
|
AccountDataServlet,
|
|
|
|
RoomAccountDataServlet,
|
|
|
|
)
|
2020-02-25 11:56:55 -05:00
|
|
|
from synapse.rest.client.v2_alpha.keys import KeyChangesServlet, KeyQueryServlet
|
|
|
|
from synapse.rest.client.v2_alpha.register import RegisterRestServlet
|
|
|
|
from synapse.rest.client.versions import VersionsRestServlet
|
2020-08-07 09:21:24 -04:00
|
|
|
from synapse.rest.health import HealthResource
|
2020-02-25 11:56:55 -05:00
|
|
|
from synapse.rest.key.v2 import KeyApiV2Resource
|
2020-08-11 13:00:17 -04:00
|
|
|
from synapse.server import HomeServer, cache_in_self
|
2020-08-05 16:38:57 -04:00
|
|
|
from synapse.storage.databases.main.censor_events import CensorEventsStore
|
|
|
|
from synapse.storage.databases.main.media_repository import MediaRepositoryStore
|
|
|
|
from synapse.storage.databases.main.monthly_active_users import (
|
2020-02-25 11:56:55 -05:00
|
|
|
MonthlyActiveUsersWorkerStore,
|
|
|
|
)
|
2020-08-05 16:38:57 -04:00
|
|
|
from synapse.storage.databases.main.presence import UserPresenceState
|
|
|
|
from synapse.storage.databases.main.search import SearchWorkerStore
|
|
|
|
from synapse.storage.databases.main.ui_auth import UIAuthWorkerStore
|
|
|
|
from synapse.storage.databases.main.user_directory import UserDirectoryStore
|
2020-02-25 11:56:55 -05:00
|
|
|
from synapse.types import ReadReceipt
|
|
|
|
from synapse.util.async_helpers import Linearizer
|
|
|
|
from synapse.util.httpresourcetree import create_resource_tree
|
|
|
|
from synapse.util.manhole import manhole
|
|
|
|
from synapse.util.versionstring import get_version_string
|
|
|
|
|
|
|
|
logger = logging.getLogger("synapse.app.generic_worker")
|
|
|
|
|
|
|
|
|
|
|
|
class PresenceStatusStubServlet(RestServlet):
|
|
|
|
"""If presence is disabled this servlet can be used to stub out setting
|
2020-05-21 09:36:46 -04:00
|
|
|
presence status.
|
2020-02-25 11:56:55 -05:00
|
|
|
"""
|
|
|
|
|
|
|
|
PATTERNS = client_patterns("/presence/(?P<user_id>[^/]*)/status")
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
super(PresenceStatusStubServlet, self).__init__()
|
|
|
|
self.auth = hs.get_auth()
|
|
|
|
|
|
|
|
async def on_GET(self, request, user_id):
|
2020-05-21 09:36:46 -04:00
|
|
|
await self.auth.get_user_by_req(request)
|
|
|
|
return 200, {"presence": "offline"}
|
2020-02-25 11:56:55 -05:00
|
|
|
|
|
|
|
async def on_PUT(self, request, user_id):
|
|
|
|
await self.auth.get_user_by_req(request)
|
|
|
|
return 200, {}
|
|
|
|
|
|
|
|
|
|
|
|
class KeyUploadServlet(RestServlet):
|
|
|
|
"""An implementation of the `KeyUploadServlet` that responds to read only
|
|
|
|
requests, but otherwise proxies through to the master instance.
|
|
|
|
"""
|
|
|
|
|
|
|
|
PATTERNS = client_patterns("/keys/upload(/(?P<device_id>[^/]+))?$")
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
hs (synapse.server.HomeServer): server
|
|
|
|
"""
|
|
|
|
super(KeyUploadServlet, self).__init__()
|
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.store = hs.get_datastore()
|
|
|
|
self.http_client = hs.get_simple_http_client()
|
|
|
|
self.main_uri = hs.config.worker_main_http_uri
|
|
|
|
|
|
|
|
async def on_POST(self, request, device_id):
|
|
|
|
requester = await self.auth.get_user_by_req(request, allow_guest=True)
|
|
|
|
user_id = requester.user.to_string()
|
|
|
|
body = parse_json_object_from_request(request)
|
|
|
|
|
|
|
|
if device_id is not None:
|
|
|
|
# passing the device_id here is deprecated; however, we allow it
|
|
|
|
# for now for compatibility with older clients.
|
|
|
|
if requester.device_id is not None and device_id != requester.device_id:
|
|
|
|
logger.warning(
|
|
|
|
"Client uploading keys for a different device "
|
|
|
|
"(logged in as %s, uploading for %s)",
|
|
|
|
requester.device_id,
|
|
|
|
device_id,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
device_id = requester.device_id
|
|
|
|
|
|
|
|
if device_id is None:
|
|
|
|
raise SynapseError(
|
|
|
|
400, "To upload keys, you must pass device_id when authenticating"
|
|
|
|
)
|
|
|
|
|
|
|
|
if body:
|
|
|
|
# They're actually trying to upload something, proxy to main synapse.
|
2020-07-09 07:34:46 -04:00
|
|
|
|
|
|
|
# Proxy headers from the original request, such as the auth headers
|
|
|
|
# (in case the access token is there) and the original IP /
|
|
|
|
# User-Agent of the request.
|
|
|
|
headers = {
|
|
|
|
header: request.requestHeaders.getRawHeaders(header, [])
|
|
|
|
for header in (b"Authorization", b"User-Agent")
|
|
|
|
}
|
|
|
|
# Add the previous hop the the X-Forwarded-For header.
|
|
|
|
x_forwarded_for = request.requestHeaders.getRawHeaders(
|
|
|
|
b"X-Forwarded-For", []
|
|
|
|
)
|
|
|
|
if isinstance(request.client, (address.IPv4Address, address.IPv6Address)):
|
|
|
|
previous_host = request.client.host.encode("ascii")
|
|
|
|
# If the header exists, add to the comma-separated list of the first
|
|
|
|
# instance of the header. Otherwise, generate a new header.
|
|
|
|
if x_forwarded_for:
|
|
|
|
x_forwarded_for = [
|
|
|
|
x_forwarded_for[0] + b", " + previous_host
|
|
|
|
] + x_forwarded_for[1:]
|
|
|
|
else:
|
|
|
|
x_forwarded_for = [previous_host]
|
|
|
|
headers[b"X-Forwarded-For"] = x_forwarded_for
|
|
|
|
|
2020-05-22 06:39:20 -04:00
|
|
|
try:
|
|
|
|
result = await self.http_client.post_json_get_json(
|
|
|
|
self.main_uri + request.uri.decode("ascii"), body, headers=headers
|
|
|
|
)
|
|
|
|
except HttpResponseException as e:
|
2020-05-26 06:42:22 -04:00
|
|
|
raise e.to_synapse_error() from e
|
2020-05-22 06:39:20 -04:00
|
|
|
except RequestSendFailed as e:
|
|
|
|
raise SynapseError(502, "Failed to talk to master") from e
|
2020-02-25 11:56:55 -05:00
|
|
|
|
|
|
|
return 200, result
|
|
|
|
else:
|
|
|
|
# Just interested in counts.
|
|
|
|
result = await self.store.count_e2e_one_time_keys(user_id, device_id)
|
|
|
|
return 200, {"one_time_key_counts": result}
|
|
|
|
|
|
|
|
|
2020-04-22 17:39:04 -04:00
|
|
|
class _NullContextManager(ContextManager[None]):
|
|
|
|
"""A context manager which does nothing."""
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-02-25 11:56:55 -05:00
|
|
|
UPDATE_SYNCING_USERS_MS = 10 * 1000
|
|
|
|
|
|
|
|
|
2020-04-22 17:39:04 -04:00
|
|
|
class GenericWorkerPresence(BasePresenceHandler):
|
2020-02-25 11:56:55 -05:00
|
|
|
def __init__(self, hs):
|
2020-04-22 17:39:04 -04:00
|
|
|
super().__init__(hs)
|
2020-02-25 11:56:55 -05:00
|
|
|
self.hs = hs
|
|
|
|
self.is_mine_id = hs.is_mine_id
|
|
|
|
self.http_client = hs.get_simple_http_client()
|
2020-04-22 17:39:04 -04:00
|
|
|
|
|
|
|
self._presence_enabled = hs.config.use_presence
|
|
|
|
|
|
|
|
# The number of ongoing syncs on this process, by user id.
|
|
|
|
# Empty if _presence_enabled is false.
|
|
|
|
self._user_to_num_current_syncs = {} # type: Dict[str, int]
|
|
|
|
|
2020-02-25 11:56:55 -05:00
|
|
|
self.notifier = hs.get_notifier()
|
2020-03-30 11:37:24 -04:00
|
|
|
self.instance_id = hs.get_instance_id()
|
2020-02-25 11:56:55 -05:00
|
|
|
|
|
|
|
# user_id -> last_sync_ms. Lists the users that have stopped syncing
|
|
|
|
# but we haven't notified the master of that yet
|
|
|
|
self.users_going_offline = {}
|
|
|
|
|
2020-05-22 11:11:35 -04:00
|
|
|
self._bump_active_client = ReplicationBumpPresenceActiveTime.make_client(hs)
|
|
|
|
self._set_state_client = ReplicationPresenceSetState.make_client(hs)
|
|
|
|
|
2020-02-25 11:56:55 -05:00
|
|
|
self._send_stop_syncing_loop = self.clock.looping_call(
|
|
|
|
self.send_stop_syncing, UPDATE_SYNCING_USERS_MS
|
|
|
|
)
|
|
|
|
|
2020-03-30 11:37:24 -04:00
|
|
|
hs.get_reactor().addSystemEventTrigger(
|
|
|
|
"before",
|
|
|
|
"shutdown",
|
|
|
|
run_as_background_process,
|
|
|
|
"generic_presence.on_shutdown",
|
|
|
|
self._on_shutdown,
|
|
|
|
)
|
|
|
|
|
|
|
|
def _on_shutdown(self):
|
2020-04-22 17:39:04 -04:00
|
|
|
if self._presence_enabled:
|
2020-03-30 11:37:24 -04:00
|
|
|
self.hs.get_tcp_replication().send_command(
|
|
|
|
ClearUserSyncsCommand(self.instance_id)
|
|
|
|
)
|
2020-02-25 11:56:55 -05:00
|
|
|
|
|
|
|
def send_user_sync(self, user_id, is_syncing, last_sync_ms):
|
2020-04-22 17:39:04 -04:00
|
|
|
if self._presence_enabled:
|
2020-02-25 11:56:55 -05:00
|
|
|
self.hs.get_tcp_replication().send_user_sync(
|
2020-03-30 11:37:24 -04:00
|
|
|
self.instance_id, user_id, is_syncing, last_sync_ms
|
2020-02-25 11:56:55 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
def mark_as_coming_online(self, user_id):
|
|
|
|
"""A user has started syncing. Send a UserSync to the master, unless they
|
|
|
|
had recently stopped syncing.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str)
|
|
|
|
"""
|
|
|
|
going_offline = self.users_going_offline.pop(user_id, None)
|
|
|
|
if not going_offline:
|
|
|
|
# Safe to skip because we haven't yet told the master they were offline
|
|
|
|
self.send_user_sync(user_id, True, self.clock.time_msec())
|
|
|
|
|
|
|
|
def mark_as_going_offline(self, user_id):
|
|
|
|
"""A user has stopped syncing. We wait before notifying the master as
|
|
|
|
its likely they'll come back soon. This allows us to avoid sending
|
|
|
|
a stopped syncing immediately followed by a started syncing notification
|
|
|
|
to the master
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str)
|
|
|
|
"""
|
|
|
|
self.users_going_offline[user_id] = self.clock.time_msec()
|
|
|
|
|
|
|
|
def send_stop_syncing(self):
|
|
|
|
"""Check if there are any users who have stopped syncing a while ago
|
|
|
|
and haven't come back yet. If there are poke the master about them.
|
|
|
|
"""
|
|
|
|
now = self.clock.time_msec()
|
|
|
|
for user_id, last_sync_ms in list(self.users_going_offline.items()):
|
|
|
|
if now - last_sync_ms > UPDATE_SYNCING_USERS_MS:
|
|
|
|
self.users_going_offline.pop(user_id, None)
|
|
|
|
self.send_user_sync(user_id, False, last_sync_ms)
|
|
|
|
|
2020-04-22 17:39:04 -04:00
|
|
|
async def user_syncing(
|
|
|
|
self, user_id: str, affect_presence: bool
|
|
|
|
) -> ContextManager[None]:
|
|
|
|
"""Record that a user is syncing.
|
|
|
|
|
|
|
|
Called by the sync and events servlets to record that a user has connected to
|
|
|
|
this worker and is waiting for some events.
|
|
|
|
"""
|
|
|
|
if not affect_presence or not self._presence_enabled:
|
|
|
|
return _NullContextManager()
|
2020-02-25 11:56:55 -05:00
|
|
|
|
2020-04-22 17:39:04 -04:00
|
|
|
curr_sync = self._user_to_num_current_syncs.get(user_id, 0)
|
|
|
|
self._user_to_num_current_syncs[user_id] = curr_sync + 1
|
2020-02-25 11:56:55 -05:00
|
|
|
|
2020-04-22 17:39:04 -04:00
|
|
|
# If we went from no in flight sync to some, notify replication
|
|
|
|
if self._user_to_num_current_syncs[user_id] == 1:
|
|
|
|
self.mark_as_coming_online(user_id)
|
2020-02-25 11:56:55 -05:00
|
|
|
|
|
|
|
def _end():
|
|
|
|
# We check that the user_id is in user_to_num_current_syncs because
|
|
|
|
# user_to_num_current_syncs may have been cleared if we are
|
|
|
|
# shutting down.
|
2020-04-22 17:39:04 -04:00
|
|
|
if user_id in self._user_to_num_current_syncs:
|
|
|
|
self._user_to_num_current_syncs[user_id] -= 1
|
2020-02-25 11:56:55 -05:00
|
|
|
|
|
|
|
# If we went from one in flight sync to non, notify replication
|
2020-04-22 17:39:04 -04:00
|
|
|
if self._user_to_num_current_syncs[user_id] == 0:
|
2020-02-25 11:56:55 -05:00
|
|
|
self.mark_as_going_offline(user_id)
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def _user_syncing():
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
_end()
|
|
|
|
|
2020-04-22 17:39:04 -04:00
|
|
|
return _user_syncing()
|
2020-02-25 11:56:55 -05:00
|
|
|
|
2020-07-17 07:08:56 -04:00
|
|
|
async def notify_from_replication(self, states, stream_id):
|
|
|
|
parties = await get_interested_parties(self.store, states)
|
2020-02-25 11:56:55 -05:00
|
|
|
room_ids_to_states, users_to_states = parties
|
|
|
|
|
|
|
|
self.notifier.on_new_event(
|
|
|
|
"presence_key",
|
|
|
|
stream_id,
|
|
|
|
rooms=room_ids_to_states.keys(),
|
|
|
|
users=users_to_states.keys(),
|
|
|
|
)
|
|
|
|
|
2020-07-17 07:08:56 -04:00
|
|
|
async def process_replication_rows(self, token, rows):
|
2020-02-25 11:56:55 -05:00
|
|
|
states = [
|
|
|
|
UserPresenceState(
|
|
|
|
row.user_id,
|
|
|
|
row.state,
|
|
|
|
row.last_active_ts,
|
|
|
|
row.last_federation_update_ts,
|
|
|
|
row.last_user_sync_ts,
|
|
|
|
row.status_msg,
|
|
|
|
row.currently_active,
|
|
|
|
)
|
|
|
|
for row in rows
|
|
|
|
]
|
|
|
|
|
|
|
|
for state in states:
|
|
|
|
self.user_to_current_state[state.user_id] = state
|
|
|
|
|
|
|
|
stream_id = token
|
2020-07-17 07:08:56 -04:00
|
|
|
await self.notify_from_replication(states, stream_id)
|
2020-02-25 11:56:55 -05:00
|
|
|
|
2020-04-22 17:39:04 -04:00
|
|
|
def get_currently_syncing_users_for_replication(self) -> Iterable[str]:
|
|
|
|
return [
|
|
|
|
user_id
|
|
|
|
for user_id, count in self._user_to_num_current_syncs.items()
|
|
|
|
if count > 0
|
|
|
|
]
|
2020-02-25 11:56:55 -05:00
|
|
|
|
2020-05-22 11:11:35 -04:00
|
|
|
async def set_state(self, target_user, state, ignore_status_msg=False):
|
|
|
|
"""Set the presence state of the user.
|
|
|
|
"""
|
|
|
|
presence = state["presence"]
|
|
|
|
|
|
|
|
valid_presence = (
|
|
|
|
PresenceState.ONLINE,
|
|
|
|
PresenceState.UNAVAILABLE,
|
|
|
|
PresenceState.OFFLINE,
|
|
|
|
)
|
|
|
|
if presence not in valid_presence:
|
|
|
|
raise SynapseError(400, "Invalid presence state")
|
|
|
|
|
|
|
|
user_id = target_user.to_string()
|
|
|
|
|
|
|
|
# If presence is disabled, no-op
|
|
|
|
if not self.hs.config.use_presence:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Proxy request to master
|
|
|
|
await self._set_state_client(
|
|
|
|
user_id=user_id, state=state, ignore_status_msg=ignore_status_msg
|
|
|
|
)
|
|
|
|
|
|
|
|
async def bump_presence_active_time(self, user):
|
|
|
|
"""We've seen the user do something that indicates they're interacting
|
|
|
|
with the app.
|
|
|
|
"""
|
|
|
|
# If presence is disabled, no-op
|
|
|
|
if not self.hs.config.use_presence:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Proxy request to master
|
|
|
|
user_id = user.to_string()
|
|
|
|
await self._bump_active_client(user_id=user_id)
|
|
|
|
|
2020-02-25 11:56:55 -05:00
|
|
|
|
|
|
|
class GenericWorkerSlavedStore(
|
|
|
|
# FIXME(#3714): We need to add UserDirectoryStore as we write directly
|
|
|
|
# rather than going via the correct worker.
|
|
|
|
UserDirectoryStore,
|
2020-04-30 13:47:49 -04:00
|
|
|
UIAuthWorkerStore,
|
2020-02-25 11:56:55 -05:00
|
|
|
SlavedDeviceInboxStore,
|
|
|
|
SlavedDeviceStore,
|
|
|
|
SlavedReceiptsStore,
|
|
|
|
SlavedPushRuleStore,
|
|
|
|
SlavedGroupServerStore,
|
|
|
|
SlavedAccountDataStore,
|
|
|
|
SlavedPusherStore,
|
2020-05-13 12:15:40 -04:00
|
|
|
CensorEventsStore,
|
2020-02-25 11:56:55 -05:00
|
|
|
SlavedEventStore,
|
|
|
|
SlavedKeyStore,
|
|
|
|
RoomStore,
|
|
|
|
DirectoryStore,
|
|
|
|
SlavedApplicationServiceStore,
|
|
|
|
SlavedRegistrationStore,
|
|
|
|
SlavedTransactionStore,
|
|
|
|
SlavedProfileStore,
|
|
|
|
SlavedClientIpStore,
|
|
|
|
SlavedPresenceStore,
|
|
|
|
SlavedFilteringStore,
|
|
|
|
MonthlyActiveUsersWorkerStore,
|
|
|
|
MediaRepositoryStore,
|
2020-05-15 12:22:47 -04:00
|
|
|
SearchWorkerStore,
|
2020-02-25 11:56:55 -05:00
|
|
|
BaseSlavedStore,
|
|
|
|
):
|
2020-07-10 13:26:36 -04:00
|
|
|
pass
|
2020-02-25 11:56:55 -05:00
|
|
|
|
|
|
|
|
|
|
|
class GenericWorkerServer(HomeServer):
|
|
|
|
DATASTORE_CLASS = GenericWorkerSlavedStore
|
|
|
|
|
2020-06-16 07:44:07 -04:00
|
|
|
def _listen_http(self, listener_config: ListenerConfig):
|
|
|
|
port = listener_config.port
|
|
|
|
bind_addresses = listener_config.bind_addresses
|
|
|
|
|
|
|
|
assert listener_config.http_options is not None
|
|
|
|
|
|
|
|
site_tag = listener_config.http_options.tag
|
|
|
|
if site_tag is None:
|
|
|
|
site_tag = port
|
2020-08-07 09:21:24 -04:00
|
|
|
|
|
|
|
# We always include a health resource.
|
|
|
|
resources = {"/health": HealthResource()}
|
|
|
|
|
2020-06-16 07:44:07 -04:00
|
|
|
for res in listener_config.http_options.resources:
|
|
|
|
for name in res.names:
|
2020-02-25 11:56:55 -05:00
|
|
|
if name == "metrics":
|
|
|
|
resources[METRICS_PREFIX] = MetricsResource(RegistryProxy)
|
|
|
|
elif name == "client":
|
|
|
|
resource = JsonResource(self, canonical_json=False)
|
|
|
|
|
|
|
|
PublicRoomListRestServlet(self).register(resource)
|
|
|
|
RoomMemberListRestServlet(self).register(resource)
|
|
|
|
JoinedRoomMemberListRestServlet(self).register(resource)
|
|
|
|
RoomStateRestServlet(self).register(resource)
|
|
|
|
RoomEventContextServlet(self).register(resource)
|
|
|
|
RoomMessageListRestServlet(self).register(resource)
|
|
|
|
RegisterRestServlet(self).register(resource)
|
|
|
|
LoginRestServlet(self).register(resource)
|
|
|
|
ThreepidRestServlet(self).register(resource)
|
|
|
|
KeyQueryServlet(self).register(resource)
|
|
|
|
KeyChangesServlet(self).register(resource)
|
|
|
|
VoipRestServlet(self).register(resource)
|
|
|
|
PushRuleRestServlet(self).register(resource)
|
|
|
|
VersionsRestServlet(self).register(resource)
|
|
|
|
RoomSendEventRestServlet(self).register(resource)
|
|
|
|
RoomMembershipRestServlet(self).register(resource)
|
|
|
|
RoomStateEventRestServlet(self).register(resource)
|
|
|
|
JoinRoomAliasServlet(self).register(resource)
|
|
|
|
ProfileAvatarURLRestServlet(self).register(resource)
|
|
|
|
ProfileDisplaynameRestServlet(self).register(resource)
|
|
|
|
ProfileRestServlet(self).register(resource)
|
|
|
|
KeyUploadServlet(self).register(resource)
|
2020-04-21 05:46:30 -04:00
|
|
|
AccountDataServlet(self).register(resource)
|
|
|
|
RoomAccountDataServlet(self).register(resource)
|
2020-07-16 10:12:54 -04:00
|
|
|
RoomTypingRestServlet(self).register(resource)
|
2020-02-25 11:56:55 -05:00
|
|
|
|
|
|
|
sync.register_servlets(self, resource)
|
|
|
|
events.register_servlets(self, resource)
|
|
|
|
InitialSyncRestServlet(self).register(resource)
|
|
|
|
RoomInitialSyncRestServlet(self).register(resource)
|
|
|
|
|
|
|
|
user_directory.register_servlets(self, resource)
|
|
|
|
|
|
|
|
# If presence is disabled, use the stub servlet that does
|
|
|
|
# not allow sending presence
|
|
|
|
if not self.config.use_presence:
|
|
|
|
PresenceStatusStubServlet(self).register(resource)
|
|
|
|
|
|
|
|
groups.register_servlets(self, resource)
|
|
|
|
|
|
|
|
resources.update({CLIENT_API_PREFIX: resource})
|
|
|
|
elif name == "federation":
|
|
|
|
resources.update({FEDERATION_PREFIX: TransportLayerServer(self)})
|
|
|
|
elif name == "media":
|
2020-02-27 11:33:21 -05:00
|
|
|
if self.config.can_load_media_repo:
|
|
|
|
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(
|
|
|
|
{
|
|
|
|
MEDIA_PREFIX: media_repo,
|
|
|
|
LEGACY_MEDIA_PREFIX: media_repo,
|
|
|
|
"/_synapse/admin": admin_resource,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
logger.warning(
|
|
|
|
"A 'media' listener is configured but the media"
|
|
|
|
" repository is disabled. Ignoring."
|
|
|
|
)
|
2020-02-25 11:56:55 -05:00
|
|
|
|
2020-06-16 07:44:07 -04:00
|
|
|
if name == "openid" and "federation" not in res.names:
|
2020-02-25 11:56:55 -05:00
|
|
|
# Only load the openid resource separately if federation resource
|
|
|
|
# is not specified since federation resource includes openid
|
|
|
|
# resource.
|
|
|
|
resources.update(
|
|
|
|
{
|
|
|
|
FEDERATION_PREFIX: TransportLayerServer(
|
|
|
|
self, servlet_groups=["openid"]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
if name in ["keys", "federation"]:
|
|
|
|
resources[SERVER_KEY_V2_PREFIX] = KeyApiV2Resource(self)
|
|
|
|
|
2020-05-18 07:24:48 -04:00
|
|
|
if name == "replication":
|
|
|
|
resources[REPLICATION_PREFIX] = ReplicationRestResource(self)
|
|
|
|
|
2020-05-22 09:30:07 -04:00
|
|
|
root_resource = create_resource_tree(resources, OptionsResource())
|
2020-02-25 11:56:55 -05:00
|
|
|
|
|
|
|
_base.listen_tcp(
|
|
|
|
bind_addresses,
|
|
|
|
port,
|
|
|
|
SynapseSite(
|
|
|
|
"synapse.access.http.%s" % (site_tag,),
|
|
|
|
site_tag,
|
|
|
|
listener_config,
|
|
|
|
root_resource,
|
|
|
|
self.version_string,
|
|
|
|
),
|
|
|
|
reactor=self.get_reactor(),
|
|
|
|
)
|
|
|
|
|
|
|
|
logger.info("Synapse worker now listening on port %d", port)
|
|
|
|
|
2020-06-16 07:44:07 -04:00
|
|
|
def start_listening(self, listeners: Iterable[ListenerConfig]):
|
2020-02-25 11:56:55 -05:00
|
|
|
for listener in listeners:
|
2020-06-16 07:44:07 -04:00
|
|
|
if listener.type == "http":
|
2020-02-25 11:56:55 -05:00
|
|
|
self._listen_http(listener)
|
2020-06-16 07:44:07 -04:00
|
|
|
elif listener.type == "manhole":
|
2020-02-25 11:56:55 -05:00
|
|
|
_base.listen_tcp(
|
2020-06-16 07:44:07 -04:00
|
|
|
listener.bind_addresses,
|
|
|
|
listener.port,
|
2020-02-25 11:56:55 -05:00
|
|
|
manhole(
|
|
|
|
username="matrix", password="rabbithole", globals={"hs": self}
|
|
|
|
),
|
|
|
|
)
|
2020-06-16 07:44:07 -04:00
|
|
|
elif listener.type == "metrics":
|
2020-02-25 11:56:55 -05:00
|
|
|
if not self.get_config().enable_metrics:
|
|
|
|
logger.warning(
|
|
|
|
(
|
|
|
|
"Metrics listener configured, but "
|
|
|
|
"enable_metrics is not True!"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
2020-06-16 07:44:07 -04:00
|
|
|
_base.listen_metrics(listener.bind_addresses, listener.port)
|
2020-02-25 11:56:55 -05:00
|
|
|
else:
|
2020-06-16 07:44:07 -04:00
|
|
|
logger.warning("Unsupported listener type: %s", listener.type)
|
2020-02-25 11:56:55 -05:00
|
|
|
|
|
|
|
self.get_tcp_replication().start_replication(self)
|
|
|
|
|
2020-07-30 06:56:55 -04:00
|
|
|
async def remove_pusher(self, app_id, push_key, user_id):
|
2020-02-25 11:56:55 -05:00
|
|
|
self.get_tcp_replication().send_remove_pusher(app_id, push_key, user_id)
|
|
|
|
|
2020-08-11 13:00:17 -04:00
|
|
|
@cache_in_self
|
|
|
|
def get_replication_data_handler(self):
|
2020-02-25 11:56:55 -05:00
|
|
|
return GenericWorkerReplicationHandler(self)
|
|
|
|
|
2020-08-11 13:00:17 -04:00
|
|
|
@cache_in_self
|
|
|
|
def get_presence_handler(self):
|
2020-02-25 11:56:55 -05:00
|
|
|
return GenericWorkerPresence(self)
|
|
|
|
|
|
|
|
|
2020-04-06 04:58:42 -04:00
|
|
|
class GenericWorkerReplicationHandler(ReplicationDataHandler):
|
2020-02-25 11:56:55 -05:00
|
|
|
def __init__(self, hs):
|
2020-05-14 09:01:39 -04:00
|
|
|
super(GenericWorkerReplicationHandler, self).__init__(hs)
|
2020-02-25 11:56:55 -05:00
|
|
|
|
|
|
|
self.store = hs.get_datastore()
|
2020-04-22 17:39:04 -04:00
|
|
|
self.presence_handler = hs.get_presence_handler() # type: GenericWorkerPresence
|
2020-02-25 11:56:55 -05:00
|
|
|
self.notifier = hs.get_notifier()
|
|
|
|
|
|
|
|
self.notify_pushers = hs.config.start_pushers
|
|
|
|
self.pusher_pool = hs.get_pusherpool()
|
|
|
|
|
2020-05-26 06:41:38 -04:00
|
|
|
self.send_handler = None # type: Optional[FederationSenderHandler]
|
2020-02-25 11:56:55 -05:00
|
|
|
if hs.config.send_federation:
|
2020-05-26 06:41:38 -04:00
|
|
|
self.send_handler = FederationSenderHandler(hs)
|
2020-02-25 11:56:55 -05:00
|
|
|
|
2020-05-01 12:19:56 -04:00
|
|
|
async def on_rdata(self, stream_name, instance_name, token, rows):
|
|
|
|
await super().on_rdata(stream_name, instance_name, token, rows)
|
|
|
|
await self._process_and_notify(stream_name, instance_name, token, rows)
|
2020-02-25 11:56:55 -05:00
|
|
|
|
2020-05-01 12:19:56 -04:00
|
|
|
async def _process_and_notify(self, stream_name, instance_name, token, rows):
|
2020-02-25 11:56:55 -05:00
|
|
|
try:
|
|
|
|
if self.send_handler:
|
2020-04-03 07:29:30 -04:00
|
|
|
await self.send_handler.process_replication_rows(
|
|
|
|
stream_name, token, rows
|
|
|
|
)
|
2020-02-25 11:56:55 -05:00
|
|
|
|
2020-05-14 09:01:39 -04:00
|
|
|
if stream_name == PushRulesStream.NAME:
|
2020-02-25 11:56:55 -05:00
|
|
|
self.notifier.on_new_event(
|
|
|
|
"push_rules_key", token, users=[row.user_id for row in rows]
|
|
|
|
)
|
2020-03-23 07:39:36 -04:00
|
|
|
elif stream_name in (AccountDataStream.NAME, TagAccountDataStream.NAME):
|
2020-02-25 11:56:55 -05:00
|
|
|
self.notifier.on_new_event(
|
|
|
|
"account_data_key", token, users=[row.user_id for row in rows]
|
|
|
|
)
|
2020-03-23 07:39:36 -04:00
|
|
|
elif stream_name == ReceiptsStream.NAME:
|
2020-02-25 11:56:55 -05:00
|
|
|
self.notifier.on_new_event(
|
|
|
|
"receipt_key", token, rooms=[row.room_id for row in rows]
|
|
|
|
)
|
|
|
|
await self.pusher_pool.on_new_receipts(
|
|
|
|
token, token, {row.room_id for row in rows}
|
|
|
|
)
|
2020-03-23 07:39:36 -04:00
|
|
|
elif stream_name == ToDeviceStream.NAME:
|
2020-02-25 11:56:55 -05:00
|
|
|
entities = [row.entity for row in rows if row.entity.startswith("@")]
|
|
|
|
if entities:
|
|
|
|
self.notifier.on_new_event("to_device_key", token, users=entities)
|
2020-03-23 07:39:36 -04:00
|
|
|
elif stream_name == DeviceListsStream.NAME:
|
2020-05-26 06:41:38 -04:00
|
|
|
all_room_ids = set() # type: Set[str]
|
2020-02-25 11:56:55 -05:00
|
|
|
for row in rows:
|
2020-03-02 07:52:28 -05:00
|
|
|
if row.entity.startswith("@"):
|
|
|
|
room_ids = await self.store.get_rooms_for_user(row.entity)
|
|
|
|
all_room_ids.update(room_ids)
|
2020-02-25 11:56:55 -05:00
|
|
|
self.notifier.on_new_event("device_list_key", token, rooms=all_room_ids)
|
2020-03-23 07:39:36 -04:00
|
|
|
elif stream_name == PresenceStream.NAME:
|
2020-02-25 11:56:55 -05:00
|
|
|
await self.presence_handler.process_replication_rows(token, rows)
|
2020-03-23 07:39:36 -04:00
|
|
|
elif stream_name == GroupServerStream.NAME:
|
2020-02-25 11:56:55 -05:00
|
|
|
self.notifier.on_new_event(
|
|
|
|
"groups_key", token, users=[row.user_id for row in rows]
|
|
|
|
)
|
2020-03-23 07:39:36 -04:00
|
|
|
elif stream_name == PushersStream.NAME:
|
2020-02-25 11:56:55 -05:00
|
|
|
for row in rows:
|
|
|
|
if row.deleted:
|
|
|
|
self.stop_pusher(row.user_id, row.app_id, row.pushkey)
|
|
|
|
else:
|
|
|
|
await self.start_pusher(row.user_id, row.app_id, row.pushkey)
|
|
|
|
except Exception:
|
|
|
|
logger.exception("Error processing replication")
|
|
|
|
|
2020-06-15 08:44:54 -04:00
|
|
|
async def on_position(self, stream_name: str, instance_name: str, token: int):
|
|
|
|
await super().on_position(stream_name, instance_name, token)
|
|
|
|
# Also call on_rdata to ensure that stream positions are properly reset.
|
|
|
|
await self.on_rdata(stream_name, instance_name, token, [])
|
|
|
|
|
2020-02-25 11:56:55 -05:00
|
|
|
def stop_pusher(self, user_id, app_id, pushkey):
|
|
|
|
if not self.notify_pushers:
|
|
|
|
return
|
|
|
|
|
|
|
|
key = "%s:%s" % (app_id, pushkey)
|
|
|
|
pushers_for_user = self.pusher_pool.pushers.get(user_id, {})
|
|
|
|
pusher = pushers_for_user.pop(key, None)
|
|
|
|
if pusher is None:
|
|
|
|
return
|
|
|
|
logger.info("Stopping pusher %r / %r", user_id, key)
|
|
|
|
pusher.on_stop()
|
|
|
|
|
|
|
|
async def start_pusher(self, user_id, app_id, pushkey):
|
|
|
|
if not self.notify_pushers:
|
|
|
|
return
|
|
|
|
|
|
|
|
key = "%s:%s" % (app_id, pushkey)
|
|
|
|
logger.info("Starting pusher %r / %r", user_id, key)
|
|
|
|
return await self.pusher_pool.start_pusher_by_id(app_id, pushkey, user_id)
|
|
|
|
|
|
|
|
def on_remote_server_up(self, server: str):
|
|
|
|
"""Called when get a new REMOTE_SERVER_UP command."""
|
|
|
|
|
|
|
|
# Let's wake up the transaction queue for the server in case we have
|
|
|
|
# pending stuff to send to it.
|
|
|
|
if self.send_handler:
|
|
|
|
self.send_handler.wake_destination(server)
|
|
|
|
|
|
|
|
|
|
|
|
class FederationSenderHandler(object):
|
2020-05-26 06:41:38 -04:00
|
|
|
"""Processes the fedration replication stream
|
|
|
|
|
|
|
|
This class is only instantiate on the worker responsible for sending outbound
|
|
|
|
federation transactions. It receives rows from the replication stream and forwards
|
|
|
|
the appropriate entries to the FederationSender class.
|
2020-02-25 11:56:55 -05:00
|
|
|
"""
|
|
|
|
|
2020-05-26 06:41:38 -04:00
|
|
|
def __init__(self, hs: GenericWorkerServer):
|
2020-02-25 11:56:55 -05:00
|
|
|
self.store = hs.get_datastore()
|
|
|
|
self._is_mine_id = hs.is_mine_id
|
|
|
|
self.federation_sender = hs.get_federation_sender()
|
2020-05-26 06:41:38 -04:00
|
|
|
self._hs = hs
|
|
|
|
|
2020-07-10 13:26:36 -04:00
|
|
|
# Stores the latest position in the federation stream we've gotten up
|
|
|
|
# to. This is always set before we use it.
|
|
|
|
self.federation_position = None
|
2020-02-25 11:56:55 -05:00
|
|
|
|
2020-05-26 06:41:38 -04:00
|
|
|
self._fed_position_linearizer = Linearizer(name="_fed_position_linearizer")
|
2020-02-25 11:56:55 -05:00
|
|
|
|
|
|
|
def on_start(self):
|
|
|
|
# There may be some events that are persisted but haven't been sent,
|
|
|
|
# so send them now.
|
|
|
|
self.federation_sender.notify_new_events(
|
|
|
|
self.store.get_room_max_stream_ordering()
|
|
|
|
)
|
|
|
|
|
|
|
|
def wake_destination(self, server: str):
|
|
|
|
self.federation_sender.wake_destination(server)
|
|
|
|
|
2020-04-03 07:29:30 -04:00
|
|
|
async def process_replication_rows(self, stream_name, token, rows):
|
2020-02-25 11:56:55 -05:00
|
|
|
# The federation stream contains things that we want to send out, e.g.
|
|
|
|
# presence, typing, etc.
|
|
|
|
if stream_name == "federation":
|
|
|
|
send_queue.process_rows_for_federation(self.federation_sender, rows)
|
2020-04-03 07:29:30 -04:00
|
|
|
await self.update_token(token)
|
2020-02-25 11:56:55 -05:00
|
|
|
|
|
|
|
# We also need to poke the federation sender when new events happen
|
|
|
|
elif stream_name == "events":
|
|
|
|
self.federation_sender.notify_new_events(token)
|
|
|
|
|
|
|
|
# ... and when new receipts happen
|
|
|
|
elif stream_name == ReceiptsStream.NAME:
|
2020-04-03 07:29:30 -04:00
|
|
|
await self._on_new_receipts(rows)
|
2020-02-25 11:56:55 -05:00
|
|
|
|
|
|
|
# ... as well as device updates and messages
|
|
|
|
elif stream_name == DeviceListsStream.NAME:
|
2020-03-18 06:13:55 -04:00
|
|
|
# The entities are either user IDs (starting with '@') whose devices
|
|
|
|
# have changed, or remote servers that we need to tell about
|
|
|
|
# changes.
|
2020-02-28 06:24:05 -05:00
|
|
|
hosts = {row.entity for row in rows if not row.entity.startswith("@")}
|
2020-02-25 11:56:55 -05:00
|
|
|
for host in hosts:
|
|
|
|
self.federation_sender.send_device_messages(host)
|
|
|
|
|
|
|
|
elif stream_name == ToDeviceStream.NAME:
|
|
|
|
# The to_device stream includes stuff to be pushed to both local
|
|
|
|
# clients and remote servers, so we ignore entities that start with
|
|
|
|
# '@' (since they'll be local users rather than destinations).
|
|
|
|
hosts = {row.entity for row in rows if not row.entity.startswith("@")}
|
|
|
|
for host in hosts:
|
|
|
|
self.federation_sender.send_device_messages(host)
|
|
|
|
|
|
|
|
async def _on_new_receipts(self, rows):
|
|
|
|
"""
|
|
|
|
Args:
|
2020-03-23 09:59:11 -04:00
|
|
|
rows (Iterable[synapse.replication.tcp.streams.ReceiptsStream.ReceiptsStreamRow]):
|
2020-02-25 11:56:55 -05:00
|
|
|
new receipts to be processed
|
|
|
|
"""
|
|
|
|
for receipt in rows:
|
|
|
|
# we only want to send on receipts for our own users
|
|
|
|
if not self._is_mine_id(receipt.user_id):
|
|
|
|
continue
|
|
|
|
receipt_info = ReadReceipt(
|
|
|
|
receipt.room_id,
|
|
|
|
receipt.receipt_type,
|
|
|
|
receipt.user_id,
|
|
|
|
[receipt.event_id],
|
|
|
|
receipt.data,
|
|
|
|
)
|
|
|
|
await self.federation_sender.send_read_receipt(receipt_info)
|
|
|
|
|
|
|
|
async def update_token(self, token):
|
2020-05-26 06:41:38 -04:00
|
|
|
"""Update the record of where we have processed to in the federation stream.
|
|
|
|
|
|
|
|
Called after we have processed a an update received over replication. Sends
|
|
|
|
a FEDERATION_ACK back to the master, and stores the token that we have processed
|
|
|
|
in `federation_stream_position` so that we can restart where we left off.
|
|
|
|
"""
|
2020-05-27 14:31:44 -04:00
|
|
|
self.federation_position = token
|
|
|
|
|
|
|
|
# We save and send the ACK to master asynchronously, so we don't block
|
|
|
|
# processing on persistence. We don't need to do this operation for
|
|
|
|
# every single RDATA we receive, we just need to do it periodically.
|
|
|
|
|
|
|
|
if self._fed_position_linearizer.is_queued(None):
|
|
|
|
# There is already a task queued up to save and send the token, so
|
|
|
|
# no need to queue up another task.
|
|
|
|
return
|
2020-02-25 11:56:55 -05:00
|
|
|
|
2020-05-27 14:31:44 -04:00
|
|
|
run_as_background_process("_save_and_send_ack", self._save_and_send_ack)
|
|
|
|
|
|
|
|
async def _save_and_send_ack(self):
|
|
|
|
"""Save the current federation position in the database and send an ACK
|
|
|
|
to master with where we're up to.
|
|
|
|
"""
|
|
|
|
try:
|
2020-02-25 11:56:55 -05:00
|
|
|
# We linearize here to ensure we don't have races updating the token
|
2020-05-26 06:41:38 -04:00
|
|
|
#
|
|
|
|
# XXX this appears to be redundant, since the ReplicationCommandHandler
|
|
|
|
# has a linearizer which ensures that we only process one line of
|
|
|
|
# replication data at a time. Should we remove it, or is it doing useful
|
|
|
|
# service for robustness? Or could we replace it with an assertion that
|
|
|
|
# we're not being re-entered?
|
|
|
|
|
2020-02-25 11:56:55 -05:00
|
|
|
with (await self._fed_position_linearizer.queue(None)):
|
2020-05-27 14:45:42 -04:00
|
|
|
# We persist and ack the same position, so we take a copy of it
|
|
|
|
# here as otherwise it can get modified from underneath us.
|
|
|
|
current_position = self.federation_position
|
|
|
|
|
2020-05-26 06:41:38 -04:00
|
|
|
await self.store.update_federation_out_pos(
|
2020-05-27 14:45:42 -04:00
|
|
|
"federation", current_position
|
2020-05-26 06:41:38 -04:00
|
|
|
)
|
2020-02-25 11:56:55 -05:00
|
|
|
|
2020-05-26 06:41:38 -04:00
|
|
|
# We ACK this token over replication so that the master can drop
|
|
|
|
# its in memory queues
|
2020-05-27 14:45:42 -04:00
|
|
|
self._hs.get_tcp_replication().send_federation_ack(current_position)
|
2020-02-25 11:56:55 -05:00
|
|
|
except Exception:
|
|
|
|
logger.exception("Error updating federation stream position")
|
|
|
|
|
|
|
|
|
|
|
|
def start(config_options):
|
|
|
|
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.
|
|
|
|
assert config.worker_app in (
|
|
|
|
"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",
|
|
|
|
)
|
|
|
|
|
|
|
|
if config.worker_app == "synapse.app.appservice":
|
2020-07-10 13:26:36 -04:00
|
|
|
if config.appservice.notify_appservices:
|
2020-02-25 11:56:55 -05:00
|
|
|
sys.stderr.write(
|
|
|
|
"\nThe appservices must be disabled in the main synapse process"
|
|
|
|
"\nbefore they can be run in a separate worker."
|
|
|
|
"\nPlease add ``notify_appservices: false`` to the main config"
|
|
|
|
"\n"
|
|
|
|
)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Force the appservice to start since they will be disabled in the main config
|
2020-07-10 13:26:36 -04:00
|
|
|
config.appservice.notify_appservices = True
|
2020-03-24 06:35:00 -04:00
|
|
|
else:
|
|
|
|
# For other worker types we force this to off.
|
2020-07-10 13:26:36 -04:00
|
|
|
config.appservice.notify_appservices = False
|
2020-02-25 11:56:55 -05:00
|
|
|
|
|
|
|
if config.worker_app == "synapse.app.pusher":
|
2020-07-10 13:26:36 -04:00
|
|
|
if config.server.start_pushers:
|
2020-02-25 11:56:55 -05:00
|
|
|
sys.stderr.write(
|
|
|
|
"\nThe pushers must be disabled in the main synapse process"
|
|
|
|
"\nbefore they can be run in a separate worker."
|
|
|
|
"\nPlease add ``start_pushers: false`` to the main config"
|
|
|
|
"\n"
|
|
|
|
)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Force the pushers to start since they will be disabled in the main config
|
2020-07-10 13:26:36 -04:00
|
|
|
config.server.start_pushers = True
|
2020-03-24 06:35:00 -04:00
|
|
|
else:
|
|
|
|
# For other worker types we force this to off.
|
2020-07-10 13:26:36 -04:00
|
|
|
config.server.start_pushers = False
|
2020-02-25 11:56:55 -05:00
|
|
|
|
|
|
|
if config.worker_app == "synapse.app.user_dir":
|
2020-07-10 13:26:36 -04:00
|
|
|
if config.server.update_user_directory:
|
2020-02-25 11:56:55 -05:00
|
|
|
sys.stderr.write(
|
|
|
|
"\nThe update_user_directory must be disabled in the main synapse process"
|
|
|
|
"\nbefore they can be run in a separate worker."
|
|
|
|
"\nPlease add ``update_user_directory: false`` to the main config"
|
|
|
|
"\n"
|
|
|
|
)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Force the pushers to start since they will be disabled in the main config
|
2020-07-10 13:26:36 -04:00
|
|
|
config.server.update_user_directory = True
|
2020-03-24 06:35:00 -04:00
|
|
|
else:
|
|
|
|
# For other worker types we force this to off.
|
2020-07-10 13:26:36 -04:00
|
|
|
config.server.update_user_directory = False
|
2020-02-25 11:56:55 -05:00
|
|
|
|
|
|
|
if config.worker_app == "synapse.app.federation_sender":
|
2020-07-29 18:22:13 -04:00
|
|
|
if config.worker.send_federation:
|
2020-02-25 11:56:55 -05:00
|
|
|
sys.stderr.write(
|
|
|
|
"\nThe send_federation must be disabled in the main synapse process"
|
|
|
|
"\nbefore they can be run in a separate worker."
|
|
|
|
"\nPlease add ``send_federation: false`` to the main config"
|
|
|
|
"\n"
|
|
|
|
)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Force the pushers to start since they will be disabled in the main config
|
2020-07-29 18:22:13 -04:00
|
|
|
config.worker.send_federation = True
|
2020-03-24 06:35:00 -04:00
|
|
|
else:
|
|
|
|
# For other worker types we force this to off.
|
2020-07-29 18:22:13 -04:00
|
|
|
config.worker.send_federation = False
|
2020-02-25 11:56:55 -05:00
|
|
|
|
|
|
|
synapse.events.USE_FROZEN_DICTS = config.use_frozen_dicts
|
|
|
|
|
2020-04-28 08:34:12 -04:00
|
|
|
hs = GenericWorkerServer(
|
2020-02-25 11:56:55 -05:00
|
|
|
config.server_name,
|
|
|
|
config=config,
|
|
|
|
version_string="Synapse/" + get_version_string(synapse),
|
|
|
|
)
|
|
|
|
|
2020-04-28 08:34:12 -04:00
|
|
|
setup_logging(hs, config, use_worker_options=True)
|
|
|
|
|
|
|
|
hs.setup()
|
|
|
|
|
|
|
|
# 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()
|
2020-02-25 11:56:55 -05:00
|
|
|
|
|
|
|
reactor.addSystemEventTrigger(
|
2020-04-28 08:34:12 -04:00
|
|
|
"before", "startup", _base.start, hs, config.worker_listeners
|
2020-02-25 11:56:55 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
_base.start_worker_reactor("synapse-generic-worker", config)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
with LoggingContext("main"):
|
|
|
|
start(sys.argv[1:])
|