Unset RoomServerEvent, since we can't be sure that Set actually updates the cached entry (#3002)

This should deflake UTs and be more correct in terms of getting
`Events`.
`Events` tries to fetch the event from the cache first and may get an
unredacted event from it, while it should already be redacted.
This commit is contained in:
Till 2023-03-09 09:52:13 +01:00 committed by GitHub
parent baef523cb0
commit 70322699ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 2 deletions

View File

@ -10,6 +10,7 @@ import (
type RoomServerEventsCache interface {
GetRoomServerEvent(eventNID types.EventNID) (*gomatrixserverlib.Event, bool)
StoreRoomServerEvent(eventNID types.EventNID, event *gomatrixserverlib.Event)
InvalidateRoomServerEvent(eventNID types.EventNID)
}
func (c Caches) GetRoomServerEvent(eventNID types.EventNID) (*gomatrixserverlib.Event, bool) {
@ -19,3 +20,7 @@ func (c Caches) GetRoomServerEvent(eventNID types.EventNID) (*gomatrixserverlib.
func (c Caches) StoreRoomServerEvent(eventNID types.EventNID, event *gomatrixserverlib.Event) {
c.RoomServerEvents.Set(int64(eventNID), event)
}
func (c Caches) InvalidateRoomServerEvent(eventNID types.EventNID) {
c.RoomServerEvents.Unset(int64(eventNID))
}

View File

@ -567,6 +567,7 @@ func (d *EventDatabase) events(
if !redactionsArePermanent {
d.applyRedactions(results)
}
return results, nil
}
eventJSONs, err := d.EventJSONTable.BulkSelectEventJSON(ctx, txn, eventNIDs)
if err != nil {
@ -578,8 +579,9 @@ func (d *EventDatabase) events(
}
for _, eventJSON := range eventJSONs {
redacted := gjson.GetBytes(eventJSON.EventJSON, "unsigned.redacted_because").Exists()
events[eventJSON.EventNID], err = gomatrixserverlib.NewEventFromTrustedJSONWithEventID(
eventIDs[eventJSON.EventNID], eventJSON.EventJSON, false, roomInfo.RoomVersion,
eventIDs[eventJSON.EventNID], eventJSON.EventJSON, redacted, roomInfo.RoomVersion,
)
if err != nil {
return nil, err
@ -1020,7 +1022,9 @@ func (d *EventDatabase) MaybeRedactEvent(
return fmt.Errorf("d.RedactionsTable.MarkRedactionValidated: %w", err)
}
d.Cache.StoreRoomServerEvent(redactedEvent.EventNID, redactedEvent.Event)
// We remove the entry from the cache, as if we just "StoreRoomServerEvent", we can't be
// certain that the cached entry actually is updated, since ristretto is eventual-persistent.
d.Cache.InvalidateRoomServerEvent(redactedEvent.EventNID)
return nil
})