MatrixSynapse/synapse/federation/replication.py

665 lines
22 KiB
Python
Raw Normal View History

2014-08-12 16:10:52 +02:00
# -*- coding: utf-8 -*-
2015-01-06 14:21:39 +01:00
# Copyright 2014, 2015 OpenMarket Ltd
2014-08-12 16:10:52 +02:00
#
# 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.
2014-08-12 16:10:52 +02:00
"""This layer is responsible for replicating with remote home servers using
a given transport.
"""
from twisted.internet import defer
2014-11-14 22:25:02 +01:00
from .units import Transaction, Edu
2014-10-31 15:00:14 +01:00
from .persistence import TransactionActions
from .transaction_queue import TransactionQueue
2014-08-12 16:10:52 +02:00
from synapse.util.logutils import log_function
from synapse.util.logcontext import PreserveLoggingContext
from synapse.events import FrozenEvent
2014-08-12 16:10:52 +02:00
import logging
logger = logging.getLogger(__name__)
class ReplicationLayer(object):
"""This layer is responsible for replicating with remote home servers over
the given transport. I.e., does the sending and receiving of PDUs to
remote home servers.
The layer communicates with the rest of the server via a registered
ReplicationHandler.
In more detail, the layer:
* Receives incoming data and processes it into transactions and pdus.
* Fetches any PDUs it thinks it might have missed.
* Keeps the current state for contexts up to date by applying the
suitable conflict resolution.
* Sends outgoing pdus wrapped in transactions.
* Fills out the references to previous pdus/transactions appropriately
for outgoing data.
"""
def __init__(self, hs, transport_layer):
self.server_name = hs.hostname
self.transport_layer = transport_layer
self.transport_layer.register_received_handler(self)
self.transport_layer.register_request_handler(self)
self.store = hs.get_datastore()
2014-10-31 10:59:59 +01:00
# self.pdu_actions = PduActions(self.store)
2014-08-12 16:10:52 +02:00
self.transaction_actions = TransactionActions(self.store)
self._transaction_queue = TransactionQueue(
2014-08-12 16:10:52 +02:00
hs, self.transaction_actions, transport_layer
)
self.handler = None
self.edu_handlers = {}
self.query_handlers = {}
2014-08-12 16:10:52 +02:00
self._order = 0
self._clock = hs.get_clock()
self.event_builder_factory = hs.get_event_builder_factory()
2014-11-14 22:25:02 +01:00
2014-08-12 16:10:52 +02:00
def set_handler(self, handler):
"""Sets the handler that the replication layer will use to communicate
receipt of new PDUs from other home servers. The required methods are
documented on :py:class:`.ReplicationHandler`.
"""
self.handler = handler
def register_edu_handler(self, edu_type, handler):
if edu_type in self.edu_handlers:
2014-11-04 16:09:34 +01:00
raise KeyError("Already have an EDU handler for %s" % (edu_type,))
2014-08-12 16:10:52 +02:00
self.edu_handlers[edu_type] = handler
def register_query_handler(self, query_type, handler):
"""Sets the handler callable that will be used to handle an incoming
federation Query of the given type.
Args:
query_type (str): Category name of the query, which should match
the string used by make_query.
handler (callable): Invoked to handle incoming queries of this type
handler is invoked as:
result = handler(args)
where 'args' is a dict mapping strings to strings of the query
arguments. It should return a Deferred that will eventually yield an
object to encode as JSON.
"""
if query_type in self.query_handlers:
2014-11-04 16:09:34 +01:00
raise KeyError(
"Already have a Query handler for %s" % (query_type,)
)
self.query_handlers[query_type] = handler
2014-08-12 16:10:52 +02:00
@log_function
def send_pdu(self, pdu, destinations):
2014-08-12 16:10:52 +02:00
"""Informs the replication layer about a new PDU generated within the
home server that should be transmitted to others.
TODO: Figure out when we should actually resolve the deferred.
Args:
pdu (Pdu): The new Pdu.
Returns:
Deferred: Completes when we have successfully processed the PDU
and replicated it to any interested remote home servers.
"""
order = self._order
self._order += 1
logger.debug("[%s] transaction_layer.enqueue_pdu... ", pdu.event_id)
2014-08-12 16:10:52 +02:00
# TODO, add errback, etc.
self._transaction_queue.enqueue_pdu(pdu, destinations, order)
2014-08-12 16:10:52 +02:00
2014-11-04 16:09:34 +01:00
logger.debug(
"[%s] transaction_layer.enqueue_pdu... done",
pdu.event_id
)
2014-08-12 16:10:52 +02:00
@log_function
def send_edu(self, destination, edu_type, content):
edu = Edu(
origin=self.server_name,
destination=destination,
edu_type=edu_type,
content=content,
)
# TODO, add errback, etc.
self._transaction_queue.enqueue_edu(edu)
return defer.succeed(None)
2014-08-12 16:10:52 +02:00
2014-11-04 15:17:55 +01:00
@log_function
def send_failure(self, failure, destination):
self._transaction_queue.enqueue_failure(failure, destination)
return defer.succeed(None)
@log_function
def make_query(self, destination, query_type, args,
retry_on_dns_fail=True):
"""Sends a federation Query to a remote homeserver of the given type
and arguments.
Args:
destination (str): Domain name of the remote homeserver
query_type (str): Category of the query type; should match the
handler name used in register_query_handler().
args (dict): Mapping of strings to strings containing the details
of the query request.
Returns:
a Deferred which will eventually yield a JSON object from the
response
"""
return self.transport_layer.make_query(
destination, query_type, args, retry_on_dns_fail=retry_on_dns_fail
)
2014-08-12 16:10:52 +02:00
@defer.inlineCallbacks
@log_function
def backfill(self, dest, context, limit, extremities):
2014-08-12 16:10:52 +02:00
"""Requests some more historic PDUs for the given context from the
given destination server.
Args:
dest (str): The remote home server to ask.
context (str): The context to backfill.
2014-08-12 16:10:52 +02:00
limit (int): The maximum number of PDUs to return.
extremities (list): List of PDU id and origins of the first pdus
we have seen from the context
2014-08-12 16:10:52 +02:00
Returns:
Deferred: Results in the received PDUs.
"""
logger.debug("backfill extrem=%s", extremities)
2014-08-12 16:10:52 +02:00
# If there are no extremeties then we've (probably) reached the start.
if not extremities:
return
transaction_data = yield self.transport_layer.backfill(
2014-08-12 16:10:52 +02:00
dest, context, extremities, limit)
logger.debug("backfill transaction_data=%s", repr(transaction_data))
2014-08-12 16:10:52 +02:00
transaction = Transaction(**transaction_data)
2014-11-14 22:25:02 +01:00
pdus = [
self.event_from_pdu_json(p, outlier=False)
for p in transaction.pdus
]
2014-08-12 16:10:52 +02:00
for pdu in pdus:
yield self._handle_new_pdu(dest, pdu, backfilled=True)
2014-08-12 16:10:52 +02:00
defer.returnValue(pdus)
@defer.inlineCallbacks
@log_function
def get_pdu(self, destination, event_id, outlier=False):
2014-08-12 16:10:52 +02:00
"""Requests the PDU with given origin and ID from the remote home
server.
This will persist the PDU locally upon receipt.
Args:
destination (str): Which home server to query
pdu_origin (str): The home server that originally sent the pdu.
event_id (str)
2014-08-12 16:10:52 +02:00
outlier (bool): Indicates whether the PDU is an `outlier`, i.e. if
it's from an arbitary point in the context as opposed to part
of the current block of PDUs. Defaults to `False`
Returns:
Deferred: Results in the requested PDU.
"""
transaction_data = yield self.transport_layer.get_event(
destination, event_id
)
2014-08-12 16:10:52 +02:00
transaction = Transaction(**transaction_data)
2014-11-14 22:25:02 +01:00
pdu_list = [
self.event_from_pdu_json(p, outlier=outlier)
for p in transaction.pdus
]
2014-08-12 16:10:52 +02:00
pdu = None
if pdu_list:
pdu = pdu_list[0]
yield self._handle_new_pdu(destination, pdu)
2014-08-12 16:10:52 +02:00
defer.returnValue(pdu)
@defer.inlineCallbacks
@log_function
def get_state_for_room(self, destination, room_id, event_id):
"""Requests all of the `current` state PDUs for a given room from
2014-08-12 16:10:52 +02:00
a remote home server.
Args:
destination (str): The remote homeserver to query for the state.
room_id (str): The id of the room we're interested in.
event_id (str): The id of the event we want the state at.
2014-08-12 16:10:52 +02:00
Returns:
Deferred: Results in a list of PDUs.
"""
result = yield self.transport_layer.get_room_state(
destination, room_id, event_id=event_id,
)
2014-08-12 16:10:52 +02:00
2014-11-14 22:25:02 +01:00
pdus = [
self.event_from_pdu_json(p, outlier=True) for p in result["pdus"]
]
auth_chain = [
2014-11-14 22:25:02 +01:00
self.event_from_pdu_json(p, outlier=True)
for p in result.get("auth_chain", [])
2014-11-14 22:25:02 +01:00
]
2014-08-12 16:10:52 +02:00
defer.returnValue((pdus, auth_chain))
2014-08-12 16:10:52 +02:00
@defer.inlineCallbacks
@log_function
def get_event_auth(self, destination, room_id, event_id):
res = yield self.transport_layer.get_event_auth(
destination, room_id, event_id,
)
auth_chain = [
self.event_from_pdu_json(p, outlier=True)
for p in res["auth_chain"]
]
auth_chain.sort(key=lambda e: e.depth)
defer.returnValue(auth_chain)
2014-08-12 16:10:52 +02:00
@defer.inlineCallbacks
@log_function
def on_backfill_request(self, origin, room_id, versions, limit):
2014-10-31 10:59:59 +01:00
pdus = yield self.handler.on_backfill_request(
origin, room_id, versions, limit
2014-10-31 10:59:59 +01:00
)
2014-08-12 16:10:52 +02:00
defer.returnValue((200, self._transaction_from_pdus(pdus).get_dict()))
@defer.inlineCallbacks
@log_function
def on_incoming_transaction(self, transaction_data):
transaction = Transaction(**transaction_data)
for p in transaction.pdus:
2014-10-17 17:50:04 +02:00
if "unsigned" in p:
unsigned = p["unsigned"]
if "age" in unsigned:
p["age"] = unsigned["age"]
if "age" in p:
p["age_ts"] = int(self._clock.time_msec()) - int(p["age"])
del p["age"]
2014-11-14 22:25:02 +01:00
pdu_list = [
self.event_from_pdu_json(p) for p in transaction.pdus
]
2014-08-12 16:10:52 +02:00
logger.debug("[%s] Got transaction", transaction.transaction_id)
response = yield self.transaction_actions.have_responded(transaction)
if response:
logger.debug("[%s] We've already responed to this request",
transaction.transaction_id)
defer.returnValue(response)
return
logger.debug("[%s] Transaction is new", transaction.transaction_id)
2014-08-12 16:10:52 +02:00
with PreserveLoggingContext():
dl = []
for pdu in pdu_list:
dl.append(self._handle_new_pdu(transaction.origin, pdu))
2014-08-12 16:10:52 +02:00
if hasattr(transaction, "edus"):
for edu in [Edu(**x) for x in transaction.edus]:
self.received_edu(
transaction.origin,
edu.edu_type,
edu.content
)
2014-08-12 16:10:52 +02:00
results = yield defer.DeferredList(dl)
2014-08-12 16:10:52 +02:00
ret = []
for r in results:
if r[0]:
ret.append({})
else:
logger.exception(r[1])
ret.append({"error": str(r[1])})
logger.debug("Returning: %s", str(ret))
yield self.transaction_actions.set_response(
transaction,
200, response
)
defer.returnValue((200, response))
def received_edu(self, origin, edu_type, content):
if edu_type in self.edu_handlers:
self.edu_handlers[edu_type](origin, content)
else:
logger.warn("Received EDU of type %s with no handler", edu_type)
@defer.inlineCallbacks
@log_function
def on_context_state_request(self, origin, room_id, event_id):
if event_id:
pdus = yield self.handler.get_state_for_pdu(
origin, room_id, event_id,
)
auth_chain = yield self.store.get_auth_chain(
[pdu.event_id for pdu in pdus]
)
else:
2014-10-31 11:47:34 +01:00
raise NotImplementedError("Specify an event")
2014-08-12 16:10:52 +02:00
defer.returnValue((200, {
"pdus": [pdu.get_pdu_json() for pdu in pdus],
"auth_chain": [pdu.get_pdu_json() for pdu in auth_chain],
}))
2014-08-12 16:10:52 +02:00
@defer.inlineCallbacks
@log_function
def on_pdu_request(self, origin, event_id):
pdu = yield self._get_persisted_pdu(origin, event_id)
2014-08-12 16:10:52 +02:00
if pdu:
defer.returnValue(
(200, self._transaction_from_pdus([pdu]).get_dict())
)
else:
defer.returnValue((404, ""))
@defer.inlineCallbacks
@log_function
def on_pull_request(self, origin, versions):
raise NotImplementedError("Pull transactions not implemented")
2014-10-31 10:59:59 +01:00
@defer.inlineCallbacks
def on_query_request(self, query_type, args):
if query_type in self.query_handlers:
response = yield self.query_handlers[query_type](args)
defer.returnValue((200, response))
else:
defer.returnValue(
(404, "No handler for Query type '%s'" % (query_type,))
)
@defer.inlineCallbacks
def on_make_join_request(self, room_id, user_id):
pdu = yield self.handler.on_make_join_request(room_id, user_id)
time_now = self._clock.time_msec()
defer.returnValue({"event": pdu.get_pdu_json(time_now)})
@defer.inlineCallbacks
def on_invite_request(self, origin, content):
2014-11-14 22:25:02 +01:00
pdu = self.event_from_pdu_json(content)
ret_pdu = yield self.handler.on_invite_request(origin, pdu)
time_now = self._clock.time_msec()
defer.returnValue((200, {"event": ret_pdu.get_pdu_json(time_now)}))
@defer.inlineCallbacks
def on_send_join_request(self, origin, content):
logger.debug("on_send_join_request: content: %s", content)
2014-11-14 22:25:02 +01:00
pdu = self.event_from_pdu_json(content)
logger.debug("on_send_join_request: pdu sigs: %s", pdu.signatures)
res_pdus = yield self.handler.on_send_join_request(origin, pdu)
time_now = self._clock.time_msec()
defer.returnValue((200, {
"state": [p.get_pdu_json(time_now) for p in res_pdus["state"]],
2014-11-20 18:26:36 +01:00
"auth_chain": [
p.get_pdu_json(time_now) for p in res_pdus["auth_chain"]
],
}))
2014-11-07 16:35:53 +01:00
@defer.inlineCallbacks
def on_event_auth(self, origin, room_id, event_id):
time_now = self._clock.time_msec()
2014-11-07 16:35:53 +01:00
auth_pdus = yield self.handler.on_event_auth(event_id)
defer.returnValue((200, {
"auth_chain": [a.get_pdu_json(time_now) for a in auth_pdus],
}))
2014-11-07 16:35:53 +01:00
@defer.inlineCallbacks
def make_join(self, destination, room_id, user_id):
ret = yield self.transport_layer.make_join(
destination, room_id, user_id
)
pdu_dict = ret["event"]
logger.debug("Got response to make_join: %s", pdu_dict)
2014-11-14 22:25:02 +01:00
defer.returnValue(self.event_from_pdu_json(pdu_dict))
@defer.inlineCallbacks
def send_join(self, destination, pdu):
2014-11-20 18:26:36 +01:00
time_now = self._clock.time_msec()
_, content = yield self.transport_layer.send_join(
destination=destination,
room_id=pdu.room_id,
event_id=pdu.event_id,
content=pdu.get_pdu_json(time_now),
)
logger.debug("Got content: %s", content)
2014-11-14 22:25:02 +01:00
state = [
self.event_from_pdu_json(p, outlier=True)
for p in content.get("state", [])
]
auth_chain = [
self.event_from_pdu_json(p, outlier=True)
for p in content.get("auth_chain", [])
]
auth_chain.sort(key=lambda e: e.depth)
defer.returnValue({
"state": state,
"auth_chain": auth_chain,
})
@defer.inlineCallbacks
def send_invite(self, destination, room_id, event_id, pdu):
time_now = self._clock.time_msec()
code, content = yield self.transport_layer.send_invite(
destination=destination,
room_id=room_id,
event_id=event_id,
content=pdu.get_pdu_json(time_now),
)
pdu_dict = content["event"]
logger.debug("Got response to send_invite: %s", pdu_dict)
2014-11-14 22:25:02 +01:00
defer.returnValue(self.event_from_pdu_json(pdu_dict))
2014-08-12 16:10:52 +02:00
@log_function
def _get_persisted_pdu(self, origin, event_id, do_auth=True):
2014-08-12 16:10:52 +02:00
""" Get a PDU from the database with given origin and id.
Returns:
Deferred: Results in a `Pdu`.
"""
return self.handler.get_persisted_pdu(
origin, event_id, do_auth=do_auth
)
2014-08-12 16:10:52 +02:00
def _transaction_from_pdus(self, pdu_list):
"""Returns a new Transaction containing the given PDUs suitable for
transmission.
"""
time_now = self._clock.time_msec()
pdus = [p.get_pdu_json(time_now) for p in pdu_list]
2014-08-12 16:10:52 +02:00
return Transaction(
origin=self.server_name,
pdus=pdus,
origin_server_ts=int(time_now),
2014-08-12 16:10:52 +02:00
destination=None,
)
@defer.inlineCallbacks
@log_function
def _handle_new_pdu(self, origin, pdu, backfilled=False):
2014-08-12 16:10:52 +02:00
# We reprocess pdus when we have seen them only as outliers
existing = yield self._get_persisted_pdu(
origin, pdu.event_id, do_auth=False
)
2014-08-12 16:10:52 +02:00
2014-12-08 10:08:26 +01:00
already_seen = (
existing and (
not existing.internal_metadata.is_outlier()
or pdu.internal_metadata.is_outlier()
2014-12-08 10:08:26 +01:00
)
)
if already_seen:
logger.debug("Already seen pdu %s", pdu.event_id)
2014-08-12 16:10:52 +02:00
defer.returnValue({})
return
state = None
auth_chain = []
# We need to make sure we have all the auth events.
# for e_id, _ in pdu.auth_events:
# exists = yield self._get_persisted_pdu(
# origin,
# e_id,
# do_auth=False
# )
#
# if not exists:
# try:
# logger.debug(
# "_handle_new_pdu fetch missing auth event %s from %s",
# e_id,
# origin,
# )
#
# yield self.get_pdu(
# origin,
# event_id=e_id,
# outlier=True,
# )
#
# logger.debug("Processed pdu %s", e_id)
# except:
# logger.warn(
# "Failed to get auth event %s from %s",
# e_id,
# origin
# )
2014-08-12 16:10:52 +02:00
# Get missing pdus if necessary.
if not pdu.internal_metadata.is_outlier():
# We only backfill backwards to the min depth.
2014-10-31 11:47:34 +01:00
min_depth = yield self.handler.get_min_depth_for_context(
pdu.room_id
2014-10-31 11:47:34 +01:00
)
2014-08-12 16:10:52 +02:00
logger.debug(
"_handle_new_pdu min_depth for %s: %d",
pdu.room_id, min_depth
)
2014-08-12 16:10:52 +02:00
if min_depth and pdu.depth > min_depth:
for event_id, hashes in pdu.prev_events:
exists = yield self._get_persisted_pdu(
origin,
event_id,
do_auth=False
)
2014-08-12 16:10:52 +02:00
if not exists:
logger.debug(
"_handle_new_pdu requesting pdu %s",
event_id
)
2014-08-12 16:10:52 +02:00
try:
yield self.get_pdu(
origin,
event_id=event_id,
2014-08-12 16:10:52 +02:00
)
logger.debug("Processed pdu %s", event_id)
2014-08-12 16:10:52 +02:00
except:
# TODO(erikj): Do some more intelligent retries.
logger.exception("Failed to get PDU")
else:
# We need to get the state at this event, since we have reached
# a backward extremity edge.
logger.debug(
"_handle_new_pdu getting state for %s",
pdu.room_id
)
state, auth_chain = yield self.get_state_for_room(
origin, pdu.room_id, pdu.event_id,
)
2014-08-12 16:10:52 +02:00
2014-08-19 15:20:03 +02:00
if not backfilled:
ret = yield self.handler.on_receive_pdu(
origin,
pdu,
backfilled=backfilled,
state=state,
auth_chain=auth_chain,
)
2014-08-19 15:20:03 +02:00
else:
ret = None
2014-08-12 16:10:52 +02:00
2014-10-31 10:59:59 +01:00
# yield self.pdu_actions.mark_as_processed(pdu)
2014-08-12 16:10:52 +02:00
defer.returnValue(ret)
def __str__(self):
return "<ReplicationLayer(%s)>" % self.server_name
2014-11-14 22:25:02 +01:00
def event_from_pdu_json(self, pdu_json, outlier=False):
event = FrozenEvent(
pdu_json
2014-11-14 22:25:02 +01:00
)
event.internal_metadata.outlier = outlier
return event