2015-01-22 11:37:08 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 23:26:29 -05:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2015-01-22 11:37:08 -05: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.
|
2017-03-22 20:12:21 -04:00
|
|
|
import datetime
|
2018-07-09 02:09:20 -04:00
|
|
|
import logging
|
2015-01-22 11:37:08 -05:00
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
from six import itervalues
|
2015-01-22 11:37:08 -05:00
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
from prometheus_client import Counter
|
|
|
|
|
|
|
|
from twisted.internet import defer
|
2015-01-22 11:37:08 -05:00
|
|
|
|
2015-03-04 13:15:34 -05:00
|
|
|
import synapse.metrics
|
2018-07-09 02:09:20 -04:00
|
|
|
from synapse.api.errors import FederationDeniedError, HttpResponseException
|
|
|
|
from synapse.handlers.presence import format_user_presence_state, get_interested_remotes
|
2018-05-21 20:47:37 -04:00
|
|
|
from synapse.metrics import (
|
2018-07-09 02:09:20 -04:00
|
|
|
LaterGauge,
|
2018-08-07 14:09:48 -04:00
|
|
|
event_processing_loop_counter,
|
|
|
|
event_processing_loop_room_count,
|
2018-07-09 02:09:20 -04:00
|
|
|
events_processed_counter,
|
2018-05-22 17:54:22 -04:00
|
|
|
sent_edus_counter,
|
|
|
|
sent_transactions_counter,
|
|
|
|
)
|
2018-07-18 05:50:33 -04:00
|
|
|
from synapse.metrics.background_process_metrics import run_as_background_process
|
|
|
|
from synapse.util import logcontext
|
2018-07-09 02:09:20 -04:00
|
|
|
from synapse.util.metrics import measure_func
|
|
|
|
from synapse.util.retryutils import NotRetryingDestination, get_retry_limiter
|
2018-05-21 20:47:37 -04:00
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
from .persistence import TransactionActions
|
|
|
|
from .units import Edu, Transaction
|
2015-01-22 11:37:08 -05:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2018-06-21 04:39:58 -04:00
|
|
|
sent_pdus_destination_dist_count = Counter(
|
|
|
|
"synapse_federation_client_sent_pdu_destinations:count", ""
|
|
|
|
)
|
|
|
|
sent_pdus_destination_dist_total = Counter(
|
|
|
|
"synapse_federation_client_sent_pdu_destinations:total", ""
|
2018-05-22 17:54:22 -04:00
|
|
|
)
|
2018-01-15 13:20:30 -05:00
|
|
|
|
2015-01-22 11:37:08 -05:00
|
|
|
|
|
|
|
class TransactionQueue(object):
|
|
|
|
"""This class makes sure we only have one transaction in flight at
|
|
|
|
a time for a given destination.
|
|
|
|
|
|
|
|
It batches pending PDUs into single transactions.
|
|
|
|
"""
|
|
|
|
|
2016-11-16 09:15:50 -05:00
|
|
|
def __init__(self, hs):
|
2018-08-17 11:08:45 -04:00
|
|
|
self.hs = hs
|
2015-01-22 11:37:08 -05:00
|
|
|
self.server_name = hs.hostname
|
2015-01-26 05:45:24 -05:00
|
|
|
|
|
|
|
self.store = hs.get_datastore()
|
2016-11-17 10:46:44 -05:00
|
|
|
self.state = hs.get_state_handler()
|
2015-01-26 05:45:24 -05:00
|
|
|
self.transaction_actions = TransactionActions(self.store)
|
|
|
|
|
2016-11-16 09:15:50 -05:00
|
|
|
self.transport_layer = hs.get_federation_transport_client()
|
2015-01-22 11:37:08 -05:00
|
|
|
|
2016-08-10 05:56:38 -04:00
|
|
|
self.clock = hs.get_clock()
|
2017-01-05 06:26:30 -05:00
|
|
|
self.is_mine_id = hs.is_mine_id
|
2015-01-22 11:37:08 -05:00
|
|
|
|
|
|
|
# Is a mapping from destinations -> deferreds. Used to keep track
|
|
|
|
# of which destinations have transactions in flight and when they are
|
|
|
|
# done
|
|
|
|
self.pending_transactions = {}
|
|
|
|
|
2018-05-22 17:54:22 -04:00
|
|
|
LaterGauge(
|
2018-05-23 14:03:51 -04:00
|
|
|
"synapse_federation_transaction_queue_pending_destinations",
|
2018-05-22 17:54:22 -04:00
|
|
|
"",
|
|
|
|
[],
|
2015-03-10 14:24:52 -04:00
|
|
|
lambda: len(self.pending_transactions),
|
|
|
|
)
|
|
|
|
|
2015-01-22 11:37:08 -05:00
|
|
|
# Is a mapping from destination -> list of
|
|
|
|
# tuple(pending pdus, deferred, order)
|
2015-03-04 13:15:34 -05:00
|
|
|
self.pending_pdus_by_dest = pdus = {}
|
2015-01-22 11:37:08 -05:00
|
|
|
# destination -> list of tuple(edu, deferred)
|
2015-03-04 13:15:34 -05:00
|
|
|
self.pending_edus_by_dest = edus = {}
|
2015-01-22 11:37:08 -05:00
|
|
|
|
2017-04-11 10:15:34 -04:00
|
|
|
# Map of user_id -> UserPresenceState for all the pending presence
|
|
|
|
# to be sent out by user_id. Entries here get processed and put in
|
|
|
|
# pending_presence_by_dest
|
2017-04-10 11:48:30 -04:00
|
|
|
self.pending_presence = {}
|
2017-04-12 05:17:10 -04:00
|
|
|
|
2017-04-11 10:15:34 -04:00
|
|
|
# Map of destination -> user_id -> UserPresenceState of pending presence
|
|
|
|
# to be sent to each destinations
|
2016-09-09 10:59:08 -04:00
|
|
|
self.pending_presence_by_dest = presence = {}
|
2017-04-11 10:15:34 -04:00
|
|
|
|
|
|
|
# Pending EDUs by their "key". Keyed EDUs are EDUs that get clobbered
|
|
|
|
# based on their key (e.g. typing events by room_id)
|
|
|
|
# Map of destination -> (edu_type, key) -> Edu
|
2016-09-09 10:59:08 -04:00
|
|
|
self.pending_edus_keyed_by_dest = edus_keyed = {}
|
|
|
|
|
2018-05-21 20:47:37 -04:00
|
|
|
LaterGauge(
|
2018-05-23 14:03:51 -04:00
|
|
|
"synapse_federation_transaction_queue_pending_pdus",
|
2018-05-22 17:54:22 -04:00
|
|
|
"",
|
|
|
|
[],
|
2015-03-10 14:24:52 -04:00
|
|
|
lambda: sum(map(len, pdus.values())),
|
|
|
|
)
|
2018-05-21 20:47:37 -04:00
|
|
|
LaterGauge(
|
2018-05-23 14:03:51 -04:00
|
|
|
"synapse_federation_transaction_queue_pending_edus",
|
2018-05-22 17:54:22 -04:00
|
|
|
"",
|
|
|
|
[],
|
2016-09-09 10:59:08 -04:00
|
|
|
lambda: (
|
|
|
|
sum(map(len, edus.values()))
|
|
|
|
+ sum(map(len, presence.values()))
|
|
|
|
+ sum(map(len, edus_keyed.values()))
|
|
|
|
),
|
2015-03-10 14:24:52 -04:00
|
|
|
)
|
|
|
|
|
2017-03-15 08:16:55 -04:00
|
|
|
# destination -> stream_id of last successfully sent to-device message.
|
|
|
|
# NB: may be a long or an int.
|
2016-09-07 10:27:07 -04:00
|
|
|
self.last_device_stream_id_by_dest = {}
|
2017-03-15 08:16:55 -04:00
|
|
|
|
|
|
|
# destination -> stream_id of last successfully sent device list
|
|
|
|
# update.
|
2017-01-25 09:27:27 -05:00
|
|
|
self.last_device_list_stream_id_by_dest = {}
|
2016-09-07 10:27:07 -04:00
|
|
|
|
2015-01-22 11:37:08 -05:00
|
|
|
# HACK to get unique tx id
|
2016-08-10 05:56:38 -04:00
|
|
|
self._next_txn_id = int(self.clock.time_msec())
|
2015-01-22 11:37:08 -05:00
|
|
|
|
2016-11-16 08:41:21 -05:00
|
|
|
self._order = 1
|
|
|
|
|
2016-11-17 10:46:44 -05:00
|
|
|
self._is_processing = False
|
2016-11-21 06:28:37 -05:00
|
|
|
self._last_poked_id = -1
|
2016-11-17 10:46:44 -05:00
|
|
|
|
2017-04-10 11:48:30 -04:00
|
|
|
self._processing_pending_presence = False
|
|
|
|
|
2016-11-17 10:46:44 -05:00
|
|
|
def notify_new_events(self, current_id):
|
2016-11-21 11:55:23 -05:00
|
|
|
"""This gets called when we have some new events we might want to
|
|
|
|
send out to other servers.
|
|
|
|
"""
|
2016-11-21 06:28:37 -05:00
|
|
|
self._last_poked_id = max(current_id, self._last_poked_id)
|
|
|
|
|
2016-11-17 10:46:44 -05:00
|
|
|
if self._is_processing:
|
|
|
|
return
|
|
|
|
|
2018-07-18 05:50:33 -04:00
|
|
|
# fire off a processing loop in the background
|
|
|
|
run_as_background_process(
|
2018-07-18 09:35:24 -04:00
|
|
|
"process_event_queue_for_federation",
|
2018-07-18 05:50:33 -04:00
|
|
|
self._process_event_queue_loop,
|
|
|
|
)
|
2017-11-28 09:37:11 -05:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _process_event_queue_loop(self):
|
2016-11-17 10:46:44 -05:00
|
|
|
try:
|
|
|
|
self._is_processing = True
|
|
|
|
while True:
|
2016-11-21 06:28:37 -05:00
|
|
|
last_token = yield self.store.get_federation_out_pos("events")
|
|
|
|
next_token, events = yield self.store.get_all_new_events_stream(
|
2018-04-09 06:45:22 -04:00
|
|
|
last_token, self._last_poked_id, limit=100,
|
2016-11-17 10:46:44 -05:00
|
|
|
)
|
|
|
|
|
2016-11-21 06:28:37 -05:00
|
|
|
logger.debug("Handling %s -> %s", last_token, next_token)
|
|
|
|
|
|
|
|
if not events and next_token >= self._last_poked_id:
|
2016-11-17 10:46:44 -05:00
|
|
|
break
|
|
|
|
|
2018-04-09 06:45:22 -04:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def handle_event(event):
|
2017-01-05 06:26:30 -05:00
|
|
|
# Only send events for this server.
|
|
|
|
send_on_behalf_of = event.internal_metadata.get_send_on_behalf_of()
|
|
|
|
is_mine = self.is_mine_id(event.event_id)
|
|
|
|
if not is_mine and send_on_behalf_of is None:
|
2018-04-09 06:45:22 -04:00
|
|
|
return
|
2017-01-05 06:26:30 -05:00
|
|
|
|
2018-02-14 08:52:03 -05:00
|
|
|
try:
|
|
|
|
# Get the state from before the event.
|
|
|
|
# We need to make sure that this is the state from before
|
|
|
|
# the event and not from after it.
|
|
|
|
# Otherwise if the last member on a server in a room is
|
|
|
|
# banned then it won't receive the event because it won't
|
|
|
|
# be in the room after the ban.
|
|
|
|
destinations = yield self.state.get_current_hosts_in_room(
|
|
|
|
event.room_id, latest_event_ids=[
|
|
|
|
prev_id for prev_id, _ in event.prev_events
|
|
|
|
],
|
|
|
|
)
|
|
|
|
except Exception:
|
2018-04-10 07:03:33 -04:00
|
|
|
logger.exception(
|
|
|
|
"Failed to calculate hosts in room for event: %s",
|
|
|
|
event.event_id,
|
|
|
|
)
|
2018-04-09 06:45:22 -04:00
|
|
|
return
|
2018-02-14 08:52:03 -05:00
|
|
|
|
2017-05-25 12:08:41 -04:00
|
|
|
destinations = set(destinations)
|
2016-11-17 10:46:44 -05:00
|
|
|
|
2017-01-05 06:26:30 -05:00
|
|
|
if send_on_behalf_of is not None:
|
|
|
|
# If we are sending the event on behalf of another server
|
|
|
|
# then it already has the event and there is no reason to
|
|
|
|
# send the event to it.
|
|
|
|
destinations.discard(send_on_behalf_of)
|
2016-11-21 09:48:51 -05:00
|
|
|
|
2016-11-21 06:28:37 -05:00
|
|
|
logger.debug("Sending %s to %r", event, destinations)
|
|
|
|
|
2016-11-21 09:48:51 -05:00
|
|
|
self._send_pdu(event, destinations)
|
2016-11-21 06:28:37 -05:00
|
|
|
|
2018-04-09 11:02:48 -04:00
|
|
|
@defer.inlineCallbacks
|
2018-04-09 06:45:22 -04:00
|
|
|
def handle_room_events(events):
|
|
|
|
for event in events:
|
2018-04-09 11:02:48 -04:00
|
|
|
yield handle_event(event)
|
2018-04-09 06:45:22 -04:00
|
|
|
|
|
|
|
events_by_room = {}
|
|
|
|
for event in events:
|
|
|
|
events_by_room.setdefault(event.room_id, []).append(event)
|
|
|
|
|
|
|
|
yield logcontext.make_deferred_yieldable(defer.gatherResults(
|
2018-04-10 07:04:32 -04:00
|
|
|
[
|
2018-04-10 09:25:11 -04:00
|
|
|
logcontext.run_in_background(handle_room_events, evs)
|
2018-05-31 05:03:47 -04:00
|
|
|
for evs in itervalues(events_by_room)
|
2018-04-10 07:04:32 -04:00
|
|
|
],
|
2018-04-09 06:45:22 -04:00
|
|
|
consumeErrors=True
|
|
|
|
))
|
|
|
|
|
2016-11-21 06:28:37 -05:00
|
|
|
yield self.store.update_federation_out_pos(
|
|
|
|
"events", next_token
|
|
|
|
)
|
|
|
|
|
2018-04-11 06:52:19 -04:00
|
|
|
if events:
|
|
|
|
now = self.clock.time_msec()
|
|
|
|
ts = yield self.store.get_received_ts(events[-1].event_id)
|
|
|
|
|
2018-05-21 20:47:37 -04:00
|
|
|
synapse.metrics.event_processing_lag.labels(
|
|
|
|
"federation_sender").set(now - ts)
|
|
|
|
synapse.metrics.event_processing_last_ts.labels(
|
|
|
|
"federation_sender").set(ts)
|
2018-04-11 06:52:19 -04:00
|
|
|
|
2018-08-07 14:09:48 -04:00
|
|
|
events_processed_counter.inc(len(events))
|
|
|
|
|
|
|
|
event_processing_loop_room_count.labels(
|
|
|
|
"federation_sender"
|
|
|
|
).inc(len(events_by_room))
|
|
|
|
|
|
|
|
event_processing_loop_counter.labels("federation_sender").inc()
|
2018-04-12 06:18:19 -04:00
|
|
|
|
2018-05-21 20:47:37 -04:00
|
|
|
synapse.metrics.event_processing_positions.labels(
|
|
|
|
"federation_sender").set(next_token)
|
2018-04-12 06:18:19 -04:00
|
|
|
|
2016-11-17 10:46:44 -05:00
|
|
|
finally:
|
|
|
|
self._is_processing = False
|
|
|
|
|
2016-11-21 09:48:51 -05:00
|
|
|
def _send_pdu(self, pdu, destinations):
|
2015-01-22 11:37:08 -05:00
|
|
|
# We loop through all destinations to see whether we already have
|
|
|
|
# a transaction in progress. If we do, stick it in the pending_pdus
|
|
|
|
# table and we'll get back to it later.
|
|
|
|
|
2016-11-16 08:41:21 -05:00
|
|
|
order = self._order
|
|
|
|
self._order += 1
|
|
|
|
|
2015-01-22 11:37:08 -05:00
|
|
|
destinations = set(destinations)
|
2018-09-25 12:38:54 -04:00
|
|
|
destinations.discard(self.server_name)
|
2015-01-22 11:37:08 -05:00
|
|
|
logger.debug("Sending to: %s", str(destinations))
|
|
|
|
|
|
|
|
if not destinations:
|
|
|
|
return
|
|
|
|
|
2018-06-21 04:39:58 -04:00
|
|
|
sent_pdus_destination_dist_total.inc(len(destinations))
|
|
|
|
sent_pdus_destination_dist_count.inc()
|
2016-11-16 09:28:03 -05:00
|
|
|
|
2015-01-22 11:37:08 -05:00
|
|
|
for destination in destinations:
|
|
|
|
self.pending_pdus_by_dest.setdefault(destination, []).append(
|
2016-08-10 09:21:10 -04:00
|
|
|
(pdu, order)
|
2015-01-22 11:37:08 -05:00
|
|
|
)
|
|
|
|
|
2017-10-06 10:12:43 -04:00
|
|
|
self._attempt_new_transaction(destination)
|
2015-01-22 11:37:08 -05:00
|
|
|
|
2017-10-06 10:12:43 -04:00
|
|
|
@logcontext.preserve_fn # the caller should not yield on this
|
2017-04-10 11:48:30 -04:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def send_presence(self, states):
|
|
|
|
"""Send the new presence states to the appropriate destinations.
|
|
|
|
|
2017-04-11 10:15:34 -04:00
|
|
|
This actually queues up the presence states ready for sending and
|
|
|
|
triggers a background task to process them and send out the transactions.
|
2016-11-16 08:41:21 -05:00
|
|
|
|
2017-04-10 11:48:30 -04:00
|
|
|
Args:
|
|
|
|
states (list(UserPresenceState))
|
|
|
|
"""
|
2018-08-17 11:08:45 -04:00
|
|
|
if not self.hs.config.use_presence:
|
|
|
|
# No-op if presence is disabled.
|
|
|
|
return
|
2017-04-10 11:48:30 -04:00
|
|
|
|
|
|
|
# First we queue up the new presence by user ID, so multiple presence
|
|
|
|
# updates in quick successtion are correctly handled
|
2017-04-11 10:34:55 -04:00
|
|
|
# We only want to send presence for our own users, so lets always just
|
|
|
|
# filter here just in case.
|
2017-04-11 10:19:26 -04:00
|
|
|
self.pending_presence.update({
|
2016-09-09 10:59:08 -04:00
|
|
|
state.user_id: state for state in states
|
2017-04-11 10:19:26 -04:00
|
|
|
if self.is_mine_id(state.user_id)
|
2016-09-09 10:59:08 -04:00
|
|
|
})
|
|
|
|
|
2017-04-10 11:48:30 -04:00
|
|
|
# We then handle the new pending presence in batches, first figuring
|
|
|
|
# out the destinations we need to send each state to and then poking it
|
|
|
|
# to attempt a new transaction. We linearize this so that we don't
|
|
|
|
# accidentally mess up the ordering and send multiple presence updates
|
|
|
|
# in the wrong order
|
|
|
|
if self._processing_pending_presence:
|
2016-11-16 08:41:21 -05:00
|
|
|
return
|
|
|
|
|
2017-04-10 11:48:30 -04:00
|
|
|
self._processing_pending_presence = True
|
|
|
|
try:
|
|
|
|
while True:
|
2017-04-12 05:11:43 -04:00
|
|
|
states_map = self.pending_presence
|
2017-04-10 11:48:30 -04:00
|
|
|
self.pending_presence = {}
|
|
|
|
|
2017-04-12 05:11:43 -04:00
|
|
|
if not states_map:
|
2017-04-10 11:48:30 -04:00
|
|
|
break
|
2016-09-09 10:59:08 -04:00
|
|
|
|
2018-05-31 05:03:47 -04:00
|
|
|
yield self._process_presence_inner(list(states_map.values()))
|
2018-04-27 06:07:40 -04:00
|
|
|
except Exception:
|
|
|
|
logger.exception("Error sending presence states to servers")
|
2017-04-10 11:48:30 -04:00
|
|
|
finally:
|
|
|
|
self._processing_pending_presence = False
|
|
|
|
|
|
|
|
@measure_func("txnqueue._process_presence")
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _process_presence_inner(self, states):
|
|
|
|
"""Given a list of states populate self.pending_presence_by_dest and
|
|
|
|
poke to send a new transaction to each destination
|
|
|
|
|
|
|
|
Args:
|
|
|
|
states (list(UserPresenceState))
|
|
|
|
"""
|
2017-05-16 09:46:16 -04:00
|
|
|
hosts_and_states = yield get_interested_remotes(self.store, states, self.state)
|
2017-04-10 11:48:30 -04:00
|
|
|
|
|
|
|
for destinations, states in hosts_and_states:
|
|
|
|
for destination in destinations:
|
2018-09-25 12:38:54 -04:00
|
|
|
if destination == self.server_name:
|
2017-04-10 11:48:30 -04:00
|
|
|
continue
|
|
|
|
|
|
|
|
self.pending_presence_by_dest.setdefault(
|
|
|
|
destination, {}
|
|
|
|
).update({
|
|
|
|
state.user_id: state for state in states
|
|
|
|
})
|
|
|
|
|
2017-10-06 10:12:43 -04:00
|
|
|
self._attempt_new_transaction(destination)
|
2016-09-09 10:59:08 -04:00
|
|
|
|
2016-11-16 08:41:21 -05:00
|
|
|
def send_edu(self, destination, edu_type, content, key=None):
|
|
|
|
edu = Edu(
|
|
|
|
origin=self.server_name,
|
|
|
|
destination=destination,
|
|
|
|
edu_type=edu_type,
|
|
|
|
content=content,
|
|
|
|
)
|
2015-01-22 11:37:08 -05:00
|
|
|
|
2018-09-28 07:45:54 -04:00
|
|
|
if destination == self.server_name:
|
2018-09-25 12:38:54 -04:00
|
|
|
logger.info("Not sending EDU to ourselves")
|
2015-01-22 11:37:08 -05:00
|
|
|
return
|
|
|
|
|
2016-11-16 09:28:03 -05:00
|
|
|
sent_edus_counter.inc()
|
|
|
|
|
2016-09-09 10:59:08 -04:00
|
|
|
if key:
|
2016-09-09 12:13:30 -04:00
|
|
|
self.pending_edus_keyed_by_dest.setdefault(
|
|
|
|
destination, {}
|
2016-09-09 12:36:56 -04:00
|
|
|
)[(edu.edu_type, key)] = edu
|
2016-09-09 10:59:08 -04:00
|
|
|
else:
|
|
|
|
self.pending_edus_by_dest.setdefault(destination, []).append(edu)
|
2015-01-22 11:37:08 -05:00
|
|
|
|
2017-10-06 10:12:43 -04:00
|
|
|
self._attempt_new_transaction(destination)
|
2015-01-22 11:37:08 -05:00
|
|
|
|
2016-11-16 06:32:16 -05:00
|
|
|
def send_device_messages(self, destination):
|
2018-09-25 12:38:54 -04:00
|
|
|
if destination == self.server_name:
|
|
|
|
logger.info("Not sending device update to ourselves")
|
2018-09-28 07:45:54 -04:00
|
|
|
return
|
2016-09-07 11:10:51 -04:00
|
|
|
|
2017-10-06 10:12:43 -04:00
|
|
|
self._attempt_new_transaction(destination)
|
2016-09-07 11:10:51 -04:00
|
|
|
|
2016-11-21 06:53:02 -05:00
|
|
|
def get_current_token(self):
|
|
|
|
return 0
|
|
|
|
|
2015-01-22 11:37:08 -05:00
|
|
|
def _attempt_new_transaction(self, destination):
|
2017-10-06 10:12:43 -04:00
|
|
|
"""Try to start a new transaction to this destination
|
|
|
|
|
|
|
|
If there is already a transaction in progress to this destination,
|
|
|
|
returns immediately. Otherwise kicks off the process of sending a
|
|
|
|
transaction in the background.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
destination (str):
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
"""
|
2016-09-09 06:44:36 -04:00
|
|
|
# list of (pending_pdu, deferred, order)
|
|
|
|
if destination in self.pending_transactions:
|
|
|
|
# XXX: pending_transactions can get stuck on by a never-ending
|
|
|
|
# request at which point pending_pdus_by_dest just keeps growing.
|
|
|
|
# we need application-layer timeouts of some flavour of these
|
|
|
|
# requests
|
|
|
|
logger.debug(
|
|
|
|
"TX [%s] Transaction already in progress",
|
|
|
|
destination
|
|
|
|
)
|
|
|
|
return
|
2016-05-09 05:10:06 -04:00
|
|
|
|
2017-10-06 10:12:43 -04:00
|
|
|
logger.debug("TX [%s] Starting transaction loop", destination)
|
|
|
|
|
2018-07-18 09:35:24 -04:00
|
|
|
run_as_background_process(
|
|
|
|
"federation_transaction_transmission_loop",
|
|
|
|
self._transaction_transmission_loop,
|
|
|
|
destination,
|
|
|
|
)
|
2017-10-06 10:12:43 -04:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _transaction_transmission_loop(self, destination):
|
2017-03-29 06:48:27 -04:00
|
|
|
pending_pdus = []
|
2016-09-09 06:44:36 -04:00
|
|
|
try:
|
|
|
|
self.pending_transactions[destination] = 1
|
2015-01-22 11:37:08 -05:00
|
|
|
|
2017-03-29 06:48:27 -04:00
|
|
|
# This will throw if we wouldn't retry. We do this here so we fail
|
|
|
|
# quickly, but we will later check this again in the http client,
|
|
|
|
# hence why we throw the result away.
|
|
|
|
yield get_retry_limiter(destination, self.clock, self.store)
|
|
|
|
|
2017-03-24 10:44:49 -04:00
|
|
|
pending_pdus = []
|
2016-09-09 06:44:36 -04:00
|
|
|
while True:
|
2017-01-25 11:55:21 -05:00
|
|
|
device_message_edus, device_stream_id, dev_list_id = (
|
|
|
|
yield self._get_new_device_messages(destination)
|
|
|
|
)
|
2015-01-22 11:37:08 -05:00
|
|
|
|
2017-02-20 11:43:29 -05:00
|
|
|
# BEGIN CRITICAL SECTION
|
|
|
|
#
|
|
|
|
# In order to avoid a race condition, we need to make sure that
|
|
|
|
# the following code (from popping the queues up to the point
|
|
|
|
# where we decide if we actually have any pending messages) is
|
|
|
|
# atomic - otherwise new PDUs or EDUs might arrive in the
|
|
|
|
# meantime, but not get sent because we hold the
|
|
|
|
# pending_transactions flag.
|
|
|
|
|
|
|
|
pending_pdus = self.pending_pdus_by_dest.pop(destination, [])
|
2018-09-06 10:23:55 -04:00
|
|
|
|
|
|
|
# We can only include at most 50 PDUs per transactions
|
|
|
|
pending_pdus, leftover_pdus = pending_pdus[:50], pending_pdus[50:]
|
|
|
|
if leftover_pdus:
|
|
|
|
self.pending_pdus_by_dest[destination] = leftover_pdus
|
|
|
|
|
2017-02-20 11:43:29 -05:00
|
|
|
pending_edus = self.pending_edus_by_dest.pop(destination, [])
|
2018-09-06 10:23:55 -04:00
|
|
|
|
|
|
|
# We can only include at most 100 EDUs per transactions
|
|
|
|
pending_edus, leftover_edus = pending_edus[:100], pending_edus[100:]
|
|
|
|
if leftover_edus:
|
|
|
|
self.pending_edus_by_dest[destination] = leftover_edus
|
|
|
|
|
2017-02-20 11:43:29 -05:00
|
|
|
pending_presence = self.pending_presence_by_dest.pop(destination, {})
|
|
|
|
|
|
|
|
pending_edus.extend(
|
|
|
|
self.pending_edus_keyed_by_dest.pop(destination, {}).values()
|
|
|
|
)
|
|
|
|
|
2017-01-25 11:55:21 -05:00
|
|
|
pending_edus.extend(device_message_edus)
|
|
|
|
if pending_presence:
|
|
|
|
pending_edus.append(
|
|
|
|
Edu(
|
|
|
|
origin=self.server_name,
|
|
|
|
destination=destination,
|
|
|
|
edu_type="m.presence",
|
|
|
|
content={
|
|
|
|
"push": [
|
|
|
|
format_user_presence_state(
|
|
|
|
presence, self.clock.time_msec()
|
|
|
|
)
|
|
|
|
for presence in pending_presence.values()
|
|
|
|
]
|
|
|
|
},
|
2016-09-09 10:59:08 -04:00
|
|
|
)
|
2017-01-25 11:55:21 -05:00
|
|
|
)
|
2015-01-22 11:37:08 -05:00
|
|
|
|
2017-01-25 11:55:21 -05:00
|
|
|
if pending_pdus:
|
|
|
|
logger.debug("TX [%s] len(pending_pdus_by_dest[dest]) = %d",
|
|
|
|
destination, len(pending_pdus))
|
2016-09-09 06:44:36 -04:00
|
|
|
|
2018-07-30 18:24:02 -04:00
|
|
|
if not pending_pdus and not pending_edus:
|
2017-01-25 11:55:21 -05:00
|
|
|
logger.debug("TX [%s] Nothing to send", destination)
|
|
|
|
self.last_device_stream_id_by_dest[destination] = (
|
|
|
|
device_stream_id
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
2017-02-20 11:43:29 -05:00
|
|
|
# END CRITICAL SECTION
|
|
|
|
|
2017-01-25 11:55:21 -05:00
|
|
|
success = yield self._send_new_transaction(
|
2018-07-30 18:24:02 -04:00
|
|
|
destination, pending_pdus, pending_edus,
|
2017-01-25 11:55:21 -05:00
|
|
|
)
|
|
|
|
if success:
|
2017-04-11 12:16:12 -04:00
|
|
|
sent_transactions_counter.inc()
|
2017-01-25 11:55:21 -05:00
|
|
|
# Remove the acknowledged device messages from the database
|
|
|
|
# Only bother if we actually sent some device messages
|
|
|
|
if device_message_edus:
|
|
|
|
yield self.store.delete_device_msgs_for_remote(
|
|
|
|
destination, device_stream_id
|
|
|
|
)
|
|
|
|
logger.info("Marking as sent %r %r", destination, dev_list_id)
|
|
|
|
yield self.store.mark_as_sent_devices_by_remote(
|
|
|
|
destination, dev_list_id
|
2016-09-09 06:44:36 -04:00
|
|
|
)
|
|
|
|
|
2017-01-25 11:55:21 -05:00
|
|
|
self.last_device_stream_id_by_dest[destination] = device_stream_id
|
|
|
|
self.last_device_list_stream_id_by_dest[destination] = dev_list_id
|
|
|
|
else:
|
|
|
|
break
|
2017-03-22 20:12:21 -04:00
|
|
|
except NotRetryingDestination as e:
|
2017-01-17 12:20:39 -05:00
|
|
|
logger.debug(
|
2017-03-22 20:12:21 -04:00
|
|
|
"TX [%s] not ready for retry yet (next retry at %s) - "
|
2016-09-09 08:46:05 -04:00
|
|
|
"dropping transaction for now",
|
|
|
|
destination,
|
2017-03-22 20:12:21 -04:00
|
|
|
datetime.datetime.fromtimestamp(
|
|
|
|
(e.retry_last_ts + e.retry_interval) / 1000.0
|
|
|
|
),
|
|
|
|
)
|
2018-01-22 13:11:18 -05:00
|
|
|
except FederationDeniedError as e:
|
|
|
|
logger.info(e)
|
2017-03-22 20:12:21 -04:00
|
|
|
except Exception as e:
|
|
|
|
logger.warn(
|
|
|
|
"TX [%s] Failed to send transaction: %s",
|
|
|
|
destination,
|
|
|
|
e,
|
2016-09-09 08:46:05 -04:00
|
|
|
)
|
2017-03-29 06:48:27 -04:00
|
|
|
for p, _ in pending_pdus:
|
2017-03-22 20:12:21 -04:00
|
|
|
logger.info("Failed to send event %s to %s", p.event_id,
|
|
|
|
destination)
|
2016-09-09 06:44:36 -04:00
|
|
|
finally:
|
|
|
|
# We want to be *very* sure we delete this after we stop processing
|
|
|
|
self.pending_transactions.pop(destination, None)
|
2015-02-26 10:47:35 -05:00
|
|
|
|
2016-09-06 13:16:20 -04:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _get_new_device_messages(self, destination):
|
2016-09-07 10:27:07 -04:00
|
|
|
last_device_stream_id = self.last_device_stream_id_by_dest.get(destination, 0)
|
2016-09-06 13:16:20 -04:00
|
|
|
to_device_stream_id = self.store.get_to_device_stream_token()
|
|
|
|
contents, stream_id = yield self.store.get_new_device_msgs_for_remote(
|
|
|
|
destination, last_device_stream_id, to_device_stream_id
|
|
|
|
)
|
|
|
|
edus = [
|
|
|
|
Edu(
|
|
|
|
origin=self.server_name,
|
|
|
|
destination=destination,
|
|
|
|
edu_type="m.direct_to_device",
|
|
|
|
content=content,
|
|
|
|
)
|
|
|
|
for content in contents
|
|
|
|
]
|
2017-01-25 09:27:27 -05:00
|
|
|
|
|
|
|
last_device_list = self.last_device_list_stream_id_by_dest.get(destination, 0)
|
|
|
|
now_stream_id, results = yield self.store.get_devices_by_remote(
|
|
|
|
destination, last_device_list
|
|
|
|
)
|
|
|
|
edus.extend(
|
|
|
|
Edu(
|
|
|
|
origin=self.server_name,
|
|
|
|
destination=destination,
|
|
|
|
edu_type="m.device_list_update",
|
|
|
|
content=content,
|
|
|
|
)
|
|
|
|
for content in results
|
|
|
|
)
|
2017-01-25 11:55:21 -05:00
|
|
|
defer.returnValue((edus, stream_id, now_stream_id))
|
2016-09-06 13:16:20 -04:00
|
|
|
|
2016-08-10 09:21:10 -04:00
|
|
|
@measure_func("_send_new_transaction")
|
|
|
|
@defer.inlineCallbacks
|
2018-07-30 18:24:02 -04:00
|
|
|
def _send_new_transaction(self, destination, pending_pdus, pending_edus):
|
2015-11-03 11:23:35 -05:00
|
|
|
|
2016-09-09 08:12:53 -04:00
|
|
|
# Sort based on the order field
|
|
|
|
pending_pdus.sort(key=lambda t: t[1])
|
|
|
|
pdus = [x[0] for x in pending_pdus]
|
|
|
|
edus = pending_edus
|
2015-01-22 11:37:08 -05:00
|
|
|
|
2016-09-09 08:12:53 -04:00
|
|
|
success = True
|
2015-01-22 11:37:08 -05:00
|
|
|
|
2017-03-22 20:12:21 -04:00
|
|
|
logger.debug("TX [%s] _attempt_new_transaction", destination)
|
2015-01-22 11:37:08 -05:00
|
|
|
|
2017-03-22 20:12:21 -04:00
|
|
|
txn_id = str(self._next_txn_id)
|
2015-01-22 11:37:08 -05:00
|
|
|
|
2017-03-22 20:12:21 -04:00
|
|
|
logger.debug(
|
|
|
|
"TX [%s] {%s} Attempting new transaction"
|
2018-07-30 18:24:02 -04:00
|
|
|
" (pdus: %d, edus: %d)",
|
2017-03-22 20:12:21 -04:00
|
|
|
destination, txn_id,
|
|
|
|
len(pdus),
|
|
|
|
len(edus),
|
|
|
|
)
|
2015-01-22 11:37:08 -05:00
|
|
|
|
2017-03-22 20:12:21 -04:00
|
|
|
logger.debug("TX [%s] Persisting transaction...", destination)
|
2016-08-10 09:21:10 -04:00
|
|
|
|
2017-03-22 20:12:21 -04:00
|
|
|
transaction = Transaction.create_new(
|
|
|
|
origin_server_ts=int(self.clock.time_msec()),
|
|
|
|
transaction_id=txn_id,
|
|
|
|
origin=self.server_name,
|
|
|
|
destination=destination,
|
|
|
|
pdus=pdus,
|
|
|
|
edus=edus,
|
|
|
|
)
|
2016-08-10 09:21:10 -04:00
|
|
|
|
2017-03-22 20:12:21 -04:00
|
|
|
self._next_txn_id += 1
|
2015-01-22 11:37:08 -05:00
|
|
|
|
2017-03-22 20:12:21 -04:00
|
|
|
yield self.transaction_actions.prepare_to_send(transaction)
|
2015-01-22 11:37:08 -05:00
|
|
|
|
2017-03-22 20:12:21 -04:00
|
|
|
logger.debug("TX [%s] Persisted transaction", destination)
|
|
|
|
logger.info(
|
|
|
|
"TX [%s] {%s} Sending transaction [%s],"
|
2018-07-30 18:24:02 -04:00
|
|
|
" (PDUs: %d, EDUs: %d)",
|
2017-03-22 20:12:21 -04:00
|
|
|
destination, txn_id,
|
|
|
|
transaction.transaction_id,
|
|
|
|
len(pdus),
|
|
|
|
len(edus),
|
|
|
|
)
|
2016-08-10 09:21:10 -04:00
|
|
|
|
2017-03-22 20:12:21 -04:00
|
|
|
# Actually send the transaction
|
|
|
|
|
|
|
|
# FIXME (erikj): This is a bit of a hack to make the Pdu age
|
|
|
|
# keys work
|
|
|
|
def json_data_cb():
|
|
|
|
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:
|
|
|
|
response = yield self.transport_layer.send_transaction(
|
|
|
|
transaction, json_data_cb
|
|
|
|
)
|
|
|
|
code = 200
|
|
|
|
|
|
|
|
if response:
|
|
|
|
for e_id, r in response.get("pdus", {}).items():
|
|
|
|
if "error" in r:
|
|
|
|
logger.warn(
|
|
|
|
"Transaction returned error for %s: %s",
|
|
|
|
e_id, r,
|
2016-11-24 10:04:49 -05:00
|
|
|
)
|
2017-03-22 20:12:21 -04:00
|
|
|
except HttpResponseException as e:
|
|
|
|
code = e.code
|
|
|
|
response = e.response
|
2016-11-24 10:04:49 -05:00
|
|
|
|
2017-03-22 20:12:21 -04:00
|
|
|
if e.code in (401, 404, 429) or 500 <= e.code:
|
2016-09-09 08:12:53 -04:00
|
|
|
logger.info(
|
|
|
|
"TX [%s] {%s} got %d response",
|
|
|
|
destination, txn_id, code
|
2016-08-10 09:21:10 -04:00
|
|
|
)
|
2017-03-22 20:12:21 -04:00
|
|
|
raise e
|
2015-01-22 11:37:08 -05:00
|
|
|
|
2017-03-22 20:12:21 -04:00
|
|
|
logger.info(
|
|
|
|
"TX [%s] {%s} got %d response",
|
|
|
|
destination, txn_id, code
|
|
|
|
)
|
2016-08-10 09:21:10 -04:00
|
|
|
|
2017-03-22 20:12:21 -04:00
|
|
|
logger.debug("TX [%s] Sent transaction", destination)
|
|
|
|
logger.debug("TX [%s] Marking as delivered...", destination)
|
2015-01-22 11:37:08 -05:00
|
|
|
|
2017-03-22 20:12:21 -04:00
|
|
|
yield self.transaction_actions.delivered(
|
|
|
|
transaction, code, response
|
|
|
|
)
|
2016-09-09 08:12:53 -04:00
|
|
|
|
2017-03-22 20:12:21 -04:00
|
|
|
logger.debug("TX [%s] Marked as delivered", destination)
|
2016-09-09 08:12:53 -04:00
|
|
|
|
2017-03-22 20:12:21 -04:00
|
|
|
if code != 200:
|
2016-09-09 08:12:53 -04:00
|
|
|
for p in pdus:
|
2017-03-22 20:12:21 -04:00
|
|
|
logger.info(
|
|
|
|
"Failed to send event %s to %s", p.event_id, destination
|
|
|
|
)
|
2016-09-09 08:12:53 -04:00
|
|
|
success = False
|
|
|
|
|
|
|
|
defer.returnValue(success)
|