2020-04-22 06:07:41 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# 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
|
2020-07-27 11:54:43 -06:00
|
|
|
from inspect import isawaitable
|
2020-10-02 02:57:12 -06:00
|
|
|
from typing import TYPE_CHECKING, Optional
|
2020-04-22 06:07:41 -06:00
|
|
|
|
|
|
|
import txredisapi
|
|
|
|
|
2020-07-21 17:40:42 -06:00
|
|
|
from synapse.logging.context import PreserveLoggingContext, make_deferred_yieldable
|
|
|
|
from synapse.metrics.background_process_metrics import (
|
|
|
|
BackgroundProcessLoggingContext,
|
|
|
|
run_as_background_process,
|
|
|
|
)
|
2020-04-22 06:07:41 -06:00
|
|
|
from synapse.replication.tcp.commands import (
|
|
|
|
Command,
|
|
|
|
ReplicateCommand,
|
|
|
|
parse_command_from_line,
|
|
|
|
)
|
2020-04-22 09:26:19 -06:00
|
|
|
from synapse.replication.tcp.protocol import (
|
|
|
|
AbstractConnection,
|
|
|
|
tcp_inbound_commands_counter,
|
|
|
|
tcp_outbound_commands_counter,
|
|
|
|
)
|
2020-04-22 06:07:41 -06:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from synapse.replication.tcp.handler import ReplicationCommandHandler
|
|
|
|
from synapse.server import HomeServer
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class RedisSubscriber(txredisapi.SubscriberProtocol, AbstractConnection):
|
|
|
|
"""Connection to redis subscribed to replication stream.
|
|
|
|
|
2020-05-05 11:53:38 -06:00
|
|
|
This class fulfils two functions:
|
|
|
|
|
|
|
|
(a) it implements the twisted Protocol API, where it handles the SUBSCRIBEd redis
|
|
|
|
connection, parsing *incoming* messages into replication commands, and passing them
|
|
|
|
to `ReplicationCommandHandler`
|
|
|
|
|
|
|
|
(b) it implements the AbstractConnection API, where it sends *outgoing* commands
|
|
|
|
onto outbound_redis_connection.
|
2020-04-22 06:07:41 -06:00
|
|
|
|
|
|
|
Due to the vagaries of `txredisapi` we don't want to have a custom
|
|
|
|
constructor, so instead we expect the defined attributes below to be set
|
|
|
|
immediately after initialisation.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
handler: The command handler to handle incoming commands.
|
2020-05-05 11:53:38 -06:00
|
|
|
stream_name: The *redis* stream name to subscribe to and publish from
|
|
|
|
(not anything to do with Synapse replication streams).
|
2020-04-22 06:07:41 -06:00
|
|
|
outbound_redis_connection: The connection to redis to use to send
|
|
|
|
commands.
|
|
|
|
"""
|
|
|
|
|
|
|
|
handler = None # type: ReplicationCommandHandler
|
|
|
|
stream_name = None # type: str
|
|
|
|
outbound_redis_connection = None # type: txredisapi.RedisProtocol
|
|
|
|
|
2020-07-21 17:40:42 -06:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
# a logcontext which we use for processing incoming commands. We declare it as a
|
|
|
|
# background process so that the CPU stats get reported to prometheus.
|
|
|
|
self._logging_context = BackgroundProcessLoggingContext(
|
|
|
|
"replication_command_handler"
|
|
|
|
)
|
|
|
|
|
2020-04-22 06:07:41 -06:00
|
|
|
def connectionMade(self):
|
2020-05-05 11:53:38 -06:00
|
|
|
logger.info("Connected to redis")
|
2020-05-04 07:04:09 -06:00
|
|
|
super().connectionMade()
|
2020-05-05 11:53:38 -06:00
|
|
|
run_as_background_process("subscribe-replication", self._send_subscribe)
|
2020-04-22 06:07:41 -06:00
|
|
|
|
2020-05-05 11:53:38 -06:00
|
|
|
async def _send_subscribe(self):
|
|
|
|
# it's important to make sure that we only send the REPLICATE command once we
|
|
|
|
# have successfully subscribed to the stream - otherwise we might miss the
|
|
|
|
# POSITION response sent back by the other end.
|
|
|
|
logger.info("Sending redis SUBSCRIBE for %s", self.stream_name)
|
|
|
|
await make_deferred_yieldable(self.subscribe(self.stream_name))
|
|
|
|
logger.info(
|
|
|
|
"Successfully subscribed to redis stream, sending REPLICATE command"
|
|
|
|
)
|
2020-05-13 02:57:15 -06:00
|
|
|
self.handler.new_connection(self)
|
2020-05-05 11:53:38 -06:00
|
|
|
await self._async_send_command(ReplicateCommand())
|
|
|
|
logger.info("REPLICATE successfully sent")
|
|
|
|
|
2020-05-13 02:57:15 -06:00
|
|
|
# We send out our positions when there is a new connection in case the
|
|
|
|
# other side missed updates. We do this for Redis connections as the
|
|
|
|
# otherside won't know we've connected and so won't issue a REPLICATE.
|
|
|
|
self.handler.send_positions_to_connection(self)
|
|
|
|
|
2020-04-22 06:07:41 -06:00
|
|
|
def messageReceived(self, pattern: str, channel: str, message: str):
|
|
|
|
"""Received a message from redis.
|
|
|
|
"""
|
2020-07-21 17:40:42 -06:00
|
|
|
with PreserveLoggingContext(self._logging_context):
|
|
|
|
self._parse_and_dispatch_message(message)
|
2020-04-22 06:07:41 -06:00
|
|
|
|
2020-07-21 17:40:42 -06:00
|
|
|
def _parse_and_dispatch_message(self, message: str):
|
2020-04-22 06:07:41 -06:00
|
|
|
if message.strip() == "":
|
|
|
|
# Ignore blank lines
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
cmd = parse_command_from_line(message)
|
|
|
|
except Exception:
|
|
|
|
logger.exception(
|
2020-05-07 12:51:38 -06:00
|
|
|
"Failed to parse replication line: %r", message,
|
2020-04-22 06:07:41 -06:00
|
|
|
)
|
|
|
|
return
|
|
|
|
|
2020-04-22 09:26:19 -06:00
|
|
|
# We use "redis" as the name here as we don't have 1:1 connections to
|
|
|
|
# remote instances.
|
|
|
|
tcp_inbound_commands_counter.labels(cmd.NAME, "redis").inc()
|
|
|
|
|
2020-07-27 11:54:43 -06:00
|
|
|
self.handle_command(cmd)
|
2020-04-22 06:07:41 -06:00
|
|
|
|
2020-07-27 11:54:43 -06:00
|
|
|
def handle_command(self, cmd: Command) -> None:
|
2020-04-22 06:07:41 -06:00
|
|
|
"""Handle a command we have received over the replication stream.
|
|
|
|
|
2020-07-27 11:54:43 -06:00
|
|
|
Delegates to `self.handler.on_<COMMAND>` (which can optionally return an
|
|
|
|
Awaitable).
|
2020-04-22 06:07:41 -06:00
|
|
|
|
|
|
|
Args:
|
|
|
|
cmd: received command
|
|
|
|
"""
|
|
|
|
|
|
|
|
cmd_func = getattr(self.handler, "on_%s" % (cmd.NAME,), None)
|
2020-07-27 11:54:43 -06:00
|
|
|
if not cmd_func:
|
2020-04-22 06:07:41 -06:00
|
|
|
logger.warning("Unhandled command: %r", cmd)
|
2020-07-27 11:54:43 -06:00
|
|
|
return
|
|
|
|
|
|
|
|
res = cmd_func(self, cmd)
|
|
|
|
|
|
|
|
# the handler might be a coroutine: fire it off as a background process
|
|
|
|
# if so.
|
|
|
|
|
|
|
|
if isawaitable(res):
|
|
|
|
run_as_background_process(
|
|
|
|
"replication-" + cmd.get_logcontext_id(), lambda: res
|
|
|
|
)
|
2020-04-22 06:07:41 -06:00
|
|
|
|
|
|
|
def connectionLost(self, reason):
|
2020-05-05 11:53:38 -06:00
|
|
|
logger.info("Lost connection to redis")
|
2020-05-04 07:04:09 -06:00
|
|
|
super().connectionLost(reason)
|
2020-04-22 06:07:41 -06:00
|
|
|
self.handler.lost_connection(self)
|
|
|
|
|
2020-07-21 17:40:42 -06:00
|
|
|
# mark the logging context as finished
|
|
|
|
self._logging_context.__exit__(None, None, None)
|
|
|
|
|
2020-04-22 06:07:41 -06:00
|
|
|
def send_command(self, cmd: Command):
|
|
|
|
"""Send a command if connection has been established.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
cmd (Command)
|
|
|
|
"""
|
2020-10-26 03:30:19 -06:00
|
|
|
run_as_background_process(
|
|
|
|
"send-cmd", self._async_send_command, cmd, bg_start_span=False
|
|
|
|
)
|
2020-05-05 11:53:38 -06:00
|
|
|
|
|
|
|
async def _async_send_command(self, cmd: Command):
|
|
|
|
"""Encode a replication command and send it over our outbound connection"""
|
2020-04-22 06:07:41 -06:00
|
|
|
string = "%s %s" % (cmd.NAME, cmd.to_line())
|
|
|
|
if "\n" in string:
|
|
|
|
raise Exception("Unexpected newline in command: %r", string)
|
|
|
|
|
|
|
|
encoded_string = string.encode("utf-8")
|
|
|
|
|
2020-04-22 09:26:19 -06:00
|
|
|
# We use "redis" as the name here as we don't have 1:1 connections to
|
|
|
|
# remote instances.
|
|
|
|
tcp_outbound_commands_counter.labels(cmd.NAME, "redis").inc()
|
|
|
|
|
2020-05-05 11:53:38 -06:00
|
|
|
await make_deferred_yieldable(
|
|
|
|
self.outbound_redis_connection.publish(self.stream_name, encoded_string)
|
|
|
|
)
|
2020-04-22 06:07:41 -06:00
|
|
|
|
|
|
|
|
|
|
|
class RedisDirectTcpReplicationClientFactory(txredisapi.SubscriberFactory):
|
|
|
|
"""This is a reconnecting factory that connects to redis and immediately
|
|
|
|
subscribes to a stream.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
hs
|
|
|
|
outbound_redis_connection: A connection to redis that will be used to
|
2020-07-09 07:52:58 -06:00
|
|
|
send outbound commands (this is separate to the redis connection
|
2020-04-22 06:07:41 -06:00
|
|
|
used to subscribe).
|
|
|
|
"""
|
|
|
|
|
|
|
|
maxDelay = 5
|
|
|
|
continueTrying = True
|
|
|
|
protocol = RedisSubscriber
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, hs: "HomeServer", outbound_redis_connection: txredisapi.RedisProtocol
|
|
|
|
):
|
|
|
|
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
# This sets the password on the RedisFactory base class (as
|
|
|
|
# SubscriberFactory constructor doesn't pass it through).
|
|
|
|
self.password = hs.config.redis.redis_password
|
|
|
|
|
|
|
|
self.handler = hs.get_tcp_replication()
|
|
|
|
self.stream_name = hs.hostname
|
|
|
|
|
|
|
|
self.outbound_redis_connection = outbound_redis_connection
|
|
|
|
|
|
|
|
def buildProtocol(self, addr):
|
|
|
|
p = super().buildProtocol(addr) # type: RedisSubscriber
|
|
|
|
|
|
|
|
# We do this here rather than add to the constructor of `RedisSubcriber`
|
|
|
|
# as to do so would involve overriding `buildProtocol` entirely, however
|
|
|
|
# the base method does some other things than just instantiating the
|
|
|
|
# protocol.
|
|
|
|
p.handler = self.handler
|
|
|
|
p.outbound_redis_connection = self.outbound_redis_connection
|
|
|
|
p.stream_name = self.stream_name
|
2020-05-04 07:04:09 -06:00
|
|
|
p.password = self.password
|
2020-04-22 06:07:41 -06:00
|
|
|
|
|
|
|
return p
|
2020-10-02 02:57:12 -06:00
|
|
|
|
|
|
|
|
|
|
|
def lazyConnection(
|
|
|
|
reactor,
|
|
|
|
host: str = "localhost",
|
|
|
|
port: int = 6379,
|
|
|
|
dbid: Optional[int] = None,
|
|
|
|
reconnect: bool = True,
|
|
|
|
charset: str = "utf-8",
|
|
|
|
password: Optional[str] = None,
|
|
|
|
connectTimeout: Optional[int] = None,
|
|
|
|
replyTimeout: Optional[int] = None,
|
|
|
|
convertNumbers: bool = True,
|
|
|
|
) -> txredisapi.RedisProtocol:
|
|
|
|
"""Equivalent to `txredisapi.lazyConnection`, except allows specifying a
|
|
|
|
reactor.
|
|
|
|
"""
|
|
|
|
|
|
|
|
isLazy = True
|
|
|
|
poolsize = 1
|
|
|
|
|
|
|
|
uuid = "%s:%d" % (host, port)
|
|
|
|
factory = txredisapi.RedisFactory(
|
|
|
|
uuid,
|
|
|
|
dbid,
|
|
|
|
poolsize,
|
|
|
|
isLazy,
|
|
|
|
txredisapi.ConnectionHandler,
|
|
|
|
charset,
|
|
|
|
password,
|
|
|
|
replyTimeout,
|
|
|
|
convertNumbers,
|
|
|
|
)
|
|
|
|
factory.continueTrying = reconnect
|
|
|
|
for x in range(poolsize):
|
|
|
|
reactor.connectTCP(host, port, factory, connectTimeout)
|
|
|
|
|
|
|
|
return factory.handler
|