From 18ddda383220e58f5d053ae7cd8d345f05339388 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Wed, 3 Aug 2022 16:22:59 +0100 Subject: [PATCH] Return stubby caches anyway --- roomserver/internal/input/input_events.go | 2 -- .../internal/input/input_latest_events.go | 10 ++++------ roomserver/storage/shared/room_updater.go | 19 +++++++++---------- roomserver/storage/shared/storage.go | 4 ++-- roomserver/types/types.go | 1 - 5 files changed, 15 insertions(+), 21 deletions(-) diff --git a/roomserver/internal/input/input_events.go b/roomserver/internal/input/input_events.go index 6640f6be1..01d4a4622 100644 --- a/roomserver/internal/input/input_events.go +++ b/roomserver/internal/input/input_events.go @@ -337,12 +337,10 @@ func (r *Inputer) processRoomEvent( // Request the room info again — it's possible that the room has been // created by now if it didn't exist already. - logrus.Printf("Room info before: %+v", roomInfo) roomInfo, err = r.DB.RoomInfo(ctx, event.RoomID()) if err != nil { return fmt.Errorf("updater.RoomInfo: %w", err) } - logrus.Printf("Room info after: %+v", roomInfo) if roomInfo == nil { return fmt.Errorf("updater.RoomInfo missing for room %s", event.RoomID()) } diff --git a/roomserver/internal/input/input_latest_events.go b/roomserver/internal/input/input_latest_events.go index 7446f9e6f..70eb71119 100644 --- a/roomserver/internal/input/input_latest_events.go +++ b/roomserver/internal/input/input_latest_events.go @@ -89,11 +89,9 @@ func (r *Inputer) updateLatestEvents( // Since it's entirely possible that this types.RoomInfo came from the // cache, we should make sure to update that entry so that the next run // works from live data. - defer func() { - if succeeded { - u.roomInfo.Update(u.newStateNID, len(u.latest) == 0) - } - }() + if u.newStateNID != u.oldStateNID { + roomInfo.Update(u.newStateNID, len(u.latest) == 0) + } succeeded = true return @@ -145,7 +143,7 @@ func (u *latestEventsUpdater) doUpdateLatestEvents() error { // that we knew about before matter anymore. u.oldLatest = types.StateAtEventAndReferences{} if !u.rewritesState { - u.oldStateNID = u.updater.CurrentStateSnapshotNID() + u.oldStateNID = u.roomInfo.StateSnapshotNID() u.oldLatest = u.updater.LatestEvents() } diff --git a/roomserver/storage/shared/room_updater.go b/roomserver/storage/shared/room_updater.go index 4497b58c4..85b86b311 100644 --- a/roomserver/storage/shared/room_updater.go +++ b/roomserver/storage/shared/room_updater.go @@ -11,12 +11,11 @@ import ( type RoomUpdater struct { transaction - d *Database - roomInfo *types.RoomInfo - latestEvents []types.StateAtEventAndReference - lastEventIDSent string - currentStateSnapshotNID types.StateSnapshotNID - roomExists bool + d *Database + roomInfo *types.RoomInfo + latestEvents []types.StateAtEventAndReference + lastEventIDSent string + roomExists bool } func rollback(txn *sql.Tx) { @@ -34,11 +33,11 @@ func NewRoomUpdater(ctx context.Context, d *Database, txn *sql.Tx, roomInfo *typ // succeed, processing a create event which creates the room, or it won't. if roomInfo == nil { return &RoomUpdater{ - transaction{ctx, txn}, d, nil, nil, "", 0, false, + transaction{ctx, txn}, d, nil, nil, "", false, }, nil } - eventNIDs, lastEventNIDSent, currentStateSnapshotNID, err := + eventNIDs, lastEventNIDSent, _, err := d.RoomsTable.SelectLatestEventsNIDsForUpdate(ctx, txn, roomInfo.RoomNID) if err != nil { rollback(txn) @@ -58,7 +57,7 @@ func NewRoomUpdater(ctx context.Context, d *Database, txn *sql.Tx, roomInfo *typ } } return &RoomUpdater{ - transaction{ctx, txn}, d, roomInfo, stateAndRefs, lastEventIDSent, currentStateSnapshotNID, true, + transaction{ctx, txn}, d, roomInfo, stateAndRefs, lastEventIDSent, true, }, nil } @@ -100,7 +99,7 @@ func (u *RoomUpdater) LastEventIDSent() string { // CurrentStateSnapshotNID implements types.RoomRecentEventsUpdater func (u *RoomUpdater) CurrentStateSnapshotNID() types.StateSnapshotNID { - return u.currentStateSnapshotNID + return u.roomInfo.StateSnapshotNID() } // StorePreviousEvents implements types.RoomRecentEventsUpdater - This must be called from a Writer diff --git a/roomserver/storage/shared/storage.go b/roomserver/storage/shared/storage.go index 0211f6737..c33013773 100644 --- a/roomserver/storage/shared/storage.go +++ b/roomserver/storage/shared/storage.go @@ -157,7 +157,7 @@ func (d *Database) RoomInfo(ctx context.Context, roomID string) (*types.RoomInfo func (d *Database) roomInfo(ctx context.Context, txn *sql.Tx, roomID string) (*types.RoomInfo, error) { roomInfo, ok := d.Cache.GetRoomInfo(roomID) - if ok && roomInfo != nil && !roomInfo.IsStub() && roomInfo.RoomNID != 0 { + if ok && roomInfo != nil { // The data that's in the cache is not stubby, so return it. return roomInfo, nil } @@ -694,7 +694,7 @@ func (d *Database) storeEvent( succeeded := false if updater == nil { var roomInfo *types.RoomInfo - roomInfo, err = d.roomInfo(ctx, txn, event.RoomID()) + roomInfo, err = d.roomInfo(ctx, nil, event.RoomID()) if err != nil { return 0, 0, types.StateAtEvent{}, nil, "", fmt.Errorf("d.RoomInfo: %w", err) } diff --git a/roomserver/types/types.go b/roomserver/types/types.go index a05e94fe0..9ceaa278a 100644 --- a/roomserver/types/types.go +++ b/roomserver/types/types.go @@ -302,7 +302,6 @@ func (r *RoomInfo) IsStub() bool { func (r *RoomInfo) Update(nid StateSnapshotNID, isStub bool) { r.mu.Lock() defer r.mu.Unlock() - r.stateSnapshotNID = nid r.isStub = isStub }