2017-03-27 10:42:07 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2017 Vector Creations Ltd
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
"""The server side of the replication stream.
|
|
|
|
"""
|
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
import logging
|
2019-03-20 12:04:35 -04:00
|
|
|
import random
|
2020-01-27 08:47:50 -05:00
|
|
|
from typing import Any, List
|
2018-07-09 02:09:20 -04:00
|
|
|
|
|
|
|
from six import itervalues
|
|
|
|
|
|
|
|
from prometheus_client import Counter
|
|
|
|
|
2017-03-27 10:42:07 -04:00
|
|
|
from twisted.internet.protocol import Factory
|
|
|
|
|
2018-05-21 20:48:57 -04:00
|
|
|
from synapse.metrics import LaterGauge
|
2018-07-25 04:41:12 -04:00
|
|
|
from synapse.metrics.background_process_metrics import run_as_background_process
|
2018-07-09 02:09:20 -04:00
|
|
|
from synapse.util.metrics import Measure, measure_func
|
2017-03-27 10:42:07 -04:00
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
from .protocol import ServerReplicationStreamProtocol
|
2019-03-27 07:45:42 -04:00
|
|
|
from .streams import STREAMS_MAP
|
|
|
|
from .streams.federation import FederationStream
|
2017-03-27 10:42:07 -04:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
stream_updates_counter = Counter(
|
|
|
|
"synapse_replication_tcp_resource_stream_updates", "", ["stream_name"]
|
|
|
|
)
|
2018-05-21 20:48:57 -04:00
|
|
|
user_sync_counter = Counter("synapse_replication_tcp_resource_user_sync", "")
|
|
|
|
federation_ack_counter = Counter("synapse_replication_tcp_resource_federation_ack", "")
|
|
|
|
remove_pusher_counter = Counter("synapse_replication_tcp_resource_remove_pusher", "")
|
2019-06-20 05:32:02 -04:00
|
|
|
invalidate_cache_counter = Counter(
|
|
|
|
"synapse_replication_tcp_resource_invalidate_cache", ""
|
|
|
|
)
|
2018-05-21 20:48:57 -04:00
|
|
|
user_ip_cache_counter = Counter("synapse_replication_tcp_resource_user_ip_cache", "")
|
2017-03-27 10:42:07 -04:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class ReplicationStreamProtocolFactory(Factory):
|
|
|
|
"""Factory for new replication connections.
|
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2017-03-27 10:42:07 -04:00
|
|
|
def __init__(self, hs):
|
|
|
|
self.streamer = ReplicationStreamer(hs)
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.server_name = hs.config.server_name
|
|
|
|
|
|
|
|
def buildProtocol(self, addr):
|
|
|
|
return ServerReplicationStreamProtocol(
|
2019-06-20 05:32:02 -04:00
|
|
|
self.server_name, self.clock, self.streamer
|
2017-03-27 10:42:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class ReplicationStreamer(object):
|
|
|
|
"""Handles replication connections.
|
|
|
|
|
|
|
|
This needs to be poked when new replication data may be available. When new
|
|
|
|
data is available it will propagate to all connected clients.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
self.store = hs.get_datastore()
|
|
|
|
self.presence_handler = hs.get_presence_handler()
|
|
|
|
self.clock = hs.get_clock()
|
2017-06-09 06:24:41 -04:00
|
|
|
self.notifier = hs.get_notifier()
|
2018-05-17 12:35:31 -04:00
|
|
|
self._server_notices_sender = hs.get_server_notices_sender()
|
2017-03-27 10:42:07 -04:00
|
|
|
|
2019-03-20 12:04:35 -04:00
|
|
|
self._replication_torture_level = hs.config.replication_torture_level
|
|
|
|
|
2017-03-27 10:42:07 -04:00
|
|
|
# Current connections.
|
2020-01-14 09:08:06 -05:00
|
|
|
self.connections = [] # type: List[ServerReplicationStreamProtocol]
|
2017-03-27 10:42:07 -04:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
LaterGauge(
|
|
|
|
"synapse_replication_tcp_resource_total_connections",
|
|
|
|
"",
|
|
|
|
[],
|
|
|
|
lambda: len(self.connections),
|
|
|
|
)
|
2017-03-27 10:42:07 -04:00
|
|
|
|
|
|
|
# List of streams that clients can subscribe to.
|
|
|
|
# We only support federation stream if federation sending hase been
|
|
|
|
# disabled on the master.
|
|
|
|
self.streams = [
|
2019-06-20 05:32:02 -04:00
|
|
|
stream(hs)
|
|
|
|
for stream in itervalues(STREAMS_MAP)
|
2017-03-27 10:42:07 -04:00
|
|
|
if stream != FederationStream or not hs.config.send_federation
|
|
|
|
]
|
|
|
|
|
|
|
|
self.streams_by_name = {stream.NAME: stream for stream in self.streams}
|
|
|
|
|
2018-05-21 20:48:57 -04:00
|
|
|
LaterGauge(
|
2019-06-20 05:32:02 -04:00
|
|
|
"synapse_replication_tcp_resource_connections_per_stream",
|
|
|
|
"",
|
2018-05-22 11:56:03 -04:00
|
|
|
["stream_name"],
|
2017-03-27 10:42:07 -04:00
|
|
|
lambda: {
|
2019-06-20 05:32:02 -04:00
|
|
|
(stream_name,): len(
|
|
|
|
[
|
|
|
|
conn
|
|
|
|
for conn in self.connections
|
|
|
|
if stream_name in conn.replication_streams
|
|
|
|
]
|
|
|
|
)
|
2017-03-27 10:42:07 -04:00
|
|
|
for stream_name in self.streams_by_name
|
2019-06-20 05:32:02 -04:00
|
|
|
},
|
|
|
|
)
|
2017-03-27 10:42:07 -04:00
|
|
|
|
|
|
|
self.federation_sender = None
|
|
|
|
if not hs.config.send_federation:
|
|
|
|
self.federation_sender = hs.get_federation_sender()
|
|
|
|
|
2017-06-09 06:24:41 -04:00
|
|
|
self.notifier.add_replication_callback(self.on_notifier_poke)
|
2020-01-17 05:27:19 -05:00
|
|
|
self.notifier.add_remote_server_up_callback(self.send_remote_server_up)
|
2017-03-27 10:42:07 -04:00
|
|
|
|
|
|
|
# Keeps track of whether we are currently checking for updates
|
|
|
|
self.is_looping = False
|
|
|
|
self.pending_updates = False
|
|
|
|
|
2018-06-25 09:08:28 -04:00
|
|
|
hs.get_reactor().addSystemEventTrigger("before", "shutdown", self.on_shutdown)
|
2017-03-27 10:42:07 -04:00
|
|
|
|
|
|
|
def on_shutdown(self):
|
|
|
|
# close all connections on shutdown
|
|
|
|
for conn in self.connections:
|
|
|
|
conn.send_error("server shutting down")
|
|
|
|
|
|
|
|
def on_notifier_poke(self):
|
|
|
|
"""Checks if there is actually any new data and sends it to the
|
|
|
|
connections if there are.
|
|
|
|
|
|
|
|
This should get called each time new data is available, even if it
|
|
|
|
is currently being executed, so that nothing gets missed
|
|
|
|
"""
|
|
|
|
if not self.connections:
|
2017-04-03 10:22:56 -04:00
|
|
|
# Don't bother if nothing is listening. We still need to advance
|
|
|
|
# the stream tokens otherwise they'll fall beihind forever
|
|
|
|
for stream in self.streams:
|
2017-04-04 08:19:26 -04:00
|
|
|
stream.discard_updates_and_advance()
|
2017-03-27 10:42:07 -04:00
|
|
|
return
|
|
|
|
|
2018-07-25 04:41:12 -04:00
|
|
|
self.pending_updates = True
|
|
|
|
|
2017-03-27 10:42:07 -04:00
|
|
|
if self.is_looping:
|
2018-07-25 04:41:12 -04:00
|
|
|
logger.debug("Notifier poke loop already running")
|
2017-03-27 10:42:07 -04:00
|
|
|
return
|
|
|
|
|
2018-07-25 04:41:12 -04:00
|
|
|
run_as_background_process("replication_notifier", self._run_notifier_loop)
|
|
|
|
|
2020-01-16 04:16:12 -05:00
|
|
|
async def _run_notifier_loop(self):
|
2017-03-27 10:42:07 -04:00
|
|
|
self.is_looping = True
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Keep looping while there have been pokes about potential updates.
|
|
|
|
# This protects against the race where a stream we already checked
|
|
|
|
# gets an update while we're handling other streams.
|
|
|
|
while self.pending_updates:
|
|
|
|
self.pending_updates = False
|
|
|
|
|
|
|
|
with Measure(self.clock, "repl.stream.get_updates"):
|
2019-03-20 12:04:35 -04:00
|
|
|
all_streams = self.streams
|
|
|
|
|
|
|
|
if self._replication_torture_level is not None:
|
|
|
|
# there is no guarantee about ordering between the streams,
|
|
|
|
# so let's shuffle them around a bit when we are in torture mode.
|
|
|
|
all_streams = list(all_streams)
|
|
|
|
random.shuffle(all_streams)
|
|
|
|
|
|
|
|
for stream in all_streams:
|
2020-03-20 10:40:47 -04:00
|
|
|
if stream.last_token == stream.current_token():
|
2017-03-27 10:42:07 -04:00
|
|
|
continue
|
|
|
|
|
2019-03-20 12:04:35 -04:00
|
|
|
if self._replication_torture_level:
|
2020-01-16 04:16:12 -05:00
|
|
|
await self.clock.sleep(
|
2019-03-20 12:04:35 -04:00
|
|
|
self._replication_torture_level / 1000.0
|
|
|
|
)
|
|
|
|
|
2017-03-27 10:42:07 -04:00
|
|
|
logger.debug(
|
|
|
|
"Getting stream: %s: %s -> %s",
|
2019-06-20 05:32:02 -04:00
|
|
|
stream.NAME,
|
|
|
|
stream.last_token,
|
2020-03-20 10:40:47 -04:00
|
|
|
stream.current_token(),
|
2017-03-27 10:42:07 -04:00
|
|
|
)
|
2017-10-12 06:50:44 -04:00
|
|
|
try:
|
2020-01-16 04:16:12 -05:00
|
|
|
updates, current_token = await stream.get_updates()
|
2017-10-23 10:52:32 -04:00
|
|
|
except Exception:
|
2017-10-12 06:50:44 -04:00
|
|
|
logger.info("Failed to handle stream %s", stream.NAME)
|
|
|
|
raise
|
2017-03-27 10:42:07 -04:00
|
|
|
|
|
|
|
logger.debug(
|
|
|
|
"Sending %d updates to %d connections",
|
2019-06-20 05:32:02 -04:00
|
|
|
len(updates),
|
|
|
|
len(self.connections),
|
2017-03-27 10:42:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
if updates:
|
|
|
|
logger.info(
|
|
|
|
"Streaming: %s -> %s", stream.NAME, updates[-1][0]
|
|
|
|
)
|
2018-05-21 20:48:57 -04:00
|
|
|
stream_updates_counter.labels(stream.NAME).inc(len(updates))
|
2017-03-27 10:42:07 -04:00
|
|
|
|
|
|
|
# Some streams return multiple rows with the same stream IDs,
|
|
|
|
# we need to make sure they get sent out in batches. We do
|
|
|
|
# this by setting the current token to all but the last of
|
|
|
|
# a series of updates with the same token to have a None
|
|
|
|
# token. See RdataCommand for more details.
|
|
|
|
batched_updates = _batch_updates(updates)
|
|
|
|
|
|
|
|
for conn in self.connections:
|
|
|
|
for token, row in batched_updates:
|
|
|
|
try:
|
|
|
|
conn.stream_update(stream.NAME, token, row)
|
|
|
|
except Exception:
|
|
|
|
logger.exception("Failed to replicate")
|
|
|
|
|
|
|
|
logger.debug("No more pending updates, breaking poke loop")
|
|
|
|
finally:
|
|
|
|
self.pending_updates = False
|
|
|
|
self.is_looping = False
|
|
|
|
|
|
|
|
@measure_func("repl.get_stream_updates")
|
2020-01-16 04:16:12 -05:00
|
|
|
async def get_stream_updates(self, stream_name, token):
|
2017-03-27 10:42:07 -04:00
|
|
|
"""For a given stream get all updates since token. This is called when
|
|
|
|
a client first subscribes to a stream.
|
|
|
|
"""
|
|
|
|
stream = self.streams_by_name.get(stream_name, None)
|
|
|
|
if not stream:
|
|
|
|
raise Exception("unknown stream %s", stream_name)
|
|
|
|
|
2020-01-16 04:16:12 -05:00
|
|
|
return await stream.get_updates_since(token)
|
2017-03-27 10:42:07 -04:00
|
|
|
|
|
|
|
@measure_func("repl.federation_ack")
|
|
|
|
def federation_ack(self, token):
|
|
|
|
"""We've received an ack for federation stream from a client.
|
|
|
|
"""
|
|
|
|
federation_ack_counter.inc()
|
|
|
|
if self.federation_sender:
|
|
|
|
self.federation_sender.federation_ack(token)
|
|
|
|
|
|
|
|
@measure_func("repl.on_user_sync")
|
2020-01-16 04:16:12 -05:00
|
|
|
async def on_user_sync(self, conn_id, user_id, is_syncing, last_sync_ms):
|
2017-03-27 10:42:07 -04:00
|
|
|
"""A client has started/stopped syncing on a worker.
|
|
|
|
"""
|
|
|
|
user_sync_counter.inc()
|
2020-01-16 04:16:12 -05:00
|
|
|
await self.presence_handler.update_external_syncs_row(
|
2019-06-20 05:32:02 -04:00
|
|
|
conn_id, user_id, is_syncing, last_sync_ms
|
2017-03-27 10:42:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
@measure_func("repl.on_remove_pusher")
|
2020-01-16 04:16:12 -05:00
|
|
|
async def on_remove_pusher(self, app_id, push_key, user_id):
|
2017-03-27 10:42:07 -04:00
|
|
|
"""A client has asked us to remove a pusher
|
|
|
|
"""
|
|
|
|
remove_pusher_counter.inc()
|
2020-01-16 04:16:12 -05:00
|
|
|
await self.store.delete_pusher_by_app_id_pushkey_user_id(
|
2017-03-27 10:42:07 -04:00
|
|
|
app_id=app_id, pushkey=push_key, user_id=user_id
|
|
|
|
)
|
|
|
|
|
|
|
|
self.notifier.on_new_replication_data()
|
|
|
|
|
|
|
|
@measure_func("repl.on_invalidate_cache")
|
2020-01-27 08:47:50 -05:00
|
|
|
async def on_invalidate_cache(self, cache_func: str, keys: List[Any]):
|
2017-03-27 10:42:07 -04:00
|
|
|
"""The client has asked us to invalidate a cache
|
|
|
|
"""
|
|
|
|
invalidate_cache_counter.inc()
|
2020-01-27 08:47:50 -05:00
|
|
|
|
|
|
|
# We invalidate the cache locally, but then also stream that to other
|
|
|
|
# workers.
|
|
|
|
await self.store.invalidate_cache_and_stream(cache_func, tuple(keys))
|
2017-03-27 10:42:07 -04:00
|
|
|
|
2017-06-27 09:58:10 -04:00
|
|
|
@measure_func("repl.on_user_ip")
|
2020-01-16 04:16:12 -05:00
|
|
|
async def on_user_ip(
|
|
|
|
self, user_id, access_token, ip, user_agent, device_id, last_seen
|
|
|
|
):
|
2017-06-27 09:58:10 -04:00
|
|
|
"""The client saw a user request
|
|
|
|
"""
|
|
|
|
user_ip_cache_counter.inc()
|
2020-01-16 04:16:12 -05:00
|
|
|
await self.store.insert_client_ip(
|
2019-06-20 05:32:02 -04:00
|
|
|
user_id, access_token, ip, user_agent, device_id, last_seen
|
2017-06-27 09:58:10 -04:00
|
|
|
)
|
2020-01-16 04:16:12 -05:00
|
|
|
await self._server_notices_sender.on_user_ip(user_id)
|
2017-06-27 09:58:10 -04:00
|
|
|
|
2020-01-17 05:27:19 -05:00
|
|
|
@measure_func("repl.on_remote_server_up")
|
|
|
|
def on_remote_server_up(self, server: str):
|
|
|
|
self.notifier.notify_remote_server_up(server)
|
|
|
|
|
|
|
|
def send_remote_server_up(self, server: str):
|
|
|
|
for conn in self.connections:
|
|
|
|
conn.send_remote_server_up(server)
|
|
|
|
|
2017-03-27 10:42:07 -04:00
|
|
|
def send_sync_to_all_connections(self, data):
|
|
|
|
"""Sends a SYNC command to all clients.
|
|
|
|
|
|
|
|
Used in tests.
|
|
|
|
"""
|
|
|
|
for conn in self.connections:
|
|
|
|
conn.send_sync(data)
|
|
|
|
|
|
|
|
def new_connection(self, connection):
|
|
|
|
"""A new client connection has been established
|
|
|
|
"""
|
|
|
|
self.connections.append(connection)
|
|
|
|
|
|
|
|
def lost_connection(self, connection):
|
|
|
|
"""A client connection has been lost
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
self.connections.remove(connection)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# We need to tell the presence handler that the connection has been
|
|
|
|
# lost so that it can handle any ongoing syncs on that connection.
|
2020-02-26 10:33:26 -05:00
|
|
|
run_as_background_process(
|
|
|
|
"update_external_syncs_clear",
|
|
|
|
self.presence_handler.update_external_syncs_clear,
|
|
|
|
connection.conn_id,
|
|
|
|
)
|
2017-03-27 10:42:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _batch_updates(updates):
|
|
|
|
"""Takes a list of updates of form [(token, row)] and sets the token to
|
|
|
|
None for all rows where the next row has the same token. This is used to
|
|
|
|
implement batching.
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
[(1, _), (1, _), (2, _), (3, _), (3, _)]
|
|
|
|
|
|
|
|
becomes:
|
|
|
|
|
|
|
|
[(None, _), (1, _), (2, _), (None, _), (3, _)]
|
|
|
|
"""
|
|
|
|
if not updates:
|
|
|
|
return []
|
|
|
|
|
|
|
|
new_updates = []
|
|
|
|
for i, update in enumerate(updates[:-1]):
|
|
|
|
if update[0] == updates[i + 1][0]:
|
|
|
|
new_updates.append((None, update[1]))
|
|
|
|
else:
|
|
|
|
new_updates.append(update)
|
|
|
|
|
|
|
|
new_updates.append(updates[-1])
|
|
|
|
return new_updates
|