diff options
author | Neil Alexander <neilalexander@users.noreply.github.com> | 2020-07-20 16:55:20 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-20 16:55:20 +0100 |
commit | 11a39fe3b5afeadafd2c842951462d9332ebecca (patch) | |
tree | 88c3601666ecd6e891c64875f29e1f8ce22eac34 /federationsender/storage/shared | |
parent | f3c482b078c2fbb6d6d7a41be44c2e841d70d8b8 (diff) |
Deduplicate FS database, EDU persistence table (#1207)
* Deduplicate FS database, add some EDU persistence groundwork
* Extend TransactionWriter to use optional existing transaction, use that for FS SQLite database writes
* Fix build due to bad keyserver import
* Working EDU persistence
* gocyclo, unsurprisingly
* Remove unused
* Update copyright notices
Diffstat (limited to 'federationsender/storage/shared')
-rw-r--r-- | federationsender/storage/shared/storage.go | 138 | ||||
-rw-r--r-- | federationsender/storage/shared/storage_edus.go | 143 | ||||
-rw-r--r-- | federationsender/storage/shared/storage_pdus.go | 155 |
3 files changed, 436 insertions, 0 deletions
diff --git a/federationsender/storage/shared/storage.go b/federationsender/storage/shared/storage.go new file mode 100644 index 00000000..75681ea3 --- /dev/null +++ b/federationsender/storage/shared/storage.go @@ -0,0 +1,138 @@ +// Copyright 2020 The Matrix.org Foundation C.I.C. +// +// 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 shared + +import ( + "context" + "database/sql" + "encoding/json" + "fmt" + + "github.com/matrix-org/dendrite/federationsender/storage/tables" + "github.com/matrix-org/dendrite/federationsender/types" + "github.com/matrix-org/dendrite/internal/sqlutil" + "github.com/matrix-org/gomatrixserverlib" +) + +type Database struct { + DB *sql.DB + FederationSenderQueuePDUs tables.FederationSenderQueuePDUs + FederationSenderQueueEDUs tables.FederationSenderQueueEDUs + FederationSenderQueueJSON tables.FederationSenderQueueJSON + FederationSenderJoinedHosts tables.FederationSenderJoinedHosts + FederationSenderRooms tables.FederationSenderRooms +} + +// An Receipt contains the NIDs of a call to GetNextTransactionPDUs/EDUs. +// We don't actually export the NIDs but we need the caller to be able +// to pass them back so that we can clean up if the transaction sends +// successfully. +type Receipt struct { + nids []int64 +} + +func (e *Receipt) Empty() bool { + return len(e.nids) == 0 +} + +func (e *Receipt) String() string { + j, _ := json.Marshal(e.nids) + return string(j) +} + +// UpdateRoom updates the joined hosts for a room and returns what the joined +// hosts were before the update, or nil if this was a duplicate message. +// This is called when we receive a message from kafka, so we pass in +// oldEventID and newEventID to check that we haven't missed any messages or +// this isn't a duplicate message. +func (d *Database) UpdateRoom( + ctx context.Context, + roomID, oldEventID, newEventID string, + addHosts []types.JoinedHost, + removeHosts []string, +) (joinedHosts []types.JoinedHost, err error) { + err = sqlutil.WithTransaction(d.DB, func(txn *sql.Tx) error { + err = d.FederationSenderRooms.InsertRoom(ctx, txn, roomID) + if err != nil { + return err + } + + lastSentEventID, err := d.FederationSenderRooms.SelectRoomForUpdate(ctx, txn, roomID) + if err != nil { + return err + } + + if lastSentEventID == newEventID { + // We've handled this message before, so let's just ignore it. + // We can only get a duplicate for the last message we processed, + // so its enough just to compare the newEventID with lastSentEventID + return nil + } + + if lastSentEventID != "" && lastSentEventID != oldEventID { + return types.EventIDMismatchError{ + DatabaseID: lastSentEventID, RoomServerID: oldEventID, + } + } + + joinedHosts, err = d.FederationSenderJoinedHosts.SelectJoinedHostsWithTx(ctx, txn, roomID) + if err != nil { + return err + } + + for _, add := range addHosts { + err = d.FederationSenderJoinedHosts.InsertJoinedHosts(ctx, txn, roomID, add.MemberEventID, add.ServerName) + if err != nil { + return err + } + } + if err = d.FederationSenderJoinedHosts.DeleteJoinedHosts(ctx, txn, removeHosts); err != nil { + return err + } + return d.FederationSenderRooms.UpdateRoom(ctx, txn, roomID, newEventID) + }) + return +} + +// GetJoinedHosts returns the currently joined hosts for room, +// as known to federationserver. +// Returns an error if something goes wrong. +func (d *Database) GetJoinedHosts( + ctx context.Context, roomID string, +) ([]types.JoinedHost, error) { + return d.FederationSenderJoinedHosts.SelectJoinedHosts(ctx, roomID) +} + +// GetAllJoinedHosts returns the currently joined hosts for +// all rooms known to the federation sender. +// Returns an error if something goes wrong. +func (d *Database) GetAllJoinedHosts(ctx context.Context) ([]gomatrixserverlib.ServerName, error) { + return d.FederationSenderJoinedHosts.SelectAllJoinedHosts(ctx) +} + +// StoreJSON adds a JSON blob into the queue JSON table and returns +// a NID. The NID will then be used when inserting the per-destination +// metadata entries. +func (d *Database) StoreJSON( + ctx context.Context, js string, +) (*Receipt, error) { + nid, err := d.FederationSenderQueueJSON.InsertQueueJSON(ctx, nil, js) + if err != nil { + return nil, fmt.Errorf("d.insertQueueJSON: %w", err) + } + return &Receipt{ + nids: []int64{nid}, + }, nil +} diff --git a/federationsender/storage/shared/storage_edus.go b/federationsender/storage/shared/storage_edus.go new file mode 100644 index 00000000..75a6dd51 --- /dev/null +++ b/federationsender/storage/shared/storage_edus.go @@ -0,0 +1,143 @@ +// Copyright 2020 The Matrix.org Foundation C.I.C. +// +// 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 shared + +import ( + "context" + "database/sql" + "encoding/json" + "errors" + "fmt" + + "github.com/matrix-org/dendrite/internal/sqlutil" + "github.com/matrix-org/gomatrixserverlib" +) + +// AssociateEDUWithDestination creates an association that the +// destination queues will use to determine which JSON blobs to send +// to which servers. +func (d *Database) AssociateEDUWithDestination( + ctx context.Context, + serverName gomatrixserverlib.ServerName, + receipt *Receipt, +) error { + return sqlutil.WithTransaction(d.DB, func(txn *sql.Tx) error { + for _, nid := range receipt.nids { + if err := d.FederationSenderQueueEDUs.InsertQueueEDU( + ctx, // context + txn, // SQL transaction + "", // TODO: EDU type for coalescing + serverName, // destination server name + nid, // NID from the federationsender_queue_json table + ); err != nil { + return fmt.Errorf("InsertQueueEDU: %w", err) + } + } + return nil + }) +} + +// GetNextTransactionEDUs retrieves events from the database for +// the next pending transaction, up to the limit specified. +func (d *Database) GetNextTransactionEDUs( + ctx context.Context, + serverName gomatrixserverlib.ServerName, + limit int, +) ( + edus []*gomatrixserverlib.EDU, + receipt *Receipt, + err error, +) { + err = sqlutil.WithTransaction(d.DB, func(txn *sql.Tx) error { + nids, err := d.FederationSenderQueueEDUs.SelectQueueEDUs(ctx, txn, serverName, limit) + if err != nil { + return fmt.Errorf("SelectQueueEDUs: %w", err) + } + + receipt = &Receipt{ + nids: nids, + } + + blobs, err := d.FederationSenderQueueJSON.SelectQueueJSON(ctx, txn, nids) + if err != nil { + return fmt.Errorf("SelectQueueJSON: %w", err) + } + + for _, blob := range blobs { + var event gomatrixserverlib.EDU + if err := json.Unmarshal(blob, &event); err != nil { + return fmt.Errorf("json.Unmarshal: %w", err) + } + edus = append(edus, &event) + } + + return nil + }) + return +} + +// CleanEDUs cleans up all specified EDUs. This is done when a +// transaction was sent successfully. +func (d *Database) CleanEDUs( + ctx context.Context, + serverName gomatrixserverlib.ServerName, + receipt *Receipt, +) error { + if receipt == nil { + return errors.New("expected receipt") + } + + return sqlutil.WithTransaction(d.DB, func(txn *sql.Tx) error { + if err := d.FederationSenderQueueEDUs.DeleteQueueEDUs(ctx, txn, serverName, receipt.nids); err != nil { + return err + } + + var deleteNIDs []int64 + for _, nid := range receipt.nids { + count, err := d.FederationSenderQueueEDUs.SelectQueueEDUReferenceJSONCount(ctx, txn, nid) + if err != nil { + return fmt.Errorf("SelectQueueEDUReferenceJSONCount: %w", err) + } + if count == 0 { + deleteNIDs = append(deleteNIDs, nid) + } + } + + if len(deleteNIDs) > 0 { + if err := d.FederationSenderQueueJSON.DeleteQueueJSON(ctx, txn, deleteNIDs); err != nil { + return fmt.Errorf("DeleteQueueJSON: %w", err) + } + } + + return nil + }) +} + +// GetPendingEDUCount returns the number of EDUs waiting to be +// sent for a given servername. +func (d *Database) GetPendingEDUCount( + ctx context.Context, + serverName gomatrixserverlib.ServerName, +) (int64, error) { + return d.FederationSenderQueueEDUs.SelectQueueEDUCount(ctx, nil, serverName) +} + +// GetPendingServerNames returns the server names that have EDUs +// waiting to be sent. +func (d *Database) GetPendingEDUServerNames( + ctx context.Context, +) ([]gomatrixserverlib.ServerName, error) { + return d.FederationSenderQueueEDUs.SelectQueueEDUServerNames(ctx, nil) +} diff --git a/federationsender/storage/shared/storage_pdus.go b/federationsender/storage/shared/storage_pdus.go new file mode 100644 index 00000000..00588956 --- /dev/null +++ b/federationsender/storage/shared/storage_pdus.go @@ -0,0 +1,155 @@ +// Copyright 2020 The Matrix.org Foundation C.I.C. +// +// 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 shared + +import ( + "context" + "database/sql" + "encoding/json" + "errors" + "fmt" + + "github.com/matrix-org/dendrite/internal/sqlutil" + "github.com/matrix-org/gomatrixserverlib" +) + +// AssociatePDUWithDestination creates an association that the +// destination queues will use to determine which JSON blobs to send +// to which servers. +func (d *Database) AssociatePDUWithDestination( + ctx context.Context, + transactionID gomatrixserverlib.TransactionID, + serverName gomatrixserverlib.ServerName, + receipt *Receipt, +) error { + return sqlutil.WithTransaction(d.DB, func(txn *sql.Tx) error { + for _, nid := range receipt.nids { + if err := d.FederationSenderQueuePDUs.InsertQueuePDU( + ctx, // context + txn, // SQL transaction + transactionID, // transaction ID + serverName, // destination server name + nid, // NID from the federationsender_queue_json table + ); err != nil { + return fmt.Errorf("InsertQueuePDU: %w", err) + } + } + return nil + }) +} + +// GetNextTransactionPDUs retrieves events from the database for +// the next pending transaction, up to the limit specified. +func (d *Database) GetNextTransactionPDUs( + ctx context.Context, + serverName gomatrixserverlib.ServerName, + limit int, +) ( + transactionID gomatrixserverlib.TransactionID, + events []*gomatrixserverlib.HeaderedEvent, + receipt *Receipt, + err error, +) { + err = sqlutil.WithTransaction(d.DB, func(txn *sql.Tx) error { + transactionID, err = d.FederationSenderQueuePDUs.SelectQueuePDUNextTransactionID(ctx, txn, serverName) + if err != nil { + return fmt.Errorf("SelectQueuePDUNextTransactionID: %w", err) + } + + if transactionID == "" { + return nil + } + + nids, err := d.FederationSenderQueuePDUs.SelectQueuePDUs(ctx, txn, serverName, transactionID, limit) + if err != nil { + return fmt.Errorf("SelectQueuePDUs: %w", err) + } + + receipt = &Receipt{ + nids: nids, + } + + blobs, err := d.FederationSenderQueueJSON.SelectQueueJSON(ctx, txn, nids) + if err != nil { + return fmt.Errorf("SelectQueueJSON: %w", err) + } + + for _, blob := range blobs { + var event gomatrixserverlib.HeaderedEvent + if err := json.Unmarshal(blob, &event); err != nil { + return fmt.Errorf("json.Unmarshal: %w", err) + } + events = append(events, &event) + } + + return nil + }) + return +} + +// CleanTransactionPDUs cleans up all associated events for a +// given transaction. This is done when the transaction was sent +// successfully. +func (d *Database) CleanPDUs( + ctx context.Context, + serverName gomatrixserverlib.ServerName, + receipt *Receipt, +) error { + if receipt == nil { + return errors.New("expected receipt") + } + + return sqlutil.WithTransaction(d.DB, func(txn *sql.Tx) error { + if err := d.FederationSenderQueuePDUs.DeleteQueuePDUs(ctx, txn, serverName, receipt.nids); err != nil { + return err + } + + var deleteNIDs []int64 + for _, nid := range receipt.nids { + count, err := d.FederationSenderQueuePDUs.SelectQueuePDUReferenceJSONCount(ctx, txn, nid) + if err != nil { + return fmt.Errorf("SelectQueuePDUReferenceJSONCount: %w", err) + } + if count == 0 { + deleteNIDs = append(deleteNIDs, nid) + } + } + + if len(deleteNIDs) > 0 { + if err := d.FederationSenderQueueJSON.DeleteQueueJSON(ctx, txn, deleteNIDs); err != nil { + return fmt.Errorf("DeleteQueueJSON: %w", err) + } + } + + return nil + }) +} + +// GetPendingPDUCount returns the number of PDUs waiting to be +// sent for a given servername. +func (d *Database) GetPendingPDUCount( + ctx context.Context, + serverName gomatrixserverlib.ServerName, +) (int64, error) { + return d.FederationSenderQueuePDUs.SelectQueuePDUCount(ctx, nil, serverName) +} + +// GetPendingServerNames returns the server names that have PDUs +// waiting to be sent. +func (d *Database) GetPendingPDUServerNames( + ctx context.Context, +) ([]gomatrixserverlib.ServerName, error) { + return d.FederationSenderQueuePDUs.SelectQueuePDUServerNames(ctx, nil) +} |