From 18be3a2b0c00d464bca5f24605ea8d8c7e08ca7a Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Thu, 4 Feb 2021 10:50:52 +0000 Subject: [PATCH] Remove last sent event ID column from federation sender --- .../storage/postgres/room_table.go | 24 ++---------------- federationsender/storage/shared/storage.go | 20 +-------------- .../storage/sqlite3/room_table.go | 25 ++----------------- federationsender/storage/tables/interface.go | 1 - 4 files changed, 5 insertions(+), 65 deletions(-) diff --git a/federationsender/storage/postgres/room_table.go b/federationsender/storage/postgres/room_table.go index 8d3ed20ff..c7a205786 100644 --- a/federationsender/storage/postgres/room_table.go +++ b/federationsender/storage/postgres/room_table.go @@ -25,28 +25,20 @@ import ( const roomSchema = ` CREATE TABLE IF NOT EXISTS federationsender_rooms ( -- The string ID of the room - room_id TEXT PRIMARY KEY, - -- The most recent event state by the room server. - -- We can use this to tell if our view of the room state has become - -- desynchronised. - last_event_id TEXT NOT NULL + room_id TEXT PRIMARY KEY );` const insertRoomSQL = "" + - "INSERT INTO federationsender_rooms (room_id, last_event_id) VALUES ($1, '')" + + "INSERT INTO federationsender_rooms (room_id) VALUES ($1)" + " ON CONFLICT DO NOTHING" const selectRoomForUpdateSQL = "" + "SELECT last_event_id FROM federationsender_rooms WHERE room_id = $1 FOR UPDATE" -const updateRoomSQL = "" + - "UPDATE federationsender_rooms SET last_event_id = $2 WHERE room_id = $1" - type roomStatements struct { db *sql.DB insertRoomStmt *sql.Stmt selectRoomForUpdateStmt *sql.Stmt - updateRoomStmt *sql.Stmt } func NewPostgresRoomsTable(db *sql.DB) (s *roomStatements, err error) { @@ -63,9 +55,6 @@ func NewPostgresRoomsTable(db *sql.DB) (s *roomStatements, err error) { if s.selectRoomForUpdateStmt, err = s.db.Prepare(selectRoomForUpdateSQL); err != nil { return } - if s.updateRoomStmt, err = s.db.Prepare(updateRoomSQL); err != nil { - return - } return } @@ -93,12 +82,3 @@ func (s *roomStatements) SelectRoomForUpdate( return lastEventID, nil } -// updateRoom updates the last_event_id for the room. selectRoomForUpdate should -// have already been called earlier within the transaction. -func (s *roomStatements) UpdateRoom( - ctx context.Context, txn *sql.Tx, roomID, lastEventID string, -) error { - stmt := sqlutil.TxStmt(txn, s.updateRoomStmt) - _, err := stmt.ExecContext(ctx, roomID, lastEventID) - return err -} diff --git a/federationsender/storage/shared/storage.go b/federationsender/storage/shared/storage.go index 4c9490424..72e5eb40f 100644 --- a/federationsender/storage/shared/storage.go +++ b/federationsender/storage/shared/storage.go @@ -69,24 +69,6 @@ func (d *Database) UpdateRoom( 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 @@ -101,7 +83,7 @@ func (d *Database) UpdateRoom( if err = d.FederationSenderJoinedHosts.DeleteJoinedHosts(ctx, txn, removeHosts); err != nil { return err } - return d.FederationSenderRooms.UpdateRoom(ctx, txn, roomID, newEventID) + return nil }) return } diff --git a/federationsender/storage/sqlite3/room_table.go b/federationsender/storage/sqlite3/room_table.go index 0710ccca3..ad7c10fe2 100644 --- a/federationsender/storage/sqlite3/room_table.go +++ b/federationsender/storage/sqlite3/room_table.go @@ -25,28 +25,20 @@ import ( const roomSchema = ` CREATE TABLE IF NOT EXISTS federationsender_rooms ( -- The string ID of the room - room_id TEXT PRIMARY KEY, - -- The most recent event state by the room server. - -- We can use this to tell if our view of the room state has become - -- desynchronised. - last_event_id TEXT NOT NULL + room_id TEXT PRIMARY KEY );` const insertRoomSQL = "" + - "INSERT INTO federationsender_rooms (room_id, last_event_id) VALUES ($1, '')" + + "INSERT INTO federationsender_rooms (room_id) VALUES ($1)" + " ON CONFLICT DO NOTHING" const selectRoomForUpdateSQL = "" + "SELECT last_event_id FROM federationsender_rooms WHERE room_id = $1" -const updateRoomSQL = "" + - "UPDATE federationsender_rooms SET last_event_id = $2 WHERE room_id = $1" - type roomStatements struct { db *sql.DB insertRoomStmt *sql.Stmt selectRoomForUpdateStmt *sql.Stmt - updateRoomStmt *sql.Stmt } func NewSQLiteRoomsTable(db *sql.DB) (s *roomStatements, err error) { @@ -64,9 +56,6 @@ func NewSQLiteRoomsTable(db *sql.DB) (s *roomStatements, err error) { if s.selectRoomForUpdateStmt, err = db.Prepare(selectRoomForUpdateSQL); err != nil { return } - if s.updateRoomStmt, err = db.Prepare(updateRoomSQL); err != nil { - return - } return } @@ -93,13 +82,3 @@ func (s *roomStatements) SelectRoomForUpdate( } return lastEventID, nil } - -// updateRoom updates the last_event_id for the room. selectRoomForUpdate should -// have already been called earlier within the transaction. -func (s *roomStatements) UpdateRoom( - ctx context.Context, txn *sql.Tx, roomID, lastEventID string, -) error { - stmt := sqlutil.TxStmt(txn, s.updateRoomStmt) - _, err := stmt.ExecContext(ctx, roomID, lastEventID) - return err -} diff --git a/federationsender/storage/tables/interface.go b/federationsender/storage/tables/interface.go index 22fd5554f..430b6dcc0 100644 --- a/federationsender/storage/tables/interface.go +++ b/federationsender/storage/tables/interface.go @@ -59,7 +59,6 @@ type FederationSenderJoinedHosts interface { type FederationSenderRooms interface { InsertRoom(ctx context.Context, txn *sql.Tx, roomID string) error SelectRoomForUpdate(ctx context.Context, txn *sql.Tx, roomID string) (string, error) - UpdateRoom(ctx context.Context, txn *sql.Tx, roomID, lastEventID string) error } type FederationSenderBlacklist interface {