2015-01-22 17:37:08 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-07 05:26:29 +01:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2015-01-22 17:37:08 +01: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.
|
|
|
|
|
|
|
|
|
|
|
|
from twisted.internet import defer
|
|
|
|
|
2015-01-26 11:45:24 +01:00
|
|
|
from .persistence import TransactionActions
|
2016-09-06 19:16:20 +02:00
|
|
|
from .units import Transaction, Edu
|
2015-01-22 17:37:08 +01:00
|
|
|
|
2015-02-04 17:28:12 +01:00
|
|
|
from synapse.api.errors import HttpResponseException
|
2016-05-09 11:10:06 +02:00
|
|
|
from synapse.util.async import run_on_reactor
|
2016-08-10 15:21:10 +02:00
|
|
|
from synapse.util.logcontext import preserve_context_over_fn
|
2015-02-17 18:20:56 +01:00
|
|
|
from synapse.util.retryutils import (
|
|
|
|
get_retry_limiter, NotRetryingDestination,
|
|
|
|
)
|
2016-08-10 11:56:38 +02:00
|
|
|
from synapse.util.metrics import measure_func
|
2016-11-17 16:46:44 +01:00
|
|
|
from synapse.types import get_domain_from_id
|
2016-09-09 16:59:08 +02:00
|
|
|
from synapse.handlers.presence import format_user_presence_state
|
2015-03-04 19:15:34 +01:00
|
|
|
import synapse.metrics
|
2015-01-22 17:37:08 +01:00
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-03-04 19:15:34 +01:00
|
|
|
metrics = synapse.metrics.get_metrics_for(__name__)
|
|
|
|
|
2016-11-16 15:28:03 +01:00
|
|
|
client_metrics = synapse.metrics.get_metrics_for("synapse.federation.client")
|
|
|
|
sent_pdus_destination_dist = client_metrics.register_distribution(
|
|
|
|
"sent_pdu_destinations"
|
|
|
|
)
|
|
|
|
sent_edus_counter = client_metrics.register_counter("sent_edus")
|
|
|
|
|
2015-01-22 17:37:08 +01: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 15:15:50 +01:00
|
|
|
def __init__(self, hs):
|
2015-01-22 17:37:08 +01:00
|
|
|
self.server_name = hs.hostname
|
2015-01-26 11:45:24 +01:00
|
|
|
|
|
|
|
self.store = hs.get_datastore()
|
2016-11-17 16:46:44 +01:00
|
|
|
self.state = hs.get_state_handler()
|
2015-01-26 11:45:24 +01:00
|
|
|
self.transaction_actions = TransactionActions(self.store)
|
|
|
|
|
2016-11-16 15:15:50 +01:00
|
|
|
self.transport_layer = hs.get_federation_transport_client()
|
2015-01-22 17:37:08 +01:00
|
|
|
|
2016-08-10 11:56:38 +02:00
|
|
|
self.clock = hs.get_clock()
|
2015-01-22 17:37:08 +01: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 = {}
|
|
|
|
|
2015-03-12 17:24:38 +01:00
|
|
|
metrics.register_callback(
|
|
|
|
"pending_destinations",
|
2015-03-10 19:24:52 +01:00
|
|
|
lambda: len(self.pending_transactions),
|
|
|
|
)
|
|
|
|
|
2015-01-22 17:37:08 +01:00
|
|
|
# Is a mapping from destination -> list of
|
|
|
|
# tuple(pending pdus, deferred, order)
|
2015-03-04 19:15:34 +01:00
|
|
|
self.pending_pdus_by_dest = pdus = {}
|
2015-01-22 17:37:08 +01:00
|
|
|
# destination -> list of tuple(edu, deferred)
|
2015-03-04 19:15:34 +01:00
|
|
|
self.pending_edus_by_dest = edus = {}
|
2015-01-22 17:37:08 +01:00
|
|
|
|
2016-09-09 18:17:23 +02:00
|
|
|
# Presence needs to be separate as we send single aggragate EDUs
|
2016-09-09 16:59:08 +02:00
|
|
|
self.pending_presence_by_dest = presence = {}
|
|
|
|
self.pending_edus_keyed_by_dest = edus_keyed = {}
|
|
|
|
|
2015-03-12 17:24:38 +01:00
|
|
|
metrics.register_callback(
|
|
|
|
"pending_pdus",
|
2015-03-10 19:24:52 +01:00
|
|
|
lambda: sum(map(len, pdus.values())),
|
|
|
|
)
|
2015-03-12 17:24:38 +01:00
|
|
|
metrics.register_callback(
|
|
|
|
"pending_edus",
|
2016-09-09 16:59:08 +02:00
|
|
|
lambda: (
|
|
|
|
sum(map(len, edus.values()))
|
|
|
|
+ sum(map(len, presence.values()))
|
|
|
|
+ sum(map(len, edus_keyed.values()))
|
|
|
|
),
|
2015-03-10 19:24:52 +01:00
|
|
|
)
|
|
|
|
|
2015-01-22 17:37:08 +01:00
|
|
|
# destination -> list of tuple(failure, deferred)
|
|
|
|
self.pending_failures_by_dest = {}
|
|
|
|
|
2016-09-07 16:27:07 +02:00
|
|
|
self.last_device_stream_id_by_dest = {}
|
|
|
|
|
2015-01-22 17:37:08 +01:00
|
|
|
# HACK to get unique tx id
|
2016-08-10 11:56:38 +02:00
|
|
|
self._next_txn_id = int(self.clock.time_msec())
|
2015-01-22 17:37:08 +01:00
|
|
|
|
2016-11-16 14:41:21 +01:00
|
|
|
self._order = 1
|
|
|
|
|
2016-11-17 16:46:44 +01:00
|
|
|
self._is_processing = False
|
2016-11-21 12:28:37 +01:00
|
|
|
self._last_poked_id = -1
|
2016-11-17 16:46:44 +01:00
|
|
|
|
2015-02-18 12:51:00 +01:00
|
|
|
def can_send_to(self, destination):
|
|
|
|
"""Can we send messages to the given server?
|
|
|
|
|
|
|
|
We can't send messages to ourselves. If we are running on localhost
|
|
|
|
then we can only federation with other servers running on localhost.
|
|
|
|
Otherwise we only federate with servers on a public domain.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
destination(str): The server we are possibly trying to send to.
|
|
|
|
Returns:
|
|
|
|
bool: True if we can send to the server.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if destination == self.server_name:
|
|
|
|
return False
|
|
|
|
if self.server_name.startswith("localhost"):
|
|
|
|
return destination.startswith("localhost")
|
|
|
|
else:
|
|
|
|
return not destination.startswith("localhost")
|
|
|
|
|
2016-11-17 16:46:44 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def notify_new_events(self, current_id):
|
2016-11-21 12:28:37 +01:00
|
|
|
self._last_poked_id = max(current_id, self._last_poked_id)
|
|
|
|
|
2016-11-17 16:46:44 +01:00
|
|
|
if self._is_processing:
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
self._is_processing = True
|
|
|
|
while True:
|
2016-11-21 12:28:37 +01:00
|
|
|
last_token = yield self.store.get_federation_out_pos("events")
|
|
|
|
next_token, events = yield self.store.get_all_new_events_stream(
|
|
|
|
last_token, self._last_poked_id, limit=20,
|
2016-11-17 16:46:44 +01:00
|
|
|
)
|
|
|
|
|
2016-11-21 12:28:37 +01:00
|
|
|
logger.debug("Handling %s -> %s", last_token, next_token)
|
|
|
|
|
|
|
|
if not events and next_token >= self._last_poked_id:
|
2016-11-17 16:46:44 +01:00
|
|
|
break
|
|
|
|
|
|
|
|
for event in events:
|
|
|
|
users_in_room = yield self.state.get_current_user_in_room(
|
|
|
|
event.room_id, latest_event_ids=[event.event_id],
|
|
|
|
)
|
|
|
|
|
|
|
|
destinations = [
|
|
|
|
get_domain_from_id(user_id) for user_id in users_in_room
|
|
|
|
]
|
2016-11-21 12:28:37 +01:00
|
|
|
|
|
|
|
logger.debug("Sending %s to %r", event, destinations)
|
|
|
|
|
2016-11-17 16:46:44 +01:00
|
|
|
self.send_pdu(event, destinations)
|
2016-11-21 12:28:37 +01:00
|
|
|
|
|
|
|
yield self.store.update_federation_out_pos(
|
|
|
|
"events", next_token
|
|
|
|
)
|
|
|
|
|
2016-11-17 16:46:44 +01:00
|
|
|
finally:
|
|
|
|
self._is_processing = False
|
|
|
|
|
2016-11-16 14:41:21 +01:00
|
|
|
def send_pdu(self, pdu, destinations):
|
2015-01-22 17:37:08 +01: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 14:41:21 +01:00
|
|
|
order = self._order
|
|
|
|
self._order += 1
|
|
|
|
|
2015-01-22 17:37:08 +01:00
|
|
|
destinations = set(destinations)
|
2015-02-18 12:51:00 +01:00
|
|
|
destinations = set(
|
|
|
|
dest for dest in destinations if self.can_send_to(dest)
|
|
|
|
)
|
2015-01-22 17:37:08 +01:00
|
|
|
|
|
|
|
logger.debug("Sending to: %s", str(destinations))
|
|
|
|
|
|
|
|
if not destinations:
|
|
|
|
return
|
|
|
|
|
2016-11-16 15:28:03 +01:00
|
|
|
sent_pdus_destination_dist.inc_by(len(destinations))
|
|
|
|
|
2015-01-22 17:37:08 +01:00
|
|
|
for destination in destinations:
|
|
|
|
self.pending_pdus_by_dest.setdefault(destination, []).append(
|
2016-08-10 15:21:10 +02:00
|
|
|
(pdu, order)
|
2015-01-22 17:37:08 +01:00
|
|
|
)
|
|
|
|
|
2016-08-10 15:21:10 +02:00
|
|
|
preserve_context_over_fn(
|
|
|
|
self._attempt_new_transaction, destination
|
|
|
|
)
|
2015-01-22 17:37:08 +01:00
|
|
|
|
2016-11-16 12:32:16 +01:00
|
|
|
def send_presence(self, destination, states):
|
2016-11-16 14:41:21 +01:00
|
|
|
if not self.can_send_to(destination):
|
|
|
|
return
|
|
|
|
|
2016-09-09 16:59:08 +02:00
|
|
|
self.pending_presence_by_dest.setdefault(destination, {}).update({
|
|
|
|
state.user_id: state for state in states
|
|
|
|
})
|
|
|
|
|
|
|
|
preserve_context_over_fn(
|
|
|
|
self._attempt_new_transaction, destination
|
|
|
|
)
|
|
|
|
|
2016-11-16 14:41:21 +01: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 17:37:08 +01:00
|
|
|
|
2015-02-18 12:51:00 +01:00
|
|
|
if not self.can_send_to(destination):
|
2015-01-22 17:37:08 +01:00
|
|
|
return
|
|
|
|
|
2016-11-16 15:28:03 +01:00
|
|
|
sent_edus_counter.inc()
|
|
|
|
|
2016-09-09 16:59:08 +02:00
|
|
|
if key:
|
2016-09-09 18:13:30 +02:00
|
|
|
self.pending_edus_keyed_by_dest.setdefault(
|
|
|
|
destination, {}
|
2016-09-09 18:36:56 +02:00
|
|
|
)[(edu.edu_type, key)] = edu
|
2016-09-09 16:59:08 +02:00
|
|
|
else:
|
|
|
|
self.pending_edus_by_dest.setdefault(destination, []).append(edu)
|
2015-01-22 17:37:08 +01:00
|
|
|
|
2016-08-10 15:21:10 +02:00
|
|
|
preserve_context_over_fn(
|
|
|
|
self._attempt_new_transaction, destination
|
|
|
|
)
|
2015-01-22 17:37:08 +01:00
|
|
|
|
2016-11-16 12:32:16 +01:00
|
|
|
def send_failure(self, failure, destination):
|
2015-02-18 12:32:39 +01:00
|
|
|
if destination == self.server_name or destination == "localhost":
|
2015-02-18 12:30:37 +01:00
|
|
|
return
|
|
|
|
|
2015-02-18 12:51:00 +01:00
|
|
|
if not self.can_send_to(destination):
|
|
|
|
return
|
|
|
|
|
2015-01-22 17:37:08 +01:00
|
|
|
self.pending_failures_by_dest.setdefault(
|
|
|
|
destination, []
|
2016-08-10 15:21:10 +02:00
|
|
|
).append(failure)
|
2015-02-17 14:58:52 +01:00
|
|
|
|
2016-08-10 15:21:10 +02:00
|
|
|
preserve_context_over_fn(
|
|
|
|
self._attempt_new_transaction, destination
|
|
|
|
)
|
2015-01-22 17:37:08 +01:00
|
|
|
|
2016-11-16 12:32:16 +01:00
|
|
|
def send_device_messages(self, destination):
|
2016-09-07 17:10:51 +02:00
|
|
|
if destination == self.server_name or destination == "localhost":
|
|
|
|
return
|
|
|
|
|
|
|
|
if not self.can_send_to(destination):
|
|
|
|
return
|
|
|
|
|
|
|
|
preserve_context_over_fn(
|
|
|
|
self._attempt_new_transaction, destination
|
|
|
|
)
|
|
|
|
|
2016-11-21 12:53:02 +01:00
|
|
|
def get_current_token(self):
|
|
|
|
return 0
|
|
|
|
|
2015-01-22 17:37:08 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _attempt_new_transaction(self, destination):
|
2016-09-09 12:44:36 +02: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 11:10:06 +02:00
|
|
|
|
2016-09-09 12:44:36 +02:00
|
|
|
try:
|
|
|
|
self.pending_transactions[destination] = 1
|
2015-01-22 17:37:08 +01:00
|
|
|
|
2016-09-09 12:44:36 +02:00
|
|
|
yield run_on_reactor()
|
2016-09-07 16:39:13 +02:00
|
|
|
|
2016-09-09 12:44:36 +02:00
|
|
|
while True:
|
|
|
|
pending_pdus = self.pending_pdus_by_dest.pop(destination, [])
|
|
|
|
pending_edus = self.pending_edus_by_dest.pop(destination, [])
|
2016-09-09 16:59:08 +02:00
|
|
|
pending_presence = self.pending_presence_by_dest.pop(destination, {})
|
2016-09-09 12:44:36 +02:00
|
|
|
pending_failures = self.pending_failures_by_dest.pop(destination, [])
|
2016-09-07 16:39:13 +02:00
|
|
|
|
2016-09-09 16:59:08 +02:00
|
|
|
pending_edus.extend(
|
|
|
|
self.pending_edus_keyed_by_dest.pop(destination, {}).values()
|
|
|
|
)
|
|
|
|
|
2016-09-09 14:46:05 +02:00
|
|
|
limiter = yield get_retry_limiter(
|
|
|
|
destination,
|
|
|
|
self.clock,
|
|
|
|
self.store,
|
|
|
|
)
|
|
|
|
|
2016-09-09 12:44:36 +02:00
|
|
|
device_message_edus, device_stream_id = (
|
|
|
|
yield self._get_new_device_messages(destination)
|
|
|
|
)
|
2015-01-22 17:37:08 +01:00
|
|
|
|
2016-09-09 12:44:36 +02:00
|
|
|
pending_edus.extend(device_message_edus)
|
2016-09-09 16:59:08 +02:00
|
|
|
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()
|
|
|
|
]
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
2015-01-22 17:37:08 +01:00
|
|
|
|
2016-09-09 12:44:36 +02:00
|
|
|
if pending_pdus:
|
|
|
|
logger.debug("TX [%s] len(pending_pdus_by_dest[dest]) = %d",
|
|
|
|
destination, len(pending_pdus))
|
|
|
|
|
|
|
|
if not pending_pdus and not pending_edus and not pending_failures:
|
|
|
|
logger.debug("TX [%s] Nothing to send", destination)
|
|
|
|
self.last_device_stream_id_by_dest[destination] = (
|
|
|
|
device_stream_id
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
2016-09-09 14:12:53 +02:00
|
|
|
success = yield self._send_new_transaction(
|
2016-09-09 12:44:36 +02:00
|
|
|
destination, pending_pdus, pending_edus, pending_failures,
|
|
|
|
device_stream_id,
|
2016-09-09 14:46:05 +02:00
|
|
|
should_delete_from_device_stream=bool(device_message_edus),
|
|
|
|
limiter=limiter,
|
2016-09-09 12:44:36 +02:00
|
|
|
)
|
2016-09-09 14:12:53 +02:00
|
|
|
if not success:
|
|
|
|
break
|
2016-09-09 14:46:05 +02:00
|
|
|
except NotRetryingDestination:
|
|
|
|
logger.info(
|
|
|
|
"TX [%s] not ready for retry yet - "
|
|
|
|
"dropping transaction for now",
|
|
|
|
destination,
|
|
|
|
)
|
2016-09-09 12:44:36 +02:00
|
|
|
finally:
|
|
|
|
# We want to be *very* sure we delete this after we stop processing
|
|
|
|
self.pending_transactions.pop(destination, None)
|
2015-02-26 16:47:35 +01:00
|
|
|
|
2016-09-06 19:16:20 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _get_new_device_messages(self, destination):
|
2016-09-07 16:27:07 +02:00
|
|
|
last_device_stream_id = self.last_device_stream_id_by_dest.get(destination, 0)
|
2016-09-06 19:16:20 +02: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
|
|
|
|
]
|
|
|
|
defer.returnValue((edus, stream_id))
|
|
|
|
|
2016-08-10 15:21:10 +02:00
|
|
|
@measure_func("_send_new_transaction")
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _send_new_transaction(self, destination, pending_pdus, pending_edus,
|
2016-09-07 16:39:13 +02:00
|
|
|
pending_failures, device_stream_id,
|
2016-09-09 14:46:05 +02:00
|
|
|
should_delete_from_device_stream, limiter):
|
2015-11-03 17:23:35 +01:00
|
|
|
|
2016-09-09 14:12:53 +02: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
|
|
|
|
failures = [x.get_dict() for x in pending_failures]
|
2015-01-22 17:37:08 +01:00
|
|
|
|
2016-09-09 14:12:53 +02:00
|
|
|
success = True
|
2015-01-22 17:37:08 +01:00
|
|
|
|
2016-09-09 14:12:53 +02:00
|
|
|
try:
|
|
|
|
logger.debug("TX [%s] _attempt_new_transaction", destination)
|
2015-01-22 17:37:08 +01:00
|
|
|
|
2016-09-09 14:12:53 +02:00
|
|
|
txn_id = str(self._next_txn_id)
|
2015-01-22 17:37:08 +01:00
|
|
|
|
2016-09-09 14:12:53 +02:00
|
|
|
logger.debug(
|
|
|
|
"TX [%s] {%s} Attempting new transaction"
|
|
|
|
" (pdus: %d, edus: %d, failures: %d)",
|
|
|
|
destination, txn_id,
|
|
|
|
len(pdus),
|
|
|
|
len(edus),
|
|
|
|
len(failures)
|
|
|
|
)
|
2015-01-22 17:37:08 +01:00
|
|
|
|
2016-09-09 14:12:53 +02:00
|
|
|
logger.debug("TX [%s] Persisting transaction...", destination)
|
2016-08-10 15:21:10 +02:00
|
|
|
|
2016-09-09 14:12:53 +02: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,
|
|
|
|
pdu_failures=failures,
|
|
|
|
)
|
2016-08-10 15:21:10 +02:00
|
|
|
|
2016-09-09 14:12:53 +02:00
|
|
|
self._next_txn_id += 1
|
2015-01-22 17:37:08 +01:00
|
|
|
|
2016-09-09 14:12:53 +02:00
|
|
|
yield self.transaction_actions.prepare_to_send(transaction)
|
2015-01-22 17:37:08 +01:00
|
|
|
|
2016-09-09 14:12:53 +02:00
|
|
|
logger.debug("TX [%s] Persisted transaction", destination)
|
|
|
|
logger.info(
|
|
|
|
"TX [%s] {%s} Sending transaction [%s],"
|
|
|
|
" (PDUs: %d, EDUs: %d, failures: %d)",
|
|
|
|
destination, txn_id,
|
|
|
|
transaction.transaction_id,
|
|
|
|
len(pdus),
|
|
|
|
len(edus),
|
|
|
|
len(failures),
|
|
|
|
)
|
2016-08-10 15:21:10 +02:00
|
|
|
|
2016-09-09 14:12:53 +02:00
|
|
|
with limiter:
|
|
|
|
# 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
|
2016-08-10 15:21:10 +02:00
|
|
|
)
|
2016-09-09 14:12:53 +02:00
|
|
|
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,
|
|
|
|
)
|
|
|
|
except HttpResponseException as e:
|
|
|
|
code = e.code
|
|
|
|
response = e.response
|
2015-02-17 18:20:56 +01:00
|
|
|
|
2016-09-09 14:12:53 +02:00
|
|
|
logger.info(
|
|
|
|
"TX [%s] {%s} got %d response",
|
|
|
|
destination, txn_id, code
|
2016-08-10 15:21:10 +02:00
|
|
|
)
|
2015-01-22 17:37:08 +01:00
|
|
|
|
2016-09-09 14:12:53 +02:00
|
|
|
logger.debug("TX [%s] Sent transaction", destination)
|
|
|
|
logger.debug("TX [%s] Marking as delivered...", destination)
|
2016-08-10 15:21:10 +02:00
|
|
|
|
2016-09-09 14:12:53 +02:00
|
|
|
yield self.transaction_actions.delivered(
|
|
|
|
transaction, code, response
|
|
|
|
)
|
2016-08-10 15:21:10 +02:00
|
|
|
|
2016-09-09 14:12:53 +02:00
|
|
|
logger.debug("TX [%s] Marked as delivered", destination)
|
2015-01-22 17:37:08 +01:00
|
|
|
|
2016-09-09 14:12:53 +02:00
|
|
|
if code != 200:
|
2016-08-10 15:21:10 +02:00
|
|
|
for p in pdus:
|
2016-09-09 14:12:53 +02:00
|
|
|
logger.info(
|
|
|
|
"Failed to send event %s to %s", p.event_id, destination
|
|
|
|
)
|
|
|
|
success = False
|
|
|
|
else:
|
|
|
|
# Remove the acknowledged device messages from the database
|
|
|
|
if should_delete_from_device_stream:
|
|
|
|
yield self.store.delete_device_msgs_for_remote(
|
|
|
|
destination, device_stream_id
|
|
|
|
)
|
|
|
|
self.last_device_stream_id_by_dest[destination] = device_stream_id
|
|
|
|
except RuntimeError as e:
|
|
|
|
# We capture this here as there as nothing actually listens
|
|
|
|
# for this finishing functions deferred.
|
|
|
|
logger.warn(
|
|
|
|
"TX [%s] Problem in _attempt_transaction: %s",
|
|
|
|
destination,
|
|
|
|
e,
|
|
|
|
)
|
|
|
|
|
|
|
|
success = False
|
|
|
|
|
|
|
|
for p in pdus:
|
|
|
|
logger.info("Failed to send event %s to %s", p.event_id, destination)
|
|
|
|
except Exception as e:
|
|
|
|
# We capture this here as there as nothing actually listens
|
|
|
|
# for this finishing functions deferred.
|
|
|
|
logger.warn(
|
|
|
|
"TX [%s] Problem in _attempt_transaction: %s",
|
|
|
|
destination,
|
|
|
|
e,
|
|
|
|
)
|
|
|
|
|
|
|
|
success = False
|
|
|
|
|
|
|
|
for p in pdus:
|
|
|
|
logger.info("Failed to send event %s to %s", p.event_id, destination)
|
|
|
|
|
|
|
|
defer.returnValue(success)
|