2017-03-27 11:10:55 -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.
|
|
|
|
"""A replication client for use by synapse workers.
|
|
|
|
"""
|
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
import logging
|
2020-05-01 10:21:35 -04:00
|
|
|
from typing import TYPE_CHECKING
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2017-03-27 11:10:55 -04:00
|
|
|
from twisted.internet.protocol import ReconnectingClientFactory
|
|
|
|
|
2019-11-04 07:40:18 -05:00
|
|
|
from synapse.replication.slave.storage._base import BaseSlavedStore
|
2020-04-06 04:58:42 -04:00
|
|
|
from synapse.replication.tcp.protocol import ClientReplicationStreamProtocol
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from synapse.server import HomeServer
|
|
|
|
from synapse.replication.tcp.handler import ReplicationCommandHandler
|
2017-03-27 11:10:55 -04:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-04-22 08:07:41 -04:00
|
|
|
class DirectTcpReplicationClientFactory(ReconnectingClientFactory):
|
2017-03-27 11:10:55 -04:00
|
|
|
"""Factory for building connections to the master. Will reconnect if the
|
|
|
|
connection is lost.
|
|
|
|
|
2020-04-06 04:58:42 -04:00
|
|
|
Accepts a handler that is passed to `ClientReplicationStreamProtocol`.
|
2017-03-27 11:10:55 -04:00
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2020-01-03 09:19:09 -05:00
|
|
|
initialDelay = 0.1
|
|
|
|
maxDelay = 1 # Try at least once every N seconds
|
2017-03-27 11:10:55 -04:00
|
|
|
|
2020-04-06 04:58:42 -04:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
hs: "HomeServer",
|
|
|
|
client_name: str,
|
|
|
|
command_handler: "ReplicationCommandHandler",
|
|
|
|
):
|
2017-03-27 11:10:55 -04:00
|
|
|
self.client_name = client_name
|
2020-04-06 04:58:42 -04:00
|
|
|
self.command_handler = command_handler
|
2017-03-27 11:10:55 -04:00
|
|
|
self.server_name = hs.config.server_name
|
2020-03-25 10:54:01 -04:00
|
|
|
self.hs = hs
|
2017-03-27 11:10:55 -04:00
|
|
|
self._clock = hs.get_clock() # As self.clock is defined in super class
|
|
|
|
|
2018-06-25 09:08:28 -04:00
|
|
|
hs.get_reactor().addSystemEventTrigger("before", "shutdown", self.stopTrying)
|
2017-03-27 11:10:55 -04:00
|
|
|
|
|
|
|
def startedConnecting(self, connector):
|
|
|
|
logger.info("Connecting to replication: %r", connector.getDestination())
|
|
|
|
|
|
|
|
def buildProtocol(self, addr):
|
|
|
|
logger.info("Connected to replication: %r", addr)
|
|
|
|
return ClientReplicationStreamProtocol(
|
2020-04-06 04:58:42 -04:00
|
|
|
self.hs,
|
|
|
|
self.client_name,
|
|
|
|
self.server_name,
|
|
|
|
self._clock,
|
|
|
|
self.command_handler,
|
2017-03-27 11:10:55 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
def clientConnectionLost(self, connector, reason):
|
|
|
|
logger.error("Lost replication conn: %r", reason)
|
|
|
|
ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
|
|
|
|
|
|
|
|
def clientConnectionFailed(self, connector, reason):
|
|
|
|
logger.error("Failed to connect to replication: %r", reason)
|
2019-06-20 05:32:02 -04:00
|
|
|
ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
|
2017-03-27 11:10:55 -04:00
|
|
|
|
|
|
|
|
2020-04-06 04:58:42 -04:00
|
|
|
class ReplicationDataHandler:
|
|
|
|
"""Handles incoming stream updates from replication.
|
2017-03-27 11:10:55 -04:00
|
|
|
|
2020-04-06 04:58:42 -04:00
|
|
|
This instance notifies the slave data store about updates. Can be subclassed
|
|
|
|
to handle updates in additional ways.
|
2017-03-27 11:10:55 -04:00
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2019-11-04 07:40:18 -05:00
|
|
|
def __init__(self, store: BaseSlavedStore):
|
2017-03-27 11:10:55 -04:00
|
|
|
self.store = store
|
|
|
|
|
2020-04-06 04:58:42 -04:00
|
|
|
async def on_rdata(self, stream_name: str, token: int, rows: list):
|
2019-03-27 17:12:36 -04:00
|
|
|
"""Called to handle a batch of replication data with a given stream token.
|
2017-03-27 11:10:55 -04:00
|
|
|
|
2019-03-27 03:40:32 -04:00
|
|
|
By default this just pokes the slave store. Can be overridden in subclasses to
|
2019-03-27 17:12:36 -04:00
|
|
|
handle more.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
stream_name (str): name of the replication stream for this batch of rows
|
|
|
|
token (int): stream token for this batch of rows
|
2019-03-27 03:40:32 -04:00
|
|
|
rows (list): a list of Stream.ROW_TYPE objects as returned by
|
|
|
|
Stream.parse_row.
|
2017-03-27 11:10:55 -04:00
|
|
|
"""
|
2020-01-16 04:16:12 -05:00
|
|
|
self.store.process_replication_rows(stream_name, token, rows)
|
2017-03-27 11:10:55 -04:00
|
|
|
|
2020-04-06 04:58:42 -04:00
|
|
|
async def on_position(self, stream_name: str, token: int):
|
|
|
|
self.store.process_replication_rows(stream_name, token, [])
|
2019-02-26 10:04:34 -05:00
|
|
|
|
2020-04-06 04:58:42 -04:00
|
|
|
def on_remote_server_up(self, server: str):
|
|
|
|
"""Called when get a new REMOTE_SERVER_UP command."""
|