2017-06-28 16:10:17 +01:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
package queue
|
|
|
|
|
|
|
|
import (
|
2017-09-13 11:03:41 +01:00
|
|
|
"context"
|
2020-03-27 16:28:22 +00:00
|
|
|
"encoding/json"
|
2017-06-28 16:10:17 +01:00
|
|
|
"fmt"
|
2020-07-01 11:46:38 +01:00
|
|
|
"sync"
|
2017-06-28 16:10:17 +01:00
|
|
|
"time"
|
|
|
|
|
2020-07-01 11:46:38 +01:00
|
|
|
"github.com/matrix-org/dendrite/federationsender/storage"
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
"github.com/matrix-org/dendrite/federationsender/types"
|
2020-06-10 16:54:43 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
"github.com/matrix-org/gomatrix"
|
2017-06-28 16:10:17 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-11-16 10:12:02 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-04-03 14:29:06 +01:00
|
|
|
"go.uber.org/atomic"
|
2017-06-28 16:10:17 +01:00
|
|
|
)
|
|
|
|
|
2020-07-01 11:46:38 +01:00
|
|
|
const maxPDUsPerTransaction = 50
|
2020-07-09 15:39:35 +01:00
|
|
|
const queueIdleTimeout = time.Second * 30
|
2020-07-01 11:46:38 +01:00
|
|
|
|
2017-06-28 16:10:17 +01:00
|
|
|
// destinationQueue is a queue of events for a single destination.
|
|
|
|
// It is responsible for sending the events to the destination and
|
|
|
|
// ensures that only one request is in flight to a given destination
|
|
|
|
// at a time.
|
|
|
|
type destinationQueue struct {
|
2020-07-01 11:46:38 +01:00
|
|
|
db storage.Database
|
2020-06-10 16:54:43 +01:00
|
|
|
signing *SigningInfo
|
|
|
|
rsAPI api.RoomserverInternalAPI
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
client *gomatrixserverlib.FederationClient // federation client
|
|
|
|
origin gomatrixserverlib.ServerName // origin of requests
|
|
|
|
destination gomatrixserverlib.ServerName // destination of requests
|
|
|
|
running atomic.Bool // is the queue worker running?
|
2020-06-01 18:34:08 +01:00
|
|
|
backingOff atomic.Bool // true if we're backing off
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
statistics *types.ServerStatistics // statistics about this remote server
|
|
|
|
incomingInvites chan *gomatrixserverlib.InviteV2Request // invites to send
|
2020-07-01 11:46:38 +01:00
|
|
|
incomingEDUs chan *gomatrixserverlib.EDU // EDUs to send
|
|
|
|
transactionIDMutex sync.Mutex // protects transactionID
|
|
|
|
transactionID gomatrixserverlib.TransactionID // last transaction ID
|
|
|
|
transactionCount atomic.Int32 // how many events in this transaction so far
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
pendingEDUs []*gomatrixserverlib.EDU // owned by backgroundSend
|
|
|
|
pendingInvites []*gomatrixserverlib.InviteV2Request // owned by backgroundSend
|
2020-07-03 16:31:56 +01:00
|
|
|
notifyPDUs chan bool // interrupts idle wait for PDUs
|
|
|
|
interruptBackoff chan bool // interrupts backoff
|
2017-06-28 16:10:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send event adds the event to the pending queue for the destination.
|
|
|
|
// If the queue is empty then it starts a background goroutine to
|
|
|
|
// start sending events to that destination.
|
2020-07-01 11:46:38 +01:00
|
|
|
func (oq *destinationQueue) sendEvent(nid int64) {
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
if oq.statistics.Blacklisted() {
|
|
|
|
// If the destination is blacklisted then drop the event.
|
2020-07-07 16:36:10 +01:00
|
|
|
log.Infof("%s is blacklisted; dropping event", oq.destination)
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
return
|
|
|
|
}
|
2020-07-01 11:46:38 +01:00
|
|
|
// Create a transaction ID. We'll either do this if we don't have
|
|
|
|
// one made up yet, or if we've exceeded the number of maximum
|
|
|
|
// events allowed in a single tranaction. We'll reset the counter
|
|
|
|
// when we do.
|
|
|
|
oq.transactionIDMutex.Lock()
|
|
|
|
if oq.transactionID == "" || oq.transactionCount.Load() >= maxPDUsPerTransaction {
|
|
|
|
now := gomatrixserverlib.AsTimestamp(time.Now())
|
|
|
|
oq.transactionID = gomatrixserverlib.TransactionID(fmt.Sprintf("%d-%d", now, oq.statistics.SuccessCount()))
|
|
|
|
oq.transactionCount.Store(0)
|
|
|
|
}
|
|
|
|
oq.transactionIDMutex.Unlock()
|
|
|
|
// Create a database entry that associates the given PDU NID with
|
|
|
|
// this destination queue. We'll then be able to retrieve the PDU
|
|
|
|
// later.
|
|
|
|
if err := oq.db.AssociatePDUWithDestination(
|
|
|
|
context.TODO(),
|
|
|
|
oq.transactionID, // the current transaction ID
|
|
|
|
oq.destination, // the destination server name
|
|
|
|
[]int64{nid}, // NID from federationsender_queue_json table
|
|
|
|
); err != nil {
|
|
|
|
log.WithError(err).Errorf("failed to associate PDU NID %d with destination %q", nid, oq.destination)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// We've successfully added a PDU to the transaction so increase
|
|
|
|
// the counter.
|
|
|
|
oq.transactionCount.Add(1)
|
2020-07-09 15:39:35 +01:00
|
|
|
// Wake up the queue if it's asleep.
|
|
|
|
oq.wakeQueueIfNeeded()
|
|
|
|
// If we're blocking on waiting PDUs then tell the queue that we
|
|
|
|
// have work to do.
|
|
|
|
select {
|
|
|
|
case oq.notifyPDUs <- true:
|
|
|
|
default:
|
2020-07-01 11:46:38 +01:00
|
|
|
}
|
2017-06-28 16:10:17 +01:00
|
|
|
}
|
|
|
|
|
2018-08-10 16:26:57 +01:00
|
|
|
// sendEDU adds the EDU event to the pending queue for the destination.
|
|
|
|
// If the queue is empty then it starts a background goroutine to
|
2020-04-03 14:29:06 +01:00
|
|
|
// start sending events to that destination.
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
func (oq *destinationQueue) sendEDU(ev *gomatrixserverlib.EDU) {
|
|
|
|
if oq.statistics.Blacklisted() {
|
|
|
|
// If the destination is blacklisted then drop the event.
|
|
|
|
return
|
|
|
|
}
|
2020-07-02 17:43:07 +01:00
|
|
|
oq.wakeQueueIfNeeded()
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
oq.incomingEDUs <- ev
|
2020-04-03 14:29:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// sendInvite adds the invite event to the pending queue for the
|
|
|
|
// destination. If the queue is empty then it starts a background
|
|
|
|
// goroutine to start sending events to that destination.
|
|
|
|
func (oq *destinationQueue) sendInvite(ev *gomatrixserverlib.InviteV2Request) {
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
if oq.statistics.Blacklisted() {
|
|
|
|
// If the destination is blacklisted then drop the event.
|
|
|
|
return
|
|
|
|
}
|
2020-07-02 17:43:07 +01:00
|
|
|
oq.wakeQueueIfNeeded()
|
|
|
|
oq.incomingInvites <- ev
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:31:56 +01:00
|
|
|
// wakeQueueIfNeeded will wake up the destination queue if it is
|
|
|
|
// not already running. If it is running but it is backing off
|
|
|
|
// then we will interrupt the backoff, causing any federation
|
|
|
|
// requests to retry.
|
2020-07-02 17:43:07 +01:00
|
|
|
func (oq *destinationQueue) wakeQueueIfNeeded() {
|
2020-07-03 16:31:56 +01:00
|
|
|
// If we are backing off then interrupt the backoff.
|
|
|
|
if oq.backingOff.CAS(true, false) {
|
|
|
|
oq.interruptBackoff <- true
|
|
|
|
}
|
|
|
|
// If we aren't running then wake up the queue.
|
2020-04-03 14:29:06 +01:00
|
|
|
if !oq.running.Load() {
|
2020-07-09 15:39:35 +01:00
|
|
|
// Start the queue.
|
2018-08-10 16:26:57 +01:00
|
|
|
go oq.backgroundSend()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 15:39:35 +01:00
|
|
|
// waitForPDUs returns a channel for pending PDUs, which will be
|
|
|
|
// used in backgroundSend select. It returns a closed channel if
|
|
|
|
// there is something pending right now, or an open channel if
|
|
|
|
// we're waiting for something.
|
|
|
|
func (oq *destinationQueue) waitForPDUs() chan bool {
|
|
|
|
pendingPDUs, err := oq.db.GetPendingPDUCount(context.TODO(), oq.destination)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("Failed to get pending PDU count on queue %q", oq.destination)
|
|
|
|
}
|
|
|
|
// If there are PDUs pending right now then we'll return a closed
|
|
|
|
// channel. This will mean that the backgroundSend will not block.
|
|
|
|
if pendingPDUs > 0 {
|
|
|
|
ch := make(chan bool, 1)
|
|
|
|
close(ch)
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
// If there are no PDUs pending right now then instead we'll return
|
|
|
|
// the notify channel, so that backgroundSend can pick up normal
|
|
|
|
// notifications from sendEvent.
|
|
|
|
return oq.notifyPDUs
|
|
|
|
}
|
|
|
|
|
2020-04-03 14:29:06 +01:00
|
|
|
// backgroundSend is the worker goroutine for sending events.
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
// nolint:gocyclo
|
2017-06-28 16:10:17 +01:00
|
|
|
func (oq *destinationQueue) backgroundSend() {
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
// Check if a worker is already running, and if it isn't, then
|
|
|
|
// mark it as started.
|
|
|
|
if !oq.running.CAS(false, true) {
|
|
|
|
return
|
|
|
|
}
|
2020-04-03 14:29:06 +01:00
|
|
|
defer oq.running.Store(false)
|
|
|
|
|
2017-06-28 16:10:17 +01:00
|
|
|
for {
|
2020-07-09 15:39:35 +01:00
|
|
|
pendingPDUs := false
|
|
|
|
|
2020-07-01 11:46:38 +01:00
|
|
|
// If we have nothing to do then wait either for incoming events, or
|
|
|
|
// until we hit an idle timeout.
|
2020-07-02 17:43:07 +01:00
|
|
|
select {
|
2020-07-09 15:39:35 +01:00
|
|
|
case <-oq.waitForPDUs():
|
2020-07-02 17:43:07 +01:00
|
|
|
// We were woken up because there are new PDUs waiting in the
|
|
|
|
// database.
|
2020-07-09 15:39:35 +01:00
|
|
|
pendingPDUs = true
|
2020-07-02 17:43:07 +01:00
|
|
|
case edu := <-oq.incomingEDUs:
|
|
|
|
// EDUs are handled in-memory for now. We will try to keep
|
|
|
|
// the ordering intact.
|
|
|
|
// TODO: Certain EDU types need persistence, e.g. send-to-device
|
|
|
|
oq.pendingEDUs = append(oq.pendingEDUs, edu)
|
|
|
|
// If there are any more things waiting in the channel queue
|
|
|
|
// then read them. This is safe because we guarantee only
|
|
|
|
// having one goroutine per destination queue, so the channel
|
|
|
|
// isn't being consumed anywhere else.
|
|
|
|
for len(oq.incomingEDUs) > 0 {
|
|
|
|
oq.pendingEDUs = append(oq.pendingEDUs, <-oq.incomingEDUs)
|
|
|
|
}
|
|
|
|
case invite := <-oq.incomingInvites:
|
|
|
|
// There's no strict ordering requirement for invites like
|
|
|
|
// there is for transactions, so we put the invite onto the
|
|
|
|
// front of the queue. This means that if an invite that is
|
|
|
|
// stuck failing already, that it won't block our new invite
|
|
|
|
// from being sent.
|
|
|
|
oq.pendingInvites = append(
|
|
|
|
[]*gomatrixserverlib.InviteV2Request{invite},
|
|
|
|
oq.pendingInvites...,
|
|
|
|
)
|
|
|
|
// If there are any more things waiting in the channel queue
|
|
|
|
// then read them. This is safe because we guarantee only
|
|
|
|
// having one goroutine per destination queue, so the channel
|
|
|
|
// isn't being consumed anywhere else.
|
|
|
|
for len(oq.incomingInvites) > 0 {
|
|
|
|
oq.pendingInvites = append(oq.pendingInvites, <-oq.incomingInvites)
|
2020-05-27 12:16:53 +01:00
|
|
|
}
|
2020-07-09 15:39:35 +01:00
|
|
|
case <-time.After(queueIdleTimeout):
|
2020-07-02 17:43:07 +01:00
|
|
|
// The worker is idle so stop the goroutine. It'll get
|
|
|
|
// restarted automatically the next time we have an event to
|
|
|
|
// send.
|
2020-07-09 15:39:35 +01:00
|
|
|
log.Infof("Queue %q has been idle for %s, going to sleep", oq.destination, queueIdleTimeout)
|
2020-07-02 17:43:07 +01:00
|
|
|
return
|
2017-06-28 16:10:17 +01:00
|
|
|
}
|
|
|
|
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
// If we are backing off this server then wait for the
|
2020-06-01 18:34:08 +01:00
|
|
|
// backoff duration to complete first, or until explicitly
|
|
|
|
// told to retry.
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
if backoff, duration := oq.statistics.BackoffDuration(); backoff {
|
2020-07-07 16:36:10 +01:00
|
|
|
log.WithField("duration", duration).Infof("Backing off %s", oq.destination)
|
2020-06-01 18:34:08 +01:00
|
|
|
oq.backingOff.Store(true)
|
|
|
|
select {
|
|
|
|
case <-time.After(duration):
|
2020-07-03 16:31:56 +01:00
|
|
|
case <-oq.interruptBackoff:
|
2020-07-09 15:39:35 +01:00
|
|
|
log.Infof("Interrupting backoff for %q", oq.destination)
|
2020-06-01 18:34:08 +01:00
|
|
|
}
|
|
|
|
oq.backingOff.Store(false)
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we have pending PDUs or EDUs then construct a transaction.
|
2020-07-09 15:39:35 +01:00
|
|
|
if pendingPDUs || len(oq.pendingEDUs) > 0 {
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
// Try sending the next transaction and see what happens.
|
2020-07-01 11:46:38 +01:00
|
|
|
transaction, terr := oq.nextTransaction(oq.pendingEDUs)
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
if terr != nil {
|
|
|
|
// We failed to send the transaction.
|
|
|
|
if giveUp := oq.statistics.Failure(); giveUp {
|
2020-07-01 11:46:38 +01:00
|
|
|
// It's been suggested that we should give up because the backoff
|
|
|
|
// has exceeded a maximum allowable value. Clean up the in-memory
|
|
|
|
// buffers at this point. The PDU clean-up is already on a defer.
|
|
|
|
oq.cleanPendingEDUs()
|
|
|
|
oq.cleanPendingInvites()
|
2020-07-09 15:39:35 +01:00
|
|
|
log.Infof("Blacklisting %q due to errors", oq.destination)
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
return
|
2020-07-03 11:49:49 +01:00
|
|
|
} else {
|
|
|
|
// We haven't been told to give up terminally yet but we still have
|
|
|
|
// PDUs waiting to be sent. By sending a message into the wake chan,
|
|
|
|
// the next loop iteration will try processing these PDUs again,
|
|
|
|
// subject to the backoff.
|
2020-07-10 16:28:18 +01:00
|
|
|
select {
|
|
|
|
case oq.notifyPDUs <- true:
|
|
|
|
default:
|
|
|
|
}
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
}
|
|
|
|
} else if transaction {
|
|
|
|
// If we successfully sent the transaction then clear out
|
2020-07-01 11:46:38 +01:00
|
|
|
// the pending events and EDUs, and wipe our transaction ID.
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
oq.statistics.Success()
|
2020-07-01 11:46:38 +01:00
|
|
|
// Clean up the in-memory buffers.
|
|
|
|
oq.cleanPendingEDUs()
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try sending the next invite and see what happens.
|
2020-07-01 11:46:38 +01:00
|
|
|
if len(oq.pendingInvites) > 0 {
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
sent, ierr := oq.nextInvites(oq.pendingInvites)
|
|
|
|
if ierr != nil {
|
|
|
|
// We failed to send the transaction so increase the
|
|
|
|
// backoff and give it another go shortly.
|
|
|
|
if giveUp := oq.statistics.Failure(); giveUp {
|
|
|
|
// It's been suggested that we should give up because
|
|
|
|
// the backoff has exceeded a maximum allowable value.
|
2020-07-09 15:39:35 +01:00
|
|
|
log.Infof("Blacklisting %q due to errors", oq.destination)
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if sent > 0 {
|
|
|
|
// If we successfully sent the invites then clear out
|
|
|
|
// the pending invites.
|
|
|
|
oq.statistics.Success()
|
|
|
|
// Reallocate so that the underlying array can be GC'd, as
|
|
|
|
// opposed to growing forever.
|
2020-07-01 11:46:38 +01:00
|
|
|
oq.cleanPendingInvites()
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
}
|
|
|
|
}
|
2017-06-28 16:10:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 11:46:38 +01:00
|
|
|
// cleanPendingEDUs cleans out the pending EDU buffer, removing
|
|
|
|
// all references so that the underlying objects can be GC'd.
|
|
|
|
func (oq *destinationQueue) cleanPendingEDUs() {
|
|
|
|
for i := 0; i < len(oq.pendingEDUs); i++ {
|
|
|
|
oq.pendingEDUs[i] = nil
|
|
|
|
}
|
|
|
|
oq.pendingEDUs = []*gomatrixserverlib.EDU{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanPendingInvites cleans out the pending invite buffer,
|
|
|
|
// removing all references so that the underlying objects can
|
|
|
|
// be GC'd.
|
|
|
|
func (oq *destinationQueue) cleanPendingInvites() {
|
|
|
|
for i := 0; i < len(oq.pendingInvites); i++ {
|
|
|
|
oq.pendingInvites[i] = nil
|
|
|
|
}
|
|
|
|
oq.pendingInvites = []*gomatrixserverlib.InviteV2Request{}
|
|
|
|
}
|
|
|
|
|
2020-04-03 14:29:06 +01:00
|
|
|
// nextTransaction creates a new transaction from the pending event
|
|
|
|
// queue and sends it. Returns true if a transaction was sent or
|
|
|
|
// false otherwise.
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
func (oq *destinationQueue) nextTransaction(
|
|
|
|
pendingEDUs []*gomatrixserverlib.EDU,
|
|
|
|
) (bool, error) {
|
2020-07-01 11:46:38 +01:00
|
|
|
// Before we do anything, we need to roll over the transaction
|
|
|
|
// ID that is being used to coalesce events into the next TX.
|
|
|
|
// Otherwise it's possible that we'll pick up an incomplete
|
|
|
|
// transaction and end up nuking the rest of the events at the
|
|
|
|
// cleanup stage.
|
|
|
|
oq.transactionIDMutex.Lock()
|
|
|
|
oq.transactionID = ""
|
|
|
|
oq.transactionIDMutex.Unlock()
|
|
|
|
oq.transactionCount.Store(0)
|
|
|
|
|
|
|
|
// Create the transaction.
|
2020-02-28 14:54:51 +00:00
|
|
|
t := gomatrixserverlib.Transaction{
|
2020-03-27 16:28:22 +00:00
|
|
|
PDUs: []json.RawMessage{},
|
2020-02-28 14:54:51 +00:00
|
|
|
EDUs: []gomatrixserverlib.EDU{},
|
|
|
|
}
|
2017-06-28 16:10:17 +01:00
|
|
|
t.Origin = oq.origin
|
|
|
|
t.Destination = oq.destination
|
2020-07-01 11:46:38 +01:00
|
|
|
t.OriginServerTS = gomatrixserverlib.AsTimestamp(time.Now())
|
|
|
|
|
|
|
|
// Ask the database for any pending PDUs from the next transaction.
|
|
|
|
// maxPDUsPerTransaction is an upper limit but we probably won't
|
|
|
|
// actually retrieve that many events.
|
2020-07-02 17:43:07 +01:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
|
|
|
defer cancel()
|
2020-07-01 11:46:38 +01:00
|
|
|
txid, pdus, err := oq.db.GetNextTransactionPDUs(
|
2020-07-02 17:43:07 +01:00
|
|
|
ctx, // context
|
2020-07-01 11:46:38 +01:00
|
|
|
oq.destination, // server name
|
|
|
|
maxPDUsPerTransaction, // max events to retrieve
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("failed to get next transaction PDUs for server %q", oq.destination)
|
|
|
|
return false, fmt.Errorf("oq.db.GetNextTransactionPDUs: %w", err)
|
2017-06-28 16:10:17 +01:00
|
|
|
}
|
2018-08-10 16:26:57 +01:00
|
|
|
|
2020-07-01 11:46:38 +01:00
|
|
|
// If we didn't get anything from the database and there are no
|
|
|
|
// pending EDUs then there's nothing to do - stop here.
|
|
|
|
if len(pdus) == 0 && len(pendingEDUs) == 0 {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pick out the transaction ID from the database. If we didn't
|
|
|
|
// get a transaction ID (i.e. because there are no PDUs but only
|
|
|
|
// EDUs) then generate a transaction ID.
|
|
|
|
t.TransactionID = txid
|
|
|
|
if t.TransactionID == "" {
|
|
|
|
now := gomatrixserverlib.AsTimestamp(time.Now())
|
|
|
|
t.TransactionID = gomatrixserverlib.TransactionID(fmt.Sprintf("%d-%d", now, oq.statistics.SuccessCount()))
|
|
|
|
}
|
2018-08-10 16:26:57 +01:00
|
|
|
|
2020-07-01 11:46:38 +01:00
|
|
|
// Go through PDUs that we retrieved from the database, if any,
|
|
|
|
// and add them into the transaction.
|
|
|
|
for _, pdu := range pdus {
|
2020-03-27 16:28:22 +00:00
|
|
|
// Append the JSON of the event, since this is a json.RawMessage type in the
|
|
|
|
// gomatrixserverlib.Transaction struct
|
|
|
|
t.PDUs = append(t.PDUs, (*pdu).JSON())
|
2017-06-28 16:10:17 +01:00
|
|
|
}
|
2018-08-10 16:26:57 +01:00
|
|
|
|
2020-07-01 11:46:38 +01:00
|
|
|
// Do the same for pending EDUS in the queue.
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
for _, edu := range pendingEDUs {
|
2018-08-10 16:26:57 +01:00
|
|
|
t.EDUs = append(t.EDUs, *edu)
|
|
|
|
}
|
|
|
|
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
logrus.WithField("server_name", oq.destination).Infof("Sending transaction %q containing %d PDUs, %d EDUs", t.TransactionID, len(t.PDUs), len(t.EDUs))
|
2020-04-03 14:29:06 +01:00
|
|
|
|
2020-07-01 11:46:38 +01:00
|
|
|
// Try to send the transaction to the destination server.
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
// TODO: we should check for 500-ish fails vs 400-ish here,
|
|
|
|
// since we shouldn't queue things indefinitely in response
|
|
|
|
// to a 400-ish error
|
2020-07-10 16:28:18 +01:00
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), time.Second*30)
|
2020-07-08 14:52:48 +01:00
|
|
|
defer cancel()
|
|
|
|
_, err = oq.client.SendTransaction(ctx, t)
|
2020-07-07 16:54:14 +01:00
|
|
|
switch err.(type) {
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
case nil:
|
2020-07-01 11:46:38 +01:00
|
|
|
// Clean up the transaction in the database.
|
|
|
|
if err = oq.db.CleanTransactionPDUs(
|
2020-07-08 14:52:48 +01:00
|
|
|
context.Background(),
|
2020-07-01 11:46:38 +01:00
|
|
|
t.Destination,
|
|
|
|
t.TransactionID,
|
|
|
|
); err != nil {
|
|
|
|
log.WithError(err).Errorf("failed to clean transaction %q for server %q", t.TransactionID, t.Destination)
|
|
|
|
}
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
return true, nil
|
|
|
|
case gomatrix.HTTPError:
|
2020-07-07 16:53:10 +01:00
|
|
|
// Report that we failed to send the transaction and we
|
|
|
|
// will retry again, subject to backoff.
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
return false, err
|
|
|
|
default:
|
2020-04-03 14:29:06 +01:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"destination": oq.destination,
|
|
|
|
log.ErrorKey: err,
|
|
|
|
}).Info("problem sending transaction")
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
return false, err
|
2020-04-03 14:29:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// nextInvite takes pending invite events from the queue and sends
|
|
|
|
// them. Returns true if a transaction was sent or false otherwise.
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
func (oq *destinationQueue) nextInvites(
|
|
|
|
pendingInvites []*gomatrixserverlib.InviteV2Request,
|
|
|
|
) (int, error) {
|
|
|
|
done := 0
|
|
|
|
for _, inviteReq := range pendingInvites {
|
2020-04-28 10:53:07 +01:00
|
|
|
ev, roomVersion := inviteReq.Event(), inviteReq.RoomVersion()
|
2020-04-03 14:29:06 +01:00
|
|
|
|
2020-04-28 10:53:07 +01:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"event_id": ev.EventID(),
|
|
|
|
"room_version": roomVersion,
|
|
|
|
"destination": oq.destination,
|
|
|
|
}).Info("sending invite")
|
|
|
|
|
|
|
|
inviteRes, err := oq.client.SendInviteV2(
|
2020-04-03 14:29:06 +01:00
|
|
|
context.TODO(),
|
|
|
|
oq.destination,
|
|
|
|
*inviteReq,
|
2020-04-28 10:53:07 +01:00
|
|
|
)
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
switch e := err.(type) {
|
|
|
|
case nil:
|
|
|
|
done++
|
|
|
|
case gomatrix.HTTPError:
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"event_id": ev.EventID(),
|
|
|
|
"state_key": ev.StateKey(),
|
|
|
|
"destination": oq.destination,
|
|
|
|
"status_code": e.Code,
|
|
|
|
}).WithError(err).Error("failed to send invite due to HTTP error")
|
|
|
|
// Check whether we should do something about the error or
|
|
|
|
// just accept it as unavoidable.
|
|
|
|
if e.Code >= 400 && e.Code <= 499 {
|
|
|
|
// We tried but the remote side has sent back a client error.
|
|
|
|
// It's no use retrying because it will happen again.
|
|
|
|
done++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return done, err
|
|
|
|
default:
|
2020-04-03 14:29:06 +01:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"event_id": ev.EventID(),
|
|
|
|
"state_key": ev.StateKey(),
|
|
|
|
"destination": oq.destination,
|
|
|
|
}).WithError(err).Error("failed to send invite")
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
return done, err
|
2020-04-28 10:53:07 +01:00
|
|
|
}
|
|
|
|
|
2020-06-10 16:54:43 +01:00
|
|
|
invEv := inviteRes.Event.Sign(string(oq.signing.ServerName), oq.signing.KeyID, oq.signing.PrivateKey).Headered(roomVersion)
|
|
|
|
_, err = api.SendEvents(context.TODO(), oq.rsAPI, []gomatrixserverlib.HeaderedEvent{invEv}, oq.signing.ServerName, nil)
|
|
|
|
if err != nil {
|
2020-04-28 10:53:07 +01:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"event_id": ev.EventID(),
|
|
|
|
"state_key": ev.StateKey(),
|
|
|
|
"destination": oq.destination,
|
|
|
|
}).WithError(err).Error("failed to return signed invite to roomserver")
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
return done, err
|
2020-04-03 14:29:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
return done, nil
|
2017-06-28 16:10:17 +01:00
|
|
|
}
|