Fix transaction issues in events table in PSQL (#2165)

* Revert "Revert "Fix storage bug in PSQL events table""

This reverts commit cf447dd52a.

* Membership updater to use updater

* Fix membership updater to use transactions properly
This commit is contained in:
Neil Alexander 2022-02-10 09:30:16 +00:00 committed by GitHub
parent aa5c3b88de
commit 37cbe263ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 6 deletions

View File

@ -48,7 +48,7 @@ func (r *Inputer) updateMemberships(
// Load the event JSON so we can look up the "membership" key. // Load the event JSON so we can look up the "membership" key.
// TODO: Maybe add a membership key to the events table so we can load that // TODO: Maybe add a membership key to the events table so we can load that
// key without having to load the entire event JSON? // key without having to load the entire event JSON?
events, err := r.DB.Events(ctx, eventNIDs) events, err := updater.Events(ctx, eventNIDs)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -74,7 +74,7 @@ const insertEventSQL = "" +
"INSERT INTO roomserver_events AS e (room_nid, event_type_nid, event_state_key_nid, event_id, reference_sha256, auth_event_nids, depth, is_rejected)" + "INSERT INTO roomserver_events AS e (room_nid, event_type_nid, event_state_key_nid, event_id, reference_sha256, auth_event_nids, depth, is_rejected)" +
" VALUES ($1, $2, $3, $4, $5, $6, $7, $8)" + " VALUES ($1, $2, $3, $4, $5, $6, $7, $8)" +
" ON CONFLICT ON CONSTRAINT roomserver_event_id_unique DO UPDATE" + " ON CONFLICT ON CONSTRAINT roomserver_event_id_unique DO UPDATE" +
" SET is_rejected = $8 WHERE e.is_rejected = FALSE" + " SET is_rejected = $8 WHERE e.event_id = $4 AND e.is_rejected = FALSE" +
" RETURNING event_nid, state_snapshot_nid" " RETURNING event_nid, state_snapshot_nid"
const selectEventSQL = "" + const selectEventSQL = "" +
@ -192,7 +192,8 @@ func (s *eventStatements) InsertEvent(
) (types.EventNID, types.StateSnapshotNID, error) { ) (types.EventNID, types.StateSnapshotNID, error) {
var eventNID int64 var eventNID int64
var stateNID int64 var stateNID int64
err := s.insertEventStmt.QueryRowContext( stmt := sqlutil.TxStmt(txn, s.insertEventStmt)
err := stmt.QueryRowContext(
ctx, int64(roomNID), int64(eventTypeNID), int64(eventStateKeyNID), ctx, int64(roomNID), int64(eventTypeNID), int64(eventStateKeyNID),
eventID, referenceSHA256, eventNIDsAsArray(authEventNIDs), depth, eventID, referenceSHA256, eventNIDsAsArray(authEventNIDs), depth,
isRejected, isRejected,

View File

@ -136,7 +136,7 @@ func (u *MembershipUpdater) SetToJoin(senderUserID string, eventID string, isUpd
} }
// Look up the NID of the new join event // Look up the NID of the new join event
nIDs, err := u.d.EventNIDs(u.ctx, []string{eventID}) nIDs, err := u.d.eventNIDs(u.ctx, u.txn, []string{eventID})
if err != nil { if err != nil {
return fmt.Errorf("u.d.EventNIDs: %w", err) return fmt.Errorf("u.d.EventNIDs: %w", err)
} }
@ -170,7 +170,7 @@ func (u *MembershipUpdater) SetToLeave(senderUserID string, eventID string) ([]s
} }
// Look up the NID of the new leave event // Look up the NID of the new leave event
nIDs, err := u.d.EventNIDs(u.ctx, []string{eventID}) nIDs, err := u.d.eventNIDs(u.ctx, u.txn, []string{eventID})
if err != nil { if err != nil {
return fmt.Errorf("u.d.EventNIDs: %w", err) return fmt.Errorf("u.d.EventNIDs: %w", err)
} }
@ -196,7 +196,7 @@ func (u *MembershipUpdater) SetToKnock(event *gomatrixserverlib.Event) (bool, er
} }
if u.membership != tables.MembershipStateKnock { if u.membership != tables.MembershipStateKnock {
// Look up the NID of the new knock event // Look up the NID of the new knock event
nIDs, err := u.d.EventNIDs(u.ctx, []string{event.EventID()}) nIDs, err := u.d.eventNIDs(u.ctx, u.txn, []string{event.EventID()})
if err != nil { if err != nil {
return fmt.Errorf("u.d.EventNIDs: %w", err) return fmt.Errorf("u.d.EventNIDs: %w", err)
} }