Reset invalid state snapshots for events during state storage refactor migration (#2209)

This should help with #2204. We can't do this for rooms, only events.
This commit is contained in:
Neil Alexander 2022-02-21 15:25:54 +00:00 committed by GitHub
parent a386fbed2c
commit a02dd7721d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 17 deletions

View file

@ -256,23 +256,17 @@ func UpStateBlocksRefactor(tx *sql.Tx) error {
return fmt.Errorf("assertion query failed: %s", err) return fmt.Errorf("assertion query failed: %s", err)
} }
if count > 0 { if count > 0 {
var debugEventID, debugRoomID string var res sql.Result
var debugEventTypeNID, debugStateKeyNID, debugSnapNID, debugDepth int64 var c int64
err = tx.QueryRow( res, err = tx.Exec(`UPDATE roomserver_events SET state_snapshot_nid = 0 WHERE state_snapshot_nid < $1 AND state_snapshot_nid != 0`, maxsnapshotid)
`SELECT event_id, event_type_nid, event_state_key_nid, roomserver_events.state_snapshot_nid, depth, room_id FROM roomserver_events if err != nil && err != sql.ErrNoRows {
JOIN roomserver_rooms ON roomserver_rooms.room_nid = roomserver_events.room_nid WHERE roomserver_events.state_snapshot_nid < $1 AND roomserver_events.state_snapshot_nid != 0`, maxsnapshotid, return fmt.Errorf("failed to reset invalid state snapshots: %w", err)
).Scan(&debugEventID, &debugEventTypeNID, &debugStateKeyNID, &debugSnapNID, &debugDepth, &debugRoomID) }
if err != nil { if c, err = res.RowsAffected(); err != nil {
logrus.Errorf("cannot extract debug info: %v", err) return fmt.Errorf("failed to get row count for invalid state snapshots updated: %w", err)
} else { } else if c != count {
logrus.Errorf( return fmt.Errorf("expected to reset %d event(s) but only updated %d event(s)", count, c)
"Affected row: event_id=%v room_id=%v type=%v state_key=%v snapshot=%v depth=%v",
debugEventID, debugRoomID, debugEventTypeNID, debugStateKeyNID, debugSnapNID, debugDepth,
)
logrus.Errorf("To fix this manually, run this query first then retry the migration: "+
"UPDATE roomserver_events SET state_snapshot_nid=0 WHERE event_id='%v'", debugEventID)
} }
return fmt.Errorf("%d events exist in roomserver_events which have not been converted to a new state_snapshot_nid; this is a bug, please report", count)
} }
if err = tx.QueryRow(`SELECT COUNT(*) FROM roomserver_rooms WHERE state_snapshot_nid < $1 AND state_snapshot_nid != 0`, maxsnapshotid).Scan(&count); err != nil { if err = tx.QueryRow(`SELECT COUNT(*) FROM roomserver_rooms WHERE state_snapshot_nid < $1 AND state_snapshot_nid != 0`, maxsnapshotid).Scan(&count); err != nil {
return fmt.Errorf("assertion query failed: %s", err) return fmt.Errorf("assertion query failed: %s", err)

View file

@ -179,7 +179,17 @@ func UpStateBlocksRefactor(tx *sql.Tx) error {
return fmt.Errorf("assertion query failed: %s", err) return fmt.Errorf("assertion query failed: %s", err)
} }
if count > 0 { if count > 0 {
return fmt.Errorf("%d events exist in roomserver_events which have not been converted to a new state_snapshot_nid; this is a bug, please report", count) var res sql.Result
var c int64
res, err = tx.Exec(`UPDATE roomserver_events SET state_snapshot_nid = 0 WHERE state_snapshot_nid < $1 AND state_snapshot_nid != 0`, oldMaxSnapshotID)
if err != nil && err != sql.ErrNoRows {
return fmt.Errorf("failed to reset invalid state snapshots: %w", err)
}
if c, err = res.RowsAffected(); err != nil {
return fmt.Errorf("failed to get row count for invalid state snapshots updated: %w", err)
} else if c != count {
return fmt.Errorf("expected to reset %d event(s) but only updated %d event(s)", count, c)
}
} }
if err = tx.QueryRow(`SELECT COUNT(*) FROM roomserver_rooms WHERE state_snapshot_nid < $1 AND state_snapshot_nid != 0`, oldMaxSnapshotID).Scan(&count); err != nil { if err = tx.QueryRow(`SELECT COUNT(*) FROM roomserver_rooms WHERE state_snapshot_nid < $1 AND state_snapshot_nid != 0`, oldMaxSnapshotID).Scan(&count); err != nil {
return fmt.Errorf("assertion query failed: %s", err) return fmt.Errorf("assertion query failed: %s", err)