2019-03-13 16:02:56 -04:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2019-03-13 16:02:56 -04:00
|
|
|
#
|
|
|
|
#
|
|
|
|
import logging
|
2020-09-03 14:23:07 -04:00
|
|
|
from typing import TYPE_CHECKING, List
|
2019-03-13 16:02:56 -04:00
|
|
|
|
2020-10-01 06:09:12 -04:00
|
|
|
from prometheus_client import Gauge
|
|
|
|
|
2022-05-27 07:14:36 -04:00
|
|
|
from synapse.api.constants import EduTypes
|
2019-03-13 16:02:56 -04:00
|
|
|
from synapse.api.errors import HttpResponseException
|
2020-02-07 08:56:38 -05:00
|
|
|
from synapse.events import EventBase
|
2019-03-13 16:02:56 -04:00
|
|
|
from synapse.federation.persistence import TransactionActions
|
2020-02-07 08:56:38 -05:00
|
|
|
from synapse.federation.units import Edu, Transaction
|
2019-08-22 13:21:10 -04:00
|
|
|
from synapse.logging.opentracing import (
|
|
|
|
extract_text_map,
|
|
|
|
set_tag,
|
|
|
|
start_active_span_follows_from,
|
|
|
|
tags,
|
2019-09-05 09:41:04 -04:00
|
|
|
whitelisted_homeserver,
|
2019-08-22 13:21:10 -04:00
|
|
|
)
|
2021-08-06 09:39:59 -04:00
|
|
|
from synapse.types import JsonDict
|
2020-08-19 07:26:03 -04:00
|
|
|
from synapse.util import json_decoder
|
2019-03-13 16:02:56 -04:00
|
|
|
from synapse.util.metrics import measure_func
|
|
|
|
|
2020-05-05 09:15:57 -04:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
import synapse.server
|
|
|
|
|
2019-03-13 16:02:56 -04:00
|
|
|
logger = logging.getLogger(__name__)
|
2022-01-20 08:38:44 -05:00
|
|
|
issue_8631_logger = logging.getLogger("synapse.8631_debug")
|
2019-03-13 16:02:56 -04:00
|
|
|
|
2021-03-04 11:40:18 -05:00
|
|
|
last_pdu_ts_metric = Gauge(
|
|
|
|
"synapse_federation_last_sent_pdu_time",
|
|
|
|
"The timestamp of the last PDU which was successfully sent to the given domain",
|
2020-10-01 06:09:12 -04:00
|
|
|
labelnames=("server_name",),
|
|
|
|
)
|
|
|
|
|
2019-03-13 16:02:56 -04:00
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
class TransactionManager:
|
2019-03-13 16:02:56 -04:00
|
|
|
"""Helper class which handles building and sending transactions
|
|
|
|
|
|
|
|
shared between PerDestinationQueue objects
|
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2020-02-07 08:56:38 -05:00
|
|
|
def __init__(self, hs: "synapse.server.HomeServer"):
|
2019-03-13 16:02:56 -04:00
|
|
|
self._server_name = hs.hostname
|
|
|
|
self.clock = hs.get_clock() # nb must be called this for @measure_func
|
2022-02-23 06:04:02 -05:00
|
|
|
self._store = hs.get_datastores().main
|
2019-03-13 16:02:56 -04:00
|
|
|
self._transaction_actions = TransactionActions(self._store)
|
|
|
|
self._transport_layer = hs.get_federation_transport_client()
|
|
|
|
|
2020-10-01 06:09:12 -04:00
|
|
|
self._federation_metrics_domains = (
|
2021-04-14 14:09:08 -04:00
|
|
|
hs.config.federation.federation_metrics_domains
|
2020-10-01 06:09:12 -04:00
|
|
|
)
|
|
|
|
|
2019-03-13 16:02:56 -04:00
|
|
|
# HACK to get unique tx id
|
|
|
|
self._next_txn_id = int(self.clock.time_msec())
|
|
|
|
|
|
|
|
@measure_func("_send_new_transaction")
|
2020-02-07 08:56:38 -05:00
|
|
|
async def send_new_transaction(
|
2020-09-03 14:23:07 -04:00
|
|
|
self,
|
|
|
|
destination: str,
|
|
|
|
pdus: List[EventBase],
|
|
|
|
edus: List[Edu],
|
2021-03-15 10:42:40 -04:00
|
|
|
) -> None:
|
2020-09-03 14:23:07 -04:00
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
destination: The destination to send to (e.g. 'example.org')
|
|
|
|
pdus: In-order list of PDUs to send
|
|
|
|
edus: List of EDUs to send
|
|
|
|
"""
|
2019-03-13 16:02:56 -04:00
|
|
|
|
2019-08-22 13:21:10 -04:00
|
|
|
# Make a transaction-sending opentracing span. This span follows on from
|
|
|
|
# all the edus in that transaction. This needs to be done since there is
|
|
|
|
# no active span here, so if the edus were not received by the remote the
|
|
|
|
# span would have no causality and it would be forgotten.
|
2019-03-13 16:02:56 -04:00
|
|
|
|
2019-09-05 09:41:04 -04:00
|
|
|
span_contexts = []
|
|
|
|
keep_destination = whitelisted_homeserver(destination)
|
|
|
|
|
2020-09-03 14:23:07 -04:00
|
|
|
for edu in edus:
|
2019-09-05 10:07:17 -04:00
|
|
|
context = edu.get_context()
|
|
|
|
if context:
|
2020-08-19 07:26:03 -04:00
|
|
|
span_contexts.append(extract_text_map(json_decoder.decode(context)))
|
2019-09-05 09:41:04 -04:00
|
|
|
if keep_destination:
|
|
|
|
edu.strip_context()
|
2019-03-13 16:02:56 -04:00
|
|
|
|
2019-08-22 13:21:10 -04:00
|
|
|
with start_active_span_follows_from("send_transaction", span_contexts):
|
|
|
|
logger.debug("TX [%s] _attempt_new_transaction", destination)
|
2019-03-13 16:02:56 -04:00
|
|
|
|
2019-08-22 13:21:10 -04:00
|
|
|
txn_id = str(self._next_txn_id)
|
2019-03-13 16:02:56 -04:00
|
|
|
|
2019-08-22 13:21:10 -04:00
|
|
|
logger.debug(
|
2019-11-21 07:00:14 -05:00
|
|
|
"TX [%s] {%s} Attempting new transaction (pdus: %d, edus: %d)",
|
2019-08-22 13:21:10 -04:00
|
|
|
destination,
|
|
|
|
txn_id,
|
|
|
|
len(pdus),
|
|
|
|
len(edus),
|
|
|
|
)
|
2019-03-13 16:02:56 -04:00
|
|
|
|
2021-08-06 09:39:59 -04:00
|
|
|
transaction = Transaction(
|
2019-08-22 13:21:10 -04:00
|
|
|
origin_server_ts=int(self.clock.time_msec()),
|
|
|
|
transaction_id=txn_id,
|
|
|
|
origin=self._server_name,
|
|
|
|
destination=destination,
|
2021-08-06 09:39:59 -04:00
|
|
|
pdus=[p.get_pdu_json() for p in pdus],
|
|
|
|
edus=[edu.get_dict() for edu in edus],
|
2019-03-13 16:02:56 -04:00
|
|
|
)
|
|
|
|
|
2019-08-22 13:21:10 -04:00
|
|
|
self._next_txn_id += 1
|
2019-03-13 16:02:56 -04:00
|
|
|
|
2019-08-22 13:21:10 -04:00
|
|
|
logger.info(
|
2019-11-21 07:00:14 -05:00
|
|
|
"TX [%s] {%s} Sending transaction [%s], (PDUs: %d, EDUs: %d)",
|
2019-08-22 13:21:10 -04:00
|
|
|
destination,
|
|
|
|
txn_id,
|
|
|
|
transaction.transaction_id,
|
|
|
|
len(pdus),
|
|
|
|
len(edus),
|
|
|
|
)
|
2022-01-20 08:38:44 -05:00
|
|
|
if issue_8631_logger.isEnabledFor(logging.DEBUG):
|
2022-05-27 07:14:36 -04:00
|
|
|
DEVICE_UPDATE_EDUS = {
|
|
|
|
EduTypes.DEVICE_LIST_UPDATE,
|
|
|
|
EduTypes.SIGNING_KEY_UPDATE,
|
|
|
|
}
|
2022-01-20 08:38:44 -05:00
|
|
|
device_list_updates = [
|
|
|
|
edu.content for edu in edus if edu.edu_type in DEVICE_UPDATE_EDUS
|
|
|
|
]
|
|
|
|
if device_list_updates:
|
|
|
|
issue_8631_logger.debug(
|
|
|
|
"about to send txn [%s] including device list updates: %s",
|
|
|
|
transaction.transaction_id,
|
|
|
|
device_list_updates,
|
|
|
|
)
|
2019-03-13 16:02:56 -04:00
|
|
|
|
2019-08-22 13:21:10 -04:00
|
|
|
# Actually send the transaction
|
|
|
|
|
|
|
|
# FIXME (erikj): This is a bit of a hack to make the Pdu age
|
|
|
|
# keys work
|
2020-10-01 06:09:12 -04:00
|
|
|
# FIXME (richardv): I also believe it no longer works. We (now?) store
|
|
|
|
# "age_ts" in "unsigned" rather than at the top level. See
|
|
|
|
# https://github.com/matrix-org/synapse/issues/8429.
|
2021-08-06 09:39:59 -04:00
|
|
|
def json_data_cb() -> JsonDict:
|
2019-08-22 13:21:10 -04:00
|
|
|
data = transaction.get_dict()
|
|
|
|
now = int(self.clock.time_msec())
|
|
|
|
if "pdus" in data:
|
|
|
|
for p in data["pdus"]:
|
|
|
|
if "age_ts" in p:
|
|
|
|
unsigned = p.setdefault("unsigned", {})
|
|
|
|
unsigned["age"] = now - int(p["age_ts"])
|
|
|
|
del p["age_ts"]
|
|
|
|
return data
|
|
|
|
|
|
|
|
try:
|
2020-02-07 08:56:38 -05:00
|
|
|
response = await self._transport_layer.send_transaction(
|
2019-08-22 13:21:10 -04:00
|
|
|
transaction, json_data_cb
|
|
|
|
)
|
|
|
|
except HttpResponseException as e:
|
|
|
|
code = e.code
|
|
|
|
|
2021-03-15 10:42:40 -04:00
|
|
|
set_tag(tags.ERROR, True)
|
|
|
|
|
|
|
|
logger.info("TX [%s] {%s} got %d response", destination, txn_id, code)
|
|
|
|
raise
|
|
|
|
|
|
|
|
logger.info("TX [%s] {%s} got 200 response", destination, txn_id)
|
|
|
|
|
|
|
|
for e_id, r in response.get("pdus", {}).items():
|
|
|
|
if "error" in r:
|
2019-10-31 06:23:24 -04:00
|
|
|
logger.warning(
|
2021-03-15 10:42:40 -04:00
|
|
|
"TX [%s] {%s} Remote returned error for %s: %s",
|
2019-03-13 16:02:56 -04:00
|
|
|
destination,
|
|
|
|
txn_id,
|
2021-03-15 10:42:40 -04:00
|
|
|
e_id,
|
|
|
|
r,
|
2019-03-13 16:02:56 -04:00
|
|
|
)
|
|
|
|
|
2021-03-15 10:42:40 -04:00
|
|
|
if pdus and destination in self._federation_metrics_domains:
|
2020-10-01 06:09:12 -04:00
|
|
|
last_pdu = pdus[-1]
|
2021-03-04 11:40:18 -05:00
|
|
|
last_pdu_ts_metric.labels(server_name=destination).set(
|
|
|
|
last_pdu.origin_server_ts / 1000
|
2020-10-01 06:09:12 -04:00
|
|
|
)
|